Use WeakMap to store objects as key and rename MonitorWidget signals field into signalManager to avoid confusions

This commit is contained in:
Grégory Soutadé 2021-09-30 16:32:26 +02:00
parent 4d6fe82790
commit 45d0cc9d11

View File

@ -63,7 +63,8 @@ class SignalMgt {
this.fullname = this.name + '@' + this.group; this.fullname = this.name + '@' + this.group;
this.dbus = dbus; this.dbus = dbus;
this.menu = menu; this.menu = menu;
this.signals = {} this.signals = new WeakMap();
this.widgets = new Array();
this.nbClicks = 0; this.nbClicks = 0;
this.button = -1; this.button = -1;
@ -78,8 +79,8 @@ class SignalMgt {
} }
destructor() { destructor() {
for(let widgetIdx in this.signals) for(let widgetIdx in this.widgets)
this.disconnectWidgetSignals(this.signals[widgetIdx]); this.disconnectWidgetSignals(this.widgets[widgetIdx]);
} }
updateSignals(item) { updateSignals(item) {
@ -93,23 +94,26 @@ class SignalMgt {
} }
connectWidgetSignals(widget) { connectWidgetSignals(widget) {
this.signals[widget] = []; this.widgets.push(widget);
let array = new Array();
this.signals.set(widget, array);
let id; let id;
id = widget.connect('enter-event', this._onEnter.bind(this)); 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)); 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)); id = widget.connect('scroll-event', this._onScroll.bind(this));
this.signals[widget].push(id); array.push(id);
widget.set_reactive(true); widget.set_reactive(true);
id = widget.connect('button-release-event', Lang.bind(this, this._clicked)); id = widget.connect('button-release-event', Lang.bind(this, this._clicked));
this.signals[widget].push(id); array.push(id);
} }
disconnectWidgetSignals(widget) { disconnectWidgetSignals(widget) {
for(let idx in this.signals[widget]) let array = this.signals.get(widget);
widget.disconnect(this.signals[widget][idx]); for(let idx in array)
this.signals[widget] = null; widget.disconnect(array[idx]);
this.signals.set(widget, null);
} }
_manageEventAction(action, signalName) { _manageEventAction(action, signalName) {
@ -222,8 +226,9 @@ class MonitorWidget extends PanelMenu.Button {
this.group = group; this.group = group;
this.fullname = this.name + '@' + this.group; this.fullname = this.name + '@' + this.group;
this.dbus = dbus; this.dbus = dbus;
this.signals = new SignalMgt(item, this.name, group, dbus, this.menu); this.signalManager = new SignalMgt(item, this.name, group, dbus, this.menu);
this.popup_signals = {}; this.popup_signals = null;
this.popup_widgets = null;
if (item.hasOwnProperty('icon')) if (item.hasOwnProperty('icon'))
{ {
@ -232,7 +237,7 @@ class MonitorWidget extends PanelMenu.Button {
else else
this.icon = this._createIcon(item['icon']); this.icon = this._createIcon(item['icon']);
if (this.icon !== null) { if (this.icon !== null) {
this.signals.connectWidgetSignals(this.icon); this.signalManager.connectWidgetSignals(this.icon);
this.add_child(this.icon); this.add_child(this.icon);
} }
} else } else
@ -246,7 +251,7 @@ class MonitorWidget extends PanelMenu.Button {
this.widget = this._createText(item['text']); this.widget = this._createText(item['text']);
if (this.widget !== null) { if (this.widget !== null) {
this.signals.connectWidgetSignals(this.widget); this.signalManager.connectWidgetSignals(this.widget);
this.add_child(this.widget); this.add_child(this.widget);
} }
} else } else
@ -320,7 +325,7 @@ class MonitorWidget extends PanelMenu.Button {
} }
if (prevIcon) { if (prevIcon) {
this.signals.disconnectWidgetSignals(prevIcon); this.signalManager.disconnectWidgetSignals(prevIcon);
this.insert_child_above(this.icon, prevIcon); this.insert_child_above(this.icon, prevIcon);
this.remove_child(prevIcon); this.remove_child(prevIcon);
//delete prevIcon; //delete prevIcon;
@ -338,8 +343,8 @@ class MonitorWidget extends PanelMenu.Button {
if (this.menuItem) { if (this.menuItem) {
if (menuOpen) if (menuOpen)
this.menu.close(); this.menu.close();
for(let widgetIdx in this.popup_signals) for(let widgetIdx in this.popup_widgets)
this.signals.disconnectWidgetSignals(this.popup_signals[widgetIdx]); this.signalManager.disconnectWidgetSignals(this.popup_widgets[widgetIdx]);
this.menu.removeAll(); this.menu.removeAll();
//delete this.menuItem; //delete this.menuItem;
} }
@ -349,7 +354,7 @@ class MonitorWidget extends PanelMenu.Button {
this.menu.open(true); this.menu.open(true);
} }
this.signals.updateSignals(item); this.signalManager.updateSignals(item);
} }
openPopup() { openPopup() {
@ -381,6 +386,8 @@ class MonitorWidget extends PanelMenu.Button {
} }
let widgets = []; let widgets = [];
this.popup_signals = new WeakMap();
this.popup_widgets = new Array();
for (let itemIndex in item['items']) { for (let itemIndex in item['items']) {
let widget = null; let widget = null;
let widgetDict = item['items'][itemIndex]; let widgetDict = item['items'][itemIndex];
@ -406,6 +413,7 @@ class MonitorWidget extends PanelMenu.Button {
this.fullname, this.dbus, this.fullname, this.dbus,
this.menu); this.menu);
this.popup_signals[widget].connectWidgetSignals(widget); this.popup_signals[widget].connectWidgetSignals(widget);
this.popup_widgets.push(widget);
} }
if (widgets.length > 0) { if (widgets.length > 0) {