summaryrefslogblamecommitdiffstats
path: root/native/src/widget/operation.rs
blob: 2cfba005c56dff1f1e7dc246ebaa7c6f7254580e (plain) (tree)
1
2
3
4
5
6
7
8






                         
                                                                   






























                                                  





                                                   






                             
                                                                       






                                     
use crate::widget::state;
use crate::widget::Id;

pub trait Operation<T> {
    fn container(
        &mut self,
        id: Option<&Id>,
        operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
    );

    fn focusable(
        &mut self,
        _state: &mut dyn state::Focusable,
        _id: Option<&Id>,
    ) {
    }

    fn finish(&self) -> Outcome<T> {
        Outcome::None
    }
}

pub enum Outcome<T> {
    None,
    Some(T),
    Chain(Box<dyn Operation<T>>),
}

pub fn focus<T>(target: Id) -> impl Operation<T> {
    struct Focus {
        target: Id,
    }

    impl<T> Operation<T> for Focus {
        fn focusable(
            &mut self,
            state: &mut dyn state::Focusable,
            id: Option<&Id>,
        ) {
            match id {
                Some(id) if id == &self.target => {
                    state.focus();
                }
                _ => {
                    state.unfocus();
                }
            }
        }

        fn container(
            &mut self,
            _id: Option<&Id>,
            operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
        ) {
            operate_on_children(self)
        }
    }

    Focus { target }
}