StateMachineChild

Trait StateMachineChild 

Source
pub trait StateMachineChild {
    // Required method
    fn id(&self) -> Arc<SourceID>;

    // Provided methods
    fn init(
        &self,
        driver: &Weak<Driver>,
    ) -> Result<Box<dyn StateMachineWrapper>, Error> { ... }
    fn apply_children(
        &self,
        _: &mut dyn FnMut(&dyn StateMachineChild) -> Result<()>,
    ) -> Result<()> { ... }
}
Expand description

Represents any potentially stateful component. All components must implement this trait because all components are tracked by the state manager even if they are stateless. A derive macro feather_macro::StateMachineChild is provided to make it easier for stateless components to correctly implement StateMachineChild and correctly propagate events to their children. It is important that this is done correctly, as a component can be stateless itself, but have stateful children.

§Examples

use feather_ui::component::ChildOf;
use feather_ui::layout::fixed;
use feather_ui::{ StateMachineChild, SourceID};
use std::sync::Arc;
use std::rc::Rc;

pub struct MyComponent<T> {
    pub id: Arc<SourceID>,
    pub props: Rc<T>,
    pub children: im::Vector<Option<Box<ChildOf<dyn fixed::Prop>>>>,
}

impl<T: Default> StateMachineChild for MyComponent<T> {
    fn id(&self) -> std::sync::Arc<SourceID> {
        self.id.clone()
    }
    fn apply_children(
        &self,
        f: &mut dyn FnMut(&dyn StateMachineChild) -> eyre::Result<()>,
    ) -> eyre::Result<()> {
        self.children
            .iter()
            .try_for_each(|x| f(x.as_ref().unwrap().as_ref()))
    }
}

Required Methods§

Source

fn id(&self) -> Arc<SourceID>

Provided Methods§

Source

fn init( &self, driver: &Weak<Driver>, ) -> Result<Box<dyn StateMachineWrapper>, Error>

Source

fn apply_children( &self, _: &mut dyn FnMut(&dyn StateMachineChild) -> Result<()>, ) -> Result<()>

Implementors§