polyhorn_ui/layout/algorithm/
mod.rs

1#[cfg(feature = "layout-stretch")]
2pub mod stretch;
3
4/// Flexbox implementation powered by Yoga (the default).
5#[cfg(feature = "layout-yoga")]
6pub mod yoga;
7
8#[cfg(feature = "layout-stretch")]
9pub use self::stretch::*;
10
11#[cfg(feature = "layout-yoga")]
12pub use self::yoga::*;
13
14use std::hash::Hash;
15
16use crate::geometry::Dimension;
17use crate::styles::ViewStyle;
18
19use super::{Layout, MeasureFunc, Size};
20
21/// Abstract interface of a flexbox algorithm.
22pub trait Algorithm {
23    /// Represents the type of a node within this flexbox.
24    type Node: Copy + Clone + Hash + Eq;
25
26    /// Create a new instance of the flexbox (i.e. root).
27    fn new() -> Self;
28
29    /// Create a new node with the given child nodes.
30    fn new_node(&mut self, style: ViewStyle, children: &[Self::Node]) -> Self::Node;
31
32    /// Create a new leaf with an intrinsic content size that is determined by
33    /// the given measure function.
34    fn new_leaf(&mut self, style: ViewStyle, measure: MeasureFunc) -> Self::Node;
35
36    /// Add a child node to a parent node.
37    fn add_child(&mut self, parent: Self::Node, child: Self::Node);
38
39    /// Remove a child node from a parent node.
40    fn remove_child(&mut self, parent: Self::Node, child: Self::Node);
41
42    /// Returns the number of child nodes of a specific parent node.
43    fn child_count(&self, parent: Self::Node) -> usize;
44
45    /// Removes the given node from this flexbox.
46    fn remove(&mut self, node: Self::Node);
47
48    /// Updates the style of a specific node within this flexbox.
49    fn set_style(&mut self, node: Self::Node, style: ViewStyle);
50
51    /// Updates the measure function of a specific node within this flexbox.
52    fn set_measure(&mut self, node: Self::Node, measure: MeasureFunc);
53
54    /// Computes the layout of a node within this flexbox within the given
55    /// size.
56    fn compute_layout(&mut self, node: Self::Node, size: Size<Dimension<f32>>);
57
58    /// Returns the computed layout of a node within this flexbox.
59    fn layout(&self, node: Self::Node) -> Layout;
60}