lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
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
use crate::{
  animation::Easing,
  app::theme::{PaletteColor, ThemePalette},
  node::{BackgroundColor, color::Color},
  time::{Duration, Instant},
};

const DEFAULT_SCROLLBAR_WIDTH: f32 = 8.0;
const DEFAULT_SCROLLBAR_MIN_THUMB_LENGTH: f32 = 24.0;
const DEFAULT_SCROLLBAR_TRACK_COLOR: Color = Color::new(0, 0, 0, 0);
/// The thumb of lurq 0.62: black at a third of its opacity.
const LEGACY_SCROLLBAR_THUMB_COLOR: Color = Color::new(0, 0, 0, 80);
const DEFAULT_SCROLLBAR_THUMB_RADIUS: f32 = 4.0;
const DEFAULT_SCROLLBAR_TRACK_RADIUS: f32 = 4.0;
const DEFAULT_SCROLLBAR_PADDING: f32 = 2.0;
const DEFAULT_SCROLLBAR_FADE_DELAY: Duration = Duration::from_millis(600);
const DEFAULT_SCROLLBAR_FADE_DURATION: Duration = Duration::from_millis(200);
/// How long a fading bar keeps one opacity: a frame of a 60 Hz display.
const FADE_FRAME: Duration = Duration::from_millis(16);

const THIN_SCROLLBAR_WIDTH: f32 = 4.0;
const THIN_SCROLLBAR_MIN_THUMB_LENGTH: f32 = 16.0;
const THIN_SCROLLBAR_THUMB_RADIUS: f32 = 2.0;
const THIN_SCROLLBAR_TRACK_RADIUS: f32 = 2.0;
const THIN_SCROLLBAR_PADDING: f32 = 1.0;

const WIDE_SCROLLBAR_WIDTH: f32 = 12.0;
const WIDE_SCROLLBAR_MIN_THUMB_LENGTH: f32 = 32.0;
const WIDE_SCROLLBAR_THUMB_RADIUS: f32 = 6.0;
const WIDE_SCROLLBAR_TRACK_RADIUS: f32 = 6.0;
const WIDE_SCROLLBAR_PADDING: f32 = 2.0;

