Don't access global timer object

This commit is contained in:
Romain Vigier
2022-02-20 02:52:23 +01:00
parent a7502c9108
commit b3e0085851
2 changed files with 26 additions and 19 deletions
+2 -2
View File
@@ -133,12 +133,12 @@ var Timer = class {
this.#sources.push(new TimerSchedule()); this.#sources.push(new TimerSchedule());
break; break;
case 'ondemand': case 'ondemand':
this.#sources.push(new TimerOndemand()); this.#sources.push(new TimerOndemand({ timer: this }));
break; break;
} }
if (this.#settings.get_boolean('always-enable-ondemand') && ['nightlight', 'location', 'schedule'].includes(source)) if (this.#settings.get_boolean('always-enable-ondemand') && ['nightlight', 'location', 'schedule'].includes(source))
this.#sources.unshift(new TimerOndemand()); this.#sources.unshift(new TimerOndemand({ timer: this }));
} }
#enableSources() { #enableSources() {
+24 -17
View File
@@ -13,7 +13,6 @@ const { PopupMenuItem, PopupSubMenuMenuItem } = imports.ui.popupMenu;
const Me = extensionUtils.getCurrentExtension(); const Me = extensionUtils.getCurrentExtension();
const _ = extensionUtils.gettext; const _ = extensionUtils.gettext;
const e = Me.imports.extension;
const utils = Me.imports.utils; const utils = Me.imports.utils;
const { Time } = Me.imports.enums.Time; const { Time } = Me.imports.enums.Time;
@@ -25,6 +24,7 @@ const { Time } = Me.imports.enums.Time;
* The user can change the key combination in the extension's preferences. * The user can change the key combination in the extension's preferences.
*/ */
var TimerOndemand = class { var TimerOndemand = class {
#timer;
#settings; #settings;
#settingsConnections = []; #settingsConnections = [];
@@ -32,7 +32,8 @@ var TimerOndemand = class {
#previousKeybinding = null; #previousKeybinding = null;
#timerConnection = null; #timerConnection = null;
constructor() { constructor({ timer }) {
this.#timer = timer;
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time')); this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
} }
@@ -85,12 +86,12 @@ var TimerOndemand = class {
#connectTimer() { #connectTimer() {
console.debug('Connecting On-demand Timer to Timer...'); console.debug('Connecting On-demand Timer to Timer...');
this.#timerConnection = e.timer.connect('time-changed', this.#onTimeChanged.bind(this)); this.#timerConnection = this.#timer.connect('time-changed', this.#onTimeChanged.bind(this));
} }
#disconnectTimer() { #disconnectTimer() {
if (this.#timerConnection) { if (this.#timerConnection) {
e.timer.disconnect(this.#timerConnection); this.#timer.disconnect(this.#timerConnection);
this.#timerConnection = null; this.#timerConnection = null;
} }
console.debug('Disconnected On-demand Timer from Timer.'); console.debug('Disconnected On-demand Timer from Timer.');
@@ -112,7 +113,7 @@ var TimerOndemand = class {
} }
#onTimeChanged(_timer, _newTime) { #onTimeChanged(_timer, _newTime) {
this.#settings.set_string('ondemand-time', e.timer.time); this.#settings.set_string('ondemand-time', this.#timer.time);
this.#updateButton(); this.#updateButton();
} }
@@ -169,7 +170,7 @@ var TimerOndemand = class {
#addButtonToPanel() { #addButtonToPanel() {
console.debug('Adding On-demand Timer button to the panel...'); console.debug('Adding On-demand Timer button to the panel...');
this.#button = new NtsPanelMenuButton({ toggleCallback: this.#toggleTime.bind(this) }); this.#button = new NtsPanelMenuButton({ timer: this.#timer, toggleCallback: this.#toggleTime.bind(this) });
main.panel.addToStatusArea('NightThemeSwitcherButton', this.#button); main.panel.addToStatusArea('NightThemeSwitcherButton', this.#button);
console.debug('Added On-demand Timer button to the panel.'); console.debug('Added On-demand Timer button to the panel.');
} }
@@ -178,13 +179,13 @@ var TimerOndemand = class {
console.debug('Adding On-demand Timer button to the menu...'); console.debug('Adding On-demand Timer button to the menu...');
const aggregateMenu = main.panel.statusArea.aggregateMenu; const aggregateMenu = main.panel.statusArea.aggregateMenu;
const position = utils.findShellAggregateMenuItemPosition(aggregateMenu._system.menu) - 1; const position = utils.findShellAggregateMenuItemPosition(aggregateMenu._system.menu) - 1;
this.#button = new NtsPopupSubMenuMenuItem({ toggleCallback: this.#toggleTime.bind(this) }); this.#button = new NtsPopupSubMenuMenuItem({ timer: this.#timer, toggleCallback: this.#toggleTime.bind(this) });
aggregateMenu.menu.addMenuItem(this.#button, position); aggregateMenu.menu.addMenuItem(this.#button, position);
console.debug('Added On-demand Timer button to the menu.'); console.debug('Added On-demand Timer button to the menu.');
} }
#toggleTime() { #toggleTime() {
this.#settings.set_string('ondemand-time', e.timer.time === Time.DAY ? Time.NIGHT : Time.DAY); this.#settings.set_string('ondemand-time', this.#timer.time === Time.DAY ? Time.NIGHT : Time.DAY);
this.emit('time-changed', this.time); this.emit('time-changed', this.time);
} }
}; };
@@ -192,8 +193,11 @@ Signals.addSignalMethods(TimerOndemand.prototype);
var NtsPanelMenuButton = GObject.registerClass( var NtsPanelMenuButton = GObject.registerClass(
class NtsPanelMenuButton extends PanelMenuButton { class NtsPanelMenuButton extends PanelMenuButton {
constructor({ toggleCallback }) { #timer;
constructor({ timer, toggleCallback }) {
super(0.0); super(0.0);
this.#timer = timer;
this.icon = new St.Icon({ this.icon = new St.Icon({
style_class: 'system-status-icon', style_class: 'system-status-icon',
}); });
@@ -204,17 +208,20 @@ var NtsPanelMenuButton = GObject.registerClass(
} }
update() { update() {
this.icon.icon_name = _getIconNameForTime(e.timer.time); this.icon.icon_name = _getIconNameForTime(this.#timer.time);
this.icon.fallback_gicon = _getGiconForTime(e.timer.time); this.icon.fallback_gicon = _getGiconForTime(this.#timer.time);
this.accessible_name = e.timer.time === Time.DAY ? _('Turn Night Mode On') : _('Turn Night Mode Off'); this.accessible_name = this.#timer.time === Time.DAY ? _('Turn Night Mode On') : _('Turn Night Mode Off');
} }
} }
); );
var NtsPopupSubMenuMenuItem = GObject.registerClass( var NtsPopupSubMenuMenuItem = GObject.registerClass(
class NtsPopupSubMenuMenuItem extends PopupSubMenuMenuItem { class NtsPopupSubMenuMenuItem extends PopupSubMenuMenuItem {
constructor({ toggleCallback }) { #timer;
constructor({ timer, toggleCallback }) {
super('', true); super('', true);
this.#timer = timer;
this._toggleItem = new PopupMenuItem(''); this._toggleItem = new PopupMenuItem('');
this._toggleItem.connect('activate', () => toggleCallback()); this._toggleItem.connect('activate', () => toggleCallback());
this.menu.addMenuItem(this._toggleItem); this.menu.addMenuItem(this._toggleItem);
@@ -225,10 +232,10 @@ var NtsPopupSubMenuMenuItem = GObject.registerClass(
} }
update() { update() {
this.icon.icon_name = _getIconNameForTime(e.timer.time); this.icon.icon_name = _getIconNameForTime(this.#timer.time);
this.icon.fallback_gicon = _getGiconForTime(e.timer.time); this.icon.fallback_gicon = _getGiconForTime(this.#timer.time);
this.label.text = e.timer.time === Time.DAY ? _('Night Mode Off') : _('Night Mode On'); this.label.text = this.#timer.time === Time.DAY ? _('Night Mode Off') : _('Night Mode On');
this._toggleItem.label.text = e.timer.time === Time.DAY ? _('Turn On') : _('Turn Off'); this._toggleItem.label.text = this.#timer.time === Time.DAY ? _('Turn On') : _('Turn Off');
} }
} }
); );