mitsuami-core 2.0.0

Node tree, layout, styles, units, a11y model and backend contract for mitsuami
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
//! Layout with Taffy: windows, lists and toolbars, measuring leaves, and
//! the sizes views watch.

use crate::backend::{AvailableSpace, MeasureRequest};
use crate::command::Command;
use crate::geometry::{Point, Rect, Size};
use crate::widget::{NodeId, Prop, RowKey, WidgetKind};

use super::{Fit, Inner, Observer, Ui};

impl Ui {
    /// A node's laid-out size; zero for none.
    pub(crate) fn measured_size(&self, id: Option<NodeId>) -> Size {
        let inner = self.inner.borrow();
        id.and_then(|id| inner.nodes.get(&id)).map_or(Size::ZERO, |n| n.frame.size)
    }

    /// Keeps `size` up to date with the size of the node `target` gives,
    /// after each layout, until [`Ui::unobserve_size`].
    pub(crate) fn observe_size(
        &self,
        target: Box<dyn Fn() -> Option<NodeId>>,
        size: mitsuami_reactive::Signal<Size>,
    ) -> u64 {
        let mut inner = self.inner.borrow_mut();
        let id = inner.next_observer;
        inner.next_observer += 1;
        let last = size.get_untracked();
        inner.observers.insert(id, Observer { target, size, last });
        id
    }

    pub(crate) fn unobserve_size(&self, id: u64) {
        self.inner.borrow_mut().observers.remove(&id);
    }

    /// Sets the watched sizes that changed since they were last reported.
    /// Whether any did.
    pub(super) fn report_sizes(&self) -> bool {
        let changed: Vec<_> = {
            let mut inner = self.inner.borrow_mut();
            let Inner { observers, nodes, .. } = &mut *inner;
            observers
                .values_mut()
                .filter_map(|o| {
                    let size = (o.target)().and_then(|id| nodes.get(&id)).map_or(Size::ZERO, |n| n.frame.size);
                    (size != o.last).then(|| {
                        o.last = size;
                        (o.size, size)
                    })
                })
                .collect()
        };
        if changed.is_empty() {
            return false;
        }
        crate::task::with_current(self, || {
            mitsuami_reactive::batch(|| {
                for (signal, size) in changed {
                    if signal.is_alive() {
                        signal.set(size);
                    }
                }
            })
        });
        true
    }
}

impl Inner {
    pub(super) fn layout(&mut self) {
        self.size_tab_strips();
        self.content_limits();
        for window in self.windows.clone() {
            let node = &self.nodes[&window];
            let Some(root) = node.taffy else { continue };
            let (mut size, fit) = (node.window_size, node.fit);
            // A following window fits again when its content or its width
            // changed; one in full screen keeps the screen's size.
            let refit = match fit {
                Fit::None => false,
                Fit::Once => true,
                Fit::Follow { .. } => !node.in_full_screen() && self.taffy.dirty(root).unwrap_or(true),
            };
            if refit {
                self.set_root_height(root, None);
                let available = taffy::Size {
                    width: taffy::AvailableSpace::Definite(size.width),
                    height: taffy::AvailableSpace::MaxContent,
                };
                self.compute_layout(root, available);
                let height = self.taffy.layout(root).map_or(0.0, |l| l.size.height).ceil();
                let node = self.nodes.get_mut(&window).unwrap();
                if fit == Fit::Once {
                    node.fit = Fit::None;
                }
                if fit == Fit::Once || node.heights.last() != Some(&height) {
                    size.height = height;
                    node.window_size = size;
                    node.heights.push(height);
                    // A platform may never report some of them (a window
                    // taller than the screen): keep the latest.
                    if node.heights.len() > 16 {
                        node.heights.remove(0);
                    }
                    // The window's style takes the fitted height, and `vh`
                    // re-resolves against it.
                    self.styles_dirty = true;
                    self.pending.push(Command::SetWindowSize { id: window, size });
                }
            }
            // Fitted once, it keeps the layout it was measured with; a
            // following window is laid out at the height it asked for, or
            // the one the platform gave (its minimum, the user's).
            if fit != Fit::Once {
                if refit {
                    self.set_root_height(root, Some(size.height));
                }
                let available = taffy::Size {
                    width: taffy::AvailableSpace::Definite(size.width),
                    height: taffy::AvailableSpace::Definite(size.height),
                };
                self.compute_layout(root, available);
            }
            self.nodes.get_mut(&window).unwrap().frame = Rect { origin: Point::ZERO, size };
            self.layout_toolbar(window);
            self.collect_frames(window, Point::ZERO);
        }
        self.update_drawings();
    }

