1use uzor::input::{InputCoordinator, LayerId, Sense, WidgetKind};
8use uzor::{Rect, WidgetId};
9
10use crate::handle::VisualView;
11use crate::types::spec::Spec;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum Axis {
16 X,
18 Y,
20}
21
22#[derive(Clone, Copy, Debug, PartialEq)]
24pub enum SplitterEvent {
25 Idle,
27 Drag(f64),
29 Released,
31}
32
33#[derive(Clone, Debug, Default)]
35pub struct Splitter {
36 dragging: bool,
37}
38
39impl Splitter {
40 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.raw_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 pub fn dragging(&self) -> bool {
62 self.dragging
63 }
64
65 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
72pub 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
82pub 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 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}