1use crate::geometry::Point;
7
8pub const TOUCH_SLOP: f32 = 4.0;
14pub const DOUBLE_TAP_TIMEOUT_MS: u64 = 300;
16pub const LONG_PRESS_TIMEOUT_MS: u64 = 500;
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
23pub enum MouseButton {
24 #[default]
25 Left,
26 Right,
27 Middle,
28 Other(u16),
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub enum GestureState {
34 Possible,
36 Accepted,
38 Rejected,
40}
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
51pub struct KeyModifiers(pub u8);
52
53impl KeyModifiers {
54 pub const SHIFT: u8 = 1 << 0;
55 pub const CTRL: u8 = 1 << 1;
56 pub const ALT: u8 = 1 << 2;
57 pub const META: u8 = 1 << 3;
58
59 pub fn shift(&self) -> bool {
60 self.0 & Self::SHIFT != 0
61 }
62 pub fn ctrl(&self) -> bool {
63 self.0 & Self::CTRL != 0
64 }
65 pub fn alt(&self) -> bool {
66 self.0 & Self::ALT != 0
67 }
68 pub fn meta(&self) -> bool {
69 self.0 & Self::META != 0
70 }
71 pub fn none() -> Self {
72 KeyModifiers(0)
73 }
74 pub fn ctrl_only() -> Self {
75 KeyModifiers(Self::CTRL)
76 }
77 pub fn shift_only() -> Self {
78 KeyModifiers(Self::SHIFT)
79 }
80
81 pub fn command(&self) -> bool {
85 #[cfg(target_os = "macos")]
86 {
87 self.0 & Self::META != 0
88 }
89 #[cfg(not(target_os = "macos"))]
90 {
91 self.0 & Self::CTRL != 0
92 }
93 }
94
95 pub fn command_only() -> Self {
97 #[cfg(target_os = "macos")]
98 {
99 KeyModifiers(Self::META)
100 }
101 #[cfg(not(target_os = "macos"))]
102 {
103 KeyModifiers(Self::CTRL)
104 }
105 }
106}
107
108#[derive(Clone, Copy, Debug, PartialEq, Eq)]
112pub enum KeyCode {
113 Enter,
114 Space,
115 Escape,
116 Tab,
117 BackTab,
119 LeftArrow,
120 RightArrow,
121 UpArrow,
122 DownArrow,
123 PageUp,
124 PageDown,
125 Backspace,
126 Delete,
127 Home,
128 End,
129 Char(char),
131 F2,
133 F3,
135 F11,
137 Other,
139}
140
141#[derive(Clone, Copy, Debug, PartialEq, Default)]
143pub struct PointerData {
144 pub position: Point,
146 pub timestamp_ms: u64,
148 pub button: MouseButton,
150 pub modifiers: KeyModifiers,
152}
153
154impl PointerData {
155 pub fn new(position: Point, timestamp_ms: u64) -> Self {
156 Self { position, timestamp_ms, button: MouseButton::default(), modifiers: KeyModifiers::none() }
157 }
158
159 pub fn is_primary(&self) -> bool {
161 self.button == MouseButton::Left
162 }
163
164 pub fn with_button(position: Point, timestamp_ms: u64, button: MouseButton) -> Self {
165 Self { position, timestamp_ms, button, modifiers: KeyModifiers::none() }
166 }
167
168 pub fn with_modifiers(mut self, modifiers: KeyModifiers) -> Self {
170 self.modifiers = modifiers;
171 self
172 }
173
174 pub fn at(position: Point) -> Self {
176 Self { position, timestamp_ms: 0, button: MouseButton::Left, modifiers: KeyModifiers::none() }
177 }
178
179 pub fn at_right(position: Point) -> Self {
180 Self { position, timestamp_ms: 0, button: MouseButton::Right, modifiers: KeyModifiers::none() }
181 }
182}
183
184#[derive(Clone, Debug, PartialEq)]
193pub enum WidgetEvent {
194 PointerDown(PointerData),
195 PointerUp(PointerData),
196 PointerMove(PointerData),
197 PointerLeave,
199 PointerEnter,
201 PointerCancel,
206 Scroll(f32, f32),
208 KeyDown(KeyCode, KeyModifiers),
209 KeyUp(KeyCode, KeyModifiers),
210 FocusGained,
216 FocusVisibleChanged(bool),
223 FocusLost,
225 FocusWithin(bool),
227 ImeEnabled,
229 ImeDisabled,
231 ImePreedit(String, Option<(usize, usize)>),
233 ImeCommit(String),
235 SetText(String),
237 SetValue(f32),
239 SetExpanded {
241 item: Option<u64>,
242 expanded: bool,
243 },
244 SetTextSelection {
246 anchor: usize,
247 focus: usize,
248 },
249}
250
251#[cfg(test)]
252mod tests {
253 use super::*;
254
255 #[test]
256 fn command_modifier_is_platform_aware() {
257 let command = KeyModifiers::command_only();
258 assert!(command.command());
259 #[cfg(target_os = "macos")]
260 {
261 assert!(command.meta() && !command.ctrl(), "macOS 命令键应为 META");
262 assert!(!KeyModifiers::ctrl_only().command(), "macOS 上 Ctrl 不是命令键");
263 }
264 #[cfg(not(target_os = "macos"))]
265 {
266 assert!(command.ctrl() && !command.meta(), "非 macOS 命令键应为 CTRL");
267 assert!(!KeyModifiers(KeyModifiers::META).command(), "非 macOS 上 META 不触发命令键");
268 }
269 }
270}