    /// Measures the tab strips of the tab views, and the headings of the
    /// groups, whose titles or metrics changed (their layout is dirty),
    /// which they're at least as big as.
    /// Done here, not with the styles: the platform can only measure a
    /// strip it has created.
    fn size_tab_strips(&mut self) {
        let tabs: Vec<(NodeId, taffy::NodeId)> = self
            .nodes
            .iter()
            .filter(|(_, n)| matches!(n.kind, WidgetKind::Tabs | WidgetKind::Group))
            .filter_map(|(id, n)| Some((*id, n.taffy?)))
            .filter(|(_, t)| self.taffy.dirty(*t).unwrap_or(true))
            .collect();
        let mut changed = false;
        for (id, _) in tabs {
            let request = MeasureRequest {
                known_width: None,
                known_height: None,
                available_width: AvailableSpace::MaxContent,
                available_height: AvailableSpace::MaxContent,
            };
            let strip = self.backend.measure(id, request);
            let insets = match self.nodes[&id].kind {
                WidgetKind::Tabs => self.backend.tab_insets(id),
                _ => self.backend.group_insets(id),
            };
            let node = self.nodes.get_mut(&id).unwrap();
            changed |= node.strip != strip || node.insets != insets;
            node.strip = strip;
            node.insets = insets;
        }
        // Their styles take the new minimums.
        if changed {
            self.resolve_styles();
        }
    }

    /// Gives the least and most sizes set to `Length::MaxContent` the
    /// node's natural size, measured now: Taffy's flexbox treats an
    /// intrinsic limit as none. Only measured nodes (leaves) have one; a
    /// list's changes with its rows, which its backend reports
    /// (`Remeasure`), so it's measured again each layout.
    fn content_limits(&mut self) {
        let limited: Vec<(NodeId, taffy::NodeId)> = self
            .nodes
            .iter()
            .filter(|(_, n)| {
                let s = &n.style;
                [s.min_width, s.min_height, s.max_width, s.max_height].contains(&crate::units::Length::MaxContent)
            })
            .filter_map(|(id, n)| Some((*id, n.taffy?)))
            .filter(|(_, t)| self.taffy.children(*t).is_ok_and(|c| c.is_empty()))
            .collect();
        for (id, t) in limited {
            let request = MeasureRequest {
                known_width: None,
                known_height: None,
                available_width: AvailableSpace::MaxContent,
                available_height: AvailableSpace::MaxContent,
            };
            let node = &self.nodes[&id];
            let natural = match node.drawn() {
                Some(custom) => custom.measure_drawn(&request, &self.metrics).unwrap_or(Size::ZERO),
                None => self.backend.measure(id, request),
            };
            let content = |l: crate::units::Length, v: f32, old: taffy::LengthPercentageAuto| match l {
                crate::units::Length::MaxContent => taffy::LengthPercentageAuto::length(v),
                _ => old,
            };
            let (s, Ok(mut style)) = (&node.style, self.taffy.style(t).cloned()) else { continue };
            let min = taffy::Size {
                width: content(s.min_width, natural.width, style.min_size.width),
                height: content(s.min_height, natural.height, style.min_size.height),
            };
            let max = taffy::Size {
                width: content(s.max_width, natural.width, style.max_size.width),
                height: content(s.max_height, natural.height, style.max_size.height),
            };
            if style.min_size != min || style.max_size != max {
                (style.min_size, style.max_size) = (min, max);
                let _ = self.taffy.set_style(t, style);
            }
        }
    }

    /// Sets a window's layout height: its own, or `None` for its content's.
    fn set_root_height(&mut self, root: taffy::NodeId, height: Option<f32>) {
        let Ok(mut style) = self.taffy.style(root).cloned() else { return };
        style.size.height = height.map_or(taffy::Dimension::auto(), taffy::Dimension::length);
        let _ = self.taffy.set_style(root, style);
    }

