Add click out events to GPUI (#2659)

This PR adds a new mouse event type for catching when a click happens
outside of a given region.

This was added because I noticed a 'race condition' between the context
menu and the buttons which deploy a context menu. Buttons use on
an`on_click()` handler to deploy the context menu, but the context menu
was closing itself with an `on_down_out()` handler. This meant that the
order of operations was:

0. Context menu is open
1. User presses down on the button, _outside of the context menu_ 
2. `on_down_out()` is fired, closing the context menu
3. User releases the mouse
4. `click()` is fired, checks the state of the context menu, finds that
it's closed, and so opens it

You can see this behavior demonstrated with this video with a long-click
here:


https://github.com/zed-industries/zed/assets/2280405/588234c3-1567-477f-9a12-9e6a70643527

~~Switching from `on_down_out()` to `on_click_out()` means that the
click handler for the button can close the menu before the context menu
gets a chance to close itself.~~

~~However, GPUI does not have an `on_click_out()` event, hence this
PR.~~

~~Here's an example of the new behavior, with the same long-click
action:~~


https://github.com/zed-industries/zed/assets/2280405/a59f4d6f-db24-403f-a281-2c1148499413

Unfortunately, this `click_out` is the incorrect event for this to
happen on. This PR now adds a mechanism for delaying the firing of a
cancel action so that toggle buttons can signal that this on_down event
should not result in a menu closure.

Release Notes:

* Made context menus deployed from buttons toggle, instead of
hide-and-re-show, visibility on click
This commit is contained in:
Mikayla Maki 2023-06-29 17:33:37 -07:00 committed by GitHub
commit f6b64dc67a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 167 additions and 31 deletions

View file

@ -317,7 +317,7 @@ impl CollabTitlebarItem {
), ),
] ]
}; };
user_menu.show(Default::default(), AnchorCorner::TopRight, items, cx); user_menu.toggle(Default::default(), AnchorCorner::TopRight, items, cx);
}); });
} }
@ -683,6 +683,9 @@ impl CollabTitlebarItem {
.into_any() .into_any()
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_down(MouseButton::Left, move |_, this, cx| {
this.user_menu.update(cx, |menu, _| menu.delay_cancel());
})
.on_click(MouseButton::Left, move |_, this, cx| { .on_click(MouseButton::Left, move |_, this, cx| {
this.toggle_user_menu(&Default::default(), cx) this.toggle_user_menu(&Default::default(), cx)
}) })

View file

@ -124,6 +124,7 @@ pub struct ContextMenu {
items: Vec<ContextMenuItem>, items: Vec<ContextMenuItem>,
selected_index: Option<usize>, selected_index: Option<usize>,
visible: bool, visible: bool,
delay_cancel: bool,
previously_focused_view_id: Option<usize>, previously_focused_view_id: Option<usize>,
parent_view_id: usize, parent_view_id: usize,
_actions_observation: Subscription, _actions_observation: Subscription,
@ -178,6 +179,7 @@ impl ContextMenu {
pub fn new(parent_view_id: usize, cx: &mut ViewContext<Self>) -> Self { pub fn new(parent_view_id: usize, cx: &mut ViewContext<Self>) -> Self {
Self { Self {
show_count: 0, show_count: 0,
delay_cancel: false,
anchor_position: Default::default(), anchor_position: Default::default(),
anchor_corner: AnchorCorner::TopLeft, anchor_corner: AnchorCorner::TopLeft,
position_mode: OverlayPositionMode::Window, position_mode: OverlayPositionMode::Window,
@ -232,15 +234,23 @@ impl ContextMenu {
} }
} }
pub fn delay_cancel(&mut self) {
self.delay_cancel = true;
}
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) { fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
self.reset(cx); if !self.delay_cancel {
let show_count = self.show_count; self.reset(cx);
cx.defer(move |this, cx| { let show_count = self.show_count;
if cx.handle().is_focused(cx) && this.show_count == show_count { cx.defer(move |this, cx| {
let window_id = cx.window_id(); if cx.handle().is_focused(cx) && this.show_count == show_count {
(**cx).focus(window_id, this.previously_focused_view_id.take()); let window_id = cx.window_id();
} (**cx).focus(window_id, this.previously_focused_view_id.take());
}); }
});
} else {
self.delay_cancel = false;
}
} }
fn reset(&mut self, cx: &mut ViewContext<Self>) { fn reset(&mut self, cx: &mut ViewContext<Self>) {
@ -293,6 +303,34 @@ impl ContextMenu {
} }
} }
pub fn toggle(
&mut self,
anchor_position: Vector2F,
anchor_corner: AnchorCorner,
items: Vec<ContextMenuItem>,
cx: &mut ViewContext<Self>,
) {
if self.visible() {
self.cancel(&Cancel, cx);
} else {
let mut items = items.into_iter().peekable();
if items.peek().is_some() {
self.items = items.collect();
self.anchor_position = anchor_position;
self.anchor_corner = anchor_corner;
self.visible = true;
self.show_count += 1;
if !cx.is_self_focused() {
self.previously_focused_view_id = cx.focused_view_id();
}
cx.focus_self();
} else {
self.visible = false;
}
}
cx.notify();
}
pub fn show( pub fn show(
&mut self, &mut self,
anchor_position: Vector2F, anchor_position: Vector2F,

View file

@ -102,6 +102,9 @@ impl View for CopilotButton {
} }
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_down(MouseButton::Left, |_, this, cx| {
this.popup_menu.update(cx, |menu, _| menu.delay_cancel());
})
.on_click(MouseButton::Left, { .on_click(MouseButton::Left, {
let status = status.clone(); let status = status.clone();
move |_, this, cx| match status { move |_, this, cx| match status {
@ -186,7 +189,7 @@ impl CopilotButton {
})); }));
self.popup_menu.update(cx, |menu, cx| { self.popup_menu.update(cx, |menu, cx| {
menu.show( menu.toggle(
Default::default(), Default::default(),
AnchorCorner::BottomRight, AnchorCorner::BottomRight,
menu_options, menu_options,
@ -266,7 +269,7 @@ impl CopilotButton {
menu_options.push(ContextMenuItem::action("Sign Out", SignOut)); menu_options.push(ContextMenuItem::action("Sign Out", SignOut));
self.popup_menu.update(cx, |menu, cx| { self.popup_menu.update(cx, |menu, cx| {
menu.show( menu.toggle(
Default::default(), Default::default(),
AnchorCorner::BottomRight, AnchorCorner::BottomRight,
menu_options, menu_options,

View file

@ -8,8 +8,8 @@ use crate::{
MouseButton, MouseMovedEvent, PromptLevel, WindowBounds, MouseButton, MouseMovedEvent, PromptLevel, WindowBounds,
}, },
scene::{ scene::{
CursorRegion, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, CursorRegion, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag, MouseEvent,
MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, Scene,
}, },
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
util::post_inc, util::post_inc,
@ -524,6 +524,10 @@ impl<'a> WindowContext<'a> {
region: Default::default(), region: Default::default(),
platform_event: e.clone(), platform_event: e.clone(),
})); }));
mouse_events.push(MouseEvent::ClickOut(MouseClickOut {
region: Default::default(),
platform_event: e.clone(),
}));
} }
Event::MouseMoved( Event::MouseMoved(
@ -712,7 +716,10 @@ impl<'a> WindowContext<'a> {
} }
} }
MouseEvent::MoveOut(_) | MouseEvent::UpOut(_) | MouseEvent::DownOut(_) => { MouseEvent::MoveOut(_)
| MouseEvent::UpOut(_)
| MouseEvent::DownOut(_)
| MouseEvent::ClickOut(_) => {
for (mouse_region, _) in self.window.mouse_regions.iter().rev() { for (mouse_region, _) in self.window.mouse_regions.iter().rev() {
// NOT contains // NOT contains
if !mouse_region if !mouse_region

View file

@ -7,8 +7,8 @@ use crate::{
platform::CursorStyle, platform::CursorStyle,
platform::MouseButton, platform::MouseButton,
scene::{ scene::{
CursorRegion, HandlerSet, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseHover, CursorRegion, HandlerSet, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag,
MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut,
}, },
AnyElement, Element, EventContext, LayoutContext, MouseRegion, MouseState, SceneBuilder, AnyElement, Element, EventContext, LayoutContext, MouseRegion, MouseState, SceneBuilder,
SizeConstraint, View, ViewContext, SizeConstraint, View, ViewContext,
@ -136,6 +136,15 @@ impl<Tag, V: View> MouseEventHandler<Tag, V> {
self self
} }
pub fn on_click_out(
mut self,
button: MouseButton,
handler: impl Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
) -> Self {
self.handlers = self.handlers.on_click_out(button, handler);
self
}
pub fn on_down_out( pub fn on_down_out(
mut self, mut self,
button: MouseButton, button: MouseButton,

View file

@ -99,6 +99,20 @@ impl Deref for MouseClick {
} }
} }
#[derive(Debug, Default, Clone)]
pub struct MouseClickOut {
pub region: RectF,
pub platform_event: MouseButtonEvent,
}
impl Deref for MouseClickOut {
type Target = MouseButtonEvent;
fn deref(&self) -> &Self::Target {
&self.platform_event
}
}
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct MouseDownOut { pub struct MouseDownOut {
pub region: RectF, pub region: RectF,
@ -150,6 +164,7 @@ pub enum MouseEvent {
Down(MouseDown), Down(MouseDown),
Up(MouseUp), Up(MouseUp),
Click(MouseClick), Click(MouseClick),
ClickOut(MouseClickOut),
DownOut(MouseDownOut), DownOut(MouseDownOut),
UpOut(MouseUpOut), UpOut(MouseUpOut),
ScrollWheel(MouseScrollWheel), ScrollWheel(MouseScrollWheel),
@ -165,6 +180,7 @@ impl MouseEvent {
MouseEvent::Down(r) => r.region = region, MouseEvent::Down(r) => r.region = region,
MouseEvent::Up(r) => r.region = region, MouseEvent::Up(r) => r.region = region,
MouseEvent::Click(r) => r.region = region, MouseEvent::Click(r) => r.region = region,
MouseEvent::ClickOut(r) => r.region = region,
MouseEvent::DownOut(r) => r.region = region, MouseEvent::DownOut(r) => r.region = region,
MouseEvent::UpOut(r) => r.region = region, MouseEvent::UpOut(r) => r.region = region,
MouseEvent::ScrollWheel(r) => r.region = region, MouseEvent::ScrollWheel(r) => r.region = region,
@ -182,6 +198,7 @@ impl MouseEvent {
MouseEvent::Down(_) => true, MouseEvent::Down(_) => true,
MouseEvent::Up(_) => true, MouseEvent::Up(_) => true,
MouseEvent::Click(_) => true, MouseEvent::Click(_) => true,
MouseEvent::ClickOut(_) => true,
MouseEvent::DownOut(_) => false, MouseEvent::DownOut(_) => false,
MouseEvent::UpOut(_) => false, MouseEvent::UpOut(_) => false,
MouseEvent::ScrollWheel(_) => true, MouseEvent::ScrollWheel(_) => true,
@ -222,6 +239,10 @@ impl MouseEvent {
discriminant(&MouseEvent::Click(Default::default())) discriminant(&MouseEvent::Click(Default::default()))
} }
pub fn click_out_disc() -> Discriminant<MouseEvent> {
discriminant(&MouseEvent::ClickOut(Default::default()))
}
pub fn down_out_disc() -> Discriminant<MouseEvent> { pub fn down_out_disc() -> Discriminant<MouseEvent> {
discriminant(&MouseEvent::DownOut(Default::default())) discriminant(&MouseEvent::DownOut(Default::default()))
} }
@ -239,6 +260,7 @@ impl MouseEvent {
MouseEvent::Down(e) => HandlerKey::new(Self::down_disc(), Some(e.button)), MouseEvent::Down(e) => HandlerKey::new(Self::down_disc(), Some(e.button)),
MouseEvent::Up(e) => HandlerKey::new(Self::up_disc(), Some(e.button)), MouseEvent::Up(e) => HandlerKey::new(Self::up_disc(), Some(e.button)),
MouseEvent::Click(e) => HandlerKey::new(Self::click_disc(), Some(e.button)), MouseEvent::Click(e) => HandlerKey::new(Self::click_disc(), Some(e.button)),
MouseEvent::ClickOut(e) => HandlerKey::new(Self::click_out_disc(), Some(e.button)),
MouseEvent::UpOut(e) => HandlerKey::new(Self::up_out_disc(), Some(e.button)), MouseEvent::UpOut(e) => HandlerKey::new(Self::up_out_disc(), Some(e.button)),
MouseEvent::DownOut(e) => HandlerKey::new(Self::down_out_disc(), Some(e.button)), MouseEvent::DownOut(e) => HandlerKey::new(Self::down_out_disc(), Some(e.button)),
MouseEvent::ScrollWheel(_) => HandlerKey::new(Self::scroll_wheel_disc(), None), MouseEvent::ScrollWheel(_) => HandlerKey::new(Self::scroll_wheel_disc(), None),

View file

@ -14,7 +14,7 @@ use super::{
MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, MouseMove, MouseUp, MouseClick, MouseDown, MouseDownOut, MouseDrag, MouseEvent, MouseHover, MouseMove, MouseUp,
MouseUpOut, MouseUpOut,
}, },
MouseMoveOut, MouseScrollWheel, MouseClickOut, MouseMoveOut, MouseScrollWheel,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -89,6 +89,15 @@ impl MouseRegion {
self self
} }
pub fn on_click_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
where
V: View,
F: Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
{
self.handlers = self.handlers.on_click_out(button, handler);
self
}
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
where where
V: View, V: View,
@ -246,6 +255,10 @@ impl HandlerSet {
HandlerKey::new(MouseEvent::click_disc(), Some(button)), HandlerKey::new(MouseEvent::click_disc(), Some(button)),
SmallVec::from_buf([Rc::new(|_, _, _, _| true)]), SmallVec::from_buf([Rc::new(|_, _, _, _| true)]),
); );
set.insert(
HandlerKey::new(MouseEvent::click_out_disc(), Some(button)),
SmallVec::from_buf([Rc::new(|_, _, _, _| true)]),
);
set.insert( set.insert(
HandlerKey::new(MouseEvent::down_out_disc(), Some(button)), HandlerKey::new(MouseEvent::down_out_disc(), Some(button)),
SmallVec::from_buf([Rc::new(|_, _, _, _| true)]), SmallVec::from_buf([Rc::new(|_, _, _, _| true)]),
@ -405,6 +418,28 @@ impl HandlerSet {
self self
} }
pub fn on_click_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
where
V: View,
F: Fn(MouseClickOut, &mut V, &mut EventContext<V>) + 'static,
{
self.insert(MouseEvent::click_out_disc(), Some(button),
Rc::new(move |region_event, view, cx, view_id| {
if let MouseEvent::ClickOut(e) = region_event {
let view = view.downcast_mut().unwrap();
let mut cx = ViewContext::mutable(cx, view_id);
let mut cx = EventContext::new(&mut cx);
handler(e, view, &mut cx);
cx.handled
} else {
panic!(
"Mouse Region Event incorrectly called with mismatched event type. Expected MouseRegionEvent::ClickOut, found {:?}",
region_event);
}
}));
self
}
pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self pub fn on_down_out<V, F>(mut self, button: MouseButton, handler: F) -> Self
where where
V: View, V: View,

View file

@ -395,16 +395,17 @@ impl TerminalElement {
// Terminal Emulator controlled behavior: // Terminal Emulator controlled behavior:
region = region region = region
// Start selections // Start selections
.on_down( .on_down(MouseButton::Left, move |event, v: &mut TerminalView, cx| {
MouseButton::Left, cx.focus_parent();
TerminalElement::generic_button_handler( v.context_menu.update(cx, |menu, _cx| menu.delay_cancel());
connection, if let Some(conn_handle) = connection.upgrade(cx) {
origin, conn_handle.update(cx, |terminal, cx| {
move |terminal, origin, e, _cx| { terminal.mouse_down(&event, origin);
terminal.mouse_down(&e, origin);
}, cx.notify();
), })
) }
})
// Update drag selections // Update drag selections
.on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| { .on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| {
if cx.is_self_focused() { if cx.is_self_focused() {

View file

@ -87,6 +87,7 @@ impl TerminalPanel {
} }
}) })
}, },
|_, _| {},
None, None,
)) ))
.with_child(Pane::render_tab_bar_button( .with_child(Pane::render_tab_bar_button(
@ -100,6 +101,7 @@ impl TerminalPanel {
Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))), Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))),
cx, cx,
move |pane, cx| pane.toggle_zoom(&Default::default(), cx), move |pane, cx| pane.toggle_zoom(&Default::default(), cx),
|_, _| {},
None, None,
)) ))
.into_any() .into_any()

View file

@ -273,6 +273,11 @@ impl Pane {
Some(("New...".into(), None)), Some(("New...".into(), None)),
cx, cx,
|pane, cx| pane.deploy_new_menu(cx), |pane, cx| pane.deploy_new_menu(cx),
|pane, cx| {
pane.tab_bar_context_menu
.handle
.update(cx, |menu, _| menu.delay_cancel())
},
pane.tab_bar_context_menu pane.tab_bar_context_menu
.handle_if_kind(TabBarContextMenuKind::New), .handle_if_kind(TabBarContextMenuKind::New),
)) ))
@ -283,6 +288,11 @@ impl Pane {
Some(("Split Pane".into(), None)), Some(("Split Pane".into(), None)),
cx, cx,
|pane, cx| pane.deploy_split_menu(cx), |pane, cx| pane.deploy_split_menu(cx),
|pane, cx| {
pane.tab_bar_context_menu
.handle
.update(cx, |menu, _| menu.delay_cancel())
},
pane.tab_bar_context_menu pane.tab_bar_context_menu
.handle_if_kind(TabBarContextMenuKind::Split), .handle_if_kind(TabBarContextMenuKind::Split),
)) ))
@ -304,6 +314,7 @@ impl Pane {
Some((tooltip_label, Some(Box::new(ToggleZoom)))), Some((tooltip_label, Some(Box::new(ToggleZoom)))),
cx, cx,
move |pane, cx| pane.toggle_zoom(&Default::default(), cx), move |pane, cx| pane.toggle_zoom(&Default::default(), cx),
move |_, _| {},
None, None,
) )
}) })
@ -988,7 +999,7 @@ impl Pane {
fn deploy_split_menu(&mut self, cx: &mut ViewContext<Self>) { fn deploy_split_menu(&mut self, cx: &mut ViewContext<Self>) {
self.tab_bar_context_menu.handle.update(cx, |menu, cx| { self.tab_bar_context_menu.handle.update(cx, |menu, cx| {
menu.show( menu.toggle(
Default::default(), Default::default(),
AnchorCorner::TopRight, AnchorCorner::TopRight,
vec![ vec![
@ -1006,7 +1017,7 @@ impl Pane {
fn deploy_new_menu(&mut self, cx: &mut ViewContext<Self>) { fn deploy_new_menu(&mut self, cx: &mut ViewContext<Self>) {
self.tab_bar_context_menu.handle.update(cx, |menu, cx| { self.tab_bar_context_menu.handle.update(cx, |menu, cx| {
menu.show( menu.toggle(
Default::default(), Default::default(),
AnchorCorner::TopRight, AnchorCorner::TopRight,
vec![ vec![
@ -1416,13 +1427,17 @@ impl Pane {
.into_any() .into_any()
} }
pub fn render_tab_bar_button<F: 'static + Fn(&mut Pane, &mut EventContext<Pane>)>( pub fn render_tab_bar_button<
F1: 'static + Fn(&mut Pane, &mut EventContext<Pane>),
F2: 'static + Fn(&mut Pane, &mut EventContext<Pane>),
>(
index: usize, index: usize,
icon: &'static str, icon: &'static str,
is_active: bool, is_active: bool,
tooltip: Option<(String, Option<Box<dyn Action>>)>, tooltip: Option<(String, Option<Box<dyn Action>>)>,
cx: &mut ViewContext<Pane>, cx: &mut ViewContext<Pane>,
on_click: F, on_click: F1,
on_down: F2,
context_menu: Option<ViewHandle<ContextMenu>>, context_menu: Option<ViewHandle<ContextMenu>>,
) -> AnyElement<Pane> { ) -> AnyElement<Pane> {
enum TabBarButton {} enum TabBarButton {}
@ -1440,6 +1455,7 @@ impl Pane {
.with_height(style.button_width) .with_height(style.button_width)
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.on_down(MouseButton::Left, move |_, pane, cx| on_down(pane, cx))
.on_click(MouseButton::Left, move |_, pane, cx| on_click(pane, cx)) .on_click(MouseButton::Left, move |_, pane, cx| on_click(pane, cx))
.into_any(); .into_any();
if let Some((tooltip, action)) = tooltip { if let Some((tooltip, action)) = tooltip {