1use blit::{ContactId, ContactSource, InteractCx, Phase, Scalar, input::MouseButton};
2
3use crate::Context;
4
5#[derive(Clone, Copy, Debug)]
9pub struct Interact;
10
11#[derive(Clone, Copy, Debug, Default, PartialEq)]
13pub struct Response {
14 pub active: bool,
15 pub activated: bool,
16 pub deactivated: bool,
17 pub hovered: bool,
18 pub clicked: bool,
19 pub contact: Option<ContactId>,
21}
22
23impl<C: Context> blit::Interact<C> for Interact {
24 type State = Option<ContactId>;
26 type Response = Response;
27
28 fn update(&self, press: &mut Option<ContactId>, cx: &mut InteractCx<'_, C::Scalar>) -> Self::Response {
29 let was_active = press.is_some();
30 let mut active = false;
31 let mut clicked = false;
32 let mut contact_id = None;
33 if let Some(id) = *press
35 && let Some(mut contact) = cx.contacts().find(|contact| contact.id == id)
36 {
37 let x = (contact.position.x - contact.origin.x).to_f32();
38 let y = (contact.position.y - contact.origin.y).to_f32();
39 let slop = C::SLOP.to_f32();
40 let mouse = matches!(contact.source, ContactSource::Mouse(_));
41 if (mouse || x * x + y * y < slop * slop) && contact.claim() {
43 active = !matches!(contact.phase, Phase::Ended | Phase::Cancelled);
44 clicked = contact.phase == Phase::Ended && contact.over;
45 if active || clicked {
46 contact_id = Some(contact.id);
47 }
48 }
49 }
50 if !active {
52 *press = None;
53 if let Some(mut contact) = cx.contacts().find(|contact| {
54 contact.phase == Phase::Started
55 && contact.free
56 && matches!(
57 contact.source,
58 ContactSource::Touch(_) | ContactSource::Mouse(MouseButton::Primary)
59 )
60 }) && contact.claim()
61 {
62 *press = Some(contact.id);
63 active = true;
64 contact_id = Some(contact.id);
65 }
66 }
67 Response {
68 active,
69 activated: active && !was_active,
70 deactivated: was_active && !active,
71 hovered: cx.hovered(),
72 clicked,
73 contact: contact_id,
74 }
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use blit::{
81 Point, Tree, Ui,
82 input::{
83 Input,
84 TouchPhase::{Cancel, Down, Move, Up},
85 },
86 };
87
88 use crate::{
89 interact,
90 test::{TestContext, pointer, step},
91 };
92
93 #[test]
94 fn presses() {
95 for id in [None, Some(1)] {
96 let mut tree = Tree::<TestContext>::default();
97 let mut last_contact = None;
98 let mut press = |input| {
99 step(&mut tree, input, |mut ui: Ui<'_, TestContext>| {
100 let press = ui.interact(interact::press());
101 last_contact = press.contact.or(last_contact);
102 (
103 press,
104 last_contact.and_then(|id| ui.contact_position(id)),
105 ui.mouse_position(),
106 )
107 })
108 };
109 assert_eq!(press(Input::None).0.contact, None);
110 let (down, position, mouse) = press(pointer(id, Down, 10.0, 10.0));
111 assert!(down.active && down.activated && !down.clicked);
112 assert!(down.contact.is_some());
113 assert_eq!(position, Some(Point::new(10.0, 10.0)));
114 assert_eq!(mouse, id.is_none().then_some(Point::new(10.0, 10.0)));
115 let (held, _, _) = press(Input::None);
116 assert!(held.active && !held.activated);
117 assert_eq!(held.contact, down.contact);
118 let (moved, _, mouse) = press(pointer(id, Move, 11.0, 10.0));
119 assert_eq!(moved.contact, down.contact);
120 for phase in [Down, Move, Up] {
121 let (held, position, cursor) = press(pointer(Some(2), phase, 100.0, 100.0));
122 assert!(held.active && !held.activated && !held.clicked);
123 assert_eq!(held.contact, down.contact);
124 assert_eq!(position, Some(Point::new(11.0, 10.0)));
125 assert_eq!(cursor, mouse);
126 }
127 let released_x = if id.is_none() { 50.0 } else { 12.0 };
128 let (moved, _, _) = press(pointer(id, Move, released_x, 10.0));
129 assert!(moved.active && !moved.clicked);
130 let (up, position, _) = press(pointer(id, Up, released_x, 10.0));
131 assert!(up.clicked && up.deactivated && !up.active);
132 assert_eq!(up.contact, down.contact);
133 assert_eq!(position, Some(Point::new(released_x, 10.0)));
134 let (idle, position, _) = press(Input::None);
135 assert_eq!(idle.contact, None);
136 assert_eq!(position, None);
137 let ending = if id.is_none() { Up } else { Move };
139 for (phase, y) in [(ending, 300.0), (Cancel, 10.0)] {
140 press(pointer(id, Down, 10.0, 10.0));
141 if phase == Up {
142 assert!(press(pointer(id, Move, 300.0, 10.0)).0.active);
143 assert!(press(pointer(id, Move, 10.0, 10.0)).0.active);
144 }
145 let (ended, position, _) = press(pointer(id, phase, 10.0, y));
146 assert!(ended.deactivated && !ended.active && !ended.clicked);
147 assert_eq!(ended.contact, None);
148 assert_eq!(position, Some(Point::new(10.0, y)));
149 if phase == Move {
150 assert!(!press(pointer(id, Up, 10.0, y)).0.clicked);
151 }
152 assert_eq!(press(Input::None).1, None);
153 }
154 }
155 }
156
157 #[test]
158 fn capture_cancels_click() {
159 for ending in [Up, Cancel] {
160 let mut tree = Tree::<TestContext>::default();
161 let build = |mut ui: Ui<'_, TestContext>| {
162 let pan = ui.interact(interact::drag());
164 let press = ui
165 .layout(())
166 .child()
167 .build(|mut ui: Ui<'_, TestContext>| ui.interact(interact::press()));
168 (pan, press)
169 };
170 let mut next = |input| step(&mut tree, input, build);
171 next(Input::None);
172 let (pan, press) = next(pointer(Some(1), Down, 10.0, 10.0));
174 assert!(press.active && !pan.active);
175 let (pan, press) = next(pointer(Some(1), Move, 12.0, 10.0));
176 assert!(press.active && !pan.active);
177 let (pan, press) = next(pointer(Some(1), Move, 30.0, 10.0));
179 assert!(pan.activated && press.deactivated && !press.active);
180 assert_eq!(press.contact, None);
181 assert!(!next(pointer(Some(1), ending, 30.0, 10.0)).1.clicked);
182 }
183 }
184}