use crate::ExternalPaths;
use gpui_types::{
Capslock, KeyDownEvent, KeyUpEvent, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent,
Pixels, Point, point,
};
use std::{any::Any, ops::Deref};
#[derive(Clone, Debug)]
pub struct TouchDragEvent {
pub phase: TouchPhase,
pub start_position: Point<Pixels>,
pub position: Point<Pixels>,
}
#[derive(Clone, Debug)]
pub struct LongPressEvent {
pub phase: TouchPhase,
pub start_position: Point<Pixels>,
pub position: Point<Pixels>,
}
impl Default for LongPressEvent {
fn default() -> Self {
Self {
phase: TouchPhase::Started,
start_position: Point::default(),
position: Point::default(),
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ModifiersChangedEvent {
pub modifiers: Modifiers,
pub capslock: Capslock,
}
impl Deref for ModifiersChangedEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TouchPhase {
Started,
#[default]
Moved,
Ended,
Cancelled,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TouchId(pub u64);
#[derive(Clone, Debug, Default)]
pub struct TouchEvent {
pub id: TouchId,
pub phase: TouchPhase,
pub position: Point<Pixels>,
pub predicted_position: Option<Point<Pixels>>,
pub force: Option<f32>,
}
#[derive(Clone, Debug, Default)]
pub struct MouseUpEvent {
pub button: MouseButton,
pub position: Point<Pixels>,
pub modifiers: Modifiers,
pub click_count: usize,
}
impl MouseUpEvent {
pub fn is_focusing(&self) -> bool {
matches!(self.button, MouseButton::Left)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum PressureStage {
#[default]
Zero,
Normal,
Force,
}
#[derive(Debug, Clone, Default)]
pub struct MousePressureEvent {
pub pressure: f32,
pub stage: PressureStage,
pub position: Point<Pixels>,
pub modifiers: Modifiers,
}
#[derive(Clone, Debug, Default)]
pub struct ScrollWheelEvent {
pub position: Point<Pixels>,
pub delta: ScrollDelta,
pub modifiers: Modifiers,
pub touch_phase: TouchPhase,
}
impl Deref for ScrollWheelEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Clone, Copy, Debug)]
pub enum ScrollDelta {
Pixels(Point<Pixels>),
Lines(Point<f32>),
}
impl Default for ScrollDelta {
fn default() -> Self {
Self::Lines(Default::default())
}
}
impl ScrollDelta {
pub fn precise(&self) -> bool {
match self {
ScrollDelta::Pixels(_) => true,
ScrollDelta::Lines(_) => false,
}
}
pub fn pixel_delta(&self, line_height: Pixels) -> Point<Pixels> {
match self {
ScrollDelta::Pixels(delta) => *delta,
ScrollDelta::Lines(delta) => point(line_height * delta.x, line_height * delta.y),
}
}
pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
match (self, other) {
(ScrollDelta::Pixels(a), ScrollDelta::Pixels(b)) => {
let x = if a.x.signum() == b.x.signum() {
a.x + b.x
} else {
b.x
};
let y = if a.y.signum() == b.y.signum() {
a.y + b.y
} else {
b.y
};
ScrollDelta::Pixels(point(x, y))
}
(ScrollDelta::Lines(a), ScrollDelta::Lines(b)) => {
let x = if a.x.signum() == b.x.signum() {
a.x + b.x
} else {
b.x
};
let y = if a.y.signum() == b.y.signum() {
a.y + b.y
} else {
b.y
};
ScrollDelta::Lines(point(x, y))
}
_ => other,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct PinchEvent {
pub position: Point<Pixels>,
pub delta: f32,
pub modifiers: Modifiers,
pub phase: TouchPhase,
}
impl Deref for PinchEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Clone, Debug, Default)]
pub struct MouseExitEvent {
pub position: Point<Pixels>,
pub pressed_button: Option<MouseButton>,
pub modifiers: Modifiers,
}
impl Deref for MouseExitEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Debug, Clone)]
pub enum FileDropEvent {
Entered {
position: Point<Pixels>,
paths: ExternalPaths,
},
Pending {
position: Point<Pixels>,
},
Submit {
position: Point<Pixels>,
},
Exited,
Ended,
}
#[derive(Clone, Debug)]
pub enum PlatformInput {
KeyDown(KeyDownEvent),
KeyUp(KeyUpEvent),
ModifiersChanged(ModifiersChangedEvent),
MouseDown(MouseDownEvent),
MouseUp(MouseUpEvent),
MousePressure(MousePressureEvent),
MouseMove(MouseMoveEvent),
MouseExited(MouseExitEvent),
ScrollWheel(ScrollWheelEvent),
Pinch(PinchEvent),
LongPress(LongPressEvent),
TouchDrag(TouchDragEvent),
FileDrop(FileDropEvent),
Touch(TouchEvent),
}
impl PlatformInput {
pub fn mouse_event(&self) -> Option<&dyn Any> {
match self {
PlatformInput::KeyDown { .. } => None,
PlatformInput::KeyUp { .. } => None,
PlatformInput::ModifiersChanged { .. } => None,
PlatformInput::MouseDown(event) => Some(event),
PlatformInput::MouseUp(event) => Some(event),
PlatformInput::MouseMove(event) => Some(event),
PlatformInput::MousePressure(event) => Some(event),
PlatformInput::MouseExited(event) => Some(event),
PlatformInput::ScrollWheel(event) => Some(event),
PlatformInput::Pinch(event) => Some(event),
PlatformInput::LongPress(event) => Some(event),
PlatformInput::TouchDrag(event) => Some(event),
PlatformInput::FileDrop(event) => Some(event),
PlatformInput::Touch(_) => None,
}
}
pub fn keyboard_event(&self) -> Option<&dyn Any> {
match self {
PlatformInput::KeyDown(event) => Some(event),
PlatformInput::KeyUp(event) => Some(event),
PlatformInput::ModifiersChanged(event) => Some(event),
PlatformInput::MouseDown(_) => None,
PlatformInput::MouseUp(_) => None,
PlatformInput::MouseMove(_) => None,
PlatformInput::MousePressure(_) => None,
PlatformInput::MouseExited(_) => None,
PlatformInput::ScrollWheel(_) => None,
PlatformInput::Pinch(_) => None,
PlatformInput::LongPress(_) => None,
PlatformInput::TouchDrag(_) => None,
PlatformInput::FileDrop(_) => None,
PlatformInput::Touch(_) => None,
}
}
pub fn kind_name(&self) -> &'static str {
match self {
PlatformInput::KeyDown(_) => "key_down",
PlatformInput::KeyUp(_) => "key_up",
PlatformInput::ModifiersChanged(_) => "modifiers_changed",
PlatformInput::MouseDown(_) => "mouse_down",
PlatformInput::MouseUp(_) => "mouse_up",
PlatformInput::MousePressure(_) => "mouse_pressure",
PlatformInput::MouseMove(_) => "mouse_move",
PlatformInput::MouseExited(_) => "mouse_exited",
PlatformInput::ScrollWheel(_) => "scroll_wheel",
PlatformInput::Pinch(_) => "pinch",
PlatformInput::LongPress(_) => "long_press",
PlatformInput::TouchDrag(_) => "touch_drag",
PlatformInput::FileDrop(_) => "file_drop",
PlatformInput::Touch(_) => "touch",
}
}
pub fn touch_event(&self) -> Option<&TouchEvent> {
match self {
PlatformInput::Touch(event) => Some(event),
_ => None,
}
}
}