17 Commits
v3 ... v12

Author SHA1 Message Date
b1feb004e8 Update metadata.json 2022-10-28 17:20:14 +02:00
73021a25a1 Update README 2022-10-28 08:09:34 +02:00
4c73986596 Updates for GNOME 43 2022-10-27 17:54:37 +02:00
d3665f2ff0 Quick update for GNOME 42. There is still some issues with menu. 2022-04-10 14:21:53 +02:00
00fb6120c9 Update README.md and version to be aligned on Gnome store version 2022-01-07 08:35:13 +01:00
02d3e8b72a Remove timer resource during SignalMgt destructor 2022-01-06 09:52:11 +01:00
78254a550e Update support for version 41 in metadata 2022-01-06 09:14:55 +01:00
7a5f506c23 Remove use of deprecated "lang" package 2021-10-01 09:02:34 +02:00
1d3328f71e Update support for version 40 in metadata 2021-09-30 16:33:06 +02:00
45d0cc9d11 Use WeakMap to store objects as key and rename MonitorWidget signals field into signalManager to avoid confusions 2021-09-30 16:32:26 +02:00
4d6fe82790 Update version 2021-02-02 07:04:45 +01:00
9943a9075f Popup text must be labels not buttons 2021-01-31 20:18:11 +01:00
83609b58ab After linter pass 2021-01-29 09:15:40 +01:00
b85ff4a782 Update examples 2021-01-28 10:34:18 +01:00
30bfc796b9 Update README 2021-01-28 10:34:18 +01:00
35e34b52f2 Move signal management into external class. It allows to handle signals for popup nested elements. 2021-01-28 10:34:17 +01:00
1b4430c3ac Update README 2020-11-28 12:14:48 +01:00
7 changed files with 438 additions and 320 deletions

View File

