theframework 0.9.3

A cross platform application UI framework.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use crate::prelude::*;

pub struct TheVerticalScrollbar {
    id: TheId,
    limiter: TheSizeLimiter,

    state: TheWidgetState,

    scroll_offset: i32,
    total_height: i32,

    mouse_down_coord: Vec2<i32>,

    dim: TheDim,
    is_dirty: bool,
}

impl TheWidget for TheVerticalScrollbar {
    fn new(id: TheId) -> Self
    where
        Self: Sized,
    {
        let mut limiter = TheSizeLimiter::new();
        limiter.set_max_width(142);
        limiter.set_max_height(20);

        Self {
            id,
            limiter,

            state: TheWidgetState::None,

            scroll_offset: 0,
            total_height: 0,

            mouse_down_coord: Vec2::zero(),

            dim: TheDim::zero(),
            is_dirty: false,
        }
    }

    fn id(&self) -> &TheId {
        &self.id
    }

    fn on_event(&mut self, event: &TheEvent, ctx: &mut TheContext) -> bool {
        let mut redraw = false;
        // println!("event ({}): {:?}", self.widget_id.name, event);
        match event {
            TheEvent::MouseDown(coord) => {
                let dim = TheDim::new(
                    0,
                    self.scrollbar_position() as i32,
                    self.dim.width,
                    self.scrollbar_thumb_height(),
                );
                if dim.contains(*coord) {
                    self.is_dirty = true;
                    if self.state != TheWidgetState::Clicked {
                        self.state = TheWidgetState::Clicked;
                        ctx.ui.send_widget_state_changed(self.id(), self.state);
                        ctx.ui.set_focus(self.id());
                        self.mouse_down_coord = *coord;
                    }
                } else {
                    self.is_dirty = true;
                    self.scroll_from_track_click(coord.y);
                }
                redraw = true;
            }
            TheEvent::MouseDragged(coord) => {
                if self.state == TheWidgetState::Clicked {
                    self.is_dirty = true;
                    redraw = true;
                    let d = coord - self.mouse_down_coord;
                    self.adjust_scroll_from_thumb_delta(d.y);
                    self.mouse_down_coord = *coord;
                }
            }
            TheEvent::MouseUp(_coord) => {
                self.is_dirty = true;
                if self.state == TheWidgetState::Clicked {
                    self.state = TheWidgetState::None;
                    ctx.ui.send_widget_state_changed(self.id(), self.state);
                }
                redraw = true;
            }
            TheEvent::Hover(coord) => {
                if self.state != TheWidgetState::Clicked {
                    let dim = TheDim::new(
                        0,
                        self.scrollbar_position() as i32,
                        self.dim.width,
                        self.scrollbar_thumb_height(),
                    );
                    if dim.contains(*coord) {
                        if !self.id().equals(&ctx.ui.hover) {
                            self.is_dirty = true;
                            ctx.ui.set_hover(self.id());
                            redraw = true;
                        }
                    } else {
                        self.is_dirty = true;
                        ctx.ui.clear_hover();
                        redraw = true;
                    }
                }
            }
            TheEvent::ScrollBy(_id, delta) => {
                self.adjust_scroll_from_thumb_delta(delta.y);
            }
            _ => {}
        }
        redraw
    }

    fn dim(&self) -> &TheDim {
        &self.dim
    }

    fn dim_mut(&mut self) -> &mut TheDim {
        &mut self.dim
    }

    fn set_dim(&mut self, dim: TheDim, _ctx: &mut TheContext) {
        if self.dim != dim {
            self.dim = dim;
            self.is_dirty = true;
        }
    }

    fn limiter(&self) -> &TheSizeLimiter {
        &self.limiter
    }

    fn limiter_mut(&mut self) -> &mut TheSizeLimiter {
        &mut self.limiter
    }

    fn needs_redraw(&mut self) -> bool {
        self.is_dirty
    }

    fn set_needs_redraw(&mut self, redraw: bool) {
        self.is_dirty = redraw;
    }

    fn state(&self) -> TheWidgetState {
        self.state
    }

    fn set_state(&mut self, state: TheWidgetState) {
        self.state = state;
        self.is_dirty = true;
    }

