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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::icons::{
    ICON_CHEVRON_DOWN, ICON_CHEVRON_LEFT, ICON_CHEVRON_RIGHT, ICON_CHEVRON_UP, ICON_MINUS,
    ICON_PLUS,
};
use crate::prelude::*;

pub enum SpinboxEvent {
    Increment,
    Decrement,
}

#[derive(Lens)]
pub struct Spinbox {
    orientation: Orientation,
    icons: SpinboxIcons,

    on_decrement: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
    on_increment: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
}

#[derive(Clone, Copy, Debug, PartialEq, Data)]
pub enum SpinboxIcons {
    PlusMinus,
    Chevrons,
}

impl_res_simple!(SpinboxIcons);

impl Spinbox {
    pub fn new<L>(cx: &mut Context, lens: L) -> Handle<Spinbox>
    where
        L: Lens<Target: Data + ToStringLocalized>,
    {
        Self::custom(cx, move |cx| Label::new(cx, lens))
    }

    pub fn custom<F, V>(cx: &mut Context, content: F) -> Handle<Spinbox>
    where
        F: Fn(&mut Context) -> Handle<V>,
        V: 'static + View,
    {
        Self {
            orientation: Orientation::Horizontal,
            icons: SpinboxIcons::Chevrons,
            on_decrement: None,
            on_increment: None,
        }
        .build(cx, move |cx| {
            Binding::new(cx, Spinbox::orientation, move |cx, spinbox_kind| {
                match spinbox_kind.get(cx) {
                    Orientation::Horizontal => {
                        Button::new(cx, |cx| {
                            Svg::new(
                                cx,
                                Spinbox::icons.map(|icons| match icons {
                                    SpinboxIcons::PlusMinus => ICON_MINUS,
                                    SpinboxIcons::Chevrons => ICON_CHEVRON_LEFT,
                                }),
                            )
                        })
                        .on_press(|ex| ex.emit(SpinboxEvent::Decrement))
                        .navigable(true)
                        .class("spinbox-button");
                    }

                    Orientation::Vertical => {
                        Button::new(cx, |cx| {
                            Svg::new(
                                cx,
                                Spinbox::icons.map(|icons| match icons {
                                    SpinboxIcons::PlusMinus => ICON_PLUS,
                                    SpinboxIcons::Chevrons => ICON_CHEVRON_UP,
                                }),
                            )
                        })
                        .on_press(|ex| ex.emit(SpinboxEvent::Increment))
                        .navigable(true)
                        .class("spinbox-button");
                    }
                }
            });
            (content)(cx).class("spinbox-value");
            Binding::new(cx, Spinbox::orientation, move |cx, spinbox_kind| {
                match spinbox_kind.get(cx) {
                    Orientation::Horizontal => {
                        Button::new(cx, |cx| {
                            Svg::new(
                                cx,
                                Spinbox::icons.map(|icons| match icons {
                                    SpinboxIcons::PlusMinus => ICON_PLUS,
                                    SpinboxIcons::Chevrons => ICON_CHEVRON_RIGHT,
                                }),
                            )
                        })
                        .on_press(|ex| ex.emit(SpinboxEvent::Increment))
                        .navigable(true)
                        .class("spinbox-button");
                    }

                    Orientation::Vertical => {
                        Button::new(cx, |cx| {
                            Svg::new(
                                cx,
                                Spinbox::icons.map(|icons| match icons {
                                    SpinboxIcons::PlusMinus => ICON_MINUS,
                                    SpinboxIcons::Chevrons => ICON_CHEVRON_DOWN,
                                }),
                            )
                        })
                        .on_press(|ex| ex.emit(SpinboxEvent::Decrement))
                        .navigable(true)
                        .class("spinbox-button");
                    }
                }
            });
        })
        .toggle_class("horizontal", Spinbox::orientation.map(|o| o == &Orientation::Horizontal))
        .toggle_class("vertical", Spinbox::orientation.map(|o| o == &Orientation::Vertical))
        .navigable(true)
    }
}

impl<'a> Handle<'a, Spinbox> {
    pub fn on_increment<F>(self, callback: F) -> Self
    where
        F: 'static + Fn(&mut EventContext) + Send + Sync,
    {
        self.modify(|spinbox: &mut Spinbox| spinbox.on_increment = Some(Box::new(callback)))
    }

    pub fn on_decrement<F>(self, callback: F) -> Self
    where
        F: 'static + Fn(&mut EventContext) + Send + Sync,
    {
        self.modify(|spinbox: &mut Spinbox| spinbox.on_decrement = Some(Box::new(callback)))
    }

    pub fn orientation(self, orientation: impl Res<Orientation>) -> Self {
        self.bind(orientation, move |handle, orientation| {
            let orientation = orientation.get(&handle);
            handle.modify(move |spinbox| spinbox.orientation = orientation);
        })
    }

    pub fn icons(self, icons: impl Res<SpinboxIcons>) -> Self {
        self.bind(icons, move |handle, icons| {
            let icons = icons.get(&handle);
            handle.modify(move |spinbox| spinbox.icons = icons);
        })
    }
}

impl View for Spinbox {
    fn element(&self) -> Option<&'static str> {
        Some("spinbox")
    }

    fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
        event.map(|spinbox_event, _| match spinbox_event {
            SpinboxEvent::Increment => {
                if let Some(callback) = &self.on_increment {
                    (callback)(cx)
                }
            }

            SpinboxEvent::Decrement => {
                if let Some(callback) = &self.on_decrement {
                    (callback)(cx)
                }
            }
        });
    }
}