lurq 0.41.2

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
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
use std::sync::{Arc, Mutex};

use crate::{
  layout::{
    Alignment, StackAlignment,
    scrollbar::{ScrollBarGeometry, ScrollBarPart, ScrollBarStyle, ScrollBarVisibility},
  },
  node::{dimension::Dimension, spacing_value::SpacingValue},
};

const DEFAULT_FLEX_GROW: f32 = 1.0;
const DEFAULT_FLEX_SHRINK: f32 = 0.0;
const SCROLL_END_EPSILON: f32 = 2.0;

#[derive(Clone)]
pub enum LayoutKind {
  Leaf,
  Row {
    spacing: SpacingValue,
    align: Alignment,
    justify: Justify,
    wrap: FlexWrap,
  },
  Column {
    spacing: SpacingValue,
    align: Alignment,
    justify: Justify,
    wrap: FlexWrap,
  },
  Stack {
    align: StackAlignment,
  },
  LogicalModifier,
  ScrollModifier {
    state: ScrollState,
    direction: ScrollDirection,
    culling: bool,
  },
}

#[derive(Clone)]
pub struct ScrollState {
  inner: Arc<Mutex<ScrollStateInner>>,
}

#[derive(Clone, Copy)]
enum PendingScroll {
  Offset(f32),
  Start,
  End,
  PreservePrependAnchor {
    previous_content: f32,
    previous_scroll: f32,
  },
  /// Scroll by the least amount that shows `[start, end]` of the content.
  Reveal {
    start: f32,
    end: f32,
  },
}

/// Scroll position and range when the current layout pass first measured
/// this scroll container.
#[derive(Clone, Copy)]
struct ScrollPassStart {
  scroll_x: f32,
  scroll_y: f32,
  max_scroll_x: f32,
  max_scroll_y: f32,
}

struct ScrollStateInner {
  scroll_x: f32,
  scroll_y: f32,
  pending_scroll_x: Option<PendingScroll>,
  pending_scroll_y: Option<PendingScroll>,
  max_scroll_x: f32,
  max_scroll_y: f32,
  content_width: f32,
  content_height: f32,
  container_width: f32,
  container_height: f32,
  viewport_width: f32,
  viewport_height: f32,
  viewport_abs_x: f32,
  viewport_abs_y: f32,
  /// Viewport top in layout space (the space of `ElementRef` positions), set
  /// when element refs are updated after layout — unlike `viewport_abs_y`,
  /// which is set while painting and lags one frame behind layout.
  layout_viewport_y: f32,
  /// Set by the first measurement of a layout pass; later measurements in the
  /// same pass (a flex parent re-laying out a shrunk or stretched child)
  /// restart from it, so the final measurement decides the offset.
  pass_start: Option<ScrollPassStart>,
  thumb_hovered: bool,
  dragging: bool,
  drag_start_x: f32,
  drag_start_y: f32,
  drag_start_scroll_x: f32,
  drag_start_scroll_y: f32,
  scrollbar_style: ScrollBarStyle,
  scroll_dirty: bool,
  equal_overflow_scroll_probe_enabled: bool,
}

impl ScrollState {
  pub fn new() -> Self {
    Self {
      inner: Arc::new(Mutex::new(ScrollStateInner {
        scroll_x: 0.0,
        scroll_y: 0.0,
        pending_scroll_x: None,
        pending_scroll_y: None,
        max_scroll_x: 0.0,
        max_scroll_y: 0.0,
        content_width: 0.0,
        content_height: 0.0,
        container_width: 0.0,
        container_height: 0.0,
        viewport_width: 0.0,
        viewport_height: 0.0,
        viewport_abs_x: 0.0,
        viewport_abs_y: 0.0,
        layout_viewport_y: 0.0,
        pass_start: None,
        thumb_hovered: false,
        dragging: false,
        drag_start_x: 0.0,
        drag_start_y: 0.0,
        drag_start_scroll_x: 0.0,
        drag_start_scroll_y: 0.0,
        scrollbar_style: ScrollBarStyle::default(),
        scroll_dirty: false,
        equal_overflow_scroll_probe_enabled: false,
      })),
    }
  }

  pub fn scroll_x(&self) -> f32 {
    self.inner.lock().unwrap().scroll_x
  }

  pub fn scroll_y(&self) -> f32 {
    self.inner.lock().unwrap().scroll_y
  }

