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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std::cell::RefCell;
use std::rc::Rc;

use drawing::primitive::Primitive;
use drawing::units::{PixelPoint, PixelRect, PixelSize};
use fui_core::*;
use typed_builder::TypedBuilder;

use crate::style::*;

#[derive(TypedBuilder)]
pub struct ScrollBar {
    #[builder(default = Orientation::Vertical)]
    pub orientation: Orientation,

    #[builder(default = Property::new(0.0f32))]
    pub min_value: Property<f32>,

    #[builder(default = Property::new(1.0f32))]
    pub max_value: Property<f32>,

    #[builder(default = Property::new(0.0f32))]
    pub value: Property<f32>,

    /// How much of the range of value is visible on the screen
    /// (affects the length of thumb)
    #[builder(default = Property::new(0.0f32))]
    pub viewport_size: Property<f32>,

    /// How much to modify the value on mouse wheel
    #[builder(default = Property::new(0.05f32))]
    pub single_step_size: Property<f32>,
}

impl ScrollBar {
    pub fn to_view(
        self,
        style: Option<Box<dyn Style<Self>>>,
        context: ViewContext,
    ) -> Rc<RefCell<dyn ControlObject>> {
        StyledControl::new(
            self,
            style.unwrap_or_else(|| {
                Box::new(DefaultScrollBarStyle::new(
                    DefaultScrollBarStyleParams::builder().build(),
                ))
            }),
            context,
        )
    }
}

//
// Default ScrollBar Style
//

const START_MARGIN: f32 = 1.0f32;
const END_MARGIN: f32 = 1.0f32;
const SIDE_MARGIN: f32 = 1.0f32;
const MIN_THUMB_SIZE: f32 = 20.0f32;
const MIN_SIZE: f32 = MIN_THUMB_SIZE * 2.0f32;

#[derive(TypedBuilder)]
pub struct DefaultScrollBarStyleParams {}

pub struct DefaultScrollBarStyle {
    thumb_pos_px: f32,
    thumb_size_px: f32,

    is_thumb_hover: Property<bool>,
    is_thumb_pressed: Property<bool>,
    pressed_offset: f32,
}

impl DefaultScrollBarStyle {
    pub fn new(_params: DefaultScrollBarStyleParams) -> Self {
        DefaultScrollBarStyle {
            thumb_pos_px: 0f32,
            thumb_size_px: 0f32,
            is_thumb_hover: Property::new(false),
            is_thumb_pressed: Property::new(false),
            pressed_offset: 0.0f32,
        }
    }

    fn calc_sizes(&mut self, data: &ScrollBar, rect: Rect) {
        let scroll_bar_size_px = match data.orientation {
            Orientation::Horizontal => rect.width - START_MARGIN - END_MARGIN,
            Orientation::Vertical => rect.height - START_MARGIN - END_MARGIN,
        };
        let scroll_bar_size_f32 =
            data.max_value.get() - data.min_value.get() + data.viewport_size.get();

        self.thumb_size_px = ((data.viewport_size.get() * scroll_bar_size_px)
            / scroll_bar_size_f32)
            .round()
            .max(MIN_THUMB_SIZE);

        self.thumb_pos_px = ((scroll_bar_size_px - self.thumb_size_px)
            * (data.value.get() - data.min_value.get())
            / (data.max_value.get() - data.min_value.get()))
        .round();
    }
}

impl Style<ScrollBar> for DefaultScrollBarStyle {
    fn setup(&mut self, data: &mut ScrollBar, control_context: &mut ControlContext) {
        control_context.dirty_watch_property(&self.is_thumb_hover);
        control_context.dirty_watch_property(&self.is_thumb_pressed);
        control_context.dirty_watch_property(&data.min_value);
        control_context.dirty_watch_property(&data.max_value);
        control_context.dirty_watch_property(&data.value);
        control_context.dirty_watch_property(&data.viewport_size);
    }

    fn handle_event(
        &mut self,
        data: &mut ScrollBar,
        control_context: &mut ControlContext,
        _drawing_context: &mut dyn DrawingContext,
        _event_context: &mut dyn EventContext,
        event: ControlEvent,
    ) {
        match event {
            ControlEvent::TapDown { position } => {
                let rect = control_context.get_rect();
                let pos = match data.orientation {
                    Orientation::Horizontal => position.x - rect.x - START_MARGIN,
                    Orientation::Vertical => position.y - rect.y - START_MARGIN,
                };
                if pos >= self.thumb_pos_px && pos < self.thumb_pos_px + self.thumb_size_px {
                    self.is_thumb_pressed.set(true);
                    self.pressed_offset = pos - self.thumb_pos_px;
                }
            }

            ControlEvent::TapUp { .. } => {
                self.is_thumb_pressed.set(false);
            }

            ControlEvent::TapMove { ref position } => {
                if self.is_thumb_pressed.get() {
                    let rect = control_context.get_rect();

                    let scroll_bar_size_px = match data.orientation {
                        Orientation::Horizontal => rect.width - START_MARGIN - END_MARGIN,
                        Orientation::Vertical => rect.height - START_MARGIN - END_MARGIN,
                    };

                    let pos = match data.orientation {
                        Orientation::Horizontal => position.x - rect.x - START_MARGIN,
                        Orientation::Vertical => position.y - rect.y - START_MARGIN,
                    };

                    let new_thumb_pos_px = pos - self.pressed_offset;
                    let new_value = (data.min_value.get()
                        + new_thumb_pos_px * (data.max_value.get() - data.min_value.get())
                            / (scroll_bar_size_px - self.thumb_size_px))
                        .max(data.min_value.get())
                        .min(data.max_value.get());

                    if new_value != data.value.get() {
                        data.value.set(new_value);
                    }
                }
            }

            ControlEvent::ScrollWheel { delta } => {
                match delta {
                    ScrollDelta::LineDelta(x, y) => {
                        let single_step = data.single_step_size.get();
                        let min_value = data.min_value.get();
                        let max_value = data.max_value.get();
                        let steps = if let Orientation::Vertical = data.orientation {
                            y
                        } else {
                            if x != 0.0f32 {
                                x
                            } else {
                                y
                            }
                        };
                        data.value.change(move |v| {
                            (v - steps * single_step).min(max_value).max(min_value)
                        });
                    }
                    ScrollDelta::PixelDelta(_, _) => (),
                };
            }

            ControlEvent::HoverChange(value) => {
                self.is_thumb_hover.set(value);
            }

            _ => (),
        }
    }