@@ -7,6 +7,10 @@ This GNOME Shell Extension aims to display information to center box. Using DBUS
Installation
------------
Install it from https://extensions.gnome.org/extension/2826/generic-monitor/
OR
Create a symbolic link from your _.local_ directory and enable extension
ln -s $PWD/generic-monitor@gnome-shell-extensions/ ~/.local/share/gnome-shell/extensions/
@@ -67,6 +71,8 @@ DBUS object
<text_object> is defined as :
"text" : {
"name" : "" // Optional, used with popup nested element
<signals_description>, // Optional, used with popup nested element
"text" : "Text to be displayed",
"style" : "CSS style to be applied", // Optional
}
@@ -79,6 +85,8 @@ DBUS object
<picture_object> is defined as :
"picture" : {
"name" : "" // Optional, used with popup nested element
<signals_description>, // Optional, used with popup nested element
"path" : "Icon path",
"width" : XXX, // Optional : Force width in pixels, can be -1 for defaut value
"height" : XXX, // Optional : Force height in pixels, can be -1 for defaut value
@@ -105,11 +113,11 @@ Signals can be :
Targets :
* signal : emit a signal to desktop application
* delete : Delete item
* open-popup : Open the popup if there is one
* close-popup : Close the popup if there is one
* toggle-popup : Toggle (open/close) the popup if there is one
* signal : Emit a signal to desktop application
* delete : Delete item
* open-popup : Open the popup if there is one
* close-popup : Close the popup if there is one
* toggle-popup : Toggle (open/close) the popup if there is one
Signal names emit when action "signal" is specified :
@@ -124,6 +132,8 @@ Signal names emit when action "signal" is specified :
* onScrollDown
Each signal is sent with one parameter : "<itemName>@<groupName>"
For popup nested elements, parameter is "<nestedName>@<itemName>@<groupName>"
where nestedName can be empty if not defined by user
Other signals are available when extension is activated/deactivated :
@@ -136,11 +146,11 @@ Example
You can test it with command line :
gdbus call --session --dest org.gnome.Shell --object-path /com/soutade/GenericMonitor --method com.soutade.GenericMonitor.notify '{"group":"new","items":[{"name":"first","on-click":"toggle-popup","text":"Hello","style":"color:green","popup":{"items":[{"picture":{"path":"/tmp/cat2.jpg"}}]}}]}'
gdbus call --session --dest org.gnome.Shell --object-path /com/soutade/GenericMonitor --method com.soutade.GenericMonitor.notify '{"group":"new","items":[{"name":"first","on-click":"toggle-popup","text":{"text":"Hello","style":"color:green"},"popup":{"items":[{"picture":{"path":"/tmp/cat.jpg"}}]}}]}'
gdbus call --session --dest org.gnome.Shell --object-path /com/soutade/GenericMonitor --method com.soutade.GenericMonitor.deleteGroups '{"groups":["new"]}'
Python examples is available
Python examples are available @ https://indefero.soutade.fr/p/genericmonitor/source/tree/master/examples
Development

View File

@@ -1,47 +1,47 @@
<interface name="com.soutade.GenericMonitor">
<!-- Functions -->
<method name="notify">
<arg type="s" direction="in" />
<arg name="parameters" type="s" direction="in" />
</method>
<method name="deleteItems">
<arg type="s" direction="in" />
<arg name="items" type="s" direction="in" />
</method>
<method name="deleteGroups">
<arg type="s" direction="in" />
<arg name="groups" type="s" direction="in" />
</method>
<method name="openPopup">
<arg type="s" direction="in" />
<arg name="popup" type="s" direction="in" />
</method>
<method name="closePopup">
<arg type="s" direction="in" />
<arg name="popup" type="s" direction="in" />
</method>
<method name="togglePopup">
<arg type="s" direction="in" />
<arg name="popup" type="s" direction="in" />
</method>
<!-- Events -->
<signal name="onClick">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onRightClick">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onDblClick">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onRightDblClick">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onScrollUp">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onScrollDown">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onEnter">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<signal name="onLeave">
<arg type="s" direction="out" />
<arg name="fullName" type="s" direction="out" />
</signal>
<!-- Activate/Deactivate signals -->
<signal name="onActivate">

View File

@@ -235,11 +235,25 @@ class GenericMonitor:
class GenericMonitorGenericWidget:
""" Generic widget class, parent of all widgets
"""
def __init__(self):
self.valuesToMap = []
def __init__(self, style='', name='', signals={}):
"""
Parameters
----------
name : str, optional
Widget name
signals : dictionary, optional
Dictionary of signals and their action
"""
self.valuesToMap = ['name', 'style']
self.mapValues = {}
self.mapName = ''
self.style = style
self.name = name
self.signals = signals
def setStyle(self, style):
self.style = style
def _toMap(self):
""" Return dictionary of class elements to send to addon
"""
@@ -247,12 +261,14 @@ class GenericMonitorGenericWidget:
for p in self.valuesToMap:
if self.__dict__[p]:
self.mapValues[p] = self.__dict__[p]
for (name, value) in self.signals.items():
self.mapValues[name] = value
return {self.mapName:self.mapValues}
class GenericMonitorTextWidget(GenericMonitorGenericWidget):
""" Text widget
"""
def __init__(self, text, style=''):
def __init__(self, text, style='', name='', signals={}):
"""
Parameters
----------
@@ -260,19 +276,20 @@ class GenericMonitorTextWidget(GenericMonitorGenericWidget):
Text to display
style : str, optional
CSS style
name : str, optional
Widget name
signals : dictionary, optional
Dictionary of signals and their action
"""
self.valuesToMap = ('text', 'style')
super().__init__(style, name, signals)
self.valuesToMap += ['text']
self.mapName = 'text'
self.text = text
self.style = style
def setText(self, text):
self.text = text
def setStyle(self, style):
self.style = style
class GenericMonitorIconWidget(GenericMonitorGenericWidget):
""" Icon widget
"""
@@ -285,7 +302,8 @@ class GenericMonitorIconWidget(GenericMonitorGenericWidget):
style : str, optional
CSS style
"""
self.valuesToMap = ('path', 'style')
super().__init__(style=style)
self.valuesToMap += ['path']
self.mapName = 'icon'
self.path = path
@@ -293,15 +311,12 @@ class GenericMonitorIconWidget(GenericMonitorGenericWidget):
def setPath(self, path):
self.path = path
def setStyle(self, style):
self.style = style
class GenericMonitorPictureWidget(GenericMonitorIconWidget):
class GenericMonitorPictureWidget(GenericMonitorGenericWidget):
""" Picture widget
"""
def __init__(self, path, style='', width=-1, height=-1):
def __init__(self, path, style='', width=-1, height=-1, name='', signals={}):
"""
Parameters
----------
@@ -313,15 +328,23 @@ class GenericMonitorPictureWidget(GenericMonitorIconWidget):
Width of displayed picture (-1 for default width)
height : int, optional
Width of displayed picture (-1 for default width)
name : str, optional
Widget name
signals : dictionary, optional
Dictionary of signals and their action
"""
super().__init__(path, style)
self.valuesToMap = ('path', 'style', 'width', 'height')
super().__init__(style, name, signals)
self.valuesToMap += ['path', 'width', 'height']
self.mapName = 'picture'
self.path = path
self.width = width
self.height = height
def setPath(self, path):
self.path = path
def setWidth(self, width):
self.width = width

View File

@@ -19,6 +19,7 @@
Display random picture from unsplash.com in a popup
* Click : open/close popup
* Popup item click : display who clicked
* ScrollUp/ScrollDown/Double click : display next picture
* Right click : exit
'''
@@ -50,12 +51,12 @@ class PicturePopup(GenericMonitor):
with open('/tmp/cat2.jpg', 'wb') as f:
f.write(datatowrite)
widget = GenericMonitorTextWidget('#%d' % self.imgs_idx, 'color:purple')
url_widget = GenericMonitorTextWidget(url, 'color:white;font-weight:bold')
picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg')
url_widget = GenericMonitorTextWidget(url, 'color:white;font-weight:bold', signals={'on-click':'signal'}) # No name here
picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg', name='NestedWidget', signals={'on-click':'signal'})
popup = GenericMonitorPopup([url_widget, picture_widget])
signals = {
'on-click':'toggle-popup',
# Could also use this behavior
# Could also use this behavior [bugged since GNOME 42]
# 'on-enter':'open-popup',
# 'on-leave':'close-popup',
'on-dblclick':'signal',
@@ -68,7 +69,11 @@ class PicturePopup(GenericMonitor):
self.imgs_idx += 1
def _forMe(self, sender):
return sender == self.item.getFullName()
return str(sender).endswith(self.item.getFullName())
def onClick(self, sender):
if not self._forMe(sender): return
print('Click from {}'.format(sender))
def _onScroll(self, sender):
if not self._forMe(sender): return

View File

@@ -65,7 +65,7 @@ class TimerThread(Thread,GenericMonitor):
self._stopLoop = False
self.textWidget = GenericMonitorTextWidget('')
signals = {'on-click':'signal'}
signals = {'on-click':'signal','on-dblclick':'signal','on-rightclick':'signal'}
self.monitorItem = GenericMonitorItem('timer', [self.textWidget], signals, box='right')
self.monitorGroup = GenericMonitorGroup('Timer', self.monitorItem)

View File

@@ -24,14 +24,13 @@
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
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/master/js/ui/panelMenu.js
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/master/js/ui/popupMenu.js
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/master/js/ui/panel.js
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/panelMenu.js
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/popupMenu.js
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/panel.js
*/
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;
@@ -55,69 +54,21 @@ function log(message) {
}
var MyPopupMenuItem = GObject.registerClass({
GTypeName: "MyPopupMenuItem"
},
class MyPopupMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(widgets, params) {
super._init(params);
class SignalMgt {
this.box = new St.BoxLayout({ style_class: 'popup-combobox-item' });
this.box.set_vertical(true);
for (let widgetIndex in widgets)
this.box.add(widgets[widgetIndex]);
this.add_child(this.box);
}
});
var MonitorWidget = GObject.registerClass({
GTypeName: "MonitorWidget"
},
class MonitorWidget extends PanelMenu.Button {
_init(item, group, dbus, position) {
super._init(0.0);
this.name = item['name'];
constructor(item, name, group, dbus, buttonMenu) {
this.name = name;
this.group = group;
this.fullname = this.name + '@' + this.group;
this.dbus = dbus;
this.signals = {}
this.buttonMenu = buttonMenu;
this.signals = new WeakMap();
this.widgets = new Array();
this.timeouts = new Array();
if (item.hasOwnProperty('icon'))
{
if (typeof(item['icon']) === "string")
this.icon = this._createIconOld(item);
else
this.icon = this._createIcon(item['icon']);
if (this.icon !== null) {
this._connectWidgetSignals(this.icon);
this.add_child(this.icon);
}
}
else
this.icon = null;
if (item.hasOwnProperty('text'))
{
if (typeof(item['text']) === "string")
this.widget = this._createTextOld(item);
else
this.widget = this._createText(item['text']);
if (this.widget !== null) {
this._connectWidgetSignals(this.widget);
this.add_child(this.widget);
}
}
else
this.widget = null;
if (item.hasOwnProperty('popup'))
this._createPopup(item['popup']);
this.menuOpen = false;
this.nbClicks = 0;
this.button = -1;
this.onClick = hashGet(item, 'on-click', '');
this.onDblClick = hashGet(item, 'on-dblclick', '');
@@ -126,199 +77,94 @@ class MonitorWidget extends PanelMenu.Button {
this.onEnter = hashGet(item, 'on-enter', '');
this.onLeave = hashGet(item, 'on-leave', '');
this.onScroll = hashGet(item, 'on-scroll', '');
let box = hashGet(item, 'box', 'center');
if (box === 'right' && position == -1)
position = 0;
this.connect('style-changed', this._onStyleChanged.bind(this));
// Disable click event at PanelMenu.button level
this.setSensitive(false);
this.nbClicks = 0;
this.button = -1;
Main.panel.addToStatusArea(this.fullname, this, position, box);
}
_onStyleChanged() {
// Force these values to avoid big spaces between each widgets
this._minHPadding = 1;
this._natHPadding = 1;
destructor() {
for(let widgetIdx in this.widgets)
this.disconnectWidgetSignals(this.widgets[widgetIdx]);
for(let timeoutIdx in this.timeouts)
GLib.Source.remove(this.timeouts[timeoutIdx]);
}
updateSignals(item) {
this.onClick = hashGet(item, 'on-click', this.onClick);
this.onDblClick = hashGet(item, 'on-dblclick', this.onDblClick);
this.onRightClick = hashGet(item, 'on-rightclick', this.onRightClick);
this.onRightDblClick = hashGet(item, 'on-rightdblclick', this.onRightDblClick);
this.onEnter = hashGet(item, 'on-enter', this.onEnter);
this.onLeave = hashGet(item, 'on-leave', this.onLeave);
this.onScroll = hashGet(item, 'on-scroll', this.onScroll);
}
_connectWidgetSignals(widget) {
this.signals[widget] = [];
connectWidgetSignals(widget) {
this.widgets.push(widget);
let array = new Array();
this.signals.set(widget, array);
let id;
id = widget.connect('enter-event', this._onEnter.bind(this));
this.signals[widget].push(id);
array.push(id);
id = widget.connect('leave-event', this._onLeave.bind(this));
this.signals[widget].push(id);
array.push(id);
id = widget.connect('scroll-event', this._onScroll.bind(this));
this.signals[widget].push(id);
array.push(id);
widget.set_reactive(true);
id = widget.connect('button-release-event', Lang.bind(this, this._clicked));
this.signals[widget].push(id);
id = widget.connect('button-release-event', this._clicked.bind(this));
array.push(id);
}
_disconnectWidgetSignals(widget) {
for(let idx in this.signals[widget])
widget.disconnect(this.signals[widget][idx]);
this.signals[widget] = null;
disconnectWidgetSignals(widget) {
let array = this.signals.get(widget);
for(let idx in array)
widget.disconnect(array[idx]);
this.signals.set(widget, null);
}
toggleMenu() {
if (this.menuOpen)
{
this.buttonMenu.menu.close();
this.menuOpen = false;
}
else
{
this.buttonMenu.menu.open(true);
this.menuOpen = true;
}
}
_createPopup(item) {
if (!item.hasOwnProperty('items')) {
return null;
}
let widgets = [];
for (let itemIndex in item['items']) {
let widget = null;
let widgetDict = item['items'][itemIndex];
if (widgetDict.hasOwnProperty('text'))
widget = this._createText(widgetDict['text']);
else if (widgetDict.hasOwnProperty('picture'))
widget = this._createPicture(widgetDict['picture']);
if (widget !== null)
widgets.push(widget);
}
if (widgets.length > 0) {
this.menuItem = new MyPopupMenuItem(widgets, {});
this.menu.addMenuItem(this.menuItem);
this.menu.setSensitive(false);
return this.menuItem;
}
return null;
}
_createTextOld(item) {
var itemValues = {};
itemValues = { 'text':item['text'] };
if (item.hasOwnProperty('style'))
itemValues['style'] = item['style'];
return this._createText(itemValues);
}
_createText(item) {
if (!item.hasOwnProperty('text')) {
log("Text must have a 'text' value");
return null;
}
let style = hashGet(item, 'style', '');
this.textProperties = item;
if (item['text'] === '') {
return null;
} else {
let widget = new St.Button({ label: item['text'] });
widget.set_style(style);
return widget;
}
}
_createIconOld(item) {
var itemValues = {};
itemValues = { 'path':item['icon'] };
if (item.hasOwnProperty('iconStyle'))
itemValues['style'] = item['iconStyle'];
return this._createIcon(itemValues);
}
_createIcon(item) {
if (!item.hasOwnProperty('path')) {
log("Icon must have a 'path' value");
return null;
}
let style = hashGet(item, 'style', '');
this.iconProperties = item;
if (item['path'] === '') {
return null;
} else {
let gicon = Gio.icon_new_for_string(item['path']);
gicon = new St.Icon({ gicon });
gicon.set_style(style);
return gicon;
}
}
_createPicture(item) {
if (!item.hasOwnProperty('path')) {
log("Picture must have a 'path' value");
return null;
}
let width = hashGet(item, 'width', -1);
let height = hashGet(item, 'height', -1);
if (typeof(width) === "string")
width = parseInt(width, 10);
if (typeof(height) === "string")
height = parseInt(height, 10);
let img = new Clutter.Image();
let initialPixbuf = Pixbuf.Pixbuf.new_from_file(item['path']);
img.set_data(initialPixbuf.get_pixels(),
initialPixbuf.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888
: Cogl.PixelFormat.RGB_888,
initialPixbuf.get_width(),
initialPixbuf.get_height(),
initialPixbuf.get_rowstride());
let picture = new Clutter.Actor();
picture.set_content(img);
picture.set_size((width != -1)?width:initialPixbuf.get_width(),
(height != -1)?height:initialPixbuf.get_height());
// Pack it in a box to avoid picture resize
let box = new St.BoxLayout({});
box.add_child(picture);
return box;
}
_manageEventAction(action, signalName) {
if (action === 'open-popup')
this.menu.open(true);
{
this.buttonMenu.menu.open(true);
this.menuOpen = true;
}
else if (action === 'close-popup')
this.menu.close();
{
this.buttonMenu.menu.close();
this.menuOpen = false;
}
else if (action === 'toggle-popup')
this.menu.toggle();
else if (action == 'delete')
{
this.toggleMenu();
}
else if (action === 'delete')
this.dbus.deleteItem(this, this.group);
else if (action === 'signal')
this.dbus.emitSignal(signalName, this.fullname);
return Clutter.EVENT_PROPAGATE;
return Clutter.EVENT_STOP;
}
_manageLeaveEvent() {
this._manageEventAction(this.onLeave);
}
_doClickCallback() {
let right = '';
let nbClicks = '';
if (this.button == 3)
right = 'Right';
if (this.nbClicks > 1)
nbClicks = 'Dbl';
let signalName = 'on' + right + nbClicks + 'Click';
const signalName = 'on' + right + nbClicks + 'Click';
let action = 'signal';
switch(signalName) {
@@ -343,8 +189,9 @@ class MonitorWidget extends PanelMenu.Button {
this.button = event.get_button();
this.nbClicks = 1;
Mainloop.timeout_add(this.dbus.ClutterSettings['double-click-time'],
Lang.bind(this, this._doClickCallback));
let sourceId = Mainloop.timeout_add(this.dbus.ClutterSettings['double-click-time'],
this._doClickCallback.bind(this));
this.timeouts.push(sourceId);
}
return Clutter.EVENT_PROPAGATE;
@@ -359,36 +206,112 @@ class MonitorWidget extends PanelMenu.Button {
}
_onScroll(actor, event) {
let signalName = '';
let direction = event.get_scroll_direction ();
let signalName = '';
const direction = event.get_scroll_direction ();
if (direction == Clutter.ScrollDirection.UP)
signalName = 'onScrollUp';
else if (direction == Clutter.ScrollDirection.DOWN)
signalName = 'onScrollDown';
return this._manageEventAction(this.onScroll, signalName);
return this._manageEventAction(this.onScroll, signalName);
}
}
var MyPopupMenuItem = GObject.registerClass({
GTypeName: 'MyPopupMenuItem'
},
class MyPopupMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(widgets, params) {
super._init(params);
this.box = new St.BoxLayout({ style_class: 'popup-combobox-item' });
this.box.set_vertical(true);
for (let widgetIndex in widgets)
this.box.add(widgets[widgetIndex]);
this.add_child(this.box);
}
});
var MonitorWidget = GObject.registerClass({
GTypeName: 'MonitorWidget'
},
class MonitorWidget extends PanelMenu.Button {
_init(item, group, dbus, position) {
super._init(0.0);
this.name = item['name'];
this.group = group;
this.fullname = this.name + '@' + this.group;
this.dbus = dbus;
this.menuItem = null;
this.signalManager = new SignalMgt(item, this.name, group, dbus, this);
this.popup_signals = null;
this.popup_widgets = null;
if (item.hasOwnProperty('icon'))
{
if (typeof(item['icon']) === 'string')
this.icon = this._createIconOld(item);
else
this.icon = this._createIcon(item['icon']);
if (this.icon !== null) {
this.signalManager.connectWidgetSignals(this.icon);
this.add_child(this.icon);
}
} else
this.icon = null;
if (item.hasOwnProperty('text'))
{
if (typeof(item['text']) === 'string')
this.widget = this._createTextOld(item);
else
this.widget = this._createText(item['text']);
if (this.widget !== null) {
this.signalManager.connectWidgetSignals(this.widget);
this.add_child(this.widget);
}
} else
this.widget = null;
if (item.hasOwnProperty('popup'))
this._createPopup(item['popup']);
const box = hashGet(item, 'box', 'center');
if (box === 'right' && position == -1)
position = 0;
this.connect('style-changed', this._onStyleChanged.bind(this));
Main.panel.addToStatusArea(this.fullname, this, position, box);
}
update(item) {
let prevWidget = this.widget;
let prevIcon = this.icon;
const prevWidget = this.widget;
const prevIcon = this.icon;
if (item.hasOwnProperty('text'))
{
let text = '';
let style = '';
if (typeof(item['text']) === "string") {
if (typeof(item['text']) === 'string') {
text = hashGet(item, 'text', '');
style = hashGet(item, 'style', '');
} else {
let textValues = item['text'];
const textValues = item['text'];
text = hashGet(textValues, 'text', '');
style = hashGet(textValues, 'style', '');
}
if (text !== '') {
if (!this.widget) {
if (typeof(item['text']) === "string")
if (typeof(item['text']) === 'string')
this.widget = this._createTextOld(item);
else
this.widget = this._createText(item['text']);
@@ -407,24 +330,24 @@ class MonitorWidget extends PanelMenu.Button {
{
let icon = '';
let style = '';
if (typeof(item['icon']) === "string") {
if (typeof(item['icon']) === 'string') {
icon = hashGet(item, 'icon', '');
style = hashGet(item, 'iconStyle', '');
} else {
let iconValues = item['icon'];
const iconValues = item['icon'];
icon = hashGet(iconValues, 'path', '');
style = hashGet(iconValues, 'style', '');
}
if (icon !== '') {
if (typeof(item['icon']) === "string")
if (typeof(item['icon']) === 'string')
this.icon = this._createIconOld(item);
else
this.icon = this._createIcon(item['icon']);
}
if (prevIcon) {
this._disconnectWidgetSignals(prevIcon);
this.signalManager.disconnectWidgetSignals(prevIcon);
this.insert_child_above(this.icon, prevIcon);
this.remove_child(prevIcon);
//delete prevIcon;
@@ -438,26 +361,22 @@ class MonitorWidget extends PanelMenu.Button {
if (item.hasOwnProperty('popup'))
{
let menuOpen = this.menu.isOpen;
const menuOpen = this.menu.isOpen;
if (this.menuItem) {
if (menuOpen)
this.menu.close();
for(let widgetIdx in this.popup_widgets)
this.signalManager.disconnectWidgetSignals(this.popup_widgets[widgetIdx]);
this.menu.removeAll();
//delete this.menuItem;
}
let popup = this._createPopup(item['popup']);
const popup = this._createPopup(item['popup']);
if (popup !== null && menuOpen)
this.menu.open(true);
}
this.onClick = hashGet(item, 'on-click', this.onClick);
this.onDblClick = hashGet(item, 'on-dblclick', this.onDblClick);
this.onRightClick = hashGet(item, 'on-rightclick', this.onRightClick);
this.onRightDblClick = hashGet(item, 'on-rightdblclick', this.onRightDblClick);
this.onEnter = hashGet(item, 'on-enter', this.onEnter);
this.onLeave = hashGet(item, 'on-leave', this.onLeave);
this.onScroll = hashGet(item, 'on-scroll', this.onScroll);
this.signalManager.updateSignals(item);
}
openPopup() {
@@ -476,16 +395,172 @@ class MonitorWidget extends PanelMenu.Button {
this.menu.close();
super.destroy();
}
_onStyleChanged() {
// Force these values to avoid big spaces between each widgets
this._minHPadding = 1;
this._natHPadding = 1;
}
_createPopup(item) {
if (!item.hasOwnProperty('items')) {
return null;
}
let widgets = [];
this.popup_signals = new WeakMap();
this.popup_widgets = new Array();
for (let itemIndex in item['items']) {
let widget = null;
let widgetDict = item['items'][itemIndex];
let nestedItem = null;
if (widgetDict.hasOwnProperty('text')) {
nestedItem = widgetDict['text'];
widget = this._createLabel(nestedItem);
} else if (widgetDict.hasOwnProperty('picture')) {
nestedItem = widgetDict['picture'];
widget = this._createPicture(nestedItem);
} else {
log('No known widget defined in popup');
}
if (nestedItem === null) {
continue;
}
widgets.push(widget);
const name = hashGet(nestedItem, 'name', '');
this.popup_signals[widget] = new SignalMgt(nestedItem, name,
this.fullname, this.dbus,
this);
this.popup_signals[widget].connectWidgetSignals(widget);
this.popup_widgets.push(widget);
}
if (widgets.length > 0) {
this.menuItem = new MyPopupMenuItem(widgets, {});
this.menu.addMenuItem(this.menuItem);
this.menu.setSensitive(false);
return this.menuItem;
}
return null;
}
_createTextOld(item) {
var itemValues = {};
itemValues = { 'text':item['text'] };
if (item.hasOwnProperty('style'))
itemValues['style'] = item['style'];
return this._createText(itemValues);
}
__createText(item, isLabel) {
if (!item.hasOwnProperty('text')) {
log('Text must have a \'text\' value');
return null;
}
const style = hashGet(item, 'style', '');
this.textProperties = item;
if (item['text'] === '') {
return null;
} else {
let widget = null;
if (isLabel)
widget = new St.Label({ text: item['text'] });
else
widget = new St.Button({ label: item['text'] });
widget.set_style(style);
return widget;
}
}
_createText(item) {
return this.__createText(item, false);
}
_createLabel(item) {
return this.__createText(item, true);
}
_createIconOld(item) {
var itemValues = {};
itemValues = { 'path':item['icon'] };
if (item.hasOwnProperty('iconStyle'))
itemValues['style'] = item['iconStyle'];
return this._createIcon(itemValues);
}
_createIcon(item) {
if (!item.hasOwnProperty('path')) {
log('Icon must have a \'path\' value');
return null;
}
const style = hashGet(item, 'style', '');
this.iconProperties = item;
if (item['path'] === '') {
return null;
} else {
let gicon = Gio.icon_new_for_string(item['path']);
gicon = new St.Icon({ gicon });
gicon.set_style(style);
return gicon;
}
}
_createPicture(item) {
if (!item.hasOwnProperty('path')) {
log('Picture must have a \'path\' value');
return null;
}
let width = hashGet(item, 'width', -1);
let height = hashGet(item, 'height', -1);
if (typeof(width) === 'string')
width = parseInt(width, 10);
if (typeof(height) === 'string')
height = parseInt(height, 10);
const img = new Clutter.Image();
const initialPixbuf = Pixbuf.Pixbuf.new_from_file(item['path']);
img.set_data(initialPixbuf.get_pixels(),
initialPixbuf.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888
: Cogl.PixelFormat.RGB_888,
initialPixbuf.get_width(),
initialPixbuf.get_height(),
initialPixbuf.get_rowstride());
const picture = new Clutter.Actor();
picture.set_content(img);
picture.set_size((width != -1)?width:initialPixbuf.get_width(),
(height != -1)?height:initialPixbuf.get_height());
// Pack it in a box to avoid picture resize
const box = new St.BoxLayout({});
box.add_child(picture);
return box;
}
});
// 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();
const extension = imports.misc.extensionUtils.getCurrentExtension();
let interfacesDir = extension.dir.get_child('.');
const interfacesDir = extension.dir.get_child('.');
let file = interfacesDir.get_child(filename);
const file = interfacesDir.get_child(filename);
let [result, contents] = imports.gi.GLib.file_get_contents(file.get_path());
@@ -497,7 +572,7 @@ function loadInterfaceXml(filename) {
// will spit out a TypeError soon).
if (contents instanceof Uint8Array)
contents = imports.byteArray.toString(contents);
let res = `<node>${contents}</node>`;
const res = `<node>${contents}</node>`;
return res;
} else {
throw new Error(`Generic monitor: Could not load file: ${filename}`);
@@ -530,7 +605,7 @@ class GenericMonitorDBUS {
}
for (let itemIndex in parameters['items']) {
let item = parameters['items'][itemIndex];
const item = parameters['items'][itemIndex];
if (!item.hasOwnProperty('name')) {
log('No name defined for item');
return false;
@@ -542,7 +617,7 @@ class GenericMonitorDBUS {
_getItemFromGroup(group, name) {
for (let groupItemIndex in group) {
let groupItem = group[groupItemIndex];
const groupItem = group[groupItemIndex];
if (groupItem.name === name)
return groupItem;
}
@@ -551,7 +626,7 @@ class GenericMonitorDBUS {
}
_getItemFromFullName(fullname) {
let splitName = fullname.split('@');
const splitName = fullname.split('@');
if (splitName.length !== 2) {
log(`Invalid name ${fullname}`);
return null;
@@ -574,11 +649,11 @@ class GenericMonitorDBUS {
}
notify(str) {
let parameters = JSON.parse(str);
const parameters = JSON.parse(str);
if (!this._checkParameters(parameters))
return;
let groupName = parameters['group'];
const groupName = parameters['group'];
let group;
if (!this.monitor_groups.hasOwnProperty(groupName)) {
group = [];
@@ -588,7 +663,7 @@ class GenericMonitorDBUS {
}
for (let itemIndex in parameters['items']) {
let item = parameters['items'][itemIndex];
const item = parameters['items'][itemIndex];
let monitorWidget = this._getItemFromGroup(group, item['name']);
// New widget
@@ -596,8 +671,8 @@ class GenericMonitorDBUS {
let position = group.length - 1;
// Find real position
if (position != -1) {
let lastWidget = group[position].container;
let childrens = lastWidget.get_parent().get_children();
const lastWidget = group[position].container;
const childrens = lastWidget.get_parent().get_children();
for(;position < childrens.length; position++)
{
if (childrens[position] == lastWidget) {
@@ -627,7 +702,7 @@ class GenericMonitorDBUS {
}
deleteItems(str) {
let parameters = JSON.parse(str);
const parameters = JSON.parse(str);
if (!parameters.hasOwnProperty('items')) {
log('No items defined');
@@ -636,7 +711,7 @@ class GenericMonitorDBUS {
for (let itemIndex in parameters['items']) {
let itemName = parameters['items'][itemIndex];
let splitName = itemName.split('@');
const splitName = itemName.split('@');
if (splitName.length !== 2) {
log(`Invalid name ${itemName}`);
return false;
@@ -654,7 +729,7 @@ class GenericMonitorDBUS {
}
deleteGroups(str) {
let parameters = JSON.parse(str);
const parameters = JSON.parse(str);
if (!parameters.hasOwnProperty('groups')) {
log('No groups defined');
@@ -663,29 +738,29 @@ class GenericMonitorDBUS {
let groupsToDelete = [];
for (let groupIndex in parameters['groups']) {
let groupName = parameters['groups'][groupIndex];
const groupName = parameters['groups'][groupIndex];
if (!this.monitor_groups.hasOwnProperty(groupName))
continue;
let group = this.monitor_groups[groupName];
const group = this.monitor_groups[groupName];
for (let itemIndex in group)
group[itemIndex].destroy();
groupsToDelete.push(groupName);
}
for (let groupDeleteIndex in groupsToDelete) {
let groupName = groupsToDelete[groupDeleteIndex];
const groupName = groupsToDelete[groupDeleteIndex];
delete this.monitor_groups[groupName];
}
}
_popupFunction(str) {
let parameters = JSON.parse(str);
const parameters = JSON.parse(str);
if (!parameters.hasOwnProperty('item')) {
log('No item defined');
return false;
}
let monitorWidget = this._getItemFromFullName(parameters['item']);
const monitorWidget = this._getItemFromFullName(parameters['item']);
if (monitorWidget !== null)
return monitorWidget;
else
@@ -695,19 +770,19 @@ class GenericMonitorDBUS {
}
openPopup(str) {
let monitorWidget = this._popupFunction(str)
const monitorWidget = this._popupFunction(str)
if (monitorWidget !== null)
monitorWidget.openPopup();
}
closePopup(str) {
let monitorWidget = this._popupFunction(str)
const monitorWidget = this._popupFunction(str)
if (monitorWidget !== null)
monitorWidget.closePopup();
}
togglePopup(str) {
let monitorWidget = this._popupFunction(str)
const monitorWidget = this._popupFunction(str)
if (monitorWidget !== null)
monitorWidget.togglePopup();
}
@@ -715,7 +790,7 @@ class GenericMonitorDBUS {
destructor() {
this._dbusImpl.emit_signal('onDeactivate', null);
for (let groupIndex in this.monitor_groups) {
let group = this.monitor_groups[groupIndex];
const group = this.monitor_groups[groupIndex];
for (let itemIndex in group)
group[itemIndex].destroy();
}
@@ -735,15 +810,19 @@ class Extension {
}
}
let extension = new Extension();
let extension = null;
function init() {
}
function enable() {
extension = new Extension();
extension.enable();
}
function disable() {
extension.disable();
if (extension) {
extension.disable();
extension = null;
}
}

View File

@@ -2,11 +2,12 @@
"uuid": "generic-monitor@gnome-shell-extensions",
"name": "Generic Monitor",
"description": "Display text & icon on systray using DBUS",
"version": "3",
"version": "12",
"shell-version": [
"3.38",
"3.36",
"3.34"
"43",
"42.3",
"42",
"41"
],
"url": "http://indefero.soutade.fr/p/genericmonitor"
}