masonry_core 0.5.0

Traits and types of the Masonry toolkit.
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
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
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

//! The layout pass, which provides the size and position of each widget
//! before any translations applied in [`compose`](crate::passes::compose).
//!
//! The framework helps orchestrate the size computation alongside [`Widget::measure`].
//! The layout size is passed to [`Widget::layout`].

use dpi::LogicalSize;
use tracing::{info_span, trace};
use tree_arena::ArenaMut;

use crate::app::{RenderRoot, RenderRootSignal, RenderRootState, WindowSizePolicy};
use crate::core::{
    ChildrenIds, LayoutCtx, MeasureCtx, PropertiesRef, PropertyArena, Widget, WidgetArenaNode,
    WidgetState,
};
use crate::kurbo::{Axis, Insets, Point, Size};
use crate::layout::{LayoutSize, LenDef, LenReq, Length, MeasurementInputs, SizeDef};
use crate::passes::{enter_span_if, recurse_on_children};
use crate::properties::{BorderWidth, BoxShadow, Dimensions, Padding};
use crate::util::Sanitize;

// --- MARK: COMPUTE SIZE

/// Measures the preferred border-box length of `widget` on the given `axis`.
fn measure_border_box(
    widget: &mut dyn Widget,
    ctx: &mut MeasureCtx<'_>,
    props: &PropertiesRef<'_>,
    axis: Axis,
    len_req: LenReq,
    cross_length: Option<Length>,
) -> Length {
    let cache = ctx.property_cache();
    let border = props.get::<BorderWidth>(cache);
    let padding = props.get::<Padding>(cache);

    let border_and_padding_length = border.length(axis).saturating_add(padding.length(axis));

    // Reduce the border-box length by the border and padding length to get the content-box length.
    let len_req = len_req.reduce(border_and_padding_length);
    let cross_length = cross_length.map(|cross_length| {
        let cross = axis.cross();
        cross_length
            .saturating_sub(border.length(cross))
            .saturating_sub(padding.length(cross))
    });

    // Measure the content-box length.
    let content_length = widget.measure(ctx, props, axis, len_req, cross_length);

    // Add border and padding to the content-box length to return the border-box length.
    content_length.saturating_add(border_and_padding_length)
}

/// Resolves the [`LenDef`] of the provided `axis`.
///
/// Unless `Fixed`, this will result in a [`measure`] invocation.
///
/// [`measure`]: Widget::measure
fn resolve_len_def(
    widget: &mut dyn Widget,
    ctx: &mut MeasureCtx<'_>,
    props: &PropertiesRef<'_>,
    axis: Axis,
    len_def: LenDef,
    cross_length: Option<Length>,
) -> Length {
    let len_req = match len_def {
        LenDef::Fixed(val) => return val,
        LenDef::MinContent => LenReq::MinContent,
        LenDef::MaxContent => LenReq::MaxContent,
        LenDef::FitContent(space) => LenReq::FitContent(space),
    };

    let inputs = MeasurementInputs::new(axis, len_req, cross_length);
    let cached_result = ctx.widget_state.measurement_cache.get(&inputs);

    #[cfg(debug_assertions)]
    let result = {
        // With debug assertions enabled, we will always measure regardless of cache.
        let result = measure_border_box(widget, ctx, props, axis, len_req, cross_length);
        // If the cache did have the result, we verify that it matches.
        if let Some(cached_result) = cached_result {
            if cached_result != result {
                panic!(
                    "Widget '{}' {} measurement for {inputs:?} returned {result} \
                    but cache already had {cached_result}. Widget::measure needs \
                    to always return the same result for the same inputs. If some other data \
                    changed which influenced the results, you need to request layout!",
                    widget.short_type_name(),
                    ctx.widget_id(),
                );
            }
            return cached_result;
        }
        result
    };
    #[cfg(not(debug_assertions))]
    let result = {
        if let Some(cached_result) = cached_result {
            return cached_result;
        }
        measure_border_box(widget, ctx, props, axis, len_req, cross_length)
    };

    if ctx.cache_result {
        ctx.widget_state.measurement_cache.insert(inputs, result);
    }

    result
}

