system_theme/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
3//!
4#![warn(missing_docs, rust_2018_idioms, future_incompatible, keyword_idents)]
5
6pub mod error;
7mod integration;
8mod platform;
9mod theme;
10
11use error::Error;
12
13#[doc(inline)]
14pub use theme::{ThemeColor, ThemeContrast, ThemeKind, ThemePalette, ThemeScheme};
15
16/// System theme implementation.
17pub struct SystemTheme {
18    platform: platform::Platform,
19}
20
21impl AsRef<SystemTheme> for SystemTheme {
22    fn as_ref(&self) -> &SystemTheme {
23        self
24    }
25}
26
27impl From<SystemTheme> for ThemePalette {
28    fn from(theme: SystemTheme) -> Self {
29        (&theme).into()
30    }
31}
32
33impl From<&SystemTheme> for ThemePalette {
34    fn from(theme: &SystemTheme) -> Self {
35        let kind = theme.get_kind().unwrap_or_default();
36
37        let scheme = theme.get_scheme().unwrap_or_default();
38        let contrast = theme.get_contrast().unwrap_or_default();
39
40        let mut palette = ThemePalette::system_palette(kind, scheme, contrast);
41
42        if let Ok(accent) = theme.get_accent() {
43            palette.accent = accent;
44        };
45
46        palette
47    }
48}
49
50impl SystemTheme {
51    /// Create a new instance of SystemTheme.
52    pub fn new() -> Result<Self, Error> {
53        Ok(Self {
54            platform: platform::Platform::new()?,
55        })
56    }
57
58    /// Get the system theme kind.
59    pub fn get_kind(&self) -> Result<ThemeKind, Error> {
60        self.platform.theme_kind()
61    }
62
63    /// Get the system theme scheme.
64    pub fn get_scheme(&self) -> Result<ThemeScheme, Error> {
65        self.platform.theme_scheme()
66    }
67
68    /// Get the system theme contrast level.
69    pub fn get_contrast(&self) -> Result<ThemeContrast, Error> {
70        self.platform.theme_contrast()
71    }
72
73    /// Get the system theme accent color.
74    pub fn get_accent(&self) -> Result<ThemeColor, Error> {
75        self.platform.theme_accent()
76    }
77}