    /// Lays out the tree under `root`, measuring leaves natively (or, for
    /// drawn custom widgets, with their shared measure).
    fn compute_layout(&mut self, root: taffy::NodeId, available: taffy::Size<taffy::AvailableSpace>) {
        let Inner { taffy, backend, nodes, metrics, .. } = self;
        let _ = taffy.compute_layout_with_measure(root, available, |input, _, context, style| {
            taffy::compute_leaf_layout(
                input,
                style,
                |_, _| 0.0,
                |known, available| match context {
                    Some(id) => {
                        let request = MeasureRequest {
                            known_width: known.width,
                            known_height: known.height,
                            available_width: space(available.width),
                            available_height: space(available.height),
                        };
                        let node = nodes.get(id);
                        let size = match node.and_then(|n| n.drawn()) {
                            Some(custom) => {
                                let size = custom.measure_drawn(&request, metrics).unwrap_or(Size::ZERO);
                                Size::new(known.width.unwrap_or(size.width), known.height.unwrap_or(size.height))
                            }
                            // A list is as high as the platform places its
                            // rows, and scrolls in whatever it gets: at
                            // least, nothing. Otherwise a row or column
                            // holding it would be at least as high as all
                            // its rows, as in CSS without `min-height: 0`.
                            None if node.is_some_and(|n| n.kind.has_rows()) => {
                                let natural = backend.measure(*id, request);
                                let least = known.height.is_none()
                                    && matches!(available.height, taffy::AvailableSpace::MinContent);
                                if least { Size::new(natural.width, 0.0) } else { natural }
                            }
                            None => backend.measure(*id, request),
                        };
                        taffy::Size { width: size.width, height: size.height }
                    }
                    None => taffy::Size::ZERO,
                },
            )
        });
    }

    /// Lays out a `List`'s mounted rows, each on its own at the width the
    /// list gives its rows. Their sizes are sent; where they go is the
    /// platform's to decide. A `Tree`'s are narrower by their indent.
    fn layout_list(&mut self, list: NodeId) {
        let node = &self.nodes[&list];
        let width = node.row_width.unwrap_or(node.frame.width());
        let depths: std::collections::HashMap<RowKey, u32> = crate::find_prop!(node.props, TreeRows)
            .map(|rows| rows.iter().map(|r| (r.row, r.depth)).collect())
            .unwrap_or_default();
        let indent = node.tree_indent;
        for host in node.native_children.clone() {
            let depth = crate::find_prop!(self.nodes[&host].props, Row).and_then(|row| depths.get(&row).copied());
            let inset = depth.map_or(0.0, |depth| indent.at(depth));
            self.layout_hosted(host, (width - inset).max(0.0), None);
        }
    }

    /// Lays out a `Table`'s mounted cells, each on its own at the width
    /// its column gives its cells, and as high as its content. Their sizes
    /// are sent; where they go is the platform's to decide, and it makes
    /// each row as high as its highest cell, at least. A cell that fills
    /// its row is at least as high as the platform says the row is
    /// without it.
    fn layout_table(&mut self, table: NodeId) {
        let node = &self.nodes[&table];
        let columns = crate::find_prop!(node.props, Columns).unwrap_or_default();
        // Until the platform says, the widths the columns start at.
        let widths =
            node.column_widths.clone().unwrap_or_else(|| columns.iter().map(|c| c.width.unwrap_or(100.0)).collect());
        for host in node.native_children.clone() {
            let Some(cell) = crate::find_prop!(self.nodes[&host].props, Cell) else { continue };
            let width = widths.get(cell.column).copied().unwrap_or(0.0);
            let fills = columns.get(cell.column).is_some_and(|c| c.fills_row);
            let at_least = fills.then(|| self.nodes[&table].row_heights.get(&cell.row).copied()).flatten();
            self.layout_hosted(host, width, at_least);
        }
    }

    /// Lays out a row's or cell's host on its own, at this width and as
    /// high as its content (or `at_least`, if higher), and sends its size.
    fn layout_hosted(&mut self, host: NodeId, width: f32, at_least: Option<f32>) {
        let Some(t) = self.nodes[&host].taffy else { return };
        let set_size = |taffy: &mut taffy::TaffyTree<NodeId>, height: taffy::Dimension| {
            let size = taffy::Size { width: taffy::Dimension::length(width), height };
            if let Ok(style) = taffy.style(t)
                && style.size != size
            {
                let mut style = style.clone();
                style.size = size;
                let _ = taffy.set_style(t, style);
            }
        };
        set_size(&mut self.taffy, taffy::Dimension::auto());
        let available =
            taffy::Size { width: taffy::AvailableSpace::Definite(width), height: taffy::AvailableSpace::MaxContent };
        self.compute_layout(t, available);
        let mut height = self.taffy.layout(t).map_or(0.0, |l| round(l.size.height));
        if let Some(at_least) = at_least.filter(|h| *h > height) {
            set_size(&mut self.taffy, taffy::Dimension::length(at_least));
            self.compute_layout(t, available);
            height = at_least;
        }
        let frame = Rect::new(0.0, 0.0, width, height);
        let node = self.nodes.get_mut(&host).unwrap();
        if node.frame != frame || !node.framed {
            (node.frame, node.framed) = (frame, true);
            self.pending.push(Command::SetFrame { id: host, frame });
        }
        self.collect_frames(host, Point::ZERO);
    }

