Fix popin for project panel by pre-resolving keybindings in terms of the project panel

This commit is contained in:
Mikayla 2024-01-18 15:16:09 -08:00
parent 578bcecdb6
commit a5084510a1
No known key found for this signature in database
2 changed files with 96 additions and 62 deletions

View File

@ -381,67 +381,59 @@ impl ProjectPanel {
let is_local = project.is_local(); let is_local = project.is_local();
let is_read_only = project.is_read_only(); let is_read_only = project.is_read_only();
let context_menu = ContextMenu::build(cx, |mut menu, cx| { let context_menu = ContextMenu::build(cx, |menu, cx| {
if is_read_only { menu.context(self.focus_handle.clone()).then_if_else(
menu = menu.action("Copy Relative Path", Box::new(CopyRelativePath)); is_read_only,
if is_dir { |menu| {
menu = menu.action("Search Inside", Box::new(NewSearchInDirectory)) menu.action("Copy Relative Path", Box::new(CopyRelativePath))
} .then_if(is_dir, |menu| {
menu.action("Search Inside", Box::new(NewSearchInDirectory))
return menu; })
} },
|menu| {
if is_local { menu.then_if(is_local, |menu| {
menu = menu.action( menu.action(
"Add Folder to Project", "Add Folder to Project",
Box::new(workspace::AddFolderToProject), Box::new(workspace::AddFolderToProject),
); )
if is_root { .then_if(is_root, |menu| {
menu = menu.entry( menu.entry(
"Remove from Project", "Remove from Project",
None, None,
cx.handler_for(&this, move |this, cx| { cx.handler_for(&this, move |this, cx| {
this.project.update(cx, |project, cx| { this.project.update(cx, |project, cx| {
project.remove_worktree(worktree_id, cx) project.remove_worktree(worktree_id, cx)
}); });
}), }),
); )
} })
} })
.action("New File", Box::new(NewFile))
menu = menu .action("New Folder", Box::new(NewDirectory))
.action("New File", Box::new(NewFile)) .separator()
.action("New Folder", Box::new(NewDirectory)) .action("Cut", Box::new(Cut))
.separator() .action("Copy", Box::new(Copy))
.action("Cut", Box::new(Cut)) .if_some(self.clipboard_entry, |menu, entry| {
.action("Copy", Box::new(Copy)); menu.then_if(entry.worktree_id() == worktree_id, |menu| {
menu.action("Paste", Box::new(Paste))
if let Some(clipboard_entry) = self.clipboard_entry { })
if clipboard_entry.worktree_id() == worktree_id { })
menu = menu.action("Paste", Box::new(Paste)); .separator()
} .action("Copy Path", Box::new(CopyPath))
} .action("Copy Relative Path", Box::new(CopyRelativePath))
.separator()
menu = menu .action("Reveal in Finder", Box::new(RevealInFinder))
.separator() .then_if(is_dir, |menu| {
.action("Copy Path", Box::new(CopyPath)) menu
.action("Copy Relative Path", Box::new(CopyRelativePath)) .action("Open in Terminal", Box::new(OpenInTerminal))
.separator() .action("Search Inside", Box::new(NewSearchInDirectory))
.action("Reveal in Finder", Box::new(RevealInFinder)); })
.separator().action("Rename", Box::new(Rename))
if is_dir { .then_if(!is_root, |menu| {
menu = menu menu.action("Delete", Box::new(Delete))
.action("Open in Terminal", Box::new(OpenInTerminal)) })
.action("Search Inside", Box::new(NewSearchInDirectory)) },
} )
menu = menu.separator().action("Rename", Box::new(Rename));
if !is_root {
menu = menu.action("Delete", Box::new(Delete));
}
menu
}); });
cx.focus_view(&context_menu); cx.focus_view(&context_menu);

View File

@ -27,6 +27,7 @@ enum ContextMenuItem {
pub struct ContextMenu { pub struct ContextMenu {
items: Vec<ContextMenuItem>, items: Vec<ContextMenuItem>,
focus_handle: FocusHandle, focus_handle: FocusHandle,
action_context: Option<FocusHandle>,
selected_index: Option<usize>, selected_index: Option<usize>,
delayed: bool, delayed: bool,
clicked: bool, clicked: bool,
@ -56,6 +57,7 @@ impl ContextMenu {
Self { Self {
items: Default::default(), items: Default::default(),
focus_handle, focus_handle,
action_context: None,
selected_index: None, selected_index: None,
delayed: false, delayed: false,
clicked: false, clicked: false,
@ -66,6 +68,39 @@ impl ContextMenu {
}) })
} }
pub fn if_some<T>(self, condition: Option<T>, f: impl FnOnce(Self, T) -> Self) -> Self {
if let Some(t) = condition {
f(self, t)
} else {
self
}
}
pub fn then_if_else(self, condition: bool, then: impl FnOnce(Self) -> Self, otherwise: impl FnOnce(Self) -> Self) -> Self {
if condition {
then(self)
} else {
otherwise(self)
}
}
pub fn then_if(self, condition: bool, f: impl FnOnce(Self) -> Self) -> Self {
if condition {
f(self)
} else {
self
}
}
pub fn map(self, f: impl FnOnce(Self) -> Self) -> Self {
f(self)
}
pub fn context(mut self, focus: FocusHandle) -> Self {
self.action_context = Some(focus);
self
}
pub fn header(mut self, title: impl Into<SharedString>) -> Self { pub fn header(mut self, title: impl Into<SharedString>) -> Self {
self.items.push(ContextMenuItem::Header(title.into())); self.items.push(ContextMenuItem::Header(title.into()));
self self
@ -305,7 +340,14 @@ impl Render for ContextMenu {
.child(label_element) .child(label_element)
.debug_selector(|| format!("MENU_ITEM-{}", label)) .debug_selector(|| format!("MENU_ITEM-{}", label))
.children(action.as_ref().and_then(|action| { .children(action.as_ref().and_then(|action| {
KeyBinding::for_action(&**action, cx) self.action_context
.as_ref()
.map(|focus| {
KeyBinding::for_action_in(&**action, focus, cx)
})
.unwrap_or_else(|| {
KeyBinding::for_action(&**action, cx)
})
.map(|binding| div().ml_1().child(binding)) .map(|binding| div().ml_1().child(binding))
})), })),
) )