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 let hit = f.hit_regions.iter().find(|h| h.rect.contains(pos));
27 let hover_rect = hit.map(|h| h.rect);
28 let hover_info = hit.and_then(|h| {
29 f.semantics_nodes.iter().find(|s| s.id == h.id).map(|s| {
30 repose_devtools::HoveredInfo {
31 id: s.id,
32 role: format!("{:?}", s.role),
33 label: s.label.clone(),
34 }
35 })
36 });
37 inspector.hud.set_hovered(hover_rect, hover_info);
38 }
39 result.cursor
40}
41
42pub fn on_mouse_wheel(rt: &mut ReposeRuntime, delta: MouseScrollDelta, scale: f32) -> bool {
44 let (dx_px, dy_px) = match delta {
45 MouseScrollDelta::LineDelta(x, y) => {
46 let unit_px = repose_core::Dp(60.0).to_px().0 * scale;
47 (-(x * unit_px), -(y * unit_px))
48 }
49 MouseScrollDelta::PixelDelta(p) => (-(p.x as f32), -(p.y as f32)),
50 };
51 rt.handle_scroll(Vec2 { x: dx_px, y: dy_px })
52}
53
54pub fn map_mouse_button(btn: winit::event::MouseButton) -> Option<PointerButton> {
56 match btn {
57 winit::event::MouseButton::Left => Some(PointerButton::Primary),
58 winit::event::MouseButton::Right => Some(PointerButton::Secondary),
59 winit::event::MouseButton::Middle => Some(PointerButton::Tertiary),
60 _ => None,
61 }
62}
63
64pub struct TouchResult {
66 pub dirty: bool,
67 pub pinch: Option<(f32, Vec2)>,
68 pub pan: Option<Vec2>,
69 pub rotation: Option<(f32, Vec2)>,
70 pub swipe_right: Option<bool>,
71}
72
73pub fn handle_touch_raw(
74 rt: &mut ReposeRuntime,
75 touch_gestures: &mut TouchGestureState,
76 t: &Touch,
77 scale: f32,
78) -> TouchResult {
79 let pos_px = (t.location.x as f32, t.location.y as f32);
80 let tid = t.id;
81 match t.phase {
82 winit::event::TouchPhase::Started => {
83 touch_gestures.touch_started(rt, tid, pos_px);
84 TouchResult {
85 dirty: true,
86 pinch: None,
87 pan: None,
88 rotation: None,
89 swipe_right: None,
90 }
91 }
92 winit::event::TouchPhase::Moved => {
93 let (dirty, pinch, pan, rotation) = touch_gestures.touch_moved(rt, tid, pos_px, scale);
94 TouchResult {
95 dirty,
96 pinch,
97 pan,
98 rotation,
99 swipe_right: None,
100 }
101 }
102 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
103 let cancelled = t.phase == winit::event::TouchPhase::Cancelled;
104 let swipe_right = touch_gestures.touch_ended(rt, tid, pos_px, cancelled);
105 TouchResult {
106 dirty: false,
107 pinch: None,
108 pan: None,
109 rotation: None,
110 swipe_right,
111 }
112 }
113 }
114}
115
116pub fn on_touch(
119 rt: &mut ReposeRuntime,
120 touch_gestures: &mut TouchGestureState,
121 t: &Touch,
122 scale: f32,
123 mut dispatch: impl FnMut(Action) -> bool,
124) -> bool {
125 let r = handle_touch_raw(rt, touch_gestures, t, scale);
126 let mut dirty = r.dirty;
127 if let Some((delta_scale, center)) = r.pinch
128 && dispatch(Action::Gesture(Gesture::PinchWithCenter {
129 delta_scale,
130 center,
131 })) {
132 dirty = true;
133 }
134 if let Some(delta) = r.pan
135 && dispatch(Action::Gesture(Gesture::Pan { delta })) {
136 dirty = true;
137 }
138 if let Some((delta_rotation, center)) = r.rotation
139 && dispatch(Action::Gesture(Gesture::Rotate {
140 delta_rotation,
141 center,
142 })) {
143 dirty = true;
144 }
145 if let Some(right) = r.swipe_right {
146 let g = if right {
147 Gesture::SwipeRight
148 } else {
149 Gesture::SwipeLeft
150 };
151 if dispatch(Action::Gesture(g)) {
152 dirty = true;
153 }
154 }
155 dirty
156}
157
158pub fn on_touch_with_ime(
161 rt: &mut ReposeRuntime,
162 touch_gestures: &mut TouchGestureState,
163 t: &Touch,
164 scale: f32,
165 window: &winit::window::Window,
166 dispatch: impl FnMut(Action) -> bool,
167) -> bool {
168 let pos_px = (t.location.x as f32, t.location.y as f32);
169 let tid = t.id;
170 if t.phase == winit::event::TouchPhase::Started {
171 let focused = touch_gestures.touch_started(rt, tid, pos_px);
172 if let Some(fid) = focused {
173 if rt.is_textfield(fid) {
174 let (purpose, ac, cap) = rt.focused_keyboard_hints();
175 crate::common::set_ime_for_textfield_ex(window, true, purpose, ac, cap);
176 } else {
177 crate::common::set_ime_for_textfield(window, false);
178 }
179 } else {
180 crate::common::set_ime_for_textfield(window, false);
181 }
182 return true;
183 }
184 on_touch(rt, touch_gestures, t, scale, dispatch)
185}
186
187pub fn on_keyboard_input(
190 rt: &mut ReposeRuntime,
191 key_event: &winit::event::KeyEvent,
192 inspector: &mut Option<repose_devtools::Inspector>,
193) -> bool {
194 if key_event.state == ElementState::Pressed
195 && !key_event.repeat
196 && rt.modifiers.ctrl
197 && rt.modifiers.shift
198 && key_event.physical_key == PhysicalKey::Code(KeyCode::KeyI)
199 && let Some(inspector) = inspector {
200 inspector.hud.toggle_inspector();
201 return true;
202 }
203 let mapped = map_key(key_event.physical_key, &rt.modifiers);
204 let ke = winit_key_to_repose(key_event, &mapped, &rt.modifiers);
205 rt.handle_key_with_text(&ke, key_event.text.as_deref())
206}
207
208pub fn on_ime(rt: &mut ReposeRuntime, ime: &winit::event::Ime) {
210 use winit::event::Ime;
211 let ev = match ime {
212 Ime::Enabled => repose_core::input::ImeEvent::Start,
213 Ime::Preedit(text, cursor) => repose_core::input::ImeEvent::Update {
214 text: text.clone(),
215 cursor: cursor.map(|(a, b)| (a, b)),
216 },
217 Ime::Commit(text) => repose_core::input::ImeEvent::Commit(text.clone()),
218 Ime::Disabled => repose_core::input::ImeEvent::Cancel,
219 };
220 rt.handle_ime(&ev);
221}