    /// Lays out a window's toolbar items, each on its own at its natural
    /// size. Where they go is the platform's to decide.
    fn layout_toolbar(&mut self, window: NodeId) {
        for item in self.nodes[&window].native_children.clone() {
            if self.nodes[&item].kind != WidgetKind::ToolbarItem {
                continue;
            }
            let Some(t) = self.nodes[&item].taffy else { continue };
            let available =
                taffy::Size { width: taffy::AvailableSpace::MaxContent, height: taffy::AvailableSpace::MaxContent };
            self.compute_layout(t, available);
        }
    }

    /// Redraws drawn custom widgets whose props, size or metrics changed,
    /// sending the display lists that changed along with the frames.
    fn update_drawings(&mut self) {
        let mut changed = Vec::new();
        for (id, node) in &mut self.nodes {
            if node.drawn_at == Some(node.frame.size) {
                continue;
            }
            let Some(drawing) = node.drawn().and_then(|c| c.draw(node.frame.size, &self.metrics)) else { continue };
            node.drawn_at = Some(node.frame.size);
            let drawing = Prop::Drawing(drawing);
            if node.prop(&drawing) != Some(&drawing) {
                changed.push((*id, drawing));
            }
        }
        for (id, prop) in changed {
            let node = self.nodes.get_mut(&id).unwrap();
            node.props.retain(|p| p.key() != prop.key());
            node.props.push(prop.clone());
            self.pending.push(Command::SetProp { id, prop });
        }
    }

    /// Sends the frames of `parent`'s native children that changed, and
    /// theirs. `origin` is where `parent` is in its layout's root,
    /// unrounded: a frame is rounded from where the node is there, as
    /// browsers round, so a node half a point down in a parent half a
    /// point down isn't a whole point off (each rounded on its own, a
    /// button centred on a row's middle sat a point low).
    fn collect_frames(&mut self, parent: NodeId, origin: Point) {
        match self.nodes[&parent].kind {
            WidgetKind::List | WidgetKind::Tree => return self.layout_list(parent),
            WidgetKind::Table => return self.layout_table(parent),
            _ => {}
        }
        for child in self.nodes[&parent].native_children.clone() {
            // A popover is laid out on its own at its natural size, as a
            // toolbar item is; where it goes is the platform's.
            if self.nodes[&child].kind == WidgetKind::Popover
                && let Some(t) = self.nodes[&child].taffy
            {
                let available =
                    taffy::Size { width: taffy::AvailableSpace::MaxContent, height: taffy::AvailableSpace::MaxContent };
                self.compute_layout(t, available);
            }
            let node = &self.nodes[&child];
            // Laid out on its own (a popover, a toolbar item): from its own
            // origin.
            let mut at = Point::ZERO;
            // A sidebar's frame is the platform's.
            if node.kind != WidgetKind::Sidebar
                && let Some(t) = node.taffy
                && let Ok(layout) = self.taffy.layout(t)
            {
                if self.taffy.parent(t).is_some() {
                    at = Point::new(origin.x + layout.location.x, origin.y + layout.location.y);
                }
                let (x, y) = (round(at.x) - round(origin.x), round(at.y) - round(origin.y));
                let width = round(at.x + layout.size.width) - round(at.x);
                let height = round(at.y + layout.size.height) - round(at.y);
                let frame = Rect::new(x, y, width, height);
                if frame != node.frame || !node.framed {
                    let node = self.nodes.get_mut(&child).unwrap();
                    (node.frame, node.framed) = (frame, true);
                    self.pending.push(Command::SetFrame { id: child, frame });
                }
            }
            self.collect_frames(child, at);
        }
    }
}

/// To the nearest point, halves down the page and to the end, as Taffy
/// rounds.
fn round(value: f32) -> f32 {
    (value + 0.5).floor()
}

fn space(s: taffy::AvailableSpace) -> AvailableSpace {
    match s {
        taffy::AvailableSpace::Definite(v) => AvailableSpace::Definite(v),
        taffy::AvailableSpace::MinContent => AvailableSpace::MinContent,
        taffy::AvailableSpace::MaxContent => AvailableSpace::MaxContent,
    }
}