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 implementingUiBindable. Defaults to unit type().
§Fields
document- The parsed UI document containing widget tree and themesmodel- Application state model for data bindingshandler_registry- Registry of event handlers for UI interactions_marker- Type marker to capture the generic parameter
Fields§
§document: DampenDocumentThe parsed UI document containing widget tree and themes.
model: MApplication state model for data bindings.
Generic over UiBindable for type-safe field access.
handler_registry: HandlerRegistryRegistry of event handlers for UI interactions.
Implementations§
Source§impl<M: UiBindable> AppState<M>
impl<M: UiBindable> AppState<M>
Sourcepub fn new(document: DampenDocument) -> Selfwhere
M: Default,
pub fn new(document: DampenDocument) -> Selfwhere
M: Default,
Sourcepub fn with_model(document: DampenDocument, model: M) -> Self
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);Sourcepub fn with_handlers(
document: DampenDocument,
handler_registry: HandlerRegistry,
) -> Selfwhere
M: Default,
pub fn with_handlers(
document: DampenDocument,
handler_registry: HandlerRegistry,
) -> Selfwhere
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);Sourcepub fn with_all(
document: DampenDocument,
model: M,
handler_registry: HandlerRegistry,
) -> Self
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);Sourcepub fn hot_reload(&mut self, new_document: DampenDocument)
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);