/// Resolves the widget's preferred border-box length on the given `axis`.
///
/// `auto_length` specifies the fallback behavior if a widget's dimension is [`Dim::Auto`].
///
/// # Panics
///
/// Panics if a dimension resolves to a non-finite or negative value
/// and debug assertions are enabled. This can happen if the involved numbers are huge,
/// e.g. a logical size of `f64::MAX` scaled by `1.5`.
///
/// [`Dim::Auto`]: crate::layout::Dim::Auto
pub(crate) fn resolve_length(
    global_state: &mut RenderRootState,
    property_arena: &PropertyArena,
    node: ArenaMut<'_, WidgetArenaNode>,
    auto_length: LenDef,
    context_size: LayoutSize,
    axis: Axis,
    cross_length: Option<Length>,
) -> Length {
    // Get the dimensions
    let class_set = &node.item.class_set;
    let cache = &mut node.item.state.property_cache;
    let widget = &mut *node.item.widget;
    let stack = property_arena.get(node.item.state.property_stack_id, widget.type_id());
    let props = PropertiesRef {
        local: &node.item.properties,
        default_map: property_arena
            .default_properties
            .for_widget(widget.type_id()),
        stack,
        class_set,
    };
    let dims = props.get::<Dimensions>(cache);

    // Resolve the dimension on the given axis
    let len_def = dims
        .dim(axis)
        .resolve(context_size.length(axis))
        .unwrap_or(auto_length);

    // Return immediately if we already have a fixed length
    if let LenDef::Fixed(length) = len_def {
        return length;
    }

    // Otherwise fall back to measurement
    let mut children = node.children;
    let mut ctx = MeasureCtx {
        global_state,
        widget_state: &mut node.item.state,
        children: children.reborrow_mut(),
        property_arena,
        auto_length,
        context_size,
        cache_result: true,
    };

    // Resolve the cross dimension in case it's fixed
    let cross_length = cross_length.or_else(|| {
        let cross = axis.cross();
        dims.dim(cross)
            .resolve(context_size.length(cross))
            .and_then(|cross_len_def| cross_len_def.fixed())
    });

    // Measure
    resolve_len_def(widget, &mut ctx, &props, axis, len_def, cross_length)
}

