use crate::geometry::Point;
pub const TOUCH_SLOP: f32 = 4.0;
pub const DOUBLE_TAP_TIMEOUT_MS: u64 = 300;
pub const LONG_PRESS_TIMEOUT_MS: u64 = 500;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum MouseButton {
#[default]
Left,
Right,
Middle,
Other(u16),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GestureState {
Possible,
Accepted,
Rejected,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct KeyModifiers(pub u8);
impl KeyModifiers {
pub const SHIFT: u8 = 1 << 0;
pub const CTRL: u8 = 1 << 1;
pub const ALT: u8 = 1 << 2;
pub const META: u8 = 1 << 3;
pub fn shift(&self) -> bool {
self.0 & Self::SHIFT != 0
}
pub fn ctrl(&self) -> bool {
self.0 & Self::CTRL != 0
}
pub fn alt(&self) -> bool {
self.0 & Self::ALT != 0
}
pub fn meta(&self) -> bool {
self.0 & Self::META != 0
}
pub fn none() -> Self {
KeyModifiers(0)
}
pub fn ctrl_only() -> Self {
KeyModifiers(Self::CTRL)
}
pub fn shift_only() -> Self {
KeyModifiers(Self::SHIFT)
}
pub fn command(&self) -> bool {
#[cfg(target_os = "macos")]
{
self.0 & Self::META != 0
}
#[cfg(not(target_os = "macos"))]
{
self.0 & Self::CTRL != 0
}
}
pub fn command_only() -> Self {
#[cfg(target_os = "macos")]
{
KeyModifiers(Self::META)
}
#[cfg(not(target_os = "macos"))]
{
KeyModifiers(Self::CTRL)
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyCode {
Enter,
Space,
Escape,
Tab,
BackTab,
LeftArrow,
RightArrow,
UpArrow,
DownArrow,
PageUp,
PageDown,
Backspace,
Delete,
Home,
End,
Char(char),
F3,
F11,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct PointerData {
pub position: Point,
pub timestamp_ms: u64,
pub button: MouseButton,
pub modifiers: KeyModifiers,
}
impl PointerData {
pub fn new(position: Point, timestamp_ms: u64) -> Self {
Self { position, timestamp_ms, button: MouseButton::default(), modifiers: KeyModifiers::none() }
}
pub fn is_primary(&self) -> bool {
self.button == MouseButton::Left
}
pub fn with_button(position: Point, timestamp_ms: u64, button: MouseButton) -> Self {
Self { position, timestamp_ms, button, modifiers: KeyModifiers::none() }
}
pub fn with_modifiers(mut self, modifiers: KeyModifiers) -> Self {
self.modifiers = modifiers;
self
}
pub fn at(position: Point) -> Self {
Self { position, timestamp_ms: 0, button: MouseButton::Left, modifiers: KeyModifiers::none() }
}
pub fn at_right(position: Point) -> Self {
Self { position, timestamp_ms: 0, button: MouseButton::Right, modifiers: KeyModifiers::none() }
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum WidgetEvent {
PointerDown(PointerData),
PointerUp(PointerData),
PointerMove(PointerData),
PointerLeave,
PointerEnter,
PointerCancel,
Scroll(f32, f32),
KeyDown(KeyCode, KeyModifiers),
KeyUp(KeyCode, KeyModifiers),
FocusGained,
FocusLost,
FocusWithin(bool),
ImeEnabled,
ImeDisabled,
ImePreedit(String, Option<(usize, usize)>),
ImeCommit(String),
SetText(String),
SetValue(f32),
SetExpanded {
item: Option<u64>,
expanded: bool,
},
SetTextSelection {
anchor: usize,
focus: usize,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn command_modifier_is_platform_aware() {
let command = KeyModifiers::command_only();
assert!(command.command());
#[cfg(target_os = "macos")]
{
assert!(command.meta() && !command.ctrl(), "macOS 命令键应为 META");
assert!(!KeyModifiers::ctrl_only().command(), "macOS 上 Ctrl 不是命令键");
}
#[cfg(not(target_os = "macos"))]
{
assert!(command.ctrl() && !command.meta(), "非 macOS 命令键应为 CTRL");
assert!(!KeyModifiers(KeyModifiers::META).command(), "非 macOS 上 META 不触发命令键");
}
}
}