Files
portmaster/desktop/tauri/rust-dark-light/src/lib.rs
2025-02-25 11:34:21 +02:00

74 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Detect if dark mode or light mode is enabled.
//!
//! # Examples
//!
//! ```
//! let mode = dark_light::detect();
//!
//! match mode {
//! // Dark mode
//! dark_light::Mode::Dark => {},
//! // Light mode
//! dark_light::Mode::Light => {},
//! // Unspecified
//! dark_light::Mode::Default => {},
//! }
//! ```
mod platforms;
use platforms::platform;
mod utils;
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
use utils::rgb::Rgb;
/// Enum representing dark mode, light mode, or unspecified.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Mode {
/// Dark mode
Dark,
/// Light mode
Light,
/// Unspecified
Default,
}
impl Mode {
#[allow(dead_code)]
fn from_bool(b: bool) -> Self {
if b {
Mode::Dark
} else {
Mode::Light
}
}
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
))]
/// Convert an RGB color to [`Mode`]. The color is converted to grayscale, and if the grayscale value is less than 192, [`Mode::Dark`] is returned. Otherwise, [`Mode::Light`] is returned.
fn from_rgb(rgb: Rgb) -> Self {
let window_background_gray = (rgb.0 * 11 + rgb.1 * 16 + rgb.2 * 5) / 32;
if window_background_gray < 192 {
Self::Dark
} else {
Self::Light
}
}
}
/// Detect if light mode or dark mode is enabled. If the mode cant be detected, fall back to [`Mode::Default`].
pub use platform::detect::detect;
/// Notifies the user if the system theme has been changed.
pub use platform::notify::subscribe;