Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 8c1ce22

Browse files
committed
use Granite.ModeSwitch for theme changer
1 parent 8d980ff commit 8c1ce22

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

src/MainWindow.vala

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ public class MainWindow : Gtk.Window {
112112
if (current_icons.lookup_icon (icon_name, 16, Gtk.IconLookupFlags.FORCE_SIZE) == null) {
113113
icon_name = "weather-few-clouds-symbolic";
114114
}
115+
var gtk_settings = Gtk.Settings.get_default ();
116+
var theme_switch = new Granite.ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic");
117+
theme_switch.active = Docs.settings.get_boolean ("dark");
118+
theme_switch.bind_property ("active", gtk_settings, "gtk_application_prefer_dark_theme");
115119

116-
var theme_button = new Gtk.Button.from_icon_name (icon_name);
117-
theme_button.set_tooltip_text (_("Change theme"));
118-
theme_button.clicked.connect (() => {
119-
toggle_theme (dev, online);
120+
theme_switch.notify["active"].connect (()=> {
121+
toggle_theme (dev, online, gtk_settings);
120122
});
121-
122123
var offline_popover = new PackageList ();
123124

124125
var offline_button = new Gtk.MenuButton ();
@@ -130,7 +131,7 @@ public class MainWindow : Gtk.Window {
130131

131132
header.add (back);
132133
header.add (forward);
133-
header.pack_end (theme_button);
134+
header.pack_end (theme_switch);
134135
header.pack_end (offline_button);
135136

136137
add (stack);
@@ -146,12 +147,12 @@ public class MainWindow : Gtk.Window {
146147
}
147148

148149
stack.notify["visible-child"].connect (() => {
149-
stack_change (provider, theme_button, offline_button);
150+
stack_change (provider, theme_switch, offline_button);
150151
});
151152

152153
show_all ();
153154

154-
theme_button.set_visible (false);
155+
theme_switch.set_visible (false);
155156
set_tab ();
156157

157158
this.delete_event.connect (() => {
@@ -196,18 +197,18 @@ public class MainWindow : Gtk.Window {
196197
}
197198
}
198199

199-
private void stack_change (Gtk.CssProvider provider, Gtk.Button theme_button, Gtk.Button offline_button) {
200+
private void stack_change (Gtk.CssProvider provider, Granite.ModeSwitch theme_switch, Gtk.Button offline_button) {
200201
if (stack.get_visible_child_name () == "vala") {
201202
Gtk.Settings.get_default ().set ("gtk-application-prefer-dark-theme", true);
202203
Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
203204

204-
theme_button.set_visible (false);
205+
theme_switch.set_visible (false);
205206
offline_button.set_visible (true);
206207
} else {
207208
Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), provider);
208209
init_theme ();
209210

210-
theme_button.set_visible (true);
211+
theme_switch.set_visible (true);
211212
offline_button.set_visible (false);
212213
}
213214
}
@@ -228,17 +229,13 @@ public class MainWindow : Gtk.Window {
228229
stack.set_visible_child_name (tab);
229230
}
230231

231-
private void toggle_theme (View view, bool online) {
232-
var window_settings = Gtk.Settings.get_default ();
233-
var dark = Docs.settings.get_int ("dark");
234-
if (dark == 1) {
235-
window_settings.set ("gtk-application-prefer-dark-theme", false);
232+
private void toggle_theme (View view, bool online, Gtk.Settings gtk_settings) {
233+
if (!gtk_settings.gtk_application_prefer_dark_theme) {
236234
view.run_javascript.begin ("document.cookie = 'dark=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';", null);
237235
Docs.settings.set_int ("dark", 0);
238236
view.get_settings ().enable_offline_web_application_cache = true;
239237
view.reload_bypass_cache ();
240238
} else {
241-
window_settings.set ("gtk-application-prefer-dark-theme", true);
242239
view.run_javascript.begin ("document.cookie = 'dark=1; expires=01 Jan 2100 00:00:00 UTC';", null);
243240
Docs.settings.set_int ("dark", 1);
244241
if (online) {

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ executable(
1010
dependencies: [
1111
dependency('gee-0.8'),
1212
dependency('gtk+-3.0'),
13+
dependency('granite'),
1314
dependency('libarchive'),
1415
dependency('libdevhelp-3.0'),
1516
dependency('webkit2gtk-4.0'),

0 commit comments

Comments
 (0)