gradience_lib/
utils.rs

1pub fn run_command(command: &str) -> std::process::Output {
2    // if os var FLATPAK_ID exists prefix command
3    if let Ok(_) = std::env::var("FLATPAK_ID") {
4        std::process::Command::new("flatpak-spawn")
5            .arg("--host")
6            .arg("sh")
7            .arg("-c")
8            .arg(command)
9            .output()
10            .expect("failed to execute process")
11    } else {
12        std::process::Command::new("sh")
13            .arg("-c")
14            .arg(command)
15            .output()
16            .expect("failed to execute process")
17    }
18
19}
20
21#[derive(Debug)]
22pub enum ShellVersion {
23    G46,
24    Unsupported
25}
26
27pub fn get_gnome_shell_version() -> ShellVersion {
28    let output = run_command("gnome-shell --version");
29    let version = String::from_utf8(output.stdout).unwrap();
30    let version = version.split_whitespace().collect::<Vec<&str>>()[2].to_string();
31    let version = version.split(".").collect::<Vec<&str>>()[0].parse::<u32>().unwrap();
32
33    match version {
34        46 => ShellVersion::G46,
35        _ => ShellVersion::Unsupported,
36    }
37}
38
39pub fn check_installed_extension(extension: &str) -> bool {
40    let output = run_command("gnome-extensions list");
41    let output = String::from_utf8(output.stdout).unwrap();
42    output.contains(extension)
43}
44
45pub fn check_enabled_extension(extension: &str) -> bool {
46    let output = run_command("gnome-extensions list --enabled");
47    let output = String::from_utf8(output.stdout).unwrap();
48    output.contains(extension) 
49}
50
51pub fn set_shell_theme(theme: &str) {
52    if check_enabled_extension("user-theme@gnome-shell-extensions.gcampax.github.com") {
53        run_command(&format!("gsettings set org.gnome.shell.extensions.user-theme name '{}'", theme));
54    } else if check_installed_extension("user-theme@gnome-shell-extensions.gcampax.github.com") {
55        run_command("gnome-extensions enable user-theme@gnome-shell-extensions.gcampax.github.com");
56        run_command(&format!("gsettings set org.gnome.shell.extensions.user-theme name '{}'", theme));
57    }
58}
59
60pub fn reset_shell_theme() {
61    run_command("gsettings reset org.gnome.shell.extensions.user-theme name");
62}