I’d like to create a minimal extension that has a simple toggle button without menu. I simply want the icon that appears in the gnome panel to directly be a toggle button.
I found plenty of examples involving toggle buttons within submenus, but I can’t figure out how to register a click handler on the icon directly.
Any ideas?
const { GObject, St } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const _ = ExtensionUtils.gettext;
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init() {
super._init(0.0, _('My Shiny Indicator'));
const enabledIcon = new St.Icon({
icon_name: 'face-smile-symbolic',
style_class: 'system-status-icon',
});
const disabledIcon = new St.Icon({
icon_name: 'face-sad-symbolic',
style_class: 'system-status-icon',
});
this.add_child(enabledIcon);
// How can I do soemthing like this?
let toggleStatus = true
function onIconClick() {
toggleStatus = !toggleStatus
if (toggleStatus) {
Main.notify('Toggle has been turned on!');
this.remove_child(disabledIcon);
this.add_child(enabledIcon);
} else {
Main.notify('Toggle has been turned off!');
this.remove_child(enabledIcon);
this.add_child(disabledIcon);
}
Main.notify('The new toggle status is: ' + toggleStatus);
}
enabledIcon.on('click', onIconClick)
disabledIcon.on('click', onIconClick)
}
});
class Extension {
constructor(uuid) {
this._uuid = uuid;
}
enable() {
this._indicator = new Indicator();
Main.panel.addToStatusArea(this._uuid, this._indicator);
}
disable() {
this._indicator.destroy();
this._indicator = null;
}
}
function init(meta) {
return new Extension(meta.uuid);
}