#[derive(Clone, Debug, PartialEq, lurq_macros::Accessors)]
pub struct ScrollBarStyle {
  pub width: f32,
  pub min_thumb_length: f32,
  pub track_color: Color,
  /// The color of the thumb. A style that never set one has [`Self::THEME_THUMB`], which is drawn in the
  /// theme's `Ring`: grey on a light theme and on a dark one, as the design system's scrollbar is.
  pub thumb_color: Color,
  /// The color of the thumb of the bar under the pointer, a color or a role of the palette. `None` keeps
  /// [`Self::thumb_color`].
  pub thumb_hovered_color: Option<BackgroundColor>,
  /// The color of the thumb while it is dragged, a color or a role of the palette. `None` keeps the color it has
  /// under the pointer.
  pub thumb_pressed_color: Option<BackgroundColor>,
  /// The gap between the lane of the bar and its thumb, on every side: the thumb is [`Self::width`] less twice
  /// this across, and moves along the track less twice this. The design system's bar is a lane of 8 with a
  /// thumb of 6 in it.
  pub thumb_inset: f32,
  pub thumb_radius: f32,
  pub track_radius: f32,
  /// The gap around the bar: from the edge it runs along and from the two
  /// edges its track ends at. [`Self::edge_inset`] and [`Self::end_inset`]
  /// override each side.
  pub padding: f32,
  /// The gap between the bar and the edge it runs along (the right edge for
  /// a vertical bar, the bottom edge for a horizontal one), measured from the
  /// scroll container's outer bounds. `None` uses [`Self::padding`].
  pub edge_inset: Option<f32>,
  /// The gap between each end of the track and the container's outer edge
  /// (top and bottom for a vertical bar), e.g. to clear rounded corners: the
  /// track is the container's length minus twice this. `None` uses
  /// [`Self::padding`].
  pub end_inset: Option<f32>,
  pub visible: ScrollBarVisibility,
  pub placement: ScrollBarPlacement,
  /// How long a [`ScrollBarVisibility::OnHover`] bar stays after the pointer
  /// left its container, the content stopped scrolling and its thumb was
  /// let go, before it starts to fade.
  pub fade_delay: Duration,
  /// How long a [`ScrollBarVisibility::OnHover`] bar takes to fade out. Zero
  /// hides it at once when [`Self::fade_delay`] ends.
  pub fade_duration: Duration,
  /// The curve a [`ScrollBarVisibility::OnHover`] bar fades out along.
  pub fade_easing: Easing,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ScrollBarVisibility {
  /// The bar shows while the content overflows.
  #[default]
  Auto,
  /// The bar shows whether or not the content overflows.
  Always,
  /// No bar is painted and none takes the pointer; the wheel, the keyboard
  /// and scroll requests still scroll the content.
  Never,
  /// The bar of overflowing content shows while the pointer is over its
  /// scroll container, while the content scrolls and while its thumb is
  /// dragged. Then it stays for [`ScrollBarStyle::fade_delay`] and fades out
  /// over [`ScrollBarStyle::fade_duration`]. Hidden, it is not painted and
  /// takes no pointer, like [`Self::Never`]; shown, it is an [`Self::Auto`]
  /// bar.
  OnHover,
  /// A phone's scroll indicator: an [`Self::OnHover`] bar that the pointer does not show, only content that
  /// scrolls and a thumb that is dragged. A finger that rests on a list shows nothing.
  WhileScrolling,
}

impl ScrollBarVisibility {
  /// Whether the bar comes and goes by itself: shown, it is an [`Self::Auto`] bar.
  pub(crate) fn fades(self) -> bool {
    matches!(self, Self::OnHover | Self::WhileScrolling)
  }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ScrollBarPlacement {
  #[default]
  Overlay,
  Reserved,
}

impl Default for ScrollBarStyle {
  /// A desktop's bar; on iOS, [`Self::indicator`], the bar a list of the platform has.
  fn default() -> Self {
    if cfg!(target_os = "ios") {
      return Self::indicator();
    }
    Self::bar()
  }
}

impl ScrollBarStyle {
  /// The thumb color of a style that set none: a mark for "the theme's `Ring`", and the color drawn where a
  /// palette has no ring. A mark of its own, a hair from 0.62's black: a style that names that black
  /// ([`Self::LEGACY_THUMB`]) keeps it.
  pub const THEME_THUMB: Color = Color::new(0, 0, 0, 81);

  /// The thumb of lurq 0.62, black at a third of its opacity: the one [`crate::app::theme::ThemeDefinition::legacy`]
  /// draws.
  pub const LEGACY_THUMB: Color = LEGACY_SCROLLBAR_THUMB_COLOR;

  /// The color the thumb is drawn in under `palette`: its own, or the palette's `Ring` where it set none.
  pub fn thumb_in(&self, palette: &ThemePalette) -> Color {
    if self.thumb_color == Self::THEME_THUMB {
      palette.try_get(&PaletteColor::Ring).unwrap_or(self.thumb_color)
    } else {
      self.thumb_color
    }
  }

  /// The color the thumb is drawn in under `palette`, under the pointer (`hovered`) or dragged (`pressed`):
  /// [`Self::thumb_pressed_color`], else [`Self::thumb_hovered_color`], else [`Self::thumb_in`].
  pub fn thumb_when(&self, palette: &ThemePalette, hovered: bool, pressed: bool) -> Color {
    let color = |color: &BackgroundColor| match color {
      BackgroundColor::Color(color) => Some(*color),
      BackgroundColor::Palette(role) => palette.try_get(role),
      BackgroundColor::CurrentForeground => palette.try_get(&PaletteColor::Foreground),
    };
    let state = if pressed {
      self.thumb_pressed_color.as_ref().or(self.thumb_hovered_color.as_ref())
    } else if hovered {
      self.thumb_hovered_color.as_ref()
    } else {
      None
    };
    state.and_then(color).unwrap_or_else(|| self.thumb_in(palette))
  }