    fn supports_hover(&mut self) -> bool {
        true
    }

    fn draw(
        &mut self,
        buffer: &mut TheRGBABuffer,
        style: &mut Box<dyn TheStyle>,
        ctx: &mut TheContext,
    ) {
        if !self.dim().is_valid() {
            return;
        }

        let stride = buffer.stride();

        let utuple: (usize, usize, usize, usize) = self.dim.to_buffer_utuple();

        ctx.draw.rect(
            buffer.pixels_mut(),
            &utuple,
            stride,
            style.theme().color(ScrollbarBackground),
        );

        let mut icon_name = if self.state == TheWidgetState::Clicked {
            "dark_vertical_scrollbar_clicked_".to_string()
        } else {
            "dark_vertical_scrollbar_normal_".to_string()
        };

        if self.state != TheWidgetState::Clicked && self.id().equals(&ctx.ui.hover) {
            icon_name = "dark_vertical_scrollbar_hover_".to_string()
        }

        let mut scroll_bar_height = self.scrollbar_thumb_height();
        let mut offset = self.scrollbar_position() as usize;

        if scroll_bar_height > self.dim.height {
            offset = 0;
            scroll_bar_height = self.dim.height;
        }

        let safe_utuple = self.dim.to_buffer_utuple();

        if scroll_bar_height >= 5 {
            if let Some(icon) = ctx.ui.icon(&(icon_name.clone() + "top")) {
                let r = (
                    utuple.0 as isize,
                    (utuple.1 + offset) as isize,
                    icon.dim().width as usize,
                    icon.dim().height as usize,
                );
                ctx.draw.blend_slice_safe(
                    buffer.pixels_mut(),
                    icon.pixels(),
                    &r,
                    stride,
                    &safe_utuple,
                );
            }
        }

        if scroll_bar_height > 10 {
            if let Some(icon) = ctx.ui.icon(&(icon_name.clone() + "middle")) {
                let mut r = (
                    utuple.0 as isize,
                    (utuple.1 + offset + 5) as isize,
                    icon.dim().width as usize,
                    icon.dim().height as usize,
                );
                for _ in 0..scroll_bar_height - 10 {
                    ctx.draw.blend_slice_safe(
                        buffer.pixels_mut(),
                        icon.pixels(),
                        &r,
                        stride,
                        &safe_utuple,
                    );
                    r.1 += 1;
                }
            }
        }

        if scroll_bar_height >= 10 {
            if let Some(icon) = ctx.ui.icon(&(icon_name + "bottom")) {
                let r = (
                    utuple.0 as isize,
                    (utuple.1 + offset + scroll_bar_height as usize - 5) as isize,
                    icon.dim().width as usize,
                    icon.dim().height as usize,
                );
                ctx.draw.blend_slice_safe(
                    buffer.pixels_mut(),
                    icon.pixels(),
                    &r,
                    stride,
                    &safe_utuple,
                );
            }
        }

        self.is_dirty = false;
    }

    fn as_vertical_scrollbar(&mut self) -> Option<&mut dyn TheVerticalScrollbarTrait> {
        Some(self)
    }

    fn as_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

pub trait TheVerticalScrollbarTrait {
    /// Returns the total height of the content.
    fn total_height(&self) -> i32;

    /// Sets the total height of the content.
    fn set_total_height(&mut self, value: i32);

    /// Returns the visible height of the widget.
    fn viewport_height(&self) -> i32;

    /// Returns the current vertical scroll offset.
    fn scroll_offset(&self) -> i32;

    /// Sets the vertical scroll offset.
    fn set_scroll_offset(&mut self, offset: i32);

    /// Helper function to scroll by a certain amount (delta).
    /// This can be positive (to scroll down) or negative (to scroll up).
    fn scroll_by(&mut self, delta: i32) {
        let new_offset = (self.scroll_offset() + delta)
            .max(0)
            .min(self.total_height() - self.viewport_height());
        self.set_scroll_offset(new_offset);
    }

    /// Helper function to determine if the scrollbar is needed.
    fn needs_scrollbar(&self) -> bool;

