Skip to main content

xrack_layout/
tree.rs

1use crate::{LayoutNode, Rect, place_in_direction};
2use xrack_runtime::{LayoutDirection, Node};
3
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct TreeRowGeometry {
6    pub indicator: Rect,
7    pub content: Rect,
8}
9
10impl TreeRowGeometry {
11    pub fn new(node: &Node, bounds: Rect) -> Option<Self> {
12        Self::with_layout_direction(node, bounds, node.layout_direction().unwrap_or_default())
13    }
14
15    pub fn for_layout(node: &Node, layout: &LayoutNode) -> Option<Self> {
16        Self::with_layout_direction(node, layout.bounds, layout.layout_direction)
17    }
18
19    pub(crate) fn with_layout_direction(
20        node: &Node,
21        bounds: Rect,
22        direction: LayoutDirection,
23    ) -> Option<Self> {
24        let depth = node.int("tree-depth")?.max(0) as f32;
25        let indentation = node
26            .length("tree-indentation")
27            .unwrap_or(16.0)
28            .clamp(0.0, 64.0);
29        let indicator_width = 22.0_f32.min(bounds.width);
30        let indent =
31            (4.0 + depth * indentation).min((bounds.width - indicator_width - 16.0).max(0.0));
32        let indicator = Rect::new(bounds.x + indent, bounds.y, indicator_width, bounds.height);
33        let content = Rect::new(
34            bounds.x + indent + indicator_width,
35            bounds.y,
36            (bounds.width - indent - indicator_width).max(0.0),
37            bounds.height,
38        );
39        Some(Self {
40            indicator: place_in_direction(indicator, bounds, direction),
41            content: place_in_direction(content, bounds, direction),
42        })
43    }
44}