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
use ribir_types::Size;
use super::{WidgetCtx, WidgetCtxImpl};
use crate::{
prelude::{Point, ProviderCtx},
widget::{BoxClamp, WidgetTree},
widget_tree::WidgetId,
};
/// A place to compute the render object's layout.
///
/// Rather than holding children directly, `Layout` perform layout across
/// `MeasureCtx`. `MeasureCtx` provide method to perform child layout and also
/// provides methods to update descendants position.
pub struct MeasureCtx<'a> {
pub(crate) id: WidgetId,
/// The widget tree of the window, not borrow it from `wnd` is because a
/// `MeasureCtx` always in a mutable borrow.
pub(crate) tree: &'a mut WidgetTree,
pub(crate) provider_ctx: ProviderCtx,
pub(crate) laid_out_queue: &'a mut Vec<WidgetId>,
}
pub struct PlaceCtx<'a> {
pub(crate) id: WidgetId,
pub(crate) tree: &'a mut WidgetTree,
pub(crate) provider_ctx: &'a mut ProviderCtx,
}
impl<'a> WidgetCtxImpl for MeasureCtx<'a> {
#[inline]
fn id(&self) -> WidgetId { self.id }
#[inline]
fn tree(&self) -> &WidgetTree { self.tree }
}
impl<'a> WidgetCtxImpl for PlaceCtx<'a> {
#[inline]
fn id(&self) -> WidgetId { self.id }
#[inline]
fn tree(&self) -> &WidgetTree { self.tree }
}
impl<'a> MeasureCtx<'a> {
pub(crate) fn new(
id: WidgetId, tree: &'a mut WidgetTree, laid_out_queue: &'a mut Vec<WidgetId>,
) -> Self {
let provider_ctx = if let Some(p) = id.parent(tree) {
ProviderCtx::collect_from(p, tree)
} else {
ProviderCtx::default()
};
Self { id, tree, provider_ctx, laid_out_queue }
}
/// Perform layout of the widget of the context and return its size.
pub(crate) fn perform_layout(&mut self, clamp: BoxClamp) -> Size {
self
.get_calculated_size(self.id, clamp)
.unwrap_or_else(|| {
// Safety: the `tree` just use to get the widget of `id`, and `tree2` not drop
// or modify it during measure.
let tree2 = unsafe { &*(self.tree as *mut WidgetTree) };
let id = self.id();
{
let info = self.tree.store.layout_info_or_default(id);
info.clamp = clamp;
}
debug_assert!(clamp.min.is_finite());
let size = id.assert_get(tree2).measure(clamp, self);
if !size.is_finite() {
#[cfg(feature = "debug")]
let id_name = id.get(tree2).unwrap().debug_name();
#[cfg(not(feature = "debug"))]
let id_name = format!("{id:?}");
tracing::error!("Layout of {:?} is not finite: {:?}", id_name, size);
debug_assert!(size.is_finite(), "Layout of widget is not finite: {:?}", size);
}
let info = self.tree.store.layout_info_or_default(id);
info.size = Some(size);
{
let mut layout_ctx =
PlaceCtx { id, tree: self.tree, provider_ctx: &mut self.provider_ctx };
layout_ctx.perform_place(size);
}
self.provider_ctx.pop_providers_for(id);
self.laid_out_queue.push(id);
size
})
}
/// Perform layout of the `child` and return its size.
pub fn layout_child(&mut self, child: WidgetId, clamp: BoxClamp) -> Size {
self
.get_calculated_size(child, clamp)
.unwrap_or_else(|| {
let id = std::mem::replace(&mut self.id, child);
let size = self.perform_layout(clamp);
self.id = id;
size
})
}
pub fn clamp(&self) -> BoxClamp {
self
.tree
.store
.layout_info(self.id())
.unwrap()
.clamp
}
/// Adjust the size of the layout widget. Use this method to directly modify
/// the size of a widget. In most cases, it is unnecessary to call this
/// method; using clamp to constrain the child size is typically sufficient.
/// Only use this method if you are certain of its effects.
#[inline]
pub fn update_size(&mut self, child: WidgetId, size: Size) {
self.tree.store.layout_info_or_default(child).size = Some(size);
}
/// Split a children iterator from the context, returning a tuple of `&mut
/// MeasureCtx` and the iterator of the children.
pub fn split_children(&mut self) -> (&mut Self, impl Iterator<Item = WidgetId> + '_) {
// Safety: The widget tree structure is immutable during the layout phase, so we
// can safely split an iterator of children from the layout.
let tree = unsafe { &*(self.tree as *mut WidgetTree) };
let id = self.id;
(self, id.children(tree))
}
/// Quick method to do the work of computing the layout for the single child,
/// and return its size it should have.
///
/// # Panic
/// panic if there are more than one child it have.
pub fn perform_single_child_layout(&mut self, clamp: BoxClamp) -> Option<Size> {
self
.single_child()
.map(|child| self.layout_child(child, clamp))
}
/// Quick method to do the work of computing the layout for the single child,
/// and return its size.
///
/// # Panic
/// panic if there is not only one child it have.
pub fn assert_perform_single_child_layout(&mut self, clamp: BoxClamp) -> Size {
let child = self.assert_single_child();
self.layout_child(child, clamp)
}
/// Clear the child layout information, so the `child` will be force layout
/// when call `[MeasureCtx::layout_child]!` even if it has layout cache
/// information with same input.
#[inline]
pub fn force_child_relayout(&mut self, child: WidgetId) -> bool {
assert_eq!(child.parent(self.tree), Some(self.id));
self.tree.store.force_layout(child).is_some()
}
fn get_calculated_size(&self, child: WidgetId, clamp: BoxClamp) -> Option<Size> {
let info = self.tree.store.layout_info(child)?;
if info.clamp == clamp { info.size } else { None }
}
}
impl<'a> PlaceCtx<'a> {
/// Perform the complete placement flow for a widget:
/// 1. Reset all children positions to zero
/// 2. Call `Render::place_children` to let the parent place children
/// 3. Apply `adjust_position` to all children to finalize their positions
///
/// This ensures that even if the parent doesn't explicitly call
/// `update_position` for some children, their `adjust_position` will still be
/// triggered.
pub(crate) fn perform_place(&mut self, size: Size) {
// Safety: The widget tree structure is immutable during the layout phase.
let tree2 = unsafe { &*(self.tree as *mut WidgetTree) };
let id = self.id;
// Step 1: Reset all children positions to zero before calling place_children
for child in id.children(tree2) {
self.tree.store.layout_info_or_default(child).pos = Point::zero();
}
// Step 2: Let the widget place its children
id.assert_get(tree2).place_children(size, self);
// Step 3: Apply adjust_position to all children
// We need to push parent's (id) providers back because they were restored
// after place_children completed. Child's adjust_position may need to
// access parent's providers.
let mut buffer = smallvec::SmallVec::new();
self
.provider_ctx
.push_providers_for(id, tree2, &mut buffer);
for child in id.children(tree2) {
// Temporarily set id to child for the adjust_position call
self.id = child;
// Safety: we need two mutable accesses to the tree - one through self for
// the PlaceCtx needed by adjust_position, and one for accessing store.
// These accesses are to different parts of the tree and don't overlap.
let store = unsafe { &mut (*(&mut *self.tree as *mut WidgetTree)).store };
let pos = store.layout_info_or_default(child).pos;
let pos = child.assert_get(tree2).adjust_position(pos, self);
store.layout_info_or_default(child).pos = pos;
}
// Pop parent's providers and restore original id
self.provider_ctx.pop_providers_for(id);
self.id = id;
}
/// Place the child at the given position.
///
/// Note: The position will be further adjusted by `adjust_position` after
/// `place_children` completes.
#[inline]
pub fn update_position(&mut self, child: WidgetId, pos: Point) {
self.tree.store.layout_info_or_default(child).pos = pos;
}
/// Return the stored position of the widget.
#[inline]
pub fn position(&mut self, child: WidgetId) -> Option<Point> {
self
.tree
.store
.layout_info(child)
.map(|info| info.pos)
}
#[inline]
pub fn clamp(&self) -> BoxClamp {
self
.tree
.store
.layout_info(self.id)
.unwrap()
.clamp
}
/// Split a children iterator from the context, returning a tuple of `&mut
/// LayoutCtx` and the iterator of the children.
pub fn split_children(&mut self) -> (&mut Self, impl Iterator<Item = WidgetId> + '_) {
// Safety: The widget tree structure is immutable during the layout phase, so we
// can safely split an iterator of children from the layout.
let tree = unsafe { &*(self.tree as *mut WidgetTree) };
let id = self.id;
(self, id.children(tree))
}
pub fn widget_box_size(&self, widget: WidgetId) -> Option<Size> {
self
.tree
.store
.layout_info(widget)
.and_then(|info| info.size)
}
}
impl<'w> AsRef<ProviderCtx> for MeasureCtx<'w> {
fn as_ref(&self) -> &ProviderCtx { &self.provider_ctx }
}
impl<'w> AsMut<ProviderCtx> for MeasureCtx<'w> {
fn as_mut(&mut self) -> &mut ProviderCtx { &mut self.provider_ctx }
}
impl<'a> AsRef<ProviderCtx> for PlaceCtx<'a> {
fn as_ref(&self) -> &ProviderCtx { self.provider_ctx }
}
impl<'a> AsMut<ProviderCtx> for PlaceCtx<'a> {
fn as_mut(&mut self) -> &mut ProviderCtx { self.provider_ctx }
}