From 7e93edb599cd43a24d247be5cfd3c81ee57e5fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20G=C3=BCnther?= Date: Thu, 19 Dec 2024 09:23:42 +0100 Subject: [PATCH 1/4] added a dynamic check for button state --- .gitignore | 1 + extension.js | 163 +++++++++++++++--- prefs.js | 23 ++- ...tensions.custom-command-toggle.gschema.xml | 64 ++++++- 4 files changed, 226 insertions(+), 25 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22aab6f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +schemas/*.compiled diff --git a/extension.js b/extension.js index 1cc94f5..61c8971 100644 --- a/extension.js +++ b/extension.js @@ -513,41 +513,162 @@ export default class CustomQuickToggleExtension extends Extension { buttonClick5 = this._settings.get_int('buttonclick5-setting'); buttonClick6 = this._settings.get_int('buttonclick6-setting'); + function checkDynamicState(command, callback) { + try { + let [success, pid] = GLib.spawn_async( + null, + ["/bin/bash", "-c", command], + null, + GLib.SpawnFlags.SEARCH_PATH | + GLib.SpawnFlags.DO_NOT_REAP_CHILD | + GLib.SpawnFlags.STDOUT_TO_DEV_NULL, + null, + ); + + if (success) { + GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, (pid, status) => { + try { + let result = status === 0; // Assuming exit code 0 means true + callback(result); + } catch (e) { + console.log(`Error in dynamic state callback: ${e}`); + callback(false); + } + }); + } else { + console.log("Failed to spawn command"); + callback(false); + } + } catch (e) { + console.log(`Error checking dynamic state: ${e}`); + callback(false); + } + } + switch (initialState1) { - case 0: toggleState1 = true; this._settings.set_boolean('togglestate1-setting', true); break; - case 1: toggleState1 = false; this._settings.set_boolean('togglestate1-setting', false); break; - case 2: toggleState1 = this._settings.get_boolean('togglestate1-setting'); break; + case 0: + toggleState1 = true; + break; + case 1: + toggleState1 = false; + break; + case 2: + toggleState1 = this._settings.get_boolean("togglestate1-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand1-setting"); + checkDynamicState(cmd, (result) => { + toggleState1 = result; + this._indicator1.toggle1.checked = result; + }); + break; } + switch (initialState2) { - case 0: toggleState2 = true; this._settings.set_boolean('togglestate2-setting', true); break; - case 1: toggleState2 = false; this._settings.set_boolean('togglestate2-setting', false); break; - case 2: toggleState2 = this._settings.get_boolean('togglestate2-setting'); break; + case 0: + toggleState2 = true; + this._settings.set_boolean("togglestate2-setting", true); + break; + case 1: + toggleState2 = false; + this._settings.set_boolean("togglestate2-setting", false); + break; + case 2: + toggleState2 = this._settings.get_boolean("togglestate2-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand2-setting"); + checkDynamicState(cmd, (result) => { + toggleState2 = result; + this._indicator2.toggle1.checked = result; + }); + break; } switch (initialState3) { - case 0: toggleState3 = true; this._settings.set_boolean('togglestate3-setting', true); break; - case 1: toggleState3 = false; this._settings.set_boolean('togglestate3-setting', false); break; - case 2: toggleState3 = this._settings.get_boolean('togglestate3-setting'); break; + case 0: + toggleState3 = true; + this._settings.set_boolean("togglestate3-setting", true); + break; + case 1: + toggleState3 = false; + this._settings.set_boolean("togglestate3-setting", false); + break; + case 2: + toggleState3 = this._settings.get_boolean("togglestate3-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand3-setting"); + checkDynamicState(cmd, (result) => { + toggleState3 = result; + this._indicator3.toggle1.checked = result; + }); + break; } switch (initialState4) { - case 0: toggleState4 = true; this._settings.set_boolean('togglestate4-setting', true); break; - case 1: toggleState4 = false; this._settings.set_boolean('togglestate4-setting', false); break; - case 2: toggleState4 = this._settings.get_boolean('togglestate4-setting'); break; + case 0: + toggleState4 = true; + this._settings.set_boolean("togglestate4-setting", true); + break; + case 1: + toggleState4 = false; + this._settings.set_boolean("togglestate4-setting", false); + break; + case 2: + toggleState4 = this._settings.get_boolean("togglestate4-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand4-setting"); + checkDynamicState(cmd, (result) => { + toggleState4 = result; + this._indicator4.toggle1.checked = result; + }); + break; } switch (initialState5) { - case 0: toggleState5 = true; this._settings.set_boolean('togglestate5-setting', true); break; - case 1: toggleState5 = false; this._settings.set_boolean('togglestate5-setting', false); break; - case 2: toggleState5 = this._settings.get_boolean('togglestate5-setting'); break; + case 0: + toggleState5 = true; + this._settings.set_boolean("togglestate5-setting", true); + break; + case 1: + toggleState5 = false; + this._settings.set_boolean("togglestate5-setting", false); + break; + case 2: + toggleState5 = this._settings.get_boolean("togglestate5-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand5-setting"); + checkDynamicState(cmd, (result) => { + toggleState5 = result; + this._indicator5.toggle1.checked = result; + }); + break; } switch (initialState6) { - case 0: toggleState6 = true; this._settings.set_boolean('togglestate6-setting', true); break; - case 1: toggleState6 = false; this._settings.set_boolean('togglestate6-setting', false); break; - case 2: toggleState6 = this._settings.get_boolean('togglestate6-setting'); break; + case 0: + toggleState6 = true; + this._settings.set_boolean("togglestate6-setting", true); + break; + case 1: + toggleState6 = false; + this._settings.set_boolean("togglestate6-setting", false); + break; + case 2: + toggleState6 = this._settings.get_boolean("togglestate6-setting"); + break; + case 3: + let cmd = this._settings.get_string("checkcommand6-setting"); + checkDynamicState(cmd, (result) => { + toggleState6 = result; + this._indicator6.toggle1.checked = result; + }); + break; } - + refreshIndicator.call(this); - - // Run commands at boot + + // Run commands at boot let toggleStates = [ toggleState1, toggleState2, toggleState3, toggleState4, toggleState5, toggleState6 ]; for (let i = 1; i <= numToggleButtons; i++) { const runAtBootSetting = this._settings.get_boolean(`runcommandatboot${i}-setting`); diff --git a/prefs.js b/prefs.js index 4cd5ecb..f732078 100644 --- a/prefs.js +++ b/prefs.js @@ -100,7 +100,7 @@ export default class CustomCommandTogglePreferences extends ExtensionPreferences const optionList = new Gtk.StringList(); [_('On'), _('Off'), _('Previous state')].forEach(choice => optionList.append(choice)); - + const comboRow = new Adw.ComboRow({ title: _('Initial State'), subtitle: _('State of the toggle button at login/startup'), @@ -108,7 +108,26 @@ export default class CustomCommandTogglePreferences extends ExtensionPreferences selected: window._settings.get_int(`initialtogglestate${pageIndex}-setting`), }); group3.add(comboRow); - + + const checkCommandRow = new Adw.EntryRow({ + title: _("Check State Command:"), + }); + checkCommandRow.visible = comboRow.selected === 3; + group3.add(checkCommandRow); + + // Show/hide check fields based on selected option + comboRow.connect("notify::selected", () => { + checkCommandRow.visible = comboRow.selected === 3; + }); + + // Add bindings + window._settings.bind( + `checkcommand${pageIndex}-setting`, + checkCommandRow, + "text", + Gio.SettingsBindFlags.DEFAULT, + ); + const expanderRow = new Adw.ExpanderRow({ title: _('Run Command at Startup'), subtitle: _('Run associated toggle command at login/startup'), diff --git a/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml b/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml index 4481781..8dc54b6 100644 --- a/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml +++ b/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml @@ -24,6 +24,16 @@ false + + '' + Check state command for button 1 + Command to check current state for button 1 + + + '' + Check state regex for button 1 + Regex pattern to match for ON state for button 1 + false @@ -61,6 +71,16 @@ false + + '' + Check state command for button 2 + Command to check current state for button 2 + + + '' + Check state regex for button 2 + Regex pattern to match for ON state for button 2 + false @@ -98,12 +118,22 @@ false + + '' + Check state command for button 2 + Command to check current state for button 2 + + + '' + Check state regex for button 2 + Regex pattern to match for ON state for button 2 + false 3 - + true @@ -112,7 +142,7 @@ false - + @@ -135,6 +165,16 @@ false + + '' + Check state command for button 2 + Command to check current state for button 2 + + + '' + Check state regex for button 2 + Regex pattern to match for ON state for button 2 + false @@ -172,6 +212,16 @@ false + + '' + Check state command for button 2 + Command to check current state for button 2 + + + '' + Check state regex for button 2 + Regex pattern to match for ON state for button 2 + false @@ -209,6 +259,16 @@ false + + '' + Check state command for button 2 + Command to check current state for button 2 + + + '' + Check state regex for button 2 + Regex pattern to match for ON state for button 2 + false From 6141342b63116d9daeb9bbf902778a88396db66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20G=C3=BCnther?= Date: Thu, 19 Dec 2024 09:24:30 +0100 Subject: [PATCH 2/4] rm comments --- prefs.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/prefs.js b/prefs.js index f732078..eeaac37 100644 --- a/prefs.js +++ b/prefs.js @@ -115,12 +115,10 @@ export default class CustomCommandTogglePreferences extends ExtensionPreferences checkCommandRow.visible = comboRow.selected === 3; group3.add(checkCommandRow); - // Show/hide check fields based on selected option comboRow.connect("notify::selected", () => { checkCommandRow.visible = comboRow.selected === 3; }); - // Add bindings window._settings.bind( `checkcommand${pageIndex}-setting`, checkCommandRow, From 580bd8ff8769a8ffdb9b4d4dff1e41c70a245fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20G=C3=BCnther?= Date: Thu, 19 Dec 2024 11:31:02 +0100 Subject: [PATCH 3/4] added option for checking the state --- prefs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prefs.js b/prefs.js index eeaac37..f56abba 100644 --- a/prefs.js +++ b/prefs.js @@ -99,7 +99,7 @@ export default class CustomCommandTogglePreferences extends ExtensionPreferences groups.push(group3); const optionList = new Gtk.StringList(); - [_('On'), _('Off'), _('Previous state')].forEach(choice => optionList.append(choice)); + [_('On'), _('Off'), _('Previous state'), _('Check state dynamically')].forEach(choice => optionList.append(choice)); const comboRow = new Adw.ComboRow({ title: _('Initial State'), From db28eb9928167727bf14e9d7184d4bd799ab8af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20G=C3=BCnther?= Date: Thu, 19 Dec 2024 11:31:13 +0100 Subject: [PATCH 4/4] removed code stub --- ...tensions.custom-command-toggle.gschema.xml | 37 +++---------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml b/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml index 8dc54b6..8e4dd65 100644 --- a/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml +++ b/schemas/org.gnome.shell.extensions.custom-command-toggle.gschema.xml @@ -1,7 +1,7 @@ - + 1 @@ -29,11 +29,6 @@ Check state command for button 1 Command to check current state for button 1 - - '' - Check state regex for button 1 - Regex pattern to match for ON state for button 1 - false @@ -76,11 +71,6 @@ Check state command for button 2 Command to check current state for button 2 - - '' - Check state regex for button 2 - Regex pattern to match for ON state for button 2 - false @@ -95,7 +85,7 @@ false - + @@ -170,11 +160,6 @@ Check state command for button 2 Command to check current state for button 2 - - '' - Check state regex for button 2 - Regex pattern to match for ON state for button 2 - false @@ -189,7 +174,7 @@ false - + @@ -217,11 +202,6 @@ Check state command for button 2 Command to check current state for button 2 - - '' - Check state regex for button 2 - Regex pattern to match for ON state for button 2 - false @@ -239,7 +219,7 @@ - + 'notify-send "Custom Command Toggle" "Button 6 toggled on"' @@ -264,11 +244,6 @@ Check state command for button 2 Command to check current state for button 2 - - '' - Check state regex for button 2 - Regex pattern to match for ON state for button 2 - false @@ -283,10 +258,10 @@ false - + - +