dark_light/watch.rs
1use std::sync::mpsc;
2
3use crate::Mode;
4
5/// A handle to a background theme-change watcher.
6///
7/// Dropping the `Watcher` stops the underlying platform watcher and joins its
8/// background thread (where applicable).
9///
10/// Only mode transitions are emitted: if the OS reports the same mode twice
11/// in a row, no duplicate message is sent.
12pub struct Watcher {
13 pub(crate) receiver: mpsc::Receiver<Mode>,
14 #[allow(dead_code)]
15 pub(crate) guard: crate::platforms::platform::WatchGuard,
16}
17
18impl Watcher {
19 /// Blocks until the next theme change is received.
20 pub fn recv(&self) -> Result<Mode, mpsc::RecvError> {
21 self.receiver.recv()
22 }
23
24 /// Returns the next theme change if one is already available, without blocking.
25 pub fn try_recv(&self) -> Result<Mode, mpsc::TryRecvError> {
26 self.receiver.try_recv()
27 }
28
29 /// Returns a blocking iterator over theme changes.
30 pub fn iter(&self) -> mpsc::Iter<'_, Mode> {
31 self.receiver.iter()
32 }
33
34 /// Returns a non-blocking iterator that yields only already-available theme changes.
35 pub fn try_iter(&self) -> mpsc::TryIter<'_, Mode> {
36 self.receiver.try_iter()
37 }
38}