cranpose-ui 0.1.99

UI primitives for Cranpose
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
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
//! An explicit scrollbar: a track, a thumb that reports the scroll position,
//! and a thumb the user can drag.
//!
//! A scroll indicator that only reports is half a scrollbar. On a desktop, and
//! on any platform driven by a mouse, the bar is also a control: grabbing the
//! thumb and pulling it is how a long document is crossed, and a bar that
//! cannot be grabbed sends the user back to the wheel for every long jump.
//!
//! The thumb geometry is [`crate::scrollbar`], shared with the curved indicator
//! a round watch draws, so both answer "how long is the thumb and where does it
//! sit" the same way. The drag is [`Modifier::draggable`], so pulling a thumb
//! obeys the same touch slop and axis locking as scrolling the content itself.

#![allow(non_snake_case)]

use crate::composable;
use crate::draggable::rememberDraggableState;
use crate::modifier::Modifier;
use crate::scroll::ScrollState;
use crate::scrollbar::{content_delta_for_thumb_drag, ThumbBounds};
use crate::widgets::scopes::{BoxWithConstraintsScope, BoxWithConstraintsScopeImpl};
use crate::widgets::{BoxWithConstraints, Canvas};
use cranpose_core::NodeId;
use cranpose_ui_graphics::{Brush, Color, CornerRadii, DrawScope, Point, Rect, Size};
use cranpose_ui_layout::Axis;

/// How wide a bar is across its short axis.
pub const DEFAULT_SCROLLBAR_THICKNESS: f32 = 8.0;
/// How short the thumb may get, in logical pixels.
///
/// A thumb proportional to a very long document shrinks to a sliver nobody can
/// hit; a floor in pixels is what keeps it grabbable, and a floor as a fraction
/// of the track — which is what a watch's indicator uses — would make the bar
/// lie about how much content there is on short lists.
pub const DEFAULT_MIN_THUMB_EXTENT: f32 = 24.0;

/// The colours a [`Scrollbar`] paints with.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScrollbarColors {
    /// The rail behind the thumb. Fully transparent hides it.
    pub track: Color,
    /// The thumb at rest.
    pub thumb: Color,
    /// The thumb while it is being dragged.
    pub dragged_thumb: Color,
}

impl ScrollbarColors {
    /// The thumb colour for the current interaction.
    pub fn thumb_for(self, dragging: bool) -> Color {
        if dragging {
            self.dragged_thumb
        } else {
            self.thumb
        }
    }
}

impl Default for ScrollbarColors {
    fn default() -> Self {
        Self {
            track: Color(0.0, 0.0, 0.0, 0.06),
            thumb: Color(0.0, 0.0, 0.0, 0.32),
            dragged_thumb: Color(0.0, 0.0, 0.0, 0.56),
        }
    }
}

/// How a [`Scrollbar`] is drawn and how short its thumb may get.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScrollbarSpec {
    /// Width across the short axis.
    pub thickness: f32,
    /// The shortest the thumb may get, in logical pixels.
    pub min_thumb_extent: f32,
    /// Corner radius of the track and the thumb. Defaults to a full pill.
    pub corner_radius: Option<f32>,
    pub colors: ScrollbarColors,
    /// Whether the bar disappears entirely when the content fits.
    ///
    /// A bar left visible over content that cannot scroll invites a drag that
    /// does nothing.
    pub hide_when_content_fits: bool,
}

impl ScrollbarSpec {
    pub fn thickness(mut self, thickness: f32) -> Self {
        self.thickness = thickness.max(0.0);
        self
    }

    pub fn min_thumb_extent(mut self, extent: f32) -> Self {
        self.min_thumb_extent = extent.max(0.0);
        self
    }

    pub fn corner_radius(mut self, radius: f32) -> Self {
        self.corner_radius = Some(radius.max(0.0));
        self
    }

    pub fn colors(mut self, colors: ScrollbarColors) -> Self {
        self.colors = colors;
        self
    }

    pub fn hide_when_content_fits(mut self, hide: bool) -> Self {
        self.hide_when_content_fits = hide;
        self
    }