  /// A phone's scroll indicator: a thin rounded bar over the content, clear of the corners, that shows while
  /// the content scrolls and fades out after. Grey, to show over light content and dark.
  pub fn indicator() -> Self {
    Self {
      width: 3.0,
      min_thumb_length: 36.0,
      thumb_color: Color::new(128, 128, 128, 170),
      thumb_radius: 1.5,
      track_radius: 1.5,
      padding: 3.0,
      end_inset: Some(6.0),
      visible: ScrollBarVisibility::WhileScrolling,
      fade_delay: Duration::from_millis(350),
      fade_duration: Duration::from_millis(250),
      ..Self::bar()
    }
  }

  fn bar() -> Self {
    Self {
      width: DEFAULT_SCROLLBAR_WIDTH,
      min_thumb_length: DEFAULT_SCROLLBAR_MIN_THUMB_LENGTH,
      track_color: DEFAULT_SCROLLBAR_TRACK_COLOR,
      thumb_color: Self::THEME_THUMB,
      thumb_hovered_color: None,
      thumb_pressed_color: None,
      thumb_inset: 0.0,
      thumb_radius: DEFAULT_SCROLLBAR_THUMB_RADIUS,
      track_radius: DEFAULT_SCROLLBAR_TRACK_RADIUS,
      padding: DEFAULT_SCROLLBAR_PADDING,
      edge_inset: None,
      end_inset: None,
      visible: ScrollBarVisibility::Auto,
      placement: ScrollBarPlacement::Overlay,
      fade_delay: DEFAULT_SCROLLBAR_FADE_DELAY,
      fade_duration: DEFAULT_SCROLLBAR_FADE_DURATION,
      fade_easing: Easing::EASE_OUT,
    }
  }
}

impl ScrollBarStyle {
  pub fn thin() -> Self {
    Self {
      width: THIN_SCROLLBAR_WIDTH,
      min_thumb_length: THIN_SCROLLBAR_MIN_THUMB_LENGTH,
      thumb_radius: THIN_SCROLLBAR_THUMB_RADIUS,
      track_radius: THIN_SCROLLBAR_TRACK_RADIUS,
      padding: THIN_SCROLLBAR_PADDING,
      ..Self::default()
    }
  }

  pub fn wide() -> Self {
    Self {
      width: WIDE_SCROLLBAR_WIDTH,
      min_thumb_length: WIDE_SCROLLBAR_MIN_THUMB_LENGTH,
      thumb_radius: WIDE_SCROLLBAR_THUMB_RADIUS,
      track_radius: WIDE_SCROLLBAR_TRACK_RADIUS,
      padding: WIDE_SCROLLBAR_PADDING,
      ..Self::default()
    }
  }

  /// Sets [`Self::edge_inset`] and [`Self::end_inset`].
  pub fn insets(mut self, edge: f32, end: f32) -> Self {
    self.edge_inset = Some(edge);
    self.end_inset = Some(end);
    self
  }

  /// The resolved gap between the bar and the edge it runs along.
  pub fn resolved_edge_inset(&self) -> f32 {
    self.edge_inset.unwrap_or(self.padding)
  }

  /// The resolved gap between each track end and the container's edge.
  pub fn resolved_end_inset(&self) -> f32 {
    self.end_inset.unwrap_or(self.padding)
  }

  pub fn hidden() -> Self {
    Self {
      visible: ScrollBarVisibility::Never,
      ..Self::default()
    }
  }

  /// The default bar, shown only while the pointer is over its container,
  /// the content scrolls or its thumb is dragged
  /// ([`ScrollBarVisibility::OnHover`]).
  pub fn on_hover() -> Self {
    Self {
      visible: ScrollBarVisibility::OnHover,
      ..Self::default()
    }
  }

  /// Sets [`Self::fade_delay`] and [`Self::fade_duration`].
  pub fn fade(mut self, delay: Duration, duration: Duration) -> Self {
    self.fade_delay = delay;
    self.fade_duration = duration;
    self
  }
}

/// What a [`ScrollBarVisibility::OnHover`] bar shows in one frame.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ScrollBarRevealFrame {
  /// 0 for a hidden bar, 1 for a shown one, between them while it fades.
  pub(crate) opacity: f32,
  /// When the bar next looks different with nothing else happening: the end
  /// of its delay or the next frame of its fade.
  pub(crate) next_change: Option<Instant>,
}

impl ScrollBarRevealFrame {
  const HIDDEN: Self = Self {
    opacity: 0.0,
    next_change: None,
  };
  const SHOWN: Self = Self {
    opacity: 1.0,
    next_change: None,
  };
}

/// Whether a [`ScrollBarVisibility::OnHover`] bar is shown, and since when
/// nothing holds it there.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct ScrollBarReveal {
  shown: bool,
  idle_since: Option<Instant>,
}

