AppState

Struct AppState 

Source
pub struct AppState<M: UiBindable = (), S: UiBindable + Send + Sync + 'static = ()> {
    pub document: DampenDocument,
    pub model: M,
    pub handler_registry: HandlerRegistry,
    pub shared_context: Option<SharedContext<S>>,
    pub theme_context: Option<ThemeContext>,
    /* 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 local model type implementing UiBindable. Defaults to unit type ().
  • S - The shared state type implementing UiBindable + Send + Sync. Defaults to unit type ().

§Backward Compatibility

For applications not using shared state, S defaults to () and shared_context is None. All existing code continues to work unchanged.

§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
  • shared_context - Optional reference to shared state across views
  • theme_context - Optional theme context for theming support

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.

§shared_context: Option<SharedContext<S>>

Optional reference to shared context for inter-window communication.

§theme_context: Option<ThemeContext>

Optional theme context for theming support. None when no theme.dampen file is present.

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.

This is the simplest constructor for static UIs that don’t use data binding or shared state.

§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 for apps without shared state, 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§

impl<M: UiBindable, S: UiBindable + Send + Sync + 'static> AppState<M, S>

Source

pub fn with_shared( document: DampenDocument, model: M, handler_registry: HandlerRegistry, shared_context: SharedContext<S>, ) -> Self

Creates an AppState with shared context for inter-window communication.

This constructor is used when your application needs to share state across multiple views.

§Arguments
  • document - Parsed UI document
  • model - Local model for this view
  • handler_registry - Event handlers for this view
  • shared_context - Shared state accessible from all views
§Examples
use dampen_core::{parse, AppState, SharedContext, HandlerRegistry};
use dampen_macros::UiModel;

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

#[derive(UiModel, Default, Clone)]
struct SharedState { theme: String }

let xml = r#"<column><text value="{shared.theme}" /></column>"#;
let document = parse(xml).unwrap();
let shared = SharedContext::new(SharedState { theme: "dark".to_string() });

let state = AppState::with_shared(
    document,
    MyModel::default(),
    HandlerRegistry::new(),
    shared,
);
Source

pub fn shared(&self) -> Option<RwLockReadGuard<'_, S>>

Get read access to shared state (if configured).

Returns None if this AppState was created without shared context.

§Examples
if let Some(guard) = state.shared() {
    println!("Current theme: {}", guard.theme);
}
Source

pub fn shared_mut(&self) -> Option<RwLockWriteGuard<'_, S>>

Get write access to shared state (if configured).

Returns None if this AppState was created without shared context.

§Examples
if let Some(mut guard) = state.shared_mut() {
    guard.theme = "light".to_string();
}
Source

pub fn has_shared(&self) -> bool

Check if this AppState has shared context configured.

§Examples
if state.has_shared() {
    // Use shared state features
}
Source

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

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

This method is designed for development mode hot-reload scenarios where the UI definition (XML) changes but the application state (model and shared state) 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);
Source

pub fn set_theme_context(&mut self, theme_context: ThemeContext)

Set the theme context for this AppState.

This is used by the application initialization code to load themes from theme.dampen files and apply them to the application.

§Arguments
  • theme_context - The theme context to use for this application state
Source

pub fn theme_context(&self) -> Option<&ThemeContext>

Get a reference to the theme context if available.

§Returns

Reference to the theme context, or None if no theme is loaded

Source

pub fn theme_context_mut(&mut self) -> Option<&mut ThemeContext>

Get a mutable reference to the theme context if available.

§Returns

Mutable reference to the theme context, or None if no theme is loaded

Trait Implementations§

Source§

impl<M: Clone + UiBindable, S: Clone + UiBindable + Send + Sync + 'static> Clone for AppState<M, S>

Source§

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

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, S: Debug + UiBindable + Send + Sync + 'static> Debug for AppState<M, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<M, S> RefUnwindSafe for AppState<M, S>

§

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

§

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

§

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

§

impl<M, S> UnwindSafe for AppState<M, S>
where M: UnwindSafe, S: 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.