    /// The corner radius to paint with on a bar of this thickness: a full pill
    /// unless the caller asked for something squarer.
    pub fn resolved_corner_radius(&self, thickness: f32) -> f32 {
        self.corner_radius.unwrap_or(thickness * 0.5).max(0.0)
    }

    /// How short the thumb may get on a track of `track`, as a fraction.
    pub fn thumb_bounds(&self, track: f32) -> ThumbBounds {
        ThumbBounds::at_least(self.min_thumb_extent, track)
    }
}

impl Default for ScrollbarSpec {
    fn default() -> Self {
        Self {
            thickness: DEFAULT_SCROLLBAR_THICKNESS,
            min_thumb_extent: DEFAULT_MIN_THUMB_EXTENT,
            corner_radius: None,
            colors: ScrollbarColors::default(),
            hide_when_content_fits: true,
        }
    }
}

/// A vertical scrollbar for `state`, drawn down the space the modifier gives it.
///
/// Place it beside or over the scrolling content — a `Box` with the bar aligned
/// to the end edge is the ordinary arrangement.
#[composable]
pub fn VerticalScrollbar(modifier: Modifier, state: ScrollState) -> NodeId {
    Scrollbar(modifier, state, Axis::Vertical, ScrollbarSpec::default())
}

/// A horizontal scrollbar for `state`.
#[composable]
pub fn HorizontalScrollbar(modifier: Modifier, state: ScrollState) -> NodeId {
    Scrollbar(modifier, state, Axis::Horizontal, ScrollbarSpec::default())
}

/// A scrollbar along `axis`, drawn and bounded by `spec`.
#[composable]
pub fn Scrollbar(
    modifier: Modifier,
    state: ScrollState,
    axis: Axis,
    spec: ScrollbarSpec,
) -> NodeId {
    BoxWithConstraints(modifier, move |constraints: BoxWithConstraintsScopeImpl| {
        let constraints = constraints.constraints();
        let track = if axis.is_vertical() {
            constraints.max_height
        } else {
            constraints.max_width
        };
        let track = if track.is_finite() {
            track.max(0.0)
        } else {
            0.0
        };
        let bounds = spec.thumb_bounds(track);

        // The drag runs against the metrics of the frame the finger is in, not
        // the ones composition happened to see, so a thumb dragged while the
        // content is still growing keeps following the finger.
        let dragged = rememberDraggableState(move |delta| {
            let metrics = state.metrics();
            let Some(geometry) = metrics.thumb(bounds) else {
                return;
            };
            let scroll = content_delta_for_thumb_drag(delta, track, geometry, metrics.max_offset);
            if scroll != 0.0 {
                state.dispatch_raw_delta(scroll);
            }
        });

        let drawn = dragged.clone();
        Canvas(
            Modifier::empty()
                .fill_max_size()
                .draggable(axis, dragged.clone()),
            move |scope: &mut dyn DrawScope| {
                draw_scrollbar(scope, state, axis, spec, drawn.is_dragging());
            },
        );
    })
}

/// Draws a scrollbar into a scope whose bounds are the whole bar.
///
/// Split out from the composable so the picture can be asserted against a bare
/// draw scope, and so an application still drawing its own chrome can use it.
pub fn draw_scrollbar(
    scope: &mut dyn DrawScope,
    state: ScrollState,
    axis: Axis,
    spec: ScrollbarSpec,
    dragging: bool,
) {
    let size = scope.size();
    let track = if axis.is_vertical() {
        size.height
    } else {
        size.width
    };
    let thickness = if axis.is_vertical() {
        size.width
    } else {
        size.height
    };
    if track <= 0.0 || thickness <= 0.0 {
        return;
    }

    let metrics = state.metrics();
    let geometry = metrics.thumb(spec.thumb_bounds(track));
    if geometry.is_none() && spec.hide_when_content_fits {
        return;
    }

    let radii = CornerRadii::uniform(spec.resolved_corner_radius(thickness));
    if spec.colors.track.3 > 0.0 {
        scope.draw_round_rect_at(
            Rect::from_size(size),
            Brush::Solid(spec.colors.track),
            radii,
        );
    }

    let Some(geometry) = geometry else {
        return;
    };
    let thumb_extent = (geometry.length * track).max(0.0);
    let thumb_offset = (geometry.offset * track).max(0.0);
    if thumb_extent <= 0.0 {
        return;
    }
    let rect = if axis.is_vertical() {
        Rect::from_origin_size(
            Point::new(0.0, thumb_offset),
            Size::new(thickness, thumb_extent),
        )
    } else {
        Rect::from_origin_size(
            Point::new(thumb_offset, 0.0),
            Size::new(thumb_extent, thickness),
        )
    };
    scope.draw_round_rect_at(rect, Brush::Solid(spec.colors.thumb_for(dragging)), radii);
}