    /// Get the maximum scroll offset for the thumb.
    fn max_thumb_offset(&self) -> i32 {
        self.viewport_height() - self.scrollbar_thumb_height()
    }

    /// Get the position of the scrollbar slider, accounting for the border.
    fn scrollbar_position(&self) -> f32 {
        if self.needs_scrollbar() {
            (self.scroll_offset() as f32 * self.max_thumb_offset() as f32)
                / (self.total_height() - self.viewport_height()) as f32
        } else {
            0.0
        }
    }

    /// Get the height of the scrollbar slider (thumb) as a proportion of the viewport height.
    fn scrollbar_thumb_height(&self) -> i32 {
        (self.viewport_height() as f32
            * (self.viewport_height() as f32 / self.total_height() as f32)) as i32
    }

    /// Adjust the scroll offset based on the mouse's delta movement on the thumb.
    fn adjust_scroll_from_thumb_delta(&mut self, delta_y: i32) {
        let thumb_height = self.scrollbar_thumb_height();
        let scale_factor = (self.total_height() - self.viewport_height()) as f32
            / (self.viewport_height() - thumb_height) as f32;

        let content_delta_y = (delta_y as f32 * scale_factor) as i32;

        self.scroll_by(content_delta_y);
    }

    /// Scroll content based on a click on the scrollbar track.
    fn scroll_from_track_click(&mut self, click_y: i32) {
        let thumb_top = self.scrollbar_position() as i32;
        let thumb_bottom = thumb_top + self.scrollbar_thumb_height();

        let new_offset;
        if click_y < thumb_top {
            // Page up
            new_offset = self.scroll_offset() - self.viewport_height();
        } else if click_y > thumb_bottom {
            // Page down
            new_offset = self.scroll_offset() + self.viewport_height();
        } else {
            return;
        }

        let clamped_offset = new_offset
            .max(0)
            .min(self.total_height() - self.viewport_height());
        self.set_scroll_offset(clamped_offset);
    }

    /// Adjust the scroll offset and total height based on a new zoom level.
    fn adjust_to_new_zoom_level(&mut self, new_zoom_level: f32, last_zoom_level: f32) {
        // Calculate the middle point of the currently visible content.
        let middle_point =
            (self.scroll_offset() + self.viewport_height() / 2) as f32 / last_zoom_level;

        // Calculate the new total height based on the new zoom level.
        let base_total_height = self.total_height() as f32 / last_zoom_level;
        let new_total_height = (base_total_height * new_zoom_level).round() as i32;

        // Calculate the new scroll offset so that the middle point remains visible.
        let new_scroll_offset =
            (middle_point * new_zoom_level - self.viewport_height() as f32 / 2.0).round() as i32;

        // Clamp the new scroll offset to ensure it is within bounds.
        let clamped_new_scroll_offset = new_scroll_offset
            .min(new_total_height - self.viewport_height())
            .max(0);

        // Set the new total height and scroll offset.
        self.set_total_height(new_total_height);
        self.set_scroll_offset(clamped_new_scroll_offset);
    }

    /// Scrolls to center the specified offset within the total height.
    fn scroll_to(&mut self, offset: i32) {
        let viewport_half_height = self.viewport_height() / 2;
        let new_scroll_offset = offset - viewport_half_height;

        // Clamp the new scroll offset to ensure it doesn't go out of bounds.
        let clamped_scroll_offset = new_scroll_offset
            .max(0)
            .min(self.total_height() - self.viewport_height());

        // Set the new scroll offset.
        self.set_scroll_offset(clamped_scroll_offset);
    }
}

impl TheVerticalScrollbarTrait for TheVerticalScrollbar {
    fn total_height(&self) -> i32 {
        self.total_height
    }

    fn set_total_height(&mut self, value: i32) {
        self.total_height = value;
    }

    fn viewport_height(&self) -> i32 {
        self.dim().height
    }

    fn scroll_offset(&self) -> i32 {
        self.scroll_offset
    }

    fn set_scroll_offset(&mut self, offset: i32) {
        self.scroll_offset = offset;
        self.is_dirty = true;
    }

    fn needs_scrollbar(&self) -> bool {
        self.total_height() > self.dim().height
    }
}