Remove internal actions from project_panel

This commit is contained in:
Antonio Scandurra 2023-04-28 17:21:10 +02:00
parent e1535735b8
commit 33da9e5690

View file

@ -10,7 +10,6 @@ use gpui::{
ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState, ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState,
}, },
geometry::vector::Vector2F, geometry::vector::Vector2F,
impl_internal_actions,
keymap_matcher::KeymapContext, keymap_matcher::KeymapContext,
platform::{CursorStyle, MouseButton, PromptLevel}, platform::{CursorStyle, MouseButton, PromptLevel},
AnyElement, AppContext, ClipboardItem, Element, Entity, ModelHandle, Task, View, ViewContext, AnyElement, AppContext, ClipboardItem, Element, Entity, ModelHandle, Task, View, ViewContext,
@ -88,28 +87,6 @@ pub struct EntryDetails {
is_cut: bool, is_cut: bool,
} }
#[derive(Clone, PartialEq)]
pub struct ToggleExpanded(pub ProjectEntryId);
#[derive(Clone, PartialEq)]
pub struct Open {
pub entry_id: ProjectEntryId,
pub change_focus: bool,
}
#[derive(Clone, PartialEq)]
pub struct MoveProjectEntry {
pub entry_to_move: ProjectEntryId,
pub destination: ProjectEntryId,
pub destination_is_file: bool,
}
#[derive(Clone, PartialEq)]
pub struct DeployContextMenu {
pub position: Vector2F,
pub entry_id: ProjectEntryId,
}
actions!( actions!(
project_panel, project_panel,
[ [
@ -128,19 +105,12 @@ actions!(
ToggleFocus ToggleFocus
] ]
); );
impl_internal_actions!(
project_panel,
[Open, ToggleExpanded, DeployContextMenu, MoveProjectEntry]
);
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.add_action(ProjectPanel::deploy_context_menu);
cx.add_action(ProjectPanel::expand_selected_entry); cx.add_action(ProjectPanel::expand_selected_entry);
cx.add_action(ProjectPanel::collapse_selected_entry); cx.add_action(ProjectPanel::collapse_selected_entry);
cx.add_action(ProjectPanel::toggle_expanded);
cx.add_action(ProjectPanel::select_prev); cx.add_action(ProjectPanel::select_prev);
cx.add_action(ProjectPanel::select_next); cx.add_action(ProjectPanel::select_next);
cx.add_action(ProjectPanel::open_entry);
cx.add_action(ProjectPanel::new_file); cx.add_action(ProjectPanel::new_file);
cx.add_action(ProjectPanel::new_directory); cx.add_action(ProjectPanel::new_directory);
cx.add_action(ProjectPanel::rename); cx.add_action(ProjectPanel::rename);
@ -157,7 +127,6 @@ pub fn init(cx: &mut AppContext) {
this.paste(action, cx); this.paste(action, cx);
}, },
); );
cx.add_action(ProjectPanel::move_entry);
} }
pub enum Event { pub enum Event {
@ -277,10 +246,14 @@ impl ProjectPanel {
project_panel project_panel
} }
fn deploy_context_menu(&mut self, action: &DeployContextMenu, cx: &mut ViewContext<Self>) { fn deploy_context_menu(
&mut self,
position: Vector2F,
entry_id: ProjectEntryId,
cx: &mut ViewContext<Self>,
) {
let project = self.project.read(cx); let project = self.project.read(cx);
let entry_id = action.entry_id;
let worktree_id = if let Some(id) = project.worktree_id_for_entry(entry_id, cx) { let worktree_id = if let Some(id) = project.worktree_id_for_entry(entry_id, cx) {
id id
} else { } else {
@ -332,7 +305,7 @@ impl ProjectPanel {
} }
self.context_menu.update(cx, |menu, cx| { self.context_menu.update(cx, |menu, cx| {
menu.show(action.position, AnchorCorner::TopLeft, menu_entries, cx); menu.show(position, AnchorCorner::TopLeft, menu_entries, cx);
}); });
cx.notify(); cx.notify();
@ -391,8 +364,7 @@ impl ProjectPanel {
} }
} }
fn toggle_expanded(&mut self, action: &ToggleExpanded, cx: &mut ViewContext<Self>) { fn toggle_expanded(&mut self, entry_id: ProjectEntryId, cx: &mut ViewContext<Self>) {
let entry_id = action.0;
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) {
if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) { if let Some(expanded_dir_ids) = self.expanded_dir_ids.get_mut(&worktree_id) {
match expanded_dir_ids.binary_search(&entry_id) { match expanded_dir_ids.binary_search(&entry_id) {
@ -440,13 +412,7 @@ impl ProjectPanel {
Some(task) Some(task)
} else if let Some((_, entry)) = self.selected_entry(cx) { } else if let Some((_, entry)) = self.selected_entry(cx) {
if entry.is_file() { if entry.is_file() {
self.open_entry( self.open_entry(entry.id, true, cx);
&Open {
entry_id: entry.id,
change_focus: true,
},
cx,
);
} }
None None
} else { } else {
@ -510,13 +476,7 @@ impl ProjectPanel {
} }
this.update_visible_entries(None, cx); this.update_visible_entries(None, cx);
if is_new_entry && !is_dir { if is_new_entry && !is_dir {
this.open_entry( this.open_entry(new_entry.id, true, cx);
&Open {
entry_id: new_entry.id,
change_focus: true,
},
cx,
);
} }
cx.notify(); cx.notify();
})?; })?;
@ -531,10 +491,15 @@ impl ProjectPanel {
cx.notify(); cx.notify();
} }
fn open_entry(&mut self, action: &Open, cx: &mut ViewContext<Self>) { fn open_entry(
&mut self,
entry_id: ProjectEntryId,
focus_opened_item: bool,
cx: &mut ViewContext<Self>,
) {
cx.emit(Event::OpenedEntry { cx.emit(Event::OpenedEntry {
entry_id: action.entry_id, entry_id,
focus_opened_item: action.change_focus, focus_opened_item,
}); });
} }
@ -816,11 +781,9 @@ impl ProjectPanel {
fn move_entry( fn move_entry(
&mut self, &mut self,
&MoveProjectEntry { entry_to_move: ProjectEntryId,
entry_to_move, destination: ProjectEntryId,
destination, destination_is_file: bool,
destination_is_file,
}: &MoveProjectEntry,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
let destination_worktree = self.project.update(cx, |project, cx| { let destination_worktree = self.project.update(cx, |project, cx| {
@ -1196,34 +1159,29 @@ impl ProjectPanel {
cx, cx,
) )
}) })
.on_click(MouseButton::Left, move |e, _, cx| { .on_click(MouseButton::Left, move |event, this, cx| {
if !show_editor { if !show_editor {
if kind == EntryKind::Dir { if kind == EntryKind::Dir {
cx.dispatch_action(ToggleExpanded(entry_id)) this.toggle_expanded(entry_id, cx);
} else { } else {
cx.dispatch_action(Open { this.open_entry(entry_id, event.click_count > 1, cx);
entry_id,
change_focus: e.click_count > 1,
})
} }
} }
}) })
.on_down(MouseButton::Right, move |e, _, cx| { .on_down(MouseButton::Right, move |event, this, cx| {
cx.dispatch_action(DeployContextMenu { this.deploy_context_menu(event.position, entry_id, cx);
entry_id,
position: e.position,
})
}) })
.on_up(MouseButton::Left, move |_, _, cx| { .on_up(MouseButton::Left, move |_, this, cx| {
if let Some((_, dragged_entry)) = cx if let Some((_, dragged_entry)) = cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window_id())
{ {
cx.dispatch_action(MoveProjectEntry { this.move_entry(
entry_to_move: *dragged_entry, *dragged_entry,
destination: entry_id, entry_id,
destination_is_file: matches!(details.kind, EntryKind::File(_)), matches!(details.kind, EntryKind::File(_)),
}); cx,
);
} }
}) })
.on_move(move |_, this, cx| { .on_move(move |_, this, cx| {
@ -1307,14 +1265,11 @@ impl View for ProjectPanel {
.with_style(container_style) .with_style(container_style)
.expanded() .expanded()
}) })
.on_down(MouseButton::Right, move |e, _, cx| { .on_down(MouseButton::Right, move |event, this, cx| {
// When deploying the context menu anywhere below the last project entry, // When deploying the context menu anywhere below the last project entry,
// act as if the user clicked the root of the last worktree. // act as if the user clicked the root of the last worktree.
if let Some(entry_id) = last_worktree_root_id { if let Some(entry_id) = last_worktree_root_id {
cx.dispatch_action(DeployContextMenu { this.deploy_context_menu(event.position, entry_id, cx);
entry_id,
position: e.position,
})
} }
}), }),
) )
@ -1895,7 +1850,7 @@ mod tests {
let worktree = worktree.read(cx); let worktree = worktree.read(cx);
if let Ok(relative_path) = path.strip_prefix(worktree.root_name()) { if let Ok(relative_path) = path.strip_prefix(worktree.root_name()) {
let entry_id = worktree.entry_for_path(relative_path).unwrap().id; let entry_id = worktree.entry_for_path(relative_path).unwrap().id;
panel.toggle_expanded(&ToggleExpanded(entry_id), cx); panel.toggle_expanded(entry_id, cx);
return; return;
} }
} }