remote_servers.rs

   1use std::any::Any;
   2use std::borrow::Cow;
   3use std::collections::BTreeSet;
   4use std::path::PathBuf;
   5use std::rc::Rc;
   6use std::sync::Arc;
   7use std::sync::atomic;
   8use std::sync::atomic::AtomicUsize;
   9
  10use editor::Editor;
  11use file_finder::OpenPathDelegate;
  12use futures::FutureExt;
  13use futures::channel::oneshot;
  14use futures::future::Shared;
  15use futures::select;
  16use gpui::ClickEvent;
  17use gpui::ClipboardItem;
  18use gpui::Subscription;
  19use gpui::Task;
  20use gpui::WeakEntity;
  21use gpui::canvas;
  22use gpui::{
  23    AnyElement, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
  24    PromptLevel, ScrollHandle, Window,
  25};
  26use paths::global_ssh_config_file;
  27use paths::user_ssh_config_file;
  28use picker::Picker;
  29use project::Fs;
  30use project::Project;
  31use remote::remote_client::ConnectionIdentifier;
  32use remote::{RemoteClient, SshConnectionOptions};
  33use settings::Settings;
  34use settings::SettingsStore;
  35use settings::update_settings_file;
  36use settings::watch_config_file;
  37use smol::stream::StreamExt as _;
  38use ui::Navigable;
  39use ui::NavigableEntry;
  40use ui::{
  41    IconButtonShape, List, ListItem, ListSeparator, Modal, ModalHeader, Scrollbar, ScrollbarState,
  42    Section, Tooltip, prelude::*,
  43};
  44use util::{
  45    ResultExt,
  46    paths::{PathStyle, RemotePathBuf},
  47};
  48use workspace::OpenOptions;
  49use workspace::Toast;
  50use workspace::notifications::NotificationId;
  51use workspace::{
  52    ModalView, Workspace, notifications::DetachAndPromptErr,
  53    open_ssh_project_with_existing_connection,
  54};
  55
  56use crate::ssh_config::parse_ssh_config_hosts;
  57use crate::ssh_connections::RemoteSettingsContent;
  58use crate::ssh_connections::SshConnection;
  59use crate::ssh_connections::SshConnectionHeader;
  60use crate::ssh_connections::SshConnectionModal;
  61use crate::ssh_connections::SshProject;
  62use crate::ssh_connections::SshPrompt;
  63use crate::ssh_connections::SshSettings;
  64use crate::ssh_connections::connect_over_ssh;
  65use crate::ssh_connections::open_ssh_project;
  66
  67mod navigation_base {}
  68pub struct RemoteServerProjects {
  69    mode: Mode,
  70    focus_handle: FocusHandle,
  71    workspace: WeakEntity<Workspace>,
  72    retained_connections: Vec<Entity<RemoteClient>>,
  73    ssh_config_updates: Task<()>,
  74    ssh_config_servers: BTreeSet<SharedString>,
  75    create_new_window: bool,
  76    _subscription: Subscription,
  77}
  78
  79struct CreateRemoteServer {
  80    address_editor: Entity<Editor>,
  81    address_error: Option<SharedString>,
  82    ssh_prompt: Option<Entity<SshPrompt>>,
  83    _creating: Option<Task<Option<()>>>,
  84}
  85
  86impl CreateRemoteServer {
  87    fn new(window: &mut Window, cx: &mut App) -> Self {
  88        let address_editor = cx.new(|cx| Editor::single_line(window, cx));
  89        address_editor.update(cx, |this, cx| {
  90            this.focus_handle(cx).focus(window);
  91        });
  92        Self {
  93            address_editor,
  94            address_error: None,
  95            ssh_prompt: None,
  96            _creating: None,
  97        }
  98    }
  99}
 100
 101struct ProjectPicker {
 102    connection_string: SharedString,
 103    nickname: Option<SharedString>,
 104    picker: Entity<Picker<OpenPathDelegate>>,
 105    _path_task: Shared<Task<Option<()>>>,
 106}
 107
 108struct EditNicknameState {
 109    index: usize,
 110    editor: Entity<Editor>,
 111}
 112
 113impl EditNicknameState {
 114    fn new(index: usize, window: &mut Window, cx: &mut App) -> Self {
 115        let this = Self {
 116            index,
 117            editor: cx.new(|cx| Editor::single_line(window, cx)),
 118        };
 119        let starting_text = SshSettings::get_global(cx)
 120            .ssh_connections()
 121            .nth(index)
 122            .and_then(|state| state.nickname)
 123            .filter(|text| !text.is_empty());
 124        this.editor.update(cx, |this, cx| {
 125            this.set_placeholder_text("Add a nickname for this server", cx);
 126            if let Some(starting_text) = starting_text {
 127                this.set_text(starting_text, window, cx);
 128            }
 129        });
 130        this.editor.focus_handle(cx).focus(window);
 131        this
 132    }
 133}
 134
 135impl Focusable for ProjectPicker {
 136    fn focus_handle(&self, cx: &App) -> FocusHandle {
 137        self.picker.focus_handle(cx)
 138    }
 139}
 140
 141impl ProjectPicker {
 142    fn new(
 143        create_new_window: bool,
 144        ix: usize,
 145        connection: SshConnectionOptions,
 146        project: Entity<Project>,
 147        home_dir: RemotePathBuf,
 148        path_style: PathStyle,
 149        workspace: WeakEntity<Workspace>,
 150        window: &mut Window,
 151        cx: &mut Context<RemoteServerProjects>,
 152    ) -> Entity<Self> {
 153        let (tx, rx) = oneshot::channel();
 154        let lister = project::DirectoryLister::Project(project.clone());
 155        let delegate = file_finder::OpenPathDelegate::new(tx, lister, false, path_style);
 156
 157        let picker = cx.new(|cx| {
 158            let picker = Picker::uniform_list(delegate, window, cx)
 159                .width(rems(34.))
 160                .modal(false);
 161            picker.set_query(home_dir.to_string(), window, cx);
 162            picker
 163        });
 164        let connection_string = connection.connection_string().into();
 165        let nickname = connection.nickname.clone().map(|nick| nick.into());
 166        let _path_task = cx
 167            .spawn_in(window, {
 168                let workspace = workspace;
 169                async move |this, cx| {
 170                    let Ok(Some(paths)) = rx.await else {
 171                        workspace
 172                            .update_in(cx, |workspace, window, cx| {
 173                                let fs = workspace.project().read(cx).fs().clone();
 174                                let weak = cx.entity().downgrade();
 175                                workspace.toggle_modal(window, cx, |window, cx| {
 176                                    RemoteServerProjects::new(
 177                                        create_new_window,
 178                                        fs,
 179                                        window,
 180                                        weak,
 181                                        cx,
 182                                    )
 183                                });
 184                            })
 185                            .log_err()?;
 186                        return None;
 187                    };
 188
 189                    let app_state = workspace
 190                        .read_with(cx, |workspace, _| workspace.app_state().clone())
 191                        .ok()?;
 192
 193                    cx.update(|_, cx| {
 194                        let fs = app_state.fs.clone();
 195                        update_settings_file::<SshSettings>(fs, cx, {
 196                            let paths = paths
 197                                .iter()
 198                                .map(|path| path.to_string_lossy().to_string())
 199                                .collect();
 200                            move |setting, _| {
 201                                if let Some(server) = setting
 202                                    .ssh_connections
 203                                    .as_mut()
 204                                    .and_then(|connections| connections.get_mut(ix))
 205                                {
 206                                    server.projects.insert(SshProject { paths });
 207                                }
 208                            }
 209                        });
 210                    })
 211                    .log_err();
 212
 213                    let options = cx
 214                        .update(|_, cx| (app_state.build_window_options)(None, cx))
 215                        .log_err()?;
 216                    let window = cx
 217                        .open_window(options, |window, cx| {
 218                            cx.new(|cx| {
 219                                telemetry::event!("SSH Project Created");
 220                                Workspace::new(None, project.clone(), app_state.clone(), window, cx)
 221                            })
 222                        })
 223                        .log_err()?;
 224
 225                    open_ssh_project_with_existing_connection(
 226                        connection, project, paths, app_state, window, cx,
 227                    )
 228                    .await
 229                    .log_err();
 230
 231                    this.update(cx, |_, cx| {
 232                        cx.emit(DismissEvent);
 233                    })
 234                    .ok();
 235                    Some(())
 236                }
 237            })
 238            .shared();
 239        cx.new(|_| Self {
 240            _path_task,
 241            picker,
 242            connection_string,
 243            nickname,
 244        })
 245    }
 246}
 247
 248impl gpui::Render for ProjectPicker {
 249    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 250        v_flex()
 251            .child(
 252                SshConnectionHeader {
 253                    connection_string: self.connection_string.clone(),
 254                    paths: Default::default(),
 255                    nickname: self.nickname.clone(),
 256                }
 257                .render(window, cx),
 258            )
 259            .child(
 260                div()
 261                    .border_t_1()
 262                    .border_color(cx.theme().colors().border_variant)
 263                    .child(self.picker.clone()),
 264            )
 265    }
 266}
 267
 268#[derive(Clone)]
 269enum RemoteEntry {
 270    Project {
 271        open_folder: NavigableEntry,
 272        projects: Vec<(NavigableEntry, SshProject)>,
 273        configure: NavigableEntry,
 274        connection: SshConnection,
 275    },
 276    SshConfig {
 277        open_folder: NavigableEntry,
 278        host: SharedString,
 279    },
 280}
 281
 282impl RemoteEntry {
 283    fn is_from_zed(&self) -> bool {
 284        matches!(self, Self::Project { .. })
 285    }
 286
 287    fn connection(&self) -> Cow<'_, SshConnection> {
 288        match self {
 289            Self::Project { connection, .. } => Cow::Borrowed(connection),
 290            Self::SshConfig { host, .. } => Cow::Owned(SshConnection {
 291                host: host.clone(),
 292                ..SshConnection::default()
 293            }),
 294        }
 295    }
 296}
 297
 298#[derive(Clone)]
 299struct DefaultState {
 300    scrollbar: ScrollbarState,
 301    add_new_server: NavigableEntry,
 302    servers: Vec<RemoteEntry>,
 303}
 304
 305impl DefaultState {
 306    fn new(ssh_config_servers: &BTreeSet<SharedString>, cx: &mut App) -> Self {
 307        let handle = ScrollHandle::new();
 308        let scrollbar = ScrollbarState::new(handle.clone());
 309        let add_new_server = NavigableEntry::new(&handle, cx);
 310
 311        let ssh_settings = SshSettings::get_global(cx);
 312        let read_ssh_config = ssh_settings.read_ssh_config;
 313
 314        let mut servers: Vec<RemoteEntry> = ssh_settings
 315            .ssh_connections()
 316            .map(|connection| {
 317                let open_folder = NavigableEntry::new(&handle, cx);
 318                let configure = NavigableEntry::new(&handle, cx);
 319                let projects = connection
 320                    .projects
 321                    .iter()
 322                    .map(|project| (NavigableEntry::new(&handle, cx), project.clone()))
 323                    .collect();
 324                RemoteEntry::Project {
 325                    open_folder,
 326                    configure,
 327                    projects,
 328                    connection,
 329                }
 330            })
 331            .collect();
 332
 333        if read_ssh_config {
 334            let mut extra_servers_from_config = ssh_config_servers.clone();
 335            for server in &servers {
 336                if let RemoteEntry::Project { connection, .. } = server {
 337                    extra_servers_from_config.remove(&connection.host);
 338                }
 339            }
 340            servers.extend(extra_servers_from_config.into_iter().map(|host| {
 341                RemoteEntry::SshConfig {
 342                    open_folder: NavigableEntry::new(&handle, cx),
 343                    host,
 344                }
 345            }));
 346        }
 347
 348        Self {
 349            scrollbar,
 350            add_new_server,
 351            servers,
 352        }
 353    }
 354}
 355
 356#[derive(Clone)]
 357struct ViewServerOptionsState {
 358    server_index: usize,
 359    connection: SshConnection,
 360    entries: [NavigableEntry; 4],
 361}
 362enum Mode {
 363    Default(DefaultState),
 364    ViewServerOptions(ViewServerOptionsState),
 365    EditNickname(EditNicknameState),
 366    ProjectPicker(Entity<ProjectPicker>),
 367    CreateRemoteServer(CreateRemoteServer),
 368}
 369
 370impl Mode {
 371    fn default_mode(ssh_config_servers: &BTreeSet<SharedString>, cx: &mut App) -> Self {
 372        Self::Default(DefaultState::new(ssh_config_servers, cx))
 373    }
 374}
 375impl RemoteServerProjects {
 376    pub fn new(
 377        create_new_window: bool,
 378        fs: Arc<dyn Fs>,
 379        window: &mut Window,
 380        workspace: WeakEntity<Workspace>,
 381        cx: &mut Context<Self>,
 382    ) -> Self {
 383        let focus_handle = cx.focus_handle();
 384        let mut read_ssh_config = SshSettings::get_global(cx).read_ssh_config;
 385        let ssh_config_updates = if read_ssh_config {
 386            spawn_ssh_config_watch(fs.clone(), cx)
 387        } else {
 388            Task::ready(())
 389        };
 390
 391        let mut base_style = window.text_style();
 392        base_style.refine(&gpui::TextStyleRefinement {
 393            color: Some(cx.theme().colors().editor_foreground),
 394            ..Default::default()
 395        });
 396
 397        let _subscription =
 398            cx.observe_global_in::<SettingsStore>(window, move |recent_projects, _, cx| {
 399                let new_read_ssh_config = SshSettings::get_global(cx).read_ssh_config;
 400                if read_ssh_config != new_read_ssh_config {
 401                    read_ssh_config = new_read_ssh_config;
 402                    if read_ssh_config {
 403                        recent_projects.ssh_config_updates = spawn_ssh_config_watch(fs.clone(), cx);
 404                    } else {
 405                        recent_projects.ssh_config_servers.clear();
 406                        recent_projects.ssh_config_updates = Task::ready(());
 407                    }
 408                }
 409            });
 410
 411        Self {
 412            mode: Mode::default_mode(&BTreeSet::new(), cx),
 413            focus_handle,
 414            workspace,
 415            retained_connections: Vec::new(),
 416            ssh_config_updates,
 417            ssh_config_servers: BTreeSet::new(),
 418            create_new_window,
 419            _subscription,
 420        }
 421    }
 422
 423    pub fn project_picker(
 424        create_new_window: bool,
 425        ix: usize,
 426        connection_options: remote::SshConnectionOptions,
 427        project: Entity<Project>,
 428        home_dir: RemotePathBuf,
 429        path_style: PathStyle,
 430        window: &mut Window,
 431        cx: &mut Context<Self>,
 432        workspace: WeakEntity<Workspace>,
 433    ) -> Self {
 434        let fs = project.read(cx).fs().clone();
 435        let mut this = Self::new(create_new_window, fs, window, workspace.clone(), cx);
 436        this.mode = Mode::ProjectPicker(ProjectPicker::new(
 437            create_new_window,
 438            ix,
 439            connection_options,
 440            project,
 441            home_dir,
 442            path_style,
 443            workspace,
 444            window,
 445            cx,
 446        ));
 447        cx.notify();
 448
 449        this
 450    }
 451
 452    fn create_ssh_server(
 453        &mut self,
 454        editor: Entity<Editor>,
 455        window: &mut Window,
 456        cx: &mut Context<Self>,
 457    ) {
 458        let input = get_text(&editor, cx);
 459        if input.is_empty() {
 460            return;
 461        }
 462
 463        let connection_options = match SshConnectionOptions::parse_command_line(&input) {
 464            Ok(c) => c,
 465            Err(e) => {
 466                self.mode = Mode::CreateRemoteServer(CreateRemoteServer {
 467                    address_editor: editor,
 468                    address_error: Some(format!("could not parse: {:?}", e).into()),
 469                    ssh_prompt: None,
 470                    _creating: None,
 471                });
 472                return;
 473            }
 474        };
 475        let ssh_prompt = cx.new(|cx| SshPrompt::new(&connection_options, window, cx));
 476
 477        let connection = connect_over_ssh(
 478            ConnectionIdentifier::setup(),
 479            connection_options.clone(),
 480            ssh_prompt.clone(),
 481            window,
 482            cx,
 483        )
 484        .prompt_err("Failed to connect", window, cx, |_, _, _| None);
 485
 486        let address_editor = editor.clone();
 487        let creating = cx.spawn_in(window, async move |this, cx| {
 488            match connection.await {
 489                Some(Some(client)) => this
 490                    .update_in(cx, |this, window, cx| {
 491                        telemetry::event!("SSH Server Created");
 492                        this.retained_connections.push(client);
 493                        this.add_ssh_server(connection_options, cx);
 494                        this.mode = Mode::default_mode(&this.ssh_config_servers, cx);
 495                        this.focus_handle(cx).focus(window);
 496                        cx.notify()
 497                    })
 498                    .log_err(),
 499                _ => this
 500                    .update(cx, |this, cx| {
 501                        address_editor.update(cx, |this, _| {
 502                            this.set_read_only(false);
 503                        });
 504                        this.mode = Mode::CreateRemoteServer(CreateRemoteServer {
 505                            address_editor,
 506                            address_error: None,
 507                            ssh_prompt: None,
 508                            _creating: None,
 509                        });
 510                        cx.notify()
 511                    })
 512                    .log_err(),
 513            };
 514            None
 515        });
 516
 517        editor.update(cx, |this, _| {
 518            this.set_read_only(true);
 519        });
 520        self.mode = Mode::CreateRemoteServer(CreateRemoteServer {
 521            address_editor: editor,
 522            address_error: None,
 523            ssh_prompt: Some(ssh_prompt),
 524            _creating: Some(creating),
 525        });
 526    }
 527
 528    fn view_server_options(
 529        &mut self,
 530        (server_index, connection): (usize, SshConnection),
 531        window: &mut Window,
 532        cx: &mut Context<Self>,
 533    ) {
 534        self.mode = Mode::ViewServerOptions(ViewServerOptionsState {
 535            server_index,
 536            connection,
 537            entries: std::array::from_fn(|_| NavigableEntry::focusable(cx)),
 538        });
 539        self.focus_handle(cx).focus(window);
 540        cx.notify();
 541    }
 542
 543    fn create_ssh_project(
 544        &mut self,
 545        ix: usize,
 546        ssh_connection: SshConnection,
 547        window: &mut Window,
 548        cx: &mut Context<Self>,
 549    ) {
 550        let Some(workspace) = self.workspace.upgrade() else {
 551            return;
 552        };
 553
 554        let create_new_window = self.create_new_window;
 555        let connection_options = ssh_connection.into();
 556        workspace.update(cx, |_, cx| {
 557            cx.defer_in(window, move |workspace, window, cx| {
 558                let app_state = workspace.app_state().clone();
 559                workspace.toggle_modal(window, cx, |window, cx| {
 560                    SshConnectionModal::new(&connection_options, Vec::new(), window, cx)
 561                });
 562                let prompt = workspace
 563                    .active_modal::<SshConnectionModal>(cx)
 564                    .unwrap()
 565                    .read(cx)
 566                    .prompt
 567                    .clone();
 568
 569                let connect = connect_over_ssh(
 570                    ConnectionIdentifier::setup(),
 571                    connection_options.clone(),
 572                    prompt,
 573                    window,
 574                    cx,
 575                )
 576                .prompt_err("Failed to connect", window, cx, |_, _, _| None);
 577
 578                cx.spawn_in(window, async move |workspace, cx| {
 579                    let session = connect.await;
 580
 581                    workspace.update(cx, |workspace, cx| {
 582                        if let Some(prompt) = workspace.active_modal::<SshConnectionModal>(cx) {
 583                            prompt.update(cx, |prompt, cx| prompt.finished(cx))
 584                        }
 585                    })?;
 586
 587                    let Some(Some(session)) = session else {
 588                        return workspace.update_in(cx, |workspace, window, cx| {
 589                            let weak = cx.entity().downgrade();
 590                            let fs = workspace.project().read(cx).fs().clone();
 591                            workspace.toggle_modal(window, cx, |window, cx| {
 592                                RemoteServerProjects::new(create_new_window, fs, window, weak, cx)
 593                            });
 594                        });
 595                    };
 596
 597                    let (path_style, project) = cx.update(|_, cx| {
 598                        (
 599                            session.read(cx).path_style(),
 600                            project::Project::remote(
 601                                session,
 602                                app_state.client.clone(),
 603                                app_state.node_runtime.clone(),
 604                                app_state.user_store.clone(),
 605                                app_state.languages.clone(),
 606                                app_state.fs.clone(),
 607                                cx,
 608                            ),
 609                        )
 610                    })?;
 611
 612                    let home_dir = project
 613                        .read_with(cx, |project, cx| project.resolve_abs_path("~", cx))?
 614                        .await
 615                        .and_then(|path| path.into_abs_path())
 616                        .map(|path| RemotePathBuf::new(path, path_style))
 617                        .unwrap_or_else(|| match path_style {
 618                            PathStyle::Posix => RemotePathBuf::from_str("/", PathStyle::Posix),
 619                            PathStyle::Windows => {
 620                                RemotePathBuf::from_str("C:\\", PathStyle::Windows)
 621                            }
 622                        });
 623
 624                    workspace
 625                        .update_in(cx, |workspace, window, cx| {
 626                            let weak = cx.entity().downgrade();
 627                            workspace.toggle_modal(window, cx, |window, cx| {
 628                                RemoteServerProjects::project_picker(
 629                                    create_new_window,
 630                                    ix,
 631                                    connection_options,
 632                                    project,
 633                                    home_dir,
 634                                    path_style,
 635                                    window,
 636                                    cx,
 637                                    weak,
 638                                )
 639                            });
 640                        })
 641                        .ok();
 642                    Ok(())
 643                })
 644                .detach();
 645            })
 646        })
 647    }
 648
 649    fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 650        match &self.mode {
 651            Mode::Default(_) | Mode::ViewServerOptions(_) => {}
 652            Mode::ProjectPicker(_) => {}
 653            Mode::CreateRemoteServer(state) => {
 654                if let Some(prompt) = state.ssh_prompt.as_ref() {
 655                    prompt.update(cx, |prompt, cx| {
 656                        prompt.confirm(window, cx);
 657                    });
 658                    return;
 659                }
 660
 661                self.create_ssh_server(state.address_editor.clone(), window, cx);
 662            }
 663            Mode::EditNickname(state) => {
 664                let text = Some(state.editor.read(cx).text(cx)).filter(|text| !text.is_empty());
 665                let index = state.index;
 666                self.update_settings_file(cx, move |setting, _| {
 667                    if let Some(connections) = setting.ssh_connections.as_mut()
 668                        && let Some(connection) = connections.get_mut(index)
 669                    {
 670                        connection.nickname = text;
 671                    }
 672                });
 673                self.mode = Mode::default_mode(&self.ssh_config_servers, cx);
 674                self.focus_handle.focus(window);
 675            }
 676        }
 677    }
 678
 679    fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
 680        match &self.mode {
 681            Mode::Default(_) => cx.emit(DismissEvent),
 682            Mode::CreateRemoteServer(state) if state.ssh_prompt.is_some() => {
 683                let new_state = CreateRemoteServer::new(window, cx);
 684                let old_prompt = state.address_editor.read(cx).text(cx);
 685                new_state.address_editor.update(cx, |this, cx| {
 686                    this.set_text(old_prompt, window, cx);
 687                });
 688
 689                self.mode = Mode::CreateRemoteServer(new_state);
 690                cx.notify();
 691            }
 692            _ => {
 693                self.mode = Mode::default_mode(&self.ssh_config_servers, cx);
 694                self.focus_handle(cx).focus(window);
 695                cx.notify();
 696            }
 697        }
 698    }
 699
 700    fn render_ssh_connection(
 701        &mut self,
 702        ix: usize,
 703        ssh_server: RemoteEntry,
 704        window: &mut Window,
 705        cx: &mut Context<Self>,
 706    ) -> impl IntoElement {
 707        let connection = ssh_server.connection().into_owned();
 708        let (main_label, aux_label) = if let Some(nickname) = connection.nickname.clone() {
 709            let aux_label = SharedString::from(format!("({})", connection.host));
 710            (nickname.into(), Some(aux_label))
 711        } else {
 712            (connection.host.clone(), None)
 713        };
 714        v_flex()
 715            .w_full()
 716            .child(ListSeparator)
 717            .child(
 718                h_flex()
 719                    .group("ssh-server")
 720                    .w_full()
 721                    .pt_0p5()
 722                    .px_3()
 723                    .gap_1()
 724                    .overflow_hidden()
 725                    .child(
 726                        div().max_w_96().overflow_hidden().text_ellipsis().child(
 727                            Label::new(main_label)
 728                                .size(LabelSize::Small)
 729                                .color(Color::Muted),
 730                        ),
 731                    )
 732                    .children(
 733                        aux_label.map(|label| {
 734                            Label::new(label).size(LabelSize::Small).color(Color::Muted)
 735                        }),
 736                    ),
 737            )
 738            .child(match &ssh_server {
 739                RemoteEntry::Project {
 740                    open_folder,
 741                    projects,
 742                    configure,
 743                    connection,
 744                } => List::new()
 745                    .empty_message("No projects.")
 746                    .children(projects.iter().enumerate().map(|(pix, p)| {
 747                        v_flex().gap_0p5().child(self.render_ssh_project(
 748                            ix,
 749                            ssh_server.clone(),
 750                            pix,
 751                            p,
 752                            window,
 753                            cx,
 754                        ))
 755                    }))
 756                    .child(
 757                        h_flex()
 758                            .id(("new-remote-project-container", ix))
 759                            .track_focus(&open_folder.focus_handle)
 760                            .anchor_scroll(open_folder.scroll_anchor.clone())
 761                            .on_action(cx.listener({
 762                                let ssh_connection = connection.clone();
 763                                move |this, _: &menu::Confirm, window, cx| {
 764                                    this.create_ssh_project(ix, ssh_connection.clone(), window, cx);
 765                                }
 766                            }))
 767                            .child(
 768                                ListItem::new(("new-remote-project", ix))
 769                                    .toggle_state(
 770                                        open_folder.focus_handle.contains_focused(window, cx),
 771                                    )
 772                                    .inset(true)
 773                                    .spacing(ui::ListItemSpacing::Sparse)
 774                                    .start_slot(Icon::new(IconName::Plus).color(Color::Muted))
 775                                    .child(Label::new("Open Folder"))
 776                                    .on_click(cx.listener({
 777                                        let ssh_connection = connection.clone();
 778                                        move |this, _, window, cx| {
 779                                            this.create_ssh_project(
 780                                                ix,
 781                                                ssh_connection.clone(),
 782                                                window,
 783                                                cx,
 784                                            );
 785                                        }
 786                                    })),
 787                            ),
 788                    )
 789                    .child(
 790                        h_flex()
 791                            .id(("server-options-container", ix))
 792                            .track_focus(&configure.focus_handle)
 793                            .anchor_scroll(configure.scroll_anchor.clone())
 794                            .on_action(cx.listener({
 795                                let ssh_connection = connection.clone();
 796                                move |this, _: &menu::Confirm, window, cx| {
 797                                    this.view_server_options(
 798                                        (ix, ssh_connection.clone()),
 799                                        window,
 800                                        cx,
 801                                    );
 802                                }
 803                            }))
 804                            .child(
 805                                ListItem::new(("server-options", ix))
 806                                    .toggle_state(
 807                                        configure.focus_handle.contains_focused(window, cx),
 808                                    )
 809                                    .inset(true)
 810                                    .spacing(ui::ListItemSpacing::Sparse)
 811                                    .start_slot(Icon::new(IconName::Settings).color(Color::Muted))
 812                                    .child(Label::new("View Server Options"))
 813                                    .on_click(cx.listener({
 814                                        let ssh_connection = connection.clone();
 815                                        move |this, _, window, cx| {
 816                                            this.view_server_options(
 817                                                (ix, ssh_connection.clone()),
 818                                                window,
 819                                                cx,
 820                                            );
 821                                        }
 822                                    })),
 823                            ),
 824                    ),
 825                RemoteEntry::SshConfig { open_folder, host } => List::new().child(
 826                    h_flex()
 827                        .id(("new-remote-project-container", ix))
 828                        .track_focus(&open_folder.focus_handle)
 829                        .anchor_scroll(open_folder.scroll_anchor.clone())
 830                        .on_action(cx.listener({
 831                            let ssh_connection = connection.clone();
 832                            let host = host.clone();
 833                            move |this, _: &menu::Confirm, window, cx| {
 834                                let new_ix = this.create_host_from_ssh_config(&host, cx);
 835                                this.create_ssh_project(new_ix, ssh_connection.clone(), window, cx);
 836                            }
 837                        }))
 838                        .child(
 839                            ListItem::new(("new-remote-project", ix))
 840                                .toggle_state(open_folder.focus_handle.contains_focused(window, cx))
 841                                .inset(true)
 842                                .spacing(ui::ListItemSpacing::Sparse)
 843                                .start_slot(Icon::new(IconName::Plus).color(Color::Muted))
 844                                .child(Label::new("Open Folder"))
 845                                .on_click(cx.listener({
 846                                    let ssh_connection = connection;
 847                                    let host = host.clone();
 848                                    move |this, _, window, cx| {
 849                                        let new_ix = this.create_host_from_ssh_config(&host, cx);
 850                                        this.create_ssh_project(
 851                                            new_ix,
 852                                            ssh_connection.clone(),
 853                                            window,
 854                                            cx,
 855                                        );
 856                                    }
 857                                })),
 858                        ),
 859                ),
 860            })
 861    }
 862
 863    fn render_ssh_project(
 864        &mut self,
 865        server_ix: usize,
 866        server: RemoteEntry,
 867        ix: usize,
 868        (navigation, project): &(NavigableEntry, SshProject),
 869        window: &mut Window,
 870        cx: &mut Context<Self>,
 871    ) -> impl IntoElement {
 872        let create_new_window = self.create_new_window;
 873        let is_from_zed = server.is_from_zed();
 874        let element_id_base = SharedString::from(format!("remote-project-{server_ix}"));
 875        let container_element_id_base =
 876            SharedString::from(format!("remote-project-container-{element_id_base}"));
 877
 878        let callback = Rc::new({
 879            let project = project.clone();
 880            move |remote_server_projects: &mut Self,
 881                  secondary_confirm: bool,
 882                  window: &mut Window,
 883                  cx: &mut Context<Self>| {
 884                let Some(app_state) = remote_server_projects
 885                    .workspace
 886                    .read_with(cx, |workspace, _| workspace.app_state().clone())
 887                    .log_err()
 888                else {
 889                    return;
 890                };
 891                let project = project.clone();
 892                let server = server.connection().into_owned();
 893                cx.emit(DismissEvent);
 894
 895                let replace_window = match (create_new_window, secondary_confirm) {
 896                    (true, false) | (false, true) => None,
 897                    (true, true) | (false, false) => window.window_handle().downcast::<Workspace>(),
 898                };
 899
 900                cx.spawn_in(window, async move |_, cx| {
 901                    let result = open_ssh_project(
 902                        server.into(),
 903                        project.paths.into_iter().map(PathBuf::from).collect(),
 904                        app_state,
 905                        OpenOptions {
 906                            replace_window,
 907                            ..OpenOptions::default()
 908                        },
 909                        cx,
 910                    )
 911                    .await;
 912                    if let Err(e) = result {
 913                        log::error!("Failed to connect: {e:#}");
 914                        cx.prompt(
 915                            gpui::PromptLevel::Critical,
 916                            "Failed to connect",
 917                            Some(&e.to_string()),
 918                            &["Ok"],
 919                        )
 920                        .await
 921                        .ok();
 922                    }
 923                })
 924                .detach();
 925            }
 926        });
 927
 928        div()
 929            .id((container_element_id_base, ix))
 930            .track_focus(&navigation.focus_handle)
 931            .anchor_scroll(navigation.scroll_anchor.clone())
 932            .on_action(cx.listener({
 933                let callback = callback.clone();
 934                move |this, _: &menu::Confirm, window, cx| {
 935                    callback(this, false, window, cx);
 936                }
 937            }))
 938            .on_action(cx.listener({
 939                let callback = callback.clone();
 940                move |this, _: &menu::SecondaryConfirm, window, cx| {
 941                    callback(this, true, window, cx);
 942                }
 943            }))
 944            .child(
 945                ListItem::new((element_id_base, ix))
 946                    .toggle_state(navigation.focus_handle.contains_focused(window, cx))
 947                    .inset(true)
 948                    .spacing(ui::ListItemSpacing::Sparse)
 949                    .start_slot(
 950                        Icon::new(IconName::Folder)
 951                            .color(Color::Muted)
 952                            .size(IconSize::Small),
 953                    )
 954                    .child(Label::new(project.paths.join(", ")))
 955                    .on_click(cx.listener(move |this, e: &ClickEvent, window, cx| {
 956                        let secondary_confirm = e.modifiers().platform;
 957                        callback(this, secondary_confirm, window, cx)
 958                    }))
 959                    .when(is_from_zed, |server_list_item| {
 960                        server_list_item.end_hover_slot::<AnyElement>(Some(
 961                            div()
 962                                .mr_2()
 963                                .child({
 964                                    let project = project.clone();
 965                                    // Right-margin to offset it from the Scrollbar
 966                                    IconButton::new("remove-remote-project", IconName::Trash)
 967                                        .icon_size(IconSize::Small)
 968                                        .shape(IconButtonShape::Square)
 969                                        .size(ButtonSize::Large)
 970                                        .tooltip(Tooltip::text("Delete Remote Project"))
 971                                        .on_click(cx.listener(move |this, _, _, cx| {
 972                                            this.delete_ssh_project(server_ix, &project, cx)
 973                                        }))
 974                                })
 975                                .into_any_element(),
 976                        ))
 977                    }),
 978            )
 979    }
 980
 981    fn update_settings_file(
 982        &mut self,
 983        cx: &mut Context<Self>,
 984        f: impl FnOnce(&mut RemoteSettingsContent, &App) + Send + Sync + 'static,
 985    ) {
 986        let Some(fs) = self
 987            .workspace
 988            .read_with(cx, |workspace, _| workspace.app_state().fs.clone())
 989            .log_err()
 990        else {
 991            return;
 992        };
 993        update_settings_file::<SshSettings>(fs, cx, move |setting, cx| f(setting, cx));
 994    }
 995
 996    fn delete_ssh_server(&mut self, server: usize, cx: &mut Context<Self>) {
 997        self.update_settings_file(cx, move |setting, _| {
 998            if let Some(connections) = setting.ssh_connections.as_mut() {
 999                connections.remove(server);
1000            }
1001        });
1002    }
1003
1004    fn delete_ssh_project(&mut self, server: usize, project: &SshProject, cx: &mut Context<Self>) {
1005        let project = project.clone();
1006        self.update_settings_file(cx, move |setting, _| {
1007            if let Some(server) = setting
1008                .ssh_connections
1009                .as_mut()
1010                .and_then(|connections| connections.get_mut(server))
1011            {
1012                server.projects.remove(&project);
1013            }
1014        });
1015    }
1016
1017    fn add_ssh_server(
1018        &mut self,
1019        connection_options: remote::SshConnectionOptions,
1020        cx: &mut Context<Self>,
1021    ) {
1022        self.update_settings_file(cx, move |setting, _| {
1023            setting
1024                .ssh_connections
1025                .get_or_insert(Default::default())
1026                .push(SshConnection {
1027                    host: SharedString::from(connection_options.host),
1028                    username: connection_options.username,
1029                    port: connection_options.port,
1030                    projects: BTreeSet::new(),
1031                    nickname: None,
1032                    args: connection_options.args.unwrap_or_default(),
1033                    upload_binary_over_ssh: None,
1034                    port_forwards: connection_options.port_forwards,
1035                })
1036        });
1037    }
1038
1039    fn render_create_remote_server(
1040        &self,
1041        state: &CreateRemoteServer,
1042        cx: &mut Context<Self>,
1043    ) -> impl IntoElement {
1044        let ssh_prompt = state.ssh_prompt.clone();
1045
1046        state.address_editor.update(cx, |editor, cx| {
1047            if editor.text(cx).is_empty() {
1048                editor.set_placeholder_text("ssh user@example -p 2222", cx);
1049            }
1050        });
1051
1052        let theme = cx.theme();
1053
1054        v_flex()
1055            .track_focus(&self.focus_handle(cx))
1056            .id("create-remote-server")
1057            .overflow_hidden()
1058            .size_full()
1059            .flex_1()
1060            .child(
1061                div()
1062                    .p_2()
1063                    .border_b_1()
1064                    .border_color(theme.colors().border_variant)
1065                    .child(state.address_editor.clone()),
1066            )
1067            .child(
1068                h_flex()
1069                    .bg(theme.colors().editor_background)
1070                    .rounded_b_sm()
1071                    .w_full()
1072                    .map(|this| {
1073                        if let Some(ssh_prompt) = ssh_prompt {
1074                            this.child(h_flex().w_full().child(ssh_prompt))
1075                        } else if let Some(address_error) = &state.address_error {
1076                            this.child(
1077                                h_flex().p_2().w_full().gap_2().child(
1078                                    Label::new(address_error.clone())
1079                                        .size(LabelSize::Small)
1080                                        .color(Color::Error),
1081                                ),
1082                            )
1083                        } else {
1084                            this.child(
1085                                h_flex()
1086                                    .p_2()
1087                                    .w_full()
1088                                    .gap_1()
1089                                    .child(
1090                                        Label::new(
1091                                            "Enter the command you use to SSH into this server.",
1092                                        )
1093                                        .color(Color::Muted)
1094                                        .size(LabelSize::Small),
1095                                    )
1096                                    .child(
1097                                        Button::new("learn-more", "Learn More")
1098                                            .label_size(LabelSize::Small)
1099                                            .icon(IconName::ArrowUpRight)
1100                                            .icon_size(IconSize::XSmall)
1101                                            .on_click(|_, _, cx| {
1102                                                cx.open_url(
1103                                                    "https://zed.dev/docs/remote-development",
1104                                                );
1105                                            }),
1106                                    ),
1107                            )
1108                        }
1109                    }),
1110            )
1111    }
1112
1113    fn render_view_options(
1114        &mut self,
1115        ViewServerOptionsState {
1116            server_index,
1117            connection,
1118            entries,
1119        }: ViewServerOptionsState,
1120        window: &mut Window,
1121        cx: &mut Context<Self>,
1122    ) -> impl IntoElement {
1123        let connection_string = connection.host.clone();
1124
1125        let mut view = Navigable::new(
1126            div()
1127                .track_focus(&self.focus_handle(cx))
1128                .size_full()
1129                .child(
1130                    SshConnectionHeader {
1131                        connection_string: connection_string.clone(),
1132                        paths: Default::default(),
1133                        nickname: connection.nickname.clone().map(|s| s.into()),
1134                    }
1135                    .render(window, cx),
1136                )
1137                .child(
1138                    v_flex()
1139                        .pb_1()
1140                        .child(ListSeparator)
1141                        .child({
1142                            let label = if connection.nickname.is_some() {
1143                                "Edit Nickname"
1144                            } else {
1145                                "Add Nickname to Server"
1146                            };
1147                            div()
1148                                .id("ssh-options-add-nickname")
1149                                .track_focus(&entries[0].focus_handle)
1150                                .on_action(cx.listener(
1151                                    move |this, _: &menu::Confirm, window, cx| {
1152                                        this.mode = Mode::EditNickname(EditNicknameState::new(
1153                                            server_index,
1154                                            window,
1155                                            cx,
1156                                        ));
1157                                        cx.notify();
1158                                    },
1159                                ))
1160                                .child(
1161                                    ListItem::new("add-nickname")
1162                                        .toggle_state(
1163                                            entries[0].focus_handle.contains_focused(window, cx),
1164                                        )
1165                                        .inset(true)
1166                                        .spacing(ui::ListItemSpacing::Sparse)
1167                                        .start_slot(Icon::new(IconName::Pencil).color(Color::Muted))
1168                                        .child(Label::new(label))
1169                                        .on_click(cx.listener(move |this, _, window, cx| {
1170                                            this.mode = Mode::EditNickname(EditNicknameState::new(
1171                                                server_index,
1172                                                window,
1173                                                cx,
1174                                            ));
1175                                            cx.notify();
1176                                        })),
1177                                )
1178                        })
1179                        .child({
1180                            let workspace = self.workspace.clone();
1181                            fn callback(
1182                                workspace: WeakEntity<Workspace>,
1183                                connection_string: SharedString,
1184                                cx: &mut App,
1185                            ) {
1186                                cx.write_to_clipboard(ClipboardItem::new_string(
1187                                    connection_string.to_string(),
1188                                ));
1189                                workspace
1190                                    .update(cx, |this, cx| {
1191                                        struct SshServerAddressCopiedToClipboard;
1192                                        let notification = format!(
1193                                            "Copied server address ({}) to clipboard",
1194                                            connection_string
1195                                        );
1196
1197                                        this.show_toast(
1198                                            Toast::new(
1199                                                NotificationId::composite::<
1200                                                    SshServerAddressCopiedToClipboard,
1201                                                >(
1202                                                    connection_string.clone()
1203                                                ),
1204                                                notification,
1205                                            )
1206                                            .autohide(),
1207                                            cx,
1208                                        );
1209                                    })
1210                                    .ok();
1211                            }
1212                            div()
1213                                .id("ssh-options-copy-server-address")
1214                                .track_focus(&entries[1].focus_handle)
1215                                .on_action({
1216                                    let connection_string = connection_string.clone();
1217                                    let workspace = self.workspace.clone();
1218                                    move |_: &menu::Confirm, _, cx| {
1219                                        callback(workspace.clone(), connection_string.clone(), cx);
1220                                    }
1221                                })
1222                                .child(
1223                                    ListItem::new("copy-server-address")
1224                                        .toggle_state(
1225                                            entries[1].focus_handle.contains_focused(window, cx),
1226                                        )
1227                                        .inset(true)
1228                                        .spacing(ui::ListItemSpacing::Sparse)
1229                                        .start_slot(Icon::new(IconName::Copy).color(Color::Muted))
1230                                        .child(Label::new("Copy Server Address"))
1231                                        .end_hover_slot(
1232                                            Label::new(connection_string.clone())
1233                                                .color(Color::Muted),
1234                                        )
1235                                        .on_click({
1236                                            let connection_string = connection_string.clone();
1237                                            move |_, _, cx| {
1238                                                callback(
1239                                                    workspace.clone(),
1240                                                    connection_string.clone(),
1241                                                    cx,
1242                                                );
1243                                            }
1244                                        }),
1245                                )
1246                        })
1247                        .child({
1248                            fn remove_ssh_server(
1249                                remote_servers: Entity<RemoteServerProjects>,
1250                                index: usize,
1251                                connection_string: SharedString,
1252                                window: &mut Window,
1253                                cx: &mut App,
1254                            ) {
1255                                let prompt_message =
1256                                    format!("Remove server `{}`?", connection_string);
1257
1258                                let confirmation = window.prompt(
1259                                    PromptLevel::Warning,
1260                                    &prompt_message,
1261                                    None,
1262                                    &["Yes, remove it", "No, keep it"],
1263                                    cx,
1264                                );
1265
1266                                cx.spawn(async move |cx| {
1267                                    if confirmation.await.ok() == Some(0) {
1268                                        remote_servers
1269                                            .update(cx, |this, cx| {
1270                                                this.delete_ssh_server(index, cx);
1271                                            })
1272                                            .ok();
1273                                        remote_servers
1274                                            .update(cx, |this, cx| {
1275                                                this.mode = Mode::default_mode(
1276                                                    &this.ssh_config_servers,
1277                                                    cx,
1278                                                );
1279                                                cx.notify();
1280                                            })
1281                                            .ok();
1282                                    }
1283                                    anyhow::Ok(())
1284                                })
1285                                .detach_and_log_err(cx);
1286                            }
1287                            div()
1288                                .id("ssh-options-copy-server-address")
1289                                .track_focus(&entries[2].focus_handle)
1290                                .on_action(cx.listener({
1291                                    let connection_string = connection_string.clone();
1292                                    move |_, _: &menu::Confirm, window, cx| {
1293                                        remove_ssh_server(
1294                                            cx.entity(),
1295                                            server_index,
1296                                            connection_string.clone(),
1297                                            window,
1298                                            cx,
1299                                        );
1300                                        cx.focus_self(window);
1301                                    }
1302                                }))
1303                                .child(
1304                                    ListItem::new("remove-server")
1305                                        .toggle_state(
1306                                            entries[2].focus_handle.contains_focused(window, cx),
1307                                        )
1308                                        .inset(true)
1309                                        .spacing(ui::ListItemSpacing::Sparse)
1310                                        .start_slot(Icon::new(IconName::Trash).color(Color::Error))
1311                                        .child(Label::new("Remove Server").color(Color::Error))
1312                                        .on_click(cx.listener(move |_, _, window, cx| {
1313                                            remove_ssh_server(
1314                                                cx.entity(),
1315                                                server_index,
1316                                                connection_string.clone(),
1317                                                window,
1318                                                cx,
1319                                            );
1320                                            cx.focus_self(window);
1321                                        })),
1322                                )
1323                        })
1324                        .child(ListSeparator)
1325                        .child({
1326                            div()
1327                                .id("ssh-options-copy-server-address")
1328                                .track_focus(&entries[3].focus_handle)
1329                                .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| {
1330                                    this.mode = Mode::default_mode(&this.ssh_config_servers, cx);
1331                                    cx.focus_self(window);
1332                                    cx.notify();
1333                                }))
1334                                .child(
1335                                    ListItem::new("go-back")
1336                                        .toggle_state(
1337                                            entries[3].focus_handle.contains_focused(window, cx),
1338                                        )
1339                                        .inset(true)
1340                                        .spacing(ui::ListItemSpacing::Sparse)
1341                                        .start_slot(
1342                                            Icon::new(IconName::ArrowLeft).color(Color::Muted),
1343                                        )
1344                                        .child(Label::new("Go Back"))
1345                                        .on_click(cx.listener(|this, _, window, cx| {
1346                                            this.mode =
1347                                                Mode::default_mode(&this.ssh_config_servers, cx);
1348                                            cx.focus_self(window);
1349                                            cx.notify()
1350                                        })),
1351                                )
1352                        }),
1353                )
1354                .into_any_element(),
1355        );
1356        for entry in entries {
1357            view = view.entry(entry);
1358        }
1359
1360        view.render(window, cx).into_any_element()
1361    }
1362
1363    fn render_edit_nickname(
1364        &self,
1365        state: &EditNicknameState,
1366        window: &mut Window,
1367        cx: &mut Context<Self>,
1368    ) -> impl IntoElement {
1369        let Some(connection) = SshSettings::get_global(cx)
1370            .ssh_connections()
1371            .nth(state.index)
1372        else {
1373            return v_flex()
1374                .id("ssh-edit-nickname")
1375                .track_focus(&self.focus_handle(cx));
1376        };
1377
1378        let connection_string = connection.host.clone();
1379        let nickname = connection.nickname.map(|s| s.into());
1380
1381        v_flex()
1382            .id("ssh-edit-nickname")
1383            .track_focus(&self.focus_handle(cx))
1384            .child(
1385                SshConnectionHeader {
1386                    connection_string,
1387                    paths: Default::default(),
1388                    nickname,
1389                }
1390                .render(window, cx),
1391            )
1392            .child(
1393                h_flex()
1394                    .p_2()
1395                    .border_t_1()
1396                    .border_color(cx.theme().colors().border_variant)
1397                    .child(state.editor.clone()),
1398            )
1399    }
1400
1401    fn render_default(
1402        &mut self,
1403        mut state: DefaultState,
1404        window: &mut Window,
1405        cx: &mut Context<Self>,
1406    ) -> impl IntoElement {
1407        let ssh_settings = SshSettings::get_global(cx);
1408        let mut should_rebuild = false;
1409
1410        if ssh_settings
1411            .ssh_connections
1412            .as_ref()
1413            .is_some_and(|connections| {
1414                state
1415                    .servers
1416                    .iter()
1417                    .filter_map(|server| match server {
1418                        RemoteEntry::Project { connection, .. } => Some(connection),
1419                        RemoteEntry::SshConfig { .. } => None,
1420                    })
1421                    .ne(connections.iter())
1422            })
1423        {
1424            should_rebuild = true;
1425        };
1426
1427        if !should_rebuild && ssh_settings.read_ssh_config {
1428            let current_ssh_hosts: BTreeSet<SharedString> = state
1429                .servers
1430                .iter()
1431                .filter_map(|server| match server {
1432                    RemoteEntry::SshConfig { host, .. } => Some(host.clone()),
1433                    _ => None,
1434                })
1435                .collect();
1436            let mut expected_ssh_hosts = self.ssh_config_servers.clone();
1437            for server in &state.servers {
1438                if let RemoteEntry::Project { connection, .. } = server {
1439                    expected_ssh_hosts.remove(&connection.host);
1440                }
1441            }
1442            should_rebuild = current_ssh_hosts != expected_ssh_hosts;
1443        }
1444
1445        if should_rebuild {
1446            self.mode = Mode::default_mode(&self.ssh_config_servers, cx);
1447            if let Mode::Default(new_state) = &self.mode {
1448                state = new_state.clone();
1449            }
1450        }
1451
1452        let scroll_state = state.scrollbar.parent_entity(&cx.entity());
1453        let connect_button = div()
1454            .id("ssh-connect-new-server-container")
1455            .track_focus(&state.add_new_server.focus_handle)
1456            .anchor_scroll(state.add_new_server.scroll_anchor.clone())
1457            .child(
1458                ListItem::new("register-remove-server-button")
1459                    .toggle_state(
1460                        state
1461                            .add_new_server
1462                            .focus_handle
1463                            .contains_focused(window, cx),
1464                    )
1465                    .inset(true)
1466                    .spacing(ui::ListItemSpacing::Sparse)
1467                    .start_slot(Icon::new(IconName::Plus).color(Color::Muted))
1468                    .child(Label::new("Connect New Server"))
1469                    .on_click(cx.listener(|this, _, window, cx| {
1470                        let state = CreateRemoteServer::new(window, cx);
1471                        this.mode = Mode::CreateRemoteServer(state);
1472
1473                        cx.notify();
1474                    })),
1475            )
1476            .on_action(cx.listener(|this, _: &menu::Confirm, window, cx| {
1477                let state = CreateRemoteServer::new(window, cx);
1478                this.mode = Mode::CreateRemoteServer(state);
1479
1480                cx.notify();
1481            }));
1482
1483        let handle = &**scroll_state.scroll_handle() as &dyn Any;
1484        let Some(scroll_handle) = handle.downcast_ref::<ScrollHandle>() else {
1485            unreachable!()
1486        };
1487
1488        let mut modal_section = Navigable::new(
1489            v_flex()
1490                .track_focus(&self.focus_handle(cx))
1491                .id("ssh-server-list")
1492                .overflow_y_scroll()
1493                .track_scroll(scroll_handle)
1494                .size_full()
1495                .child(connect_button)
1496                .child(
1497                    List::new()
1498                        .empty_message(
1499                            v_flex()
1500                                .child(
1501                                    div().px_3().child(
1502                                        Label::new("No remote servers registered yet.")
1503                                            .color(Color::Muted),
1504                                    ),
1505                                )
1506                                .into_any_element(),
1507                        )
1508                        .children(state.servers.iter().enumerate().map(|(ix, connection)| {
1509                            self.render_ssh_connection(ix, connection.clone(), window, cx)
1510                                .into_any_element()
1511                        })),
1512                )
1513                .into_any_element(),
1514        )
1515        .entry(state.add_new_server.clone());
1516
1517        for server in &state.servers {
1518            match server {
1519                RemoteEntry::Project {
1520                    open_folder,
1521                    projects,
1522                    configure,
1523                    ..
1524                } => {
1525                    for (navigation_state, _) in projects {
1526                        modal_section = modal_section.entry(navigation_state.clone());
1527                    }
1528                    modal_section = modal_section
1529                        .entry(open_folder.clone())
1530                        .entry(configure.clone());
1531                }
1532                RemoteEntry::SshConfig { open_folder, .. } => {
1533                    modal_section = modal_section.entry(open_folder.clone());
1534                }
1535            }
1536        }
1537        let mut modal_section = modal_section.render(window, cx).into_any_element();
1538
1539        let (create_window, reuse_window) = if self.create_new_window {
1540            (
1541                window.keystroke_text_for(&menu::Confirm),
1542                window.keystroke_text_for(&menu::SecondaryConfirm),
1543            )
1544        } else {
1545            (
1546                window.keystroke_text_for(&menu::SecondaryConfirm),
1547                window.keystroke_text_for(&menu::Confirm),
1548            )
1549        };
1550        let placeholder_text = Arc::from(format!(
1551            "{reuse_window} reuses this window, {create_window} opens a new one",
1552        ));
1553
1554        Modal::new("remote-projects", None)
1555            .header(
1556                ModalHeader::new()
1557                    .child(Headline::new("Remote Projects").size(HeadlineSize::XSmall))
1558                    .child(
1559                        Label::new(placeholder_text)
1560                            .color(Color::Muted)
1561                            .size(LabelSize::XSmall),
1562                    ),
1563            )
1564            .section(
1565                Section::new().padded(false).child(
1566                    v_flex()
1567                        .min_h(rems(20.))
1568                        .size_full()
1569                        .relative()
1570                        .child(ListSeparator)
1571                        .child(
1572                            canvas(
1573                                |bounds, window, cx| {
1574                                    modal_section.prepaint_as_root(
1575                                        bounds.origin,
1576                                        bounds.size.into(),
1577                                        window,
1578                                        cx,
1579                                    );
1580                                    modal_section
1581                                },
1582                                |_, mut modal_section, window, cx| {
1583                                    modal_section.paint(window, cx);
1584                                },
1585                            )
1586                            .size_full(),
1587                        )
1588                        .child(
1589                            div()
1590                                .occlude()
1591                                .h_full()
1592                                .absolute()
1593                                .top_1()
1594                                .bottom_1()
1595                                .right_1()
1596                                .w(px(8.))
1597                                .children(Scrollbar::vertical(scroll_state)),
1598                        ),
1599                ),
1600            )
1601            .into_any_element()
1602    }
1603
1604    fn create_host_from_ssh_config(
1605        &mut self,
1606        ssh_config_host: &SharedString,
1607        cx: &mut Context<'_, Self>,
1608    ) -> usize {
1609        let new_ix = Arc::new(AtomicUsize::new(0));
1610
1611        let update_new_ix = new_ix.clone();
1612        self.update_settings_file(cx, move |settings, _| {
1613            update_new_ix.store(
1614                settings
1615                    .ssh_connections
1616                    .as_ref()
1617                    .map_or(0, |connections| connections.len()),
1618                atomic::Ordering::Release,
1619            );
1620        });
1621
1622        self.add_ssh_server(
1623            SshConnectionOptions {
1624                host: ssh_config_host.to_string(),
1625                ..SshConnectionOptions::default()
1626            },
1627            cx,
1628        );
1629        self.mode = Mode::default_mode(&self.ssh_config_servers, cx);
1630        new_ix.load(atomic::Ordering::Acquire)
1631    }
1632}
1633
1634fn spawn_ssh_config_watch(fs: Arc<dyn Fs>, cx: &Context<RemoteServerProjects>) -> Task<()> {
1635    let mut user_ssh_config_watcher =
1636        watch_config_file(cx.background_executor(), fs.clone(), user_ssh_config_file());
1637    let mut global_ssh_config_watcher = watch_config_file(
1638        cx.background_executor(),
1639        fs,
1640        global_ssh_config_file().to_owned(),
1641    );
1642
1643    cx.spawn(async move |remote_server_projects, cx| {
1644        let mut global_hosts = BTreeSet::default();
1645        let mut user_hosts = BTreeSet::default();
1646        let mut running_receivers = 2;
1647
1648        loop {
1649            select! {
1650                new_global_file_contents = global_ssh_config_watcher.next().fuse() => {
1651                    match new_global_file_contents {
1652                        Some(new_global_file_contents) => {
1653                            global_hosts = parse_ssh_config_hosts(&new_global_file_contents);
1654                            if remote_server_projects.update(cx, |remote_server_projects, cx| {
1655                                remote_server_projects.ssh_config_servers = global_hosts.iter().chain(user_hosts.iter()).map(SharedString::from).collect();
1656                                cx.notify();
1657                            }).is_err() {
1658                                return;
1659                            }
1660                        },
1661                        None => {
1662                            running_receivers -= 1;
1663                            if running_receivers == 0 {
1664                                return;
1665                            }
1666                        }
1667                    }
1668                },
1669                new_user_file_contents = user_ssh_config_watcher.next().fuse() => {
1670                    match new_user_file_contents {
1671                        Some(new_user_file_contents) => {
1672                            user_hosts = parse_ssh_config_hosts(&new_user_file_contents);
1673                            if remote_server_projects.update(cx, |remote_server_projects, cx| {
1674                                remote_server_projects.ssh_config_servers = global_hosts.iter().chain(user_hosts.iter()).map(SharedString::from).collect();
1675                                cx.notify();
1676                            }).is_err() {
1677                                return;
1678                            }
1679                        },
1680                        None => {
1681                            running_receivers -= 1;
1682                            if running_receivers == 0 {
1683                                return;
1684                            }
1685                        }
1686                    }
1687                },
1688            }
1689        }
1690    })
1691}
1692
1693fn get_text(element: &Entity<Editor>, cx: &mut App) -> String {
1694    element.read(cx).text(cx).trim().to_string()
1695}
1696
1697impl ModalView for RemoteServerProjects {}
1698
1699impl Focusable for RemoteServerProjects {
1700    fn focus_handle(&self, cx: &App) -> FocusHandle {
1701        match &self.mode {
1702            Mode::ProjectPicker(picker) => picker.focus_handle(cx),
1703            _ => self.focus_handle.clone(),
1704        }
1705    }
1706}
1707
1708impl EventEmitter<DismissEvent> for RemoteServerProjects {}
1709
1710impl Render for RemoteServerProjects {
1711    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1712        div()
1713            .elevation_3(cx)
1714            .w(rems(34.))
1715            .key_context("RemoteServerModal")
1716            .on_action(cx.listener(Self::cancel))
1717            .on_action(cx.listener(Self::confirm))
1718            .capture_any_mouse_down(cx.listener(|this, _, window, cx| {
1719                this.focus_handle(cx).focus(window);
1720            }))
1721            .on_mouse_down_out(cx.listener(|this, _, _, cx| {
1722                if matches!(this.mode, Mode::Default(_)) {
1723                    cx.emit(DismissEvent)
1724                }
1725            }))
1726            .child(match &self.mode {
1727                Mode::Default(state) => self
1728                    .render_default(state.clone(), window, cx)
1729                    .into_any_element(),
1730                Mode::ViewServerOptions(state) => self
1731                    .render_view_options(state.clone(), window, cx)
1732                    .into_any_element(),
1733                Mode::ProjectPicker(element) => element.clone().into_any_element(),
1734                Mode::CreateRemoteServer(state) => self
1735                    .render_create_remote_server(state, cx)
1736                    .into_any_element(),
1737                Mode::EditNickname(state) => self
1738                    .render_edit_nickname(state, window, cx)
1739                    .into_any_element(),
1740            })
1741    }
1742}