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 if 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 }
40 result.cursor
41}
42
43pub fn on_mouse_wheel(rt: &mut ReposeRuntime, delta: MouseScrollDelta, scale: f32) -> bool {
45 let (dx_px, dy_px) = match delta {
46 MouseScrollDelta::LineDelta(x, y) => {
47 let unit_px = repose_core::locals::dp_to_px(60.0) * scale;
48 (-(x * unit_px), -(y * unit_px))
49 }
50 MouseScrollDelta::PixelDelta(p) => (-(p.x as f32), -(p.y as f32)),
51 };
52 rt.handle_scroll(Vec2 { x: dx_px, y: dy_px })
53}
54
55pub fn map_mouse_button(btn: winit::event::MouseButton) -> Option<PointerButton> {
57 match btn {
58 winit::event::MouseButton::Left => Some(PointerButton::Primary),
59 winit::event::MouseButton::Right => Some(PointerButton::Secondary),
60 winit::event::MouseButton::Middle => Some(PointerButton::Tertiary),
61 _ => None,
62 }
63}
64
65pub struct TouchResult {
67 pub dirty: bool,
68 pub pinch: Option<(f32, Vec2)>,
69 pub pan: Option<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 swipe_right: None,
89 }
90 }
91 winit::event::TouchPhase::Moved => {
92 let (dirty, pinch, pan) = touch_gestures.touch_moved(rt, tid, pos_px, scale);
93 TouchResult {
94 dirty,
95 pinch,
96 pan,
97 swipe_right: None,
98 }
99 }
100 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
101 let cancelled = t.phase == winit::event::TouchPhase::Cancelled;
102 let swipe_right = touch_gestures.touch_ended(rt, tid, pos_px, cancelled);
103 TouchResult {
104 dirty: false,
105 pinch: None,
106 pan: None,
107 swipe_right,
108 }
109 }
110 }
111}
112
113pub fn on_touch(
116 rt: &mut ReposeRuntime,
117 touch_gestures: &mut TouchGestureState,
118 t: &Touch,
119 scale: f32,
120 mut dispatch: impl FnMut(Action) -> bool,
121) -> bool {
122 let r = handle_touch_raw(rt, touch_gestures, t, scale);
123 let mut dirty = r.dirty;
124 if let Some((delta_scale, center)) = r.pinch {
125 if dispatch(Action::Gesture(Gesture::PinchWithCenter {
126 delta_scale,
127 center,
128 })) {
129 dirty = true;
130 }
131 }
132 if let Some(delta) = r.pan {
133 if dispatch(Action::Gesture(Gesture::Pan { delta })) {
134 dirty = true;
135 }
136 }
137 if let Some(right) = r.swipe_right {
138 let g = if right {
139 Gesture::SwipeRight
140 } else {
141 Gesture::SwipeLeft
142 };
143 if dispatch(Action::Gesture(g)) {
144 dirty = true;
145 }
146 }
147 dirty
148}
149
150pub fn on_touch_with_ime(
153 rt: &mut ReposeRuntime,
154 touch_gestures: &mut TouchGestureState,
155 t: &Touch,
156 scale: f32,
157 window: &winit::window::Window,
158 dispatch: impl FnMut(Action) -> bool,
159) -> bool {
160 let pos_px = (t.location.x as f32, t.location.y as f32);
161 let tid = t.id;
162 if t.phase == winit::event::TouchPhase::Started {
163 let focused = touch_gestures.touch_started(rt, tid, pos_px);
164 if let Some(fid) = focused {
165 if rt.is_textfield(fid) {
166 let (purpose, ac, cap) = rt.focused_keyboard_hints();
167 crate::common::set_ime_for_textfield_ex(window, true, purpose, ac, cap);
168 } else {
169 crate::common::set_ime_for_textfield(window, false);
170 }
171 } else {
172 crate::common::set_ime_for_textfield(window, false);
173 }
174 return true;
175 }
176 on_touch(rt, touch_gestures, t, scale, dispatch)
177}
178
179pub fn on_keyboard_input(
182 rt: &mut ReposeRuntime,
183 key_event: &winit::event::KeyEvent,
184 inspector: &mut Option<repose_devtools::Inspector>,
185) -> bool {
186 if key_event.state == ElementState::Pressed
187 && !key_event.repeat
188 && rt.modifiers.ctrl
189 && rt.modifiers.shift
190 && key_event.physical_key == PhysicalKey::Code(KeyCode::KeyI)
191 {
192 if let Some(inspector) = inspector {
193 inspector.hud.toggle_inspector();
194 return true;
195 }
196 }
197 let mapped = map_key(key_event.physical_key, &rt.modifiers);
198 let ke = winit_key_to_repose(key_event, &mapped, &rt.modifiers);
199 rt.handle_key_with_text(&ke, key_event.text.as_deref())
200}
201
202pub fn on_ime(rt: &mut ReposeRuntime, ime: &winit::event::Ime) {
204 use winit::event::Ime;
205 let ev = match ime {
206 Ime::Enabled => repose_core::input::ImeEvent::Start,
207 Ime::Preedit(text, cursor) => repose_core::input::ImeEvent::Update {
208 text: text.clone(),
209 cursor: cursor.map(|(a, b)| (a as usize, b as usize)),
210 },
211 Ime::Commit(text) => repose_core::input::ImeEvent::Commit(text.clone()),
212 Ime::Disabled => repose_core::input::ImeEvent::Cancel,
213 };
214 rt.handle_ime(&ev);
215}