Add missing mouse button events and mouse history navigation

Co-Authored-By: Max Brunsfeld
Co-Authored-By: Nathan Sobo
This commit is contained in:
Keith Simmons 2022-03-08 15:34:44 -08:00
parent 03aa906068
commit fc36c706d3
6 changed files with 179 additions and 37 deletions

View file

@ -10,6 +10,8 @@ pub struct EventHandler {
child: ElementBox, child: ElementBox,
capture: Option<Box<dyn FnMut(&Event, RectF, &mut EventContext) -> bool>>, capture: Option<Box<dyn FnMut(&Event, RectF, &mut EventContext) -> bool>>,
mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>, mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>,
right_mouse_down: Option<Box<dyn FnMut(&mut EventContext) -> bool>>,
other_mouse_down: Option<Box<dyn FnMut(u16, &mut EventContext) -> bool>>,
} }
impl EventHandler { impl EventHandler {
@ -18,6 +20,8 @@ impl EventHandler {
child, child,
capture: None, capture: None,
mouse_down: None, mouse_down: None,
right_mouse_down: None,
other_mouse_down: None,
} }
} }
@ -29,6 +33,22 @@ impl EventHandler {
self self
} }
pub fn on_right_mouse_down<F>(mut self, callback: F) -> Self
where
F: 'static + FnMut(&mut EventContext) -> bool,
{
self.right_mouse_down = Some(Box::new(callback));
self
}
pub fn on_other_mouse_down<F>(mut self, callback: F) -> Self
where
F: 'static + FnMut(u16, &mut EventContext) -> bool,
{
self.other_mouse_down = Some(Box::new(callback));
self
}
pub fn capture<F>(mut self, callback: F) -> Self pub fn capture<F>(mut self, callback: F) -> Self
where where
F: 'static + FnMut(&Event, RectF, &mut EventContext) -> bool, F: 'static + FnMut(&Event, RectF, &mut EventContext) -> bool,
@ -86,7 +106,23 @@ impl Element for EventHandler {
} }
} }
false false
},
Event::RightMouseDown { position, .. } => {
if let Some(callback) = self.right_mouse_down.as_mut() {
if bounds.contains_point(*position) {
return callback(cx);
} }
}
false
},
Event::OtherMouseDown { position, button, .. } => {
if let Some(callback) = self.other_mouse_down.as_mut() {
if bounds.contains_point(*position) {
return callback(*button, cx);
}
}
false
},
_ => false, _ => false,
} }
} }

View file

