use uzor::input::{InputCoordinator, LayerId, Sense, WidgetKind};
use uzor::{Rect, WidgetId};
use crate::handle::VisualView;
use crate::types::spec::Spec;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
X,
Y,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SplitterEvent {
Idle,
Drag(f64),
Released,
}
#[derive(Clone, Debug, Default)]
pub struct Splitter {
dragging: bool,
}
impl Splitter {
pub fn update<S: Spec>(&mut self, view: &VisualView<S>, id: &WidgetId, axis: Axis) -> SplitterEvent {
if view.is_pressed(id) && view.button_down() {
self.dragging = true;
}
if !self.dragging {
return SplitterEvent::Idle;
}
if !view.button_down() {
self.dragging = false;
return SplitterEvent::Released;
}
match (view.raw_pointer(), axis) {
(Some((x, _)), Axis::X) => SplitterEvent::Drag(x),
(Some((_, y)), Axis::Y) => SplitterEvent::Drag(y),
(None, _) => SplitterEvent::Idle,
}
}
pub fn dragging(&self) -> bool {
self.dragging
}
pub fn hot<S: Spec>(&self, view: &VisualView<S>, id: &WidgetId) -> bool {
self.dragging || (!view.button_down() && view.is_hovered(id))
}
}
pub fn grab_rect(axis: Axis, pos: f64, span: Rect, grab: f64) -> Rect {
let h = (grab - 1.0) / 2.0;
match axis {
Axis::X => Rect::new(pos - h, span.y, grab, span.height),
Axis::Y => Rect::new(span.x, pos - h, span.width, grab),
}
}
pub fn register(coord: &mut InputCoordinator, id: impl Into<WidgetId>, rect: Rect, layer: &LayerId) {
coord.register_atomic(id.into(), WidgetKind::Splitter, rect, Sense::CLICK_AND_DRAG, layer);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::spec::{PanelHome, Spec};
use uzor::layout::docking::DockPanel;
#[derive(Clone, Debug)]
struct P;
impl DockPanel for P {
fn title(&self) -> &str {
"p"
}
fn type_id(&self) -> &'static str {
"p"
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Tag {}
struct TestSpec;
impl Spec for TestSpec {
type Panel = P;
type Overlay = Tag;
type Action = Tag;
fn decode_panel(_home: PanelHome, _type_id: &str) -> Option<P> {
None
}
}
#[test]
fn press_drag_release() {
let id = WidgetId::from("split");
let mut s = Splitter::default();
let mut v = VisualView::<TestSpec>::default();
v.pointer = Some((100.0, 5.0));
v.hovered = Some(id.clone());
assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Idle);
assert!(s.hot(&v, &id));
v.pressed = Some(id.clone());
v.button_down = true;
assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Drag(100.0));
v.pressed = None;
v.hovered = None;
v.pointer = Some((140.0, 30.0));
assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Drag(140.0));
assert_eq!(s.update(&v, &id, Axis::Y), SplitterEvent::Drag(30.0));
assert!(s.hot(&v, &id));
v.button_down = false;
assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Released);
assert_eq!(s.update(&v, &id, Axis::X), SplitterEvent::Idle);
assert!(!s.hot(&v, &id));
}
#[test]
fn grab_strip_centres_on_the_line() {
let span = Rect::new(0.0, 10.0, 300.0, 200.0);
assert_eq!(grab_rect(Axis::X, 50.0, span, 9.0), Rect::new(46.0, 10.0, 9.0, 200.0));
assert_eq!(grab_rect(Axis::Y, 50.0, span, 9.0), Rect::new(0.0, 46.0, 300.0, 9.0));
}
}