Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux support #4

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9730b48
Linux support
edfloreshz Sep 1, 2021
50c3c17
Linux support
edfloreshz Sep 1, 2021
22759e9
Update src/linux.rs
edfloreshz Sep 21, 2021
7107383
Update src/linux.rs
edfloreshz Sep 21, 2021
ac3ed4c
Mark dependencies as Linux-only
Funami580 Nov 3, 2021
3db16e7
Fix check for config files
Funami580 Nov 3, 2021
4772c8e
Initial support for freedesktop's color-scheme
edfloreshz Jan 5, 2022
edff243
Replaced dbus crate with zbus
edfloreshz Jan 5, 2022
51661a7
Removed unnecesary case in match statement
edfloreshz Jan 5, 2022
0787309
Fixed files for macOS and Windows
edfloreshz Jan 5, 2022
387a35b
Renamed get_appearance => get_freedesktop_color_scheme
edfloreshz Jan 5, 2022
2ccb32b
Fix: now is only called once
edfloreshz Jan 5, 2022
f461ac7
cargo fmt
edfloreshz Jan 5, 2022
05f98fd
Added from to Mode enum
edfloreshz Jan 5, 2022
4b1ceae
Returning None when 0 is returned.
edfloreshz Jan 5, 2022
c5a49be
Update src/linux.rs
edfloreshz Jan 6, 2022
c87cf69
Removed breaking change
edfloreshz Jan 6, 2022
d6f58ca
Better error message.
edfloreshz Jan 6, 2022
905da65
Added specific cfg for freedesktop-based systems.
edfloreshz Jan 6, 2022
4d42d51
Renamed linux.rs to freedesktop.rs
edfloreshz Jan 6, 2022
e7c107a
Merge branch 'linux-support' of https://github.com/edfloreshz/rust-da…
edfloreshz Jan 6, 2022
1c4fbfc
Removed unnecessary `Result` from `get_freedesktop_color_scheme` and …
edfloreshz Jan 6, 2022
21ac29b
Added specific cfg for freedesktop-based systems.
edfloreshz Jan 6, 2022
7bff400
Fixed review issues
edfloreshz Jan 6, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ description = "Detect if dark mode or light mode is enabled"
readme = "README.md"

[dependencies]
anyhow = "1.0.52"

[target.'cfg(unix)'.dependencies]
detect-desktop-environment = "0.2.0"
dconf_rs = "0.3.0"
dirs = "4.0.0"
dbus = "0.9.5"

[target.'cfg(windows)'.dependencies]
winreg = "0.8.0"
winreg = "0.8.0"
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ mod windows;
#[cfg(target_os = "windows")]
use windows as platform;

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux as platform;

use anyhow::Result;

#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
mod platform {
pub fn detect() -> crate::Mode {
Mode::Light
Expand All @@ -37,6 +44,6 @@ pub enum Mode {
}

/// Detect if light mode or dark mode is enabled. If the mode can’t be detected, fall back to [`Mode::Light`].
pub fn detect() -> Mode {
pub fn detect() -> Result<Mode> {
platform::detect()
}
85 changes: 85 additions & 0 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::path::Path;
use std::time::Duration;
use dbus::arg;
use detect_desktop_environment::DesktopEnvironment;
use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
use dbus::blocking::Connection;

use anyhow::Result;

use crate::Mode;

fn is_dark_mode_enabled() -> Result<crate::Mode> {
let mode = if get_appearance().is_some() {
get_freedesktop_color_scheme()
} else {
match DesktopEnvironment::detect() {
DesktopEnvironment::Unknown => Mode::Light,
DesktopEnvironment::Cinnamon => check_dconf("/org/cinnamon/desktop/interface/gtk-theme"),
DesktopEnvironment::Gnome => check_dconf("/org/gnome/desktop/interface/gtk-theme"),
DesktopEnvironment::Kde => check_config_file("Name=", "kdeglobals"),
DesktopEnvironment::Mate => check_dconf("/org/mate/desktop/interface/gtk-theme"),
DesktopEnvironment::Unity => check_dconf("/org/gnome/desktop/interface/gtk-theme"),
DesktopEnvironment::Xfce => check_config_file("name=\"ThemeName\"", "xfce4/xfconf/xfce-perchannel-xml/xsettings.xml"),
_ => Mode::Light
}
};
Ok(mode)
}

fn get_freedesktop_color_scheme() -> Mode {
let appearance: Option<arg::PropMap> = get_appearance();
if appearance.is_none() {
Mode::Light
} else {
let appearance = appearance.unwrap();
let theme: Option<&i32> = arg::prop_cast(&appearance, "color-scheme");
match theme.unwrap() {
1 => Mode::Dark,
_ => Mode::Light,
}
}
}

fn get_appearance() -> Option<arg::PropMap> {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", Duration::from_millis(5000));
let data = proxy.get("org.freedesktop.portal.Settings", "org.freedesktop.appearance").ok();
data
}

fn check_file(pattern: &str, path: &Path) -> Mode {
if let Ok(content) = std::fs::read_to_string(path) {
let theme = content.lines().filter(|line| line.contains(pattern)).collect::<String>();
if theme.to_lowercase().contains("dark") {
Mode::Dark
} else {
Mode::Light
}
} else {
Mode::Light
}
}

fn check_config_file(pattern: &str, path: &str) -> Mode {
if let Some(config_dir) = dirs::config_dir() {
check_file(pattern, &config_dir.join(path))
} else {
Mode::Light
}
}

fn check_dconf(pattern: &str) -> Mode {
match dconf_rs::get_string(pattern) {
Ok(theme) => if theme.contains("dark") {
Mode::Dark
} else {
Mode::Light
},
Err(_) => Mode::Light,
}
}

pub fn detect() -> Result<crate::Mode> {
is_dark_mode_enabled()
}