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
//! Measure/layout for the retained tree: [`RenderNode::layout`] re-reads
//! signals, re-measures through [`NodeSubView`], and caches each container's
//! child frames for the flush pass.
use super::*;
use waterui_graphics::{resolve_scene_proposal, scene_stretch_axis};
impl RenderNode {
/// The stretch axis this node exposes when it is a layout child.
/// The layout priority this subtree carries, or `0` when nothing set one.
///
/// Only layout-transparent wrappers are walked through: a priority applies to
/// the child it wraps, not across a container boundary.
pub(super) fn priority(&self) -> i32 {
match self {
RenderNode::Wrapper(node) => match &node.effect {
WrapperEffect::LayoutPriority(priority) => priority.get(),
_ => node.child.priority(),
},
RenderNode::Opacity(node) => node.child.priority(),
RenderNode::Scale(node) => node.child.priority(),
RenderNode::Rotation(node) => node.child.priority(),
RenderNode::Offset(node) => node.child.priority(),
RenderNode::Retain(node) => node.child.priority(),
RenderNode::Env(node) => node.child.priority(),
RenderNode::Dynamic(node) => node.child.priority(),
RenderNode::AppliedFilter(node) => node.child.priority(),
_ => 0,
}
}
/// The variant's name, for diagnostics that have to point at a node.
pub(super) fn kind(&self) -> &'static str {
match self {
RenderNode::Color(_) => "Color",
RenderNode::Text(_) => "Text",
RenderNode::Container(_) => "Container",
RenderNode::Opacity(_) => "Opacity",
RenderNode::Scale(_) => "Scale",
RenderNode::Rotation(_) => "Rotation",
RenderNode::Offset(_) => "Offset",
RenderNode::Retain(_) => "Retain",
RenderNode::Env(_) => "Env",
RenderNode::Dynamic(_) => "Dynamic",
RenderNode::SceneView(_) => "SceneView",
RenderNode::GpuSurface(_) => "GpuSurface",
RenderNode::ViewEffect(_) => "ViewEffect",
RenderNode::AppliedFilter(_) => "AppliedFilter",
RenderNode::Scroll(_) => "Scroll",
RenderNode::LazyStack(_) => "LazyStack",
RenderNode::Collection(_) => "Collection",
RenderNode::Wrapper(_) => "Wrapper",
RenderNode::Widget(_) => "Widget",
}
}
/// The child nodes this one owns, for diagnostics that walk the tree.
///
/// Deliberately a diagnostic-only accessor: layout and flush each need more
/// than the children (frames, environments, retained caches), so they keep
/// their own matches rather than routing through this.
pub(super) fn child_nodes(&self) -> Vec<&RenderNode> {
match self {
RenderNode::Container(node) => node.children.iter().collect(),
RenderNode::Opacity(node) => vec![&node.child],
RenderNode::Scale(node) => vec![&node.child],
RenderNode::Rotation(node) => vec![&node.child],
RenderNode::Offset(node) => vec![&node.child],
RenderNode::Retain(node) => vec![&node.child],
RenderNode::Env(node) => vec![&node.child],
RenderNode::Dynamic(node) => vec![&node.child],
RenderNode::AppliedFilter(node) => vec![&node.child],
RenderNode::Wrapper(node) => vec![&node.child],
RenderNode::Scroll(node) => vec![&node.child],
_ => Vec::new(),
}
}
pub(super) fn stretch(&self) -> StretchAxis {
match self {
RenderNode::Color(_) => StretchAxis::Both,
RenderNode::Text(_) => StretchAxis::None,
RenderNode::Container(container) => {
// The retained children answer for themselves, so a transparent
// layout reports what its content currently claims rather than
// what it claimed when the tree was built.
let child_axes: Vec<StretchAxis> =
container.children.iter().map(RenderNode::stretch).collect();
container.layout.stretch_axis(&child_axes)
}
RenderNode::Opacity(node) => node.child.stretch(),
RenderNode::Scale(node) => node.child.stretch(),
RenderNode::Rotation(node) => node.child.stretch(),
RenderNode::Offset(node) => node.child.stretch(),
RenderNode::Retain(node) => node.child.stretch(),
RenderNode::Env(node) => node.child.stretch(),
RenderNode::Dynamic(node) => node.child.stretch(),
// Scene content that is naturally a size is content-sized and claims
// no leftover space; content that has no size of its own fills.
RenderNode::SceneView(node) => {
scene_stretch_axis(node.content.borrow().intrinsic_size())
}
// A GpuSurface fills its proposal (`GpuView::stretch_axis` default);
// a ViewEffect is a `StretchAxis::None` raw view; an AppliedFilter is
// a layout-transparent wrapper delegating to its child.
RenderNode::GpuSurface(_) => StretchAxis::Both,
RenderNode::ViewEffect(_) => StretchAxis::None,
RenderNode::AppliedFilter(node) => node.child.stretch(),
RenderNode::Scroll(_) => StretchAxis::Both,
// The same stack laid out eagerly is content-sized on both axes, so a
// lazy one has to be too: making a stack virtualizable must not change
// how it sizes. Rows that want the full cross axis ask for it
// themselves, exactly as they do in the eager path.
RenderNode::LazyStack(_) => StretchAxis::None,
RenderNode::Collection(node) => {
// A collection's membership is reactive; its layout is one of the
// content-sized stacks, which ignores the children anyway.
node.layout.stretch_axis(&[])
}
RenderNode::Wrapper(node) => node.child.stretch(),
RenderNode::Widget(node) => node.stretch,
}
}
/// Measure this node under a proposal (recursive). Text shaping runs through
/// the renderer's [`HydroState`] on the main thread.
pub(super) fn measure(
&self,
state: &mut HydroState,
env: &Environment,
proposal: ProposalSize,
) -> ViewDimensions {
match self {
RenderNode::Color(_) => ViewDimensions::new(Size::new(
proposal.width.unwrap_or(0.0),
proposal.height.unwrap_or(0.0),
)),
RenderNode::Text(text) => HydrolysisRenderer::measure_text_dimensions(
state,
text.content.get(),
text.alignment.get(),
env,
proposal.width,
text.line_limit,
),
RenderNode::Container(container) => {
let cell = RefCell::new(state);
let subs: Vec<NodeSubView> = container
.children
.iter()
.map(|child| NodeSubView::new(child, &cell, env))
.collect();
let refs: Vec<&dyn SubView> = subs.iter().map(|sub| sub as &dyn SubView).collect();
ViewDimensions::new(container.layout.size_that_fits(proposal, &refs))
}
// Transform/opacity wrappers are layout-transparent.
RenderNode::Opacity(node) => node.child.measure(state, env, proposal),
RenderNode::Scale(node) => node.child.measure(state, env, proposal),
RenderNode::Rotation(node) => node.child.measure(state, env, proposal),
RenderNode::Offset(node) => node.child.measure(state, env, proposal),
RenderNode::Retain(node) => node.child.measure(state, env, proposal),
RenderNode::Env(node) => node.child.measure(state, &node.env, proposal),
RenderNode::Dynamic(node) => node.child.measure(state, env, proposal),
// Scene content that is naturally a size (an SVG's viewBox, a
// formula's typeset box) answers with it on whichever axis the
// container left open; content that is not fills the proposal.
RenderNode::SceneView(node) => {
let resolved =
resolve_scene_proposal(node.content.borrow().intrinsic_size(), proposal);
ViewDimensions::new(Size::new(
resolved.width.unwrap_or(0.0),
resolved.height.unwrap_or(0.0),
))
}
// A GpuSurface fills its proposal, like a self-drawn scene.
RenderNode::GpuSurface(_) => ViewDimensions::new(Size::new(
proposal.width.unwrap_or(0.0),
proposal.height.unwrap_or(0.0),
)),
// A ViewEffect and an AppliedFilter are sized by their content: the
// effect captures the child into a texture at the child's bounds.
RenderNode::ViewEffect(node) => node.child.borrow().measure(state, &node.env, proposal),
RenderNode::AppliedFilter(node) => node.child.measure(state, &node.env, proposal),
RenderNode::Scroll(_) => ViewDimensions::new(Size::new(
proposal.width.unwrap_or(0.0),
proposal.height.unwrap_or(0.0),
)),
RenderNode::LazyStack(node) => node.measure(state, proposal),
RenderNode::Collection(node) => node.measure(state, proposal),
// Layout-transparent: the wrapper measures its child under the node's
// scoped environment (effect colors/a11y read env every frame).
RenderNode::Wrapper(node) => node.child.measure(state, &node.env, proposal),
RenderNode::Widget(node) => node.behavior.measure(state, proposal, &node.env),
}
}
/// Re-measure and re-place this subtree, caching each container's child
/// frames. Run on build and whenever a geometry-affecting input changes.
pub(crate) fn layout(
&mut self,
renderer: &mut HydrolysisRenderer,
env: &Environment,
size: Size,
) {
// No proposal parameter: at layout time a node is always placed at a concrete
// `size`, so a layout-transparent wrapper lays its child out at exactly that
// size (`ProposalSize::new(Some(size.w), Some(size.h))`).
match self {
RenderNode::Container(container) => {
let rects = {
let cell = RefCell::new(&mut renderer.state);
let subs: Vec<NodeSubView> = container
.children
.iter()
.map(|child| NodeSubView::new(child, &cell, env))
.collect();
let refs: Vec<&dyn SubView> =
subs.iter().map(|sub| sub as &dyn SubView).collect();
container.layout.place(Rect::from_size(size), &refs)
};
for (child, rect) in container.children.iter_mut().zip(rects.iter()) {
child.layout(renderer, env, *rect.size());
}
container.placed = rects;
}
// Transform/opacity wrappers are layout-transparent: the child lays out
// at the same concrete size as the wrapper.
RenderNode::Opacity(node) => node.child.layout(renderer, env, size),
RenderNode::Scale(node) => node.child.layout(renderer, env, size),
RenderNode::Rotation(node) => node.child.layout(renderer, env, size),
RenderNode::Offset(node) => node.child.layout(renderer, env, size),
RenderNode::Retain(node) => node.child.layout(renderer, env, size),
RenderNode::Env(node) => {
let node_env = node.env.clone();
node.child.layout(renderer, &node_env, size);
}
// Layout-transparent: the child lays out at the same concrete size,
// under the wrapper's scoped environment.
RenderNode::Wrapper(node) => {
let node_env = node.env.clone();
node.child.layout(renderer, &node_env, size);
}
RenderNode::Dynamic(node) => node.child.layout(renderer, env, size),
RenderNode::Scroll(node) => {
let child_proposal = match node.axis {
ScrollAxis::Horizontal => ProposalSize::new(None, Some(size.height)),
ScrollAxis::Vertical => ProposalSize::new(Some(size.width), None),
ScrollAxis::All => ProposalSize::UNSPECIFIED,
_ => panic!("hydrolysis render tree: unsupported scroll axis"),
};
let intrinsic = node
.child
.measure(&mut renderer.state, env, child_proposal)
.size;
let content_size = match node.axis {
ScrollAxis::Horizontal => {
Size::new(intrinsic.width.max(size.width), size.height)
}
ScrollAxis::Vertical => {
Size::new(size.width, intrinsic.height.max(size.height))
}
ScrollAxis::All => Size::new(
intrinsic.width.max(size.width),
intrinsic.height.max(size.height),
),
_ => panic!("hydrolysis render tree: unsupported scroll axis"),
};
node.child.layout(renderer, env, content_size);
let handle = if let Some(handle) = node.handle.as_mut() {
handle.rebind(
node.axis,
f64::from(size.width),
f64::from(size.height),
f64::from(content_size.width),
f64::from(content_size.height),
)
} else {
ScrollHandle::new(
node.axis,
f64::from(size.width),
f64::from(size.height),
f64::from(content_size.width),
f64::from(content_size.height),
)
};
if let Some(controller) = &node.controller {
let generation = renderer.read_signal(&controller.generation());
if generation != node.applied_scroll_generation.get() {
let target = renderer.read_signal(&controller.target());
let _ = handle.scroll_to(f64::from(target.x), f64::from(target.y));
node.applied_scroll_generation.set(generation);
}
}
node.handle = Some(handle);
node.content_size = content_size;
node.viewport = size;
}
RenderNode::Collection(node) => node.layout(renderer, size),
// A ViewEffect captures its child into a texture sized to the child's
// bounds, so the child lays out at the concrete size (re-laid-out only
// on a change). An AppliedFilter is layout-transparent: its child lays
// out at the same concrete size under the node's scoped environment.
RenderNode::ViewEffect(node) => {
if size != node.laid_out.get() {
let node_env = node.env.clone();
node.child.borrow_mut().layout(renderer, &node_env, size);
node.laid_out.set(size);
}
}
RenderNode::AppliedFilter(node) => {
let node_env = node.env.clone();
node.child.layout(renderer, &node_env, size);
}
// A lazy stack places its items lazily at flush (offset-dependent); a
// widget leaf or GpuSurface renders itself at flush from `ctx.bounds`.
// Nothing to pre-lay-out for any of these.
RenderNode::Color(_)
| RenderNode::Text(_)
| RenderNode::SceneView(_)
| RenderNode::GpuSurface(_)
| RenderNode::LazyStack(_)
| RenderNode::Widget(_) => {}
}
}
}