use crate::{
Bounds, Context, Empty, ExternalPaths, FileDropEvent, IntoElement, KeyDownEvent, KeyUpEvent,
Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseExitEvent, MouseMoveEvent,
MousePressureEvent, MouseUpEvent, PinchEvent, Pixels, PlatformInput, Point, Render,
ScrollWheelEvent, TouchEvent, Window, seal::Sealed,
};
use std::fmt::Debug;
pub trait InputEvent: Sealed + 'static {
fn to_platform_input(self) -> PlatformInput;
}
pub trait KeyEvent: InputEvent {}
pub trait MouseEvent: InputEvent {}
pub trait GestureEvent: InputEvent {}
impl Sealed for KeyDownEvent {}
impl InputEvent for KeyDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyDown(self)
}
}
impl KeyEvent for KeyDownEvent {}
impl Sealed for KeyUpEvent {}
impl InputEvent for KeyUpEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::KeyUp(self)
}
}
impl KeyEvent for KeyUpEvent {}
impl Sealed for ModifiersChangedEvent {}
impl InputEvent for ModifiersChangedEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::ModifiersChanged(self)
}
}
impl KeyEvent for ModifiersChangedEvent {}
impl Sealed for TouchEvent {}
impl InputEvent for TouchEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::Touch(self)
}
}
impl Sealed for MouseDownEvent {}
impl InputEvent for MouseDownEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseDown(self)
}
}
impl MouseEvent for MouseDownEvent {}
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,
}
impl Sealed for MousePressureEvent {}
impl InputEvent for MousePressureEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MousePressure(self)
}
}
impl MouseEvent for MousePressureEvent {}
#[derive(Clone, Debug, Default)]
pub struct KeyboardClickEvent {
pub button: KeyboardButton,
pub bounds: Bounds<Pixels>,
}
#[derive(Clone, Debug, Default)]
pub struct TouchClickEvent {
pub position: Point<Pixels>,
pub tap_count: usize,
pub long_press: bool,
}
#[derive(Clone, Debug)]
pub enum ClickEvent {
Mouse(MouseClickEvent),
Keyboard(KeyboardClickEvent),
Touch(TouchClickEvent),
}
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,
ClickEvent::Touch(_) => Modifiers::default(),
}
}
pub fn position(&self) -> Point<Pixels> {
match self {
ClickEvent::Keyboard(event) => event.bounds.bottom_left(),
ClickEvent::Mouse(event) => event.up.position,
ClickEvent::Touch(event) => event.position,
}
}
pub fn mouse_position(&self) -> Option<Point<Pixels>> {
match self {
ClickEvent::Keyboard(_) => None,
ClickEvent::Mouse(event) => Some(event.up.position),
ClickEvent::Touch(_) => None,
}
}
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
}
ClickEvent::Touch(_) => false,
}
}
pub fn is_middle_click(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => false,
ClickEvent::Mouse(event) => {
event.down.button == MouseButton::Middle && event.up.button == MouseButton::Middle
}
ClickEvent::Touch(_) => false,
}
}
pub fn is_secondary(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => false,
ClickEvent::Mouse(event) => {
event.down.button == MouseButton::Right && event.up.button == MouseButton::Right
}
ClickEvent::Touch(event) => event.long_press,
}
}
pub fn standard_click(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => true,
ClickEvent::Mouse(event) => {
event.down.button == MouseButton::Left && event.up.button == MouseButton::Left
}
ClickEvent::Touch(event) => !event.long_press,
}
}
pub fn first_focus(&self) -> bool {
match self {
ClickEvent::Keyboard(_) => false,
ClickEvent::Mouse(event) => event.down.first_mouse,
ClickEvent::Touch(_) => false,
}
}
pub fn click_count(&self) -> usize {
match self {
ClickEvent::Keyboard(_) => 1,
ClickEvent::Mouse(event) => event.up.click_count,
ClickEvent::Touch(event) => event.tap_count,
}
}
pub fn is_keyboard(&self) -> bool {
match self {
ClickEvent::Mouse(_) | ClickEvent::Touch(_) => false,
ClickEvent::Keyboard(_) => true,
}
}
}
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug, Default)]
pub enum KeyboardButton {
#[default]
Enter,
Space,
}
impl Sealed for MouseMoveEvent {}
impl InputEvent for MouseMoveEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseMove(self)
}
}
impl MouseEvent for MouseMoveEvent {}
impl Sealed for ScrollWheelEvent {}
impl InputEvent for ScrollWheelEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::ScrollWheel(self)
}
}
impl MouseEvent for ScrollWheelEvent {}
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 Sealed for MouseExitEvent {}
impl InputEvent for MouseExitEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::MouseExited(self)
}
}
impl MouseEvent for MouseExitEvent {}
impl Render for ExternalPaths {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
impl Sealed for FileDropEvent {}
impl InputEvent for FileDropEvent {
fn to_platform_input(self) -> PlatformInput {
PlatformInput::FileDrop(self)
}
}
impl MouseEvent for FileDropEvent {}
#[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, cx)
})
.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);
});
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));
}
}