/// Resolves the widget's preferred border-box size.
///
/// The returned size will be finite, non-negative, and in logical pixels.
///
/// `auto_size` specifies the fallback behavior if a widget's dimension is [`Dim::Auto`].
///
/// # Panics
///
/// Panics if a dimension resolves to a non-finite or negative value
/// and debug assertions are enabled. This can happen if the involved numbers are huge,
/// e.g. a logical size of `f64::MAX` scaled by `1.5`.
///
/// [`Dim::Auto`]: crate::layout::Dim::Auto
pub(crate) fn resolve_size(
    global_state: &mut RenderRootState,
    property_arena: &PropertyArena,
    node: ArenaMut<'_, WidgetArenaNode>,
    auto_size: SizeDef,
    context_size: LayoutSize,
) -> Size {
    // Currently we only support the common horizontal-tb writing mode,
    // so the assignments are hardcoded here, but the rest of the function adapts.
    let (inline, block) = (Axis::Horizontal, Axis::Vertical);

    // Get the dimensions
    let class_set = &node.item.class_set;
    let cache = &mut node.item.state.property_cache;
    let widget = &mut *node.item.widget;
    let stack = property_arena.get(node.item.state.property_stack_id, widget.type_id());
    let props = PropertiesRef {
        local: &node.item.properties,
        default_map: property_arena
            .default_properties
            .for_widget(widget.type_id()),
        stack,
        class_set,
    };
    let dims = props.get::<Dimensions>(cache);

    // Resolve the dimensions
    let inline_auto = auto_size.dim(inline);
    let inline_def = dims
        .dim(inline)
        .resolve(context_size.length(inline))
        .unwrap_or(inline_auto);
    let block_auto = auto_size.dim(block);
    let block_def = dims
        .dim(block)
        .resolve(context_size.length(block))
        .unwrap_or(block_auto);

    // Return immediately if we already have a fixed size
    let inline_length = inline_def.fixed();
    let block_length = block_def.fixed();
    if let Some(inline_length) = inline_length
        && let Some(block_length) = block_length
    {
        return inline.pack_size(inline_length.get(), block_length.get());
    }

    // Otherwise fall back to measurement
    let mut children = node.children;
    let mut ctx = MeasureCtx {
        global_state,
        widget_state: &mut node.item.state,
        children: children.reborrow_mut(),
        property_arena,
        auto_length: inline_auto,
        context_size,
        cache_result: true,
    };

    let inline_length = inline_length.unwrap_or_else(|| {
        resolve_len_def(widget, &mut ctx, &props, inline, inline_def, block_length)
    });

    // Update the auto length
    ctx.auto_length = block_auto;

    // Even if the inline measurement couldn't cache, the block one might be able to.
    ctx.cache_result = true;

    let block_length = block_length.unwrap_or_else(|| {
        resolve_len_def(
            widget,
            &mut ctx,
            &props,
            block,
            block_def,
            Some(inline_length),
        )
    });

    inline.pack_size(inline_length.get(), block_length.get())
}