#[cfg(test)]
mod tests {
    use super::*;
    use cranpose_core::{DefaultScheduler, Runtime};
    use cranpose_ui_graphics::{DrawPrimitive, DrawScopeDefault};
    use std::sync::Arc;

    fn scrollable_state(viewport: f32, content: f32, offset: f32) -> ScrollState {
        let state = ScrollState::new(0.0);
        state.set_viewport_extent(viewport);
        state.set_max_value((content - viewport).max(0.0));
        state.scroll_to(offset);
        state
    }

    fn scene(
        size: Size,
        state: ScrollState,
        axis: Axis,
        spec: ScrollbarSpec,
    ) -> Vec<DrawPrimitive> {
        let mut scope = DrawScopeDefault::new(size);
        draw_scrollbar(&mut scope, state, axis, spec, false);
        scope.into_primitives()
    }

    fn rects(primitives: &[DrawPrimitive]) -> Vec<Rect> {
        primitives
            .iter()
            .filter_map(|primitive| match primitive {
                DrawPrimitive::Rect { rect, .. } => Some(*rect),
                DrawPrimitive::RoundRect { rect, .. } => Some(*rect),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn colors_answer_for_the_current_interaction() {
        let colors = ScrollbarColors::default();
        assert_eq!(colors.thumb_for(false), colors.thumb);
        assert_eq!(colors.thumb_for(true), colors.dragged_thumb);
    }

    #[test]
    fn a_bar_is_a_pill_unless_it_was_asked_for_something_squarer() {
        let spec = ScrollbarSpec::default();
        assert_eq!(spec.resolved_corner_radius(8.0), 4.0);
        assert_eq!(spec.corner_radius(0.0).resolved_corner_radius(8.0), 0.0);
        assert_eq!(spec.corner_radius(-3.0).resolved_corner_radius(8.0), 0.0);
    }

    #[test]
    fn spec_builders_clamp_to_drawable_values() {
        let spec = ScrollbarSpec::default()
            .thickness(-4.0)
            .min_thumb_extent(-1.0)
            .hide_when_content_fits(false);
        assert_eq!(spec.thickness, 0.0);
        assert_eq!(spec.min_thumb_extent, 0.0);
        assert!(!spec.hide_when_content_fits);
    }

    #[test]
    fn content_that_fits_draws_nothing_at_all() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let state = scrollable_state(200.0, 200.0, 0.0);
        let primitives = scene(
            Size::new(8.0, 200.0),
            state,
            Axis::Vertical,
            ScrollbarSpec::default(),
        );
        assert!(primitives.is_empty());
    }

    #[test]
    fn content_that_fits_still_draws_its_track_when_the_bar_is_pinned_visible() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let state = scrollable_state(200.0, 200.0, 0.0);
        let spec = ScrollbarSpec::default().hide_when_content_fits(false);
        let primitives = scene(Size::new(8.0, 200.0), state, Axis::Vertical, spec);
        assert_eq!(
            rects(&primitives),
            vec![Rect::from_size(Size::new(8.0, 200.0))]
        );
    }

    #[test]
    fn the_thumb_is_the_share_of_content_on_screen_and_moves_with_it() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let spec = ScrollbarSpec::default().min_thumb_extent(0.0);

        let top = scrollable_state(200.0, 800.0, 0.0);
        let drawn = rects(&scene(Size::new(8.0, 200.0), top, Axis::Vertical, spec));
        assert_eq!(drawn.len(), 2, "a track and a thumb");
        assert_eq!(
            drawn[1],
            Rect::from_origin_size(Point::new(0.0, 0.0), Size::new(8.0, 50.0))
        );

        let bottom = scrollable_state(200.0, 800.0, 600.0);
        let drawn = rects(&scene(Size::new(8.0, 200.0), bottom, Axis::Vertical, spec));
        assert_eq!(
            drawn[1],
            Rect::from_origin_size(Point::new(0.0, 150.0), Size::new(8.0, 50.0))
        );
    }

