Skip to content

Commit fe30090

Browse files
committed
improv: Use gtk::Settings to set dark mode
This seems a little cleaner than using an env var here, and it doesn't hard code a particular theme name.
1 parent 978044f commit fe30090

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/application/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ fn daemon() -> Rc<dyn Daemon> {
261261

262262
#[cfg(target_os = "macos")]
263263
fn macos_init() {
264+
use gtk::SettingsExt;
264265
use std::{env, process};
266+
let mut prefer_dark = false;
265267
// This command returns Dark if we should use the dark theme
266268
// defaults read -g AppleInterfaceStyle
267269
if let Ok(output) = process::Command::new("defaults")
@@ -270,37 +272,42 @@ fn macos_init() {
270272
.arg("AppleInterfaceStyle")
271273
.output()
272274
{
273-
if output.stdout.starts_with(b"Dark") {
274-
let _ = env::set_var("GTK_THEME", "Adwaita:dark");
275-
}
275+
prefer_dark = output.stdout.starts_with(b"Dark");
276+
}
277+
278+
if let Some(settings) = gtk::Settings::get_default() {
279+
settings.set_property_gtk_application_prefer_dark_theme(prefer_dark);
276280
}
277281
}
278282

279283
#[cfg(target_os = "windows")]
280284
fn windows_init() {
281-
use std::env;
282285
// This is a dword with a value of 0 if we should use the dark theme:
283286
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme
287+
use gtk::SettingsExt;
284288
use winreg::RegKey;
289+
let mut prefer_dark = false;
285290
let hkcu = RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
286291
if let Ok(subkey) = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize") {
287292
if let Ok(dword) = subkey.get_value::<u32, _>("AppsUseLightTheme") {
288-
if dword == 0 {
289-
let _ = env::set_var("GTK_THEME", "Adwaita:dark");
290-
}
293+
prefer_dark = (dword == 0);
291294
}
292295
}
296+
297+
if let Some(settings) = gtk::Settings::get_default() {
298+
settings.set_property_gtk_application_prefer_dark_theme(prefer_dark);
299+
}
293300
}
294301

295302
pub fn run(args: Vec<String>) -> i32 {
303+
gtk::init().unwrap();
304+
296305
#[cfg(target_os = "macos")]
297306
macos_init();
298307

299308
#[cfg(target_os = "windows")]
300309
windows_init();
301310

302-
gtk::init().unwrap();
303-
304311
let application = ConfiguratorApp::new();
305312
application.run(&args)
306313
}

0 commit comments

Comments
 (0)