Skip to main content

uzor_framework/widgets/
splitter.rs

1//! A draggable pane separator (the DOM "dragger"): a thin line with a wider
2//! grab strip. A press on the strip starts the drag; while the button is
3//! held the pane edge follows the pointer (the engine captures the pointer
4//! for the drag-sensitive strip); the release ends it. The app owns the
5//! sizes, the splitter only reports where the edge should go.
6
7use uzor::input::{InputCoordinator, LayerId, Sense, WidgetKind};
8use uzor::{Rect, WidgetId};
9
10use crate::handle::VisualView;
11use crate::types::spec::Spec;
12
13/// The direction the separator moves in.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum Axis {
16    /// A vertical line, dragged left / right.
17    X,
18    /// A horizontal line, dragged up / down.
19    Y,
20}
21
22/// What a frame of the splitter yields.
23#[derive(Clone, Copy, Debug, PartialEq)]
24pub enum SplitterEvent {
25    /// Not dragging.
26    Idle,
27    /// Dragging: the pointer coordinate along the axis.
28    Drag(f64),
29    /// The drag ended this frame (persist the size now).
30    Released,
31}
32
33/// Per-separator drag state.
34#[derive(Clone, Debug, Default)]
35pub struct Splitter {
36    dragging: bool,
37}
38
39impl Splitter {
40    /// Advance one frame from the view: a press on `id` with the button
41    /// down starts a drag; the drag follows the pointer until release.
42    pub fn update<S: Spec>(&mut self, view: &VisualView<S>, id: &WidgetId, axis: Axis) -> SplitterEvent {
43        if view.is_pressed(id) && view.button_down() {
44            self.dragging = true;
45        }
46        if !self.dragging {
47            return SplitterEvent::Idle;
48        }
49        if !view.button_down() {
50            self.dragging = false;
51            return SplitterEvent::Released;
52        }
53        match (view.pointer(), axis) {
54            (Some((x, _)), Axis::X) => SplitterEvent::Drag(x),
55            (Some((_, y)), Axis::Y) => SplitterEvent::Drag(y),
56            (None, _) => SplitterEvent::Idle,
57        }
58    }
59
60    /// A drag is in progress.
61    pub fn dragging(&self) -> bool {
62        self.dragging
63    }
64
65    /// Paint the line highlighted: while dragged, or hovered while no
66    /// other button is held.
67    pub fn hot<S: Spec>(&self, view: &VisualView<S>, id: &WidgetId) -> bool {
68        self.dragging || (!view.button_down() && view.is_hovered(id))
69    }
70}
71
72/// The grab strip of a line at `pos` across `span` (`grab` px wide,
73/// centred on the 1 px line, like the 9 px `.rz` hit of a 1 px border).
74pub fn grab_rect(axis: Axis, pos: f64, span: Rect, grab: f64) -> Rect {
75    let h = (grab - 1.0) / 2.0;
76    match axis {
77        Axis::X => Rect::new(pos - h, span.y, grab, span.height),
78        Axis::Y => Rect::new(span.x, pos - h, span.width, grab),
79    }
80}
81
82/// Register the grab strip (a drag-sensitive `Splitter`, resize cursor).
83pub fn register(coord: &mut InputCoordinator, id: impl Into<WidgetId>, rect: Rect, layer: &LayerId) {
84    coord.register_atomic(id.into(), WidgetKind::Splitter, rect, Sense::CLICK_AND_DRAG, layer);
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90    use crate::types::spec::{PanelHome, Spec};
91    use uzor::layout::docking::DockPanel;
92
93    #[derive(Clone, Debug)]
94    struct P;
95    impl DockPanel for P {
96        fn title(&self) -> &str {
97            "p"
98        }
99        fn type_id(&self) -> &'static str {
100            "p"
101        }
102    }
103    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
104    enum Tag {}
105    struct TestSpec;
106    impl Spec for TestSpec {
107        type Panel = P;
108        type Overlay = Tag;
109        type Action = Tag;
110        fn decode_panel(_home: PanelHome, _type_id: &str) -> Option<P> {
111            None
112        }
113    }
114
115    #[test]
116    fn press_drag_release() {
117        let id = WidgetId::from("split");
118        let mut s = Splitter::default();
119        let mut v = VisualView::<TestSpec>::default();
120        v.pointer = Some((100.0, 5.0));
121        v.hovered = Some(id.clone());
122        assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Idle);
123        assert!(s.hot(&v, &id));
124        v.pressed = Some(id.clone());
125        v.button_down = true;
126        assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Drag(100.0));
127        // The pointer leaves the strip and the press: the drag keeps going.
128        v.pressed = None;
129        v.hovered = None;
130        v.pointer = Some((140.0, 30.0));
131        assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Drag(140.0));
132        assert_eq!(s.update(&v, &id, Axis::Y), SplitterEvent::Drag(30.0));
133        assert!(s.hot(&v, &id));
134        v.button_down = false;
135        assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Released);
136        assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Idle);
137        assert!(!s.hot(&v, &id));
138    }
139
140    #[test]
141    fn grab_strip_centres_on_the_line() {
142        let span = Rect::new(0.0, 10.0, 300.0, 200.0);
143        assert_eq!(grab_rect(Axis::X, 50.0, span, 9.0), Rect::new(46.0, 10.0, 9.0, 200.0));
144        assert_eq!(grab_rect(Axis::Y, 50.0, span, 9.0), Rect::new(0.0, 46.0, 300.0, 9.0));
145    }
146}