Use system background switching

This commit is contained in:
Romain Vigier
2022-02-18 01:56:43 +01:00
parent b340033a06
commit 6e16cde3d0
4 changed files with 50 additions and 197 deletions
+24 -8
View File
@@ -1,20 +1,21 @@
// 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
'use strict';
const { GLib } = imports.gi;
const { Gio, GLib } = imports.gi;
const { extensionUtils } = imports.misc;
const { extensionManager } = imports.ui.main;
const Me = extensionUtils.getCurrentExtension();
const utils = Me.imports.utils;
const { Timer } = Me.imports.modules.Timer;
const { GtkThemer } = Me.imports.modules.GtkThemer;
const { ShellThemer } = Me.imports.modules.ShellThemer;
const { IconThemer } = Me.imports.modules.IconThemer;
const { CursorThemer } = Me.imports.modules.CursorThemer;
const { Backgrounder } = Me.imports.modules.Backgrounder;
const { Commander } = Me.imports.modules.Commander;
@@ -24,7 +25,6 @@ var gtkThemer = null;
var shellThemer = null;
var iconThemer = null;
var cursorThemer = null;
var backgrounder = null;
var commander = null;
@@ -34,6 +34,7 @@ var commander = null;
function init() {
console.debug('Initializing extension...');
extensionUtils.initTranslations();
_migrateBackgroundSettings();
console.debug('Extension initialized.');
}
@@ -55,7 +56,6 @@ function start() {
shellThemer = new ShellThemer();
iconThemer = new IconThemer();
cursorThemer = new CursorThemer();
backgrounder = new Backgrounder();
commander = new Commander();
timer.enable();
@@ -63,7 +63,6 @@ function start() {
shellThemer.enable();
iconThemer.enable();
cursorThemer.enable();
backgrounder.enable();
commander.enable();
enabled = true;
@@ -81,7 +80,6 @@ function disable() {
shellThemer.disable();
iconThemer.disable();
cursorThemer.disable();
backgrounder.disable();
commander.disable();
timer.disable();
@@ -90,7 +88,6 @@ function disable() {
shellThemer = null;
iconThemer = null;
cursorThemer = null;
backgrounder = null;
commander = null;
console.debug('Extension disabled.');
}
@@ -110,3 +107,22 @@ function _waitForExtensionManager() {
resolve();
});
}
/**
* Migrate background settings from the extension to the system.
*/
function _migrateBackgroundSettings() {
const systemSettings = new Gio.Settings({ schema: 'org.gnome.desktop.background' });
const extensionSettings = extensionUtils.getSettings(utils.getSettingsSchema('backgrounds'));
if (extensionSettings.get_string('day')) {
systemSettings.set_string('picture-uri', extensionSettings.get_string('day'));
systemSettings.set_string('picture-uri-dark', extensionSettings.get_string('day'));
extensionSettings.set_string('day', '');
}
if (extensionSettings.get_string('night')) {
systemSettings.set_string('picture-uri-dark', extensionSettings.get_string('night'));
extensionSettings.set_string('night', '');
}
}
-136
View File
@@ -1,136 +0,0 @@
// 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 utils = Me.imports.utils;
const { Time } = Me.imports.enums.Time;
/**
* The Backgrounder is responsible for changing the desktop background
* according to the time.
*
* When the user changes its desktop background (for example via the system
* settings), it will use it as the current time background.
*/
var Backgrounder = class {
constructor() {
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() {
console.debug('Enabling Backgrounder...');
this._watchStatus();
if (this._backgroundsSettings.get_boolean('enabled')) {
this._connectSettings();
this._connectTimer();
this._updateSystemBackground(e.timer.time);
}
console.debug('Backgrounder enabled.');
}
disable() {
console.debug('Disabling Backgrounder...');
this._disconnectTimer();
this._disconnectSettings();
this._unwatchStatus();
console.debug('Backgrounder disabled.');
}
_watchStatus() {
console.debug('Watching backgrounds status...');
this._statusConnection = this._backgroundsSettings.connect('changed::enabled', this._onStatusChanged.bind(this));
}
_unwatchStatus() {
if (this._statusConnection) {
this._backgroundsSettings.disconnect(this._statusConnection);
this._statusConnection = null;
}
console.debug('Stopped watching backgrounds status.');
}
_connectSettings() {
console.debug('Connecting Backgrounder to settings...');
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() {
this._settingsConnections.forEach(connection => connection.settings.disconnect(connection.id));
this._settingsConnections = [];
console.debug('Disconnected Backgrounder from settings.');
}
_connectTimer() {
console.debug('Connecting Backgrounder to Timer...');
this._timerConnection = e.timer.connect('time-changed', this._onTimeChanged.bind(this));
}
_disconnectTimer() {
if (this._timerConnection) {
e.timer.disconnect(this._timerConnection);
this._timerConnection = null;
}
console.debug('Disconnected Backgrounder from Timer.');
}
_onStatusChanged() {
console.debug(`Backgrounds switching has been ${this._backgroundsSettings.get_boolean('enabled') ? 'enabled' : 'disabled'}.`);
this.disable();
this.enable();
}
_onDayBackgroundChanged() {
console.debug(`Day background changed to '${this._backgroundsSettings.get_string('day')}'.`);
this._updateSystemBackground();
}
_onNightBackgroundChanged() {
console.debug(`Night background changed to '${this._backgroundsSettings.get_string('night')}'.`);
this._updateSystemBackground();
}
_onSystemBackgroundChanged() {
console.debug(`System background changed to '${this._systemBackgroundSettings.get_string('picture-uri')}'.`);
this._updateCurrentBackground();
}
_onTimeChanged() {
this._updateSystemBackground();
}
_updateCurrentBackground() {
if (e.timer.time === Time.UNKNOWN)
return;
this._backgroundsSettings.set_string(e.timer.time, this._systemBackgroundSettings.get_string('picture-uri'));
}
_updateSystemBackground() {
if (e.timer.time === Time.UNKNOWN || !this._backgroundsSettings.get_string(e.timer.time))
return;
this._systemBackgroundSettings.set_string('picture-uri', this._backgroundsSettings.get_string(e.timer.time));
}
};
+4 -14
View File
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Romain Vigier <contact AT romainvigier.fr>
// SPDX-FileCopyrightText: 2021, 2022 Romain Vigier <contact AT romainvigier.fr>
// SPDX-License-Identifier: GPL-3.0-or-later
const { Gio, GLib, GObject, Gtk } = imports.gi;
@@ -6,14 +6,11 @@ const { extensionUtils } = imports.misc;
const Me = extensionUtils.getCurrentExtension();
const utils = Me.imports.utils;
var BackgroundsPreferences = GObject.registerClass({
GTypeName: 'BackgroundsPreferences',
Template: `file://${GLib.build_filenamev([Me.path, 'preferences', 'ui', 'BackgroundsPreferences.ui'])}`,
InternalChildren: [
'enabled_switch',
'day_button',
'night_button',
],
@@ -29,24 +26,17 @@ var BackgroundsPreferences = GObject.registerClass({
}, class BackgroundsPreferences extends Gtk.Box {
_init(props = {}) {
super._init(props);
this.settings = extensionUtils.getSettings(utils.getSettingsSchema('backgrounds'));
this.settings = new Gio.Settings({ schema: 'org.gnome.desktop.background' });
this.settings.bind(
'enabled',
this._enabled_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
this.settings.bind(
'day',
'picture-uri',
this._day_button,
'path',
Gio.SettingsBindFlags.DEFAULT
);
this.settings.bind(
'night',
'picture-uri-dark',
this._night_button,
'path',
Gio.SettingsBindFlags.DEFAULT
+1 -18
View File
@@ -1,36 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-FileCopyrightText: 2021 Romain Vigier <contact AT romainvigier.fr>
SPDX-FileCopyrightText: 2021, 2022 Romain Vigier <contact AT romainvigier.fr>
SPDX-License-Identifier: GPL-3.0-or-later
-->
<interface domain="nightthemeswitcher@romainvigier.fr">
<template class="BackgroundsPreferences" parent="GtkBox">
<property name="orientation">vertical</property>
<property name="spacing">8</property>
<child>
<object class="GtkBox">
<property name="orientation">horizontal</property>
<child>
<object class="GtkLabel">
<property name="label" translatable="yes">Switch backgrounds</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="xalign">0</property>
<style>
<class name="heading"/>
</style>
</object>
</child>
<child>
<object class="GtkSwitch" id="enabled_switch">
<property name="valign">center</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkRevealer">
<property name="reveal-child" bind-source="enabled_switch" bind-property="active" bind-flags="sync-create"/>
<child>
<object class="PreferencesList">
<property name="margin-bottom">36</property>
@@ -52,7 +37,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
</child>
</object>
</child>
</object>
</child>
</template>
</interface>