pub struct ThemeSubscription { /* private fields */ }Expand description
RAII guard that keeps a theme watcher alive.
Holds a background thread and a shutdown channel. When dropped, the channel sender is dropped (signaling shutdown via disconnection) and the background thread is joined. On macOS it holds the notification observer instead, and dropping it removes the observer.
Note: ThemeSubscription is Send but not Sync. The background thread
handle and shutdown channel are not safe to share across threads, but the
guard can be moved to a different thread if needed.
§Dropping
Drop joins the watcher thread: a callback that is running when the drop
starts finishes before the drop returns, and none runs after it. A
callback may drop its own subscription: the drop then runs on the watcher
thread, so it does not join it but detaches it, and the thread exits once
the callback returns. On macOS, where there is no watcher thread,
dropping removes the observer and waits for a callback already queued to
finish; dropped from inside the callback, it returns without waiting, and
a notification already queued can still run the callback once.
Because the drop waits for the callback, it deadlocks if a callback waits on a lock the dropping thread holds, or, on macOS, dispatches synchronously to the main thread while the main thread drops the subscription.
§Important
You must bind this to a variable. If you discard it, the watcher stops immediately:
// WRONG -- watcher is dropped immediately:
on_theme_change(|e| println!("{e:?}"));
// RIGHT -- watcher lives as long as `_watcher`:
let _watcher = on_theme_change(|e| println!("{e:?}")).unwrap();