[desktop] Add rust-dark-light as direct dependency
This commit is contained in:
11
desktop/tauri/rust-dark-light/src/platforms/websys/detect.rs
Normal file
11
desktop/tauri/rust-dark-light/src/platforms/websys/detect.rs
Normal 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
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod detect;
|
||||
pub mod notify;
|
||||
23
desktop/tauri/rust-dark-light/src/platforms/websys/notify.rs
Normal file
23
desktop/tauri/rust-dark-light/src/platforms/websys/notify.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user