@ -26,6 +26,30 @@ pub enum Event {
LeftMouseDragged { LeftMouseDragged {
position: Vector2F, position: Vector2F,
}, },
RightMouseDown {
position: Vector2F,
ctrl: bool,
alt: bool,
shift: bool,
cmd: bool,
click_count: usize,
},
RightMouseUp {
position: Vector2F,
},
OtherMouseDown {
position: Vector2F,
button: u16,
ctrl: bool,
alt: bool,
shift: bool,
cmd: bool,
click_count: usize,
},
OtherMouseUp {
position: Vector2F,
button: u16,
},
MouseMoved { MouseMoved {
position: Vector2F, position: Vector2F,
left_mouse_down: bool, left_mouse_down: bool,

View file

@ -125,6 +125,48 @@ impl Event {
window_height - native_event.locationInWindow().y as f32, window_height - native_event.locationInWindow().y as f32,
), ),
}), }),
NSEventType::NSRightMouseDown => {
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| Self::RightMouseDown {
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
}
NSEventType::NSRightMouseUp => window_height.map(|window_height| Self::RightMouseUp {
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
}),
NSEventType::NSOtherMouseDown => {
let modifiers = native_event.modifierFlags();
window_height.map(|window_height| Self::OtherMouseDown {
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
button: native_event.buttonNumber() as u16,
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask),
shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask),
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
click_count: native_event.clickCount() as usize,
})
}
NSEventType::NSOtherMouseUp => window_height.map(|window_height| Self::OtherMouseUp {
position: vec2f(
native_event.locationInWindow().x as f32,
window_height - native_event.locationInWindow().y as f32,
),
button: native_event.buttonNumber() as u16,
}),
NSEventType::NSLeftMouseDragged => { NSEventType::NSLeftMouseDragged => {
window_height.map(|window_height| Self::LeftMouseDragged { window_height.map(|window_height| Self::LeftMouseDragged {
position: vec2f( position: vec2f(

View file

@ -95,6 +95,22 @@ unsafe fn build_classes() {
sel!(mouseUp:), sel!(mouseUp:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),
); );
decl.add_method(
sel!(rightMouseDown:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(rightMouseUp:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(otherMouseDown:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(otherMouseUp:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method( decl.add_method(
sel!(mouseMoved:), sel!(mouseMoved:),
handle_view_event as extern "C" fn(&Object, Sel, id), handle_view_event as extern "C" fn(&Object, Sel, id),

View file

@ -8,7 +8,7 @@ use gpui::{
keymap::Binding, keymap::Binding,
platform::CursorStyle, platform::CursorStyle,
AnyViewHandle, Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext, AnyViewHandle, Entity, MutableAppContext, Quad, RenderContext, Task, View, ViewContext,
ViewHandle, ViewHandle, WeakViewHandle,
}; };
use postage::watch; use postage::watch;
use project::ProjectPath; use project::ProjectPath;
@ -27,8 +27,8 @@ action!(ActivateNextItem);
action!(CloseActiveItem); action!(CloseActiveItem);
action!(CloseInactiveItems); action!(CloseInactiveItems);
action!(CloseItem, usize); action!(CloseItem, usize);
action!(GoBack); action!(GoBack, Option<WeakViewHandle<Pane>>);
action!(GoForward); action!(GoForward, Option<WeakViewHandle<Pane>>);
const MAX_NAVIGATION_HISTORY_LEN: usize = 1024; const MAX_NAVIGATION_HISTORY_LEN: usize = 1024;
@ -54,11 +54,19 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(|pane: &mut Pane, action: &Split, cx| { cx.add_action(|pane: &mut Pane, action: &Split, cx| {
pane.split(action.0, cx); pane.split(action.0, cx);
}); });
cx.add_action(|workspace: &mut Workspace, _: &GoBack, cx| { cx.add_action(|workspace: &mut Workspace, action: &GoBack, cx| {
Pane::go_back(workspace, cx).detach(); Pane::go_back(
workspace,
action.0.as_ref().and_then(|weak_handle| weak_handle.upgrade(cx)),
cx
).detach();
}); });
cx.add_action(|workspace: &mut Workspace, _: &GoForward, cx| { cx.add_action(|workspace: &mut Workspace, action: &GoForward, cx| {
Pane::go_forward(workspace, cx).detach(); Pane::go_forward(
workspace,
action.0.as_ref().and_then(|weak_handle| weak_handle.upgrade(cx)),
cx
).detach();
}); });
cx.add_bindings(vec![ cx.add_bindings(vec![
@ -70,8 +78,8 @@ pub fn init(cx: &mut MutableAppContext) {
Binding::new("cmd-k down", Split(SplitDirection::Down), Some("Pane")), Binding::new("cmd-k down", Split(SplitDirection::Down), Some("Pane")),
Binding::new("cmd-k left", Split(SplitDirection::Left), Some("Pane")), Binding::new("cmd-k left", Split(SplitDirection::Left), Some("Pane")),
Binding::new("cmd-k right", Split(SplitDirection::Right), Some("Pane")), Binding::new("cmd-k right", Split(SplitDirection::Right), Some("Pane")),
Binding::new("ctrl--", GoBack, Some("Pane")), Binding::new("ctrl--", GoBack(None), Some("Pane")),
Binding::new("shift-ctrl-_", GoForward, Some("Pane")), Binding::new("shift-ctrl-_", GoForward(None), Some("Pane")),
]); ]);
} }
@ -163,19 +171,19 @@ impl Pane {
cx.emit(Event::Activate); cx.emit(Event::Activate);
} }
pub fn go_back(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Task<()> { pub fn go_back(workspace: &mut Workspace, pane: Option<ViewHandle<Pane>>, cx: &mut ViewContext<Workspace>) -> Task<()> {
Self::navigate_history( Self::navigate_history(
workspace, workspace,
workspace.active_pane().clone(), pane.unwrap_or_else(|| workspace.active_pane().clone()),
NavigationMode::GoingBack, NavigationMode::GoingBack,
cx, cx,
) )
} }
pub fn go_forward(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Task<()> { pub fn go_forward(workspace: &mut Workspace, pane: Option<ViewHandle<Pane>>, cx: &mut ViewContext<Workspace>) -> Task<()> {
Self::navigate_history( Self::navigate_history(
workspace, workspace,
workspace.active_pane().clone(), pane.unwrap_or_else(|| workspace.active_pane().clone()),
NavigationMode::GoingForward, NavigationMode::GoingForward,
cx, cx,
) )
@ -187,6 +195,8 @@ impl Pane {
mode: NavigationMode, mode: NavigationMode,
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) -> Task<()> { ) -> Task<()> {
workspace.activate_pane(pane.clone(), cx);
let to_load = pane.update(cx, |pane, cx| { let to_load = pane.update(cx, |pane, cx| {
// Retrieve the weak item handle from the history. // Retrieve the weak item handle from the history.
let entry = pane.nav_history.borrow_mut().pop(mode)?; let entry = pane.nav_history.borrow_mut().pop(mode)?;
@ -634,6 +644,9 @@ impl View for Pane {
} }
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
let this = cx.handle();
EventHandler::new(
if let Some(active_item) = self.active_item() { if let Some(active_item) = self.active_item() {
Flex::column() Flex::column()
.with_child(self.render_tabs(cx)) .with_child(self.render_tabs(cx))
@ -643,10 +656,21 @@ impl View for Pane {
.map(|view| ChildView::new(view).boxed()), .map(|view| ChildView::new(view).boxed()),
) )
.with_child(ChildView::new(active_item).flexible(1., true).boxed()) .with_child(ChildView::new(active_item).flexible(1., true).boxed())
.named("pane") .boxed()
} else { } else {
Empty::new().named("pane") Empty::new().boxed()
} }
)
.on_other_mouse_down(move |button, cx| {
match button {
3 => cx.dispatch_action(GoBack(Some(this.clone()))),
4 => cx.dispatch_action(GoForward(Some(this.clone()))),
_ => return false,
};
true
})
.named("pane")
} }
fn on_focus(&mut self, cx: &mut ViewContext<Self>) { fn on_focus(&mut self, cx: &mut ViewContext<Self>) {

View file

@ -747,44 +747,44 @@ mod tests {
(file3.clone(), DisplayPoint::new(15, 0)) (file3.clone(), DisplayPoint::new(15, 0))
); );
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0)) (file3.clone(), DisplayPoint::new(0, 0))
); );
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file2.clone(), DisplayPoint::new(0, 0)) (file2.clone(), DisplayPoint::new(0, 0))
); );
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0)) (file1.clone(), DisplayPoint::new(10, 0))
); );
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(0, 0)) (file1.clone(), DisplayPoint::new(0, 0))
); );
// Go back one more time and ensure we don't navigate past the first item in the history. // Go back one more time and ensure we don't navigate past the first item in the history.
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(0, 0)) (file1.clone(), DisplayPoint::new(0, 0))
); );
workspace.update(cx, |w, cx| Pane::go_forward(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_forward(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0)) (file1.clone(), DisplayPoint::new(10, 0))
); );
workspace.update(cx, |w, cx| Pane::go_forward(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_forward(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file2.clone(), DisplayPoint::new(0, 0)) (file2.clone(), DisplayPoint::new(0, 0))
@ -798,7 +798,7 @@ mod tests {
.update(cx, |pane, cx| pane.close_item(editor3.id(), cx)); .update(cx, |pane, cx| pane.close_item(editor3.id(), cx));
drop(editor3); drop(editor3);
}); });
workspace.update(cx, |w, cx| Pane::go_forward(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_forward(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0)) (file3.clone(), DisplayPoint::new(0, 0))
@ -818,12 +818,12 @@ mod tests {
}) })
.await .await
.unwrap(); .unwrap();
workspace.update(cx, |w, cx| Pane::go_back(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_back(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file1.clone(), DisplayPoint::new(10, 0)) (file1.clone(), DisplayPoint::new(10, 0))
); );
workspace.update(cx, |w, cx| Pane::go_forward(w, cx)).await; workspace.update(cx, |w, cx| Pane::go_forward(w, None, cx)).await;
assert_eq!( assert_eq!(
active_location(&workspace, cx), active_location(&workspace, cx),
(file3.clone(), DisplayPoint::new(0, 0)) (file3.clone(), DisplayPoint::new(0, 0))