forked from frewsxcv/rust-dark-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freedesktop.rs
96 lines (90 loc) · 3.06 KB
/
freedesktop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use detect_desktop_environment::DesktopEnvironment;
use ini::Ini;
use std::path::{Path, PathBuf};
use zbus::blocking::Connection;
use zvariant::Value;
use crate::Mode;
const XDG_KDEGLOBALS: &str = "/etc/xdg/kdeglobals";
fn get_freedesktop_color_scheme() -> Option<Mode> {
let conn = Connection::session();
if conn.is_err() {
return None;
}
let reply = conn.unwrap().call_method(
Some("org.freedesktop.portal.Desktop"),
"/org/freedesktop/portal/desktop",
Some("org.freedesktop.portal.Settings"),
"Read",
&("org.freedesktop.appearance", "color-scheme"),
);
if let Ok(reply) = &reply {
let theme = reply.body::<Value>();
if theme.is_err() {
return None;
}
let theme = theme.unwrap().downcast::<u32>();
match theme.unwrap() {
1 => Some(Mode::Dark),
2 => Some(Mode::Light),
_ => None,
}
} else {
None
}
}
fn detect_gtk(pattern: &str) -> Mode {
match dconf_rs::get_string(pattern) {
Ok(theme) => Mode::from(theme.to_lowercase().contains("dark")),
Err(_) => Mode::Light,
}
}
fn detect_kde(path: &str) -> Mode {
match Ini::load_from_file(path) {
Ok(cfg) => {
let section = match cfg.section(Some("Colors:Window")) {
Some(section) => section,
None => return Mode::Light,
};
let values = match section.get("BackgroundNormal") {
Some(string) => string,
None => return Mode::Light,
};
let rgb = values
.split(',')
.map(|s| s.parse::<u32>().unwrap_or(255))
.collect::<Vec<u32>>();
let rgb = if rgb.len() > 2 {
rgb
} else {
vec![255, 255, 255]
};
let (r, g, b) = (rgb[0], rgb[1], rgb[2]);
Mode::rgb(r, g, b)
}
Err(e) => {
eprintln!("{:?}", e);
Mode::Light
}
}
}
pub fn detect() -> Mode {
match get_freedesktop_color_scheme() {
Some(mode) => mode,
// Other desktop environments are still being worked on, fow now, only the following implementations work.
None => match DesktopEnvironment::detect() {
DesktopEnvironment::Kde => {
let path = if Path::new(XDG_KDEGLOBALS).exists() {
PathBuf::from(XDG_KDEGLOBALS)
} else {
dirs::home_dir().unwrap().join(".config/kdeglobals")
};
detect_kde(path.to_str().unwrap())
}
DesktopEnvironment::Cinnamon => detect_gtk("/org/cinnamon/desktop/interface/gtk-theme"),
DesktopEnvironment::Gnome => detect_gtk("/org/gnome/desktop/interface/gtk-theme"),
DesktopEnvironment::Mate => detect_gtk("/org/mate/desktop/interface/gtk-theme"),
DesktopEnvironment::Unity => detect_gtk("/org/gnome/desktop/interface/gtk-theme"),
_ => Mode::Default,
},
}
}