2022-12-14 23:59:50 +00:00
|
|
|
mod highlighted_workspace_location;
|
|
|
|
|
|
|
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
|
|
|
use gpui::{
|
|
|
|
actions,
|
2023-04-20 13:25:48 +00:00
|
|
|
anyhow::Result,
|
|
|
|
elements::{Flex, ParentElement},
|
2023-04-27 17:13:15 +00:00
|
|
|
AnyElement, AppContext, Element, Task, ViewContext, WeakViewHandle,
|
2022-12-14 23:59:50 +00:00
|
|
|
};
|
|
|
|
use highlighted_workspace_location::HighlightedWorkspaceLocation;
|
|
|
|
use ordered_float::OrderedFloat;
|
2023-04-20 13:25:48 +00:00
|
|
|
use picker::{Picker, PickerDelegate, PickerEvent};
|
2022-12-14 23:59:50 +00:00
|
|
|
use settings::Settings;
|
2023-04-27 17:13:15 +00:00
|
|
|
use std::sync::{Arc, Weak};
|
2023-02-07 08:10:11 +00:00
|
|
|
use workspace::{
|
2023-04-27 17:13:15 +00:00
|
|
|
notifications::simple_message_notification::MessageNotification, AppState, Workspace,
|
2023-02-07 08:10:11 +00:00
|
|
|
WorkspaceLocation, WORKSPACE_DB,
|
|
|
|
};
|
2022-12-14 23:59:50 +00:00
|
|
|
|
2023-01-23 21:40:34 +00:00
|
|
|
actions!(projects, [OpenRecent]);
|
2022-12-14 23:59:50 +00:00
|
|
|
|
2023-04-27 17:13:15 +00:00
|
|
|
pub fn init(cx: &mut AppContext, app_state: Weak<AppState>) {
|
|
|
|
cx.add_async_action(
|
|
|
|
move |_: &mut Workspace, _: &OpenRecent, cx: &mut ViewContext<Workspace>| {
|
|
|
|
toggle(app_state.clone(), cx)
|
|
|
|
},
|
|
|
|
);
|
2023-04-20 13:25:48 +00:00
|
|
|
RecentProjects::init(cx);
|
2022-12-14 23:59:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:13:15 +00:00
|
|
|
fn toggle(app_state: Weak<AppState>, cx: &mut ViewContext<Workspace>) -> Option<Task<Result<()>>> {
|
2023-04-20 13:25:48 +00:00
|
|
|
Some(cx.spawn(|workspace, mut cx| async move {
|
|
|
|
let workspace_locations: Vec<_> = cx
|
|
|
|
.background()
|
|
|
|
.spawn(async {
|
|
|
|
WORKSPACE_DB
|
|
|
|
.recent_workspaces_on_disk()
|
|
|
|
.await
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, location)| location)
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
workspace.update(&mut cx, |workspace, cx| {
|
|
|
|
if !workspace_locations.is_empty() {
|
|
|
|
workspace.toggle_modal(cx, |_, cx| {
|
2023-04-27 17:13:15 +00:00
|
|
|
let workspace = cx.weak_handle();
|
2023-04-20 13:25:48 +00:00
|
|
|
cx.add_view(|cx| {
|
2023-04-27 17:13:15 +00:00
|
|
|
RecentProjects::new(
|
|
|
|
RecentProjectsDelegate::new(
|
|
|
|
workspace,
|
|
|
|
workspace_locations,
|
|
|
|
app_state.clone(),
|
|
|
|
),
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
.with_max_size(800., 1200.)
|
2023-04-20 13:25:48 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
workspace.show_notification(0, cx, |cx| {
|
2023-05-01 12:09:39 +00:00
|
|
|
cx.add_view(|_| MessageNotification::new("No recent projects to open."))
|
2023-04-20 13:25:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
type RecentProjects = Picker<RecentProjectsDelegate>;
|
|
|
|
|
|
|
|
struct RecentProjectsDelegate {
|
2023-04-27 17:13:15 +00:00
|
|
|
workspace: WeakViewHandle<Workspace>,
|
2022-12-14 23:59:50 +00:00
|
|
|
workspace_locations: Vec<WorkspaceLocation>,
|
2023-04-27 17:13:15 +00:00
|
|
|
app_state: Weak<AppState>,
|
2022-12-14 23:59:50 +00:00
|
|
|
selected_match_index: usize,
|
|
|
|
matches: Vec<StringMatch>,
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
impl RecentProjectsDelegate {
|
2023-04-27 17:13:15 +00:00
|
|
|
fn new(
|
|
|
|
workspace: WeakViewHandle<Workspace>,
|
|
|
|
workspace_locations: Vec<WorkspaceLocation>,
|
|
|
|
app_state: Weak<AppState>,
|
|
|
|
) -> Self {
|
2022-12-14 23:59:50 +00:00
|
|
|
Self {
|
2023-04-27 17:13:15 +00:00
|
|
|
workspace,
|
2022-12-14 23:59:50 +00:00
|
|
|
workspace_locations,
|
2023-04-27 17:13:15 +00:00
|
|
|
app_state,
|
2022-12-14 23:59:50 +00:00
|
|
|
selected_match_index: 0,
|
|
|
|
matches: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
impl PickerDelegate for RecentProjectsDelegate {
|
|
|
|
fn placeholder_text(&self) -> Arc<str> {
|
|
|
|
"Recent Projects...".into()
|
2022-12-14 23:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn match_count(&self) -> usize {
|
|
|
|
self.matches.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selected_index(&self) -> usize {
|
|
|
|
self.selected_match_index
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<RecentProjects>) {
|
2022-12-14 23:59:50 +00:00
|
|
|
self.selected_match_index = ix;
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn update_matches(
|
|
|
|
&mut self,
|
|
|
|
query: String,
|
|
|
|
cx: &mut ViewContext<RecentProjects>,
|
|
|
|
) -> gpui::Task<()> {
|
2022-12-14 23:59:50 +00:00
|
|
|
let query = query.trim_start();
|
|
|
|
let smart_case = query.chars().any(|c| c.is_uppercase());
|
|
|
|
let candidates = self
|
|
|
|
.workspace_locations
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(id, location)| {
|
|
|
|
let combined_string = location
|
|
|
|
.paths()
|
|
|
|
.iter()
|
|
|
|
.map(|path| path.to_string_lossy().to_owned())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("");
|
|
|
|
StringMatchCandidate::new(id, combined_string)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.matches = smol::block_on(fuzzy::match_strings(
|
|
|
|
candidates.as_slice(),
|
|
|
|
query,
|
|
|
|
smart_case,
|
|
|
|
100,
|
|
|
|
&Default::default(),
|
|
|
|
cx.background().clone(),
|
|
|
|
));
|
|
|
|
self.matches.sort_unstable_by_key(|m| m.candidate_id);
|
|
|
|
|
|
|
|
self.selected_match_index = self
|
|
|
|
.matches
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2023-01-05 16:27:50 +00:00
|
|
|
.rev()
|
2022-12-14 23:59:50 +00:00
|
|
|
.max_by_key(|(_, m)| OrderedFloat(m.score))
|
|
|
|
.map(|(ix, _)| ix)
|
|
|
|
.unwrap_or(0);
|
|
|
|
Task::ready(())
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn confirm(&mut self, cx: &mut ViewContext<RecentProjects>) {
|
2023-04-27 17:13:15 +00:00
|
|
|
if let Some(((selected_match, workspace), app_state)) = self
|
|
|
|
.matches
|
|
|
|
.get(self.selected_index())
|
|
|
|
.zip(self.workspace.upgrade(cx))
|
|
|
|
.zip(self.app_state.upgrade())
|
|
|
|
{
|
2023-03-13 16:44:42 +00:00
|
|
|
let workspace_location = &self.workspace_locations[selected_match.candidate_id];
|
2023-04-27 17:13:15 +00:00
|
|
|
workspace
|
|
|
|
.update(cx, |workspace, cx| {
|
|
|
|
workspace.open_workspace_for_paths(
|
|
|
|
workspace_location.paths().as_ref().clone(),
|
|
|
|
app_state,
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.detach_and_log_err(cx);
|
2023-04-20 13:25:48 +00:00
|
|
|
cx.emit(PickerEvent::Dismiss);
|
2023-03-13 16:44:42 +00:00
|
|
|
}
|
2022-12-14 23:59:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn dismissed(&mut self, _cx: &mut ViewContext<RecentProjects>) {}
|
2022-12-14 23:59:50 +00:00
|
|
|
|
|
|
|
fn render_match(
|
|
|
|
&self,
|
|
|
|
ix: usize,
|
|
|
|
mouse_state: &mut gpui::MouseState,
|
|
|
|
selected: bool,
|
|
|
|
cx: &gpui::AppContext,
|
2023-04-21 19:04:03 +00:00
|
|
|
) -> AnyElement<Picker<Self>> {
|
2022-12-14 23:59:50 +00:00
|
|
|
let settings = cx.global::<Settings>();
|
|
|
|
let string_match = &self.matches[ix];
|
|
|
|
let style = settings.theme.picker.item.style_for(mouse_state, selected);
|
|
|
|
|
|
|
|
let highlighted_location = HighlightedWorkspaceLocation::new(
|
|
|
|
&string_match,
|
|
|
|
&self.workspace_locations[string_match.candidate_id],
|
|
|
|
);
|
|
|
|
|
|
|
|
Flex::column()
|
|
|
|
.with_child(highlighted_location.names.render(style.label.clone()))
|
|
|
|
.with_children(
|
|
|
|
highlighted_location
|
|
|
|
.paths
|
|
|
|
.into_iter()
|
|
|
|
.map(|highlighted_path| highlighted_path.render(style.label.clone())),
|
|
|
|
)
|
|
|
|
.flex(1., false)
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
2023-04-21 19:04:03 +00:00
|
|
|
.into_any_named("match")
|
2022-12-14 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|