disconnected_overlay.rs

  1use gpui::{ClickEvent, DismissEvent, EventEmitter, FocusHandle, Focusable, Render, WeakEntity};
  2use project::project_settings::ProjectSettings;
  3use remote::SshConnectionOptions;
  4use settings::Settings;
  5use ui::{
  6    Button, ButtonCommon, ButtonStyle, Clickable, Context, ElevationIndex, FluentBuilder, Headline,
  7    HeadlineSize, IconName, IconPosition, InteractiveElement, IntoElement, Label, Modal,
  8    ModalFooter, ModalHeader, ParentElement, Section, Styled, StyledExt, Window, div, h_flex, rems,
  9};
 10use workspace::{ModalView, OpenOptions, Workspace, notifications::DetachAndPromptErr};
 11
 12use crate::open_ssh_project;
 13
 14enum Host {
 15    RemoteProject,
 16    SshRemoteProject(SshConnectionOptions),
 17}
 18
 19pub struct DisconnectedOverlay {
 20    workspace: WeakEntity<Workspace>,
 21    host: Host,
 22    focus_handle: FocusHandle,
 23    finished: bool,
 24}
 25
 26impl EventEmitter<DismissEvent> for DisconnectedOverlay {}
 27impl Focusable for DisconnectedOverlay {
 28    fn focus_handle(&self, _cx: &gpui::App) -> gpui::FocusHandle {
 29        self.focus_handle.clone()
 30    }
 31}
 32impl ModalView for DisconnectedOverlay {
 33    fn on_before_dismiss(
 34        &mut self,
 35        _window: &mut Window,
 36        _: &mut Context<Self>,
 37    ) -> workspace::DismissDecision {
 38        workspace::DismissDecision::Dismiss(self.finished)
 39    }
 40    fn fade_out_background(&self) -> bool {
 41        true
 42    }
 43}
 44
 45impl DisconnectedOverlay {
 46    pub fn register(
 47        workspace: &mut Workspace,
 48        window: Option<&mut Window>,
 49        cx: &mut Context<Workspace>,
 50    ) {
 51        let Some(window) = window else {
 52            return;
 53        };
 54        cx.subscribe_in(
 55            workspace.project(),
 56            window,
 57            |workspace, project, event, window, cx| {
 58                if !matches!(
 59                    event,
 60                    project::Event::DisconnectedFromHost
 61                        | project::Event::DisconnectedFromSshRemote
 62                ) {
 63                    return;
 64                }
 65                let handle = cx.entity().downgrade();
 66
 67                let remote_connection_options = project.read(cx).remote_connection_options(cx);
 68                let host = if let Some(ssh_connection_options) = remote_connection_options {
 69                    Host::SshRemoteProject(ssh_connection_options)
 70                } else {
 71                    Host::RemoteProject
 72                };
 73
 74                workspace.toggle_modal(window, cx, |_, cx| DisconnectedOverlay {
 75                    finished: false,
 76                    workspace: handle,
 77                    host,
 78                    focus_handle: cx.focus_handle(),
 79                });
 80            },
 81        )
 82        .detach();
 83    }
 84
 85    fn handle_reconnect(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
 86        self.finished = true;
 87        cx.emit(DismissEvent);
 88
 89        if let Host::SshRemoteProject(ssh_connection_options) = &self.host {
 90            self.reconnect_to_ssh_remote(ssh_connection_options.clone(), window, cx);
 91        }
 92    }
 93
 94    fn reconnect_to_ssh_remote(
 95        &self,
 96        connection_options: SshConnectionOptions,
 97        window: &mut Window,
 98        cx: &mut Context<Self>,
 99    ) {
100        let Some(workspace) = self.workspace.upgrade() else {
101            return;
102        };
103
104        let Some(window_handle) = window.window_handle().downcast::<Workspace>() else {
105            return;
106        };
107
108        let app_state = workspace.read(cx).app_state().clone();
109        let paths = workspace
110            .read(cx)
111            .root_paths(cx)
112            .iter()
113            .map(|path| path.to_path_buf())
114            .collect();
115
116        cx.spawn_in(window, async move |_, cx| {
117            open_ssh_project(
118                connection_options,
119                paths,
120                app_state,
121                OpenOptions {
122                    replace_window: Some(window_handle),
123                    ..Default::default()
124                },
125                cx,
126            )
127            .await?;
128            Ok(())
129        })
130        .detach_and_prompt_err("Failed to reconnect", window, cx, |_, _, _| None);
131    }
132
133    fn cancel(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
134        self.finished = true;
135        cx.emit(DismissEvent)
136    }
137}
138
139impl Render for DisconnectedOverlay {
140    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
141        let can_reconnect = matches!(self.host, Host::SshRemoteProject(_));
142
143        let message = match &self.host {
144            Host::RemoteProject => {
145                "Your connection to the remote project has been lost.".to_string()
146            }
147            Host::SshRemoteProject(options) => {
148                let autosave = if ProjectSettings::get_global(cx)
149                    .session
150                    .restore_unsaved_buffers
151                {
152                    "\nUnsaved changes are stored locally."
153                } else {
154                    ""
155                };
156                format!(
157                    "Your connection to {} has been lost.{}",
158                    options.host, autosave
159                )
160            }
161        };
162
163        div()
164            .track_focus(&self.focus_handle(cx))
165            .elevation_3(cx)
166            .on_action(cx.listener(Self::cancel))
167            .occlude()
168            .w(rems(24.))
169            .max_h(rems(40.))
170            .child(
171                Modal::new("disconnected", None)
172                    .header(
173                        ModalHeader::new()
174                            .show_dismiss_button(true)
175                            .child(Headline::new("Disconnected").size(HeadlineSize::Small)),
176                    )
177                    .section(Section::new().child(Label::new(message)))
178                    .footer(
179                        ModalFooter::new().end_slot(
180                            h_flex()
181                                .gap_2()
182                                .child(
183                                    Button::new("close-window", "Close Window")
184                                        .style(ButtonStyle::Filled)
185                                        .layer(ElevationIndex::ModalSurface)
186                                        .on_click(cx.listener(move |_, _, window, _| {
187                                            window.remove_window();
188                                        })),
189                                )
190                                .when(can_reconnect, |el| {
191                                    el.child(
192                                        Button::new("reconnect", "Reconnect")
193                                            .style(ButtonStyle::Filled)
194                                            .layer(ElevationIndex::ModalSurface)
195                                            .icon(IconName::ArrowCircle)
196                                            .icon_position(IconPosition::Start)
197                                            .on_click(cx.listener(Self::handle_reconnect)),
198                                    )
199                                }),
200                        ),
201                    ),
202            )
203    }
204}