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

Improvements to error handling #46

Merged
merged 3 commits into from
Oct 10, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build = "build.rs"

[dependencies]
futures = "0.3.30"
anyhow = "1.0.79"
log = "0.4.22"

[dev-dependencies]
tokio = { version = "1.23.0", features = ["full"] }
Expand Down
4 changes: 3 additions & 1 deletion examples/notify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::error::Error;

use futures::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> Result<(), Box<dyn Error>> {
let mut stream = dark_light::subscribe().await?;
while let Some(mode) = stream.next().await {
println!("System theme changed: {:?}", mode);
Expand Down
3 changes: 1 addition & 2 deletions src/platforms/freedesktop/detect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::initial_value;
use crate::Mode;

pub fn detect() -> Mode {
pollster::block_on(initial_value())
pollster::block_on(super::get_color_scheme())
}
20 changes: 12 additions & 8 deletions src/platforms/freedesktop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ impl From<PortalColorScheme> for Mode {
}
}

pub(crate) async fn initial_value() -> Mode {
XdgPortalSettings::new()
.await
.unwrap()
.color_scheme()
.await
.unwrap()
.into()
pub(crate) async fn get_color_scheme() -> Mode {
let Ok(settings) = XdgPortalSettings::new().await else {
log::error!("Failed to create a new XDG Desktop Portal settings instance.");
return Mode::Default;
};

let Ok(color_scheme) = settings.color_scheme().await else {
log::error!("Failed to get the current color scheme from XDG Desktop Portal, defaulting to Mode::Default.");
return Mode::Default;
};

color_scheme.into()
}
6 changes: 4 additions & 2 deletions src/platforms/freedesktop/notify.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::error::Error;

use ashpd::desktop::settings::Settings;
use futures::{stream, Stream, StreamExt};

use crate::Mode;

pub async fn subscribe() -> anyhow::Result<impl Stream<Item = Mode> + Send> {
let initial = stream::once(super::initial_value()).boxed();
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let initial = stream::once(super::get_color_scheme()).boxed();
let later_updates = Settings::new()
.await?
.receive_color_scheme_changed()
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/macos/notify.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error::Error;
use std::task::Poll;

use futures::{stream, Stream};

use crate::{detect, Mode};

pub async fn subscribe() -> anyhow::Result<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/websys/notify.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error::Error;
use std::task::Poll;

use futures::{stream, Stream};

use crate::{detect, Mode};

pub async fn subscribe() -> anyhow::Result<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/windows/notify.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error::Error;
use std::task::Poll;

use futures::{stream, Stream};

use crate::{detect, Mode};

pub async fn subscribe() -> anyhow::Result<impl Stream<Item = Mode> + Send> {
pub async fn subscribe() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> {
let mut last_mode = detect();

let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
Expand Down
Loading