Skip to main content

velox_engine/pod/
events.rs

1//! 统一跨端输入事件与基础几何/手势/键盘/IME 类型定义。
2
3/// 按钮点击事件参数。
4#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
5pub struct ClickEvent {
6    pub x: i16,
7    pub y: i16,
8}
9
10/// 鼠标按键标识(左键、右键、中键)。
11#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
12pub enum PointerButton {
13    /// 左键 / 主触摸点。
14    #[default]
15    Left,
16    /// 右键(上下文菜单)。
17    Right,
18    /// 中键(滚轮按下)。
19    Middle,
20}
21
22/// 指针输入设备来源类型(用于隔离桌面鼠标与触控屏的拖拽滚动/惯性/边缘返回手势)。
23#[repr(u8)]
24#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
25pub enum PointerKind {
26    /// 触摸屏 / 手指触控(默认启用跟手拖拽滚动、松手惯性滚动与左边缘右滑返回手势)。
27    #[default]
28    Touch = 0,
29    /// 桌面鼠标 / 触控板指针(默认仅触发显式 `.on_drag(...)` 控件,禁止在空白可滚动页面上按住左键拖动画布)。
30    Mouse = 1,
31    /// 手写笔 / 压感笔(按精确指针处理,禁止在空白滚动区误触发拖拽滚屏)。
32    Pen = 2,
33}
34
35/// 鼠标光标视觉形状(对应 `wp-cursor-shape-v1`、CSS Cursor 与桌面平台标准光标形态)。
36#[repr(u8)]
37#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
38pub enum CursorShape {
39    /// 默认箭头光标。
40    #[default]
41    Default = 0,
42    /// 手型点击光标(链接/按钮/复选框/开关/页签悬停)。
43    Pointer = 1,
44    /// I 型文本编辑光标(输入框/代码编辑器)。
45    Text = 2,
46    /// 可抓取手型光标(滑块/拖拽把手悬停)。
47    Grab = 3,
48    /// 抓取拖拽中光标(滑块/把手按住拖拽)。
49    Grabbing = 4,
50    /// 禁止/禁用不可交互光标(`disabled` 控件悬停)。
51    NotAllowed = 5,
52    /// 加载/后台处理进度光标(`loading` 按钮悬停)。
53    Progress = 6,
54    /// 四向移动光标。
55    Move = 7,
56    /// 十字准星光标(取色器/画布区域选择)。
57    Crosshair = 8,
58    /// 垂直双向调整尺寸光标(上下分隔条/上下边框)。
59    ResizeNS = 9,
60    /// 水平双向调整尺寸光标(左右分隔条/左右边框)。
61    ResizeEW = 10,
62    /// 对角线调整尺寸光标(左上-右下)。
63    ResizeNWSE = 11,
64    /// 对角线调整尺寸光标(右上-左下)。
65    ResizeNESW = 12,
66}
67
68impl CursorShape {
69    /// 返回标准 CSS `cursor` 属性名(便于 WebAssembly `<canvas>` 直接同步浏览器光标)。
70    #[inline(always)]
71    pub const fn as_css_str(self) -> &'static str {
72        match self {
73            Self::Default => "default",
74            Self::Pointer => "pointer",
75            Self::Text => "text",
76            Self::Grab => "grab",
77            Self::Grabbing => "grabbing",
78            Self::NotAllowed => "not-allowed",
79            Self::Progress => "progress",
80            Self::Move => "move",
81            Self::Crosshair => "crosshair",
82            Self::ResizeNS => "ns-resize",
83            Self::ResizeEW => "ew-resize",
84            Self::ResizeNWSE => "nwse-resize",
85            Self::ResizeNESW => "nesw-resize",
86        }
87    }
88}
89
90/// 触摸/鼠标快速滑动(Swipe)手势方向。
91#[derive(Copy, Clone, PartialEq, Eq, Debug)]
92pub enum SwipeDir {
93    /// 向左快速滑动(如进入下一页)。
94    Left,
95    /// 向右快速滑动(如边缘返回上一页)。
96    Right,
97    /// 向上快速滑动(如呼出底部通知栏)。
98    Up,
99    /// 向下快速滑动(如拉出顶部控制中心)。
100    Down,
101}
102
103/// 触摸/鼠标拖拽事件参数。
104#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
105pub struct DragEvent {
106    /// 当前指针屏幕坐标 X。
107    pub x: i16,
108    /// 当前指针屏幕坐标 Y。
109    pub y: i16,
110    /// 相对目标元素左边界的局部坐标 X。
111    pub local_x: i16,
112    /// 相对目标元素上边界的局部坐标 Y。
113    pub local_y: i16,
114    /// 目标元素宽度(便于计算百分比进度,如 `Slider`)。
115    pub width: i16,
116    /// 目标元素高度。
117    pub height: i16,
118    /// 相对上一帧的位移增量 X。
119    pub delta_x: i16,
120    /// 相对上一帧的位移增量 Y。
121    pub delta_y: i16,
122}
123
124/// 滚轮缩放/滚动事件参数(供交互画布实现 Zoom-to-Cursor 等精细控制)。
125#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
126pub struct WheelEvent {
127    /// 指针屏幕坐标 X。
128    pub x: i16,
129    /// 指针屏幕坐标 Y。
130    pub y: i16,
131    /// 相对目标元素左边界的局部坐标 X。
132    pub local_x: i16,
133    /// 相对目标元素上边界的局部坐标 Y。
134    pub local_y: i16,
135    /// 目标元素宽度。
136    pub width: i16,
137    /// 目标元素高度。
138    pub height: i16,
139    /// 水平滚轮增量(像素步长)。
140    pub delta_x: i16,
141    /// 垂直滚轮增量(像素步长;负值通常代表向上滚/放大)。
142    pub delta_y: i16,
143}
144
145/// 双指捏合缩放手势事件参数(支持触控板 `PinchGesture` 与移动端多点触控)。
146#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
147pub struct PinchEvent {
148    /// 缩放中心屏幕坐标 X。
149    pub x: i16,
150    /// 缩放中心屏幕坐标 Y。
151    pub y: i16,
152    /// 相对目标元素左边界的局部中心坐标 X。
153    pub local_x: i16,
154    /// 相对目标元素上边界的局部中心坐标 Y。
155    pub local_y: i16,
156    /// 目标元素宽度。
157    pub width: i16,
158    /// 目标元素高度。
159    pub height: i16,
160    /// 缩放相对增量 × 1000(如 `+100` 表示放大 10%,`-100` 表示缩小 10%)。
161    pub delta_scale_x1000: i16,
162}
163
164/// 物理按键与旋转编码器导航键值(面向无触摸屏 MCU 与桌面键盘)。
165#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
166pub enum KeyCode {
167    /// 切换焦点至下一个可聚焦元素(`Tab` 或旋钮顺时针)。
168    Tab,
169    /// 切换焦点至上一个可聚焦元素(`Shift+Tab` 或旋钮逆时针)。
170    BackTab,
171    /// 方向键:上。
172    Up,
173    /// 方向键:下。
174    Down,
175    /// 方向键:左。
176    Left,
177    /// 方向键:右。
178    Right,
179    /// 确认/触发当前焦点元素(`Enter` 或旋钮按下)。
180    Enter,
181    /// 空格触发。
182    Space,
183    /// 返回/取消。
184    Escape,
185    /// 退格键(删除光标前字符)。
186    Backspace,
187    /// 删除键(删除光标后字符)。
188    Delete,
189    /// 移至行首。
190    Home,
191    /// 移至行尾。
192    End,
193    /// 向上翻页(`PageUp`)。
194    PageUp,
195    /// 向下翻页(`PageDown`)。
196    PageDown,
197    /// 字符输入。
198    Char(char),
199}
200
201/// 键盘修饰键状态位掩码(紧凑 1 字节表示,支持 Shift / Ctrl / Alt / Super)。
202#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
203pub struct KeyModifiers(pub u8);
204
205impl KeyModifiers {
206    /// 无修饰键按下。
207    pub const NONE: Self = Self(0);
208    /// `Shift` 修饰键。
209    pub const SHIFT: Self = Self(1 << 0);
210    /// `Ctrl` 修饰键。
211    pub const CTRL: Self = Self(1 << 1);
212    /// `Alt` 修饰键。
213    pub const ALT: Self = Self(1 << 2);
214    /// `Super` / `Logo` / `Meta` 修饰键(窗口管理器默认主修饰键)。
215    pub const LOGO: Self = Self(1 << 3);
216
217    /// 是否没有任何修饰键按下。
218    #[inline(always)]
219    pub const fn is_empty(self) -> bool {
220        self.0 == 0
221    }
222
223    /// 是否按下了 `Shift`。
224    #[inline(always)]
225    pub const fn shift(self) -> bool {
226        (self.0 & Self::SHIFT.0) != 0
227    }
228
229    /// 是否按下了 `Ctrl`。
230    #[inline(always)]
231    pub const fn ctrl(self) -> bool {
232        (self.0 & Self::CTRL.0) != 0
233    }
234
235    /// 是否按下了 `Alt`。
236    #[inline(always)]
237    pub const fn alt(self) -> bool {
238        (self.0 & Self::ALT.0) != 0
239    }
240
241    /// 是否按下了 `Super` / `Logo`。
242    #[inline(always)]
243    pub const fn logo(self) -> bool {
244        (self.0 & Self::LOGO.0) != 0
245    }
246
247    /// 合并修饰键集合。
248    #[inline(always)]
249    pub const fn union(self, other: Self) -> Self {
250        Self(self.0 | other.0)
251    }
252
253    /// 添加指定修饰键标志(等价于 [`Self::union`])。
254    #[inline(always)]
255    pub const fn with(self, other: Self) -> Self {
256        Self(self.0 | other.0)
257    }
258
259    /// 移除指定修饰键。
260    #[inline(always)]
261    pub const fn without(self, other: Self) -> Self {
262        Self(self.0 & !other.0)
263    }
264
265    /// 是否包含命令快捷键修饰键(`Super`/`Logo`、`Alt` 或 `Ctrl`),用于区分普通打字输入与全局快捷键。
266    #[inline(always)]
267    pub const fn has_shortcut_modifier(self) -> bool {
268        (self.0 & (Self::LOGO.0 | Self::ALT.0 | Self::CTRL.0)) != 0
269    }
270}
271
272/// 移动端与异形屏安全区域内边距(状态栏、刘海屏/灵动岛、圆角边缘与底部 Home Indicator)。
273#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
274pub struct SafeAreaInsets {
275    pub top: i16,
276    pub right: i16,
277    pub bottom: i16,
278    pub left: i16,
279}
280
281impl SafeAreaInsets {
282    /// 全零安全区域常量(适用于 `const fn` 零栈初始化)。
283    pub const ZERO: Self = Self {
284        top: 0,
285        right: 0,
286        bottom: 0,
287        left: 0,
288    };
289
290    /// 创建四向安全区域内边距 `(top, right, bottom, left)`。
291    #[inline(always)]
292    pub const fn new(top: i16, right: i16, bottom: i16, left: i16) -> Self {
293        Self {
294            top,
295            right,
296            bottom,
297            left,
298        }
299    }
300
301    /// 常见全面屏手机竖屏默认安全区域(顶部状态栏/灵动岛 `44px`,底部 Home Indicator `34px`)。
302    #[inline(always)]
303    pub const fn phone_portrait() -> Self {
304        Self {
305            top: 44,
306            right: 0,
307            bottom: 34,
308            left: 0,
309        }
310    }
311
312    /// 常见全面屏手机横屏默认安全区域(左右异形区 `44px`,底部 Home Indicator `21px`)。
313    #[inline(always)]
314    pub const fn phone_landscape() -> Self {
315        Self {
316            top: 0,
317            right: 44,
318            bottom: 21,
319            left: 44,
320        }
321    }
322
323    /// Android 全面屏手机竖屏默认安全区域(状态栏/挖孔屏 `48px`,底部手势导航条 `34px`)。
324    #[inline(always)]
325    pub const fn android_portrait() -> Self {
326        Self {
327            top: 48,
328            right: 0,
329            bottom: 34,
330            left: 0,
331        }
332    }
333
334    /// Android 全面屏手机横屏默认安全区域(左侧挖孔区 `48px`,底部手势横条 `24px`)。
335    #[inline(always)]
336    pub const fn android_landscape() -> Self {
337        Self {
338            top: 0,
339            right: 0,
340            bottom: 24,
341            left: 48,
342        }
343    }
344
345    /// iOS (iPhone 灵动岛/全面屏) 竖屏默认安全区域(顶部灵动岛 `59pt`,底部 Home Indicator `34pt`)。
346    #[inline(always)]
347    pub const fn ios_portrait() -> Self {
348        Self {
349            top: 59,
350            right: 0,
351            bottom: 34,
352            left: 0,
353        }
354    }
355
356    /// iOS (iPhone 灵动岛/全面屏) 横屏默认安全区域(左右安全区 `59pt`,底部 Home Indicator `21pt`)。
357    #[inline(always)]
358    pub const fn ios_landscape() -> Self {
359        Self {
360            top: 0,
361            right: 59,
362            bottom: 21,
363            left: 59,
364        }
365    }
366
367    /// 鸿蒙 (HarmonyOS NEXT / OpenHarmony) 手机竖屏默认避让区域(顶部状态栏 `38vp`,底部导航条 `28vp`)。
368    #[inline(always)]
369    pub const fn harmony_portrait() -> Self {
370        Self {
371            top: 38,
372            right: 0,
373            bottom: 28,
374            left: 0,
375        }
376    }
377
378    /// 鸿蒙 (HarmonyOS NEXT / OpenHarmony) 手机横屏默认避让区域(左右避让区 `38vp`,底部导航条 `20vp`)。
379    #[inline(always)]
380    pub const fn harmony_landscape() -> Self {
381        Self {
382            top: 0,
383            right: 38,
384            bottom: 20,
385            left: 38,
386        }
387    }
388}
389
390/// 零堆内联 IME 组合输入预编辑(Preedit)负载(支持最多 64 字节 UTF-8 组合串与选区光标区间)。
391///
392/// 100% 兼容 `#![no_std]` 与 `Copy`,用于中文拼音、日文假名、韩文等输入法实时预编辑展示。
393#[derive(Copy, Clone, PartialEq, Eq, Debug)]
394pub struct ImePreedit {
395    pub buf: [u8; 64],
396    pub len: u8,
397    pub cursor_start: u8,
398    pub cursor_end: u8,
399    pub has_cursor: bool,
400}
401
402impl Default for ImePreedit {
403    #[inline(always)]
404    fn default() -> Self {
405        Self::empty()
406    }
407}
408
409impl ImePreedit {
410    /// 空预编辑状态(表示当前未处于组合输入中)。
411    #[inline(always)]
412    pub const fn empty() -> Self {
413        Self {
414            buf: [0; 64],
415            len: 0,
416            cursor_start: 0,
417            cursor_end: 0,
418            has_cursor: false,
419        }
420    }
421
422    /// 从 UTF-8 字符串与可选字节级光标选区构造内联预编辑状态(超长时按 UTF-8 字符边界安全截断)。
423    pub fn from_str(text: &str, cursor: Option<(usize, usize)>) -> Self {
424        let mut out = Self::empty();
425        let bytes = text.as_bytes();
426        let mut copy_len = bytes.len().min(64);
427        while copy_len > 0 && !text.is_char_boundary(copy_len) {
428            copy_len -= 1;
429        }
430        out.buf[..copy_len].copy_from_slice(&bytes[..copy_len]);
431        out.len = copy_len as u8;
432        if let Some((s, e)) = cursor {
433            out.cursor_start = s.min(copy_len) as u8;
434            out.cursor_end = e.min(copy_len) as u8;
435            out.has_cursor = true;
436        }
437        out
438    }
439
440    /// 返回当前预编辑 UTF-8 字符串切片。
441    #[inline]
442    pub fn as_str(&self) -> &str {
443        let n = (self.len as usize).min(64);
444        core::str::from_utf8(&self.buf[..n]).unwrap_or("")
445    }
446
447    /// 当前是否无正在组合的预编辑文本。
448    #[inline(always)]
449    pub const fn is_empty(&self) -> bool {
450        self.len == 0
451    }
452
453    /// 返回预编辑串内的可选光标/高亮字节区间 `(start, end)`。
454    #[inline(always)]
455    pub const fn cursor(&self) -> Option<(usize, usize)> {
456        if self.has_cursor {
457            Some((self.cursor_start as usize, self.cursor_end as usize))
458        } else {
459            None
460        }
461    }
462}
463
464/// 统一输入事件枚举(支持完整触摸手势生命周期、滚轮、物理按键焦点导航、原生 IME 组合输入、移动端安全区与系统返回)。
465#[derive(Copy, Clone, PartialEq, Eq, Debug)]
466pub enum InputEvent {
467    /// 指针/手指按下。
468    PointerDown { x: i16, y: i16 },
469    /// 指针/手指移动或拖拽。
470    PointerMove { x: i16, y: i16 },
471    /// 指针/手指抬起。
472    PointerUp { x: i16, y: i16 },
473    /// 鼠标右键(次键)按下。
474    PointerSecondaryDown { x: i16, y: i16 },
475    /// 鼠标右键(次键)抬起。
476    PointerSecondaryUp { x: i16, y: i16 },
477    /// 指针移出当前窗口/表面区域(清除悬停状态)。
478    PointerLeave,
479    /// 指针/手势取消。
480    PointerCancel,
481    /// 更新当前指针输入设备来源类型(区分桌面 `Mouse` / `Pen` 与触控屏 `Touch`)。
482    PointerKindChanged(PointerKind),
483    /// 鼠标滚轮或编码器滚动。
484    Scroll {
485        x: i16,
486        y: i16,
487        delta_y: i16,
488    },
489    /// 水平滚轮滚动。
490    ScrollX {
491        x: i16,
492        y: i16,
493        delta_x: i16,
494    },
495    /// 物理按键或旋钮按键按下。
496    KeyDown(KeyCode),
497    /// 物理按键释放。
498    KeyUp(KeyCode),
499    /// 键盘修饰键状态变更(Ctrl / Alt / Shift / Super)。
500    ModifiersChanged(KeyModifiers),
501    /// 退格删除一个字符(面向处于聚焦态的文本输入框)。
502    Backspace,
503    /// 删除光标后一个字符(`Delete` 键)。
504    Delete,
505    /// 输入法(IME)提交单个字符。
506    TextCommit(char),
507    /// 系统输入法(IME)会话激活。
508    ImeEnabled,
509    /// 系统输入法(IME)会话关闭/失焦(清空预编辑态)。
510    ImeDisabled,
511    /// 系统输入法(IME)组合输入预编辑文本更新(如拼音 `"zhongwen"` 及选区光标)。
512    ImePreedit(ImePreedit),
513    /// 系统级返回导航请求(Android 返回键 / 预测性返回手势 / iOS 左边缘右滑返回 / Web 浏览器 `popstate`)。
514    NavigateBack,
515    /// 移动端异形屏安全区域发生变更(如屏幕旋转、状态栏显隐切换)。
516    SafeAreaChanged(SafeAreaInsets),
517    /// 移动端屏幕软键盘弹出高度发生变更(`0` 表示软键盘收起)。
518    KeyboardInsetChanged(i16),
519    /// 双指捏合/触控板缩放手势(`delta_scale_x1000` 为千分比增量,如 `+50` 表示放大 5%)。
520    Pinch {
521        x: i16,
522        y: i16,
523        delta_scale_x1000: i16,
524    },
525    /// 双指旋转手势(`delta_deg10` 单位 `0.1°` 顺时针增量)。
526    Rotate {
527        x: i16,
528        y: i16,
529        delta_deg10: i16,
530    },
531    /// 触控板高精度像素级双轴平滑滚动。
532    ScrollPixel {
533        x: i16,
534        y: i16,
535        dx: i16,
536        dy: i16,
537    },
538    /// 外部 OS 文件拖拽悬停在窗口上方。
539    FileHovered { x: i16, y: i16 },
540    /// 外部 OS 文件在窗口指定坐标处放下(路径列表通过引擎 `dropped_files()` 读取)。
541    FileDropped { x: i16, y: i16 },
542    /// 外部 OS 文件拖拽离开窗口取消。
543    FileHoverCancelled,
544    /// 操作系统深浅色主题热切换通知。
545    SystemThemeChanged { dark: bool },
546}
547
548/// 标准 W3C 事件传播阶段(捕获 -> 目标 -> 冒泡)。
549#[repr(u8)]
550#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
551pub enum EventPhase {
552    Capture = 0,
553    #[default]
554    Target = 1,
555    Bubble = 2,
556}
557
558/// 跨控件应用内拖拽传输载荷(轻量 POD,无堆)。
559#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
560pub struct DragPayload {
561    /// 载荷类别标签(如 `1 = Tab`, `2 = TreeNode`, `3 = File`)。
562    pub kind_tag: u32,
563    /// 业务自定义数据 ID。
564    pub payload_id: u64,
565    /// 发起拖拽的源控件 ID。
566    pub source_id: u32,
567}
568