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:
parent
506660f1da
commit
f536c89e36
|
@ -1,22 +1,25 @@
|
||||||
|
|
||||||
class DBUSItem:
|
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.name = name
|
||||||
self.text = text
|
self.text = text
|
||||||
self.style= style
|
self.style= style
|
||||||
self.icon = icon
|
self.icon = icon
|
||||||
self.iconStyle = iconStyle
|
self.iconStyle = iconStyle
|
||||||
self.onClick = onClick
|
self.onClick = onClick
|
||||||
|
self.box = box
|
||||||
|
|
||||||
self._checkValues()
|
self._checkValues()
|
||||||
|
|
||||||
def _checkValues(self):
|
def _checkValues(self):
|
||||||
if self.onClick and not self.onClick in ('signal', 'delete'):
|
if self.onClick and not self.onClick in ('signal', 'delete'):
|
||||||
raise ValueError('Invalid onClick value')
|
raise ValueError('Invalid onClick value')
|
||||||
|
if self.box and not self.box in ('left', 'center', 'right'):
|
||||||
|
raise ValueError('Invalid box value')
|
||||||
|
|
||||||
def toMap(self):
|
def toMap(self):
|
||||||
myMap = {"name":self.name}
|
myMap = {"name":self.name}
|
||||||
for p in ('text', 'style', 'icon'):
|
for p in ('text', 'style', 'icon', 'box'):
|
||||||
if self.__dict__[p]:
|
if self.__dict__[p]:
|
||||||
myMap[p] = self.__dict__[p]
|
myMap[p] = self.__dict__[p]
|
||||||
if self.iconStyle:
|
if self.iconStyle:
|
||||||
|
|
|
@ -45,7 +45,7 @@ class TimerThread(Thread):
|
||||||
def _displayTimerValue(self):
|
def _displayTimerValue(self):
|
||||||
encoder = json.JSONEncoder()
|
encoder = json.JSONEncoder()
|
||||||
res = {'group':'Timer', 'items':[]}
|
res = {'group':'Timer', 'items':[]}
|
||||||
item = DBUSItem('timer', onClick='signal')
|
item = DBUSItem('timer', onClick='signal', box='right')
|
||||||
curValue = self.timers[self.curTimer]
|
curValue = self.timers[self.curTimer]
|
||||||
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
|
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
|
||||||
if curValue >= (60*60):
|
if curValue >= (60*60):
|
||||||
|
|
125
extension.js
125
extension.js
|
@ -27,12 +27,33 @@ const Mainloop = imports.mainloop;
|
||||||
const clutter = imports.gi.Clutter;
|
const clutter = imports.gi.Clutter;
|
||||||
|
|
||||||
class MonitorWidget {
|
class MonitorWidget {
|
||||||
constructor(name, group, text, style, icon, iconStyle, onClick) {
|
constructor(name, group, text, style, icon, iconStyle, onClick, box, lastWidget) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.group = group;
|
this.group = group;
|
||||||
this._createIcon(icon, iconStyle);
|
this._createIcon(icon, iconStyle);
|
||||||
this._createText(text, style);
|
this._createText(text, style);
|
||||||
this.onClick = onClick;
|
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) {
|
_createText(text, style) {
|
||||||
|
@ -56,23 +77,70 @@ class MonitorWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
update(text, style, icon, iconStyle) {
|
_addToBox(lastWidget) {
|
||||||
if (!this.widget) {
|
// lastWidget => NULL, insert at the end
|
||||||
this._createText(text, style);
|
// 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 {
|
} 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;
|
this.widget.label = text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (style !== '' && this.widget) {
|
||||||
this.style = style;
|
this.style = style;
|
||||||
this.widget.set_style(this.style);
|
this.widget.set_style(this.style);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (icon !== '')
|
if (icon !== '') {
|
||||||
this._createIcon(icon, iconStyle);
|
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.iconStyle = style;
|
||||||
this.icon.set_style(this.iconStyle);
|
this.icon.set_style(this.iconStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prevIcon && icon !== '')
|
||||||
|
this.box.remove_child(prevIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +175,6 @@ class GenericMonitorDBUS {
|
||||||
this.monitor_groups = {};
|
this.monitor_groups = {};
|
||||||
this.actor_clicked = {};
|
this.actor_clicked = {};
|
||||||
this.clutterSettings = clutter.Settings.get_default();
|
this.clutterSettings = clutter.Settings.get_default();
|
||||||
this.box = Main.panel._centerBox;
|
|
||||||
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(loadInterfaceXml('dbus.xml'), this);
|
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(loadInterfaceXml('dbus.xml'), this);
|
||||||
this._dbusImpl.export(Gio.DBus.session, '/com/soutade/GenericMonitor');
|
this._dbusImpl.export(Gio.DBus.session, '/com/soutade/GenericMonitor');
|
||||||
}
|
}
|
||||||
|
@ -129,6 +196,10 @@ class GenericMonitorDBUS {
|
||||||
if (item['on-click'] !== 'signal' && item['on-click'] !== 'delete')
|
if (item['on-click'] !== 'signal' && item['on-click'] !== 'delete')
|
||||||
throw new Error('Invalid on-click value');
|
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 = '';
|
let onClick = '';
|
||||||
if (item.hasOwnProperty('on-click'))
|
if (item.hasOwnProperty('on-click'))
|
||||||
onClick = item['on-click'];
|
onClick = item['on-click'];
|
||||||
|
let box = 'center';
|
||||||
|
if (item.hasOwnProperty('box'))
|
||||||
|
box = item['box'];
|
||||||
|
|
||||||
let monitorWidget = this._getItemFromGroup(group, item['name']);
|
let monitorWidget = this._getItemFromGroup(group, item['name']);
|
||||||
let lastWidget = null;
|
let lastWidget = null;
|
||||||
|
|
||||||
// New widget
|
// New widget
|
||||||
if (monitorWidget === null) {
|
if (monitorWidget === null) {
|
||||||
monitorWidget = new MonitorWidget(item['name'], groupName, text, style, icon, iconStyle, onClick);
|
|
||||||
if (group.length)
|
if (group.length)
|
||||||
lastWidget = group[group.length - 1].widget;
|
lastWidget = group[group.length - 1].widget;
|
||||||
|
monitorWidget = new MonitorWidget(item['name'], groupName, text, style, icon, iconStyle, onClick, box, lastWidget);
|
||||||
group.push(monitorWidget);
|
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
|
// Connect signals
|
||||||
if (onClick !== '') {
|
if (onClick !== '') {
|
||||||
if (monitorWidget.widget)
|
if (monitorWidget.widget)
|
||||||
|
@ -271,34 +338,14 @@ class GenericMonitorDBUS {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let prevWidget = monitorWidget.widget;
|
|
||||||
let prevIcon = monitorWidget.icon;
|
|
||||||
|
|
||||||
monitorWidget.update(text, style, icon, iconStyle);
|
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) {
|
deleteItem(item, groupName) {
|
||||||
let group = this.monitor_groups[groupName];
|
let group = this.monitor_groups[groupName];
|
||||||
this._removeItemFromBox(item);
|
item.removeFromBox();
|
||||||
group = this._removeFromArray(group, item);
|
group = this._removeFromArray(group, item);
|
||||||
if (group.length === 0)
|
if (group.length === 0)
|
||||||
delete this.monitor_groups[groupName];
|
delete this.monitor_groups[groupName];
|
||||||
|
@ -342,7 +389,7 @@ class GenericMonitorDBUS {
|
||||||
continue;
|
continue;
|
||||||
let group = this.monitor_groups[groupName];
|
let group = this.monitor_groups[groupName];
|
||||||
for (let itemIndex in group)
|
for (let itemIndex in group)
|
||||||
this._removeItemFromBox(group[itemIndex]);
|
group[itemIndex].removeFromBox();
|
||||||
groupsToDelete.push(groupName);
|
groupsToDelete.push(groupName);
|
||||||
}
|
}
|
||||||
for (let groupDeleteIndex in groupsToDelete) {
|
for (let groupDeleteIndex in groupsToDelete) {
|
||||||
|
@ -355,7 +402,7 @@ class GenericMonitorDBUS {
|
||||||
for (let groupIndex in this.monitor_groups) {
|
for (let groupIndex in this.monitor_groups) {
|
||||||
let group = this.monitor_groups[groupIndex];
|
let group = this.monitor_groups[groupIndex];
|
||||||
for (let itemIndex in group)
|
for (let itemIndex in group)
|
||||||
this._removeItemFromBox(group[itemIndex]);
|
group[itemIndex].removeFromBox();
|
||||||
}
|
}
|
||||||
this.monitor_groups = {};
|
this.monitor_groups = {};
|
||||||
this._dbusImpl.unexport();
|
this._dbusImpl.unexport();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user