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/// 物理按键与旋转编码器导航键值(面向无触摸屏 MCU 与桌面键盘)。
125#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
126pub enum KeyCode {
127    /// 切换焦点至下一个可聚焦元素(`Tab` 或旋钮顺时针)。
128    Tab,
129    /// 切换焦点至上一个可聚焦元素(`Shift+Tab` 或旋钮逆时针)。
130    BackTab,
131    /// 方向键:上。
132    Up,
133    /// 方向键:下。
134    Down,
135    /// 方向键:左。
136    Left,
137    /// 方向键:右。
138    Right,
139    /// 确认/触发当前焦点元素(`Enter` 或旋钮按下)。
140    Enter,
141    /// 空格触发。
142    Space,
143    /// 返回/取消。
144    Escape,
145    /// 退格键(删除光标前字符)。
146    Backspace,
147    /// 删除键(删除光标后字符)。
148    Delete,
149    /// 移至行首。
150    Home,
151    /// 移至行尾。
152    End,
153    /// 向上翻页(`PageUp`)。
154    PageUp,
155    /// 向下翻页(`PageDown`)。
156    PageDown,
157    /// 字符输入。
158    Char(char),
159}
160
161/// 键盘修饰键状态位掩码(紧凑 1 字节表示,支持 Shift / Ctrl / Alt / Super)。
162#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
163pub struct KeyModifiers(pub u8);
164
165impl KeyModifiers {
166    /// 无修饰键按下。
167    pub const NONE: Self = Self(0);
168    /// `Shift` 修饰键。
169    pub const SHIFT: Self = Self(1 << 0);
170    /// `Ctrl` 修饰键。
171    pub const CTRL: Self = Self(1 << 1);
172    /// `Alt` 修饰键。
173    pub const ALT: Self = Self(1 << 2);
174    /// `Super` / `Logo` / `Meta` 修饰键(窗口管理器默认主修饰键)。
175    pub const LOGO: Self = Self(1 << 3);
176
177    /// 是否没有任何修饰键按下。
178    #[inline(always)]
179    pub const fn is_empty(self) -> bool {
180        self.0 == 0
181    }
182
183    /// 是否按下了 `Shift`。
184    #[inline(always)]
185    pub const fn shift(self) -> bool {
186        (self.0 & Self::SHIFT.0) != 0
187    }
188
189    /// 是否按下了 `Ctrl`。
190    #[inline(always)]
191    pub const fn ctrl(self) -> bool {
192        (self.0 & Self::CTRL.0) != 0
193    }
194
195    /// 是否按下了 `Alt`。
196    #[inline(always)]
197    pub const fn alt(self) -> bool {
198        (self.0 & Self::ALT.0) != 0
199    }
200
201    /// 是否按下了 `Super` / `Logo`。
202    #[inline(always)]
203    pub const fn logo(self) -> bool {
204        (self.0 & Self::LOGO.0) != 0
205    }
206
207    /// 合并修饰键集合。
208    #[inline(always)]
209    pub const fn union(self, other: Self) -> Self {
210        Self(self.0 | other.0)
211    }
212
213    /// 添加指定修饰键标志(等价于 [`Self::union`])。
214    #[inline(always)]
215    pub const fn with(self, other: Self) -> Self {
216        Self(self.0 | other.0)
217    }
218
219    /// 移除指定修饰键。
220    #[inline(always)]
221    pub const fn without(self, other: Self) -> Self {
222        Self(self.0 & !other.0)
223    }
224
225    /// 是否包含命令快捷键修饰键(`Super`/`Logo`、`Alt` 或 `Ctrl`),用于区分普通打字输入与全局快捷键。
226    #[inline(always)]
227    pub const fn has_shortcut_modifier(self) -> bool {
228        (self.0 & (Self::LOGO.0 | Self::ALT.0 | Self::CTRL.0)) != 0
229    }
230}
231
232/// 移动端与异形屏安全区域内边距(状态栏、刘海屏/灵动岛、圆角边缘与底部 Home Indicator)。
233#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
234pub struct SafeAreaInsets {
235    pub top: i16,
236    pub right: i16,
237    pub bottom: i16,
238    pub left: i16,
239}
240
241impl SafeAreaInsets {
242    /// 全零安全区域常量(适用于 `const fn` 零栈初始化)。
243    pub const ZERO: Self = Self {
244        top: 0,
245        right: 0,
246        bottom: 0,
247        left: 0,
248    };
249
250    /// 创建四向安全区域内边距 `(top, right, bottom, left)`。
251    #[inline(always)]
252    pub const fn new(top: i16, right: i16, bottom: i16, left: i16) -> Self {
253        Self {
254            top,
255            right,
256            bottom,
257            left,
258        }
259    }
260
261    /// 常见全面屏手机竖屏默认安全区域(顶部状态栏/灵动岛 `44px`,底部 Home Indicator `34px`)。
262    #[inline(always)]
263    pub const fn phone_portrait() -> Self {
264        Self {
265            top: 44,
266            right: 0,
267            bottom: 34,
268            left: 0,
269        }
270    }
271
272    /// 常见全面屏手机横屏默认安全区域(左右异形区 `44px`,底部 Home Indicator `21px`)。
273    #[inline(always)]
274    pub const fn phone_landscape() -> Self {
275        Self {
276            top: 0,
277            right: 44,
278            bottom: 21,
279            left: 44,
280        }
281    }
282
283    /// Android 全面屏手机竖屏默认安全区域(状态栏/挖孔屏 `48px`,底部手势导航条 `34px`)。
284    #[inline(always)]
285    pub const fn android_portrait() -> Self {
286        Self {
287            top: 48,
288            right: 0,
289            bottom: 34,
290            left: 0,
291        }
292    }
293
294    /// Android 全面屏手机横屏默认安全区域(左侧挖孔区 `48px`,底部手势横条 `24px`)。
295    #[inline(always)]
296    pub const fn android_landscape() -> Self {
297        Self {
298            top: 0,
299            right: 0,
300            bottom: 24,
301            left: 48,
302        }
303    }
304
305    /// iOS (iPhone 灵动岛/全面屏) 竖屏默认安全区域(顶部灵动岛 `59pt`,底部 Home Indicator `34pt`)。
306    #[inline(always)]
307    pub const fn ios_portrait() -> Self {
308        Self {
309            top: 59,
310            right: 0,
311            bottom: 34,
312            left: 0,
313        }
314    }
315
316    /// iOS (iPhone 灵动岛/全面屏) 横屏默认安全区域(左右安全区 `59pt`,底部 Home Indicator `21pt`)。
317    #[inline(always)]
318    pub const fn ios_landscape() -> Self {
319        Self {
320            top: 0,
321            right: 59,
322            bottom: 21,
323            left: 59,
324        }
325    }
326
327    /// 鸿蒙 (HarmonyOS NEXT / OpenHarmony) 手机竖屏默认避让区域(顶部状态栏 `38vp`,底部导航条 `28vp`)。
328    #[inline(always)]
329    pub const fn harmony_portrait() -> Self {
330        Self {
331            top: 38,
332            right: 0,
333            bottom: 28,
334            left: 0,
335        }
336    }
337
338    /// 鸿蒙 (HarmonyOS NEXT / OpenHarmony) 手机横屏默认避让区域(左右避让区 `38vp`,底部导航条 `20vp`)。
339    #[inline(always)]
340    pub const fn harmony_landscape() -> Self {
341        Self {
342            top: 0,
343            right: 38,
344            bottom: 20,
345            left: 38,
346        }
347    }
348}
349
350/// 零堆内联 IME 组合输入预编辑(Preedit)负载(支持最多 64 字节 UTF-8 组合串与选区光标区间)。
351///
352/// 100% 兼容 `#![no_std]` 与 `Copy`,用于中文拼音、日文假名、韩文等输入法实时预编辑展示。
353#[derive(Copy, Clone, PartialEq, Eq, Debug)]
354pub struct ImePreedit {
355    pub buf: [u8; 64],
356    pub len: u8,
357    pub cursor_start: u8,
358    pub cursor_end: u8,
359    pub has_cursor: bool,
360}
361
362impl Default for ImePreedit {
363    #[inline(always)]
364    fn default() -> Self {
365        Self::empty()
366    }
367}
368
369impl ImePreedit {
370    /// 空预编辑状态(表示当前未处于组合输入中)。
371    #[inline(always)]
372    pub const fn empty() -> Self {
373        Self {
374            buf: [0; 64],
375            len: 0,
376            cursor_start: 0,
377            cursor_end: 0,
378            has_cursor: false,
379        }
380    }
381
382    /// 从 UTF-8 字符串与可选字节级光标选区构造内联预编辑状态(超长时按 UTF-8 字符边界安全截断)。
383    pub fn from_str(text: &str, cursor: Option<(usize, usize)>) -> Self {
384        let mut out = Self::empty();
385        let bytes = text.as_bytes();
386        let mut copy_len = bytes.len().min(64);
387        while copy_len > 0 && !text.is_char_boundary(copy_len) {
388            copy_len -= 1;
389        }
390        out.buf[..copy_len].copy_from_slice(&bytes[..copy_len]);
391        out.len = copy_len as u8;
392        if let Some((s, e)) = cursor {
393            out.cursor_start = s.min(copy_len) as u8;
394            out.cursor_end = e.min(copy_len) as u8;
395            out.has_cursor = true;
396        }
397        out
398    }
399
400    /// 返回当前预编辑 UTF-8 字符串切片。
401    #[inline]
402    pub fn as_str(&self) -> &str {
403        let n = (self.len as usize).min(64);
404        core::str::from_utf8(&self.buf[..n]).unwrap_or("")
405    }
406
407    /// 当前是否无正在组合的预编辑文本。
408    #[inline(always)]
409    pub const fn is_empty(&self) -> bool {
410        self.len == 0
411    }
412
413    /// 返回预编辑串内的可选光标/高亮字节区间 `(start, end)`。
414    #[inline(always)]
415    pub const fn cursor(&self) -> Option<(usize, usize)> {
416        if self.has_cursor {
417            Some((self.cursor_start as usize, self.cursor_end as usize))
418        } else {
419            None
420        }
421    }
422}
423
424/// 统一输入事件枚举(支持完整触摸手势生命周期、滚轮、物理按键焦点导航、原生 IME 组合输入、移动端安全区与系统返回)。
425#[derive(Copy, Clone, PartialEq, Eq, Debug)]
426pub enum InputEvent {
427    /// 指针/手指按下。
428    PointerDown { x: i16, y: i16 },
429    /// 指针/手指移动或拖拽。
430    PointerMove { x: i16, y: i16 },
431    /// 指针/手指抬起。
432    PointerUp { x: i16, y: i16 },
433    /// 鼠标右键(次键)按下。
434    PointerSecondaryDown { x: i16, y: i16 },
435    /// 鼠标右键(次键)抬起。
436    PointerSecondaryUp { x: i16, y: i16 },
437    /// 指针移出当前窗口/表面区域(清除悬停状态)。
438    PointerLeave,
439    /// 指针/手势取消。
440    PointerCancel,
441    /// 更新当前指针输入设备来源类型(区分桌面 `Mouse` / `Pen` 与触控屏 `Touch`)。
442    PointerKindChanged(PointerKind),
443    /// 鼠标滚轮或编码器滚动。
444    Scroll {
445        x: i16,
446        y: i16,
447        delta_y: i16,
448    },
449    /// 水平滚轮滚动。
450    ScrollX {
451        x: i16,
452        y: i16,
453        delta_x: i16,
454    },
455    /// 物理按键或旋钮按键按下。
456    KeyDown(KeyCode),
457    /// 物理按键释放。
458    KeyUp(KeyCode),
459    /// 键盘修饰键状态变更(Ctrl / Alt / Shift / Super)。
460    ModifiersChanged(KeyModifiers),
461    /// 退格删除一个字符(面向处于聚焦态的文本输入框)。
462    Backspace,
463    /// 删除光标后一个字符(`Delete` 键)。
464    Delete,
465    /// 输入法(IME)提交单个字符。
466    TextCommit(char),
467    /// 系统输入法(IME)会话激活。
468    ImeEnabled,
469    /// 系统输入法(IME)会话关闭/失焦(清空预编辑态)。
470    ImeDisabled,
471    /// 系统输入法(IME)组合输入预编辑文本更新(如拼音 `"zhongwen"` 及选区光标)。
472    ImePreedit(ImePreedit),
473    /// 系统级返回导航请求(Android 返回键 / 预测性返回手势 / iOS 左边缘右滑返回 / Web 浏览器 `popstate`)。
474    NavigateBack,
475    /// 移动端异形屏安全区域发生变更(如屏幕旋转、状态栏显隐切换)。
476    SafeAreaChanged(SafeAreaInsets),
477    /// 移动端屏幕软键盘弹出高度发生变更(`0` 表示软键盘收起)。
478    KeyboardInsetChanged(i16),
479    /// 双指捏合/触控板缩放手势(`delta_scale_x1000` 为千分比增量,如 `+50` 表示放大 5%)。
480    Pinch {
481        x: i16,
482        y: i16,
483        delta_scale_x1000: i16,
484    },
485    /// 双指旋转手势(`delta_deg10` 单位 `0.1°` 顺时针增量)。
486    Rotate {
487        x: i16,
488        y: i16,
489        delta_deg10: i16,
490    },
491    /// 触控板高精度像素级双轴平滑滚动。
492    ScrollPixel {
493        x: i16,
494        y: i16,
495        dx: i16,
496        dy: i16,
497    },
498    /// 外部 OS 文件拖拽悬停在窗口上方。
499    FileHovered { x: i16, y: i16 },
500    /// 外部 OS 文件在窗口指定坐标处放下(路径列表通过引擎 `dropped_files()` 读取)。
501    FileDropped { x: i16, y: i16 },
502    /// 外部 OS 文件拖拽离开窗口取消。
503    FileHoverCancelled,
504    /// 操作系统深浅色主题热切换通知。
505    SystemThemeChanged { dark: bool },
506}
507
508/// 标准 W3C 事件传播阶段(捕获 -> 目标 -> 冒泡)。
509#[repr(u8)]
510#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
511pub enum EventPhase {
512    Capture = 0,
513    #[default]
514    Target = 1,
515    Bubble = 2,
516}
517
518/// 跨控件应用内拖拽传输载荷(轻量 POD,无堆)。
519#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
520pub struct DragPayload {
521    /// 载荷类别标签(如 `1 = Tab`, `2 = TreeNode`, `3 = File`)。
522    pub kind_tag: u32,
523    /// 业务自定义数据 ID。
524    pub payload_id: u64,
525    /// 发起拖拽的源控件 ID。
526    pub source_id: u32,
527}
528