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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! `ScrollBar` control

use super::{GripMsg, GripPart, ScrollRegion};
use kas::event::Scroll;
use kas::prelude::*;
use kas::theme::Feature;
use std::fmt::Debug;

/// Message from a [`ScrollBar`]
#[derive(Copy, Clone, Debug)]
pub struct ScrollMsg(pub i32);

impl_scope! {
    /// A scroll bar
    ///
    /// Scroll bars allow user-input of a value between 0 and a defined maximum,
    /// and allow the size of the grip to be specified.
    ///
    /// # Messages
    ///
    /// On value change, pushes a value of type [`ScrollMsg`].
    ///
    /// # Layout
    ///
    /// It is safe to not call `size_rules` before `set_rect` for this type.
    #[derive(Clone, Debug, Default)]
    #[widget(hover_highlight = true;)]
    pub struct ScrollBar<D: Directional = Direction> {
        core: widget_core!(),
        align: AlignPair,
        direction: D,
        // Terminology assumes vertical orientation:
        min_grip_len: i32, // units: px
        grip_len: i32, // units: px
        // grip_size, max_value and value are all in arbitrary (user-provided) units:
        grip_size: i32, // contract: > 0; relative to max_value
        max_value: i32,
        value: i32,
        invisible: bool,
        force_visible: bool,
        #[widget]
        grip: GripPart,
    }

    impl Self
    where
        D: Default,
    {
        /// Construct a scroll bar
        ///
        /// Default values are assumed for all parameters.
        #[inline]
        pub fn new() -> Self {
            ScrollBar::new_dir(D::default())
        }
    }
    impl ScrollBar<kas::dir::Down> {
        /// Construct a scroll bar (vertical)
        ///
        /// Default values are assumed for all parameters.
        pub fn down() -> Self {
            ScrollBar::new()
        }
    }
    impl ScrollBar<kas::dir::Right> {
        /// Construct a scroll bar (horizontal)
        ///
        /// Default values are assumed for all parameters.
        pub fn right() -> Self {
            ScrollBar::new()
        }
    }

    impl Self {
        /// Construct a scroll bar with the given direction
        ///
        /// Default values are assumed for all parameters.
        #[inline]
        pub fn new_dir(direction: D) -> Self {
            ScrollBar {
                core: Default::default(),
                align: Default::default(),
                direction,
                min_grip_len: 0,
                grip_len: 0,
                grip_size: 1,
                max_value: 0,
                value: 0,
                invisible: false,
                force_visible: false,
                grip: GripPart::new(),
            }
        }

        /// Get the scroll bar's direction
        #[inline]
        pub fn direction(&self) -> Direction {
            self.direction.as_direction()
        }

        /// Set invisible property
        ///
        /// An "invisible" scroll bar is only drawn on mouse-hover
        #[inline]
        pub fn set_invisible(&mut self, invisible: bool) {
            self.invisible = invisible;
        }

        /// Set invisible property (inline)
        ///
        /// An "invisible" scroll bar is only drawn on mouse-hover
        #[inline]
        pub fn with_invisible(mut self, invisible: bool) -> Self {
            self.invisible = invisible;
            self
        }

        /// Set the initial page length
        ///
        /// See [`ScrollBar::set_limits`].
        #[inline]
        #[must_use]
        pub fn with_limits(mut self, max_value: i32, grip_size: i32) -> Self {
            let _ = self.set_limits(max_value, grip_size);
            self
        }

        /// Set the initial value
        #[inline]
        #[must_use]
        pub fn with_value(mut self, value: i32) -> Self {
            self.value = value.clamp(0, self.max_value);
            self
        }

        /// Set the page limits
        ///
        /// The `max_value` parameter specifies the maximum possible value.
        /// (The minimum is always 0.) For a scroll region, this should correspond
        /// to the maximum possible offset.
        ///
        /// The `grip_size` parameter specifies the size of the grip relative to
        /// the maximum value: the grip size relative to the length of the scroll
        /// bar is `grip_size / (max_value + grip_size)`. For a scroll region,
        /// this should correspond to the size of the visible region.
        /// The minimum value is 1.
        ///
        /// The choice of units is not important (e.g. can be pixels or lines),
        /// so long as both parameters use the same units.
        ///
        /// Returns [`Action::REDRAW`] if a redraw is required.
        pub fn set_limits(&mut self, max_value: i32, grip_size: i32) -> Action {
            // We should gracefully handle zero, though appearance may be wrong.
            self.grip_size = grip_size.max(1);

            self.max_value = max_value.max(0);
            self.value = self.value.clamp(0, self.max_value);
            self.update_widgets()
        }

        /// Read the current max value
        ///
        /// See also the [`ScrollBar::set_limits`] documentation.
        #[inline]
        pub fn max_value(&self) -> i32 {
            self.max_value
        }

        /// Read the current grip value
        ///
        /// See also the [`ScrollBar::set_limits`] documentation.
        #[inline]
        pub fn grip_size(&self) -> i32 {
            self.grip_size
        }

        /// Get the current value
        #[inline]
        pub fn value(&self) -> i32 {
            self.value
        }

        /// Set the value
        ///
        /// Returns true if the value changes.
        pub fn set_value(&mut self, cx: &mut EventState, value: i32) -> bool {
            let value = value.clamp(0, self.max_value);
            let changed = value != self.value;
            if changed {
                self.value = value;
                let action = self.grip.set_offset(self.offset()).1;
                cx.action(&self, action);
            }
            self.force_visible(cx);
            changed
        }

        fn force_visible(&mut self, cx: &mut EventState) {
            self.force_visible = true;
            let delay = cx.config().touch_select_delay();
            cx.request_timer(self.id(), 0, delay);
        }

        #[inline]
        fn bar_len(&self) -> i32 {
            match self.direction.is_vertical() {
                false => self.core.rect.size.0,
                true => self.core.rect.size.1,
            }
        }

        fn update_widgets(&mut self) -> Action {
            let len = self.bar_len();
            let total = 1i64.max(i64::from(self.max_value) + i64::from(self.grip_size));
            let grip_len = i64::from(self.grip_size) * i64::conv(len) / total;
            self.grip_len = i32::conv(grip_len).max(self.min_grip_len).min(len);
            let mut size = self.core.rect.size;
            if self.direction.is_horizontal() {
                size.0 = self.grip_len;
            } else {
                size.1 = self.grip_len;
            }
            self.grip.set_size_and_offset(size, self.offset())
        }

        // translate value to offset in local coordinates
        fn offset(&self) -> Offset {
            let len = self.bar_len() - self.grip_len;
            let lhs = i64::from(self.value) * i64::conv(len);
            let rhs = i64::from(self.max_value);
            let mut pos = if rhs == 0 {
                0
            } else {
                i32::conv((lhs + (rhs / 2)) / rhs).min(len)
            };
            if self.direction.is_reversed() {
                pos = len - pos;
            }
            match self.direction.is_vertical() {
                false => Offset(pos, 0),
                true => Offset(0, pos),
            }
        }

        // true if not equal to old value
        fn apply_grip_offset(&mut self, cx: &mut EventCx, offset: Offset) {
            let (offset, action) = self.grip.set_offset(offset);
            cx.action(&self, action);

            let len = self.bar_len() - self.grip_len;
            let mut offset = match self.direction.is_vertical() {
                false => offset.0,
                true => offset.1,
            };
            if self.direction.is_reversed() {
                offset = len - offset;
            }

            let lhs = i64::from(offset) * i64::from(self.max_value);
            let rhs = i64::conv(len);
            if rhs == 0 {
                debug_assert_eq!(self.value, 0);
                return;
            }
            let value = i32::conv((lhs + (rhs / 2)) / rhs);
            if self.set_value(cx, value) {
                cx.push(ScrollMsg(value));
            }
        }
    }

    impl Layout for Self {
        fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
            self.align.set_component(
                axis,
                match axis.is_vertical() == self.direction.is_vertical() {
                    false => axis.align_or_center(),
                    true => axis.align_or_stretch(),
                },
            );
            let _ = self.grip.size_rules(sizer.re(), axis);
            sizer.feature(Feature::ScrollBar(self.direction()), axis)
        }

        fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
            let rect = cx.align_feature(Feature::ScrollBar(self.direction()), rect, self.align);
            self.core.rect = rect;
            self.grip.set_rect(cx, rect);
            self.min_grip_len = cx.size_cx().grip_len();
            let _ = self.update_widgets();
        }

        fn find_id(&mut self, coord: Coord) -> Option<Id> {
            if !self.rect().contains(coord) {
                return None;
            }
            if self.invisible && self.max_value == 0 {
                return None;
            }
            self.grip.find_id(coord).or_else(|| Some(self.id()))
        }

        fn draw(&mut self, mut draw: DrawCx) {
            if draw.ev_state().is_hovered_recursive(self.id_ref()) {
                self.force_visible(draw.ev_state());
            }

            if !self.invisible
                || (self.max_value != 0 && self.force_visible)
                || draw.ev_state().is_depressed(self.grip.id_ref())
            {
                let dir = self.direction.as_direction();
                draw.scroll_bar(self.rect(), &self.grip, dir);
            }
        }
    }

    impl Events for Self {
        type Data = ();

        fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
            match event {
                Event::Timer(_) => {
                    self.force_visible = false;
                    cx.redraw(self);
                    Used
                }
                Event::PressStart { press } => {
                    let offset = self.grip.handle_press_on_track(cx, &press);
                    self.apply_grip_offset(cx, offset);
                    Used
                }
                _ => Unused,
            }
        }

        fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
            if let Some(GripMsg::PressMove(offset)) = cx.try_pop() {
                self.apply_grip_offset(cx, offset);
            }
        }
    }
}