impl ScrollBarReveal {
  /// Whether the bar is painted, at any opacity, and so takes the pointer.
  pub(crate) fn is_shown(&self) -> bool {
    self.shown
  }

  /// Advances to the frame at `now`. `held` says the pointer is over the
  /// container or drags the thumb, `scrolled` that the content moved since
  /// the frame before; either shows the bar. A bar nothing holds stays for
  /// the style's delay from the first frame that found it so, then fades.
  /// Content that fits has no bar to show.
  pub(crate) fn advance(
    &mut self,
    style: &ScrollBarStyle,
    now: Instant,
    held: bool,
    scrolled: bool,
    overflows: bool,
  ) -> ScrollBarRevealFrame {
    if !overflows {
      *self = Self::default();
      return ScrollBarRevealFrame::HIDDEN;
    }
    if held {
      self.shown = true;
      self.idle_since = None;
      return ScrollBarRevealFrame::SHOWN;
    }
    if scrolled {
      self.shown = true;
      self.idle_since = Some(now);
    }
    if !self.shown {
      return ScrollBarRevealFrame::HIDDEN;
    }
    let idle_since = *self.idle_since.get_or_insert(now);
    let idle = now.saturating_duration_since(idle_since);
    if idle < style.fade_delay {
      return ScrollBarRevealFrame {
        opacity: 1.0,
        next_change: Some(idle_since + style.fade_delay),
      };
    }
    let fading = idle - style.fade_delay;
    if fading >= style.fade_duration {
      *self = Self::default();
      return ScrollBarRevealFrame::HIDDEN;
    }
    let progress = fading.as_secs_f64() / style.fade_duration.as_secs_f64();
    let faded = style.fade_easing.evaluate(progress).clamp(0.0, 1.0);
    ScrollBarRevealFrame {
      opacity: (1.0 - faded) as f32,
      next_change: Some(now + FADE_FRAME),
    }
  }
}

/// How far the area that takes the pointer for a scrollbar reaches beyond
/// what is painted, so a press just off a thin bar still lands on it.
const SCROLLBAR_HIT_SLOP: f32 = 4.0;

pub struct ScrollBarGeometry {
  pub track_x: f32,
  pub track_y: f32,
  pub track_width: f32,
  pub track_height: f32,
  pub thumb_x: f32,
  pub thumb_y: f32,
  pub thumb_width: f32,
  pub thumb_height: f32,
}

/// The part of a scrollbar under the pointer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ScrollBarPart {
  Thumb,
  /// The track between its start and the thumb.
  TrackBefore,
  /// The track between the thumb and its end.
  TrackAfter,
}

impl ScrollBarGeometry {
  /// The part of the scrollbar at `(x, y)` for a horizontal or a vertical bar,
  /// or `None` where the pointer belongs to the content.
  ///
  /// Where the bar visibly owns its lane, a `Reserved` gutter or an overlay
  /// bar with a painted track, the whole lane takes the pointer: the track
  /// along the axis and, across it, [`SCROLLBAR_HIT_SLOP`] or the edge inset
  /// beyond the track on both sides, whichever is larger, which covers the
  /// gutter and reaches the container edge. A plain overlay bar (transparent
  /// track) shows only its thumb over the content, so only the thumb takes the
  /// pointer, with [`SCROLLBAR_HIT_SLOP`] around it on every side; the rest of
  /// the lane is content.
  pub(crate) fn hit_part_at(&self, horizontal: bool, style: &ScrollBarStyle, x: f32, y: f32) -> Option<ScrollBarPart> {
    let (along, across) = if horizontal { (x, y) } else { (y, x) };
    let (track_start, track_length, cross_start, thickness) = if horizontal {
      (self.track_x, self.track_width, self.track_y, self.track_height)
    } else {
      (self.track_y, self.track_height, self.track_x, self.track_width)
    };
    let (thumb_start, thumb_length) = if horizontal {
      (self.thumb_x, self.thumb_width)
    } else {
      (self.thumb_y, self.thumb_height)
    };

    let owns_lane = style.placement == ScrollBarPlacement::Reserved || style.track_color.a() > 0;
    if !owns_lane {
      let on_thumb = across >= cross_start - SCROLLBAR_HIT_SLOP
        && across <= cross_start + thickness + SCROLLBAR_HIT_SLOP
        && along >= thumb_start - SCROLLBAR_HIT_SLOP
        && along <= thumb_start + thumb_length + SCROLLBAR_HIT_SLOP;
      return on_thumb.then_some(ScrollBarPart::Thumb);
    }

    let reach = SCROLLBAR_HIT_SLOP.max(style.resolved_edge_inset());
    let in_lane = across >= cross_start - reach
      && across <= cross_start + thickness + reach
      && along >= track_start
      && along <= track_start + track_length;
    if !in_lane {
      return None;
    }
    Some(if along < thumb_start {
      ScrollBarPart::TrackBefore
    } else if along > thumb_start + thumb_length {
      ScrollBarPart::TrackAfter
    } else {
      ScrollBarPart::Thumb
    })
  }
}

