HandlerRegistry

Struct HandlerRegistry 

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

Registry of event handlers

Implementations§

Source§

impl HandlerRegistry

Source

pub fn new() -> Self

Create a new empty handler registry

Source

pub fn register_simple<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any) + Send + Sync + 'static,

Register a simple handler

Source

pub fn register_with_value<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any, Box<dyn Any>) + Send + Sync + 'static,

Register a handler with a value parameter

Source

pub fn register_with_command<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any) -> Box<dyn Any> + Send + Sync + 'static,

Register a handler that returns a command

Source

pub fn register_with_shared<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any, &dyn Any) + Send + Sync + 'static,

Register a handler that receives shared context.

Use this for handlers that need to read or modify shared state that is accessible across multiple views.

§Arguments
  • name - Handler name (referenced in XML on_click="name")
  • handler - Function that receives (&mut Model, &SharedContext<S>)
§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_with_shared("update_theme", |model, shared| {
    let model = model.downcast_mut::<Model>().unwrap();
    let shared = shared.downcast_ref::<SharedContext<SharedState>>().unwrap();
    shared.write().theme = model.selected_theme.clone();
});
Source

pub fn register_with_value_and_shared<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any, Box<dyn Any>, &dyn Any) + Send + Sync + 'static,

Register a handler with both a value parameter and shared context.

Use this for handlers that receive input (like text field values) and also need access to shared state.

§Arguments
  • name - Handler name (referenced in XML on_change="name")
  • handler - Function that receives (&mut Model, Box<dyn Any>, &SharedContext<S>)
§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_with_value_and_shared("set_username", |model, value, shared| {
    let model = model.downcast_mut::<Model>().unwrap();
    let name = value.downcast_ref::<String>().unwrap();
    let shared = shared.downcast_ref::<SharedContext<SharedState>>().unwrap();
    shared.write().username = name.clone();
});
Source

pub fn register_with_command_and_shared<F>(&self, name: &str, handler: F)
where F: Fn(&mut dyn Any, &dyn Any) -> Box<dyn Any> + Send + Sync + 'static,

Register a handler that receives shared context and returns a command.

Use this for async handlers that need shared state access.

§Arguments
  • name - Handler name (referenced in XML on_click="name")
  • handler - Function that receives (&mut Model, &SharedContext<S>) -> Command
§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_with_command_and_shared("sync_settings", |model, shared| {
    let shared = shared.downcast_ref::<SharedContext<SharedState>>().unwrap();
    let settings = shared.read().clone();
    Box::new(Task::perform(save_settings(settings), Message::SettingsSaved))
});
Source

pub fn get(&self, name: &str) -> Option<HandlerEntry>

Look up a handler by name

Source

pub fn dispatch( &self, handler_name: &str, model: &mut dyn Any, value: Option<String>, )

Dispatches a handler by name, executing it with the provided model and optional value.

This is a convenience method that combines get() and handler invocation. For handlers that require shared state, use dispatch_with_shared instead.

§Arguments
  • handler_name - Name of the handler to dispatch
  • model - Mutable reference to the model (as &mut dyn Any)
  • value - Optional string value passed to WithValue handlers
§Note

This method does NOT support WithShared, WithValueAndShared, or WithCommandAndShared handlers. Those handlers will be silently ignored. Use dispatch_with_shared instead.

§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_simple("greet", |model| {
    let model = model.downcast_mut::<MyModel>().unwrap();
    model.count += 1;
});

let model = &mut MyModel { count: 0 } as &mut dyn std::any::Any;
registry.dispatch("greet", model, None);
Source

pub fn dispatch_with_command( &self, handler_name: &str, model: &mut dyn Any, value: Option<String>, ) -> Option<Box<dyn Any>>

Dispatches a handler by name and returns any command/task it produces.

This method is similar to dispatch() but returns the command/task from WithCommand handlers instead of discarding it. This is essential for integrating with the Elm/MVU pattern where handlers can return tasks.

For handlers that require shared state, use dispatch_with_shared instead.

§Arguments
  • handler_name - Name of the handler to dispatch
  • model - Mutable reference to the model (as &mut dyn Any)
  • value - Optional string value passed to WithValue handlers
§Returns
  • Some(Box<dyn Any>) - The command/task from a WithCommand handler
  • None - For Simple and WithValue handlers, or if handler not found
§Note

This method does NOT support shared handlers. Use dispatch_with_shared instead.

§Example
use dampen_core::HandlerRegistry;
use iced::Task;

let registry = HandlerRegistry::new();
registry.register_with_command("navigate", |model| {
    let model = model.downcast_mut::<MyModel>().unwrap();
    Box::new(Task::done(Message::SwitchView))
});

let model = &mut MyModel::default() as &mut dyn std::any::Any;
if let Some(boxed_task) = registry.dispatch_with_command("navigate", model, None) {
    if let Ok(task) = boxed_task.downcast::<Task<Message>>() {
        return *task;
    }
}
Source

pub fn dispatch_with_shared( &self, handler_name: &str, model: &mut dyn Any, shared: &dyn Any, value: Option<String>, ) -> Option<Box<dyn Any>>

Dispatches a handler with shared context and returns any command it produces.

This is the primary dispatch method for applications using shared state. It handles all handler variants, passing the shared context to variants that expect it.

§Arguments
  • handler_name - Name of the handler to dispatch
  • model - Mutable reference to the local model (as &mut dyn Any)
  • shared - Reference to the shared context (as &dyn Any)
  • value - Optional string value passed to WithValue/WithValueAndShared handlers
§Returns
  • Some(Box<dyn Any>) - The command from WithCommand or WithCommandAndShared handlers
  • None - For simple handlers, value handlers, or if handler not found
§Example
use dampen_core::HandlerRegistry;

let registry = HandlerRegistry::new();
registry.register_with_shared("toggle_theme", |model, shared| {
    let shared = shared.downcast_ref::<SharedContext<SharedState>>().unwrap();
    let current = shared.read().dark_mode;
    shared.write().dark_mode = !current;
});

let model = &mut Model::default() as &mut dyn std::any::Any;
let shared = &shared_context as &dyn std::any::Any;
registry.dispatch_with_shared("toggle_theme", model, shared, None);
Source

pub fn contains(&self, name: &str) -> bool

Check if a handler exists

Trait Implementations§

Source§

impl Clone for HandlerRegistry

Source§

fn clone(&self) -> HandlerRegistry

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 Debug for HandlerRegistry

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for HandlerRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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.