Skip to main content

ThemeManager

Struct ThemeManager 

Source
pub struct ThemeManager { /* private fields */ }
Expand description

Runtime theme manager with observer notifications.

Holds one active CooljapanTheme and notifies all registered listeners whenever set_theme is called.

§Example

use oxiui_core::{Color, FontSpec, Palette};
use oxiui_theme::{CooljapanTheme};
use oxiui_theme::manager::ThemeManager;
use std::sync::{Arc, Mutex};

let initial = CooljapanTheme::new(
    Palette {
        background: Color(0, 0, 0, 255),
        surface: Color(10, 10, 26, 255),
        primary: Color(255, 255, 0, 255),
        on_primary: Color(0, 0, 0, 255),
        text: Color(255, 255, 255, 255),
        muted: Color(200, 200, 200, 255),
    },
    FontSpec::new("Inter", 14.0, 400),
);
let mut manager = ThemeManager::new(initial.clone());
let called = Arc::new(Mutex::new(false));
let c = called.clone();
manager.subscribe(Box::new(move |_| { *c.lock().unwrap() = true; }));
manager.set_theme(initial);
assert!(*called.lock().unwrap());

Implementations§

Source§

impl ThemeManager

Source

pub fn new(initial: CooljapanTheme) -> Self

Construct a manager starting with initial as the active theme.

Source

pub fn theme(&self) -> &CooljapanTheme

Return a reference to the currently active theme.

Source

pub fn set_theme(&mut self, theme: CooljapanTheme)

Switch the active theme and notify every registered listener.

Source

pub fn subscribe(&mut self, f: ThemeListener) -> ListenerId

Register a listener and return its ListenerId.

The listener is called synchronously inside set_theme with a reference to the new theme.

Source

pub fn unsubscribe(&mut self, id: ListenerId)

Remove the listener registered with id.

If id is not found, this is a no-op.

Source

pub fn listener_count(&self) -> usize

Returns the number of currently registered listeners.

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.