To run a shell command when your GNOME extension's button is pressed, you need to connect a signal handler to the toggled signal of your QuickToggle class. This signal is emitted when the toggle button is clicked. Inside the handler, you can use Gio.Subprocess or GLib.spawn_command_line_async to execute your shell command.
Here’s how you can modify your code to achieve this:
javascriptimport GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import GLib from 'gi://GLib'; import * as Main from 'resource:///org/gnome/shell/ui/main.js'; import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js'; import {QuickToggle, SystemIndicator} from 'resource:///org/gnome/shell/ui/quickSettings.js'; const ExampleToggle = GObject.registerClass( class ExampleToggle extends QuickToggle { _init() { super._init({ title: _('Split VPN'), iconName: 'network-vpn-symbolic', toggleMode: true, }); // Connect the 'toggled' signal to your handler this.connect('toggled', () => this._onToggled()); } _onToggled() { // Replace with your shell command const command = 'your-shell-command-here'; try { // Run the command asynchronously GLib.spawn_command_line_async(command); log(`Command executed: ${command}`); } catch (e) { logError(e, `Failed to execute command: ${command}`); } } }); const ExampleIndicator = GObject.registerClass( class ExampleIndicator extends SystemIndicator { _init() { super._init(); this._indicator = this._addIndicator(); this._indicator.iconName = 'network-vpn-symbolic'; const toggle = new ExampleToggle(); toggle.bind_property('checked', this._indicator, 'visible', GObject.BindingFlags.SYNC_CREATE); this.quickSettingsItems.push(toggle); } }); export default class QuickSettingsExampleExtension extends Extension { enable() { this._indicator = new ExampleIndicator(); Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator); } disable() { this._indicator.quickSettingsItems.forEach(item => item.destroy()); this._indicator.destroy(); this._indicator = null; } }
toggled signal is connected to the _onToggled method in the ExampleToggle class. This method is called every time the toggle button is pressed._onToggled, GLib.spawn_command_line_async is used to run the shell command asynchronously. Replace 'your-shell-command-here' with the actual command you want to run.log() and logError() to debug and check if the command is being executed as expected.