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',
]
enums = [
'enums/SourceType.js',
'enums/SunTime.js',
'enums/Time.js',
]
modules = [
'modules/Source.js',
'modules/SourceLocation.js',
'modules/SourceSchedule.js',
'modules/Switcher.js',
'modules/SwitcherCommands.js',
'modules/SwitcherTheme.js',
'modules/Timer.js',
'modules/TimerLocation.js',
'modules/TimerSchedule.js',
]
preferences = [
'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 { Source } = Me.imports.modules.Source;
const { SunTime } = Me.imports.enums.SunTime;
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.
*
* 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.
*
* Every second, it will check if the time has changed and signal if that's the
* case.
*/
var TimerLocation = class {
var SourceLocation = class extends Source {
#suntimes;
#cancellable = null;
@@ -36,32 +39,35 @@ var TimerLocation = class {
#regularlyUpdateSuntimesTimer = null;
constructor() {
super();
// Before we have the location suntimes, we'll use the manual schedule
// times
const timeSettings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`);
this.#suntimes = new Map([
['sunrise', timeSettings.get_double('schedule-sunrise')],
['sunset', timeSettings.get_double('schedule-sunset')],
[SunTime.SUNRISE, timeSettings.get_double('schedule-sunrise')],
[SunTime.SUNSET, timeSettings.get_double('schedule-sunset')],
]);
}
enable() {
debug.message('Enabling Location Timer...');
super.enable();
debug.message('Enabling Location source...');
this.#cancellable = new Gio.Cancellable();
this.#connectToGeoclue();
this.#watchForTimeChange();
this.#regularlyUpdateSuntimes();
debug.message('Location Timer enabled.');
debug.message('Location source enabled.');
}
disable() {
debug.message('Disabling Location Timer...');
debug.message('Disabling Location source...');
this.#stopRegularlyUpdatingSuntimes();
this.#stopWatchingForTimeChange();
this.#disconnectFromGeoclue();
this.#cancellable.cancel();
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));
debug.message('Connected to GeoClue.');
this.#onLocationUpdated();
this.emit('time-changed', this.time);
}
#onLocationUpdated(_geoclue, _location) {
@@ -160,8 +167,8 @@ var TimerLocation = class {
const sunrise = timeSunrise * 24;
const sunset = timeSunset * 24;
this.#suntimes.set('sunrise', sunrise);
this.#suntimes.set('sunset', sunset);
this.#suntimes.set(SunTime.SUNRISE, sunrise);
this.#suntimes.set(SunTime.SUNSET, sunset);
debug.message(`New sun times: (sunrise: ${sunrise}; sunset: ${sunset})`);
}
@@ -182,7 +189,7 @@ var TimerLocation = class {
#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(SunTime.SUNRISE) && hour <= this.#suntimes.get(SunTime.SUNSET);
}
#watchForTimeChange() {
@@ -201,4 +208,4 @@ var TimerLocation = class {
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 { Source } = Me.imports.modules.Source;
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
* case.
*
* The user can change the schedule in the extension's preferences.
*/
var TimerSchedule = class {
var SourceSchedule = class extends Source {
#settings;
#previouslyDaytime = null;
#timeChangeTimer = null;
constructor() {
super();
this.#settings = extensionUtils.getSettings(`${Me.metadata['settings-schema']}.time`);
}
enable() {
debug.message('Enabling Schedule Timer...');
super.enable();
debug.message('Enabling Schedule source...');
this.#watchForTimeChange();
this.emit('time-changed', this.time);
debug.message('Schedule Timer enabled.');
debug.message('Schedule source enabled.');
}
disable() {
debug.message('Disabling Schedule Timer...');
debug.message('Disabling Schedule source...');
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.');
}
};
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 { SourceType } = Me.imports.enums.SourceType;
const { Time } = Me.imports.enums.Time;
const { TimerLocation } = Me.imports.modules.TimerLocation;
const { TimerSchedule } = Me.imports.modules.TimerSchedule;
const { SourceLocation } = Me.imports.modules.SourceLocation;
const { SourceSchedule } = Me.imports.modules.SourceSchedule;
/**
@@ -108,11 +109,11 @@ var Timer = class {
#createSource() {
const source = this.#getSource();
switch (source) {
case 'location':
this.#source = new TimerLocation();
case SourceType.LOCATION:
this.#source = new SourceLocation();
break;
case 'schedule':
this.#source = new TimerSchedule();
case SourceType.SCHEDULE:
this.#source = new SourceSchedule();
break;
}
}
@@ -161,9 +162,9 @@ var Timer = class {
#getSource() {
debug.message('Getting time source...');
let source = 'schedule';
let source = SourceType.SCHEDULE;
if (this.#locationSettings.get_boolean('enabled') && !this.#settings.get_boolean('manual-schedule'))
source = 'location';
source = SourceType.LOCATION;
debug.message(`Time source is ${source}.`);
return source;
}