1use repose_app::{ReposeRuntime, TouchGestureState};
4use repose_core::Vec2;
5use repose_core::input::PointerButton;
6use repose_core::shortcuts::{Action, Gesture};
7use winit::event::{ElementState, MouseScrollDelta, Touch};
8use winit::keyboard::{KeyCode, PhysicalKey};
9
10use crate::common::{map_key, winit_key_to_repose};
11
12pub fn on_modifiers_changed(rt: &mut ReposeRuntime, state: &winit::keyboard::ModifiersState) {
14 crate::common::update_modifiers(&mut rt.modifiers, state);
15}
16
17pub fn on_cursor_moved(
19 rt: &mut ReposeRuntime,
20 pos: Vec2,
21 inspector: &mut Option<repose_devtools::Inspector>,
22) -> Option<repose_core::CursorIcon> {
23 let result = rt.handle_pointer_move(pos);
24 if let (Some(inspector), Some(f)) = (inspector, &rt.frame_cache)
25 && inspector.hud.inspector_enabled
26 {
27 let hit = f.hit_regions.iter().find(|h| h.rect.contains(pos));
28 let hover_rect = hit.map(|h| h.rect);
29 let hover_info = hit.and_then(|h| {
30 f.semantics_nodes
31 .iter()
32 .find(|s| s.id == h.id)
33 .map(|s| repose_devtools::HoveredInfo {
34 id: s.id,
35 role: format!("{:?}", s.role),
36 label: s.label.clone(),
37 })
38 });
39 inspector.hud.set_hovered(hover_rect, hover_info);
40 }
41 result.cursor
42}
43
44pub fn on_mouse_wheel(rt: &mut ReposeRuntime, delta: MouseScrollDelta, scale: f32) -> bool {
46 let (dx_px, dy_px) = match delta {
47 MouseScrollDelta::LineDelta(x, y) => {
48 let _ = scale;
49 let unit_px = repose_core::Dp(60.0).to_px().0;
50 (-(x * unit_px), -(y * unit_px))
51 }
52 MouseScrollDelta::PixelDelta(p) => (-(p.x as f32), -(p.y as f32)),
53 };
54 rt.handle_scroll(Vec2 { x: dx_px, y: dy_px })
55}
56
57pub fn map_mouse_button(btn: winit::event::MouseButton) -> Option<PointerButton> {
59 match btn {
60 winit::event::MouseButton::Left => Some(PointerButton::Primary),
61 winit::event::MouseButton::Right => Some(PointerButton::Secondary),
62 winit::event::MouseButton::Middle => Some(PointerButton::Tertiary),
63 _ => None,
64 }
65}
66
67pub struct TouchResult {
69 pub dirty: bool,
70 pub pinch: Option<(f32, Vec2)>,
71 pub pan: Option<(Vec2, Vec2)>,
72 pub rotation: Option<(f32, Vec2)>,
73 pub swipe_right: Option<bool>,
74 pub press: Option<Option<u64>>,
78}
79
80pub fn handle_touch_raw(
81 rt: &mut ReposeRuntime,
82 touch_gestures: &mut TouchGestureState,
83 t: &Touch,
84 scale: f32,
85) -> TouchResult {
86 let pos_px = (t.location.x as f32, t.location.y as f32);
87 let tid = t.id;
88 match t.phase {
90 winit::event::TouchPhase::Started | winit::event::TouchPhase::Moved => {
91 touch_gestures.contact_down(tid, pos_px);
92 }
93 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
94 touch_gestures.contact_up(tid);
95 }
96 }
97 match t.phase {
98 winit::event::TouchPhase::Started => {
99 touch_gestures.touch_started(rt, tid, pos_px);
100 TouchResult {
101 dirty: true,
102 pinch: None,
103 pan: None,
104 rotation: None,
105 swipe_right: None,
106 press: None,
107 }
108 }
109 winit::event::TouchPhase::Moved => {
110 let (dirty, pinch, pan, rotation) = touch_gestures.touch_moved(rt, tid, pos_px, scale);
111 TouchResult {
112 dirty,
113 pinch,
114 pan,
115 rotation,
116 swipe_right: None,
117 press: None,
118 }
119 }
120 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
121 let cancelled = t.phase == winit::event::TouchPhase::Cancelled;
122 let ended = touch_gestures.touch_ended(rt, tid, pos_px, cancelled);
123 TouchResult {
124 dirty: false,
125 pinch: None,
126 pan: None,
127 rotation: None,
128 swipe_right: ended.swipe_right,
129 press: ended.press,
130 }
131 }
132 }
133}
134
135pub fn on_touch(
138 rt: &mut ReposeRuntime,
139 touch_gestures: &mut TouchGestureState,
140 t: &Touch,
141 scale: f32,
142 mut dispatch: impl FnMut(Action) -> bool,
143) -> bool {
144 let r = handle_touch_raw(rt, touch_gestures, t, scale);
145 let mut dirty = r.dirty;
146 if let Some((delta_scale, center)) = r.pinch
147 && dispatch(Action::Gesture(Gesture::PinchWithCenter {
148 delta_scale,
149 center,
150 }))
151 {
152 dirty = true;
153 }
154 if let Some((delta, center)) = r.pan
155 && dispatch(Action::Gesture(Gesture::Pan { delta, center }))
156 {
157 dirty = true;
158 }
159 if let Some((delta_rotation, center)) = r.rotation
160 && dispatch(Action::Gesture(Gesture::Rotate {
161 delta_rotation,
162 center,
163 }))
164 {
165 dirty = true;
166 }
167 if let Some(right) = r.swipe_right {
168 let g = if right {
169 Gesture::SwipeRight
170 } else {
171 Gesture::SwipeLeft
172 };
173 if dispatch(Action::Gesture(g)) {
174 dirty = true;
175 }
176 }
177 dirty
178}
179
180pub fn on_touch_with_ime(
183 rt: &mut ReposeRuntime,
184 touch_gestures: &mut TouchGestureState,
185 t: &Touch,
186 scale: f32,
187 window: &winit::window::Window,
188 dispatch: impl FnMut(Action) -> bool,
189) -> bool {
190 let pos_px = (t.location.x as f32, t.location.y as f32);
191 let tid = t.id;
192 if t.phase == winit::event::TouchPhase::Started {
193 let focused = touch_gestures.touch_started(rt, tid, pos_px);
194 if let Some(fid) = focused {
195 if rt.is_textfield(fid) {
196 let (purpose, ac, cap) = rt.focused_keyboard_hints();
197 crate::common::set_ime_for_textfield_ex(window, true, purpose, ac, cap);
198 } else {
199 crate::common::set_ime_for_textfield(window, false);
200 }
201 } else {
202 crate::common::set_ime_for_textfield(window, false);
203 }
204 return true;
205 }
206 on_touch(rt, touch_gestures, t, scale, dispatch)
207}
208
209pub fn on_keyboard_input(
215 rt: &mut ReposeRuntime,
216 key_event: &winit::event::KeyEvent,
217 inspector: &mut Option<repose_devtools::Inspector>,
218) -> bool {
219 rt.set_physical_key(
220 &physical_key_name(key_event.physical_key),
221 key_event.state == ElementState::Pressed,
222 );
223 {
228 let key = map_physical_key(key_event.physical_key);
229 let sched = &mut rt.sched;
230 if key_event.state == ElementState::Pressed {
231 sched.held_keys.insert(key);
232 } else {
233 sched.held_keys.remove(&key);
234 }
235 }
236 if key_event.state == ElementState::Pressed
237 && !key_event.repeat
238 && rt.modifiers.ctrl
239 && rt.modifiers.shift
240 && key_event.physical_key == PhysicalKey::Code(KeyCode::KeyI)
241 && let Some(inspector) = inspector
242 {
243 inspector.hud.toggle_inspector();
244 return true;
245 }
246 let mapped = map_key(key_event.physical_key, &rt.modifiers);
247 let ke = winit_key_to_repose(key_event, &mapped, &rt.modifiers);
248 rt.handle_key_with_text(&ke, key_event.text.as_deref())
249}
250
251pub fn physical_key_name(key: PhysicalKey) -> String {
256 match key {
257 PhysicalKey::Code(code) => format!("{code:?}"),
258 PhysicalKey::Unidentified(native) => format!("Unidentified({native:?})"),
259 }
260}
261
262pub fn map_physical_key(key: PhysicalKey) -> repose_core::input::PhysicalKey {
267 repose_core::input::PhysicalKey::from_name(&physical_key_name(key))
268}
269
270pub fn on_ime(rt: &mut ReposeRuntime, ime: &winit::event::Ime) {
272 use winit::event::Ime;
273 let ev = match ime {
274 Ime::Enabled => repose_core::input::ImeEvent::Start,
275 Ime::Preedit(text, cursor) => repose_core::input::ImeEvent::Update {
276 text: text.clone(),
277 cursor: cursor.map(|(a, b)| (a, b)),
278 },
279 Ime::Commit(text) => repose_core::input::ImeEvent::Commit(text.clone()),
280 Ime::Disabled => repose_core::input::ImeEvent::Cancel,
281 };
282 rt.handle_ime(&ev);
283}