Fix dismissing context menu when clicking on an item dispatched action (#3779)

Release Notes:

- N/A
This commit is contained in:
Antonio Scandurra 2023-12-22 12:34:08 +01:00 committed by GitHub
commit 061bcf1b6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 74 deletions

View file

@ -29,6 +29,7 @@ pub struct ContextMenu {
focus_handle: FocusHandle, focus_handle: FocusHandle,
selected_index: Option<usize>, selected_index: Option<usize>,
delayed: bool, delayed: bool,
clicked: bool,
_on_blur_subscription: Subscription, _on_blur_subscription: Subscription,
} }
@ -56,6 +57,7 @@ impl ContextMenu {
focus_handle, focus_handle,
selected_index: None, selected_index: None,
delayed: false, delayed: false,
clicked: false,
_on_blur_subscription, _on_blur_subscription,
}, },
cx, cx,
@ -187,6 +189,11 @@ impl ContextMenu {
} }
pub fn on_action_dispatch(&mut self, dispatched: &Box<dyn Action>, cx: &mut ViewContext<Self>) { pub fn on_action_dispatch(&mut self, dispatched: &Box<dyn Action>, cx: &mut ViewContext<Self>) {
if self.clicked {
cx.propagate();
return;
}
if let Some(ix) = self.items.iter().position(|item| { if let Some(ix) = self.items.iter().position(|item| {
if let ContextMenuItem::Entry { if let ContextMenuItem::Entry {
action: Some(action), action: Some(action),
@ -269,6 +276,7 @@ impl Render for ContextMenu {
action, action,
} => { } => {
let handler = handler.clone(); let handler = handler.clone();
let menu = cx.view().downgrade();
let label_element = if let Some(icon) = icon { let label_element = if let Some(icon) = icon {
h_stack() h_stack()
@ -283,10 +291,14 @@ impl Render for ContextMenu {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.selected(Some(ix) == self.selected_index) .selected(Some(ix) == self.selected_index)
.on_click(cx.listener(move |_, _, cx| { .on_click(move |_, cx| {
handler(cx); handler(cx);
cx.emit(DismissEvent); menu.update(cx, |menu, cx| {
})) menu.clicked = true;
cx.emit(DismissEvent);
})
.ok();
})
.child( .child(
h_stack() h_stack()
.w_full() .w_full()
@ -304,13 +316,18 @@ impl Render for ContextMenu {
handler, handler,
} => { } => {
let handler = handler.clone(); let handler = handler.clone();
let menu = cx.view().downgrade();
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.selected(Some(ix) == self.selected_index) .selected(Some(ix) == self.selected_index)
.on_click(cx.listener(move |_, _, cx| { .on_click(move |_, cx| {
handler(cx); handler(cx);
cx.emit(DismissEvent); menu.update(cx, |menu, cx| {
})) menu.clicked = true;
cx.emit(DismissEvent);
})
.ok();
})
.child(entry_render(cx)) .child(entry_render(cx))
.into_any_element() .into_any_element()
} }

View file

@ -1571,78 +1571,80 @@ impl Pane {
} }
}; };
let pane = cx.view().clone(); let pane = cx.view().downgrade();
right_click_menu(ix).trigger(tab).menu(move |cx| { right_click_menu(ix).trigger(tab).menu(move |cx| {
let pane = pane.clone(); let pane = pane.clone();
ContextMenu::build(cx, move |menu, cx| { ContextMenu::build(cx, move |mut menu, cx| {
let menu = menu if let Some(pane) = pane.upgrade() {
.entry( menu = menu
"Close", .entry(
Some(Box::new(CloseActiveItem { save_intent: None })), "Close",
cx.handler_for(&pane, move |pane, cx| { Some(Box::new(CloseActiveItem { save_intent: None })),
pane.close_item_by_id(item_id, SaveIntent::Close, cx) cx.handler_for(&pane, move |pane, cx| {
.detach_and_log_err(cx); pane.close_item_by_id(item_id, SaveIntent::Close, cx)
}), .detach_and_log_err(cx);
) }),
.entry( )
"Close Others", .entry(
Some(Box::new(CloseInactiveItems)), "Close Others",
cx.handler_for(&pane, move |pane, cx| { Some(Box::new(CloseInactiveItems)),
pane.close_items(cx, SaveIntent::Close, |id| id != item_id) cx.handler_for(&pane, move |pane, cx| {
.detach_and_log_err(cx); pane.close_items(cx, SaveIntent::Close, |id| id != item_id)
}), .detach_and_log_err(cx);
) }),
.separator() )
.entry( .separator()
"Close Left", .entry(
Some(Box::new(CloseItemsToTheLeft)), "Close Left",
cx.handler_for(&pane, move |pane, cx| { Some(Box::new(CloseItemsToTheLeft)),
pane.close_items_to_the_left_by_id(item_id, cx) cx.handler_for(&pane, move |pane, cx| {
.detach_and_log_err(cx); pane.close_items_to_the_left_by_id(item_id, cx)
}), .detach_and_log_err(cx);
) }),
.entry( )
"Close Right", .entry(
Some(Box::new(CloseItemsToTheRight)), "Close Right",
cx.handler_for(&pane, move |pane, cx| { Some(Box::new(CloseItemsToTheRight)),
pane.close_items_to_the_right_by_id(item_id, cx) cx.handler_for(&pane, move |pane, cx| {
.detach_and_log_err(cx); pane.close_items_to_the_right_by_id(item_id, cx)
}), .detach_and_log_err(cx);
) }),
.separator() )
.entry( .separator()
"Close Clean", .entry(
Some(Box::new(CloseCleanItems)), "Close Clean",
cx.handler_for(&pane, move |pane, cx| { Some(Box::new(CloseCleanItems)),
pane.close_clean_items(&CloseCleanItems, cx) cx.handler_for(&pane, move |pane, cx| {
.map(|task| task.detach_and_log_err(cx)); pane.close_clean_items(&CloseCleanItems, cx)
}), .map(|task| task.detach_and_log_err(cx));
) }),
.entry( )
"Close All", .entry(
Some(Box::new(CloseAllItems { save_intent: None })), "Close All",
cx.handler_for(&pane, |pane, cx| { Some(Box::new(CloseAllItems { save_intent: None })),
pane.close_all_items(&CloseAllItems { save_intent: None }, cx) cx.handler_for(&pane, |pane, cx| {
.map(|task| task.detach_and_log_err(cx)); pane.close_all_items(&CloseAllItems { save_intent: None }, cx)
}), .map(|task| task.detach_and_log_err(cx));
); }),
);
if let Some(entry) = single_entry_to_resolve { if let Some(entry) = single_entry_to_resolve {
let entry_id = entry.to_proto(); let entry_id = entry.to_proto();
menu.separator().entry( menu = menu.separator().entry(
"Reveal In Project Panel", "Reveal In Project Panel",
Some(Box::new(RevealInProjectPanel { entry_id })), Some(Box::new(RevealInProjectPanel { entry_id })),
cx.handler_for(&pane, move |pane, cx| { cx.handler_for(&pane, move |pane, cx| {
pane.project.update(cx, |_, cx| { pane.project.update(cx, |_, cx| {
cx.emit(project::Event::RevealInProjectPanel( cx.emit(project::Event::RevealInProjectPanel(
ProjectEntryId::from_proto(entry_id), ProjectEntryId::from_proto(entry_id),
)) ))
}); });
}), }),
) );
} else { }
menu
} }
menu
}) })
}) })
} }