// --- MARK: RUN LAYOUT
/// Run [`Widget::layout`] method on the given widget.
/// This will be called by [`LayoutCtx::run_layout`], which is itself called in the parent widget's `layout`.
///
/// The provided `size` will be the given widget's chosen border-box size.
///
/// If the chosen border-box `size` is smaller than what is required to fit the widget's
/// borders and padding, then the `size` will be expanded to meet those constraints.
///
/// The provided `size` must be finite, non-negative, and in logical pixels.
/// Non-finite or negative length will fall back to zero with a logged warning.
///
/// # Panics
///
/// Panics if `size` is non-finite or negative and debug assertions are enabled.
///
/// [`Widget::layout`]: crate::core::Widget::layout
pub(crate) fn run_layout_on(
    global_state: &mut RenderRootState,
    property_arena: &PropertyArena,
    node: ArenaMut<'_, WidgetArenaNode>,
    chosen_size: Size,
) {
    // Ensure the chosen size is sanitized.
    let chosen_size = Size::new(
        chosen_size.width.sanitize("chosen border-box size width"),
        chosen_size.height.sanitize("chosen border-box size height"),
    );

    let mut children = node.children;
    let widget = &mut *node.item.widget;
    let state = &mut node.item.state;
    let properties = &mut node.item.properties;
    let class_set = &node.item.class_set;
    let id = state.id;
    let trace = global_state.trace.layout;
    let _span = enter_span_if(trace, state);

    // This checks reads `is_explicitly_stashed` instead of `is_stashed` because the latter may be outdated.
    // A widget's `is_explicitly_stashed` flag is controlled by its direct parent.
    // The parent may set this flag during layout, in which case it should avoid calling `run_layout`.
    // Note that, because this check exits before recursing, `run_layout` can only ever be
    // reached for a widget whose parent is not stashed, which means `is_explicitly_stashed`
    // being false is sufficient to know the widget is non-stashed.
    if state.is_explicitly_stashed {
        debug_panic!(
            "Error in '{}' {}: trying to compute layout of stashed widget.",
            widget.short_type_name(),
            id,
        );
        state.origin = Point::ZERO;
        state.end_point = Point::ZERO;
        state.layout_border_box_size = Size::ZERO;
        return;
    }

    let stack = property_arena.get(state.property_stack_id, widget.type_id());
    let props = PropertiesRef {
        local: properties,
        default_map: property_arena
            .default_properties
            .for_widget(widget.type_id()),
        stack,
        class_set,
    };

    let border_width = props.get::<BorderWidth>(&mut state.property_cache);
    let padding = props.get::<Padding>(&mut state.property_cache);

    // Force the border-box size to be large enough to actually contain the border and padding.
    let minimum_size = Size::ZERO;
    let minimum_size = border_width.size_up(minimum_size);
    let minimum_size = padding.size_up(minimum_size);
    let border_box_size = minimum_size.max(chosen_size);

    if !state.needs_layout() && state.layout_border_box_size == border_box_size {
        // We reset this to false to mark that the current widget has been visited.
        state.request_layout = false;
        return;
    }
    state.layout_border_box_size = border_box_size;

    // TODO - Not everything that has been re-laid out needs to be repainted.
    state.needs_paint = true;
    state.needs_compose = true;
    state.needs_accessibility = true;
    state.request_pre_paint = true;
    state.request_paint = true;
    state.request_post_paint = true;
    state.request_compose = true;
    state.request_accessibility = true;

    if trace {
        trace!(
            "Computing layout with border-box size {:?}",
            border_box_size
        );
    }

    // Again, this block reads `is_explicitly_stashed` instead of `is_stashed` because the latter
    // may be outdated if layout code has called `set_stashed`.
    let mut children_ids = ChildrenIds::new();
    if cfg!(debug_assertions) {
        children_ids = widget.children_ids();

        // We forcefully set request_layout to true for all children.
        // This is used below to check that widget.layout(..) visited all of them.
        for child_id in widget.children_ids() {
            let child_state = &mut children.item_mut(child_id).unwrap().item.state;
            if !child_state.is_explicitly_stashed {
                child_state.request_layout = true;
            }
        }
    }

    state.paint_box_insets = Insets::ZERO;

    // Compute the insets for deriving the content-box from the border-box
    let border_box_insets = border_width.insets_up(Insets::ZERO);
    let border_box_insets = padding.insets_up(border_box_insets);
    state.border_box_insets = border_box_insets;

    // Compute the content-box size
    let content_box_size = border_width.size_down(border_box_size);
    let content_box_size = padding.size_down(content_box_size);

    let mut ctx = LayoutCtx {
        global_state,
        widget_state: state,
        children: children.reborrow_mut(),
        property_arena,
    };

    // Run the widget's layout
    widget.layout(&mut ctx, &props, content_box_size);

    // If children are stashed, the layout pass will not recurse over them.
    // We reset need_layout and request_layout to false directly instead.
    //
    // See the notes about `is_explicitly_stashed` above.
    recurse_on_children(id, widget, children.reborrow_mut(), |node| {
        if node.item.state.is_explicitly_stashed {
            clear_layout_flags(node);
        }
    });

    // Make sure the paint insets cover the shadow insets
    let shadow = props.get::<BoxShadow>(&mut state.property_cache);
    if shadow.is_visible() {
        let shadow_insets = shadow.get_insets();
        state.paint_box_insets = Insets {
            x0: state.paint_box_insets.x0.max(shadow_insets.x0),
            y0: state.paint_box_insets.y0.max(shadow_insets.y0),
            x1: state.paint_box_insets.x1.max(shadow_insets.x1),
            y1: state.paint_box_insets.y1.max(shadow_insets.y1),
        };
    }

    if trace {
        trace!(
            "Computed layout: border-box={}, first_baseline={}, last_baseline={} insets={:?}",
            border_box_size, state.first_baseline, state.last_baseline, state.paint_box_insets,
        );
    }

    state.request_layout = false;
    state.set_needs_layout(false);
    state.is_expecting_place_child_call = true;

    #[cfg(debug_assertions)]
    {
        let name = widget.short_type_name();
        for child_id in widget.children_ids() {
            let child_state = &mut children.item_mut(child_id).unwrap().item.state;

            if child_state.is_explicitly_stashed {
                // Above, we forcefully set `request_layout` for unstashed children, but children
                // may have become stashed in the meantime. Unset the flag for them.
                child_state.request_layout = false;
                continue;
            }

            if child_state.request_layout {
                debug_panic!(
                    "Error in '{}' {}: LayoutCtx::run_layout() was not called with child widget '{}' {}.",
                    name,
                    id,
                    child_state.widget_name,
                    child_state.id,
                );
            }

            if child_state.is_expecting_place_child_call {
                debug_panic!(
                    "Error in '{}' {}: LayoutCtx::place_child() was not called with child widget '{}' {}.",
                    name,
                    id,
                    child_state.widget_name,
                    child_state.id,
                );
            }
        }

        let new_children_ids = widget.children_ids();
        if children_ids != new_children_ids && !state.children_changed {
            debug_panic!(
                "Error in '{}' {}: children changed during layout pass",
                name,
                id,
            );
        }
    }
}

