remote projects per user (#10594)

Release Notes:

- Made remote projects per-user instead of per-channel. If you'd like to
be part of the remote development alpha, please email hi@zed.dev.

---------

Co-authored-by: Bennet Bo Fenner <53836821+bennetbo@users.noreply.github.com>
Co-authored-by: Bennet <bennetbo@gmx.de>
Co-authored-by: Nate Butler <1714999+iamnbutler@users.noreply.github.com>
Co-authored-by: Nate Butler <iamnbutler@gmail.com>
This commit is contained in:
Conrad Irwin 2024-04-23 15:33:09 -06:00 committed by GitHub
parent 8ae4c3277f
commit e0c83a1d32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 2807 additions and 1625 deletions

View file

@ -13,14 +13,21 @@ path = "src/recent_projects.rs"
doctest = false
[dependencies]
anyhow.workspace = true
feature_flags.workspace = true
fuzzy.workspace = true
gpui.workspace = true
menu.workspace = true
ordered-float.workspace = true
picker.workspace = true
remote_projects.workspace = true
rpc.workspace = true
serde.workspace = true
settings.workspace = true
smol.workspace = true
theme.workspace = true
ui.workspace = true
ui_text_field.workspace = true
util.workspace = true
workspace.workspace = true

View file

@ -1,6 +1,9 @@
mod remote_projects;
use feature_flags::FeatureFlagAppExt;
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Result,
Action, AnyElement, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView,
Subscription, Task, View, ViewContext, WeakView,
};
use ordered_float::OrderedFloat;
@ -8,11 +11,21 @@ use picker::{
highlighted_match_with_paths::{HighlightedMatchWithPaths, HighlightedText},
Picker, PickerDelegate,
};
use remote_projects::RemoteProjects;
use rpc::proto::DevServerStatus;
use serde::Deserialize;
use std::{path::Path, sync::Arc};
use ui::{prelude::*, tooltip_container, ListItem, ListItemSpacing, Tooltip};
use util::paths::PathExt;
use workspace::{ModalView, Workspace, WorkspaceId, WorkspaceLocation, WORKSPACE_DB};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use ui::{
prelude::*, tooltip_container, ButtonLike, IconWithIndicator, Indicator, KeyBinding, ListItem,
ListItemSpacing, Tooltip,
};
use util::{paths::PathExt, ResultExt};
use workspace::{
AppState, ModalView, SerializedWorkspaceLocation, Workspace, WorkspaceId, WORKSPACE_DB,
};
#[derive(PartialEq, Clone, Deserialize, Default)]
pub struct OpenRecent {
@ -25,9 +38,12 @@ fn default_create_new_window() -> bool {
}
gpui::impl_actions!(projects, [OpenRecent]);
gpui::actions!(projects, [OpenRemote]);
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(RecentProjects::register).detach();
cx.observe_new_views(remote_projects::RemoteProjects::register)
.detach();
}
pub struct RecentProjects {
@ -55,10 +71,11 @@ impl RecentProjects {
let workspaces = WORKSPACE_DB
.recent_workspaces_on_disk()
.await
.log_err()
.unwrap_or_default();
this.update(&mut cx, move |this, cx| {
this.picker.update(cx, move |picker, cx| {
picker.delegate.workspaces = workspaces;
picker.delegate.set_workspaces(workspaces);
picker.update_matches(picker.query(cx), cx)
})
})
@ -75,9 +92,7 @@ impl RecentProjects {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, open_recent: &OpenRecent, cx| {
let Some(recent_projects) = workspace.active_modal::<Self>(cx) else {
if let Some(handler) = Self::open(workspace, open_recent.create_new_window, cx) {
handler.detach_and_log_err(cx);
}
Self::open(workspace, open_recent.create_new_window, cx);
return;
};
@ -89,24 +104,17 @@ impl RecentProjects {
});
}
fn open(
_: &mut Workspace,
pub fn open(
workspace: &mut Workspace,
create_new_window: bool,
cx: &mut ViewContext<Workspace>,
) -> Option<Task<Result<()>>> {
Some(cx.spawn(|workspace, mut cx| async move {
workspace.update(&mut cx, |workspace, cx| {
let weak_workspace = cx.view().downgrade();
workspace.toggle_modal(cx, |cx| {
let delegate =
RecentProjectsDelegate::new(weak_workspace, create_new_window, true);
let modal = Self::new(delegate, 34., cx);
modal
});
})?;
Ok(())
}))
) {
let weak = cx.view().downgrade();
workspace.toggle_modal(cx, |cx| {
let delegate = RecentProjectsDelegate::new(weak, create_new_window, true);
let modal = Self::new(delegate, 34., cx);
modal
})
}
pub fn open_popover(workspace: WeakView<Workspace>, cx: &mut WindowContext<'_>) -> View<Self> {
@ -143,13 +151,14 @@ impl Render for RecentProjects {
pub struct RecentProjectsDelegate {
workspace: WeakView<Workspace>,
workspaces: Vec<(WorkspaceId, WorkspaceLocation)>,
workspaces: Vec<(WorkspaceId, SerializedWorkspaceLocation)>,
selected_match_index: usize,
matches: Vec<StringMatch>,
render_paths: bool,
create_new_window: 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,
has_any_remote_projects: bool,
}
impl RecentProjectsDelegate {
@ -162,8 +171,17 @@ impl RecentProjectsDelegate {
create_new_window,
render_paths,
reset_selected_match_index: true,
has_any_remote_projects: false,
}
}
pub fn set_workspaces(&mut self, workspaces: Vec<(WorkspaceId, SerializedWorkspaceLocation)>) {
self.workspaces = workspaces;
self.has_any_remote_projects = self
.workspaces
.iter()
.any(|(_, location)| matches!(location, SerializedWorkspaceLocation::Remote(_)));
}
}
impl EventEmitter<DismissEvent> for RecentProjectsDelegate {}
impl PickerDelegate for RecentProjectsDelegate {
@ -210,12 +228,18 @@ impl PickerDelegate for RecentProjectsDelegate {
.iter()
.enumerate()
.map(|(id, (_, location))| {
let combined_string = location
.paths()
.iter()
.map(|path| path.compact().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("");
let combined_string = match location {
SerializedWorkspaceLocation::Local(paths) => paths
.paths()
.iter()
.map(|path| path.compact().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join(""),
SerializedWorkspaceLocation::Remote(remote_project) => {
format!("{}{}", remote_project.dev_server_name, remote_project.path)
}
};
StringMatchCandidate::new(id, combined_string)
})
.collect::<Vec<_>>();
@ -261,30 +285,69 @@ impl PickerDelegate for RecentProjectsDelegate {
if workspace.database_id() == *candidate_workspace_id {
Task::ready(Ok(()))
} else {
let candidate_paths = candidate_workspace_location.paths().as_ref().clone();
if replace_current_window {
cx.spawn(move |workspace, mut cx| async move {
let continue_replacing = workspace
.update(&mut cx, |workspace, cx| {
workspace.prepare_to_close(true, cx)
})?
.await?;
if continue_replacing {
workspace
.update(&mut cx, |workspace, cx| {
workspace.open_workspace_for_paths(
true,
candidate_paths,
cx,
)
})?
.await
match candidate_workspace_location {
SerializedWorkspaceLocation::Local(paths) => {
let paths = paths.paths().as_ref().clone();
if replace_current_window {
cx.spawn(move |workspace, mut cx| async move {
let continue_replacing = workspace
.update(&mut cx, |workspace, cx| {
workspace.prepare_to_close(true, cx)
})?
.await?;
if continue_replacing {
workspace
.update(&mut cx, |workspace, cx| {
workspace
.open_workspace_for_paths(true, paths, cx)
})?
.await
} else {
Ok(())
}
})
} else {
Ok(())
workspace.open_workspace_for_paths(false, paths, cx)
}
})
} else {
workspace.open_workspace_for_paths(false, candidate_paths, cx)
}
//TODO support opening remote projects in the same window
SerializedWorkspaceLocation::Remote(remote_project) => {
let store = ::remote_projects::Store::global(cx).read(cx);
let Some(project_id) = store
.remote_project(remote_project.id)
.and_then(|p| p.project_id)
else {
let dev_server_name = remote_project.dev_server_name.clone();
return cx.spawn(|workspace, mut cx| async move {
let response =
cx.prompt(gpui::PromptLevel::Warning,
"Dev Server is offline",
Some(format!("Cannot connect to {}. To debug open the remote project settings.", dev_server_name).as_str()),
&["Ok", "Open Settings"]
).await?;
if response == 1 {
workspace.update(&mut cx, |workspace, cx| {
workspace.toggle_modal(cx, |cx| RemoteProjects::new(cx))
})?;
} else {
workspace.update(&mut cx, |workspace, cx| {
RecentProjects::open(workspace, true, cx);
})?;
}
Ok(())
})
};
if let Some(app_state) = AppState::global(cx).upgrade() {
let task =
workspace::join_remote_project(project_id, app_state, cx);
cx.spawn(|_, _| async move {
task.await?;
Ok(())
})
} else {
Task::ready(Err(anyhow::anyhow!("App state not found")))
}
}
}
}
})
@ -295,6 +358,14 @@ impl PickerDelegate for RecentProjectsDelegate {
fn dismissed(&mut self, _: &mut ViewContext<Picker<Self>>) {}
fn no_matches_text(&self, _cx: &mut WindowContext) -> SharedString {
if self.workspaces.is_empty() {
"Recently opened projects will show up here".into()
} else {
"No matches".into()
}
}
fn render_match(
&self,
ix: usize,
@ -308,9 +379,30 @@ impl PickerDelegate for RecentProjectsDelegate {
let (workspace_id, location) = &self.workspaces[hit.candidate_id];
let is_current_workspace = self.is_current_workspace(*workspace_id, cx);
let is_remote = matches!(location, SerializedWorkspaceLocation::Remote(_));
let dev_server_status =
if let SerializedWorkspaceLocation::Remote(remote_project) = location {
let store = ::remote_projects::Store::global(cx).read(cx);
Some(
store
.remote_project(remote_project.id)
.and_then(|p| store.dev_server(p.dev_server_id))
.map(|s| s.status)
.unwrap_or_default(),
)
} else {
None
};
let mut path_start_offset = 0;
let (match_labels, paths): (Vec<_>, Vec<_>) = location
.paths()
let paths = match location {
SerializedWorkspaceLocation::Local(paths) => paths.paths(),
SerializedWorkspaceLocation::Remote(remote_project) => Arc::new(vec![PathBuf::from(
format!("{}:{}", remote_project.dev_server_name, remote_project.path),
)]),
};
let (match_labels, paths): (Vec<_>, Vec<_>) = paths
.iter()
.map(|path| {
let path = path.compact();
@ -323,22 +415,58 @@ impl PickerDelegate for RecentProjectsDelegate {
.unzip();
let highlighted_match = HighlightedMatchWithPaths {
match_label: HighlightedText::join(match_labels.into_iter().flatten(), ", "),
match_label: HighlightedText::join(match_labels.into_iter().flatten(), ", ").color(
if matches!(dev_server_status, Some(DevServerStatus::Offline)) {
Color::Disabled
} else {
Color::Default
},
),
paths,
};
Some(
ListItem::new(ix)
.selected(selected)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.selected(selected)
.child({
let mut highlighted = highlighted_match.clone();
if !self.render_paths {
highlighted.paths.clear();
}
highlighted.render(cx)
})
.child(
h_flex()
.flex_grow()
.gap_3()
.when(self.has_any_remote_projects, |this| {
this.child(if is_remote {
// if disabled, Color::Disabled
let indicator_color = match dev_server_status {
Some(DevServerStatus::Online) => Color::Created,
Some(DevServerStatus::Offline) => Color::Hidden,
_ => unreachable!(),
};
IconWithIndicator::new(
Icon::new(IconName::Server).color(Color::Muted),
Some(Indicator::dot()),
)
.indicator_color(indicator_color)
.indicator_border_color(if selected {
Some(cx.theme().colors().element_selected)
} else {
None
})
.into_any_element()
} else {
Icon::new(IconName::Screen)
.color(Color::Muted)
.into_any_element()
})
})
.child({
let mut highlighted = highlighted_match.clone();
if !self.render_paths {
highlighted.paths.clear();
}
highlighted.render(cx)
}),
)
.when(!is_current_workspace, |el| {
let delete_button = div()
.child(
@ -369,6 +497,39 @@ impl PickerDelegate for RecentProjectsDelegate {
}),
)
}
fn render_footer(&self, cx: &mut ViewContext<Picker<Self>>) -> Option<AnyElement> {
if !cx.has_flag::<feature_flags::Remoting>() {
return None;
}
Some(
h_flex()
.border_t_1()
.py_2()
.pr_2()
.border_color(cx.theme().colors().border)
.justify_end()
.gap_4()
.child(
ButtonLike::new("remote")
.when_some(KeyBinding::for_action(&OpenRemote, cx), |button, key| {
button.child(key)
})
.child(Label::new("Connect remote…").color(Color::Muted))
.on_click(|_, cx| cx.dispatch_action(OpenRemote.boxed_clone())),
)
.child(
ButtonLike::new("local")
.when_some(
KeyBinding::for_action(&workspace::Open, cx),
|button, key| button.child(key),
)
.child(Label::new("Open folder…").color(Color::Muted))
.on_click(|_, cx| cx.dispatch_action(workspace::Open.boxed_clone())),
)
.into_any(),
)
}
}
// Compute the highlighted text for the name and path
@ -406,6 +567,7 @@ fn highlights_for_path(
text: text.to_string(),
highlight_positions,
char_count,
color: Color::Default,
}
});
@ -415,6 +577,7 @@ fn highlights_for_path(
text: path_string.to_string(),
highlight_positions: path_positions,
char_count: path_char_count,
color: Color::Default,
},
)
}
@ -430,7 +593,7 @@ impl RecentProjectsDelegate {
.await
.unwrap_or_default();
this.update(&mut cx, move |picker, cx| {
picker.delegate.workspaces = workspaces;
picker.delegate.set_workspaces(workspaces);
picker.delegate.set_selected_index(ix - 1, cx);
picker.delegate.reset_selected_match_index = false;
picker.update_matches(picker.query(cx), cx)
@ -475,7 +638,7 @@ mod tests {
use gpui::{TestAppContext, WindowHandle};
use project::Project;
use serde_json::json;
use workspace::{open_paths, AppState};
use workspace::{open_paths, AppState, LocalPaths};
use super::*;
@ -539,10 +702,10 @@ mod tests {
positions: Vec::new(),
string: "fake candidate".to_string(),
}];
delegate.workspaces = vec![(
delegate.set_workspaces(vec![(
WorkspaceId::default(),
WorkspaceLocation::new(vec!["/test/path/"]),
)];
LocalPaths::new(vec!["/test/path/"]).into(),
)]);
});
})
.unwrap();

View file

@ -0,0 +1,749 @@
use std::time::Duration;
use feature_flags::FeatureFlagViewExt;
use gpui::{
percentage, Action, Animation, AnimationExt, AppContext, ClipboardItem, DismissEvent,
EventEmitter, FocusHandle, FocusableView, Model, ScrollHandle, Transformation, View,
ViewContext,
};
use remote_projects::{DevServer, DevServerId, RemoteProject, RemoteProjectId};
use rpc::{
proto::{self, CreateDevServerResponse, DevServerStatus},
ErrorCode, ErrorExt,
};
use settings::Settings;
use theme::ThemeSettings;
use ui::{prelude::*, Indicator, List, ListHeader, ListItem, ModalContent, ModalHeader, Tooltip};
use ui_text_field::{FieldLabelLayout, TextField};
use util::ResultExt;
use workspace::{notifications::DetachAndPromptErr, AppState, ModalView, Workspace};
use crate::OpenRemote;
pub struct RemoteProjects {
mode: Mode,
focus_handle: FocusHandle,
scroll_handle: ScrollHandle,
remote_project_store: Model<remote_projects::Store>,
remote_project_path_input: View<TextField>,
dev_server_name_input: View<TextField>,
_subscription: gpui::Subscription,
}
#[derive(Default)]
struct CreateDevServer {
creating: bool,
dev_server: Option<CreateDevServerResponse>,
}
struct CreateRemoteProject {
dev_server_id: DevServerId,
creating: bool,
remote_project: Option<proto::RemoteProject>,
}
enum Mode {
Default,
CreateRemoteProject(CreateRemoteProject),
CreateDevServer(CreateDevServer),
}
impl RemoteProjects {
pub fn register(_: &mut Workspace, cx: &mut ViewContext<Workspace>) {
cx.observe_flag::<feature_flags::Remoting, _>(|enabled, workspace, _| {
if enabled {
workspace.register_action(|workspace, _: &OpenRemote, cx| {
workspace.toggle_modal(cx, |cx| Self::new(cx))
});
}
})
.detach();
}
pub fn new(cx: &mut ViewContext<Self>) -> Self {
let remote_project_path_input = cx.new_view(|cx| TextField::new(cx, "", "Project path"));
let dev_server_name_input =
cx.new_view(|cx| TextField::new(cx, "Name", "").with_label(FieldLabelLayout::Stacked));
let focus_handle = cx.focus_handle();
let remote_project_store = remote_projects::Store::global(cx);
let subscription = cx.observe(&remote_project_store, |_, _, cx| {
cx.notify();
});
Self {
mode: Mode::Default,
focus_handle,
scroll_handle: ScrollHandle::new(),
remote_project_store,
remote_project_path_input,
dev_server_name_input,
_subscription: subscription,
}
}
pub fn create_remote_project(
&mut self,
dev_server_id: DevServerId,
cx: &mut ViewContext<Self>,
) {
let path = self
.remote_project_path_input
.read(cx)
.editor()
.read(cx)
.text(cx)
.trim()
.to_string();
if path == "" {
return;
}
if self
.remote_project_store
.read(cx)
.remote_projects_for_server(dev_server_id)
.iter()
.any(|p| p.path == path)
{
cx.spawn(|_, mut cx| async move {
cx.prompt(
gpui::PromptLevel::Critical,
"Failed to create project",
Some(&format!(
"Project {} already exists for this dev server.",
path
)),
&["Ok"],
)
.await
})
.detach_and_log_err(cx);
return;
}
let create = {
let path = path.clone();
self.remote_project_store.update(cx, |store, cx| {
store.create_remote_project(dev_server_id, path, cx)
})
};
cx.spawn(|this, mut cx| async move {
let result = create.await;
let remote_project = result.as_ref().ok().and_then(|r| r.remote_project.clone());
this.update(&mut cx, |this, _| {
this.mode = Mode::CreateRemoteProject(CreateRemoteProject {
dev_server_id,
creating: false,
remote_project,
});
})
.log_err();
result
})
.detach_and_prompt_err("Failed to create project", cx, move |e, _| {
match e.error_code() {
ErrorCode::DevServerOffline => Some(
"The dev server is offline. Please log in and check it is connected."
.to_string(),
),
ErrorCode::RemoteProjectPathDoesNotExist => {
Some(format!("The path `{}` does not exist on the server.", path))
}
_ => None,
}
});
self.remote_project_path_input.update(cx, |input, cx| {
input.editor().update(cx, |editor, cx| {
editor.set_text("", cx);
});
});
self.mode = Mode::CreateRemoteProject(CreateRemoteProject {
dev_server_id,
creating: true,
remote_project: None,
});
}
pub fn create_dev_server(&mut self, cx: &mut ViewContext<Self>) {
let name = self
.dev_server_name_input
.read(cx)
.editor()
.read(cx)
.text(cx)
.trim()
.to_string();
if name == "" {
return;
}
let dev_server = self
.remote_project_store
.update(cx, |store, cx| store.create_dev_server(name.clone(), cx));
cx.spawn(|this, mut cx| async move {
let result = dev_server.await;
this.update(&mut cx, |this, _| match &result {
Ok(dev_server) => {
this.mode = Mode::CreateDevServer(CreateDevServer {
creating: false,
dev_server: Some(dev_server.clone()),
});
}
Err(_) => {
this.mode = Mode::CreateDevServer(Default::default());
}
})
.log_err();
result
})
.detach_and_prompt_err("Failed to create server", cx, |_, _| None);
self.mode = Mode::CreateDevServer(CreateDevServer {
creating: true,
dev_server: None,
});
cx.notify()
}
fn delete_dev_server(&mut self, id: DevServerId, cx: &mut ViewContext<Self>) {
let answer = cx.prompt(
gpui::PromptLevel::Info,
"Are you sure?",
Some("This will delete the dev server and all of its remote projects."),
&["Delete", "Cancel"],
);
cx.spawn(|this, mut cx| async move {
let answer = answer.await?;
if answer != 0 {
return Ok(());
}
this.update(&mut cx, |this, cx| {
this.remote_project_store
.update(cx, |store, cx| store.delete_dev_server(id, cx))
})?
.await
})
.detach_and_prompt_err("Failed to delete dev server", cx, |_, _| None);
}
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
match self.mode {
Mode::Default => {}
Mode::CreateRemoteProject(CreateRemoteProject { dev_server_id, .. }) => {
self.create_remote_project(dev_server_id, cx);
}
Mode::CreateDevServer(_) => {
self.create_dev_server(cx);
}
}
}
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
match self.mode {
Mode::Default => cx.emit(DismissEvent),
Mode::CreateRemoteProject(_) | Mode::CreateDevServer(_) => {
self.mode = Mode::Default;
self.focus_handle(cx).focus(cx);
cx.notify();
}
}
}
fn render_dev_server(
&mut self,
dev_server: &DevServer,
cx: &mut ViewContext<Self>,
) -> impl IntoElement {
let dev_server_id = dev_server.id;
let status = dev_server.status;
v_flex()
.w_full()
.child(
h_flex()
.group("dev-server")
.justify_between()
.child(
h_flex()
.gap_2()
.child(
div()
.id(("status", dev_server.id.0))
.relative()
.child(Icon::new(IconName::Server).size(IconSize::Small))
.child(
div().absolute().bottom_0().left(rems_from_px(8.0)).child(
Indicator::dot().color(match status {
DevServerStatus::Online => Color::Created,
DevServerStatus::Offline => Color::Hidden,
}),
),
)
.tooltip(move |cx| {
Tooltip::text(
match status {
DevServerStatus::Online => "Online",
DevServerStatus::Offline => "Offline",
},
cx,
)
}),
)
.child(dev_server.name.clone())
.child(
h_flex()
.visible_on_hover("dev-server")
.gap_1()
.child(
IconButton::new("edit-dev-server", IconName::Pencil)
.disabled(true) //TODO implement this on the collab side
.tooltip(|cx| {
Tooltip::text("Coming Soon - Edit dev server", cx)
}),
)
.child({
let dev_server_id = dev_server.id;
IconButton::new("remove-dev-server", IconName::Trash)
.on_click(cx.listener(move |this, _, cx| {
this.delete_dev_server(dev_server_id, cx)
}))
.tooltip(|cx| Tooltip::text("Remove dev server", cx))
}),
),
)
.child(
h_flex().gap_1().child(
IconButton::new(
("add-remote-project", dev_server_id.0),
IconName::Plus,
)
.tooltip(|cx| Tooltip::text("Add a remote project", cx))
.on_click(cx.listener(
move |this, _, cx| {
this.mode = Mode::CreateRemoteProject(CreateRemoteProject {
dev_server_id,
creating: false,
remote_project: None,
});
this.remote_project_path_input
.read(cx)
.focus_handle(cx)
.focus(cx);
cx.notify();
},
)),
),
),
)
.child(
v_flex()
.w_full()
.bg(cx.theme().colors().title_bar_background) // todo: this should be distinct
.border()
.border_color(cx.theme().colors().border_variant)
.rounded_md()
.my_1()
.py_0p5()
.px_3()
.child(
List::new().empty_message("No projects.").children(
self.remote_project_store
.read(cx)
.remote_projects_for_server(dev_server.id)
.iter()
.map(|p| self.render_remote_project(p, cx)),
),
),
)
}
fn render_remote_project(
&mut self,
project: &RemoteProject,
cx: &mut ViewContext<Self>,
) -> impl IntoElement {
let remote_project_id = project.id;
let project_id = project.project_id;
let is_online = project_id.is_some();
ListItem::new(("remote-project", remote_project_id.0))
.start_slot(Icon::new(IconName::FileTree).when(!is_online, |icon| icon.color(Color::Muted)))
.child(
Label::new(project.path.clone())
)
.on_click(cx.listener(move |_, _, cx| {
if let Some(project_id) = project_id {
if let Some(app_state) = AppState::global(cx).upgrade() {
workspace::join_remote_project(project_id, app_state, cx)
.detach_and_prompt_err("Could not join project", cx, |_, _| None)
}
} else {
cx.spawn(|_, mut cx| async move {
cx.prompt(gpui::PromptLevel::Critical, "This project is offline", Some("The `zed` instance running on this dev server is not connected. You will have to restart it."), &["Ok"]).await.log_err();
}).detach();
}
}))
}
fn render_create_dev_server(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let Mode::CreateDevServer(CreateDevServer {
creating,
dev_server,
}) = &self.mode
else {
unreachable!()
};
self.dev_server_name_input.update(cx, |input, cx| {
input.set_disabled(*creating || dev_server.is_some(), cx);
});
v_flex()
.id("scroll-container")
.h_full()
.overflow_y_scroll()
.track_scroll(&self.scroll_handle)
.px_1()
.pt_0p5()
.gap_px()
.child(
ModalHeader::new("remote-projects")
.show_back_button(true)
.child(Headline::new("New dev server").size(HeadlineSize::Small)),
)
.child(
ModalContent::new().child(
v_flex()
.w_full()
.child(
h_flex()
.pb_2()
.items_end()
.w_full()
.px_2()
.border_b_1()
.border_color(cx.theme().colors().border)
.child(
div()
.pl_2()
.max_w(rems(16.))
.child(self.dev_server_name_input.clone()),
)
.child(
div()
.pl_1()
.pb(px(3.))
.when(!*creating && dev_server.is_none(), |div| {
div.child(Button::new("create-dev-server", "Create").on_click(
cx.listener(move |this, _, cx| {
this.create_dev_server(cx);
}),
))
})
.when(*creating && dev_server.is_none(), |div| {
div.child(
Button::new("create-dev-server", "Creating...")
.disabled(true),
)
}),
)
)
.when(dev_server.is_none(), |div| {
div.px_2().child(Label::new("Once you have created a dev server, you will be given a command to run on the server to register it.").color(Color::Muted))
})
.when_some(dev_server.clone(), |div, dev_server| {
let status = self
.remote_project_store
.read(cx)
.dev_server_status(DevServerId(dev_server.dev_server_id));
let instructions = SharedString::from(format!(
"zed --dev-server-token {}",
dev_server.access_token
));
div.child(
v_flex()
.pl_2()
.pt_2()
.gap_2()
.child(
h_flex().justify_between().w_full()
.child(Label::new(format!(
"Please log into `{}` and run:",
dev_server.name
)))
.child(
Button::new("copy-access-token", "Copy Instructions")
.icon(Some(IconName::Copy))
.icon_size(IconSize::Small)
.on_click({
let instructions = instructions.clone();
cx.listener(move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new(
instructions.to_string(),
))
})})
)
)
.child(
v_flex()
.w_full()
.bg(cx.theme().colors().title_bar_background) // todo: this should be distinct
.border()
.border_color(cx.theme().colors().border_variant)
.rounded_md()
.my_1()
.py_0p5()
.px_3()
.font(ThemeSettings::get_global(cx).buffer_font.family.clone())
.child(Label::new(instructions))
)
.when(status == DevServerStatus::Offline, |this| {
this.child(
h_flex()
.gap_2()
.child(
Icon::new(IconName::ArrowCircle)
.size(IconSize::Medium)
.with_animation(
"arrow-circle",
Animation::new(Duration::from_secs(2)).repeat(),
|icon, delta| {
icon.transform(Transformation::rotate(percentage(delta)))
},
),
)
.child(
Label::new("Waiting for connection…"),
)
)
})
.when(status == DevServerStatus::Online, |this| {
this.child(Label::new("🎊 Connection established!"))
.child(
h_flex().justify_end().child(
Button::new("done", "Done").on_click(cx.listener(
|_, _, cx| {
cx.dispatch_action(menu::Cancel.boxed_clone())
},
))
),
)
}),
)
}),
)
)
}
fn render_default(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let dev_servers = self.remote_project_store.read(cx).dev_servers();
v_flex()
.id("scroll-container")
.h_full()
.overflow_y_scroll()
.track_scroll(&self.scroll_handle)
.px_1()
.pt_0p5()
.gap_px()
.child(
ModalHeader::new("remote-projects")
.show_dismiss_button(true)
.child(Headline::new("Remote Projects").size(HeadlineSize::Small)),
)
.child(
ModalContent::new().child(
List::new()
.empty_message("No dev servers registered.")
.header(Some(
ListHeader::new("Dev Servers").end_slot(
Button::new("register-dev-server-button", "New Server")
.icon(IconName::Plus)
.icon_position(IconPosition::Start)
.tooltip(|cx| Tooltip::text("Register a new dev server", cx))
.on_click(cx.listener(|this, _, cx| {
this.mode = Mode::CreateDevServer(Default::default());
this.dev_server_name_input.update(cx, |input, cx| {
input.editor().update(cx, |editor, cx| {
editor.set_text("", cx);
});
input.focus_handle(cx).focus(cx)
});
cx.notify();
})),
),
))
.children(dev_servers.iter().map(|dev_server| {
self.render_dev_server(dev_server, cx).into_any_element()
})),
),
)
}
fn render_create_remote_project(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let Mode::CreateRemoteProject(CreateRemoteProject {
dev_server_id,
creating,
remote_project,
}) = &self.mode
else {
unreachable!()
};
let dev_server = self
.remote_project_store
.read(cx)
.dev_server(*dev_server_id)
.cloned();
let (dev_server_name, dev_server_status) = dev_server
.map(|server| (server.name, server.status))
.unwrap_or((SharedString::from(""), DevServerStatus::Offline));
v_flex()
.px_1()
.pt_0p5()
.gap_px()
.child(
v_flex().py_0p5().px_1().child(
h_flex()
.px_1()
.py_0p5()
.child(
IconButton::new("back", IconName::ArrowLeft)
.style(ButtonStyle::Transparent)
.on_click(cx.listener(|_, _: &gpui::ClickEvent, cx| {
cx.dispatch_action(menu::Cancel.boxed_clone())
})),
)
.child(Headline::new("Add remote project").size(HeadlineSize::Small)),
),
)
.child(
h_flex()
.ml_5()
.gap_2()
.child(
div()
.id(("status", dev_server_id.0))
.relative()
.child(Icon::new(IconName::Server))
.child(div().absolute().bottom_0().left(rems_from_px(12.0)).child(
Indicator::dot().color(match dev_server_status {
DevServerStatus::Online => Color::Created,
DevServerStatus::Offline => Color::Hidden,
}),
))
.tooltip(move |cx| {
Tooltip::text(
match dev_server_status {
DevServerStatus::Online => "Online",
DevServerStatus::Offline => "Offline",
},
cx,
)
}),
)
.child(dev_server_name.clone()),
)
.child(
h_flex()
.ml_5()
.gap_2()
.child(self.remote_project_path_input.clone())
.when(!*creating && remote_project.is_none(), |div| {
div.child(Button::new("create-remote-server", "Create").on_click({
let dev_server_id = *dev_server_id;
cx.listener(move |this, _, cx| {
this.create_remote_project(dev_server_id, cx)
})
}))
})
.when(*creating, |div| {
div.child(Button::new("create-dev-server", "Creating...").disabled(true))
}),
)
.when_some(remote_project.clone(), |div, remote_project| {
let status = self
.remote_project_store
.read(cx)
.remote_project(RemoteProjectId(remote_project.id))
.map(|project| {
if project.project_id.is_some() {
DevServerStatus::Online
} else {
DevServerStatus::Offline
}
})
.unwrap_or(DevServerStatus::Offline);
div.child(
v_flex()
.ml_5()
.ml_8()
.gap_2()
.when(status == DevServerStatus::Offline, |this| {
this.child(Label::new("Waiting for project..."))
})
.when(status == DevServerStatus::Online, |this| {
this.child(Label::new("Project online! 🎊")).child(
Button::new("done", "Done").on_click(cx.listener(|_, _, cx| {
cx.dispatch_action(menu::Cancel.boxed_clone())
})),
)
}),
)
})
}
}
impl ModalView for RemoteProjects {}
impl FocusableView for RemoteProjects {
fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
self.focus_handle.clone()
}
}
impl EventEmitter<DismissEvent> for RemoteProjects {}
impl Render for RemoteProjects {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.track_focus(&self.focus_handle)
.elevation_3(cx)
.key_context("DevServerModal")
.on_action(cx.listener(Self::cancel))
.on_action(cx.listener(Self::confirm))
.on_mouse_down_out(cx.listener(|this, _, cx| {
if matches!(this.mode, Mode::Default) {
cx.emit(DismissEvent)
}
}))
.pb_4()
.w(rems(34.))
.min_h(rems(20.))
.max_h(rems(40.))
.child(match &self.mode {
Mode::Default => self.render_default(cx).into_any_element(),
Mode::CreateRemoteProject(_) => {
self.render_create_remote_project(cx).into_any_element()
}
Mode::CreateDevServer(_) => self.render_create_dev_server(cx).into_any_element(),
})
}
}