Skip to main content

xrack_runtime/
split.rs

1use crate::{Node, Value, is_window_overlay};
2use std::sync::Arc;
3
4impl Node {
5    pub fn split_pane_count(&self) -> usize {
6        self.children
7            .iter()
8            .position(|node| node.kind == "SplitHandle")
9            .unwrap_or(self.children.len())
10    }
11
12    pub fn split_panes(&self) -> impl Iterator<Item = (usize, &Node)> {
13        self.children[..self.split_pane_count()]
14            .iter()
15            .enumerate()
16            .filter(|(_, pane)| {
17                pane.is_visible()
18                    && !is_window_overlay(pane.kind)
19                    && !matches!(pane.kind, "Menu" | "Tooltip")
20            })
21    }
22
23    #[doc(hidden)]
24    pub fn add_split_handles(&mut self) {
25        let orientation = self.orientation();
26        let color = self.color("handle-color");
27        let count = self.children.len().saturating_sub(1);
28        self.children.extend((0..count).map(|_| {
29            let mut handle = Node::new("SplitHandle", None);
30            handle
31                .properties
32                .insert("orientation", Value::Orientation(orientation));
33            if let Some(color) = color {
34                handle.properties.insert("color", Value::Color(color));
35            }
36            handle
37        }));
38    }
39
40    #[doc(hidden)]
41    pub fn resized_split_weights(&self, weights: &[(usize, f32)]) -> Option<Arc<[Option<f32>]>> {
42        if self.kind != "SplitView"
43            || weights.len() < 2
44            || self
45                .split_panes()
46                .map(|(index, _)| index)
47                .ne(weights.iter().map(|(index, _)| *index))
48            || weights
49                .iter()
50                .any(|(_, value)| !value.is_finite() || *value < 0.0)
51        {
52            return None;
53        }
54        let sum: f64 = weights.iter().map(|(_, value)| f64::from(*value)).sum();
55        if sum <= 0.0 {
56            return None;
57        }
58        let mut next = self
59            .split_weights
60            .as_deref()
61            .map_or_else(Vec::new, <[_]>::to_vec);
62        next.resize(self.split_pane_count(), None);
63        for &(index, weight) in weights {
64            next[index] = Some(weight);
65        }
66        (self.split_weights.as_deref() != Some(next.as_slice())).then(|| Arc::from(next))
67    }
68}