Skip to content

Commit 0f7496c

Browse files
committed
app: move gtk theme management into separate class and module
1 parent 5363568 commit 0f7496c

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

ddterm/app/application.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const { Gdk, Gtk } = imports.gi;
4242

4343
const { AppWindow } = imports.ddterm.app.appwindow;
4444
const { PrefsDialog } = imports.ddterm.pref.dialog;
45+
const { GtkThemeManager } = imports.ddterm.app.gtktheme;
4546

4647
const APP_DIR = Me.dir.get_child('ddterm').get_child('app');
4748

@@ -154,8 +155,16 @@ const Application = GObject.registerClass(
154155
this.add_action(this.settings.create_action(key));
155156
});
156157

157-
this.settings.connect('changed::theme-variant', this.update_theme.bind(this));
158-
this.update_theme();
158+
this.theme_manager = new GtkThemeManager({
159+
'gtk-settings': Gtk.Settings.get_default(),
160+
});
161+
162+
this.settings.bind(
163+
'theme-variant',
164+
this.theme_manager,
165+
'theme-variant',
166+
Gio.SettingsBindFlags.GET
167+
);
159168

160169
const menus = Gtk.Builder.new_from_file(APP_DIR.get_child('menus.ui').get_path());
161170

@@ -308,28 +317,6 @@ const Application = GObject.registerClass(
308317
return action;
309318
}
310319

311-
update_theme() {
312-
const gtk_settings = Gtk.Settings.get_default();
313-
const theme = this.settings.get_string('theme-variant');
314-
315-
switch (theme) {
316-
case 'system':
317-
gtk_settings.reset_property('gtk-application-prefer-dark-theme');
318-
break;
319-
320-
case 'dark':
321-
gtk_settings.gtk_application_prefer_dark_theme = true;
322-
break;
323-
324-
case 'light':
325-
gtk_settings.gtk_application_prefer_dark_theme = false;
326-
break;
327-
328-
default:
329-
printerr(`Unknown theme-variant: ${theme}`);
330-
}
331-
}
332-
333320
bind_shortcut(action, settings_key) {
334321
const handler = this.update_shortcut.bind(this, action, settings_key);
335322

ddterm/app/gtktheme.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright © 2023 Aleksandr Mezin
3+
4+
This file is part of ddterm GNOME Shell extension.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
'use strict';
21+
22+
const { GObject, Gtk } = imports.gi;
23+
24+
var GtkThemeManager = GObject.registerClass(
25+
{
26+
Properties: {
27+
'gtk-settings': GObject.ParamSpec.object(
28+
'gtk-settings',
29+
'',
30+
'',
31+
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
32+
Gtk.Settings
33+
),
34+
'theme-variant': GObject.ParamSpec.string(
35+
'theme-variant',
36+
'',
37+
'',
38+
GObject.ParamFlags.WRITABLE,
39+
null
40+
),
41+
},
42+
},
43+
class DDTermGtkThemeManager extends GObject.Object {
44+
set theme_variant(value) {
45+
switch (value) {
46+
case 'system':
47+
this.gtk_settings.reset_property('gtk-application-prefer-dark-theme');
48+
break;
49+
50+
case 'dark':
51+
this.gtk_settings.gtk_application_prefer_dark_theme = true;
52+
break;
53+
54+
case 'light':
55+
this.gtk_settings.gtk_application_prefer_dark_theme = false;
56+
break;
57+
58+
default:
59+
printerr(`Unknown theme-variant: ${value}`);
60+
}
61+
}
62+
}
63+
);
64+
65+
/* exported GtkThemeManager */

0 commit comments

Comments
 (0)