Skip to content

Commit d51edbd

Browse files
committed
feat: Adding Swipe to Navigate for macOS
1 parent 858ab9c commit d51edbd

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

crates/gpui/src/interactive.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,22 @@ impl InputEvent for FileDropEvent {
534534
}
535535
impl MouseEvent for FileDropEvent {}
536536

537+
/// A swipe direction on macOS.
538+
#[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)]
539+
pub enum SwipeDirection {
540+
Left,
541+
Right,
542+
Up,
543+
Down,
544+
}
545+
/// Recognizing touch pad gesture, such as swipe.
546+
/// For now only swipe is supported.
547+
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
548+
pub enum TouchPadGestureEvent {
549+
/// A swipe gesture was performed.
550+
Swipe(SwipeDirection),
551+
}
552+
537553
/// An enum corresponding to all kinds of platform input events.
538554
#[derive(Clone, Debug)]
539555
pub enum PlatformInput {
@@ -555,6 +571,8 @@ pub enum PlatformInput {
555571
ScrollWheel(ScrollWheelEvent),
556572
/// Files were dragged and dropped onto the window.
557573
FileDrop(FileDropEvent),
574+
/// Touch pad gesture was performed.
575+
TouchPad(TouchPadGestureEvent),
558576
}
559577

560578
impl PlatformInput {
@@ -569,6 +587,7 @@ impl PlatformInput {
569587
PlatformInput::MouseExited(event) => Some(event),
570588
PlatformInput::ScrollWheel(event) => Some(event),
571589
PlatformInput::FileDrop(event) => Some(event),
590+
PlatformInput::TouchPad(_) => None,
572591
}
573592
}
574593

@@ -583,6 +602,13 @@ impl PlatformInput {
583602
PlatformInput::MouseExited(_) => None,
584603
PlatformInput::ScrollWheel(_) => None,
585604
PlatformInput::FileDrop(_) => None,
605+
PlatformInput::TouchPad(_) => None,
606+
}
607+
}
608+
pub(crate) fn touchpad_event(&self) -> Option<&TouchPadGestureEvent> {
609+
match self {
610+
PlatformInput::TouchPad(event) => Some(event),
611+
_ => None,
586612
}
587613
}
588614
}

crates/gpui/src/platform/mac/events.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
Capslock, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton,
3-
MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection, Pixels,
4-
PlatformInput, ScrollDelta, ScrollWheelEvent, TouchPhase,
3+
MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection,
4+
Pixels, PlatformInput, ScrollDelta, ScrollWheelEvent, SwipeDirection, TouchPadGestureEvent, TouchPhase,
55
platform::mac::{
66
LMGetKbdType, NSStringExt, TISCopyCurrentKeyboardLayoutInputSource,
77
TISGetInputSourceProperty, UCKeyTranslate, kTISPropertyUnicodeKeyLayoutData,
@@ -186,33 +186,14 @@ impl PlatformInput {
186186
})
187187
})
188188
}
189-
// Some mice (like Logitech MX Master) send navigation buttons as swipe events
190-
NSEventType::NSEventTypeSwipe => {
191-
let navigation_direction = match native_event.phase() {
192-
NSEventPhase::NSEventPhaseEnded => match native_event.deltaX() {
193-
x if x > 0.0 => Some(NavigationDirection::Back),
194-
x if x < 0.0 => Some(NavigationDirection::Forward),
195-
_ => return None,
196-
},
197-
_ => return None,
198-
};
199-
200-
match navigation_direction {
201-
Some(direction) => window_height.map(|window_height| {
202-
Self::MouseDown(MouseDownEvent {
203-
button: MouseButton::Navigate(direction),
204-
position: point(
205-
px(native_event.locationInWindow().x as f32),
206-
window_height - px(native_event.locationInWindow().y as f32),
207-
),
208-
modifiers: read_modifiers(native_event),
209-
click_count: 1,
210-
first_mouse: false,
211-
})
212-
}),
213-
_ => None,
214-
}
189+
NSEventType::NSEventTypeSwipe => match (native_event.deltaX() as i32, native_event.deltaY() as i32) {
190+
(1, 0) => Some(SwipeDirection::Left),
191+
(-1, 0) => Some(SwipeDirection::Right),
192+
(0, 1) => Some(SwipeDirection::Up),
193+
(0, -1) => Some(SwipeDirection::Down),
194+
_ => None,
215195
}
196+
.map(|direction| Self::TouchPad(TouchPadGestureEvent::Swipe(direction))),
216197
NSEventType::NSScrollWheel => window_height.map(|window_height| {
217198
let phase = match native_event.phase() {
218199
NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => {

crates/gpui/src/window.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::{
1212
PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptButton, PromptLevel, Quad,
1313
Render, RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams, Replay, ResizeEdge,
1414
SMOOTH_SVG_SCALE_FACTOR, SUBPIXEL_VARIANTS, ScaledPixels, Scene, Shadow, SharedString, Size,
15-
StrikethroughStyle, Style, SubscriberSet, Subscription, TabHandles, TaffyLayoutEngine, Task,
16-
TextStyle, TextStyleRefinement, TransformationMatrix, Underline, UnderlineStyle,
15+
StrikethroughStyle, Style, SubscriberSet, Subscription, SwipeDirection, TabHandles, TaffyLayoutEngine, Task, TextStyle,
16+
TextStyleRefinement, TouchPadGestureEvent, TransformationMatrix, Underline, UnderlineStyle,
1717
WindowAppearance, WindowBackgroundAppearance, WindowBounds, WindowControls, WindowDecorations,
1818
WindowOptions, WindowParams, WindowTextSystem, point, prelude::*, px, rems, size,
1919
transparent_black,
@@ -3554,12 +3554,30 @@ impl Window {
35543554
}
35553555
},
35563556
PlatformInput::KeyDown(_) | PlatformInput::KeyUp(_) => event,
3557+
PlatformInput::TouchPad(TouchPadGestureEvent::Swipe(_)) => event,
35573558
};
35583559

35593560
if let Some(any_mouse_event) = event.mouse_event() {
35603561
self.dispatch_mouse_event(any_mouse_event, cx);
35613562
} else if let Some(any_key_event) = event.keyboard_event() {
35623563
self.dispatch_key_event(any_key_event, cx);
3564+
} else if let Some(TouchPadGestureEvent::Swipe(direction)) = event.touchpad_event() {
3565+
// Eventually we want to read from a config what to do, while forward/back is default.
3566+
let action_name = match direction {
3567+
SwipeDirection::Right => Some("pane::GoForward"),
3568+
SwipeDirection::Left => Some("pane::GoBack"),
3569+
_ => None,
3570+
};
3571+
3572+
if let Ok(direction) = action_name
3573+
.ok_or_else(|| crate::ActionBuildError::NotFound {
3574+
name: format!("swipe::{:?}", direction),
3575+
})
3576+
.and_then(|action_name| cx.build_action(action_name, None))
3577+
.map(|action| {
3578+
self.dispatch_action(action, cx);
3579+
direction
3580+
});
35633581
}
35643582

35653583
DispatchEventResult {

0 commit comments

Comments
 (0)