  pub fn set_scroll(&self, x: f32, y: f32) {
    let mut inner = self.inner.lock().unwrap();
    inner.scroll_x = x.clamp(0.0, inner.max_scroll_x);
    inner.scroll_y = y.clamp(0.0, inner.max_scroll_y);
    inner.scroll_dirty = true;
  }

  /// Queue an absolute scroll offset to resolve after the next layout measurement.
  pub fn set_scroll_pending(&self, x: f32, y: f32) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_x = Some(PendingScroll::Offset(x.max(0.0)));
    inner.pending_scroll_y = Some(PendingScroll::Offset(y.max(0.0)));
    inner.scroll_dirty = true;
  }

  /// Queue the smallest vertical scroll that shows content rows
  /// `start..end`, resolved after the next layout measurement.
  pub(crate) fn reveal_y_pending(&self, start: f32, end: f32) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_y = Some(PendingScroll::Reveal { start, end });
    inner.scroll_dirty = true;
  }

  /// Queue a vertical scroll to the top after the next layout measurement.
  pub fn scroll_to_top_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_y = Some(PendingScroll::Start);
    inner.scroll_dirty = true;
  }

  /// Queue a vertical scroll to the bottom after the next layout measurement.
  pub fn scroll_to_bottom_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_y = Some(PendingScroll::End);
    inner.scroll_dirty = true;
  }

  pub(crate) fn equal_overflow_scroll_probe_enabled(&self) -> bool {
    self.inner.lock().unwrap().equal_overflow_scroll_probe_enabled
  }

  /// Queue a horizontal scroll to the left edge after the next layout measurement.
  pub fn scroll_to_left_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_x = Some(PendingScroll::Start);
    inner.scroll_dirty = true;
  }

  /// Queue a horizontal scroll to the right edge after the next layout measurement.
  pub fn scroll_to_right_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_x = Some(PendingScroll::End);
    inner.scroll_dirty = true;
  }

  /// Queue a bottom scroll only when the current vertical offset is within `threshold` of the bottom.
  pub fn stick_to_bottom_if_near_end(&self, threshold: f32) -> bool {
    let mut inner = self.inner.lock().unwrap();
    if inner.max_scroll_y - inner.scroll_y <= threshold.max(0.0) {
      inner.pending_scroll_y = Some(PendingScroll::End);
      inner.scroll_dirty = true;
      true
    } else {
      false
    }
  }

  /// Queue a right-edge scroll only when the current horizontal offset is within `threshold` of the right edge.
  pub fn stick_to_right_if_near_end(&self, threshold: f32) -> bool {
    let mut inner = self.inner.lock().unwrap();
    if inner.max_scroll_x - inner.scroll_x <= threshold.max(0.0) {
      inner.pending_scroll_x = Some(PendingScroll::End);
      inner.scroll_dirty = true;
      true
    } else {
      false
    }
  }

  /// Preserve the current vertical viewport position when content is prepended before the next layout.
  pub fn preserve_prepend_anchor_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_y = Some(PendingScroll::PreservePrependAnchor {
      previous_content: inner.content_height,
      previous_scroll: inner.scroll_y,
    });
    inner.scroll_dirty = true;
  }

  /// Preserve the current horizontal viewport position when content is prepended before the next layout.
  pub fn preserve_horizontal_prepend_anchor_pending(&self) {
    let mut inner = self.inner.lock().unwrap();
    inner.pending_scroll_x = Some(PendingScroll::PreservePrependAnchor {
      previous_content: inner.content_width,
      previous_scroll: inner.scroll_x,
    });
    inner.scroll_dirty = true;
  }

  pub fn scroll_by(&self, dx: f32, dy: f32) {
    self.scroll_by_with_overflow(dx, dy);
  }

  pub fn scroll_by_with_overflow(&self, dx: f32, dy: f32) -> (f32, f32) {
    let mut inner = self.inner.lock().unwrap();
    if dx != 0.0 {
      inner.pending_scroll_x = None;
    }
    if dy != 0.0 {
      inner.pending_scroll_y = None;
    }

    let old_x = inner.scroll_x;
    let old_y = inner.scroll_y;
    inner.scroll_x = (inner.scroll_x + dx).clamp(0.0, inner.max_scroll_x);
    inner.scroll_y = (inner.scroll_y + dy).clamp(0.0, inner.max_scroll_y);

    let consumed_x = inner.scroll_x - old_x;
    let consumed_y = inner.scroll_y - old_y;
    if consumed_x != 0.0 || consumed_y != 0.0 {
      inner.scroll_dirty = true;
    }

    (dx - consumed_x, dy - consumed_y)
  }

  pub fn content_width(&self) -> f32 {
    self.inner.lock().unwrap().content_width
  }

  pub fn content_height(&self) -> f32 {
    self.inner.lock().unwrap().content_height
  }

  pub fn viewport_width(&self) -> f32 {
    self.inner.lock().unwrap().viewport_width
  }

  pub fn viewport_height(&self) -> f32 {
    self.inner.lock().unwrap().viewport_height
  }

  /// Whether two handles share the same underlying scroll state.
  pub(crate) fn ptr_eq(&self, other: &ScrollState) -> bool {
    Arc::ptr_eq(&self.inner, &other.inner)
  }

  pub fn style(&self) -> ScrollBarStyle {
    self.inner.lock().unwrap().scrollbar_style.clone()
  }

  pub fn set_style(&self, style: ScrollBarStyle) {
    self.inner.lock().unwrap().scrollbar_style = style;
  }

  pub fn is_thumb_hovered(&self) -> bool {
    self.inner.lock().unwrap().thumb_hovered
  }

  pub fn is_dragging(&self) -> bool {
    self.inner.lock().unwrap().dragging
  }

  pub fn set_thumb_hovered(&self, hovered: bool) {
    self.inner.lock().unwrap().thumb_hovered = hovered;
  }

  pub fn begin_drag(&self, mouse_y: f32) {
    self.begin_drag_axis(ScrollAxis::Vertical, 0.0, mouse_y);
  }

  pub fn begin_drag_axis(&self, axis: ScrollAxis, mouse_x: f32, mouse_y: f32) {
    let mut inner = self.inner.lock().unwrap();
    match axis {
      ScrollAxis::Horizontal => inner.pending_scroll_x = None,
      ScrollAxis::Vertical => inner.pending_scroll_y = None,
    }
    inner.dragging = true;
    inner.drag_start_x = mouse_x;
    inner.drag_start_y = mouse_y;
    inner.drag_start_scroll_x = inner.scroll_x;
    inner.drag_start_scroll_y = inner.scroll_y;
  }

  pub fn end_drag(&self) {
    self.inner.lock().unwrap().dragging = false;
  }

  pub fn drag_to(&self, mouse_y: f32, style: &crate::layout::scrollbar::ScrollBarStyle) {
    self.drag_to_axis(ScrollAxis::Vertical, 0.0, mouse_y, style);
  }

  pub fn drag_to_axis(
    &self,
    axis: ScrollAxis,
    mouse_x: f32,
    mouse_y: f32,
    style: &crate::layout::scrollbar::ScrollBarStyle,
  ) {
    let mut inner = self.inner.lock().unwrap();
    if !inner.dragging {
      return;
    }

    match axis {
      ScrollAxis::Horizontal => {
        let track_width = inner.viewport_width - style.resolved_end_inset() * 2.0;
        if track_width <= 0.0 {
          return;
        }
        let ratio = inner.viewport_width / inner.content_width.max(1.0);
        let thumb_width = (track_width * ratio).max(style.min_thumb_length).min(track_width);
        let scrollable_track = track_width - thumb_width;
        if scrollable_track <= 0.0 {
          return;
        }

        let delta_px = mouse_x - inner.drag_start_x;
        let scroll_delta = delta_px / scrollable_track * inner.max_scroll_x;
        inner.scroll_x = (inner.drag_start_scroll_x + scroll_delta).clamp(0.0, inner.max_scroll_x);
      }
      ScrollAxis::Vertical => {
        let track_height = inner.viewport_height - style.resolved_end_inset() * 2.0;
        if track_height <= 0.0 {
          return;
        }
        let ratio = inner.viewport_height / inner.content_height.max(1.0);
        let thumb_height = (track_height * ratio).max(style.min_thumb_length).min(track_height);
        let scrollable_track = track_height - thumb_height;
        if scrollable_track <= 0.0 {
          return;
        }

        let delta_px = mouse_y - inner.drag_start_y;
        let scroll_delta = delta_px / scrollable_track * inner.max_scroll_y;
        inner.scroll_y = (inner.drag_start_scroll_y + scroll_delta).clamp(0.0, inner.max_scroll_y);
      }
    }
    inner.scroll_dirty = true;
  }

  pub fn thumb_rect(&self, style: &crate::layout::scrollbar::ScrollBarStyle) -> (f32, f32, f32, f32) {
    let inner = self.inner.lock().unwrap();
    let track_x = inner.viewport_abs_x + inner.viewport_width - style.width - style.resolved_edge_inset();
    let track_y = inner.viewport_abs_y + style.resolved_end_inset();
    let track_height = inner.viewport_height - style.resolved_end_inset() * 2.0;

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

    (track_x, thumb_y, style.width, thumb_height)
  }

  pub fn thumb_rect_for_axis(
    &self,
    axis: ScrollAxis,
    style: &crate::layout::scrollbar::ScrollBarStyle,
  ) -> Option<(f32, f32, f32, f32)> {
    let geo = self.scrollbar_geometry_for_axis(axis, style)?;
    Some((geo.thumb_x, geo.thumb_y, geo.thumb_width, geo.thumb_height))
  }

  pub fn scrollbar_geometry_for_axis(
    &self,
    axis: ScrollAxis,
    style: &crate::layout::scrollbar::ScrollBarStyle,
  ) -> Option<ScrollBarGeometry> {
    let inner = self.inner.lock().unwrap();
    match (style.visible, axis) {
      (ScrollBarVisibility::Never, _) => return None,
      (ScrollBarVisibility::Auto, ScrollAxis::Horizontal) if inner.content_width <= inner.viewport_width => {
        return None;
      }
      (ScrollBarVisibility::Auto, ScrollAxis::Vertical) if inner.content_height <= inner.viewport_height => {
        return None;
      }
      _ => {}
    }

    match axis {
      ScrollAxis::Horizontal => {
        let track_x = inner.viewport_abs_x + style.resolved_end_inset();
        let track_y = inner.viewport_abs_y + inner.container_height - style.width - style.resolved_edge_inset();
        let track_width = inner.viewport_width - style.resolved_end_inset() * 2.0;
        let track_height = style.width;
        if track_width <= 0.0 || track_height <= 0.0 {
          return None;
        }

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

        Some(ScrollBarGeometry {
          track_x,
          track_y,
          track_width,
          track_height,
          thumb_x,
          thumb_y: track_y,
          thumb_width,
          thumb_height: track_height,
        })
      }
      ScrollAxis::Vertical => {
        let track_x = inner.viewport_abs_x + inner.container_width - style.width - style.resolved_edge_inset();
        let track_y = inner.viewport_abs_y + style.resolved_end_inset();
        let track_width = style.width;
        let track_height = inner.viewport_height - style.resolved_end_inset() * 2.0;
        if track_width <= 0.0 || track_height <= 0.0 {
          return None;
        }

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

        Some(ScrollBarGeometry {
          track_x,
          track_y,
          track_width,
          track_height,
          thumb_x: track_x,
          thumb_y,
          thumb_width: track_width,
          thumb_height,
        })
      }
    }
  }

  /// The part of `axis`'s scrollbar that takes the pointer at `(x, y)` in
  /// window coordinates (see [`ScrollBarGeometry::hit_part_at`]), or `None`
  /// where the pointer belongs to the content. A bar shown while the content
  /// fits along its axis (`ScrollBarVisibility::Always`) cannot scroll
  /// anything, so it takes no pointer.
  pub(crate) fn scrollbar_part_at(
    &self,
    axis: ScrollAxis,
    style: &ScrollBarStyle,
    x: f32,
    y: f32,
  ) -> Option<ScrollBarPart> {
    let overflows = match axis {
      ScrollAxis::Horizontal => self.content_width() > self.viewport_width(),
      ScrollAxis::Vertical => self.content_height() > self.viewport_height(),
    };
    if !overflows {
      return None;
    }
    self
      .scrollbar_geometry_for_axis(axis, style)?
      .hit_part_at(axis == ScrollAxis::Horizontal, style, x, y)
  }

  /// Scrolls one viewport along `axis`, forward or back, clamped to the
  /// content: what a press on the scrollbar track does.
  pub(crate) fn page_axis(&self, axis: ScrollAxis, forward: bool) {
    let page = match axis {
      ScrollAxis::Horizontal => self.viewport_width(),
      ScrollAxis::Vertical => self.viewport_height(),
    };
    let delta = if forward { page } else { -page };
    match axis {
      ScrollAxis::Horizontal => self.scroll_by(delta, 0.0),
      ScrollAxis::Vertical => self.scroll_by(0.0, delta),
    }
  }

  pub(crate) fn take_scroll_dirty(&self) -> bool {
    let mut inner = self.inner.lock().unwrap();
    let dirty = inner.scroll_dirty;
    inner.scroll_dirty = false;
    dirty
  }

  pub(crate) fn has_scroll_dirty(&self) -> bool {
    self.inner.lock().unwrap().scroll_dirty
  }

  /// Record one measurement of the scroll container. A layout pass can
  /// measure a container more than once (a flex parent re-lays out a child it
  /// shrank or stretched); every measurement resolves the pending scroll and
  /// the stick-to-end behaviour from the state the pass started with, so only
  /// the final measurement counts. [`Self::finish_layout_pass`] then drops the
  /// resolved pending scroll.
  pub(crate) fn update_layout_with_container(
    &self,
    content_w: f32,
    content_h: f32,
    viewport_w: f32,
    viewport_h: f32,
    container_w: f32,
    container_h: f32,
  ) {
    let mut inner = self.inner.lock().unwrap();
    let start = inner.pass_start.unwrap_or(ScrollPassStart {
      scroll_x: inner.scroll_x,
      scroll_y: inner.scroll_y,
      max_scroll_x: inner.max_scroll_x,
      max_scroll_y: inner.max_scroll_y,
    });
    inner.pass_start = Some(start);
    let was_at_right = start.max_scroll_x > 0.0 && start.max_scroll_x - start.scroll_x <= SCROLL_END_EPSILON;
    let was_at_bottom = start.max_scroll_y > 0.0 && start.max_scroll_y - start.scroll_y <= SCROLL_END_EPSILON;

    inner.content_width = content_w;
    inner.content_height = content_h;
    inner.container_width = container_w;
    inner.container_height = container_h;
    inner.viewport_width = viewport_w.max(0.0);
    inner.viewport_height = viewport_h.max(0.0);
    inner.max_scroll_x = (content_w - inner.viewport_width).max(0.0);
    inner.max_scroll_y = (content_h - inner.viewport_height).max(0.0);
    inner.scroll_x = match inner.pending_scroll_x {
      Some(pending) => resolve_pending_scroll(
        pending,
        inner.content_width,
        inner.max_scroll_x,
        start.scroll_x,
        inner.viewport_width,
      ),
      None if was_at_right => inner.max_scroll_x,
      None => start.scroll_x,
    };
    inner.scroll_y = match inner.pending_scroll_y {
      Some(pending) => resolve_pending_scroll(
        pending,
        inner.content_height,
        inner.max_scroll_y,
        start.scroll_y,
        inner.viewport_height,
      ),
      None if was_at_bottom => inner.max_scroll_y,
      None => start.scroll_y,
    };
    inner.scroll_x = inner.scroll_x.clamp(0.0, inner.max_scroll_x);
    inner.scroll_y = inner.scroll_y.clamp(0.0, inner.max_scroll_y);
  }

  /// End the current layout pass: the last measurement's offset stands and
  /// the pending scroll it resolved is consumed. No-op when the container was
  /// not measured in this pass (its pending scroll keeps waiting).
  pub(crate) fn finish_layout_pass(&self) {
    let mut inner = self.inner.lock().unwrap();
    if inner.pass_start.take().is_some() {
      inner.pending_scroll_x = None;
      inner.pending_scroll_y = None;
    }
  }

  pub(crate) fn set_layout_viewport_y(&self, y: f32) {
    self.inner.lock().unwrap().layout_viewport_y = y;
  }

  /// Viewport top in layout space; see `ScrollStateInner::layout_viewport_y`.
  pub(crate) fn layout_viewport_y(&self) -> f32 {
    self.inner.lock().unwrap().layout_viewport_y
  }

  pub(crate) fn set_viewport_position(&self, x: f32, y: f32) {
    let mut inner = self.inner.lock().unwrap();
    inner.viewport_abs_x = x;
    inner.viewport_abs_y = y;
  }
}

