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
//! Where views are: measuring a node for `ref.measure()`, and the areas
//! descendants cover outside their parents' bounds, which platforms
//! hit-test ([`MountItem::Overflow`]).
use super::{Core, Key, Kind, NO_KEY};
use crate::{
JsonValue,
geometry::{Bounds, Point, Points, point, rect},
mount::MountItem,
ops::NodeId,
text::TextEngine,
};
/// 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 },
})
}
}
/// Descendants reaching less than this far out of a view do not count
/// (rounding).
const OVERFLOW_EPSILON: f32 = 0.5;
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> {
/// 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.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.kind == Kind::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)),
})
}
/// 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)));
}
/// Updates the area `key`'s descendants cover outside its bounds (its
/// children's frames and areas are current), and mounts it when it
/// changed. Clipping views have none: what is outside is not drawn.
pub(super) fn update_overflow(&mut self, key: Key) {
let node = &self.nodes[key as usize];
if !node.created || key == self.root {
return;
}
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.kind.is_root() || c.layout.is_none() || !c.created || !valid(&c.frame) {
continue;
}
let f = c.frame;
if f.size.width.0 > 0.0 && f.size.height.0 > 0.0 {
x0 = x0.min(f.origin.x.0);
y0 = y0.min(f.origin.y.0);
x1 = x1.max(f.origin.x.0 + f.size.width.0);
y1 = y1.max(f.origin.y.0 + f.size.height.0);
}
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 {
node.overflow = extent;
let id = node.id;
self.mount.push(MountItem::Overflow { id, extent });
}
}
}