status/darkMode: Add dark mode toggle

The aggregate menu with its submenus isn't well-suited for simple
on-off actions, so we didn't expose the global color-scheme support
that was introduced last cycle.

Quick settings on the other hand are a natural fit for actions like
this, so add a corresponding toggle.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2392>
This commit is contained in:
Florian Müllner
2022-07-27 03:38:28 +02:00
committed by Marge Bot
parent 4d931c2c41
commit a3a886f185
6 changed files with 57 additions and 0 deletions

View File

@ -135,6 +135,7 @@
<file>ui/status/accessibility.js</file>
<file>ui/status/autoRotate.js</file>
<file>ui/status/brightness.js</file>
<file>ui/status/darkMode.js</file>
<file>ui/status/dwellClick.js</file>
<file>ui/status/location.js</file>
<file>ui/status/keyboard.js</file>

View File

@ -424,6 +424,7 @@ class QuickSettings extends PanelMenu.Button {
this._location = new imports.ui.status.location.Indicator();
this._thunderbolt = new imports.ui.status.thunderbolt.Indicator();
this._nightLight = new imports.ui.status.nightLight.Indicator();
this._darkMode = new imports.ui.status.darkMode.Indicator();
this._rfkill = new imports.ui.status.rfkill.Indicator();
this._autoRotate = new imports.ui.status.autoRotate.Indicator();
this._unsafeMode = new UnsafeModeIndicator();
@ -433,6 +434,7 @@ class QuickSettings extends PanelMenu.Button {
this._indicators.add_child(this._thunderbolt);
this._indicators.add_child(this._location);
this._indicators.add_child(this._nightLight);
this._indicators.add_child(this._darkMode);
if (this._bluetooth)
this._indicators.add_child(this._bluetooth);
this._indicators.add_child(this._rfkill);
@ -446,6 +448,7 @@ class QuickSettings extends PanelMenu.Button {
if (this._bluetooth)
this._addItems(this._bluetooth.quickSettingsItems);
this._addItems(this._nightLight.quickSettingsItems);
this._addItems(this._darkMode.quickSettingsItems);
this._addItems(this._rfkill.quickSettingsItems);
this._addItems(this._autoRotate.quickSettingsItems);
this._addItems(this._unsafeMode.quickSettingsItems);

49
js/ui/status/darkMode.js Normal file
View File

@ -0,0 +1,49 @@
/* exported Indicator */
const {Gio, GObject} = imports.gi;
const Main = imports.ui.main;
const {QuickToggle, SystemIndicator} = imports.ui.quickSettings;
const DarkModeToggle = GObject.registerClass(
class DarkModeToggle extends QuickToggle {
_init() {
super._init({
label: _('Dark Mode'),
iconName: 'dark-mode-symbolic',
});
this._settings = new Gio.Settings({
schema_id: 'org.gnome.desktop.interface',
});
this._changedId = this._settings.connect('changed::color-scheme',
() => this._sync());
this.connectObject(
'destroy', () => this._settings.run_dispose(),
'clicked', () => this._toggleMode(),
this);
this._sync();
}
_toggleMode() {
Main.layoutManager.screenTransition.run();
this._settings.set_string('color-scheme',
this.checked ? 'default' : 'prefer-dark');
}
_sync() {
const colorScheme = this._settings.get_string('color-scheme');
const checked = colorScheme === 'prefer-dark';
if (this.checked !== checked)
this.set({checked});
}
});
var Indicator = GObject.registerClass(
class Indicator extends SystemIndicator {
_init() {
super._init();
this.quickSettingsItems.push(new DarkModeToggle());
}
});