Skip to main content

dark_light/
lib.rs

1//! This crate is designed to facilitate the development of applications that support both light and dark themes. It provides a simple API to detect the current theme mode.
2//!
3//! It supports macOS, Windows, Linux, BSDs, and WASM.
4//!
5//! On Linux the [XDG Desktop Portal](https://flatpak.github.io/xdg-desktop-portal/) D-Bus API is checked for the `color-scheme` preference, which works in Flatpak sandboxes without needing filesystem access.
6
7mod error;
8mod mode;
9mod platforms;
10#[cfg(any(feature = "tokio", feature = "async-io"))]
11mod stream;
12mod watch;
13
14pub use error::Error;
15pub use mode::Mode;
16pub use watch::Watcher;
17
18/// Detects the system theme mode.
19///
20/// # Example
21///
22/// ``` no_run
23/// use dark_light::{ Error, Mode };
24///
25/// fn main() -> Result<(), Error> {
26///     let mode = dark_light::detect()?;
27///     match mode {
28///         Mode::Dark => {},
29///         Mode::Light => {},
30///         Mode::Unspecified => {},
31///     }
32///     Ok(())
33/// }
34/// ```
35pub use platforms::platform::detect;
36
37/// Subscribes to changes in the system theme mode.
38///
39/// Returns a [`Watcher`] that receives a new [`Mode`] each time the OS theme changes.
40/// Only mode transitions are emitted, and the watcher's background thread (where used)
41/// stops when the `Watcher` is dropped.
42///
43/// # Example
44///
45/// ``` no_run
46/// use dark_light::{ Error, Mode };
47///
48/// fn main() -> Result<(), Error> {
49///     let watcher = dark_light::subscribe()?;
50///     for mode in watcher.iter() {
51///         match mode {
52///             Mode::Dark => {},
53///             Mode::Light => {},
54///             Mode::Unspecified => {},
55///         }
56///     }
57///     Ok(())
58/// }
59/// ```
60pub use platforms::platform::subscribe;
61
62/// Subscribes to changes in the system theme mode using an async stream.
63///
64/// Returns a [`Stream`] that yields a new [`Mode`] each time the OS theme changes.
65///
66/// # Example
67///
68/// ``` no_run
69/// use dark_light::{ Error, Mode };
70/// use futures_util::StreamExt;
71///
72/// fn main() -> Result<(), Error> {
73///     futures_executor::block_on(async {
74///         let mut stream = dark_light::stream()?;
75///         while let Some(mode) = stream.next().await {
76///             match mode {
77///                 Mode::Dark => {},
78///                 Mode::Light => {},
79///                 Mode::Unspecified => {},
80///             }
81///         }
82///         Ok(())
83///     })
84/// }
85/// ```
86#[cfg(any(feature = "tokio", feature = "async-io"))]
87pub use stream::stream;