    fn measure(
        &mut self,
        data: &mut ScrollBar,
        _control_context: &mut ControlContext,
        _drawing_context: &mut dyn DrawingContext,
        size: Size,
    ) -> Size {
        match data.orientation {
            Orientation::Horizontal => {
                let space = if size.width.is_infinite() {
                    MIN_SIZE
                } else {
                    size.width
                };
                Size::new(MIN_SIZE.max(space), 20.0f32)
            }
            Orientation::Vertical => {
                let space = if size.height.is_infinite() {
                    MIN_SIZE
                } else {
                    size.height
                };
                Size::new(20.0f32, MIN_SIZE.max(space))
            }
        }
    }

    fn set_rect(
        &mut self,
        data: &mut ScrollBar,
        _control_context: &mut ControlContext,
        _drawing_context: &mut dyn DrawingContext,
        rect: Rect,
    ) {
        self.calc_sizes(data, rect);
    }

    fn hit_test(
        &self,
        _data: &ScrollBar,
        control_context: &ControlContext,
        point: Point,
    ) -> Option<Rc<RefCell<dyn ControlObject>>> {
        if point.is_inside(&control_context.get_rect()) {
            Some(control_context.get_self_rc())
        } else {
            None
        }
    }

    fn to_primitives(
        &self,
        data: &ScrollBar,
        control_context: &ControlContext,
        _drawing_context: &mut dyn DrawingContext,
    ) -> (Vec<Primitive>, Vec<Primitive>) {
        let rect = control_context.get_rect();
        let x = rect.x;
        let y = rect.y;
        let width = rect.width;
        let height = rect.height;

        let scroll_bar_size_px = match data.orientation {
            Orientation::Horizontal => width - START_MARGIN - END_MARGIN,
            Orientation::Vertical => height - START_MARGIN - END_MARGIN,
        };

        let background = [0.0, 0.0, 0.0, 0.25];

        let mut vec = Vec::new();
        if self.thumb_pos_px > 0.0f32 {
            vec.push(Primitive::Rectangle {
                color: background,
                rect: match data.orientation {
                    Orientation::Horizontal => PixelRect::new(
                        PixelPoint::new(x + START_MARGIN, y + SIDE_MARGIN),
                        PixelSize::new(self.thumb_pos_px, height - SIDE_MARGIN - SIDE_MARGIN),
                    ),
                    Orientation::Vertical => PixelRect::new(
                        PixelPoint::new(x + SIDE_MARGIN, y + START_MARGIN),
                        PixelSize::new(width - SIDE_MARGIN - SIDE_MARGIN, self.thumb_pos_px),
                    ),
                },
            });
        }

        match data.orientation {
            Orientation::Horizontal => default_theme::button(
                &mut vec,
                x + self.thumb_pos_px + START_MARGIN,
                y + SIDE_MARGIN,
                self.thumb_size_px,
                height - SIDE_MARGIN - SIDE_MARGIN,
                self.is_thumb_pressed.get(),
                self.is_thumb_hover.get(),
                false,
            ),
            Orientation::Vertical => default_theme::button(
                &mut vec,
                x + SIDE_MARGIN,
                y + self.thumb_pos_px + START_MARGIN,
                width - SIDE_MARGIN - SIDE_MARGIN,
                self.thumb_size_px,
                self.is_thumb_pressed.get(),
                self.is_thumb_hover.get(),
                false,
            ),
        };

        if self.thumb_pos_px + self.thumb_size_px < scroll_bar_size_px {
            vec.push(Primitive::Rectangle {
                color: background,
                rect: match data.orientation {
                    Orientation::Horizontal => PixelRect::new(
                        PixelPoint::new(
                            x + self.thumb_pos_px + self.thumb_size_px + START_MARGIN,
                            y + SIDE_MARGIN,
                        ),
                        PixelSize::new(
                            scroll_bar_size_px - self.thumb_pos_px - self.thumb_size_px,
                            height - SIDE_MARGIN - SIDE_MARGIN,
                        ),
                    ),
                    Orientation::Vertical => PixelRect::new(
                        PixelPoint::new(
                            x + SIDE_MARGIN,
                            y + self.thumb_pos_px + self.thumb_size_px + START_MARGIN,
                        ),
                        PixelSize::new(
                            width - SIDE_MARGIN - SIDE_MARGIN,
                            scroll_bar_size_px - self.thumb_pos_px - self.thumb_size_px,
                        ),
                    ),
                },
            });
        }

        default_theme::border_3d_single(&mut vec, x, y, width, height, true, false, false);

        (vec, Vec::new())
    }
}