Extension: Move Timers to the Source class

This commit is contained in:
Romain Vigier
2022-08-17 12:23:17 +02:00
parent a1ea280fd6
commit f203c9695a
7 changed files with 104 additions and 31 deletions
+13
View File
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* Source types.
*
* @readonly
* @enum {string}
*/
var SourceType = {
LOCATION: 'location',
SCHEDULE: 'schedule',
};
+13
View File
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
/**
* Sun times.
*
* @readonly
* @enum {string}
*/
var SunTime = {
SUNSET: 'sunset',
SUNRISE: 'sunrise',
};
+5 -2
View File
@@ -27,15 +27,18 @@ main = [
'utils.js', 'utils.js',
] ]
enums = [ enums = [
'enums/SourceType.js',
'enums/SunTime.js',
'enums/Time.js', 'enums/Time.js',
] ]
modules = [ modules = [
'modules/Source.js',
'modules/SourceLocation.js',
'modules/SourceSchedule.js',
'modules/Switcher.js', 'modules/Switcher.js',
'modules/SwitcherCommands.js', 'modules/SwitcherCommands.js',
'modules/SwitcherTheme.js', 'modules/SwitcherTheme.js',
'modules/Timer.js', 'modules/Timer.js',
'modules/TimerLocation.js',
'modules/TimerSchedule.js',
] ]
preferences = [ preferences = [
'preferences/BackgroundButton.js', 'preferences/BackgroundButton.js',
+31
View File
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
const { Time } = Me.imports.enums.Time;
/**
* Time source base class.
*
* It needs to be enabled before being able to retrieve the time and disabled
* before being disposed.
*
* It emits the `time-changed` signal containing the new time when the time
* changes.
*/
var Source = class {
enable() {}
disable() {}
/**
* @type {Time}
*/
get time() {
return Time.UNKNOWN;
}
};
@@ -9,23 +9,26 @@ const Me = extensionUtils.getCurrentExtension();
const debug = Me.imports.debug; const debug = Me.imports.debug;
const { Source } = Me.imports.modules.Source;
const { SunTime } = Me.imports.enums.SunTime;
const { Time } = Me.imports.enums.Time; const { Time } = Me.imports.enums.Time;
/** /**
* The Location Timer uses Location Services to get the current sunrise and * The Location source uses Location Services to get the current sunrise and
* sunset times. * sunset times.
* *
* It gets the current user's location with the GeoClue2 DBus proxy and * It gets the current user's location with the GeoClue2 DBus proxy and
* calculate the times. * calculates the times.
* *
* It will recalculate every hour and when the user's location changed to stay * It will recalculate every hour and when the user's location changes to stay
* up to date. * up to date.
* *
* Every second, it will check if the time has changed and signal if that's the * Every second, it will check if the time has changed and signal if that's the
* case. * case.
*/ */
var TimerLocation = class { var SourceLocation = class extends Source {
#suntimes; #suntimes;
#cancellable = null; #cancellable = null;
@@ -36,32 +39,35 @@ var TimerLocation = class {
#regularlyUpdateSuntimesTimer = null; #regularlyUpdateSuntimesTimer = null;
constructor() { constructor() {
super();
// 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(`${Me.metadata['settings-schema']}.time`); const timeSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`);
this.#suntimes = new Map([ this.#suntimes = new Map([
['sunrise', timeSettings.get_double('schedule-sunrise')], [SunTime.SUNRISE, timeSettings.get_double('schedule-sunrise')],
['sunset', timeSettings.get_double('schedule-sunset')], [SunTime.SUNSET, timeSettings.get_double('schedule-sunset')],
]); ]);
} }
enable() { enable() {
debug.message('Enabling Location Timer...'); super.enable();
debug.message('Enabling Location source...');
this.#cancellable = new Gio.Cancellable(); this.#cancellable = new Gio.Cancellable();
this.#connectToGeoclue(); this.#connectToGeoclue();
this.#watchForTimeChange(); this.#watchForTimeChange();
this.#regularlyUpdateSuntimes(); this.#regularlyUpdateSuntimes();
debug.message('Location Timer enabled.'); debug.message('Location source enabled.');
} }
disable() { disable() {
debug.message('Disabling Location Timer...'); debug.message('Disabling Location source...');
this.#stopRegularlyUpdatingSuntimes(); this.#stopRegularlyUpdatingSuntimes();
this.#stopWatchingForTimeChange(); this.#stopWatchingForTimeChange();
this.#disconnectFromGeoclue(); this.#disconnectFromGeoclue();
this.#cancellable.cancel(); this.#cancellable.cancel();
this.#cancellable = null; this.#cancellable = null;
debug.message('Location Timer disabled.'); debug.message('Location source disabled.');
super.disable();
} }
@@ -95,6 +101,7 @@ var TimerLocation = class {
this.#geoclueConnection = this.#geoclue.connect('notify::location', this.#onLocationUpdated.bind(this)); this.#geoclueConnection = this.#geoclue.connect('notify::location', this.#onLocationUpdated.bind(this));
debug.message('Connected to GeoClue.'); debug.message('Connected to GeoClue.');
this.#onLocationUpdated(); this.#onLocationUpdated();
this.emit('time-changed', this.time);
} }
#onLocationUpdated(_geoclue, _location) { #onLocationUpdated(_geoclue, _location) {
@@ -160,8 +167,8 @@ 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(SunTime.SUNRISE, sunrise);
this.#suntimes.set('sunset', sunset); this.#suntimes.set(SunTime.SUNSET, sunset);
debug.message(`New sun times: (sunrise: ${sunrise}; sunset: ${sunset})`); debug.message(`New sun times: (sunrise: ${sunrise}; sunset: ${sunset})`);
} }
@@ -182,7 +189,7 @@ var TimerLocation = class {
#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(SunTime.SUNRISE) && hour <= this.#suntimes.get(SunTime.SUNSET);
} }
#watchForTimeChange() { #watchForTimeChange() {
@@ -201,4 +208,4 @@ var TimerLocation = class {
debug.message('Stopped watching for time change.'); debug.message('Stopped watching for time change.');
} }
}; };
Signals.addSignalMethods(TimerLocation.prototype); Signals.addSignalMethods(SourceLocation.prototype);
@@ -9,38 +9,43 @@ const Me = extensionUtils.getCurrentExtension();
const debug = Me.imports.debug; const debug = Me.imports.debug;
const { Source } = Me.imports.modules.Source;
const { Time } = Me.imports.enums.Time; const { Time } = Me.imports.enums.Time;
/** /**
* The Schedule Timer uses a manual schedule to get the current time. * The Schedule source uses a manual schedule to get the current time.
* *
* Every second, it will check if the time has changed and signal if that's the * Every second, it will check if the time has changed and signal if that's the
* case. * case.
* *
* 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 SourceSchedule = class extends Source {
#settings; #settings;
#previouslyDaytime = null; #previouslyDaytime = null;
#timeChangeTimer = null; #timeChangeTimer = null;
constructor() { constructor() {
super();
this.#settings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`); this.#settings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`);
} }
enable() { enable() {
debug.message('Enabling Schedule Timer...'); super.enable();
debug.message('Enabling Schedule source...');
this.#watchForTimeChange(); this.#watchForTimeChange();
this.emit('time-changed', this.time); this.emit('time-changed', this.time);
debug.message('Schedule Timer enabled.'); debug.message('Schedule source enabled.');
} }
disable() { disable() {
debug.message('Disabling Schedule Timer...'); debug.message('Disabling Schedule source...');
this.#stopWatchingForTimeChange(); this.#stopWatchingForTimeChange();
debug.message('Schedule Timer disabled.'); debug.message('Schedule source disabled.');
super.disable();
} }
@@ -71,4 +76,4 @@ var TimerSchedule = class {
debug.message('Stopped watching for time change.'); debug.message('Stopped watching for time change.');
} }
}; };
Signals.addSignalMethods(TimerSchedule.prototype); Signals.addSignalMethods(SourceSchedule.prototype);
+9 -8
View File
@@ -11,9 +11,10 @@ const Me = extensionUtils.getCurrentExtension();
const debug = Me.imports.debug; const debug = Me.imports.debug;
const { SourceType } = Me.imports.enums.SourceType;
const { Time } = Me.imports.enums.Time; const { Time } = Me.imports.enums.Time;
const { TimerLocation } = Me.imports.modules.TimerLocation; const { SourceLocation } = Me.imports.modules.SourceLocation;
const { TimerSchedule } = Me.imports.modules.TimerSchedule; const { SourceSchedule } = Me.imports.modules.SourceSchedule;
/** /**
@@ -108,11 +109,11 @@ var Timer = class {
#createSource() { #createSource() {
const source = this.#getSource(); const source = this.#getSource();
switch (source) { switch (source) {
case 'location': case SourceType.LOCATION:
this.#source = new TimerLocation(); this.#source = new SourceLocation();
break; break;
case 'schedule': case SourceType.SCHEDULE:
this.#source = new TimerSchedule(); this.#source = new SourceSchedule();
break; break;
} }
} }
@@ -161,9 +162,9 @@ var Timer = class {
#getSource() { #getSource() {
debug.message('Getting time source...'); debug.message('Getting time source...');
let source = 'schedule'; let source = SourceType.SCHEDULE;
if (this.#locationSettings.get_boolean('enabled') && !this.#settings.get_boolean('manual-schedule')) if (this.#locationSettings.get_boolean('enabled') && !this.#settings.get_boolean('manual-schedule'))
source = 'location'; source = SourceType.LOCATION;
debug.message(`Time source is ${source}.`); debug.message(`Time source is ${source}.`);
return source; return source;
} }