Introduce auto-detect-theme feature

This commit is contained in:
Héctor Ramón Jiménez 2024-03-19 23:58:17 +01:00
parent af6bc4643d
commit 9db6ac8f20
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 34 additions and 3 deletions

View file

@ -10,6 +10,9 @@ homepage.workspace = true
categories.workspace = true
keywords.workspace = true
[features]
auto-detect-theme = ["dep:dark-light"]
[dependencies]
bitflags.workspace = true
glam.workspace = true
@ -22,6 +25,9 @@ thiserror.workspace = true
web-time.workspace = true
xxhash-rust.workspace = true
dark-light.workspace = true
dark-light.optional = true
[target.'cfg(windows)'.dependencies]
raw-window-handle.workspace = true

View file

@ -7,10 +7,9 @@ use std::fmt;
use std::sync::Arc;
/// A built-in theme.
#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq)]
pub enum Theme {
/// The built-in light variant.
#[default]
Light,
/// The built-in dark variant.
Dark,
@ -161,6 +160,28 @@ impl Theme {
}
}
impl Default for Theme {
fn default() -> Self {
#[cfg(feature = "auto-detect-theme")]
{
use once_cell::sync::Lazy;
static DEFAULT: Lazy<Theme> =
Lazy::new(|| match dark_light::detect() {
dark_light::Mode::Dark => Theme::Dark,
dark_light::Mode::Light | dark_light::Mode::Default => {
Theme::Light
}
});
DEFAULT.clone()
}
#[cfg(not(feature = "auto-detect-theme"))]
Theme::Light
}
}
impl fmt::Display for Theme {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {