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

Introduce auto-detect-theme feature #2343

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ all-features = true
maintenance = { status = "actively-developed" }

[features]
default = ["wgpu", "fira-sans"]
default = ["wgpu", "fira-sans", "auto-detect-theme"]
# Enable the `wgpu` GPU-accelerated renderer backend
wgpu = ["iced_renderer/wgpu", "iced_widget/wgpu"]
# Enables the `Image` widget
Expand Down Expand Up @@ -53,8 +53,11 @@ multi-window = ["iced_winit/multi-window"]
advanced = []
# Enables embedding Fira Sans as the default font on Wasm builds
fira-sans = ["iced_renderer/fira-sans"]
# Enables auto-detecting light/dark mode for the built-in theme
auto-detect-theme = ["iced_core/auto-detect-theme"]

[dependencies]
iced_core.workspace = true
iced_futures.workspace = true
iced_renderer.workspace = true
iced_widget.workspace = true
Expand Down Expand Up @@ -121,6 +124,7 @@ async-std = "1.0"
bitflags = "2.0"
bytemuck = { version = "1.0", features = ["derive"] }
cosmic-text = "0.10"
dark-light = "1.0"
futures = "0.3"
glam = "0.25"
glyphon = "0.5"
Expand Down
6 changes: 6 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
25 changes: 23 additions & 2 deletions core/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Loading