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
//! System configuration.
use std::process::Command;

/// Query system to see if dark theming should be preferred.
pub(crate) fn prefer_dark() -> bool {
    // outputs something like: `variant       variant          uint32 1`
    let stdout = Command::new("dbus-send")
        .arg("--reply-timeout=100")
        .arg("--print-reply=literal")
        .arg("--dest=org.freedesktop.portal.Desktop")
        .arg("/org/freedesktop/portal/desktop")
        .arg("org.freedesktop.portal.Settings.Read")
        .arg("string:org.freedesktop.appearance")
        .arg("string:color-scheme")
        .output()
        .ok()
        .and_then(|out| String::from_utf8(out.stdout).ok());

    if matches!(stdout, Some(ref s) if s.is_empty()) {
        log::error!("XDG Settings Portal did not return response in time: timeout: 100ms, key: color-scheme");
    }

    matches!(stdout, Some(s) if s.trim().ends_with("uint32 1"))
}

/// Query system configuration for buttons layout.
/// Should be updated to use standard xdg-desktop-portal specs once available
/// https://github.com/flatpak/xdg-desktop-portal/pull/996
pub(crate) fn get_button_layout_config() -> Option<(String, String)> {
    let config_string = Command::new("dbus-send")
        .arg("--reply-timeout=100")
        .arg("--print-reply=literal")
        .arg("--dest=org.freedesktop.portal.Desktop")
        .arg("/org/freedesktop/portal/desktop")
        .arg("org.freedesktop.portal.Settings.Read")
        .arg("string:org.gnome.desktop.wm.preferences")
        .arg("string:button-layout")
        .output()
        .ok()
        .and_then(|out| String::from_utf8(out.stdout).ok())?;

    let sides_split: Vec<_> = config_string
        // Taking last word
        .rsplit(' ')
        .next()?
        // Split by left/right side
        .split(':')
        // Only two sides
        .take(2)
        .collect();

    match sides_split.as_slice() {
        [left, right] => Some((left.to_string(), right.to_string())),
        _ => None,
    }
}