Skip to content

Commit

Permalink
remove: subscribe (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Flores authored Jan 9, 2025
1 parent a70ef09 commit 6289e15
Show file tree
Hide file tree
Showing 16 changed files with 14 additions and 117 deletions.
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<br>
</div>

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.

Expand All @@ -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)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
4 changes: 0 additions & 4 deletions examples/detect.rs
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
10 changes: 0 additions & 10 deletions examples/subscribe.rs

This file was deleted.

33 changes: 2 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
//! <div class="warning">
//! The <code>subscribe</code> function is not yet supported on macOS, Windows, and WebAssembly.
//! Using it will result in an empty stream.
//! </div>
mod error;
mod mode;
Expand All @@ -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;
19 changes: 10 additions & 9 deletions src/platforms/freedesktop/mod.rs → src/platforms/freedesktop.rs
Original file line number Diff line number Diff line change
@@ -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<Mode, Error> {
futures_lite::future::block_on(get_color_scheme())
}

pub(crate) async fn get_color_scheme() -> Result<Mode, Error> {
let settings = XdgPortalSettings::new().await?;
let color_scheme = settings.color_scheme().await?;
Ok(color_scheme.into())
}

impl From<PortalColorScheme> for Mode {
fn from(value: PortalColorScheme) -> Self {
match value {
Expand All @@ -14,9 +21,3 @@ impl From<PortalColorScheme> for Mode {
}
}
}

pub(crate) async fn get_color_scheme() -> Result<Mode, Error> {
let settings = XdgPortalSettings::new().await?;
let color_scheme = settings.color_scheme().await?;
Ok(color_scheme.into())
}
5 changes: 0 additions & 5 deletions src/platforms/freedesktop/detect.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/platforms/freedesktop/subscribe.rs

This file was deleted.

File renamed without changes.
2 changes: 0 additions & 2 deletions src/platforms/macos/mod.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/platforms/macos/subscribe.rs

This file was deleted.

File renamed without changes.
2 changes: 0 additions & 2 deletions src/platforms/websys/mod.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/platforms/websys/subscribe.rs

This file was deleted.

File renamed without changes.
2 changes: 0 additions & 2 deletions src/platforms/windows/mod.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/platforms/windows/subscribe.rs

This file was deleted.

0 comments on commit 6289e15

Please sign in to comment.