Add box property management

Widgets can be put in left, center or right box.
Warning: right box is available (no error), but seems undefined (Gnome Shell 3.36)...
This commit is contained in:
Grégory Soutadé 2020-05-05 09:56:59 +02:00
parent 506660f1da
commit f536c89e36
3 changed files with 94 additions and 44 deletions

View File

@ -1,22 +1,25 @@
class DBUSItem:
def __init__(self, name, text='', style='', icon='', iconStyle='', onClick=''):
def __init__(self, name, text='', style='', icon='', iconStyle='', onClick='', box=''):
self.name = name
self.text = text
self.style= style
self.icon = icon
self.iconStyle = iconStyle
self.onClick = onClick
self.box = box
self._checkValues()
def _checkValues(self):
if self.onClick and not self.onClick in ('signal', 'delete'):
raise ValueError('Invalid onClick value')
if self.box and not self.box in ('left', 'center', 'right'):
raise ValueError('Invalid box value')
def toMap(self):
myMap = {"name":self.name}
for p in ('text', 'style', 'icon'):
for p in ('text', 'style', 'icon', 'box'):
if self.__dict__[p]:
myMap[p] = self.__dict__[p]
if self.iconStyle:

View File

@ -45,7 +45,7 @@ class TimerThread(Thread):
def _displayTimerValue(self):
encoder = json.JSONEncoder()
res = {'group':'Timer', 'items':[]}
item = DBUSItem('timer', onClick='signal')
item = DBUSItem('timer', onClick='signal', box='right')
curValue = self.timers[self.curTimer]
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
if curValue >= (60*60):

View File

@ -27,12 +27,33 @@ const Mainloop = imports.mainloop;
const clutter = imports.gi.Clutter;
class MonitorWidget {
constructor(name, group, text, style, icon, iconStyle, onClick) {
constructor(name, group, text, style, icon, iconStyle, onClick, box, lastWidget) {
this.name = name;
this.group = group;
this._createIcon(icon, iconStyle);
this._createText(text, style);
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} is undefined, falling back to centerBox`);
this.box = Main.panel._centerBox;
}
this._addToBox(lastWidget);
}
_createText(text, style) {
@ -56,23 +77,70 @@ class MonitorWidget {
}
}
update(text, style, icon, iconStyle) {
if (!this.widget) {
this._createText(text, style);
_addToBox(lastWidget) {
// lastWidget => NULL, insert at the end
// Add to box
if (this.box !== Main.panel._rigthBox || lastWidget) {
if (this.icon) {
this.box.insert_child_above(this.icon, lastWidget);
lastWidget = this.icon;
}
if (this.widget)
this.box.insert_child_above(this.widget, lastWidget);
} else {
if (text !== '')
if (this.icon) {
this.box.insert_child_at_index(this.icon, 0);
lastWidget = this.icon;
}
if (this.widget) {
if (lastWidget)
this.box.insert_child_above(this.widget, lastWidget);
else
this.box.insert_child_at_index(this.icon, 0);
}
}
}
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 !== '')
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 (icon === '' && iconStyle !== '' && this.icon) {
if (iconStyle !== '' && this.icon) {
this.iconStyle = style;
this.icon.set_style(this.iconStyle);
}
if (prevIcon && icon !== '')
this.box.remove_child(prevIcon);
}
}
@ -107,7 +175,6 @@ class GenericMonitorDBUS {
this.monitor_groups = {};
this.actor_clicked = {};
this.clutterSettings = clutter.Settings.get_default();
this.box = Main.panel._centerBox;
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(loadInterfaceXml('dbus.xml'), this);
this._dbusImpl.export(Gio.DBus.session, '/com/soutade/GenericMonitor');
}
@ -129,6 +196,10 @@ class GenericMonitorDBUS {
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');
}
}
}
@ -239,23 +310,19 @@ class GenericMonitorDBUS {
let onClick = '';
if (item.hasOwnProperty('on-click'))
onClick = item['on-click'];
let box = 'center';
if (item.hasOwnProperty('box'))
box = item['box'];
let monitorWidget = this._getItemFromGroup(group, item['name']);
let lastWidget = null;
// New widget
if (monitorWidget === null) {
monitorWidget = new MonitorWidget(item['name'], groupName, text, style, icon, iconStyle, onClick);
if (group.length)
lastWidget = group[group.length - 1].widget;
monitorWidget = new MonitorWidget(item['name'], groupName, text, style, icon, iconStyle, onClick, box, lastWidget);
group.push(monitorWidget);
// lastWidget => NULL, insert at the end
if (monitorWidget.icon) {
this.box.insert_child_above(monitorWidget.icon, lastWidget);
lastWidget = monitorWidget.icon;
}
if (monitorWidget.widget)
this.box.insert_child_above(monitorWidget.widget, lastWidget);
// Connect signals
if (onClick !== '') {
if (monitorWidget.widget)
@ -271,34 +338,14 @@ class GenericMonitorDBUS {
}
}
} else {
let prevWidget = monitorWidget.widget;
let prevIcon = monitorWidget.icon;
monitorWidget.update(text, style, icon, iconStyle);
if (monitorWidget.icon) {
if (prevIcon)
this.box.remove_child(prevIcon);
this.box.insert_child_above(monitorWidget.icon, lastWidget);
lastWidget = monitorWidget.icon;
}
if (!prevWidget && monitorWidget.widget)
this.box.insert_child_above(monitorWidget.widget, lastWidget);
}
}
}
_removeItemFromBox(item) {
if (item.widget)
this.box.remove_child(item.widget);
if (item.icon)
this.box.remove_child(item.icon);
}
deleteItem(item, groupName) {
let group = this.monitor_groups[groupName];
this._removeItemFromBox(item);
item.removeFromBox();
group = this._removeFromArray(group, item);
if (group.length === 0)
delete this.monitor_groups[groupName];
@ -342,7 +389,7 @@ class GenericMonitorDBUS {
continue;
let group = this.monitor_groups[groupName];
for (let itemIndex in group)
this._removeItemFromBox(group[itemIndex]);
group[itemIndex].removeFromBox();
groupsToDelete.push(groupName);
}
for (let groupDeleteIndex in groupsToDelete) {
@ -355,7 +402,7 @@ class GenericMonitorDBUS {
for (let groupIndex in this.monitor_groups) {
let group = this.monitor_groups[groupIndex];
for (let itemIndex in group)
this._removeItemFromBox(group[itemIndex]);
group[itemIndex].removeFromBox();
}
this.monitor_groups = {};
this._dbusImpl.unexport();