Add on-demand option for all automatic sources

This commit is contained in:
Romain
2020-11-06 10:36:50 +00:00
parent d7fed9aea3
commit 3cc980ff4e
8 changed files with 520 additions and 320 deletions
+48 -46
View File
@@ -35,8 +35,8 @@ const { TimerOndemand } = Me.imports.modules.TimerOndemand;
* They can connect to its 'time-changed' signal and ask its 'time' property
* for the current time.
*
* It will try to use one of this three different time sources, in this order of
* preference:
* It will try to use one of these three different time sources, in this order
* of preference:
* - Night Light
* - Location Services
* - Manual schedule
@@ -47,35 +47,35 @@ const { TimerOndemand } = Me.imports.modules.TimerOndemand;
var Timer = class {
constructor() {
this._source = null;
this._sources = [];
this._previousTime = null;
this._nightlightStatusChangedConnect = null;
this._locationStatusChangedConnect = null;
this._manualTimeSourceChangedConnect = null;
this._timeSourceChangedConnect = null;
this._timeChangedConnect = null;
this._timeChangedConnects = [];
}
enable() {
logDebug('Enabling Timer...');
this._connectSettings();
this._createSource();
this._connectSource();
this._enableSource();
this._createSources();
this._connectSources();
this._enableSources();
logDebug('Timer enabled.');
}
disable() {
logDebug('Disabling Timer...');
this._disconnectSource();
this._disableSource();
this._disconnectSources();
this._disableSources();
this._disconnectSettings();
logDebug('Timer disabled.');
}
get time() {
return this._source ? this._source.time : null;
return this._previousTime;
}
@@ -84,6 +84,7 @@ var Timer = class {
this._nightlightStatusChangedConnect = e.settings.system.connect('nightlight-status-changed', this._onSourceChanged.bind(this));
this._locationStatusChangedConnect = e.settings.system.connect('location-status-changed', this._onSourceChanged.bind(this));
this._manualTimeSourceChangedConnect = e.settings.time.connect('manual-time-source-changed', this._onSourceChanged.bind(this));
this._alwaysEnableOndemandChangedConnect = e.settings.time.connect('always-enable-ondemand-changed', this._onSourceChanged.bind(this));
this._timeSourceChangedConnect = e.settings.time.connect('time-source-changed', this._onTimeSourceChanged.bind(this));
}
@@ -107,45 +108,48 @@ var Timer = class {
logDebug('Disconnected Timer from settings.');
}
_createSource() {
switch (this._getSource()) {
_createSources() {
const source = this._getSource();
switch (source) {
case 'nightlight':
this._source = new TimerNightlight();
this._sources.push(new TimerNightlight());
break;
case 'location':
this._source = new TimerLocation();
this._sources.push(new TimerLocation());
break;
case 'schedule':
this._source = new TimerSchedule();
this._sources.push(new TimerSchedule());
break;
case 'ondemand':
this._source = new TimerOndemand();
this._sources.push(new TimerOndemand());
break;
}
if (e.settings.time.alwaysEnableOndemand && ['nightlight', 'location', 'schedule'].includes(source))
this._sources.push(new TimerOndemand());
}
_enableSource() {
this._source.enable();
_enableSources() {
this._sources.forEach(source => source.enable());
}
_disableSource() {
if (this._source) {
this._source.disable();
this._source = null;
}
_disableSources() {
this._sources.forEach(source => source.disable());
this._sources = [];
}
_connectSource() {
logDebug('Connecting to time source...');
this._timeChangedConnect = this._source.connect('time-changed', this._onTimeChanged.bind(this));
_connectSources() {
logDebug('Connecting to time sources...');
this._sources.forEach(source => this._timeChangedConnects.push({
source,
connect: source.connect('time-changed', this._onTimeChanged.bind(this)),
}));
}
_disconnectSource() {
if (this._timeChangedConnect) {
this._source.disconnect(this._timeChangedConnect);
this._timeChangedConnect = null;
}
logDebug('Disconnected from time source.');
_disconnectSources() {
this._timeChangedConnects.forEach(timeChangedConnect => timeChangedConnect.source.disconnect(timeChangedConnect.connect));
this._timeChangedConnects = [];
logDebug('Disconnected from time sources.');
}
@@ -162,8 +166,8 @@ var Timer = class {
_onTimeChanged(_source, newTime) {
if (newTime !== this._previousTime) {
logDebug(`Time has changed to ${newTime}.`);
this.emit('time-changed', newTime);
this._previousTime = newTime;
this.emit('time-changed', newTime);
}
}
@@ -171,8 +175,9 @@ var Timer = class {
_getSource() {
logDebug('Getting time source...');
let source;
if (e.settings.time.manualTimeSource) {
let source = e.settings.time.timeSource;
source = e.settings.time.timeSource;
logDebug(`Time source is forced to ${source}.`);
if (
(source === 'nightlight' && !e.settings.system.nightlightEnabled) ||
@@ -182,19 +187,16 @@ var Timer = class {
source = 'schedule';
e.settings.time.timeSource = source;
}
return source;
} else {
if (e.settings.system.nightlightEnabled)
source = 'nightlight';
else if (e.settings.system.locationEnabled)
source = 'location';
else
source = 'schedule';
logDebug(`Time source is ${source}.`);
e.settings.time.timeSource = source;
}
let source;
if (e.settings.system.nightlightEnabled)
source = 'nightlight';
else if (e.settings.system.locationEnabled)
source = 'location';
else
source = 'schedule';
logDebug(`Time source is ${source}.`);
e.settings.time.timeSource = source;
return source;
}
+35 -3
View File
@@ -47,6 +47,7 @@ var TimerOndemand = class {
this._previousKeybinding = null;
this._ondemandKeybindingConnect = null;
this._ondemandButtonPlacementConnect = null;
this._timeChangedConnect = null;
}
enable() {
@@ -54,12 +55,14 @@ var TimerOndemand = class {
this._connectSettings();
this._addKeybinding();
this._addButton();
this._connectTimer();
this.emit('time-changed', this.time);
logDebug('On-demand Timer enabled.');
}
disable() {
logDebug('Disabling On-demand Timer...');
this._disconnectTimer();
this._removeKeybinding();
this._removeButton();
this._disconnectSettings();
@@ -90,6 +93,19 @@ var TimerOndemand = class {
}
}
_connectTimer() {
logDebug('Connecting On-demand Timer to Timer...');
this._timeChangedConnect = e.timer.connect('time-changed', this._onTimeChanged.bind(this));
}
_disconnectTimer() {
if (this._timeChangedConnect) {
e.timer.disconnect(this._timeChangedConnect);
this._timeChangedConnect = null;
}
logDebug('Disconnected On-demand Timer from Timer.');
}
_onOndemandKeybindingChanged(_settings, _keybinding) {
this._removeKeybinding();
@@ -101,6 +117,10 @@ var TimerOndemand = class {
this._addButton();
}
_onTimeChanged(_timer, _newTime) {
this._updateButton();
}
_addKeybinding() {
this._previousKeybinding = e.settings.time.ondemandKeybinding;
@@ -145,6 +165,14 @@ var TimerOndemand = class {
}
}
_updateButton() {
if (this._button) {
logDebug('Updating On-demand Timer button state...');
this._button.update();
logDebug('Updated On-demand Timer button state.');
}
}
_addButtonToPanel() {
logDebug('Adding On-demand Timer button to the panel...');
const icon = new St.Icon({
@@ -154,9 +182,11 @@ var TimerOndemand = class {
this._button = new PanelMenuButton(0.0);
const buttonActor = getActor(this._button);
buttonActor.add_actor(icon);
this._button.update = () => {
icon.gicon = this._getIconForTime(e.timer.time);
};
buttonActor.connect('button-press-event', () => {
this._toggleTime();
icon.gicon = this._getIconForTime(e.timer.time);
});
main.panel.addToStatusArea('NightThemeSwitcherButton', this._button);
logDebug('Added On-demand Timer button to the panel.');
@@ -167,10 +197,12 @@ var TimerOndemand = class {
const aggregateMenu = main.panel.statusArea.aggregateMenu;
const position = findShellAggregateMenuItemPosition(aggregateMenu._system.menu) - 1;
this._button = new PopupImageMenuItem(this._getLabelForTime(e.timer.time), this._getIconForTime(e.timer.time));
this._button.connect('activate', () => {
this._toggleTime();
this._button.update = () => {
this._button.label.text = this._getLabelForTime(e.timer.time);
this._button.setIcon(this._getIconForTime(e.timer.time));
};
this._button.connect('activate', () => {
this._toggleTime();
});
aggregateMenu.menu.addMenuItem(this._button, position);
logDebug('Added On-demand Timer button to the menu.');