zed/crates/gpui/src/interactive.rs

506 lines
14 KiB
Rust
Raw Normal View History

2023-10-17 19:37:09 +00:00
use crate::{
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;
use std::{any::Any, fmt::Debug, ops::Deref, path::PathBuf};
2024-01-21 22:26:45 +00:00
/// An event from a platform input source.
pub trait InputEvent: Sealed + 'static {
2024-01-21 22:26:45 +00:00
/// Convert this event into the platform input enum.
fn to_platform_input(self) -> PlatformInput;
}
2024-01-21 22:26:45 +00:00
/// A key event from the platform.
pub trait KeyEvent: InputEvent {}
2024-01-21 22:26:45 +00:00
/// A mouse event from the platform.
pub trait MouseEvent: InputEvent {}
2024-01-21 22:26:45 +00:00
/// The key down event equivalent for the platform.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent {
2024-01-21 22:26:45 +00:00
/// The keystroke that was generated.
2023-10-19 19:52:28 +00:00
pub keystroke: Keystroke,
2024-01-21 22:26:45 +00:00
/// Whether the key is currently held down.
2023-10-19 19:52:28 +00:00
pub is_held: bool,
}
impl Sealed for KeyDownEvent {}
impl InputEvent for KeyDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyDown(self)
}
}
impl KeyEvent for KeyDownEvent {}
2024-01-21 22:26:45 +00:00
/// The key up event equivalent for the platform.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug)]
pub struct KeyUpEvent {
2024-01-21 22:26:45 +00:00
/// The keystroke that was released.
2023-10-19 19:52:28 +00:00
pub keystroke: Keystroke,
}
impl Sealed for KeyUpEvent {}
impl InputEvent for KeyUpEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyUp(self)
}
}
impl KeyEvent for KeyUpEvent {}
2024-01-21 22:26:45 +00:00
/// The modifiers changed event equivalent for the platform.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
pub struct ModifiersChangedEvent {
2024-01-21 22:26:45 +00:00
/// The new state of the modifier keys
2023-10-19 19:52:28 +00:00
pub modifiers: Modifiers,
}
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.
#[derive(Clone, Copy, Debug, Default)]
2023-10-19 19:52:28 +00:00
pub enum TouchPhase {
2024-01-21 22:26:45 +00:00
/// The touch started.
2023-10-19 19:52:28 +00:00
Started,
2024-01-21 22:26:45 +00:00
/// The touch event is moving.
#[default]
2023-10-19 19:52:28 +00:00
Moved,
2024-01-21 22:26:45 +00:00
/// The touch phase has ended
2023-10-19 19:52:28 +00:00
Ended,
}
2024-01-21 22:26:45 +00:00
/// A mouse down event from the platform
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
pub struct MouseDownEvent {
2024-01-21 22:26:45 +00:00
/// Which mouse button was pressed.
2023-10-19 19:52:28 +00:00
pub button: MouseButton,
2024-01-21 22:26:45 +00:00
/// The position of the mouse on the window.
2023-10-19 19:52:28 +00:00
pub position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The modifiers that were held down when the mouse was pressed.
2023-10-19 19:52:28 +00:00
pub modifiers: Modifiers,
2024-01-21 22:26:45 +00:00
/// The number of times the button has been clicked.
2023-10-19 19:52:28 +00:00
pub click_count: usize,
}
impl Sealed for MouseDownEvent {}
impl InputEvent for MouseDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseDown(self)
}
}
impl MouseEvent for MouseDownEvent {}
2024-01-21 22:26:45 +00:00
/// A mouse up event from the platform
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
pub struct MouseUpEvent {
2024-01-21 22:26:45 +00:00
/// Which mouse button was released.
2023-10-19 19:52:28 +00:00
pub button: MouseButton,
2024-01-21 22:26:45 +00:00
/// The position of the mouse on the window.
2023-10-19 19:52:28 +00:00
pub position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The modifiers that were held down when the mouse was released.
2023-10-19 19:52:28 +00:00
pub modifiers: Modifiers,
2024-01-21 22:26:45 +00:00
/// The number of times the button has been clicked.
2023-10-19 19:52:28 +00:00
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 {}
2024-01-21 22:26:45 +00:00
/// A click event, generated when a mouse button is pressed and released.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
2023-10-23 12:15:12 +00:00
pub struct ClickEvent {
2024-01-21 22:26:45 +00:00
/// The mouse event when the button was pressed.
2023-10-19 19:52:28 +00:00
pub down: MouseDownEvent,
2024-01-21 22:26:45 +00:00
/// The mouse event when the button was released.
2023-10-19 19:52:28 +00:00
pub up: MouseUpEvent,
}
2024-01-21 22:26:45 +00:00
/// An enum representing the mouse button that was pressed.
2023-10-19 19:52:28 +00:00
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum MouseButton {
2024-01-21 22:26:45 +00:00
/// The left mouse button.
2023-10-19 19:52:28 +00:00
Left,
2024-01-21 22:26:45 +00:00
/// The right mouse button.
2023-10-19 19:52:28 +00:00
Right,
2024-01-21 22:26:45 +00:00
/// The middle mouse button.
2023-10-19 19:52:28 +00:00
Middle,
2024-01-21 22:26:45 +00:00
/// A navigation button, such as back or forward.
2023-10-19 19:52:28 +00:00
Navigate(NavigationDirection),
}
impl MouseButton {
2024-01-21 22:26:45 +00:00
/// Get all the mouse buttons in a list.
2023-10-19 19:52:28 +00:00
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
}
}
2024-01-21 22:26:45 +00:00
/// A navigation direction, such as back or forward.
2023-10-19 19:52:28 +00:00
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
pub enum NavigationDirection {
2024-01-21 22:26:45 +00:00
/// The back button.
2023-10-19 19:52:28 +00:00
Back,
2024-01-21 22:26:45 +00:00
/// The forward button.
2023-10-19 19:52:28 +00:00
Forward,
}
impl Default for NavigationDirection {
fn default() -> Self {
Self::Back
}
}
2024-01-21 22:26:45 +00:00
/// A mouse move event from the platform
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
pub struct MouseMoveEvent {
2024-01-21 22:26:45 +00:00
/// The position of the mouse on the window.
2023-10-19 19:52:28 +00:00
pub position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The mouse button that was pressed, if any.
2023-10-19 19:52:28 +00:00
pub pressed_button: Option<MouseButton>,
2024-01-21 22:26:45 +00:00
/// The modifiers that were held down when the mouse was moved.
2023-10-19 19:52:28 +00:00
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 {
2024-01-21 22:26:45 +00:00
/// Returns true if the left mouse button is currently held down.
pub fn dragging(&self) -> bool {
self.pressed_button == Some(MouseButton::Left)
}
}
2024-01-21 22:26:45 +00:00
/// A mouse wheel event from the platform
#[derive(Clone, Debug, Default)]
2023-10-19 19:52:28 +00:00
pub struct ScrollWheelEvent {
2024-01-21 22:26:45 +00:00
/// The position of the mouse on the window.
2023-10-19 19:52:28 +00:00
pub position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The change in scroll wheel position for this event.
2023-10-19 19:52:28 +00:00
pub delta: ScrollDelta,
2024-01-21 22:26:45 +00:00
/// The modifiers that were held down when the mouse was moved.
2023-10-19 19:52:28 +00:00
pub modifiers: Modifiers,
2024-01-21 22:26:45 +00:00
/// The phase of the touch event.
2023-10-19 19:52:28 +00:00
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 {}
2023-10-19 19:52:28 +00:00
impl Deref for ScrollWheelEvent {
type Target = Modifiers;
fn deref(&self) -> &Self::Target {
&self.modifiers
}
}
2024-01-21 22:26:45 +00:00
/// The scroll delta for a scroll wheel event.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Copy, Debug)]
pub enum ScrollDelta {
2024-01-21 22:26:45 +00:00
/// An exact scroll delta in pixels.
2023-10-19 19:52:28 +00:00
Pixels(Point<Pixels>),
2024-01-21 22:26:45 +00:00
/// An inexact scroll delta in lines.
2023-10-19 19:52:28 +00:00
Lines(Point<f32>),
}
impl Default for ScrollDelta {
fn default() -> Self {
Self::Lines(Default::default())
}
}
impl ScrollDelta {
2024-01-21 22:26:45 +00:00
/// Returns true if this is a precise scroll delta in pixels.
2023-10-19 19:52:28 +00:00
pub fn precise(&self) -> bool {
match self {
ScrollDelta::Pixels(_) => true,
ScrollDelta::Lines(_) => false,
}
}
2024-01-21 22:26:45 +00:00
/// Converts this scroll event into exact pixels.
2023-10-19 19:52:28 +00:00
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-21 22:26:45 +00:00
/// Combines two scroll deltas into one.
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
}
2024-01-21 22:26:45 +00:00
/// A mouse exit event from the platform, generated when the mouse leaves the window.
/// The position generated should be just outside of the window's bounds.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug, Default)]
pub struct MouseExitEvent {
2024-01-21 22:26:45 +00:00
/// The position of the mouse relative to the window.
2023-10-19 19:52:28 +00:00
pub position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The mouse button that was pressed, if any.
2023-10-19 19:52:28 +00:00
pub pressed_button: Option<MouseButton>,
2024-01-21 22:26:45 +00:00
/// The modifiers that were held down when the mouse was moved.
2023-10-19 19:52:28 +00:00
pub modifiers: Modifiers,
}
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
}
}
2024-01-21 22:26:45 +00:00
/// A collection of paths from the platform, such as from a file drop.
#[derive(Debug, Clone, Default)]
pub struct ExternalPaths(pub(crate) SmallVec<[PathBuf; 2]>);
impl ExternalPaths {
2024-01-21 22:26:45 +00:00
/// Convert this collection of paths into a slice.
pub fn paths(&self) -> &[PathBuf] {
&self.0
}
}
impl Render for ExternalPaths {
fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
// Intentionally left empty because the platform will render icons for the dragged files
}
}
2024-01-21 22:26:45 +00:00
/// A file drop event from the platform, generated when files are dragged and dropped onto the window.
#[derive(Debug, Clone)]
pub enum FileDropEvent {
2024-01-21 22:26:45 +00:00
/// The files have entered the window.
Entered {
2024-01-21 22:26:45 +00:00
/// The position of the mouse relative to the window.
position: Point<Pixels>,
2024-01-21 22:26:45 +00:00
/// The paths of the files that are being dragged.
2024-01-06 23:17:49 +00:00
paths: ExternalPaths,
},
2024-01-21 22:26:45 +00:00
/// The files are being dragged over the window
Pending {
2024-01-21 22:26:45 +00:00
/// The position of the mouse relative to the window.
position: Point<Pixels>,
},
2024-01-21 22:26:45 +00:00
/// The files have been dropped onto the window.
Submit {
2024-01-21 22:26:45 +00:00
/// The position of the mouse relative to the window.
position: Point<Pixels>,
},
2024-01-21 22:26:45 +00:00
/// The user has stopped dragging the files over the window.
Exited,
}
impl Sealed for FileDropEvent {}
impl InputEvent for FileDropEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::FileDrop(self)
}
}
impl MouseEvent for FileDropEvent {}
2024-01-21 22:26:45 +00:00
/// An enum corresponding to all kinds of platform input events.
2023-10-19 19:52:28 +00:00
#[derive(Clone, Debug)]
pub enum PlatformInput {
2024-01-21 22:26:45 +00:00
/// A key was pressed.
2023-10-19 19:52:28 +00:00
KeyDown(KeyDownEvent),
2024-01-21 22:26:45 +00:00
/// A key was released.
2023-10-19 19:52:28 +00:00
KeyUp(KeyUpEvent),
2024-01-21 22:26:45 +00:00
/// The keyboard modifiers were changed.
2023-10-19 19:52:28 +00:00
ModifiersChanged(ModifiersChangedEvent),
2024-01-21 22:26:45 +00:00
/// The mouse was pressed.
2023-10-19 19:52:28 +00:00
MouseDown(MouseDownEvent),
2024-01-21 22:26:45 +00:00
/// The mouse was released.
2023-10-19 19:52:28 +00:00
MouseUp(MouseUpEvent),
2024-01-21 22:26:45 +00:00
/// The mouse was moved.
MouseMove(MouseMoveEvent),
2024-01-21 22:26:45 +00:00
/// The mouse exited the window.
2023-10-19 19:52:28 +00:00
MouseExited(MouseExitEvent),
2024-01-21 22:26:45 +00:00
/// The scroll wheel was used.
2023-10-19 19:52:28 +00:00
ScrollWheel(ScrollWheelEvent),
2024-01-21 22:26:45 +00:00
/// Files were dragged and dropped onto the window.
FileDrop(FileDropEvent),
2023-10-19 19:52:28 +00:00
}
impl PlatformInput {
2024-01-21 22:26:45 +00:00
pub(crate) fn mouse_event(&self) -> Option<&dyn Any> {
2023-10-19 19:52:28 +00:00
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::FileDrop(event) => Some(event),
2023-10-19 19:52:28 +00:00
}
}
2024-01-21 22:26:45 +00:00
pub(crate) fn keyboard_event(&self) -> Option<&dyn Any> {
2023-10-19 19:52:28 +00:00
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::FileDrop(_) => None,
2023-10-19 19:52:28 +00:00
}
}
}
2023-11-07 04:05:22 +00:00
#[cfg(test)]
mod test {
use crate::{
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,
}
actions!(test, [TestAction]);
2023-11-07 04:05:22 +00:00
impl Render for TestView {
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")
.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| {
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();
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();
}
}