    #[test]
    fn a_horizontal_bar_lays_its_thumb_along_the_other_axis() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let spec = ScrollbarSpec::default().min_thumb_extent(0.0);
        let state = scrollable_state(200.0, 800.0, 600.0);
        let drawn = rects(&scene(Size::new(200.0, 8.0), state, Axis::Horizontal, spec));
        assert_eq!(
            drawn[1],
            Rect::from_origin_size(Point::new(150.0, 0.0), Size::new(50.0, 8.0))
        );
    }

    #[test]
    fn a_very_long_document_keeps_a_thumb_big_enough_to_grab() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let state = scrollable_state(200.0, 200_000.0, 0.0);
        let drawn = rects(&scene(
            Size::new(8.0, 200.0),
            state,
            Axis::Vertical,
            ScrollbarSpec::default(),
        ));
        assert_eq!(drawn[1].height, DEFAULT_MIN_THUMB_EXTENT);
    }

    #[test]
    fn a_bar_with_no_room_draws_nothing() {
        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
        let _app_context = crate::render_state::app_context_test_scope();
        let state = scrollable_state(200.0, 800.0, 0.0);
        assert!(scene(
            Size::new(0.0, 200.0),
            state,
            Axis::Vertical,
            ScrollbarSpec::default()
        )
        .is_empty());
        assert!(scene(
            Size::new(8.0, 0.0),
            state,
            Axis::Vertical,
            ScrollbarSpec::default()
        )
        .is_empty());
    }

    #[test]
    fn a_specs_thumb_bounds_state_its_minimum_as_a_track_fraction() {
        let spec = ScrollbarSpec::default();
        let bounds = spec.thumb_bounds(240.0);
        assert!(
            (bounds.minimum() - DEFAULT_MIN_THUMB_EXTENT / 240.0).abs() < 1.0e-6,
            "a 24dp floor on a 240dp track is a tenth of it"
        );
        assert_eq!(bounds.maximum(), 1.0, "a thumb may still fill its track");
    }

    #[test]
    fn a_thumb_floor_taller_than_its_track_asks_for_the_whole_track() {
        let bounds = ScrollbarSpec::default().thumb_bounds(10.0);
        assert_eq!(
            bounds.minimum(),
            1.0,
            "a 24dp floor cannot fit in 10dp, so the thumb takes everything"
        );
        assert_eq!(bounds.maximum(), 1.0);
    }

    #[test]
    fn a_track_with_no_extent_leaves_the_thumb_unbounded() {
        for track in [0.0_f32, -40.0, f32::NAN, f32::INFINITY] {
            let bounds = ScrollbarSpec::default().thumb_bounds(track);
            assert_eq!(
                bounds.minimum(),
                0.0,
                "track {track} must not floor a thumb"
            );
            assert_eq!(bounds.maximum(), 1.0);
        }
    }

    #[test]
    fn resolved_corner_radius_is_a_pill_until_a_caller_squares_it() {
        let spec = ScrollbarSpec::default();
        assert_eq!(
            spec.resolved_corner_radius(8.0),
            4.0,
            "half the thickness reads as a pill"
        );
        assert_eq!(
            ScrollbarSpec::default()
                .corner_radius(0.0)
                .resolved_corner_radius(8.0),
            0.0,
            "a caller asking for square corners gets them"
        );
        assert_eq!(
            ScrollbarSpec::default()
                .corner_radius(-5.0)
                .resolved_corner_radius(8.0),
            0.0,
            "a negative radius is not a shape"
        );
    }
}