Directly use gsettings
This commit is contained in:
+55
-49
@@ -1,12 +1,14 @@
|
||||
// SPDX-FileCopyrightText: 2020, 2021 Romain Vigier <contact AT romainvigier.fr>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
const { Gio } = imports.gi;
|
||||
const { extensionUtils } = imports.misc;
|
||||
|
||||
const Me = extensionUtils.getCurrentExtension();
|
||||
|
||||
const e = Me.imports.extension;
|
||||
const { logDebug } = Me.imports.utils;
|
||||
const utils = Me.imports.utils;
|
||||
const { logDebug } = utils;
|
||||
|
||||
/**
|
||||
* The Backgrounder is responsible for changing the desktop background
|
||||
@@ -17,19 +19,20 @@ const { logDebug } = Me.imports.utils;
|
||||
*/
|
||||
var Backgrounder = class {
|
||||
constructor() {
|
||||
this._statusChangedConnect = null;
|
||||
this._backgroundChangedConnect = null;
|
||||
this._systemBackgroundChangedConnect = null;
|
||||
this._backgroundChangedConnect = null;
|
||||
this._backgroundsSettings = extensionUtils.getSettings(utils.getSettingsSchema('backgrounds'));
|
||||
this._systemBackgroundSettings = new Gio.Settings({ schema: 'org.gnome.desktop.background' });
|
||||
this._settingsConnections = [];
|
||||
this._statusConnection = null;
|
||||
this._timerConnection = null;
|
||||
}
|
||||
|
||||
enable() {
|
||||
logDebug('Enabling Backgrounder...');
|
||||
this._watchStatus();
|
||||
if (e.settings.backgrounds.enabled) {
|
||||
if (this._backgroundsSettings.get_boolean('enabled')) {
|
||||
this._connectSettings();
|
||||
this._connectTimer();
|
||||
this._changeSystemBackground(e.timer.time);
|
||||
this._updateSystemBackground(e.timer.time);
|
||||
}
|
||||
logDebug('Backgrounder enabled.');
|
||||
}
|
||||
@@ -45,83 +48,86 @@ var Backgrounder = class {
|
||||
|
||||
_watchStatus() {
|
||||
logDebug('Watching backgrounds status...');
|
||||
this._statusChangedConnect = e.settings.backgrounds.connect('status-changed', this._onStatusChanged.bind(this));
|
||||
this._statusConnection = this._backgroundsSettings.connect('changed::enabled', this._onStatusChanged.bind(this));
|
||||
}
|
||||
|
||||
_unwatchStatus() {
|
||||
if (this._statusChangedConnect) {
|
||||
e.settings.backgrounds.disconnect(this._statusChangedConnect);
|
||||
this._statusChangedConnect = null;
|
||||
if (this._statusConnection) {
|
||||
this._backgroundsSettings.disconnect(this._statusConnection);
|
||||
this._statusConnection = null;
|
||||
}
|
||||
logDebug('Stopped watching backgrounds status.');
|
||||
}
|
||||
|
||||
_connectSettings() {
|
||||
logDebug('Connecting Backgrounder to settings...');
|
||||
this._backgroundChangedConnect = e.settings.backgrounds.connect('background-changed', this._onBackgroundChanged.bind(this));
|
||||
this._systemBackgroundChangedConnect = e.settings.system.connect('background-changed', this._onSystemBackgroundChanged.bind(this));
|
||||
this._settingsConnections.push({
|
||||
settings: this._backgroundsSettings,
|
||||
id: this._backgroundsSettings.connect('changed::day', this._onDayBackgroundChanged.bind(this)),
|
||||
});
|
||||
this._settingsConnections.push({
|
||||
settings: this._backgroundsSettings,
|
||||
id: this._backgroundsSettings.connect('changed::night', this._onNightBackgroundChanged.bind(this)),
|
||||
});
|
||||
this._settingsConnections.push({
|
||||
settings: this._systemBackgroundSettings,
|
||||
id: this._systemBackgroundSettings.connect('changed::picture-uri', this._onSystemBackgroundChanged.bind(this)),
|
||||
});
|
||||
}
|
||||
|
||||
_disconnectSettings() {
|
||||
if (this._backgroundChangedConnect) {
|
||||
e.settings.backgrounds.disconnect(this._backgroundChangedConnect);
|
||||
this._backgroundChangedConnect = null;
|
||||
}
|
||||
if (this._systemBackgroundChangedConnect) {
|
||||
e.settings.system.disconnect(this._systemBackgroundChangedConnect);
|
||||
this._systemBackgroundChangedConnect = null;
|
||||
}
|
||||
this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
|
||||
this._settingsConnections = [];
|
||||
logDebug('Disconnected Backgrounder from settings.');
|
||||
}
|
||||
|
||||
_connectTimer() {
|
||||
logDebug('Connecting Backgrounder to Timer...');
|
||||
this._backgroundChangedConnect = e.timer.connect('time-changed', this._onTimeChanged.bind(this));
|
||||
this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this));
|
||||
}
|
||||
|
||||
_disconnectTimer() {
|
||||
if (this._backgroundChangedConnect) {
|
||||
e.timer.disconnect(this._backgroundChangedConnect);
|
||||
this._backgroundChangedConnect = null;
|
||||
if (this._timerConnection) {
|
||||
e.timer.disconnect(this._timerConnection);
|
||||
this._timerConnection = null;
|
||||
}
|
||||
logDebug('Disconnected Backgrounder from Timer.');
|
||||
}
|
||||
|
||||
|
||||
_onStatusChanged(_settings, _enabled) {
|
||||
_onStatusChanged() {
|
||||
logDebug(`Backgrounds switching has been ${this._backgroundsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`);
|
||||
this.disable();
|
||||
this.enable();
|
||||
}
|
||||
|
||||
_onBackgroundChanged(_settings, changedBackgroundTime) {
|
||||
if (changedBackgroundTime === e.timer.time)
|
||||
this._changeSystemBackground(changedBackgroundTime);
|
||||
_onDayBackgroundChanged() {
|
||||
logDebug(`Day background changed to '${this._backgroundsSettings.get_string('day')}'.`);
|
||||
this._updateSystemBackground();
|
||||
}
|
||||
|
||||
_onSystemBackgroundChanged(_settings, newBackground) {
|
||||
switch (e.timer.time) {
|
||||
case 'day':
|
||||
e.settings.backgrounds.day = newBackground;
|
||||
break;
|
||||
case 'night':
|
||||
e.settings.backgrounds.night = newBackground;
|
||||
}
|
||||
_onNightBackgroundChanged() {
|
||||
logDebug(`Night background changed to '${this._backgroundsSettings.get_string('night')}'.`);
|
||||
this._updateSystemBackground();
|
||||
}
|
||||
|
||||
_onTimeChanged(_timer, newTime) {
|
||||
this._changeSystemBackground(newTime);
|
||||
_onSystemBackgroundChanged() {
|
||||
logDebug(`System background changed to '${this._systemBackgroundSettings.get_string('picture-uri')}'.`);
|
||||
this._updateCurrentBackground();
|
||||
}
|
||||
|
||||
_onTimeChanged() {
|
||||
this._updateSystemBackground();
|
||||
}
|
||||
|
||||
|
||||
_changeSystemBackground(time) {
|
||||
switch (time) {
|
||||
case 'day':
|
||||
if (e.settings.backgrounds.day)
|
||||
e.settings.system.background = e.settings.backgrounds.day;
|
||||
break;
|
||||
case 'night':
|
||||
if (e.settings.backgrounds.night)
|
||||
e.settings.system.background = e.settings.backgrounds.night;
|
||||
}
|
||||
_updateCurrentBackground() {
|
||||
if (e.timer.time)
|
||||
this._backgroundsSettings.set_string(e.timer.time, this._systemBackgroundSettings.get_string('picture-uri'));
|
||||
}
|
||||
|
||||
_updateSystemBackground() {
|
||||
if (e.timer.time && this._backgroundsSettings.get_string(e.timer.time))
|
||||
this._systemBackgroundSettings.set_string('picture-uri', this._backgroundsSettings.get_string(e.timer.time));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user