[desktop] Add rust-dark-light as direct dependency

This commit is contained in:
Vladimir Stoilov
2025-02-25 11:34:21 +02:00
parent 9ff7ec96d1
commit b2907e9475
23 changed files with 654 additions and 2 deletions

View File

@@ -0,0 +1,11 @@
use crate::Mode;
pub fn detect() -> crate::Mode {
if let Some(window) = web_sys::window() {
let query_result = window.match_media("(prefers-color-scheme: dark)");
if let Ok(Some(mql)) = query_result {
return Mode::from_bool(mql.matches());
}
}
Mode::Light
}

View File

@@ -0,0 +1,2 @@
pub mod detect;
pub mod notify;

View File

@@ -0,0 +1,23 @@
use std::task::Poll;
use futures::{stream, Stream};
use crate::{detect, Mode};
pub async fn subscribe() -> anyhow::Result<impl Stream<Item = Mode> + Send> {
let mut last_mode = detect();
let stream = stream::poll_fn(move |ctx| -> Poll<Option<Mode>> {
let current_mode = detect();
if current_mode != last_mode {
last_mode = current_mode;
Poll::Ready(Some(current_mode))
} else {
ctx.waker().wake_by_ref();
Poll::Pending
}
});
Ok(stream)
}