Skip to main content

bhoo/systems/material/
split_pane.rs

1//! Resizable two-pane SplitPane container with pointer & keyboard drag divider.
2use crate::*;
3
4/// Orientation of a `SplitPane` divider split.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub enum SplitPaneOrientation {
7    #[default]
8    Horizontal,
9    Vertical,
10}
11
12/// Configuration for `Frame::split_pane`.
13#[derive(Clone, Copy, Debug)]
14pub struct SplitPaneOptions {
15    pub orientation: SplitPaneOrientation,
16    pub min_ratio: f32,
17    pub max_ratio: f32,
18    pub handle_thickness: f32,
19    pub enabled: bool,
20}
21
22impl Default for SplitPaneOptions {
23    fn default() -> Self {
24        Self {
25            orientation: SplitPaneOrientation::Horizontal,
26            min_ratio: 0.2,
27            max_ratio: 0.8,
28            handle_thickness: 12.0,
29            enabled: true,
30        }
31    }
32}
33
34impl Frame<'_> {
35    /// Two-pane resizable split container with pointer-draggable and arrow-key-adjustable divider.
36    /// `ratio` is clamped to `options.min_ratio..=options.max_ratio`.
37    pub fn split_pane(
38        &mut self,
39        id: u64,
40        height: f32,
41        ratio: &mut f32,
42        options: SplitPaneOptions,
43        first: impl FnOnce(&mut Self),
44        second: impl FnOnce(&mut Self),
45    ) -> Response {
46        let t = self.theme();
47        let bounds = self.reserve(height.max(0.0));
48        let lo = options.min_ratio.clamp(0.05, 0.95);
49        let hi = options.max_ratio.clamp(lo, 0.95);
50        *ratio = ratio.clamp(lo, hi);
51        let handle_t = options.handle_thickness.max(4.0);
52
53        let (first_rect, handle_rect, second_rect) = match options.orientation {
54            SplitPaneOrientation::Horizontal => {
55                let usable = (bounds.width - handle_t).max(0.0);
56                let first_w = usable * *ratio;
57                let second_w = (usable - first_w).max(0.0);
58                (
59                    Rect { width: first_w, ..bounds },
60                    Rect { x: bounds.x + first_w, width: handle_t, ..bounds },
61                    Rect { x: bounds.x + first_w + handle_t, width: second_w, ..bounds },
62                )
63            }
64            SplitPaneOrientation::Vertical => {
65                let usable = (bounds.height - handle_t).max(0.0);
66                let first_h = usable * *ratio;
67                let second_h = (usable - first_h).max(0.0);
68                (
69                    Rect { height: first_h, ..bounds },
70                    Rect { y: bounds.y + first_h, height: handle_t, ..bounds },
71                    Rect { y: bounds.y + first_h + handle_t, height: second_h, ..bounds },
72                )
73            }
74        };
75
76        self.within(first_rect, first);
77        let mut pct = *ratio * 100.0;
78        let vertical_drag = options.orientation == SplitPaneOrientation::Vertical;
79        let mut r = self.drag_handle(
80            id,
81            handle_rect,
82            bounds,
83            "Resize split pane",
84            &mut pct,
85            lo * 100.0..=hi * 100.0,
86            vertical_drag,
87        );
88        let next_ratio = (pct / 100.0).clamp(lo, hi);
89        if (next_ratio - *ratio).abs() > 1e-4 {
90            *ratio = next_ratio;
91            r.changed = true;
92        }
93        let pill_color = if r.pressed {
94            t.primary
95        } else if r.hovered {
96            t.on_surface_variant
97        } else {
98            t.outline_variant
99        };
100        let pill = match options.orientation {
101            SplitPaneOrientation::Horizontal => Rect {
102                x: handle_rect.x + (handle_rect.width - 4.0) * 0.5,
103                y: handle_rect.y + (handle_rect.height - 32.0).max(0.0) * 0.5,
104                width: 4.0,
105                height: 32.0_f32.min(handle_rect.height),
106            },
107            SplitPaneOrientation::Vertical => Rect {
108                x: handle_rect.x + (handle_rect.width - 32.0).max(0.0) * 0.5,
109                y: handle_rect.y + (handle_rect.height - 4.0) * 0.5,
110                width: 32.0_f32.min(handle_rect.width),
111                height: 4.0,
112            },
113        };
114        self.paint_rect(pill, 2.0, pill_color);
115        self.within(second_rect, second);
116        r
117    }
118}
119
120#[cfg(test)]
121#[path = "split_pane.spec.rs"]
122mod tests;