2023-10-17 19:37:09 +00:00
|
|
|
use crate::{
|
2024-01-17 06:19:55 +00:00
|
|
|
point, seal::Sealed, IntoElement, Keystroke, Modifiers, Pixels, Point, Render, ViewContext,
|
2023-10-19 19:52:28 +00:00
|
|
|
};
|
2023-10-20 09:00:52 +00:00
|
|
|
use smallvec::SmallVec;
|
2024-01-17 06:19:55 +00:00
|
|
|
use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf};
|
|
|
|
|
|
|
|
pub trait InputEvent: Sealed + 'static {
|
|
|
|
fn to_platform_input(self) -> PlatformInput;
|
|
|
|
}
|
|
|
|
pub trait KeyEvent: InputEvent {}
|
|
|
|
pub trait MouseEvent: InputEvent {}
|
2023-10-23 14:20:01 +00:00
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct KeyDownEvent {
|
|
|
|
pub keystroke: Keystroke,
|
|
|
|
pub is_held: bool,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for KeyDownEvent {}
|
|
|
|
impl InputEvent for KeyDownEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::KeyDown(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl KeyEvent for KeyDownEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct KeyUpEvent {
|
|
|
|
pub keystroke: Keystroke,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for KeyUpEvent {}
|
|
|
|
impl InputEvent for KeyUpEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::KeyUp(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl KeyEvent for KeyUpEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct ModifiersChangedEvent {
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for ModifiersChangedEvent {}
|
|
|
|
impl InputEvent for ModifiersChangedEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::ModifiersChanged(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl KeyEvent for ModifiersChangedEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
impl Deref for ModifiersChangedEvent {
|
|
|
|
type Target = Modifiers;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.modifiers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The phase of a touch motion event.
|
|
|
|
/// Based on the winit enum of the same name.
|
2024-01-17 00:52:55 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
2023-10-19 19:52:28 +00:00
|
|
|
pub enum TouchPhase {
|
|
|
|
Started,
|
2024-01-17 00:52:55 +00:00
|
|
|
#[default]
|
2023-10-19 19:52:28 +00:00
|
|
|
Moved,
|
|
|
|
Ended,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MouseDownEvent {
|
|
|
|
pub button: MouseButton,
|
|
|
|
pub position: Point<Pixels>,
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
pub click_count: usize,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for MouseDownEvent {}
|
|
|
|
impl InputEvent for MouseDownEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::MouseDown(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for MouseDownEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MouseUpEvent {
|
|
|
|
pub button: MouseButton,
|
|
|
|
pub position: Point<Pixels>,
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
pub click_count: usize,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for MouseUpEvent {}
|
|
|
|
impl InputEvent for MouseUpEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::MouseUp(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for MouseUpEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2023-10-23 12:15:12 +00:00
|
|
|
pub struct ClickEvent {
|
2023-10-19 19:52:28 +00:00
|
|
|
pub down: MouseDownEvent,
|
|
|
|
pub up: MouseUpEvent,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
|
|
|
pub enum MouseButton {
|
|
|
|
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),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MouseButton {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Left
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
|
|
|
|
pub enum NavigationDirection {
|
|
|
|
Back,
|
|
|
|
Forward,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for NavigationDirection {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Back
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MouseMoveEvent {
|
|
|
|
pub position: Point<Pixels>,
|
|
|
|
pub pressed_button: Option<MouseButton>,
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for MouseMoveEvent {}
|
|
|
|
impl InputEvent for MouseMoveEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::MouseMove(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for MouseMoveEvent {}
|
|
|
|
|
2023-12-09 00:29:42 +00:00
|
|
|
impl MouseMoveEvent {
|
|
|
|
pub fn dragging(&self) -> bool {
|
|
|
|
self.pressed_button == Some(MouseButton::Left)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 00:52:55 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2023-10-19 19:52:28 +00:00
|
|
|
pub struct ScrollWheelEvent {
|
|
|
|
pub position: Point<Pixels>,
|
|
|
|
pub delta: ScrollDelta,
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
pub touch_phase: TouchPhase,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for ScrollWheelEvent {}
|
|
|
|
impl InputEvent for ScrollWheelEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::ScrollWheel(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for ScrollWheelEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
2024-01-09 19:32:43 +00:00
|
|
|
|
|
|
|
pub fn coalesce(self, other: ScrollDelta) -> ScrollDelta {
|
|
|
|
match (self, other) {
|
|
|
|
(ScrollDelta::Pixels(px_a), ScrollDelta::Pixels(px_b)) => {
|
|
|
|
ScrollDelta::Pixels(px_a + px_b)
|
|
|
|
}
|
|
|
|
|
|
|
|
(ScrollDelta::Lines(lines_a), ScrollDelta::Lines(lines_b)) => {
|
|
|
|
ScrollDelta::Lines(lines_a + lines_b)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => other,
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 19:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct MouseExitEvent {
|
|
|
|
pub position: Point<Pixels>,
|
|
|
|
pub pressed_button: Option<MouseButton>,
|
|
|
|
pub modifiers: Modifiers,
|
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for MouseExitEvent {}
|
|
|
|
impl InputEvent for MouseExitEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::MouseExited(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for MouseExitEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
impl Deref for MouseExitEvent {
|
|
|
|
type Target = Modifiers;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.modifiers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 09:31:07 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2023-10-25 18:49:40 +00:00
|
|
|
pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
|
2023-10-24 17:24:21 +00:00
|
|
|
|
2023-12-07 13:18:23 +00:00
|
|
|
impl ExternalPaths {
|
|
|
|
pub fn paths(&self) -> &[PathBuf] {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 22:19:40 +00:00
|
|
|
impl Render for ExternalPaths {
|
2024-01-02 17:09:05 +00:00
|
|
|
fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
|
2024-01-17 06:19:55 +00:00
|
|
|
() // Intentionally left empty because the platform will render icons for the dragged files
|
2023-10-30 22:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 17:24:21 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2023-10-24 09:31:07 +00:00
|
|
|
pub enum FileDropEvent {
|
2023-10-24 17:24:21 +00:00
|
|
|
Entered {
|
|
|
|
position: Point<Pixels>,
|
2024-01-06 23:17:49 +00:00
|
|
|
paths: ExternalPaths,
|
2023-10-24 17:24:21 +00:00
|
|
|
},
|
2023-10-24 09:31:07 +00:00
|
|
|
Pending {
|
|
|
|
position: Point<Pixels>,
|
|
|
|
},
|
|
|
|
Submit {
|
|
|
|
position: Point<Pixels>,
|
|
|
|
},
|
2023-10-24 17:24:21 +00:00
|
|
|
Exited,
|
2023-10-24 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl Sealed for FileDropEvent {}
|
|
|
|
impl InputEvent for FileDropEvent {
|
|
|
|
fn to_platform_input(self) -> PlatformInput {
|
|
|
|
PlatformInput::FileDrop(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl MouseEvent for FileDropEvent {}
|
|
|
|
|
2023-10-19 19:52:28 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2024-01-17 06:19:55 +00:00
|
|
|
pub enum PlatformInput {
|
2023-10-19 19:52:28 +00:00
|
|
|
KeyDown(KeyDownEvent),
|
|
|
|
KeyUp(KeyUpEvent),
|
|
|
|
ModifiersChanged(ModifiersChangedEvent),
|
|
|
|
MouseDown(MouseDownEvent),
|
|
|
|
MouseUp(MouseUpEvent),
|
2023-10-24 17:24:21 +00:00
|
|
|
MouseMove(MouseMoveEvent),
|
2023-10-19 19:52:28 +00:00
|
|
|
MouseExited(MouseExitEvent),
|
|
|
|
ScrollWheel(ScrollWheelEvent),
|
2023-10-24 09:31:07 +00:00
|
|
|
FileDrop(FileDropEvent),
|
2023-10-19 19:52:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 06:19:55 +00:00
|
|
|
impl PlatformInput {
|
2023-10-19 19:52:28 +00:00
|
|
|
pub fn position(&self) -> Option<Point<Pixels>> {
|
|
|
|
match self {
|
2024-01-17 06:19:55 +00:00
|
|
|
PlatformInput::KeyDown { .. } => None,
|
|
|
|
PlatformInput::KeyUp { .. } => None,
|
|
|
|
PlatformInput::ModifiersChanged { .. } => None,
|
|
|
|
PlatformInput::MouseDown(event) => Some(event.position),
|
|
|
|
PlatformInput::MouseUp(event) => Some(event.position),
|
|
|
|
PlatformInput::MouseMove(event) => Some(event.position),
|
|
|
|
PlatformInput::MouseExited(event) => Some(event.position),
|
|
|
|
PlatformInput::ScrollWheel(event) => Some(event.position),
|
|
|
|
PlatformInput::FileDrop(FileDropEvent::Exited) => None,
|
|
|
|
PlatformInput::FileDrop(
|
2023-10-24 17:24:21 +00:00
|
|
|
FileDropEvent::Entered { position, .. }
|
|
|
|
| FileDropEvent::Pending { position, .. }
|
|
|
|
| FileDropEvent::Submit { position, .. },
|
2023-10-24 09:31:07 +00:00
|
|
|
) => Some(*position),
|
2023-10-19 19:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 23:09:24 +00:00
|
|
|
pub fn mouse_event(&self) -> Option<&dyn Any> {
|
2023-10-19 19:52:28 +00:00
|
|
|
match self {
|
2024-01-17 06:19:55 +00:00
|
|
|
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::FileDrop(event) => Some(event),
|
2023-10-19 19:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 23:09:24 +00:00
|
|
|
pub fn keyboard_event(&self) -> Option<&dyn Any> {
|
2023-10-19 19:52:28 +00:00
|
|
|
match self {
|
2024-01-17 06:19:55 +00:00
|
|
|
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::FileDrop(_) => None,
|
2023-10-19 19:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 04:05:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::{
|
2023-12-30 15:58:07 +00:00
|
|
|
self as gpui, div, Element, FocusHandle, InteractiveElement, IntoElement, KeyBinding,
|
|
|
|
Keystroke, ParentElement, Render, TestAppContext, VisualContext,
|
2023-11-07 04:05:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TestView {
|
|
|
|
saw_key_down: bool,
|
|
|
|
saw_action: bool,
|
|
|
|
focus_handle: FocusHandle,
|
|
|
|
}
|
|
|
|
|
2023-12-09 20:25:15 +00:00
|
|
|
actions!(test, [TestAction]);
|
2023-11-07 04:05:22 +00:00
|
|
|
|
|
|
|
impl Render for TestView {
|
2023-12-30 15:58:07 +00:00
|
|
|
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl Element {
|
2023-11-07 04:05:22 +00:00
|
|
|
div().id("testview").child(
|
|
|
|
div()
|
2023-11-14 18:00:52 +00:00
|
|
|
.key_context("parent")
|
2024-01-04 20:07:46 +00:00
|
|
|
.on_key_down(cx.listener(|this, _, cx| {
|
|
|
|
cx.stop_propagation();
|
|
|
|
this.saw_key_down = true
|
|
|
|
}))
|
2023-11-20 02:32:31 +00:00
|
|
|
.on_action(
|
2023-11-20 19:55:27 +00:00
|
|
|
cx.listener(|this: &mut TestView, _: &TestAction, _| {
|
2023-11-20 02:32:31 +00:00
|
|
|
this.saw_action = true
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.child(
|
2023-11-14 18:00:52 +00:00
|
|
|
div()
|
|
|
|
.key_context("nested")
|
2023-11-20 02:32:31 +00:00
|
|
|
.track_focus(&self.focus_handle)
|
2023-11-22 18:19:43 +00:00
|
|
|
.into_element(),
|
2023-11-20 02:32:31 +00:00
|
|
|
),
|
2023-11-07 04:05:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
fn test_on_events(cx: &mut TestAppContext) {
|
|
|
|
let window = cx.update(|cx| {
|
|
|
|
cx.open_window(Default::default(), |cx| {
|
2024-01-01 19:01:29 +00:00
|
|
|
cx.new_view(|cx| TestView {
|
2023-11-07 04:05:22 +00:00
|
|
|
saw_key_down: false,
|
|
|
|
saw_action: false,
|
|
|
|
focus_handle: cx.focus_handle(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.update(|cx| {
|
2023-11-14 18:00:52 +00:00
|
|
|
cx.bind_keys(vec![KeyBinding::new("ctrl-g", TestAction, Some("parent"))]);
|
2023-11-07 04:05:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window
|
|
|
|
.update(cx, |test_view, cx| cx.focus(&test_view.focus_handle))
|
|
|
|
.unwrap();
|
|
|
|
|
2024-01-04 20:07:46 +00:00
|
|
|
cx.dispatch_keystroke(*window, Keystroke::parse("a").unwrap(), false);
|
2023-11-07 04:05:22 +00:00
|
|
|
cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap(), false);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|