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 implementingUiBindable. Defaults to unit type().S- The shared state type implementingUiBindable+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 themesmodel- Application state model for data bindingshandler_registry- Registry of event handlers for UI interactionsshared_context- Optional reference to shared state across viewstheme_context- Optional theme context for theming support
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.
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, ()>
impl<M: UiBindable> AppState<M, ()>
Sourcepub fn new(document: DampenDocument) -> Selfwhere
M: Default,
pub fn new(document: DampenDocument) -> Selfwhere
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);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 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>
impl<M: UiBindable, S: UiBindable + Send + Sync + 'static> AppState<M, S>
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 documentmodel- Local model for this viewhandler_registry- Event handlers for this viewshared_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,
);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, 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);Sourcepub fn set_theme_context(&mut self, theme_context: ThemeContext)
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
Sourcepub fn theme_context(&self) -> Option<&ThemeContext>
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
Sourcepub fn theme_context_mut(&mut self) -> Option<&mut ThemeContext>
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