/* extension.js * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * SPDX-License-Identifier: GPL-3.0-or-later */ /* Based on https://stackoverflow.com/questions/33001192/how-to-send-a-string-to-a-gnome-shell-extension */ // https://github.com/bananenfisch/RecentItems/blob/master/extension.js // https://github.com/julio641742/gnome-shell-extension-reference/blob/master/tutorials/POPUPMENU-EXTENSION.md // https://gjs-docs.gnome.org/st10~1.0_api/st.widget const St = imports.gi.St; const Gio = imports.gi.Gio; const Lang = imports.lang; const GLib = imports.gi.GLib const Main = imports.ui.main; const Mainloop = imports.mainloop; const clutter = imports.gi.Clutter; const PanelMenu = imports.ui.panelMenu; const PopupMenu = imports.ui.popupMenu; const GObject = imports.gi.GObject; const Gtk = imports.gi.Gtk; const Pixbuf = imports.gi.GdkPixbuf; const Cogl = imports.gi.Cogl; var MyPopupMenuItem = GObject.registerClass({ GTypeName: "MyPopupMenuItem" }, class MyPopupMenuItem extends PopupMenu.PopupBaseMenuItem { _init(gicon, text, params) { super._init(params); this.box = new St.BoxLayout({ style_class: 'popup-combobox-item' }); this.box.set_vertical(true); this._img = new clutter.Image(); let initial_pixbuf = Pixbuf.Pixbuf.new_from_file('/tmp/cat2.jpg'); this._img.set_data(initial_pixbuf.get_pixels(), initial_pixbuf.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888, initial_pixbuf.get_width(), initial_pixbuf.get_height(), initial_pixbuf.get_rowstride()); this._icon = new clutter.Actor(); this._icon.set_content(this._img); this._icon.set_size(150,//initial_pixbuf.get_width(), 300);//initial_pixbuf.get_height()); this.box.add(this._icon); // if (gicon) // this.icon = new St.Icon({ gicon: gicon, style_class: 'popup-menu-icon' }); // else // this.icon = new St.Icon({ icon_name: 'edit-clear-symbolic', icon_size: 22 }); // this.box.add(this._icon); this.label = new St.Label({ text: " " + text }); this.box.add(this.label); this.add_child(this.box); } }); var RecentItems = GObject.registerClass({ GTypeName: "RecentItems" }, class RecentItems extends PanelMenu.Button { _init() { super._init(0.0); this.connect('enter-event', this._onEnter.bind(this)); this.connect('leave-event', this._onLeave.bind(this)); this._iconActor = new St.Icon({ icon_name: 'document-open-recent-symbolic', style_class: 'system-status-icon' }); this.actor.add_actor(this._iconActor); this.actor.add_style_class_name('panel-status-button'); Main.panel.addToStatusArea('recent-items', this); let menuItem = new MyPopupMenuItem('', 'hello', {}); this.menu.addMenuItem(menuItem); } _onEnter() { this.menu.open(true); } _onLeave() { this.menu.close(); } }); var MonitorWidget = GObject.registerClass({ GTypeName: "MonitorWidget" }, class MonitorWidget extends PanelMenu.Button { _init(item, group, lastWidget) { super._init(0.0); this.name = item['name']; this.group = group; if (item.hasOwnProperty('icon')) { this.icon = this._createIcon(icon, iconStyle); this.add_child(this.icon); } else this.icon = null; if (item.hasOwnProperty('text')) { this.widget = this._createText(text, style); this.add_child(this.widget); } else this.widget = null; if (item.hasOwnProperty('popup')) this._createPopup(item['popup']); if (item.hasOwnProperty('on-click')) onClick = item['on-click']; let box = 'center'; if (item.hasOwnProperty('box')) box = item['box']; this.onClick = onClick; switch(box) { case 'left': this.box = Main.panel._leftBox; break; case 'right': this.box = Main.panel._rigthBox; break; default: case 'center': this.box = Main.panel._centerBox; break; } // Don't know why, _rightBox seems undefined on shell 3.36 !! if (this.box === undefined) { log(`${box} box is undefined, falling back to center one`); this.box = Main.panel._centerBox; } let menuItem = new MyPopupMenuItem('', 'hello', {}); this.menu.addMenuItem(menuItem); this.connect('enter-event', this._onEnter.bind(this)); this.connect('leave-event', this._onLeave.bind(this)); Main.panel.addToStatusArea(group, this, (lastWidget>0)?lastWidget:-1, box); } _onEnter() { this.menu.open(true); } _onLeave() { this.menu.close(); } _createText(text, style) { this.style = style; if (text === '') { return null; } else { widget = new St.Button({ label: text }); widget.set_style(this.style); return widget; } } _createIcon(icon, style) { this.iconStyle = style; if (icon === '') { return null; } else { let gicon = Gio.icon_new_for_string(icon); gicon = new St.Icon({ gicon }); gicon.set_style(this.iconStyle); return gicon; } } removeFromBox() { if (this.widget) this.box.remove_child(this.widget); if (this.icon) this.box.remove_child(this.icon); } update(text, style, icon, iconStyle) { let prevWidget = this.widget; let prevIcon = this.icon; if (text !== '') { if (!this.widget) { this._createText(text, style); this.box.insert_child_above(this.widget, this.icon); } else { this.widget.label = text; } } if (style !== '' && this.widget) { this.style = style; this.widget.set_style(this.style); } if (icon !== '') { this._createIcon(icon, iconStyle); if (prevIcon) this.box.insert_child_above(this.icon, prevIcon); else this.box.insert_child_before(this.icon, prevWidget); } if (iconStyle !== '' && this.icon) { this.iconStyle = style; this.icon.set_style(this.iconStyle); } if (prevIcon && icon !== '') this.box.remove_child(prevIcon); } }); // From https://github.com/ubuntu/gnome-shell-extension-appindicator/blob/master/interfaces.js // loads a xml file into an in-memory string function loadInterfaceXml(filename) { let extension = imports.misc.extensionUtils.getCurrentExtension(); let interfacesDir = extension.dir.get_child('.'); let file = interfacesDir.get_child(filename); let [result, contents] = imports.gi.GLib.file_get_contents(file.get_path()); if (result) { // HACK: The "" + trick is important as hell because file_get_contents returns // an object (WTF?) but Gio.makeProxyWrapper requires `typeof() == "string"` // Otherwise, it will try to check `instanceof XML` and fail miserably because there // is no `XML` on very recent SpiderMonkey releases (or, if SpiderMonkey is old enough, // will spit out a TypeError soon). if (contents instanceof Uint8Array) contents = imports.byteArray.toString(contents); let res = `${contents}`; return res; } else { throw new Error(`Generic monitor: Could not load file: ${filename}`); } } class GenericMonitorDBUS { constructor() { this.monitor_groups = {}; this.actor_clicked = {}; this.clutterSettings = clutter.Settings.get_default(); this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(loadInterfaceXml('dbus.xml'), this); this._dbusImpl.export(Gio.DBus.session, '/com/soutade/GenericMonitor'); this._dbusImpl.emit_signal('onActivate', null); } _checkParmeters(parameters) { if (!parameters.hasOwnProperty('group')) throw new Error('No group defined'); if (!parameters.hasOwnProperty('items')) throw new Error('No items defined'); for (let itemIndex in parameters['items']) { let item = parameters['items'][itemIndex]; if (!item.hasOwnProperty('name')) throw new Error('No name defined for item'); // if (!item.hasOwnProperty('text') && !item.hasOwnProperty('icon')) // throw new Error('No text not icon defined for item'); // if (item.hasOwnProperty('on-click')) { // if (item['on-click'] !== 'signal' && item['on-click'] !== 'delete') // throw new Error('Invalid on-click value'); // } // if (item.hasOwnProperty('box')) { // if (item['box'] !== 'left' && item['box'] !== 'center' && item['box'] !== 'right') // throw new Error('Invalid box value'); // } } } _getItemFromGroup(group, name) { for (let groupItemIndex in group) { let groupItem = group[groupItemIndex]; if (groupItem.name === name) return groupItem; } return null; } _doClickCallback() { for(let itemIndex in this.actor_clicked) { let item = this.actor_clicked[itemIndex]; let right = ''; let nbClicks = ''; if (item['button'] == 3) right = 'Right'; if (item['nbClicks'] > 1) nbClicks = 'Dbl'; let signalName = 'on' + right + nbClicks + 'Click'; this._dbusImpl.emit_signal(signalName, GLib.Variant.new('(s)',[item['name']])); } this.actor_clicked = {} } _actorToMonitorWidget(actor) { for (let groupName in this.monitor_groups) { let group = this.monitor_groups[groupName]; for (let itemIndex in group) { let item = group[itemIndex]; if (item.widget === actor || item.icon === actor) return [groupName, item]; } } return null; } _removeFromArray(array, value) { for(let i=0; i