Use private fields and methods in classes

This commit is contained in:
Romain Vigier
2022-02-20 02:36:47 +01:00
parent d920c7dcd7
commit a7502c9108
5 changed files with 266 additions and 247 deletions
+84 -77
View File
@@ -34,173 +34,180 @@ const { TimerOndemand } = Me.imports.modules.TimerOndemand;
* schedule in the extensions's preferences.
*/
var Timer = class {
#settings;
#interfaceSettings;
#colorSettings;
#locationSettings;
#time;
#sources = [];
#settingsConnections = [];
#timeConnections = [];
constructor() {
this._timeSettings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this._interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
this._colorSettings = new Gio.Settings({ schema: 'org.gnome.settings-daemon.plugins.color' });
this._locationSettings = new Gio.Settings({ schema: 'org.gnome.system.location' });
this._sources = [];
this._settingsConnections = [];
this._timeConnections = [];
this._previousTime = this._interfaceSettings.get_string('color-scheme') === 'prefer-dark' ? Time.NIGHT : Time.DAY;
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this.#interfaceSettings = new Gio.Settings({ schema: 'org.gnome.desktop.interface' });
this.#colorSettings = new Gio.Settings({ schema: 'org.gnome.settings-daemon.plugins.color' });
this.#locationSettings = new Gio.Settings({ schema: 'org.gnome.system.location' });
this.#time = this.#interfaceSettings.get_string('color-scheme') === 'prefer-dark' ? Time.NIGHT : Time.DAY;
}
enable() {
console.debug('Enabling Timer...');
this._connectSettings();
this._createSources();
this._connectSources();
this._enableSources();
this.#connectSettings();
this.#createSources();
this.#connectSources();
this.#enableSources();
console.debug('Timer enabled.');
}
disable() {
console.debug('Disabling Timer...');
this._disconnectSources();
this._disableSources();
this._disconnectSettings();
this.#disconnectSources();
this.#disableSources();
this.#disconnectSettings();
console.debug('Timer disabled.');
}
get time() {
return this._previousTime;
return this.#time;
}
set time(time) {
if (time === this._previousTime)
if (time === this.#time)
return;
console.debug(`Time has changed to ${time}.`);
this._previousTime = time;
this._interfaceSettings.set_string('color-scheme', time === Time.NIGHT ? 'prefer-dark' : 'default');
if (this._timeSettings.get_boolean('transition'))
this.#time = time;
this.#interfaceSettings.set_string('color-scheme', time === Time.NIGHT ? 'prefer-dark' : 'default');
if (this.#settings.get_boolean('transition'))
main.layoutManager.screenTransition.run();
this.emit('time-changed', time);
}
_connectSettings() {
#connectSettings() {
console.debug('Connecting Timer to settings...');
this._settingsConnections.push({
settings: this._colorSettings,
id: this._colorSettings.connect('changed::night-light-enabled', this._onSourceChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#colorSettings,
id: this.#colorSettings.connect('changed::night-light-enabled', this.#onSourceChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._locationSettings,
id: this._locationSettings.connect('changed::enabled', this._onSourceChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#locationSettings,
id: this.#locationSettings.connect('changed::enabled', this.#onSourceChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::manual-time-source', this._onSourceChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::manual-time-source', this.#onSourceChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::always-enable-ondemand', this._onSourceChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::always-enable-ondemand', this.#onSourceChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::time-source', this._onTimeSourceChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::time-source', this.#onTimeSourceChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._interfaceSettings,
id: this._interfaceSettings.connect('changed::color-scheme', this._onColorSchemeChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#interfaceSettings,
id: this.#interfaceSettings.connect('changed::color-scheme', this.#onColorSchemeChanged.bind(this)),
});
}
_disconnectSettings() {
this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this._settingsConnections = [];
#disconnectSettings() {
this.#settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this.#settingsConnections = [];
console.debug('Disconnected Timer from settings.');
}
_createSources() {
const source = this._getSource();
#createSources() {
const source = this.#getSource();
switch (source) {
case 'nightlight':
this._sources.push(new TimerNightlight());
this.#sources.push(new TimerNightlight());
break;
case 'location':
this._sources.push(new TimerLocation());
this.#sources.push(new TimerLocation());
break;
case 'schedule':
this._sources.push(new TimerSchedule());
this.#sources.push(new TimerSchedule());
break;
case 'ondemand':
this._sources.push(new TimerOndemand());
this.#sources.push(new TimerOndemand());
break;
}
if (this._timeSettings.get_boolean('always-enable-ondemand') && ['nightlight', 'location', 'schedule'].includes(source))
this._sources.unshift(new TimerOndemand());
if (this.#settings.get_boolean('always-enable-ondemand') && ['nightlight', 'location', 'schedule'].includes(source))
this.#sources.unshift(new TimerOndemand());
}
_enableSources() {
this._sources.forEach(source => source.enable());
#enableSources() {
this.#sources.forEach(source => source.enable());
}
_disableSources() {
this._sources.forEach(source => source.disable());
this._sources = [];
#disableSources() {
this.#sources.forEach(source => source.disable());
this.#sources = [];
}
_connectSources() {
#connectSources() {
console.debug('Connecting to time sources...');
this._sources.forEach(source => this._timeConnections.push({
this.#sources.forEach(source => this.#timeConnections.push({
source,
id: source.connect('time-changed', this._onTimeChanged.bind(this)),
id: source.connect('time-changed', this.#onTimeChanged.bind(this)),
}));
}
_disconnectSources() {
this._timeConnections.forEach(connection => connection.source.disconnect(connection.id));
this._timeConnections = [];
#disconnectSources() {
this.#timeConnections.forEach(connection => connection.source.disconnect(connection.id));
this.#timeConnections = [];
console.debug('Disconnected from time sources.');
}
_onSourceChanged() {
#onSourceChanged() {
this.disable();
this.enable();
}
_onTimeSourceChanged() {
if (this._timeSettings.get_boolean('manual-time-source'))
this._onSourceChanged();
#onTimeSourceChanged() {
if (this.#settings.get_boolean('manual-time-source'))
this.#onSourceChanged();
}
_onTimeChanged(_source, newTime) {
#onTimeChanged(_source, newTime) {
this.time = newTime;
}
_onColorSchemeChanged() {
this.time = this._interfaceSettings.get_string('color-scheme') === 'prefer-dark' ? Time.NIGHT : Time.DAY;
#onColorSchemeChanged() {
this.time = this.#interfaceSettings.get_string('color-scheme') === 'prefer-dark' ? Time.NIGHT : Time.DAY;
}
_getSource() {
#getSource() {
console.debug('Getting time source...');
let source;
if (this._timeSettings.get_boolean('manual-time-source')) {
source = this._timeSettings.get_string('time-source');
if (this.#settings.get_boolean('manual-time-source')) {
source = this.#settings.get_string('time-source');
console.debug(`Time source is forced to ${source}.`);
if (
(source === 'nightlight' && !this._colorSettings.get_boolean('night-light-enabled')) ||
(source === 'location' && !this._locationSettings.get_boolean('enabled'))
(source === 'nightlight' && !this.#colorSettings.get_boolean('night-light-enabled')) ||
(source === 'location' && !this.#locationSettings.get_boolean('enabled'))
) {
console.debug(`Unable to choose ${source} time source, falling back to manual schedule.`);
source = 'schedule';
this._timeSettings.set_string('time-source', source);
this.#settings.set_string('time-source', source);
}
} else {
if (this._colorSettings.get_boolean('night-light-enabled'))
if (this.#colorSettings.get_boolean('night-light-enabled'))
source = 'nightlight';
else if (this._locationSettings.get_boolean('enabled'))
else if (this.#locationSettings.get_boolean('enabled'))
source = 'location';
else
source = 'schedule';
console.debug(`Time source is ${source}.`);
this._timeSettings.set_string('time-source', source);
this.#settings.set_string('time-source', source);
}
return source;
}
+50 -47
View File
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Geoclue, GLib } = imports.gi;
@@ -26,81 +26,84 @@ const { Time } = Me.imports.enums.Time;
* case.
*/
var TimerLocation = class {
#suntimes;
#previouslyDaytime = null;
#geoclue = null;
#geoclueConnection = null;
#timeChangeTimer = null;
#regularlyUpdateSuntimesTimer = null;
constructor() {
this._previouslyDaytime = null;
// Before we have the location suntimes, we'll use the manual schedule
// times
const timeSettings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this._suntimes = new Map([
this.#suntimes = new Map([
['sunrise', timeSettings.get_double('schedule-sunrise')],
['sunset', timeSettings.get_double('schedule-sunset')],
]);
this._geoclue = null;
this._geoclueConnection = null;
this._timeChangeTimer = null;
this._regularlyUpdateSuntimesTimer = null;
}
enable() {
console.debug('Enabling Location Timer...');
this._connectToGeoclue();
this._watchForTimeChange();
this._regularlyUpdateSuntimes();
this.#connectToGeoclue();
this.#watchForTimeChange();
this.#regularlyUpdateSuntimes();
console.debug('Location Timer enabled.');
}
disable() {
console.debug('Disabling Location Timer...');
this._stopRegularlyUpdatingSuntimes();
this._stopWatchingForTimeChange();
this._disconnectFromGeoclue();
this.#stopRegularlyUpdatingSuntimes();
this.#stopWatchingForTimeChange();
this.#disconnectFromGeoclue();
console.debug('Location Timer disabled.');
}
get time() {
return this._isDaytime() ? Time.DAY : Time.NIGHT;
return this.#isDaytime() ? Time.DAY : Time.NIGHT;
}
_connectToGeoclue() {
#connectToGeoclue() {
console.debug('Connecting to GeoClue...');
Geoclue.Simple.new(
'org.gnome.Shell',
Geoclue.AccuracyLevel.CITY,
null,
this._onGeoclueReady.bind(this)
this.#onGeoclueReady.bind(this)
);
}
_disconnectFromGeoclue() {
#disconnectFromGeoclue() {
console.debug('Disconnecting from GeoClue...');
if (this._geoclueConnection) {
this._geoclue.disconnect(this._geoclueConnection);
this._geoclueConnection = null;
if (this.#geoclueConnection) {
this.#geoclue.disconnect(this.#geoclueConnection);
this.#geoclueConnection = null;
}
console.debug('Disconnected from GeoClue.');
}
_onGeoclueReady(_, result) {
this._geoclue = Geoclue.Simple.new_finish(result);
this._geoclueConnection = this._geoclue.connect('notify::location', this._onLocationUpdated.bind(this));
#onGeoclueReady(_, result) {
this.#geoclue = Geoclue.Simple.new_finish(result);
this.#geoclueConnection = this.#geoclue.connect('notify::location', this.#onLocationUpdated.bind(this));
console.debug('Connected to GeoClue.');
this._onLocationUpdated();
this.#onLocationUpdated();
}
_onLocationUpdated(_geoclue, _location) {
#onLocationUpdated(_geoclue, _location) {
console.debug('Location has changed.');
this._updateLocation();
this._updateSuntimes();
this.#updateLocation();
this.#updateSuntimes();
}
_updateLocation() {
if (this._geoclue) {
#updateLocation() {
if (this.#geoclue) {
console.debug('Updating location...');
const { latitude, longitude } = this._geoclue.get_location();
const { latitude, longitude } = this.#geoclue.get_location();
this.location = new Map([
['latitude', latitude],
['longitude', longitude],
@@ -109,7 +112,7 @@ var TimerLocation = class {
}
}
_updateSuntimes() {
#updateSuntimes() {
if (!this.location)
return;
@@ -153,48 +156,48 @@ var TimerLocation = class {
const sunrise = timeSunrise * 24;
const sunset = timeSunset * 24;
this._suntimes.set('sunrise', sunrise);
this._suntimes.set('sunset', sunset);
this.#suntimes.set('sunrise', sunrise);
this.#suntimes.set('sunset', sunset);
console.debug(`New sun times: (sunrise: ${sunrise}; sunset: ${sunset})`);
}
_regularlyUpdateSuntimes() {
#regularlyUpdateSuntimes() {
console.debug('Regularly updating sun times...');
this._regularlyUpdateSuntimesTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 3600, () => {
this._updateSuntimes();
this.#regularlyUpdateSuntimesTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 3600, () => {
this.#updateSuntimes();
return true; // Repeat the loop
});
}
_stopRegularlyUpdatingSuntimes() {
GLib.Source.remove(this._regularlyUpdateSuntimesTimer);
this._regularlyUpdateSuntimesTimer = null;
#stopRegularlyUpdatingSuntimes() {
GLib.Source.remove(this.#regularlyUpdateSuntimesTimer);
this.#regularlyUpdateSuntimesTimer = null;
console.debug('Stopped regularly updating sun times.');
}
_isDaytime() {
#isDaytime() {
const time = GLib.DateTime.new_now_local();
const hour = time.get_hour() + time.get_minute() / 60 + time.get_second() / 3600;
return hour >= this._suntimes.get('sunrise') && hour <= this._suntimes.get('sunset');
return hour >= this.#suntimes.get('sunrise') && hour <= this.#suntimes.get('sunset');
}
_watchForTimeChange() {
#watchForTimeChange() {
console.debug('Watching for time change...');
this._timeChangeTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
this.#timeChangeTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
if (!Me.imports.extension.enabled) {
// The extension doesn't exist anymore, quit the loop
return false;
}
if (this._previouslyDaytime !== this._isDaytime()) {
this._previouslyDaytime = this._isDaytime();
if (this.#previouslyDaytime !== this.#isDaytime()) {
this.#previouslyDaytime = this.#isDaytime();
this.emit('time-changed', this.time);
}
return true; // Repeat the loop
});
}
_stopWatchingForTimeChange() {
GLib.Source.remove(this._timeChangeTimer);
#stopWatchingForTimeChange() {
GLib.Source.remove(this.#timeChangeTimer);
console.debug('Stopped watching for time change.');
}
};
+42 -39
View File
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Gio } = imports.gi;
@@ -28,41 +28,44 @@ const COLOR_INTERFACE = `
* 'NightLightActive' property and will signal any change.
*/
var TimerNightlight = class {
#settings;
#colorDbusProxy = null;
#settingsConnections = [];
#nightlightStateConnection = null;
#previousNightlightActive = null;
constructor() {
this._timeSettings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this._colorDbusProxy = null;
this._settingsConnections = [];
this._nightlightStateConnection = null;
this._previousNightlightActive = null;
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
}
enable() {
console.debug('Enabling Night Light Timer...');
this._connectToColorDbusProxy();
this._connectSettings();
this._listenToNightlightState();
this.#connectToColorDbusProxy();
this.#connectSettings();
this.#listenToNightlightState();
this.emit('time-changed', this.time);
console.debug('Night Light Timer enabled.');
}
disable() {
console.debug('Disabling Night Light Timer...');
this._stopListeningToNightlightState();
this._disconnectSettings();
this._disconnectFromColorDbusProxy();
this.#stopListeningToNightlightState();
this.#disconnectSettings();
this.#disconnectFromColorDbusProxy();
console.debug('Night Light Timer disabled.');
}
get time() {
return this._isNightlightActive() ? Time.NIGHT : Time.DAY;
return this.#isNightlightActive() ? Time.NIGHT : Time.DAY;
}
_connectToColorDbusProxy() {
#connectToColorDbusProxy() {
console.debug('Connecting to Color DBus proxy...');
const ColorProxy = Gio.DBusProxy.makeProxyWrapper(COLOR_INTERFACE);
this._colorDbusProxy = new ColorProxy(
this.#colorDbusProxy = new ColorProxy(
Gio.DBus.session,
'org.gnome.SettingsDaemon.Color',
'/org/gnome/SettingsDaemon/Color'
@@ -70,57 +73,57 @@ var TimerNightlight = class {
console.debug('Connected to Color DBus proxy.');
}
_disconnectFromColorDbusProxy() {
#disconnectFromColorDbusProxy() {
console.debug('Disconnecting from Color DBus proxy...');
this._colorDbusProxy = null;
this.#colorDbusProxy = null;
console.debug('Disconnected from Color DBus proxy.');
}
_connectSettings() {
#connectSettings() {
console.debug('Connecting Night Light Timer to settings...');
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::nightlight-follow-disable', this._onNightlightFollowDisableChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::nightlight-follow-disable', this.#onNightlightFollowDisableChanged.bind(this)),
});
}
_disconnectSettings() {
#disconnectSettings() {
console.debug('Disconnecting Night Light Timer from settings...');
this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this._settingsConnections = [];
this.#settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this.#settingsConnections = [];
}
_listenToNightlightState() {
#listenToNightlightState() {
console.debug('Listening to Night Light state...');
this._nightlightStateConnection = this._colorDbusProxy.connect(
this.#nightlightStateConnection = this.#colorDbusProxy.connect(
'g-properties-changed',
this._onNightlightStateChanged.bind(this)
this.#onNightlightStateChanged.bind(this)
);
}
_stopListeningToNightlightState() {
this._colorDbusProxy.disconnect(this._nightlightStateConnection);
#stopListeningToNightlightState() {
this.#colorDbusProxy.disconnect(this.#nightlightStateConnection);
console.debug('Stopped listening to Night Light state.');
}
_onNightlightFollowDisableChanged() {
this._onNightlightStateChanged();
#onNightlightFollowDisableChanged() {
this.#onNightlightStateChanged();
}
_onNightlightStateChanged(_sender, _dbusProperties) {
if (this._isNightlightActive() !== this._previousNightlightActive) {
console.debug(`Night Light has become ${this._isNightlightActive() ? '' : 'in'}active.`);
this._previousNightlightActive = this._isNightlightActive();
#onNightlightStateChanged(_sender, _dbusProperties) {
if (this.#isNightlightActive() !== this.#previousNightlightActive) {
console.debug(`Night Light has become ${this.#isNightlightActive() ? '' : 'in'}active.`);
this.#previousNightlightActive = this.#isNightlightActive();
this.emit('time-changed', this.time);
}
}
_isNightlightActive() {
return this._timeSettings.get_boolean('nightlight-follow-disable')
? !this._colorDbusProxy.DisabledUntilTomorrow && this._colorDbusProxy.NightLightActive
: this._colorDbusProxy.NightLightActive;
#isNightlightActive() {
return this.#settings.get_boolean('nightlight-follow-disable')
? !this.#colorDbusProxy.DisabledUntilTomorrow && this.#colorDbusProxy.NightLightActive
: this.#colorDbusProxy.NightLightActive;
}
};
Signals.addSignalMethods(TimerNightlight.prototype);
+72 -69
View File
@@ -25,163 +25,166 @@ const { Time } = Me.imports.enums.Time;
* The user can change the key combination in the extension's preferences.
*/
var TimerOndemand = class {
#settings;
#settingsConnections = [];
#button = null;
#previousKeybinding = null;
#timerConnection = null;
constructor() {
this._timeSettings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this._settingsConnections = [];
this._button = null;
this._previousKeybinding = null;
this._timerConnection = null;
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
}
enable() {
console.debug('Enabling On-demand Timer...');
this._connectSettings();
this._addKeybinding();
this._addButton();
this._connectTimer();
this.#connectSettings();
this.#addKeybinding();
this.#addButton();
this.#connectTimer();
this.emit('time-changed', this.time);
console.debug('On-demand Timer enabled.');
}
disable() {
console.debug('Disabling On-demand Timer...');
this._disconnectTimer();
this._removeKeybinding();
this._removeButton();
this._disconnectSettings();
this.#disconnectTimer();
this.#removeKeybinding();
this.#removeButton();
this.#disconnectSettings();
console.debug('On-demand Timer disabled.');
}
get time() {
return this._timeSettings.get_string('ondemand-time') === 'day' ? Time.DAY : Time.NIGHT;
return this.#settings.get_string('ondemand-time') === 'day' ? Time.DAY : Time.NIGHT;
}
_connectSettings() {
#connectSettings() {
console.debug('Connecting On-demand Timer to settings...');
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::ondemand-time', this._onOndemandTimeChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::ondemand-time', this.#onOndemandTimeChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::nightthemeswitcher-ondemand-keybinding', this._onOndemandKeybindingChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::nightthemeswitcher-ondemand-keybinding', this.#onOndemandKeybindingChanged.bind(this)),
});
this._settingsConnections.push({
settings: this._timeSettings,
id: this._timeSettings.connect('changed::ondemand-button-placement', this._onOndemandButtonPlacementChanged.bind(this)),
this.#settingsConnections.push({
settings: this.#settings,
id: this.#settings.connect('changed::ondemand-button-placement', this.#onOndemandButtonPlacementChanged.bind(this)),
});
}
_disconnectSettings() {
#disconnectSettings() {
console.debug('Disconnecting On-demand Timer from settings...');
this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this._settingsConnections = [];
this.#settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this.#settingsConnections = [];
}
_connectTimer() {
#connectTimer() {
console.debug('Connecting On-demand Timer to Timer...');
this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this));
this.#timerConnection = e.timer.connect('time-changed', this.#onTimeChanged.bind(this));
}
_disconnectTimer() {
if (this._timerConnection) {
e.timer.disconnect(this._timerConnection);
this._timerConnection = null;
#disconnectTimer() {
if (this.#timerConnection) {
e.timer.disconnect(this.#timerConnection);
this.#timerConnection = null;
}
console.debug('Disconnected On-demand Timer from Timer.');
}
_onOndemandTimeChanged() {
#onOndemandTimeChanged() {
this.emit('time-changed', this.time);
}
_onOndemandKeybindingChanged() {
this._removeKeybinding();
this._addKeybinding();
#onOndemandKeybindingChanged() {
this.#removeKeybinding();
this.#addKeybinding();
}
_onOndemandButtonPlacementChanged() {
this._removeButton();
this._addButton();
#onOndemandButtonPlacementChanged() {
this.#removeButton();
this.#addButton();
}
_onTimeChanged(_timer, _newTime) {
this._timeSettings.set_string('ondemand-time', e.timer.time);
this._updateButton();
#onTimeChanged(_timer, _newTime) {
this.#settings.set_string('ondemand-time', e.timer.time);
this.#updateButton();
}
_addKeybinding() {
this._previousKeybinding = this._timeSettings.get_strv('nightthemeswitcher-ondemand-keybinding')[0];
if (!this._timeSettings.get_strv('nightthemeswitcher-ondemand-keybinding')[0])
#addKeybinding() {
this.#previousKeybinding = this.#settings.get_strv('nightthemeswitcher-ondemand-keybinding')[0];
if (!this.#settings.get_strv('nightthemeswitcher-ondemand-keybinding')[0])
return;
console.debug('Adding On-demand Timer keybinding...');
main.wm.addKeybinding(
'nightthemeswitcher-ondemand-keybinding',
this._timeSettings,
this.#settings,
Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW,
this._toggleTime.bind(this)
this.#toggleTime.bind(this)
);
console.debug('Added On-demand Timer keybinding.');
}
_removeKeybinding() {
if (this._previousKeybinding) {
#removeKeybinding() {
if (this.#previousKeybinding) {
console.debug('Removing On-demand Timer keybinding...');
main.wm.removeKeybinding('nightthemeswitcher-ondemand-keybinding');
console.debug('Removed On-demand Timer keybinding.');
}
}
_addButton() {
switch (this._timeSettings.get_string('ondemand-button-placement')) {
#addButton() {
switch (this.#settings.get_string('ondemand-button-placement')) {
case 'panel':
this._addButtonToPanel();
this.#addButtonToPanel();
break;
case 'menu':
this._addButtonToMenu();
this.#addButtonToMenu();
}
}
_removeButton() {
if (this._button) {
#removeButton() {
if (this.#button) {
console.debug('Removing On-demand Timer button...');
this._button.destroy();
this._button = null;
this.#button.destroy();
this.#button = null;
console.debug('Removed On-demand Timer button.');
}
}
_updateButton() {
if (this._button) {
#updateButton() {
if (this.#button) {
console.debug('Updating On-demand Timer button state...');
this._button.update();
this.#button.update();
console.debug('Updated On-demand Timer button state.');
}
}
_addButtonToPanel() {
#addButtonToPanel() {
console.debug('Adding On-demand Timer button to the panel...');
this._button = new NtsPanelMenuButton({ toggleCallback: this._toggleTime.bind(this) });
main.panel.addToStatusArea('NightThemeSwitcherButton', this._button);
this.#button = new NtsPanelMenuButton({ toggleCallback: this.#toggleTime.bind(this) });
main.panel.addToStatusArea('NightThemeSwitcherButton', this.#button);
console.debug('Added On-demand Timer button to the panel.');
}
_addButtonToMenu() {
#addButtonToMenu() {
console.debug('Adding On-demand Timer button to the menu...');
const aggregateMenu = main.panel.statusArea.aggregateMenu;
const position = utils.findShellAggregateMenuItemPosition(aggregateMenu._system.menu) - 1;
this._button = new NtsPopupSubMenuMenuItem({ toggleCallback: this._toggleTime.bind(this) });
aggregateMenu.menu.addMenuItem(this._button, position);
this.#button = new NtsPopupSubMenuMenuItem({ toggleCallback: this.#toggleTime.bind(this) });
aggregateMenu.menu.addMenuItem(this.#button, position);
console.debug('Added On-demand Timer button to the menu.');
}
_toggleTime() {
this._timeSettings.set_string('ondemand-time', e.timer.time === Time.DAY ? Time.NIGHT : Time.DAY);
#toggleTime() {
this.#settings.set_string('ondemand-time', e.timer.time === Time.DAY ? Time.NIGHT : Time.DAY);
this.emit('time-changed', this.time);
}
};
+18 -15
View File
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-FileCopyrightText: 2020-2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { GLib } = imports.gi;
@@ -21,54 +21,57 @@ const { Time } = Me.imports.enums.Time;
* The user can change the schedule in the extension's preferences.
*/
var TimerSchedule = class {
#settings;
#previouslyDaytime = null;
#timeChangeTimer = null;
constructor() {
this._timeSettings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
this._previouslyDaytime = null;
this._timeChangeTimer = null;
this.#settings = extensionUtils.getSettings(utils.getSettingsSchema('time'));
}
enable() {
console.debug('Enabling Schedule Timer...');
this._watchForTimeChange();
this.#watchForTimeChange();
this.emit('time-changed', this.time);
console.debug('Schedule Timer enabled.');
}
disable() {
console.debug('Disabling Schedule Timer...');
this._stopWatchingForTimeChange();
this.#stopWatchingForTimeChange();
console.debug('Schedule Timer disabled.');
}
get time() {
return this._isDaytime() ? Time.DAY : Time.NIGHT;
return this.#isDaytime() ? Time.DAY : Time.NIGHT;
}
_isDaytime() {
#isDaytime() {
const time = GLib.DateTime.new_now_local();
const hour = time.get_hour() + time.get_minute() / 60 + time.get_second() / 3600;
return hour >= this._timeSettings.get_double('schedule-sunrise') && hour <= this._timeSettings.get_double('schedule-sunset');
return hour >= this.#settings.get_double('schedule-sunrise') && hour <= this.#settings.get_double('schedule-sunset');
}
_watchForTimeChange() {
#watchForTimeChange() {
console.debug('Watching for time change...');
this._timeChangeTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
this.#timeChangeTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
if (!Me.imports.extension.enabled) {
// The extension doesn't exist anymore, quit the loop
return false;
}
if (this._previouslyDaytime !== this._isDaytime()) {
this._previouslyDaytime = this._isDaytime();
if (this.#previouslyDaytime !== this.#isDaytime()) {
this.#previouslyDaytime = this.#isDaytime();
this.emit('time-changed', this.time);
}
return true; // Repeat the loop
});
}
_stopWatchingForTimeChange() {
GLib.Source.remove(this._timeChangeTimer);
#stopWatchingForTimeChange() {
GLib.Source.remove(this.#timeChangeTimer);
console.debug('Stopped watching for time change.');
}
};