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
//! Where views are: measuring a node for `ref.measure()`, scrolling one
//! into view (`ref.reveal()`), and the areas descendants cover
//! outside their parents' bounds, which platforms hit-test
//! ([`MountItem::Overflow`]).
use super::{Core, Key, Kind, NO_KEY};
use crate::{
EventPayload, JsonValue, PayloadValue,
geometry::{Bounds, Point, Points, point, rect},
mount::MountItem,
ops::{Command, NodeId, RevealOptions, ScrollAlignment},
props::EdgeInsets,
text::TextEngine,
};
/// The offset that places the range `start..start + len` of a scroll
/// view's content in its `viewport` as `align` says, from `offset`.
fn aligned(start: f32, len: f32, viewport: f32, offset: f32, align: ScrollAlignment) -> f32 {
match align {
ScrollAlignment::Start => start,
ScrollAlignment::Center => start + (len - viewport) / 2.0,
ScrollAlignment::End => start + len - viewport,
// CSSOM View's "determine the scroll-into-view position".
ScrollAlignment::Nearest => {
let before = start < offset;
let after = start + len > offset + viewport;
if before == after {
offset
} else if before == (len <= viewport) {
start
} else {
start + len - viewport
}
}
}
}
/// Where a view is, from the last layout (`ref.measure()`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Measurement {
/// The frame relative to the parent view, as `onLayout` reports it.
pub frame: Bounds<Points>,
/// The top left corner in the coordinates of the root view the view is
/// in (the surface, or the modal it is shown in): those of touches'
/// `pageX` / `pageY` (`pagePosition`). Scroll offsets and zoom scales of the scroll
/// views and lists around it are applied; transforms are not.
pub page: Point<Points>,
}
impl Measurement {
/// The JS object `ref.measure()` resolves to.
pub(crate) fn to_json(self) -> JsonValue {
serde_json::json!({
"bounds": {
"origin": { "x": self.frame.origin.x.0, "y": self.frame.origin.y.0 },
"size": { "width": self.frame.size.width.0, "height": self.frame.size.height.0 },
},
"pagePosition": { "x": self.page.x.0, "y": self.page.y.0 },
})
}
}
/// Where a list's viewport starts among its rows
/// (`ref.measureScrollPosition()`), as GPUI's `ListOffset`: the row there
/// and how far into it, along the list's axis from the side item 0 is on
/// (the top, the bottom of an inverted list). Scrolling the list to row
/// `index` with a view offset of `-offset` puts the viewport's start back
/// there, wherever the list's sizes still are estimates.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
#[non_exhaustive]
pub struct ListScrollPosition {
/// The first item of the row the viewport starts in (`0` while it
/// starts in the header or before it).
pub index: u32,
/// How far past the row's start the viewport starts: negative before
/// the first row, past the row's size in the gap after it.
pub offset: Points,
}
impl ListScrollPosition {
/// The JS object `ref.measureScrollPosition()` resolves to.
pub(crate) fn to_json(self) -> JsonValue {
serde_json::json!({ "index": self.index, "offset": self.offset.0 })
}
}
/// Descendants reaching less than this far out of a view do not count
/// (rounding).
const OVERFLOW_EPSILON: f32 = 0.5;
/// How a pointer meets a node ([`Core::pointer_hit`]).
struct PointerHit {
/// The node itself is hit.
own: bool,
/// Its children are looked into.
children: bool,
/// The point in its content (its scroll offset and zoom undone).
inner: Point<Points>,
}
fn valid(r: &Bounds<Points>) -> bool {
!(r.origin.x.0.is_nan()
|| r.origin.y.0.is_nan()
|| r.size.width.0.is_nan()
|| r.size.height.0.is_nan())
}
impl<M: TextEngine> Core<M> {
/// The view a pointer pressing at `page` (in the root view `id` is in)
/// would press inside `id`, with the last layout: the innermost,
/// topmost one there that hears presses, and the point in it. `id`'s
/// own `pointerEvents` do not count: the component asking lies a hit
/// layer of its own over it.
pub(super) fn press_target(
&self,
id: NodeId,
page: Point<Points>,
) -> Option<(NodeId, Point<Points>)> {
let origin = self.measure(id)?.page;
let key = self.key(id).ok()?;
self.press_target_in(key, point(page.x - origin.x, page.y - origin.y), true)
}
fn press_target_in(
&self,
key: Key,
p: Point<Points>,
top: bool,
) -> Option<(NodeId, Point<Points>)> {
let hit = self.pointer_hit(key, p, top)?;
if hit.children {
for child in self.hit_children(key) {
let origin = self.nodes[child as usize].frame.origin;
let at = point(hit.inner.x - origin.x, hit.inner.y - origin.y);
if let Some(hit) = self.press_target_in(child, at, false) {
return Some(hit);
}
}
}
let node = &self.nodes[key as usize];
(hit.own && node.mounted_events().contains(crate::EventKind::Press)).then_some((node.id, p))
}
/// How a pointer at `p` (`key`'s coordinates) meets `key`: `None` when
/// neither it nor anything in it can be hit there. `top`: `key`'s own
/// `pointerEvents` do not count.
fn pointer_hit(&self, key: Key, p: Point<Points>, top: bool) -> Option<PointerHit> {
use crate::props::PointerEvents;
let node = &self.nodes[key as usize];
if node.layout.is_none()
|| !valid(&node.frame)
|| node.view_style.hidden
|| node.hidden_in_text
{
return None;
}
let pointer = match node.props.as_deref().map(|p| p.pointer_events) {
Some(PointerEvents::Auto) | None => node.class_pointer,
Some(set) => set,
};
let pointer = if top { PointerEvents::Auto } else { pointer };
if pointer == PointerEvents::None {
return None;
}
let size = node.frame.size;
let inside = p.x.0 >= 0.0 && p.y.0 >= 0.0 && p.x < size.width && p.y < size.height;
let clips = node.view_style.overflow_hidden
|| matches!(node.kind, Kind::Scroll | Kind::List | Kind::Stack);
if !inside && (clips || pointer == PointerEvents::BoxOnly) {
return None;
}
let inner = match node.scroll.as_deref() {
Some(scroll) => point(
Points((p.x.0 + scroll.offset.0) / scroll.zoom),
Points((p.y.0 + scroll.offset.1) / scroll.zoom),
),
None => p,
};
Some(PointerHit {
own: inside && pointer != PointerEvents::BoxNone,
children: pointer != PointerEvents::BoxOnly,
inner,
})
}
/// The children of `key` a pointer can hit, topmost first: the last of
/// the highest `zIndex`.
fn hit_children(&self, key: Key) -> Vec<Key> {
let mut children: Vec<Key> = self.nodes[key as usize]
.children
.iter()
.copied()
.filter(|&c| {
let child = &self.nodes[c as usize];
!child.inline && !child.is_overlay()
})
.collect();
children.sort_by_key(|&c| self.nodes[c as usize].z_index);
children.reverse();
children
}
/// The views a pointer at `p` (`key`'s coordinates) is over in `key`,
/// innermost first, each with the point in it, as a platform picks
/// them: the innermost, topmost view hit and the views around it.
fn hover_path_in(
&self,
key: Key,
p: Point<Points>,
top: bool,
path: &mut Vec<(Key, Point<Points>)>,
) -> bool {
let Some(hit) = self.pointer_hit(key, p, top) else {
return false;
};
let inner = hit.children
&& self.hit_children(key).into_iter().any(|child| {
let origin = self.nodes[child as usize].frame.origin;
let at = point(hit.inner.x - origin.x, hit.inner.y - origin.y);
self.hover_path_in(child, at, false, path)
});
if inner || hit.own {
path.push((key, p));
}
inner || hit.own
}
/// Passes the pointers components passed on into their elements
/// ([`crate::element::ElementRef::hover_at`]) with the last layout:
/// the views that track hovers or hear the mouse under each are
/// hovered, those it left hear so from the inside out, then those it
/// came over from the outside in, then `onMouseMove` of the hovered
/// ones, inside out.
pub(super) fn pass_hovers(&mut self) {
use crate::{EventKind, element::JsEvent};
for (id, mouse) in std::mem::take(&mut self.components.hovers) {
let mut path = Vec::new();
if let Some(mouse) = mouse
&& let Some(origin) = self.measure(id).map(|m| m.page)
&& let Ok(key) = self.key(id)
{
let at = point(
mouse.page_position.x - origin.x,
mouse.page_position.y - origin.y,
);
self.hover_path_in(key, at, true, &mut path);
}
let hovered: Vec<(Key, Point<Points>)> = path
.into_iter()
.filter(|&(k, _)| {
let n = &self.nodes[k as usize];
crate::input::hoverable(n.tracked, n.mounted_events())
})
.collect();
let ids: Vec<NodeId> = hovered
.iter()
.map(|&(k, _)| self.nodes[k as usize].id)
.collect();
let old = match ids.is_empty() {
true => self.passed_hovers.remove(&id),
false => self.passed_hovers.insert(id, ids.clone()),
}
.unwrap_or_default();
let event = |position: Point<Points>| {
EventPayload::Mouse(crate::MouseEvent {
position,
..mouse.unwrap_or_default()
})
};
let (left, entered) = crate::input::hover_changes(&old, &ids);
let (left, entered): (Vec<NodeId>, Vec<NodeId>) =
(left.copied().collect(), entered.copied().collect());
for node in left {
let Ok(key) = self.key(node) else { continue };
if !std::mem::take(&mut self.nodes[key as usize].passed_hover) {
continue;
}
self.states_changed(key);
if self.nodes[key as usize]
.mounted_events()
.contains(EventKind::MouseLeave)
{
let position = match (mouse, self.measure(node)) {
(Some(m), Some(at)) => {
point(m.page_position.x - at.page.x, m.page_position.y - at.page.y)
}
_ => Point::default(),
};
self.events
.push(JsEvent::Event(node, EventKind::MouseLeave, event(position)));
}
}
for node in entered {
let Some(&(key, position)) = hovered
.iter()
.find(|&&(k, _)| self.nodes[k as usize].id == node)
else {
continue;
};
self.nodes[key as usize].passed_hover = true;
self.states_changed(key);
if self.nodes[key as usize]
.mounted_events()
.contains(EventKind::MouseEnter)
{
self.events
.push(JsEvent::Event(node, EventKind::MouseEnter, event(position)));
}
}
for &(key, position) in &hovered {
let node = &self.nodes[key as usize];
if node.mounted_events().contains(EventKind::MouseMove) {
self.events.push(JsEvent::Event(
node.id,
EventKind::MouseMove,
event(position),
));
}
}
}
}
/// The text of node `id` and the nodes inside it, in tree order
/// ([`crate::element::ElementRef::text_content`]): what `display:
/// none` hides is left out; empty when it is not in the tree.
pub(super) fn text_content(&self, id: NodeId) -> String {
let mut text = String::new();
let Ok(key) = self.key(id) else {
return text;
};
let mut stack = vec![key];
while let Some(k) = stack.pop() {
let node = &self.nodes[k as usize];
if node.layout.is_some_and(|l| self.layout.displays_none(l)) {
continue;
}
if node.kind == Kind::RawText {
text.push_str(&node.text);
}
if let Some(run) = self.folded_text(k) {
text.push_str(run);
}
stack.extend(node.children.iter().rev().copied());
}
text
}
/// Where view `id` is, from the last layout; `None` when it is not in
/// the tree or was never laid out.
pub fn measure(&self, id: NodeId) -> Option<Measurement> {
let key = self.key(id).ok()?;
let node = &self.nodes[key as usize];
if !(node.created || node.culled) || node.layout.is_none() || !valid(&node.frame) {
return None;
}
// Detached (not in the tree): no position.
if key != self.root && !self.in_tree(key) {
return None;
}
let (mut x, mut y) = (0.0f32, 0.0f32);
let mut k = key;
while k != NO_KEY {
let n = &self.nodes[k as usize];
// Modals (and the views UIKit places) are root views of their
// own: positions are in them. A screen fills its stack.
if matches!(
n.kind,
Kind::Modal | Kind::HeaderItem | Kind::InputAccessory
) {
break;
}
// Spans and raw text have no view; text attachments are placed
// in their text block.
if n.layout.is_some() && valid(&n.frame) {
x += n.frame.origin.x.0;
y += n.frame.origin.y.0;
}
// An overlay is placed in its root view.
if n.is_overlay() {
break;
}
let parent = n.parent;
if parent == NO_KEY {
break;
}
// In a scroll view's content: where the content is now.
if let Some(scroll) = self.nodes[parent as usize].scroll.as_deref() {
x = x * scroll.zoom - scroll.offset.0;
y = y * scroll.zoom - scroll.offset.1;
}
k = parent;
}
Some(Measurement {
frame: node.frame,
page: point(Points(x), Points(y)),
})
}
/// Inline attachments get their positions from their text's final layout.
/// A reveal must make a culled text first, as other commands make their view,
/// so its attachment can be measured before reveal expands into scrolls.
pub(super) fn prepare_reveals(&mut self) {
for index in 0..self.commands.len() {
let (id, command) = &self.commands[index];
if !matches!(command, Command::Reveal(_)) {
continue;
}
let Ok(mut key) = self.key(*id) else {
continue;
};
while key != NO_KEY {
if let Some(block) = self.attachment_block(key) {
if !self.nodes[block as usize].created {
self.uncull(block);
}
break;
}
key = self.nodes[key as usize].parent;
}
}
}
/// The `ScrollTo` commands that bring view `id` into view, as
/// `options` places it: one for each scroll view around it that has to
/// move, innermost first, each from where the ones inside it end up.
/// Lists and the root view's own scrolling are left alone. None when
/// the view is not laid out. A scroll view in `projected` (the offsets
/// earlier commands of the commit scroll it to, the last one last) is
/// taken to be there.
pub(super) fn reveal_scrolls(
&self,
id: NodeId,
options: RevealOptions,
projected: &[(NodeId, (f32, f32))],
) -> Vec<(NodeId, Command)> {
let mut scrolls = Vec::new();
if self.measure(id).is_none() {
log::warn!("reveal: view {id} is not laid out");
return scrolls;
}
let Ok(mut k) = self.key(id) else {
return scrolls;
};
let size = self.nodes[k as usize].frame.size;
let (mut x, mut y) = (0.0f32, 0.0f32);
let (mut width, mut height) = (size.width.0, size.height.0);
while k != NO_KEY {
let n = &self.nodes[k as usize];
if matches!(
n.kind,
Kind::Modal | Kind::HeaderItem | Kind::InputAccessory
) {
break;
}
if n.layout.is_some() && valid(&n.frame) {
x += n.frame.origin.x.0;
y += n.frame.origin.y.0;
}
if n.is_overlay() || n.parent == NO_KEY {
break;
}
let parent = &self.nodes[n.parent as usize];
if let Some(scroll) = parent.scroll.as_deref() {
(x, y) = (x * scroll.zoom, y * scroll.zoom);
(width, height) = (width * scroll.zoom, height * scroll.zoom);
let mut offset = scroll.offset;
// A culled scroll view is made for its command.
if parent.kind == Kind::Scroll && valid(&parent.frame) {
let viewport = parent.frame.size;
let inset = scroll.behavior.content_inset;
// The range the platforms scroll over: the content and its insets.
let range =
|content: Option<Points>, viewport: Points, lead: Points, trail: Points| {
let most = content.map_or(f32::INFINITY, |c| (c + trail - viewport).0);
(-lead.0, most.max(-lead.0))
};
let content = (
scroll.content.map(|c| c.width),
scroll.content.map(|c| c.height),
);
let x_range = range(content.0, viewport.width, inset.left, inset.right);
let y_range = range(content.1, viewport.height, inset.top, inset.bottom);
let issued = projected
.iter()
.rfind(|(s, _)| *s == parent.id)
.map(|&(_, to)| to)
.or(scroll.target.map(|(_, to)| to));
if let Some((px, py)) = issued {
offset = (
px.clamp(x_range.0, x_range.1),
py.clamp(y_range.0, y_range.1),
);
}
// Unclamped when it stays: UIKit adds insets the core does not know.
let settle = |to: f32, at: f32, (least, most): (f32, f32)| {
if to == at { at } else { to.clamp(least, most) }
};
let to = if scroll.horizontal {
let to = aligned(x, width, viewport.width.0, offset.0, options.horizontal);
(settle(to, offset.0, x_range), offset.1)
} else {
let to = aligned(y, height, viewport.height.0, offset.1, options.vertical);
(offset.0, settle(to, offset.1, y_range))
};
if to != offset {
scrolls.push((
parent.id,
Command::ScrollTo {
x: Points(to.0),
y: Points(to.1),
animated: options.animated,
},
));
offset = to;
}
}
(x, y) = (x - offset.0, y - offset.1);
}
k = n.parent;
}
scrolls
}
/// The offset `command` scrolls scroll view `id` to, when it does.
pub(super) fn scrolled_to(&self, id: NodeId, command: &Command) -> Option<(f32, f32)> {
let node = &self.nodes[self.key(id).ok()? as usize];
if node.kind != Kind::Scroll {
return None;
}
let scroll = node.scroll.as_deref()?;
match *command {
Command::ScrollTo { x, y, .. } => Some((x.0, y.0)),
Command::ScrollToEnd { .. } => {
let content = scroll.content?;
let viewport = node.frame.size;
Some(if scroll.horizontal {
((content.width - viewport.width).0.max(0.0), 0.0)
} else {
(0.0, (content.height - viewport.height).0.max(0.0))
})
}
_ => None,
}
}
/// Completes an input's `onCaretMove` payload from its platform: adds
/// `pageBounds`, its `bounds` (in the input) moved by where the input is
/// in its root view. The platforms know the caret in their text views,
/// the core where the input is (`ref.measure()`'s `pagePosition`, with
/// the scroll views around it).
pub fn locate_caret(&self, id: NodeId, payload: &mut EventPayload) {
if matches!(payload, EventPayload::None) {
*payload = EventPayload::Map(Vec::new());
}
let EventPayload::Map(fields) = payload else {
return;
};
if fields.iter().any(|(k, _)| *k == "pageBounds") {
return;
}
// A platform that could not place the caret reports an empty one.
let caret = match fields.iter().find(|(k, _)| *k == "bounds") {
Some(&(_, PayloadValue::Bounds(caret))) => caret,
_ => {
fields.retain(|(k, _)| *k != "bounds");
fields.push(("bounds", PayloadValue::Bounds(Bounds::default())));
Bounds::default()
}
};
fields.push((
"pageBounds",
PayloadValue::Bounds(self.page_bounds(id, caret)),
));
}
/// Completes a selectable view's `onSelectionChange` payload from its
/// platform: adds `pageBounds`, its `bounds` (the selected text in the
/// view) moved by where the view is in its root view, as
/// [`Self::locate_caret`] does for a caret. A payload without `bounds`
/// (nothing selected, an input's selection) stays as it is.
pub fn locate_selection(&self, id: NodeId, payload: &mut EventPayload) {
let EventPayload::Map(fields) = payload else {
return;
};
if fields.iter().any(|(k, _)| *k == "pageBounds") {
return;
}
if let Some(&(_, PayloadValue::Bounds(selected))) =
fields.iter().find(|(k, _)| *k == "bounds")
{
fields.push((
"pageBounds",
PayloadValue::Bounds(self.page_bounds(id, selected)),
));
}
}
/// `bounds` in view `id` moved by where the view is in its root view.
fn page_bounds(&self, id: NodeId, bounds: Bounds<Points>) -> Bounds<Points> {
let page = self.measure(id).map_or(Point::default(), |m| m.page);
Bounds {
origin: point(page.x + bounds.origin.x, page.y + bounds.origin.y),
size: bounds.size,
}
}
/// Whether `key` hangs from the main tree's root or a root view that
/// does.
pub(super) fn in_tree(&self, mut key: Key) -> bool {
while key != NO_KEY {
if key == self.root {
return true;
}
key = self.nodes[key as usize].parent;
}
false
}
/// Answers `ref.measure()` call `call` on `id` (null when it is not
/// laid out).
pub(super) fn answer_measure(&mut self, id: NodeId, call: u32) {
let value = self
.measure(id)
.map_or(JsonValue::Null, Measurement::to_json);
self.settle(call, Ok(Box::new(value)));
}
/// Where list `id`'s viewport starts among its rows, from the last
/// layout; `None` when it is no list in the tree, is not laid out or
/// has no items.
pub fn list_scroll_position(&self, id: NodeId) -> Option<ListScrollPosition> {
let key = self.key(id).ok()?;
let node = &self.nodes[key as usize];
if !node.created || (key != self.root && !self.in_tree(key)) {
return None;
}
let (index, offset) = node.list.as_ref()?.scroll_position()?;
Some(ListScrollPosition {
index,
offset: Points(offset),
})
}
/// Answers JS's `ref.measureScrollPosition()` call `call`.
pub(super) fn answer_scroll_position(&mut self, id: NodeId, call: u32) {
let value = self
.list_scroll_position(id)
.map_or(JsonValue::Null, ListScrollPosition::to_json);
self.settle(call, Ok(Box::new(value)));
}
/// Updates the area `key`'s descendants cover outside its bounds (its
/// children's frames and areas are current), and mounts it when it
/// changed; whether it did. The area takes in the descendants' hit
/// slop, so a touch there reaches them through every view around.
/// Clipping views have none: what is outside is not drawn, and takes
/// no touches.
pub(super) fn update_overflow(&mut self, key: Key) -> bool {
let node = &self.nodes[key as usize];
if !node.created || key == self.root {
return false;
}
let clips = node.view_style.overflow_hidden
|| matches!(node.kind, Kind::Scroll | Kind::List | Kind::Stack);
let extent = if clips || !valid(&node.frame) || node.children.is_empty() {
None
} else {
let (w, h) = (node.frame.size.width.0, node.frame.size.height.0);
let (mut x0, mut y0, mut x1, mut y1) = (0.0f32, 0.0f32, w, h);
for &child in &node.children {
let c = &self.nodes[child as usize];
if c.is_layout_root() || c.layout.is_none() || !c.created || !valid(&c.frame) {
continue;
}
let f = c.frame;
let slop = c
.props
.as_ref()
.map_or(EdgeInsets::default(), |p| p.hit_slop);
let (sx0, sy0) = (f.origin.x.0 - slop.left.0, f.origin.y.0 - slop.top.0);
let sx1 = f.origin.x.0 + f.size.width.0 + slop.right.0;
let sy1 = f.origin.y.0 + f.size.height.0 + slop.bottom.0;
if sx1 > sx0 && sy1 > sy0 {
x0 = x0.min(sx0);
y0 = y0.min(sy0);
x1 = x1.max(sx1);
y1 = y1.max(sy1);
}
if let Some(e) = c.overflow {
x0 = x0.min(f.origin.x.0 + e.origin.x.0);
y0 = y0.min(f.origin.y.0 + e.origin.y.0);
x1 = x1.max(f.origin.x.0 + e.origin.x.0 + e.size.width.0);
y1 = y1.max(f.origin.y.0 + e.origin.y.0 + e.size.height.0);
}
}
let out = x0 < -OVERFLOW_EPSILON
|| y0 < -OVERFLOW_EPSILON
|| x1 > w + OVERFLOW_EPSILON
|| y1 > h + OVERFLOW_EPSILON;
out.then_some(rect(x0, y0, x1 - x0, y1 - y0))
};
let node = &mut self.nodes[key as usize];
if node.overflow == extent {
return false;
}
node.overflow = extent;
let id = node.id;
self.mount.push(MountItem::Overflow { id, extent });
true
}
/// Works out again the areas of the views around those whose hit slop
/// changed, after the round's layout: up from each, while an area
/// changes.
pub(super) fn update_slop_overflow(&mut self) {
let mut moved = std::mem::take(&mut self.slop_moved);
for &key in &moved {
if !self.nodes.contains(key as usize) || !self.nodes[key as usize].created {
continue;
}
let mut parent = self.nodes[key as usize].parent;
while parent != NO_KEY && self.update_overflow(parent) {
parent = self.nodes[parent as usize].parent;
}
}
// The buffer, for the next round.
moved.clear();
self.slop_moved = moved;
}
}