summaryrefslogtreecommitdiffstats
path: root/native/src/widget/operation.rs
blob: b6c108e0df74971a1d525f56d4fe396ce89f9486 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::widget::state;
use crate::widget::Id;

pub trait Operation<T> {
    fn container(
        &mut self,
        id: Option<&Id>,
        operate_on_children: &dyn Fn(&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>,
        ) {
            if state.is_focused() {
                match id {
                    Some(id) if id == &self.target => {
                        state.focus();
                    }
                    _ => {
                        state.unfocus();
                    }
                }
            }
        }

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

    Focus { target }
}