use crate::{
Axis, Bounds, Capslock, Context, Empty, IntoElement, IsZero, Keystroke, Modifiers, Pixels,
Point, Render, Window, point, px, seal::Sealed,
};
use smallvec::SmallVec;
use std::{
any::Any,
fmt::Debug,
ops::Deref,
path::PathBuf,
time::{Duration, Instant},
};
pub trait InputEvent: Sealed + 'static {
fn to_platform_input(self) -> PlatformInput;
}
pub trait KeyEvent: InputEvent {}
pub trait MouseEvent: InputEvent {}
pub trait GestureEvent: InputEvent {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent {
pub keystroke: Keystroke,
pub is_held: bool,
pub prefer_character_input: bool,
}
impl Sealed for KeyDownEvent {}
impl InputEvent for KeyDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyDown(self)
}
}
impl KeyEvent for KeyDownEvent {}
#[derive(Clone, Debug)]
pub struct KeyUpEvent {
pub keystroke: Keystroke,
}
impl Sealed for KeyUpEvent {}
impl InputEvent for KeyUpEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyUp(self)
}
}
impl KeyEvent for KeyUpEvent {}
#[derive(Clone, Debug, Default)]
pub struct ModifiersChangedEvent {
pub modifiers: Modifiers,
pub capslock: Capslock,
}
impl Sealed for ModifiersChangedEvent {}
impl InputEvent for ModifiersChangedEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::ModifiersChanged(self)
}
}
impl KeyEvent for ModifiersChangedEvent {}
impl Deref for ModifiersChangedEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Clone, Copy, Debug, Default)]
pub enum TouchPhase {
Started,
#[default]
Moved,
Ended,
}
#[derive(Clone, Debug, Default)]
pub struct MouseDownEvent {
pub button: MouseButton,
pub position: Point<Pixels>,
pub modifiers: Modifiers,
pub click_count: usize,
pub first_mouse: bool,
}
impl Sealed for MouseDownEvent {}
impl InputEvent for MouseDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseDown(self)
}
}
impl MouseEvent for MouseDownEvent {}
#[derive(Clone, Debug, Default)]
pub struct MouseUpEvent {
pub button: MouseButton,
pub position: Point<Pixels>,
pub modifiers: Modifiers,
pub click_count: usize,
}
impl Sealed for MouseUpEvent {}
impl InputEvent for MouseUpEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseUp(self)
}
}
impl MouseEvent for MouseUpEvent {}
#[derive(Clone, Debug, Default)]
pub struct MouseClickEvent {
pub down: MouseDownEvent,
pub up: MouseUpEvent,
}
#[derive(Clone, Debug, Default)]
pub struct KeyboardClickEvent {
pub button: KeyboardButton,
pub bounds: Bounds<Pixels>,
}
#[derive(Clone, Debug)]
pub enum ClickEvent {
Mouse(MouseClickEvent),
Keyboard(KeyboardClickEvent),
}
impl Default for ClickEvent {
fn default() -> Self {
ClickEvent::Keyboard(KeyboardClickEvent::default())
}
}
impl ClickEvent {
pub fn modifiers(&self) -> Modifiers {
match self {
ClickEvent::Keyboard(_) => Modifiers::default(),
ClickEvent::Mouse(event) => event.up.modifiers,
}
}
pub fn position(&self) -> Point<Pixels> {
match self {
ClickEvent::Keyboard(event) => event.bounds.bottom_left(),
ClickEvent::Mouse(event) => event.up.position,
}
}
pub fn mouse_position(&self) -> Option<Point<Pixels>> {
match self {
ClickEvent::Keyboard(_) => None,
ClickEvent::Mouse(event) => Some(event.up.position),
}
}
pub fn is_right_click(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => false,
ClickEvent::Mouse(event) => {
event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
}
}
}
pub fn standard_click(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => true,
ClickEvent::Mouse(event) => {
event.down.button == MouseButton::Left && event.up.button == MouseButton::Left
}
}
}
pub fn first_focus(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => false,
ClickEvent::Mouse(event) => event.down.first_mouse,
}
}
pub fn click_count(&self) -> usize {
match self {
ClickEvent::Keyboard(_) => 1,
ClickEvent::Mouse(event) => event.up.click_count,
}
}
pub fn is_keyboard(&self) -> bool {
match self {
ClickEvent::Mouse(_) => false,
ClickEvent::Keyboard(_) => true,
}
}
}
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub enum KeyboardButton {
#[default]
Enter,
Space,
}
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub enum MouseButton {
#[default]
Left,
Right,
Middle,
Navigate(NavigationDirection),
}
impl MouseButton {
pub fn all() -> Vec<Self> {
vec![
MouseButton::Left,
MouseButton::Right,
MouseButton::Middle,
MouseButton::Navigate(NavigationDirection::Back),
MouseButton::Navigate(NavigationDirection::Forward),
]
}
}
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub enum NavigationDirection {
#[default]
Back,
Forward,
}
#[derive(Clone, Debug, Default)]
pub struct MouseMoveEvent {
pub position: Point<Pixels>,
pub pressed_button: Option<MouseButton>,
pub modifiers: Modifiers,
}
impl Sealed for MouseMoveEvent {}
impl InputEvent for MouseMoveEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseMove(self)
}
}
impl MouseEvent for MouseMoveEvent {}
impl MouseMoveEvent {
pub fn dragging(&self) -> bool {
self.pressed_button == Some(MouseButton::Left)
}
}
#[derive(Clone, Debug, Default)]
pub struct ScrollWheelEvent {
pub position: Point<Pixels>,
pub delta: ScrollDelta,
pub modifiers: Modifiers,
pub touch_phase: TouchPhase,
}
impl Sealed for ScrollWheelEvent {}
impl InputEvent for ScrollWheelEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::ScrollWheel(self)
}
}
impl MouseEvent for ScrollWheelEvent {}
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())
}
}
#[derive(Clone, Debug, Default)]
pub struct PinchEvent {
pub position: Point<Pixels>,
pub delta: f32,
pub modifiers: Modifiers,
pub phase: TouchPhase,
}
impl Sealed for PinchEvent {}
impl InputEvent for PinchEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::Pinch(self)
}
}
impl GestureEvent for PinchEvent {}
impl MouseEvent for PinchEvent {}
impl Deref for PinchEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
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,
}
}
}
const SCROLL_EVENT_SEPARATION: Duration = Duration::from_millis(28);
#[derive(Clone, Copy, Debug, Default)]
pub struct OngoingScroll {
last_event: Option<Instant>,
axis: Option<Axis>,
}
impl OngoingScroll {
pub fn filter(&mut self, delta: &mut Point<Pixels>, touch_phase: TouchPhase) {
self.filter_at(delta, touch_phase, Instant::now())
}
fn filter_at(&mut self, delta: &mut Point<Pixels>, touch_phase: TouchPhase, now: Instant) {
const UNLOCK_PERCENT: f32 = 1.9;
const UNLOCK_LOWER_BOUND: Pixels = px(6.);
if matches!(touch_phase, TouchPhase::Ended) {
self.last_event = None;
self.axis = None;
return;
}
let x = delta.x.abs();
let y = delta.y.abs();
if x.is_zero() && y.is_zero() {
if matches!(touch_phase, TouchPhase::Started) {
self.last_event = None;
self.axis = None;
}
return;
}
let starts_new_gesture = matches!(touch_phase, TouchPhase::Started)
|| self
.last_event
.is_none_or(|last_event| now.duration_since(last_event) >= SCROLL_EVENT_SEPARATION);
let mut axis = self.axis;
if starts_new_gesture {
axis = if x <= y {
Some(Axis::Vertical)
} else {
Some(Axis::Horizontal)
};
} else if x.max(y) >= UNLOCK_LOWER_BOUND {
match axis {
Some(Axis::Vertical) if x > y && x >= y * UNLOCK_PERCENT => {
axis = None;
}
Some(Axis::Horizontal) if y > x && y >= x * UNLOCK_PERCENT => {
axis = None;
}
_ => {}
}
}
self.last_event = Some(now);
self.axis = axis;
match axis {
Some(Axis::Vertical) => delta.x = Pixels::ZERO,
Some(Axis::Horizontal) => delta.y = Pixels::ZERO,
None => {}
}
}
}
#[derive(Clone, Debug, Default)]
pub struct MouseExitEvent {
pub position: Point<Pixels>,
pub pressed_button: Option<MouseButton>,
pub modifiers: Modifiers,
}
impl Sealed for MouseExitEvent {}
impl InputEvent for MouseExitEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseExited(self)
}
}
impl MouseEvent for MouseExitEvent {}
impl Deref for MouseExitEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
#[derive(Debug, Clone, Default)]
#[allow(missing_docs)]
pub struct ExternalPaths(pub SmallVec<[PathBuf; 2]>);
impl ExternalPaths {
pub fn paths(&self) -> &[PathBuf] {
&self.0
}
}
impl Render for ExternalPaths {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
#[derive(Debug, Clone)]
pub enum FileDropEvent {
Entered {
position: Point<Pixels>,
paths: ExternalPaths,
},
Pending {
position: Point<Pixels>,
},
Submit {
position: Point<Pixels>,
},
Exited,
}
impl Sealed for FileDropEvent {}
impl InputEvent for FileDropEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::FileDrop(self)
}
}
impl MouseEvent for FileDropEvent {}
#[derive(Clone, Debug)]
pub enum PlatformInput {
KeyDown(KeyDownEvent),
KeyUp(KeyUpEvent),
ModifiersChanged(ModifiersChangedEvent),
MouseDown(MouseDownEvent),
MouseUp(MouseUpEvent),
MouseMove(MouseMoveEvent),
MouseExited(MouseExitEvent),
ScrollWheel(ScrollWheelEvent),
Pinch(PinchEvent),
FileDrop(FileDropEvent),
}
impl PlatformInput {
pub(crate) 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::MouseExited(event) => Some(event),
PlatformInput::ScrollWheel(event) => Some(event),
PlatformInput::Pinch(event) => Some(event),
PlatformInput::FileDrop(event) => Some(event),
}
}
pub(crate) 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::MouseExited(_) => None,
PlatformInput::ScrollWheel(_) => None,
PlatformInput::Pinch(_) => None,
PlatformInput::FileDrop(_) => None,
}
}
}
#[cfg(test)]
mod test {
use crate::{
self as gpui, AppContext as _, Context, FocusHandle, InteractiveElement, IntoElement,
KeyBinding, Keystroke, Modifiers, ParentElement, Render, TestAppContext, Window, div,
};
struct TestView {
saw_key_down: bool,
saw_action: bool,
focus_handle: FocusHandle,
}
actions!(test_only, [TestAction]);
impl Render for TestView {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().id("testview").child(
div()
.key_context("parent")
.on_key_down(cx.listener(|this, _, _, cx| {
cx.stop_propagation();
this.saw_key_down = true
}))
.on_action(cx.listener(|this: &mut TestView, _: &TestAction, _, _| {
this.saw_action = true
}))
.child(
div()
.key_context("nested")
.track_focus(&self.focus_handle)
.into_element(),
),
)
}
}
#[gpui::test]
fn test_on_events(cx: &mut TestAppContext) {
let window = cx.update(|cx| {
cx.open_window(Default::default(), |_, cx| {
cx.new(|cx| TestView {
saw_key_down: false,
saw_action: false,
focus_handle: cx.focus_handle(),
})
})
.unwrap()
});
cx.update(|cx| {
cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
});
window
.update(cx, |test_view, window, _cx| {
window.focus(&test_view.focus_handle)
})
.unwrap();
cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap());
cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap());
window
.update(cx, |test_view, _, _| {
assert!(test_view.saw_key_down || test_view.saw_action);
assert!(test_view.saw_key_down);
assert!(test_view.saw_action);
})
.unwrap();
}
#[gpui::test]
fn test_multi_modifier_gesture_does_not_dispatch_standalone_modifier_binding(
cx: &mut TestAppContext,
) {
let (test_view, cx) = cx.add_window_view(|_, cx| TestView {
saw_key_down: false,
saw_action: false,
focus_handle: cx.focus_handle(),
});
cx.update(|_, cx| {
cx.bind_keys(vec![KeyBinding::new("shift", TestAction, None)]);
});
test_view.update_in(cx, |test_view, window, _cx| {
window.focus(&test_view.focus_handle);
});
cx.simulate_modifiers_change(Modifiers::alt());
cx.simulate_modifiers_change(Modifiers::alt() | Modifiers::shift());
cx.simulate_modifiers_change(Modifiers::shift());
cx.simulate_modifiers_change(Modifiers::none());
assert!(!test_view.read_with(cx, |test_view, _| test_view.saw_action));
cx.simulate_modifiers_change(Modifiers::shift());
cx.simulate_modifiers_change(Modifiers::none());
assert!(test_view.read_with(cx, |test_view, _| test_view.saw_action));
}
}
#[cfg(test)]
mod ongoing_scroll_tests {
use super::*;
#[test]
fn ongoing_scroll_locks_to_dominant_axis() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Started, now);
assert_eq!(ongoing_scroll.axis, Some(Axis::Horizontal));
assert_eq!(horizontal_delta, point(px(10.), px(0.)));
let mut continued_delta = point(px(3.), px(2.));
ongoing_scroll.filter_at(
&mut continued_delta,
TouchPhase::Moved,
now + Duration::from_millis(1),
);
assert_eq!(ongoing_scroll.axis, Some(Axis::Horizontal));
assert_eq!(continued_delta, point(px(3.), px(0.)));
}
#[test]
fn ongoing_scroll_unlocks_when_direction_changes() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Started, now);
let mut vertical_delta = point(px(2.), px(10.));
ongoing_scroll.filter_at(
&mut vertical_delta,
TouchPhase::Moved,
now + Duration::from_millis(1),
);
assert_eq!(ongoing_scroll.axis, None);
assert_eq!(vertical_delta, point(px(2.), px(10.)));
}
#[test]
fn ongoing_scroll_starts_new_gesture_at_timeout_boundary() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Moved, now);
let mut vertical_delta = point(px(2.), px(10.));
ongoing_scroll.filter_at(
&mut vertical_delta,
TouchPhase::Moved,
now + SCROLL_EVENT_SEPARATION,
);
assert_eq!(ongoing_scroll.axis, Some(Axis::Vertical));
assert_eq!(vertical_delta, point(px(0.), px(10.)));
}
#[test]
fn ongoing_scroll_ignores_zero_delta_and_resets_when_ended() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Started, now);
let mut zero_delta = Point::default();
ongoing_scroll.filter_at(
&mut zero_delta,
TouchPhase::Ended,
now + Duration::from_millis(1),
);
assert_eq!(ongoing_scroll.axis, None);
let mut vertical_delta = point(px(2.), px(3.));
ongoing_scroll.filter_at(
&mut vertical_delta,
TouchPhase::Moved,
now + Duration::from_millis(2),
);
assert_eq!(ongoing_scroll.axis, Some(Axis::Vertical));
assert_eq!(vertical_delta, point(px(0.), px(3.)));
}
#[test]
fn ongoing_scroll_ignores_zero_delta_movement() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Started, now);
let mut zero_delta = Point::default();
ongoing_scroll.filter_at(
&mut zero_delta,
TouchPhase::Moved,
now + SCROLL_EVENT_SEPARATION,
);
let mut vertical_delta = point(px(2.), px(10.));
ongoing_scroll.filter_at(
&mut vertical_delta,
TouchPhase::Moved,
now + SCROLL_EVENT_SEPARATION,
);
assert_eq!(ongoing_scroll.axis, Some(Axis::Vertical));
assert_eq!(vertical_delta, point(px(0.), px(10.)));
}
#[test]
fn ongoing_scroll_supports_moved_only_platforms() {
let now = Instant::now();
let mut ongoing_scroll = OngoingScroll::default();
let mut horizontal_delta = point(px(10.), px(2.));
ongoing_scroll.filter_at(&mut horizontal_delta, TouchPhase::Moved, now);
assert_eq!(ongoing_scroll.axis, Some(Axis::Horizontal));
assert_eq!(horizontal_delta, point(px(10.), px(0.)));
}
}