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::locals::dp_to_px(60.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 swipe_right: Option<bool>,
70}
71
72pub fn handle_touch_raw(
73 rt: &mut ReposeRuntime,
74 touch_gestures: &mut TouchGestureState,
75 t: &Touch,
76 scale: f32,
77) -> TouchResult {
78 let pos_px = (t.location.x as f32, t.location.y as f32);
79 let tid = t.id;
80 match t.phase {
81 winit::event::TouchPhase::Started => {
82 touch_gestures.touch_started(rt, tid, pos_px);
83 TouchResult {
84 dirty: true,
85 pinch: None,
86 pan: None,
87 swipe_right: None,
88 }
89 }
90 winit::event::TouchPhase::Moved => {
91 let (dirty, pinch, pan) = touch_gestures.touch_moved(rt, tid, pos_px, scale);
92 TouchResult {
93 dirty,
94 pinch,
95 pan,
96 swipe_right: None,
97 }
98 }
99 winit::event::TouchPhase::Ended | winit::event::TouchPhase::Cancelled => {
100 let cancelled = t.phase == winit::event::TouchPhase::Cancelled;
101 let swipe_right = touch_gestures.touch_ended(rt, tid, pos_px, cancelled);
102 TouchResult {
103 dirty: false,
104 pinch: None,
105 pan: None,
106 swipe_right,
107 }
108 }
109 }
110}
111
112pub fn on_touch(
115 rt: &mut ReposeRuntime,
116 touch_gestures: &mut TouchGestureState,
117 t: &Touch,
118 scale: f32,
119 mut dispatch: impl FnMut(Action) -> bool,
120) -> bool {
121 let r = handle_touch_raw(rt, touch_gestures, t, scale);
122 let mut dirty = r.dirty;
123 if let Some((delta_scale, center)) = r.pinch
124 && dispatch(Action::Gesture(Gesture::PinchWithCenter {
125 delta_scale,
126 center,
127 })) {
128 dirty = true;
129 }
130 if let Some(delta) = r.pan
131 && dispatch(Action::Gesture(Gesture::Pan { delta })) {
132 dirty = true;
133 }
134 if let Some(right) = r.swipe_right {
135 let g = if right {
136 Gesture::SwipeRight
137 } else {
138 Gesture::SwipeLeft
139 };
140 if dispatch(Action::Gesture(g)) {
141 dirty = true;
142 }
143 }
144 dirty
145}
146
147pub fn on_touch_with_ime(
150 rt: &mut ReposeRuntime,
151 touch_gestures: &mut TouchGestureState,
152 t: &Touch,
153 scale: f32,
154 window: &winit::window::Window,
155 dispatch: impl FnMut(Action) -> bool,
156) -> bool {
157 let pos_px = (t.location.x as f32, t.location.y as f32);
158 let tid = t.id;
159 if t.phase == winit::event::TouchPhase::Started {
160 let focused = touch_gestures.touch_started(rt, tid, pos_px);
161 if let Some(fid) = focused {
162 if rt.is_textfield(fid) {
163 let (purpose, ac, cap) = rt.focused_keyboard_hints();
164 crate::common::set_ime_for_textfield_ex(window, true, purpose, ac, cap);
165 } else {
166 crate::common::set_ime_for_textfield(window, false);
167 }
168 } else {
169 crate::common::set_ime_for_textfield(window, false);
170 }
171 return true;
172 }
173 on_touch(rt, touch_gestures, t, scale, dispatch)
174}
175
176pub fn on_keyboard_input(
179 rt: &mut ReposeRuntime,
180 key_event: &winit::event::KeyEvent,
181 inspector: &mut Option<repose_devtools::Inspector>,
182) -> bool {
183 if key_event.state == ElementState::Pressed
184 && !key_event.repeat
185 && rt.modifiers.ctrl
186 && rt.modifiers.shift
187 && key_event.physical_key == PhysicalKey::Code(KeyCode::KeyI)
188 && let Some(inspector) = inspector {
189 inspector.hud.toggle_inspector();
190 return true;
191 }
192 let mapped = map_key(key_event.physical_key, &rt.modifiers);
193 let ke = winit_key_to_repose(key_event, &mapped, &rt.modifiers);
194 rt.handle_key_with_text(&ke, key_event.text.as_deref())
195}
196
197pub fn on_ime(rt: &mut ReposeRuntime, ime: &winit::event::Ime) {
199 use winit::event::Ime;
200 let ev = match ime {
201 Ime::Enabled => repose_core::input::ImeEvent::Start,
202 Ime::Preedit(text, cursor) => repose_core::input::ImeEvent::Update {
203 text: text.clone(),
204 cursor: cursor.map(|(a, b)| (a, b)),
205 },
206 Ime::Commit(text) => repose_core::input::ImeEvent::Commit(text.clone()),
207 Ime::Disabled => repose_core::input::ImeEvent::Cancel,
208 };
209 rt.handle_ime(&ev);
210}