Skip to main content

Module watch

Module watch 

Source
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:

  1. 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.
  2. Channel disconnect – the shutdown channel sender is dropped, causing the receiver in the background thread to see Disconnected on its next recv() or try_recv().
  3. 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 optional platform_shutdown closure wakes the background thread’s event loop on platforms where dropping the channel sender alone is not sufficient (CFRunLoop::stop on macOS, PostThreadMessageW(WM_QUIT) on Windows, Connection::close on GNOME’s D-Bus signal iterator). Pass None on 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§

ThemeSubscription
RAII guard that keeps a theme watcher alive.

Enums§

ThemeChangeEvent
A signal that the OS theme has changed.

Functions§

on_theme_change
Start watching for OS theme changes.