Allow removing workspaces from the "recent projects" modal (#7885)

<img width="492" alt="Screenshot 2024-02-19 at 10 59 01 AM"
src="https://github.com/zed-industries/zed/assets/1823955/922117f6-81c1-409d-938a-131bcec0f24c">

<img width="675" alt="Screenshot 2024-02-19 at 10 59 27 AM"
src="https://github.com/zed-industries/zed/assets/1823955/fefac68b-9a99-43bb-ac0c-724e7c622455">

Release Notes:

- Added a way to remove entries from the recent projects modal
([7426](https://github.com/zed-industries/zed/issues/7426)).
This commit is contained in:
Tung Hoang 2024-02-21 15:30:02 -08:00 committed by GitHub
parent 266bb62813
commit f930969411
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 98 additions and 40 deletions

View File

@ -1,7 +1,7 @@
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
div, list, prelude::*, uniform_list, AnyElement, AppContext, DismissEvent, EventEmitter, div, list, prelude::*, uniform_list, AnyElement, AppContext, ClickEvent, DismissEvent,
FocusHandle, FocusableView, Length, ListState, MouseButton, MouseDownEvent, Render, Task, EventEmitter, FocusHandle, FocusableView, Length, ListState, Render, Task,
UniformListScrollHandle, View, ViewContext, WindowContext, UniformListScrollHandle, View, ViewContext, WindowContext,
}; };
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
@ -103,7 +103,7 @@ impl<D: PickerDelegate> Picker<D> {
let mut this = Self { let mut this = Self {
delegate, delegate,
editor, editor,
element_container: Self::crate_element_container(is_uniform, cx), element_container: Self::create_element_container(is_uniform, cx),
pending_update_matches: None, pending_update_matches: None,
confirm_on_update: None, confirm_on_update: None,
width: None, width: None,
@ -117,7 +117,7 @@ impl<D: PickerDelegate> Picker<D> {
this this
} }
fn crate_element_container(is_uniform: bool, cx: &mut ViewContext<Self>) -> ElementContainer { fn create_element_container(is_uniform: bool, cx: &mut ViewContext<Self>) -> ElementContainer {
if is_uniform { if is_uniform {
ElementContainer::UniformList(UniformListScrollHandle::new()) ElementContainer::UniformList(UniformListScrollHandle::new())
} else { } else {
@ -311,12 +311,10 @@ impl<D: PickerDelegate> Picker<D> {
fn render_element(&self, cx: &mut ViewContext<Self>, ix: usize) -> impl IntoElement { fn render_element(&self, cx: &mut ViewContext<Self>, ix: usize) -> impl IntoElement {
div() div()
.on_mouse_down( .id(("item", ix))
MouseButton::Left, .on_click(cx.listener(move |this, event: &ClickEvent, cx| {
cx.listener(move |this, event: &MouseDownEvent, cx| { this.handle_click(ix, event.down.modifiers.command, cx)
this.handle_click(ix, event.modifiers.command, cx) }))
}),
)
.children( .children(
self.delegate self.delegate
.render_match(ix, ix == self.delegate.selected_index(), cx), .render_match(ix, ix == self.delegate.selected_index(), cx),

View File

@ -3,16 +3,16 @@ mod projects;
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Result, Subscription, Task, AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Result,
View, ViewContext, WeakView, Subscription, Task, View, ViewContext, WeakView,
}; };
use highlighted_workspace_location::HighlightedWorkspaceLocation; use highlighted_workspace_location::HighlightedWorkspaceLocation;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use std::sync::Arc; use std::sync::Arc;
use ui::{prelude::*, tooltip_container, HighlightedLabel, ListItem, ListItemSpacing}; use ui::{prelude::*, tooltip_container, HighlightedLabel, ListItem, ListItemSpacing, Tooltip};
use util::paths::PathExt; use util::paths::PathExt;
use workspace::{ModalView, Workspace, WorkspaceLocation, WORKSPACE_DB}; use workspace::{ModalView, Workspace, WorkspaceId, WorkspaceLocation, WORKSPACE_DB};
pub use projects::OpenRecent; pub use projects::OpenRecent;
@ -45,13 +45,11 @@ impl RecentProjects {
let workspaces = WORKSPACE_DB let workspaces = WORKSPACE_DB
.recent_workspaces_on_disk() .recent_workspaces_on_disk()
.await .await
.unwrap_or_default() .unwrap_or_default();
.into_iter()
.map(|(_, location)| location)
.collect();
this.update(&mut cx, move |this, cx| { this.update(&mut cx, move |this, cx| {
this.picker.update(cx, move |picker, cx| { this.picker.update(cx, move |picker, cx| {
picker.delegate.workspace_locations = workspaces; picker.delegate.workspaces = workspaces;
picker.update_matches(picker.query(cx), cx) picker.update_matches(picker.query(cx), cx)
}) })
}) })
@ -124,20 +122,23 @@ impl Render for RecentProjects {
pub struct RecentProjectsDelegate { pub struct RecentProjectsDelegate {
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
workspace_locations: Vec<WorkspaceLocation>, workspaces: Vec<(WorkspaceId, WorkspaceLocation)>,
selected_match_index: usize, selected_match_index: usize,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
render_paths: bool, render_paths: bool,
// Flag to reset index when there is a new query vs not reset index when user delete an item
reset_selected_match_index: bool,
} }
impl RecentProjectsDelegate { impl RecentProjectsDelegate {
fn new(workspace: WeakView<Workspace>, render_paths: bool) -> Self { fn new(workspace: WeakView<Workspace>, render_paths: bool) -> Self {
Self { Self {
workspace, workspace,
workspace_locations: vec![], workspaces: vec![],
selected_match_index: 0, selected_match_index: 0,
matches: Default::default(), matches: Default::default(),
render_paths, render_paths,
reset_selected_match_index: true,
} }
} }
} }
@ -169,10 +170,10 @@ impl PickerDelegate for RecentProjectsDelegate {
let query = query.trim_start(); let query = query.trim_start();
let smart_case = query.chars().any(|c| c.is_uppercase()); let smart_case = query.chars().any(|c| c.is_uppercase());
let candidates = self let candidates = self
.workspace_locations .workspaces
.iter() .iter()
.enumerate() .enumerate()
.map(|(id, location)| { .map(|(id, (_, location))| {
let combined_string = location let combined_string = location
.paths() .paths()
.iter() .iter()
@ -192,14 +193,17 @@ impl PickerDelegate for RecentProjectsDelegate {
)); ));
self.matches.sort_unstable_by_key(|m| m.candidate_id); self.matches.sort_unstable_by_key(|m| m.candidate_id);
self.selected_match_index = self if self.reset_selected_match_index {
.matches self.selected_match_index = self
.iter() .matches
.enumerate() .iter()
.rev() .enumerate()
.max_by_key(|(_, m)| OrderedFloat(m.score)) .rev()
.map(|(ix, _)| ix) .max_by_key(|(_, m)| OrderedFloat(m.score))
.unwrap_or(0); .map(|(ix, _)| ix)
.unwrap_or(0);
}
self.reset_selected_match_index = true;
Task::ready(()) Task::ready(())
} }
@ -209,7 +213,7 @@ impl PickerDelegate for RecentProjectsDelegate {
.get(self.selected_index()) .get(self.selected_index())
.zip(self.workspace.upgrade()) .zip(self.workspace.upgrade())
{ {
let workspace_location = &self.workspace_locations[selected_match.candidate_id]; let (_, workspace_location) = &self.workspaces[selected_match.candidate_id];
workspace workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
workspace workspace
@ -226,19 +230,18 @@ impl PickerDelegate for RecentProjectsDelegate {
&self, &self,
ix: usize, ix: usize,
selected: bool, selected: bool,
_cx: &mut ViewContext<Picker<Self>>, cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> { ) -> Option<Self::ListItem> {
let Some(r#match) = self.matches.get(ix) else { let Some(r#match) = self.matches.get(ix) else {
return None; return None;
}; };
let highlighted_location = HighlightedWorkspaceLocation::new( let (workspace_id, location) = &self.workspaces[r#match.candidate_id];
&r#match, let highlighted_location: HighlightedWorkspaceLocation =
&self.workspace_locations[r#match.candidate_id], HighlightedWorkspaceLocation::new(&r#match, location);
);
let tooltip_highlighted_location = highlighted_location.clone(); let tooltip_highlighted_location = highlighted_location.clone();
let is_current_workspace = self.is_current_workspace(*workspace_id, cx);
Some( Some(
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
@ -255,6 +258,27 @@ impl PickerDelegate for RecentProjectsDelegate {
})) }))
}), }),
) )
.when(!is_current_workspace, |el| {
let delete_button = div()
.child(
IconButton::new("delete", IconName::Close)
.icon_size(IconSize::Small)
.on_click(cx.listener(move |this, _event, cx| {
cx.stop_propagation();
cx.prevent_default();
this.delegate.delete_recent_project(ix, cx)
}))
.tooltip(|cx| Tooltip::text("Delete From Recent Projects...", cx)),
)
.into_any_element();
if self.selected_index() == ix {
el.end_slot::<AnyElement>(delete_button)
} else {
el.end_hover_slot::<AnyElement>(delete_button)
}
})
.tooltip(move |cx| { .tooltip(move |cx| {
let tooltip_highlighted_location = tooltip_highlighted_location.clone(); let tooltip_highlighted_location = tooltip_highlighted_location.clone();
cx.new_view(move |_| MatchTooltip { cx.new_view(move |_| MatchTooltip {
@ -266,6 +290,42 @@ impl PickerDelegate for RecentProjectsDelegate {
} }
} }
impl RecentProjectsDelegate {
fn delete_recent_project(&self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
if let Some(selected_match) = self.matches.get(ix) {
let (workspace_id, _) = self.workspaces[selected_match.candidate_id];
cx.spawn(move |this, mut cx| async move {
let _ = WORKSPACE_DB.delete_workspace_by_id(workspace_id).await;
let workspaces = WORKSPACE_DB
.recent_workspaces_on_disk()
.await
.unwrap_or_default();
this.update(&mut cx, move |picker, cx| {
picker.delegate.workspaces = workspaces;
picker.delegate.set_selected_index(ix - 1, cx);
picker.delegate.reset_selected_match_index = false;
picker.update_matches(picker.query(cx), cx)
})
})
.detach();
}
}
fn is_current_workspace(
&self,
workspace_id: WorkspaceId,
cx: &mut ViewContext<Picker<Self>>,
) -> bool {
if let Some(workspace) = self.workspace.upgrade() {
let workspace = workspace.read(cx);
if workspace_id == workspace.database_id() {
return true;
}
}
false
}
}
struct MatchTooltip { struct MatchTooltip {
highlighted_location: HighlightedWorkspaceLocation, highlighted_location: HighlightedWorkspaceLocation,
} }

View File

@ -430,7 +430,7 @@ impl WorkspaceDb {
} }
query! { query! {
async fn delete_stale_workspace(id: WorkspaceId) -> Result<()> { pub async fn delete_workspace_by_id(id: WorkspaceId) -> Result<()> {
DELETE FROM workspaces DELETE FROM workspaces
WHERE workspace_id IS ? WHERE workspace_id IS ?
} }
@ -447,7 +447,7 @@ impl WorkspaceDb {
{ {
result.push((id, location)); result.push((id, location));
} else { } else {
delete_tasks.push(self.delete_stale_workspace(id)); delete_tasks.push(self.delete_workspace_by_id(id));
} }
} }