Skip to main content

inset_rendering/
layout_helper.rs

1//! Flutter counterpart: `rendering/layout_helper.dart` (`ChildLayouter`,
2//! `ChildBaselineGetter`, `ChildLayoutHelper`).
3
4use inset_embedder::{Size, TextBaseline};
5use inset_foundation::App;
6
7use crate::box_::{AnyRenderBox, BoxConstraints};
8
9/// Signature for a function that takes a [`AnyRenderBox`] and returns the [`Size`] that the box
10/// would have if it were laid out with the given [`BoxConstraints`].
11///
12/// [`ChildLayoutHelper::dry_layout_child`] and [`ChildLayoutHelper::layout_child`] adhere to
13/// this signature.
14pub type ChildLayouter = fn(&mut App, AnyRenderBox, BoxConstraints) -> Size;
15
16/// Signature for a function that takes a [`AnyRenderBox`] and returns the baseline offset this
17/// box would have if it were laid out with the given [`BoxConstraints`].
18///
19/// [`ChildLayoutHelper::get_dry_baseline`] and [`ChildLayoutHelper::get_baseline`] adhere to
20/// this signature.
21pub type ChildBaselineGetter =
22    fn(&mut App, AnyRenderBox, BoxConstraints, TextBaseline) -> Option<f64>;
23
24/// A collection of functions to lay a box child out with the given set of [`BoxConstraints`].
25///
26/// All of the functions adhere to the [`ChildLayouter`] or [`ChildBaselineGetter`] signature.
27pub struct ChildLayoutHelper;
28
29impl ChildLayoutHelper {
30    /// Returns the [`Size`] that the box would have if it were to be laid out with the given
31    /// [`BoxConstraints`].
32    ///
33    /// This method calls [`AnyRenderBox::get_dry_layout`] on the given box.
34    ///
35    /// This method should only be called by the parent of the provided box child as it binds
36    /// parent and child together (if the child is marked as dirty, the child will also be marked
37    /// as dirty).
38    pub fn dry_layout_child(
39        app: &mut App,
40        child: AnyRenderBox,
41        constraints: BoxConstraints,
42    ) -> Size {
43        child.get_dry_layout(app, constraints)
44    }
45
46    /// Lays out the box with the given constraints and returns its [`Size`].
47    ///
48    /// This method calls [`AnyRenderBox::layout`] on the given box with `parent_uses_size` set
49    /// to true to receive its [`Size`].
50    pub fn layout_child(app: &mut App, child: AnyRenderBox, constraints: BoxConstraints) -> Size {
51        child.layout(app, constraints, true);
52        child.size(app)
53    }
54
55    /// Convenience function that calls [`AnyRenderBox::get_dry_baseline`].
56    pub fn get_dry_baseline(
57        app: &mut App,
58        child: AnyRenderBox,
59        constraints: BoxConstraints,
60        baseline: TextBaseline,
61    ) -> Option<f64> {
62        child.get_dry_baseline(app, constraints, baseline)
63    }
64
65    /// Convenience function that calls [`AnyRenderBox::get_distance_to_baseline`].
66    ///
67    /// The given `child` must be already laid out with `constraints`.
68    pub fn get_baseline(
69        app: &mut App,
70        child: AnyRenderBox,
71        constraints: BoxConstraints,
72        baseline: TextBaseline,
73    ) -> Option<f64> {
74        debug_assert!(!child.as_object().debug_needs_layout(app));
75        debug_assert!(child.constraints(app) == constraints);
76        child.get_distance_to_baseline(app, baseline, true)
77    }
78}