fn resolve_pending_scroll(
  pending: PendingScroll,
  content_size: f32,
  max_scroll: f32,
  current: f32,
  viewport: f32,
) -> f32 {
  match pending {
    PendingScroll::Offset(offset) => offset,
    PendingScroll::Start => 0.0,
    PendingScroll::End => max_scroll,
    PendingScroll::PreservePrependAnchor {
      previous_content,
      previous_scroll,
    } => previous_scroll + (content_size - previous_content),
    PendingScroll::Reveal { start, end } => {
      if start < current || end - start > viewport {
        start
      } else if end > current + viewport {
        end - viewport
      } else {
        current
      }
    }
  }
}

#[derive(Clone, Copy, PartialEq)]
pub struct FlexParams {
  pub grow: f32,
  pub shrink: f32,
  pub basis: Option<f32>,
}

impl FlexParams {
  pub fn grow(factor: f32) -> Self {
    Self {
      grow: factor,
      shrink: 0.0,
      basis: None,
    }
  }
}

impl Default for FlexParams {
  fn default() -> Self {
    Self {
      grow: DEFAULT_FLEX_GROW,
      shrink: DEFAULT_FLEX_SHRINK,
      basis: None,
    }
  }
}

/// How far a shrinking child of a single-line `Row`/`Column` gives way when
/// the line overflows. Set with `.shrink_limit(...)`; it applies only to a
/// child with a `flex_shrink` factor.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ShrinkLimit {
  /// Shrink down to the child's `min_width`/`min_height` (0 without one).
  #[default]
  MinSize,
  /// Shrink down to the child's content minimum, and never below an explicit
  /// `min_width`/`min_height`. The content minimum of a `Row` in a row (or
  /// a `Column` in a column) is its padding and spacing, the natural size of
  /// every child that does not shrink and the limit of every child that does.
  /// Anything else (text, rect, stack, scroll container, wrapping line, line
  /// on the other axis) keeps its natural size, like a flex item with CSS
  /// `min-width: auto`.
  Content,
  /// Keep the natural size or drop out of the line: a dropped child is laid
  /// out at zero size, takes no spacing, and is neither drawn nor hit.
  /// Nothing inside it is a Tab stop or takes focus, focus inside it is
  /// blurred, keys never activate it, and its element refs report a
  /// zero-size rect where the next child starts. It stays mounted and keeps
  /// its state.
  Drop,
}