// --- MARK: CLEAR LAYOUT
// This function is called on stashed widgets and their children
// to set all layout flags to false.
fn clear_layout_flags(node: ArenaMut<'_, WidgetArenaNode>) {
    let children = node.children;
    let widget = &mut *node.item.widget;
    let state = &mut node.item.state;
    let id = state.id;

    // If `needs_layout` is false, no descendant has layout flags set.
    if !state.needs_layout() {
        return;
    }

    state.set_needs_layout(false);
    state.request_layout = false;

    recurse_on_children(id, widget, children, |node| {
        clear_layout_flags(node);
    });
}

// --- MARK: PLACE WIDGET
/// Places the child at `origin` in its parent's border-box coordinate space.
pub(crate) fn place_widget(child_state: &mut WidgetState, origin: Point) {
    let end_point = origin + child_state.layout_border_box_size.to_vec2();
    // TODO - Account for display scale in pixel snapping
    // See https://github.com/linebender/xilem/issues/1264
    let origin = origin.round();
    let end_point = end_point.round();

    // TODO - We may want to invalidate in other cases as well
    if origin != child_state.origin {
        child_state.transform_changed = true;
        child_state.needs_compose = true;
    }
    child_state.origin = origin;
    child_state.end_point = end_point;

    child_state.is_expecting_place_child_call = false;
}

// --- MARK: ROOT
/// See the [passes documentation](crate::doc::pass_system#layout-pass).
pub(crate) fn run_layout_pass(root: &mut RenderRoot) {
    if !root.root_state().needs_layout() {
        return;
    }

    let _span = info_span!("layout").entered();
    root.global_state.needs_pointer_pass = true;

    let window_size = root.get_kurbo_size();
    let mut root_node = root.widget_arena.get_node_mut(root.root_id());
    let root_node_size = match root.global_state.size_policy {
        WindowSizePolicy::User => resolve_size(
            &mut root.global_state,
            &root.property_arena,
            root_node.reborrow_mut(),
            SizeDef::fixed(window_size),
            window_size.into(),
        ),
        WindowSizePolicy::Content => resolve_size(
            &mut root.global_state,
            &root.property_arena,
            root_node.reborrow_mut(),
            SizeDef::MAX,
            LayoutSize::NONE,
        ),
    };

    run_layout_on(
        &mut root.global_state,
        &root.property_arena,
        root_node.reborrow_mut(),
        root_node_size,
    );
    place_widget(&mut root_node.item.state, Point::ORIGIN);

    if let WindowSizePolicy::Content = root.global_state.size_policy {
        // We use the aligned border-box size, which means that transforms won't affect window size.
        let size = root_node.item.state.border_box().size();
        let new_size =
            LogicalSize::new(size.width, size.height).to_physical(root.global_state.scale_factor);
        if root.global_state.size != new_size {
            root.global_state.size = new_size;
            root.global_state
                .emit_signal(RenderRootSignal::SetSize(new_size));
        }
    }
}