pub struct ThemeWatcher { /* private fields */ }serde and theme-watch only.Expand description
A non-blocking filesystem watcher that hot-reloads a TOML theme file.
Requires the theme-watch feature. The watcher runs notify’s own
background thread and buffers change events on a channel; poll drains
the channel, re-reads the file, and returns the freshly parsed
ThemeFile. On a parse error it logs context to stderr and keeps the last
good theme, so a half-saved edit never breaks the running app.
Designed for SLT’s immediate-mode loop: call poll once per frame and
apply the result via crate::Context::set_theme.
§Example
use slt::ThemeWatcher;
let mut watcher = ThemeWatcher::new("theme.toml").unwrap();
slt::run(move |ui| {
if let Some(tf) = watcher.poll() {
ui.set_theme(tf.theme);
}
ui.button("Themed");
})
.unwrap();Implementations§
Source§impl ThemeWatcher
impl ThemeWatcher
Sourcepub fn new(path: impl AsRef<Path>) -> Result<ThemeWatcher, ThemeLoadError>
pub fn new(path: impl AsRef<Path>) -> Result<ThemeWatcher, ThemeLoadError>
Start watching the theme file at path, loading it once up front.
§Errors
Returns ThemeLoadError::Io if the initial read fails or the watch
cannot be registered, or ThemeLoadError::Parse if the initial file
is not valid TOML.
§Example
use slt::ThemeWatcher;
let watcher = ThemeWatcher::new("theme.toml").unwrap();Sourcepub fn current(&self) -> &ThemeFile
pub fn current(&self) -> &ThemeFile
The most recently parsed theme (the initial load, or the last good hot-reload). Never returns a theme from a failed parse.
Sourcepub fn poll(&mut self) -> Option<ThemeFile>
pub fn poll(&mut self) -> Option<ThemeFile>
Non-blocking poll for a hot-reloaded theme.
Drains pending filesystem events; if any occurred, re-reads and parses
the watched file. Returns Some(theme) only when the file changed and
parsed cleanly. Returns None when nothing changed, or when the new
contents failed to parse — in which case the previous theme is retained
(accessible via ThemeWatcher::current) and a message is logged to
stderr.
§Example
use slt::ThemeWatcher;
let mut watcher = ThemeWatcher::new("theme.toml").unwrap();
if let Some(tf) = watcher.poll() {
println!("reloaded: {:?}", tf.theme.primary);
}