Skip to main content

ThemeWatcher

Struct ThemeWatcher 

Source
pub struct ThemeWatcher { /* private fields */ }
Available on crate feature 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

Source

pub fn new(path: impl AsRef<Path>) -> Result<ThemeWatcher, ThemeLoadError>

Available on crate feature serde only.

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();
Source

pub fn current(&self) -> &ThemeFile

Available on crate feature serde only.

The most recently parsed theme (the initial load, or the last good hot-reload). Never returns a theme from a failed parse.

Source

pub fn poll(&mut self) -> Option<ThemeFile>

Available on crate feature serde only.

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);
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.