Expand description
Runtime theme change watching.
Requires the watch feature.
Runtime theme change watching.
This module provides the public API for monitoring OS theme changes at
runtime. Call on_theme_change() with a callback to start watching;
the returned ThemeSubscription keeps the watcher alive via RAII semantics
– dropping it stops the watcher and joins the background thread.
§RAII ownership model
ThemeSubscription is an RAII guard. Dropping it stops the watcher and
joins the background thread. You must bind it to a variable – if
you discard the return value, the watcher is dropped immediately and
no events are ever delivered.
§Shutdown mechanism
When a ThemeSubscription is dropped, shutdown proceeds in three phases:
- Platform-specific wakeup – if a platform shutdown closure was registered (see constructor below), it runs first. This wakes the background thread’s event loop so it can observe the disconnect.
- Channel disconnect – the shutdown channel sender is dropped,
causing the receiver in the background thread to see
Disconnectedon its nextrecv()ortry_recv(). - Thread join –
JoinHandle::join()blocks until the background thread exits, ensuring clean shutdown before the guard is gone.
§Constructor
There is a single pub(crate) constructor:
- [
ThemeSubscription::new(tx, handle, platform_shutdown)] – the optionalplatform_shutdownclosure wakes the background thread’s event loop on platforms where dropping the channel sender alone is not sufficient (CFRunLoop::stopon macOS,PostThreadMessageW(WM_QUIT)on Windows,Connection::closeon GNOME’s D-Bus signal iterator). PassNoneon KDE, where inotify polls the channel directly.
§Signal-only events
ThemeChangeEvent carries no theme data. When you receive an event,
re-run SystemTheme::from_system()
to get the updated theme.
§Example
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
let _watcher = native_theme::watch::on_theme_change(move |event| {
let _ = tx.send(event);
})?;
// On your UI thread:
// if let Ok(event) = rx.try_recv() {
// let theme = native_theme::SystemTheme::from_system()?;
// // re-apply theme ...
// }Structs§
- Theme
Subscription - RAII guard that keeps a theme watcher alive.
Enums§
- Theme
Change Event - A signal that the OS theme has changed.
Functions§
- on_
theme_ change - Start watching for OS theme changes.