pub fn compute_vertical_scrollbar(
  style: &ScrollBarStyle,
  viewport_x: f32,
  viewport_y: f32,
  viewport_width: f32,
  viewport_height: f32,
  content_height: f32,
  scroll_y: f32,
) -> Option<ScrollBarGeometry> {
  match style.visible {
    ScrollBarVisibility::Never => return None,
    ScrollBarVisibility::Auto | ScrollBarVisibility::OnHover | ScrollBarVisibility::WhileScrolling
      if content_height <= viewport_height =>
    {
      return None;
    }
    _ => {}
  }

  let track_x = viewport_x + viewport_width - style.width - style.resolved_edge_inset();
  let track_y = viewport_y + style.resolved_end_inset();
  let track_width = style.width;
  let track_height = viewport_height - style.resolved_end_inset() * 2.0;

  let inset = style.thumb_inset;
  let travel = track_height - inset * 2.0;
  let ratio = viewport_height / content_height.max(1.0);
  let thumb_height = (travel * ratio).max(style.min_thumb_length).min(travel);
  let max_scroll = (content_height - viewport_height).max(0.0);
  let scroll_ratio = if max_scroll > 0.0 { scroll_y / max_scroll } else { 0.0 };
  let thumb_y = track_y + inset + (travel - thumb_height) * scroll_ratio;

  Some(ScrollBarGeometry {
    track_x,
    track_y,
    track_width,
    track_height,
    thumb_x: track_x + inset,
    thumb_y,
    thumb_width: track_width - inset * 2.0,
    thumb_height,
  })
}

pub fn compute_horizontal_scrollbar(
  style: &ScrollBarStyle,
  viewport_x: f32,
  viewport_y: f32,
  viewport_width: f32,
  viewport_height: f32,
  content_width: f32,
  scroll_x: f32,
) -> Option<ScrollBarGeometry> {
  match style.visible {
    ScrollBarVisibility::Never => return None,
    ScrollBarVisibility::Auto | ScrollBarVisibility::OnHover | ScrollBarVisibility::WhileScrolling
      if content_width <= viewport_width =>
    {
      return None;
    }
    _ => {}
  }

  let track_x = viewport_x + style.resolved_end_inset();
  let track_y = viewport_y + viewport_height - style.width - style.resolved_edge_inset();
  let track_width = viewport_width - style.resolved_end_inset() * 2.0;
  let track_height = style.width;

  let inset = style.thumb_inset;
  let travel = track_width - inset * 2.0;
  let ratio = viewport_width / content_width.max(1.0);
  let thumb_width = (travel * ratio).max(style.min_thumb_length).min(travel);
  let max_scroll = (content_width - viewport_width).max(0.0);
  let scroll_ratio = if max_scroll > 0.0 { scroll_x / max_scroll } else { 0.0 };
  let thumb_x = track_x + inset + (travel - thumb_width) * scroll_ratio;

  Some(ScrollBarGeometry {
    track_x,
    track_y,
    track_width,
    track_height,
    thumb_x,
    thumb_y: track_y + inset,
    thumb_width,
    thumb_height: track_height - inset * 2.0,
  })
}