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