AppState

Struct AppState 

Source
pub struct AppState<M: UiBindable = ()> {
    pub document: DampenDocument,
    pub model: M,
    pub handler_registry: HandlerRegistry,
    /* private fields */
}
Expand description

Application state container for a Dampen UI view.

This struct combines the parsed UI document with application state and event handlers. It is the central state structure used throughout Dampen applications.

§Type Parameters

  • M - The model type implementing UiBindable. Defaults to unit type ().

§Fields

  • document - The parsed UI document containing widget tree and themes
  • model - Application state model for data bindings
  • handler_registry - Registry of event handlers for UI interactions
  • _marker - Type marker to capture the generic parameter

Fields§

§document: DampenDocument

The parsed UI document containing widget tree and themes.

§model: M

Application state model for data bindings. Generic over UiBindable for type-safe field access.

§handler_registry: HandlerRegistry

Registry of event handlers for UI interactions.

Implementations§

Source§

impl<M: UiBindable> AppState<M>

Source

pub fn new(document: DampenDocument) -> Self
where M: Default,

Creates a new AppState with default model and empty handler registry.

§Examples
use dampen_core::{parse, AppState};

let xml = r#"<column><text value="Hello!" /></column>"#;
let document = parse(xml).unwrap();
let state = AppState::new(document);
Source

pub fn with_model(document: DampenDocument, model: M) -> Self

Creates an AppState with a custom model and default handler registry.

§Examples
use dampen_core::{parse, AppState};
use dampen_macros::UiModel;

#[derive(UiModel, Default)]
struct MyModel {
    count: i32,
}

let xml = r#"<column><text value="Hello!" /></column>"#;
let document = parse(xml).unwrap();
let model = MyModel { count: 42 };
let state = AppState::with_model(document, model);
Source

pub fn with_handlers( document: DampenDocument, handler_registry: HandlerRegistry, ) -> Self
where M: Default,

Creates an AppState with a custom handler registry and default model.

§Examples
use dampen_core::{parse, AppState, HandlerRegistry};

let xml = r#"<column><text value="Hello!" /></column>"#;
let document = parse(xml).unwrap();
let mut registry = HandlerRegistry::new();
registry.register_simple("greet", |_model| {
    println!("Button clicked!");
});
let state = AppState::with_handlers(document, registry);
Source

pub fn with_all( document: DampenDocument, model: M, handler_registry: HandlerRegistry, ) -> Self

Creates an AppState with custom model and handler registry.

This is the most flexible constructor, allowing you to specify all components of the application state. Useful for hot-reload scenarios where both model and handlers need to be specified.

§Examples
use dampen_core::{parse, AppState, HandlerRegistry};
use dampen_macros::UiModel;

#[derive(UiModel, Default)]
struct MyModel {
    count: i32,
}

let xml = r#"<column><text value="Hello!" /></column>"#;
let document = parse(xml).unwrap();
let model = MyModel { count: 42 };
let mut registry = HandlerRegistry::new();
registry.register_simple("increment", |model| {
    let model = model.downcast_mut::<MyModel>().unwrap();
    model.count += 1;
});
let state = AppState::with_all(document, model, registry);
Source

pub fn hot_reload(&mut self, new_document: DampenDocument)

Hot-reload: updates the UI document while preserving the model and handlers.

This method is designed for development mode hot-reload scenarios where the UI definition (XML) changes but the application state (model) should be preserved.

§Examples
use dampen_core::{parse, AppState};
use dampen_macros::UiModel;

#[derive(UiModel, Default)]
struct MyModel {
    count: i32,
}

let xml_v1 = r#"<column><text value="Old UI" /></column>"#;
let document_v1 = parse(xml_v1).unwrap();
let mut state = AppState::with_model(document_v1, MyModel { count: 42 });

// Later, the UI file changes...
let xml_v2 = r#"<column><text value="New UI" /></column>"#;
let document_v2 = parse(xml_v2).unwrap();
state.hot_reload(document_v2);

// Model state (count: 42) is preserved
assert_eq!(state.model.count, 42);

Trait Implementations§

Source§

impl<M: Clone + UiBindable> Clone for AppState<M>

Source§

fn clone(&self) -> AppState<M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug + UiBindable> Debug for AppState<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M> Freeze for AppState<M>
where M: Freeze,

§

impl<M> RefUnwindSafe for AppState<M>
where M: RefUnwindSafe,

§

impl<M> Send for AppState<M>
where M: Send,

§

impl<M> Sync for AppState<M>
where M: Sync,

§

impl<M> Unpin for AppState<M>
where M: Unpin,

§

impl<M> UnwindSafe for AppState<M>
where M: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.