From e8d967163f2f244b5c2465e910f4bf947a09eef8 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Thu, 9 Jan 2025 13:15:44 +0100 Subject: [PATCH] remove: subscribe --- README.md | 24 ++------------ examples/detect.rs | 4 --- examples/subscribe.rs | 10 ------ src/lib.rs | 33 ++----------------- .../{freedesktop/mod.rs => freedesktop.rs} | 19 ++++++----- src/platforms/freedesktop/detect.rs | 5 --- src/platforms/freedesktop/subscribe.rs | 12 ------- src/platforms/{macos/detect.rs => macos.rs} | 0 src/platforms/macos/mod.rs | 2 -- src/platforms/macos/subscribe.rs | 6 ---- src/platforms/{websys/detect.rs => websys.rs} | 0 src/platforms/websys/mod.rs | 2 -- src/platforms/websys/subscribe.rs | 6 ---- .../{windows/detect.rs => windows.rs} | 0 src/platforms/windows/mod.rs | 2 -- src/platforms/windows/subscribe.rs | 6 ---- 16 files changed, 14 insertions(+), 117 deletions(-) delete mode 100644 examples/subscribe.rs rename src/platforms/{freedesktop/mod.rs => freedesktop.rs} (87%) delete mode 100644 src/platforms/freedesktop/detect.rs delete mode 100644 src/platforms/freedesktop/subscribe.rs rename src/platforms/{macos/detect.rs => macos.rs} (100%) delete mode 100644 src/platforms/macos/mod.rs delete mode 100644 src/platforms/macos/subscribe.rs rename src/platforms/{websys/detect.rs => websys.rs} (100%) delete mode 100644 src/platforms/websys/mod.rs delete mode 100644 src/platforms/websys/subscribe.rs rename src/platforms/{windows/detect.rs => windows.rs} (100%) delete mode 100644 src/platforms/windows/mod.rs delete mode 100644 src/platforms/windows/subscribe.rs diff --git a/README.md b/README.md index 37e247f..509fd19 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@
-Supports macOS, Windows, Linux, BSDs, and WebAssembly. +Supports macOS, Windows, Linux, BSDs, and WebAssembly. On Linux the XDG Desktop Portal D-Bus API is checked for the `color-scheme` preference, which works in Flatpak sandboxes without needing filesystem access. @@ -28,29 +28,9 @@ fn main() -> Result<(), dark_light::Error> { } ``` -### Subscribe to system theme changes -You can subscribe to system theme changes by using the `subscribe` function. This function returns a stream of `Mode` values. The stream will emit a new value whenever the system theme changes. - -> [!WARNING] -> The `subscribe` function is not yet supported on macOS, Windows, and WebAssembly. -> Using it will result in an empty stream. - -```rust -use futures_lite::StreamExt; - -#[tokio::main] -async fn main() -> Result<(), dark_light::Error> { - let mut stream = dark_light::subscribe().await?; - while let Some(mode) = stream.next().await { - println!("System mode changed: {:?}", mode); - } - Ok(()) -} -``` - ## License Licensed under either of the following licenses: * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) \ No newline at end of file + * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) diff --git a/examples/detect.rs b/examples/detect.rs index 9f303c3..51f75d9 100644 --- a/examples/detect.rs +++ b/examples/detect.rs @@ -1,8 +1,4 @@ fn main() -> Result<(), dark_light::Error> { - match dark_light::detect() { - Ok(_) => todo!(), - Err(err) => println!("Error: {}", err), - } match dark_light::detect()? { dark_light::Mode::Dark => println!("Dark mode"), dark_light::Mode::Light => println!("Light mode"), diff --git a/examples/subscribe.rs b/examples/subscribe.rs deleted file mode 100644 index e4fc09f..0000000 --- a/examples/subscribe.rs +++ /dev/null @@ -1,10 +0,0 @@ -use futures_lite::StreamExt; - -#[tokio::main] -async fn main() -> Result<(), dark_light::Error> { - let mut stream = dark_light::subscribe().await?; - while let Some(mode) = stream.next().await { - println!("System mode changed: {:?}", mode); - } - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index 4a8d554..a67a72e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,8 @@ -//! 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 and subscribe to changes in the system theme mode. +//! 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. //! //! It supports macOS, Windows, Linux, BSDs, and WASM. //! //! 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. -//!
-//! The subscribe function is not yet supported on macOS, Windows, and WebAssembly. -//! Using it will result in an empty stream. -//!
mod error; mod mode; @@ -32,29 +28,4 @@ pub use mode::Mode; /// Ok(()) /// } /// ``` -pub use platforms::platform::detect::detect; - -/// Notifies the user if the system theme has been changed. -/// -/// This function returns a stream of `Mode` values. The stream will emit a new value whenever the system theme changes. -/// -/// # Example -/// -/// ``` no_run -/// use dark_light::{ Error, Mode }; -/// use futures_lite::stream::StreamExt; -/// -/// #[tokio::main] -/// async fn main() -> Result<(), Error> { -/// let mut stream = dark_light::subscribe().await?; -/// while let Some(mode) = stream.next().await { -/// match mode { -/// Mode::Dark => {}, -/// Mode::Light => {}, -/// Mode::Unspecified => {}, -/// } -/// } -/// Ok(()) -/// } -/// ``` -pub use platforms::platform::subscribe::subscribe; +pub use platforms::platform::detect; diff --git a/src/platforms/freedesktop/mod.rs b/src/platforms/freedesktop.rs similarity index 87% rename from src/platforms/freedesktop/mod.rs rename to src/platforms/freedesktop.rs index d1ac406..f8707ef 100644 --- a/src/platforms/freedesktop/mod.rs +++ b/src/platforms/freedesktop.rs @@ -1,10 +1,17 @@ -pub mod detect; -pub mod subscribe; - use crate::{Error, Mode}; use ashpd::desktop::settings::ColorScheme as PortalColorScheme; use ashpd::desktop::settings::Settings as XdgPortalSettings; +pub fn detect() -> Result { + futures_lite::future::block_on(get_color_scheme()) +} + +pub(crate) async fn get_color_scheme() -> Result { + let settings = XdgPortalSettings::new().await?; + let color_scheme = settings.color_scheme().await?; + Ok(color_scheme.into()) +} + impl From for Mode { fn from(value: PortalColorScheme) -> Self { match value { @@ -14,9 +21,3 @@ impl From for Mode { } } } - -pub(crate) async fn get_color_scheme() -> Result { - let settings = XdgPortalSettings::new().await?; - let color_scheme = settings.color_scheme().await?; - Ok(color_scheme.into()) -} diff --git a/src/platforms/freedesktop/detect.rs b/src/platforms/freedesktop/detect.rs deleted file mode 100644 index ffc80b4..0000000 --- a/src/platforms/freedesktop/detect.rs +++ /dev/null @@ -1,5 +0,0 @@ -use crate::{Error, Mode}; - -pub fn detect() -> Result { - futures_lite::future::block_on(super::get_color_scheme()) -} diff --git a/src/platforms/freedesktop/subscribe.rs b/src/platforms/freedesktop/subscribe.rs deleted file mode 100644 index c40d2b7..0000000 --- a/src/platforms/freedesktop/subscribe.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::{Error, Mode}; -use ashpd::desktop::settings::Settings; -use futures_lite::{Stream, StreamExt}; - -pub async fn subscribe() -> Result, Error> { - let stream = Settings::new() - .await? - .receive_color_scheme_changed() - .await? - .map(Mode::from); - Ok(stream) -} diff --git a/src/platforms/macos/detect.rs b/src/platforms/macos.rs similarity index 100% rename from src/platforms/macos/detect.rs rename to src/platforms/macos.rs diff --git a/src/platforms/macos/mod.rs b/src/platforms/macos/mod.rs deleted file mode 100644 index 2267a8e..0000000 --- a/src/platforms/macos/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod detect; -pub mod subscribe; diff --git a/src/platforms/macos/subscribe.rs b/src/platforms/macos/subscribe.rs deleted file mode 100644 index c31aeaa..0000000 --- a/src/platforms/macos/subscribe.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::{Error, Mode}; - -pub async fn subscribe() -> Result, Error> { - let stream = futures_lite::stream::empty(); - Ok(Box::pin(stream)) -} diff --git a/src/platforms/websys/detect.rs b/src/platforms/websys.rs similarity index 100% rename from src/platforms/websys/detect.rs rename to src/platforms/websys.rs diff --git a/src/platforms/websys/mod.rs b/src/platforms/websys/mod.rs deleted file mode 100644 index 2267a8e..0000000 --- a/src/platforms/websys/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod detect; -pub mod subscribe; diff --git a/src/platforms/websys/subscribe.rs b/src/platforms/websys/subscribe.rs deleted file mode 100644 index c31aeaa..0000000 --- a/src/platforms/websys/subscribe.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::{Error, Mode}; - -pub async fn subscribe() -> Result, Error> { - let stream = futures_lite::stream::empty(); - Ok(Box::pin(stream)) -} diff --git a/src/platforms/windows/detect.rs b/src/platforms/windows.rs similarity index 100% rename from src/platforms/windows/detect.rs rename to src/platforms/windows.rs diff --git a/src/platforms/windows/mod.rs b/src/platforms/windows/mod.rs deleted file mode 100644 index 2267a8e..0000000 --- a/src/platforms/windows/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod detect; -pub mod subscribe; diff --git a/src/platforms/windows/subscribe.rs b/src/platforms/windows/subscribe.rs deleted file mode 100644 index 9911c34..0000000 --- a/src/platforms/windows/subscribe.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::{Error, Mode}; - -pub async fn subscribe() -> Result, Error> { - let stream = futures_lite::stream::empty(); - Ok(Box::pin(stream)) -}