impl_scope! {
    /// Scroll bar controls
    ///
    /// This is a wrapper adding scroll bar controls around a child. Note that this
    /// widget does not enable scrolling; see [`ScrollBarRegion`] for that.
    ///
    /// Scroll bar positioning does not respect the inner widget's margins, since
    /// the result looks poor when content is scrolled. Instead the content should
    /// force internal margins by wrapping contents with a (zero-sized) frame.
    /// [`ScrollRegion`] already does this.
    #[autoimpl(Deref, DerefMut using self.inner)]
    #[autoimpl(class_traits using self.inner where W: trait)]
    #[impl_default(where W: trait)]
    #[derive(Clone, Debug)]
    #[widget {
        Data = W::Data;
    }]
    pub struct ScrollBars<W: Scrollable + Widget> {
        core: widget_core!(),
        mode: ScrollBarMode,
        show_bars: (bool, bool), // set by user (or set_rect when mode == Auto)
        #[widget(&())]
        horiz_bar: ScrollBar<kas::dir::Right>,
        #[widget(&())]
        vert_bar: ScrollBar<kas::dir::Down>,
        #[widget]
        inner: W,
    }

    impl Self {
        /// Construct
        ///
        /// By default scroll bars are automatically enabled based on requirements.
        /// Use the [`HasScrollBars`] trait to adjust this behaviour.
        #[inline]
        pub fn new(inner: W) -> Self {
            ScrollBars {
                core: Default::default(),
                mode: ScrollBarMode::Auto,
                show_bars: (false, false),
                horiz_bar: ScrollBar::new(),
                vert_bar: ScrollBar::new(),
                inner,
            }
        }

        /// Access inner widget directly
        #[inline]
        pub fn inner(&self) -> &W {
            &self.inner
        }

        /// Access inner widget directly
        #[inline]
        pub fn inner_mut(&mut self) -> &mut W {
            &mut self.inner
        }

        fn draw_(&mut self, mut draw: DrawCx) {
            draw.recurse(&mut self.inner);
            draw.with_pass(|mut draw| {
                if self.show_bars.0 {
                    draw.recurse(&mut self.horiz_bar);
                }
                if self.show_bars.1 {
                    draw.recurse(&mut self.vert_bar);
                }
            });
        }
    }

    impl HasScrollBars for Self {
        fn get_mode(&self) -> ScrollBarMode {
            self.mode
        }
        fn set_mode(&mut self, mode: ScrollBarMode) -> Action {
            self.mode = mode;
            let invisible = mode == ScrollBarMode::Invisible;
            self.horiz_bar.set_invisible(invisible);
            self.vert_bar.set_invisible(invisible);
            Action::RESIZE
        }

        fn get_visible_bars(&self) -> (bool, bool) {
            self.show_bars
        }
        fn set_visible_bars(&mut self, bars: (bool, bool)) -> Action {
            self.show_bars = bars;
            Action::RESIZE
        }
    }

    impl Scrollable for Self {
        #[inline]
        fn scroll_axes(&self, size: Size) -> (bool, bool) {
            self.inner.scroll_axes(size)
        }
        #[inline]
        fn max_scroll_offset(&self) -> Offset {
            self.inner.max_scroll_offset()
        }
        #[inline]
        fn scroll_offset(&self) -> Offset {
            self.inner.scroll_offset()
        }
        fn set_scroll_offset(&mut self, cx: &mut EventCx, offset: Offset) -> Offset {
            let offset = self.inner.set_scroll_offset(cx, offset);
            self.horiz_bar.set_value(cx, offset.0);
            self.vert_bar.set_value(cx, offset.1);
            offset
        }
    }

    impl Layout for Self {
        fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
            let mut rules = self.inner.size_rules(sizer.re(), axis);
            let vert_rules = self.vert_bar.size_rules(sizer.re(), axis);
            let horiz_rules = self.horiz_bar.size_rules(sizer.re(), axis);
            let (use_horiz, use_vert) = match self.mode {
                ScrollBarMode::Fixed => self.show_bars,
                ScrollBarMode::Auto => (true, true),
                ScrollBarMode::Invisible => (false, false),
            };
            if axis.is_horizontal() && use_horiz {
                rules.append(vert_rules);
            } else if axis.is_vertical() && use_vert {
                rules.append(horiz_rules);
            }
            rules
        }

        fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
            self.core.rect = rect;
            let pos = rect.pos;
            let mut child_size = rect.size;

            let bar_width = cx.size_cx().scroll_bar_width();
            if self.mode == ScrollBarMode::Auto {
                self.show_bars = self.inner.scroll_axes(child_size);
            }
            if self.show_bars.0 && !self.horiz_bar.invisible {
                child_size.1 -= bar_width;
            }
            if self.show_bars.1 && !self.vert_bar.invisible {
                child_size.0 -= bar_width;
            }

            let child_rect = Rect::new(pos, child_size);
            self.inner.set_rect(cx, child_rect);
            let max_scroll_offset = self.inner.max_scroll_offset();

            if self.show_bars.0 {
                let pos = Coord(pos.0, rect.pos2().1 - bar_width);
                let size = Size::new(child_size.0, bar_width);
                self.horiz_bar.set_rect(cx, Rect { pos, size });
                let _ = self.horiz_bar.set_limits(max_scroll_offset.0, rect.size.0);
            } else {
                self.horiz_bar.set_rect(cx, Rect::ZERO);
            }

            if self.show_bars.1 {
                let pos = Coord(rect.pos2().0 - bar_width, pos.1);
                let size = Size::new(bar_width, self.core.rect.size.1);
                self.vert_bar.set_rect(cx, Rect { pos, size });
                let _ = self.vert_bar.set_limits(max_scroll_offset.1, rect.size.1);
            } else {
                self.vert_bar.set_rect(cx, Rect::ZERO);
            }
        }

        fn find_id(&mut self, coord: Coord) -> Option<Id> {
            if !self.rect().contains(coord) {
                return None;
            }
            self.vert_bar
                .find_id(coord)
                .or_else(|| self.horiz_bar.find_id(coord))
                .or_else(|| self.inner.find_id(coord))
                .or_else(|| Some(self.id()))
        }

        #[cfg(feature = "min_spec")]
        default fn draw(&mut self, draw: DrawCx) {
            self.draw_(draw);
        }
        #[cfg(not(feature = "min_spec"))]
        fn draw(&mut self, draw: DrawCx) {
            self.draw_(draw);
        }
    }

    #[cfg(feature = "min_spec")]
    impl<W: Widget> Layout for ScrollBars<ScrollRegion<W>> {
        fn draw(&mut self, mut draw: DrawCx) {
            // Enlarge clip region to *our* rect:
            draw.with_clip_region(self.core.rect, self.inner.scroll_offset(), |mut draw| {
                draw.recurse(&mut self.inner);
            });
            // Use a second clip region to force draw order:
            draw.with_clip_region(self.core.rect, Offset::ZERO, |mut draw| {
                if self.show_bars.0 {
                    draw.recurse(&mut self.horiz_bar);
                }
                if self.show_bars.1 {
                    draw.recurse(&mut self.vert_bar);
                }
            });
        }
    }

    impl Events for Self {
        fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
            let index = cx.last_child().expect("message not sent from self");
            if index == widget_index![self.horiz_bar] {
                if let Some(ScrollMsg(x)) = cx.try_pop() {
                    let offset = Offset(x, self.inner.scroll_offset().1);
                    self.inner.set_scroll_offset(cx, offset);
                }
            } else if index == widget_index![self.vert_bar] {
                if let Some(ScrollMsg(y)) = cx.try_pop() {
                    let offset = Offset(self.inner.scroll_offset().0, y);
                    self.inner.set_scroll_offset(cx, offset);
                }
            }
        }

        fn handle_scroll(&mut self, cx: &mut EventCx, _: &Self::Data, _: Scroll) {
            // We assume the inner already updated its positions; this is just to set bars
            let offset = self.inner.scroll_offset();
            self.horiz_bar.set_value(cx, offset.0);
            self.vert_bar.set_value(cx, offset.1);
        }
    }
}

impl_scope! {
    /// A scrollable region with bars
    ///
    /// This is essentially a `ScrollBars<ScrollRegion<W>>`:
    /// [`ScrollRegion`] handles the actual scrolling and wheel/touch events,
    /// while [`ScrollBars`] adds scroll bar controls.
    ///
    /// Use the [`HasScrollBars`] trait to adjust scroll bar behaviour.
    #[autoimpl(Deref, DerefMut, HasScrollBars, Scrollable using self.0)]
    #[autoimpl(class_traits using self.0 where W: trait)]
    #[derive(Clone, Debug, Default)]
    #[widget{
        derive = self.0;
    }]
    pub struct ScrollBarRegion<W: Widget>(ScrollBars<ScrollRegion<W>>);

    impl Self {
        /// Construct a `ScrollBarRegion<W>`
        #[inline]
        pub fn new(inner: W) -> Self {
            ScrollBarRegion(ScrollBars::new(ScrollRegion::new(inner)))
        }

        /// Access inner widget directly
        #[inline]
        pub fn inner(&self) -> &W {
            self.0.inner.inner()
        }

        /// Access inner widget directly
        #[inline]
        pub fn inner_mut(&mut self) -> &mut W {
            self.0.inner.inner_mut()
        }
    }
}