Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LedControlMenu extends QuickSettings.QuickMenuToggle {
title: _('Led Control'),
subtitle: _('Led On'),
iconName: 'keyboard-brightness-high-symbolic',
toggleMode: false,
toggleMode: true,
});

this._indicator = indicator;
Expand All @@ -58,17 +58,7 @@ class LedControlMenu extends QuickSettings.QuickMenuToggle {
menuItem.actor.add_child(box);

menuItem.connect('activate', () => {
const previousIndex = this._currentCheckedIndex;
this._setPendingState(item.icon, _('Applying...'));
this._runHelperCommand([item.action]).then(() => {
this._setState(index, item.icon, item.label.trim(), menuItem);
}).catch((error) => {
if (!error?.handled) {
Main.notify(_('Error'), _('Could not run the command.'));
}
console.error('Error running the command:', error);
this._setState(previousIndex, this._menuItems[previousIndex].icon, this._menuItems[previousIndex].label.trim());
});
this._applyAction(index, menuItem);
});

this._itemsSection.addMenuItem(menuItem);
Expand All @@ -84,7 +74,9 @@ class LedControlMenu extends QuickSettings.QuickMenuToggle {
morseItem.visible = Main.sessionMode.allowSettings;

this._currentCheckedIndex = 1;
this._busy = false;
this._applySavedState();
this.connect('clicked', () => this._toggleFromButton());
}

/**
Expand Down Expand Up @@ -264,13 +256,50 @@ class LedControlMenu extends QuickSettings.QuickMenuToggle {
this._setState(index, item.icon, item.label.trim());
}

_applyAction(index, menuItem = null) {
if (this._busy) {
return;
}

const item = this._menuItems[index];
if (!item) {
return;
}

const previousIndex = this._currentCheckedIndex;
const previousItem = this._menuItems[previousIndex];

this._busy = true;
this._setPendingState(item.icon, _('Applying...'));

this._runHelperCommand([item.action]).then(() => {
this._setState(index, item.icon, item.label.trim(), menuItem);
this._busy = false;
}).catch((error) => {
if (!error?.handled) {
Main.notify(_('Error'), _('Could not run the command.'));
}
console.error('Error running the command:', error);
if (previousItem) {
this._setState(previousIndex, previousItem.icon, previousItem.label.trim());
}
this._busy = false;
});
}

_toggleFromButton() {
const targetIndex = this._currentCheckedIndex === 0 ? 1 : 0;
this._applyAction(targetIndex);
}


/**
* Updates the check state of the menu items to indicate which option is currently active.
* @param {number} checkedIndex - The index of the currently selected menu item.
*/
_updateCheckState(checkedIndex) {
this._currentCheckedIndex = checkedIndex;
this.checked = this._currentCheckedIndex !== 0;
this._itemsSection._getMenuItems().forEach((menuItem, index) => {
menuItem._tick.visible = (index === this._currentCheckedIndex);
});
Expand Down