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 {
89 winit::event::TouchPhase::Started => {
90 touch_gestures.touch_started(rt, tid, pos_px);
91 TouchResult {
92 dirty: true,
93 pinch: None,
94 pan: None,
95 rotation: None,
96 swipe_right: None,
97 press: None,
98 }
99 }
100 winit::event::TouchPhase::Moved => {
101 let (dirty, pinch, pan, rotation) = touch_gestures.touch_moved(rt, tid, pos_px, scale);
102 TouchResult {
103 dirty,
104 pinch,
105 pan,
106 rotation,
107 swipe_right: None,
108 press: None,
109 }
110 }
111 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
112 let cancelled = t.phase == winit::event::TouchPhase::Cancelled;
113 let ended = touch_gestures.touch_ended(rt, tid, pos_px, cancelled);
114 TouchResult {
115 dirty: false,
116 pinch: None,
117 pan: None,
118 rotation: None,
119 swipe_right: ended.swipe_right,
120 press: ended.press,
121 }
122 }
123 }
124}
125
126pub fn on_touch(
129 rt: &mut ReposeRuntime,
130 touch_gestures: &mut TouchGestureState,
131 t: &Touch,
132 scale: f32,
133 mut dispatch: impl FnMut(Action) -> bool,
134) -> bool {
135 let r = handle_touch_raw(rt, touch_gestures, t, scale);
136 let mut dirty = r.dirty;
137 if let Some((delta_scale, center)) = r.pinch
138 && dispatch(Action::Gesture(Gesture::PinchWithCenter {
139 delta_scale,
140 center,
141 }))
142 {
143 dirty = true;
144 }
145 if let Some((delta, center)) = r.pan
146 && dispatch(Action::Gesture(Gesture::Pan { delta, center }))
147 {
148 dirty = true;
149 }
150 if let Some((delta_rotation, center)) = r.rotation
151 && dispatch(Action::Gesture(Gesture::Rotate {
152 delta_rotation,
153 center,
154 }))
155 {
156 dirty = true;
157 }
158 if let Some(right) = r.swipe_right {
159 let g = if right {
160 Gesture::SwipeRight
161 } else {
162 Gesture::SwipeLeft
163 };
164 if dispatch(Action::Gesture(g)) {
165 dirty = true;
166 }
167 }
168 dirty
169}
170
171pub fn on_touch_with_ime(
174 rt: &mut ReposeRuntime,
175 touch_gestures: &mut TouchGestureState,
176 t: &Touch,
177 scale: f32,
178 window: &winit::window::Window,
179 dispatch: impl FnMut(Action) -> bool,
180) -> bool {
181 let pos_px = (t.location.x as f32, t.location.y as f32);
182 let tid = t.id;
183 if t.phase == winit::event::TouchPhase::Started {
184 let focused = touch_gestures.touch_started(rt, tid, pos_px);
185 if let Some(fid) = focused {
186 if rt.is_textfield(fid) {
187 let (purpose, ac, cap) = rt.focused_keyboard_hints();
188 crate::common::set_ime_for_textfield_ex(window, true, purpose, ac, cap);
189 } else {
190 crate::common::set_ime_for_textfield(window, false);
191 }
192 } else {
193 crate::common::set_ime_for_textfield(window, false);
194 }
195 return true;
196 }
197 on_touch(rt, touch_gestures, t, scale, dispatch)
198}
199
200pub fn on_keyboard_input(
203 rt: &mut ReposeRuntime,
204 key_event: &winit::event::KeyEvent,
205 inspector: &mut Option<repose_devtools::Inspector>,
206) -> bool {
207 if key_event.state == ElementState::Pressed
208 && !key_event.repeat
209 && rt.modifiers.ctrl
210 && rt.modifiers.shift
211 && key_event.physical_key == PhysicalKey::Code(KeyCode::KeyI)
212 && let Some(inspector) = inspector
213 {
214 inspector.hud.toggle_inspector();
215 return true;
216 }
217 let mapped = map_key(key_event.physical_key, &rt.modifiers);
218 let ke = winit_key_to_repose(key_event, &mapped, &rt.modifiers);
219 rt.handle_key_with_text(&ke, key_event.text.as_deref())
220}
221
222pub fn on_ime(rt: &mut ReposeRuntime, ime: &winit::event::Ime) {
224 use winit::event::Ime;
225 let ev = match ime {
226 Ime::Enabled => repose_core::input::ImeEvent::Start,
227 Ime::Preedit(text, cursor) => repose_core::input::ImeEvent::Update {
228 text: text.clone(),
229 cursor: cursor.map(|(a, b)| (a, b)),
230 },
231 Ime::Commit(text) => repose_core::input::ImeEvent::Commit(text.clone()),
232 Ime::Disabled => repose_core::input::ImeEvent::Cancel,
233 };
234 rt.handle_ime(&ev);
235}