Skip to content

Commit f03e1ac

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 3892764 commit f03e1ac

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/application/mod.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ fn with_daemon<F: Fn(Rc<dyn Daemon>)>(f: F) {
118118

119119
#[cfg(target_os = "macos")]
120120
fn macos_init() {
121+
use gtk::SettingsExt;
121122
use std::{env, process};
123+
let mut prefer_dark = false;
122124
// This command returns Dark if we should use the dark theme
123125
// defaults read -g AppleInterfaceStyle
124126
if let Ok(output) = process::Command::new("defaults")
@@ -127,40 +129,45 @@ fn macos_init() {
127129
.arg("AppleInterfaceStyle")
128130
.output()
129131
{
130-
if output.stdout.starts_with(b"Dark") {
131-
let _ = env::set_var("GTK_THEME", "Adwaita:dark");
132-
}
132+
prefer_dark = output.stdout.starts_with(b"Dark");
133+
}
134+
135+
if let Some(settings) = gtk::Settings::get_default() {
136+
settings.set_property_gtk_application_prefer_dark_theme(prefer_dark);
133137
}
134138
}
135139

136140
#[cfg(target_os = "windows")]
137141
fn windows_init() {
138-
use std::env;
139142
// This is a dword with a value of 0 if we should use the dark theme:
140143
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme
144+
use gtk::SettingsExt;
141145
use winreg::RegKey;
146+
let mut prefer_dark = false;
142147
let hkcu = RegKey::predef(winreg::enums::HKEY_CURRENT_USER);
143148
if let Ok(subkey) = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize") {
144149
if let Ok(dword) = subkey.get_value::<u32, _>("AppsUseLightTheme") {
145-
if dword == 0 {
146-
let _ = env::set_var("GTK_THEME", "Adwaita:dark");
147-
}
150+
prefer_dark = (dword == 0);
148151
}
149152
}
153+
154+
if let Some(settings) = gtk::Settings::get_default() {
155+
settings.set_property_gtk_application_prefer_dark_theme(prefer_dark);
156+
}
150157
}
151158

152159
pub fn run(args: Vec<String>) -> i32 {
153-
#[cfg(target_os = "macos")]
154-
macos_init();
155-
156-
#[cfg(target_os = "windows")]
157-
windows_init();
158-
159160
let application =
160161
gtk::Application::new(Some("com.system76.keyboard-layout"), Default::default())
161162
.expect("Failed to create gtk::Application");
162163

163164
application.connect_activate(move |app| {
165+
#[cfg(target_os = "macos")]
166+
macos_init();
167+
168+
#[cfg(target_os = "windows")]
169+
windows_init();
170+
164171
if let Some(window) = app.get_active_window() {
165172
//TODO
166173
eprintln!("Focusing current window");

0 commit comments

Comments
 (0)