use crate::window::ThemeMode;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left,
Right,
Middle,
Other(u16),
}
impl MouseButton {
pub fn to_button_index(&self) -> i16 {
match self {
MouseButton::Left => 0,
MouseButton::Middle => 1,
MouseButton::Right => 2,
MouseButton::Other(b) => *b as i16,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KeyCode {
KeyA,
KeyB,
KeyC,
KeyD,
KeyE,
KeyF,
KeyG,
KeyH,
KeyI,
KeyJ,
KeyK,
KeyL,
KeyM,
KeyN,
KeyO,
KeyP,
KeyQ,
KeyR,
KeyS,
KeyT,
KeyU,
KeyV,
KeyW,
KeyX,
KeyY,
KeyZ,
Digit0,
Digit1,
Digit2,
Digit3,
Digit4,
Digit5,
Digit6,
Digit7,
Digit8,
Digit9,
Escape,
Enter,
Tab,
Space,
Backspace,
Delete,
ArrowUp,
ArrowDown,
ArrowLeft,
ArrowRight,
Home,
End,
PageUp,
PageDown,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Slash,
Comma,
Period,
Minus,
Equals,
BracketLeft,
BracketRight,
Semicolon,
Quote,
Backslash,
Backquote,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool, }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TouchPhase {
Started,
Moved,
Ended,
Cancelled,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PlatformEvent {
WindowResized {
width: u32,
height: u32,
},
WindowMoved {
x: i32,
y: i32,
},
WindowCloseRequested,
WindowFocused(bool),
WindowMaximized(bool),
ScaleFactorChanged {
scale_factor: f32,
},
ThemeChanged(ThemeMode),
RedrawRequested,
AnimationTick,
MouseMove {
x: f32,
y: f32,
},
MouseDown {
button: MouseButton,
x: f32,
y: f32,
},
MouseUp {
button: MouseButton,
x: f32,
y: f32,
},
DoubleClick {
button: MouseButton,
x: f32,
y: f32,
},
Scroll {
delta_x: f32,
delta_y: f32,
is_momentum: bool,
},
CursorEntered,
CursorLeft,
KeyDown {
key: KeyCode,
modifiers: Modifiers,
},
KeyUp {
key: KeyCode,
modifiers: Modifiers,
},
ReceivedCharacter(char),
ImePreedit {
text: String,
cursor: Option<(usize, usize)>,
},
ImeCommit(String),
ImeDeleteSurrounding {
offset: i32,
n_chars: u32,
},
Touch {
touch_id: u64,
phase: TouchPhase,
x: f32,
y: f32,
},
Stylus {
phase: StylusPhase,
pointer_id: u64,
tool: PointerKind,
x: f32,
y: f32,
pressure: f32,
tilt_x: f32,
tilt_y: f32,
twist: f32,
tangential_pressure: f32,
hovering: bool,
buttons: u16,
},
FileDrop(Vec<PathBuf>),
TrayIconClicked,
TrayMenuItemClicked(u64),
SingleInstanceTriggered(Vec<String>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PointerKind {
Mouse,
Touch,
Pen,
Eraser,
Brush,
Airbrush,
}
impl PointerKind {
pub const fn as_w3c_str(self) -> &'static str {
match self {
Self::Mouse => "mouse",
Self::Touch => "touch",
Self::Pen | Self::Eraser | Self::Brush | Self::Airbrush => "pen",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StylusPhase {
Down,
Move,
Up,
Hover,
Cancel,
}
pub fn decode_file_uri_to_path(uri: &str) -> Option<PathBuf> {
let trimmed = uri.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
return None;
}
let raw_path = if let Some(rest) = trimmed.strip_prefix("file://localhost") {
rest
} else if let Some(rest) = trimmed.strip_prefix("file://") {
rest
} else {
trimmed
};
if raw_path.is_empty() {
return None;
}
let bytes = raw_path.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let h1 = (bytes[i + 1] as char).to_digit(16);
let h2 = (bytes[i + 2] as char).to_digit(16);
if let (Some(d1), Some(d2)) = (h1, h2) {
decoded.push(((d1 << 4) | d2) as u8);
i += 3;
continue;
}
}
decoded.push(bytes[i]);
i += 1;
}
let s = String::from_utf8_lossy(&decoded).into_owned();
Some(PathBuf::from(s))
}
pub fn parse_dropped_uri_list_to_paths(payload: &str) -> Vec<PathBuf> {
payload
.lines()
.filter_map(decode_file_uri_to_path)
.collect()
}