/// When and how far a shrinking flex child gives way: a node's
/// `.shrink_order(...)`, `.shrink_limit(...)` and `.shrink_drop_below(...)`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct ShrinkRule {
  pub(crate) order: i32,
  pub(crate) limit: ShrinkLimit,
  /// Shrink no further than this main size, then drop (see
  /// `.shrink_drop_below`).
  pub(crate) drop_below: Option<f32>,
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum FlexWrap {
  #[default]
  NoWrap,
  Wrap,
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum Justify {
  #[default]
  Start,
  End,
  Center,
  SpaceBetween,
  SpaceAround,
  SpaceEvenly,
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum Overflow {
  Visible,
  #[default]
  Hidden,
}

#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum ScrollDirection {
  Horizontal,
  #[default]
  Vertical,
  Both,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ScrollAxis {
  Horizontal,
  Vertical,
}

#[derive(Clone, Copy, Default, PartialEq)]
pub struct FrameConstraints {
  pub width: Option<Dimension>,
  pub height: Option<Dimension>,
  pub min_width: Option<Dimension>,
  pub max_width: Option<Dimension>,
  pub min_height: Option<Dimension>,
  pub max_height: Option<Dimension>,
}

#[derive(Clone, Copy, Default, PartialEq)]
pub enum Position {
  #[default]
  Static,
  Absolute {
    x: f32,
    y: f32,
    width: Option<Dimension>,
    height: Option<Dimension>,
  },
}

impl FrameConstraints {
  pub fn with_width(mut self, value: Option<Dimension>) -> Self {
    self.width = value;
    self
  }
  pub fn with_height(mut self, value: Option<Dimension>) -> Self {
    self.height = value;
    self
  }
  pub fn with_min_width(mut self, value: Option<Dimension>) -> Self {
    self.min_width = value;
    self
  }
  pub fn with_max_width(mut self, value: Option<Dimension>) -> Self {
    self.max_width = value;
    self
  }
  pub fn with_min_height(mut self, value: Option<Dimension>) -> Self {
    self.min_height = value;
    self
  }
  pub fn with_max_height(mut self, value: Option<Dimension>) -> Self {
    self.max_height = value;
    self
  }
}