This commit is contained in:
Antonio Scandurra 2022-05-26 11:17:10 +02:00
parent 580f1a4125
commit a5044ccbba
2 changed files with 56 additions and 39 deletions

View file

@ -13,9 +13,9 @@ pub enum ContextMenuItem {
} }
impl ContextMenuItem { impl ContextMenuItem {
pub fn item(label: String, action: impl 'static + Action) -> Self { pub fn item(label: impl ToString, action: impl 'static + Action) -> Self {
Self::Item { Self::Item {
label, label: label.to_string(),
action: Box::new(action), action: Box::new(action),
} }
} }

View file

@ -91,6 +91,10 @@ actions!(
CollapseSelectedEntry, CollapseSelectedEntry,
AddDirectory, AddDirectory,
AddFile, AddFile,
Copy,
CopyPath,
Cut,
Paste,
Delete, Delete,
Rename Rename
] ]
@ -207,27 +211,25 @@ impl ProjectPanel {
} }
fn deploy_context_menu(&mut self, action: &DeployContextMenu, cx: &mut ViewContext<Self>) { fn deploy_context_menu(&mut self, action: &DeployContextMenu, cx: &mut ViewContext<Self>) {
let mut menu_entries = Vec::new();
menu_entries.push(ContextMenuItem::item("New File", AddFile));
menu_entries.push(ContextMenuItem::item("New Directory", AddDirectory));
if let Some(entry_id) = action.entry_id { if let Some(entry_id) = action.entry_id {
if let Some(worktree_id) = self.project.read(cx).worktree_id_for_entry(entry_id, cx) { if let Some(worktree_id) = self.project.read(cx).worktree_id_for_entry(entry_id, cx) {
self.selection = Some(Selection { self.selection = Some(Selection {
worktree_id, worktree_id,
entry_id, entry_id,
}); });
menu_entries.push(ContextMenuItem::Separator);
menu_entries.push(ContextMenuItem::item("Copy", Copy));
menu_entries.push(ContextMenuItem::item("Copy Path", CopyPath));
menu_entries.push(ContextMenuItem::item("Cut", Cut));
menu_entries.push(ContextMenuItem::item("Rename", Rename));
menu_entries.push(ContextMenuItem::item("Delete", Delete));
} }
} }
self.context_menu.update(cx, |menu, cx| { self.context_menu.update(cx, |menu, cx| {
menu.show( menu.show(action.position, menu_entries, cx);
action.position,
[
ContextMenuItem::item("New File".to_string(), AddFile),
ContextMenuItem::item("New Directory".to_string(), AddDirectory),
ContextMenuItem::Separator,
ContextMenuItem::item("Rename".to_string(), Rename),
ContextMenuItem::item("Delete".to_string(), Delete),
],
cx,
);
}); });
cx.notify(); cx.notify();
@ -906,12 +908,14 @@ impl View for ProjectPanel {
} }
fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox { fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> gpui::ElementBox {
enum Tag {}
let theme = &cx.global::<Settings>().theme.project_panel; let theme = &cx.global::<Settings>().theme.project_panel;
let mut container_style = theme.container; let mut container_style = theme.container;
let padding = std::mem::take(&mut container_style.padding); let padding = std::mem::take(&mut container_style.padding);
let handle = self.handle.clone(); let handle = self.handle.clone();
Stack::new() Stack::new()
.with_child( .with_child(
MouseEventHandler::new::<Tag, _, _>(0, cx, |_, _| {
UniformList::new( UniformList::new(
self.list.clone(), self.list.clone(),
self.visible_entries self.visible_entries
@ -922,7 +926,10 @@ impl View for ProjectPanel {
let theme = cx.global::<Settings>().theme.clone(); let theme = cx.global::<Settings>().theme.clone();
let this = handle.upgrade(cx).unwrap(); let this = handle.upgrade(cx).unwrap();
this.update(cx.app, |this, cx| { this.update(cx.app, |this, cx| {
this.for_each_visible_entry(range.clone(), cx, |id, details, cx| { this.for_each_visible_entry(
range.clone(),
cx,
|id, details, cx| {
items.push(Self::render_entry( items.push(Self::render_entry(
id, id,
details, details,
@ -930,7 +937,8 @@ impl View for ProjectPanel {
&theme.project_panel, &theme.project_panel,
cx, cx,
)); ));
}); },
);
}) })
}, },
) )
@ -938,6 +946,15 @@ impl View for ProjectPanel {
.with_padding_bottom(padding.bottom) .with_padding_bottom(padding.bottom)
.contained() .contained()
.with_style(container_style) .with_style(container_style)
.expanded()
.boxed()
})
.on_right_mouse_down(move |position, cx| {
cx.dispatch_action(DeployContextMenu {
entry_id: None,
position,
})
})
.boxed(), .boxed(),
) )
.with_child(ChildView::new(&self.context_menu).boxed()) .with_child(ChildView::new(&self.context_menu).boxed())