Skip to main content

subspace_shell/
startup_failure.rs

1//! A start-up failure a person can see. An app that cannot start, because a
2//! setting it reads before the shell is wrong, opens one small window
3//! saying what is wrong and where, with Quit, instead of only writing to a
4//! terminal that someone who opened it from the Finder, the Dock or the
5//! Start menu never sees.
6
7use std::process::ExitCode;
8
9use gpui_kit::component::v_flex;
10use gpui_kit::{
11    AppContext as _, Context, FontWeight, InteractiveElement as _, IntoElement, ParentElement as _,
12    Render, SharedString, Styled as _, TestSupportExt as _, TitlebarOptions, Window, WindowBounds,
13    WindowOptions, div, px, size,
14};
15use subspace_dashboard::Tokens;
16use subspace_dashboard::action::{Intent, dialog_button};
17use subspace_dashboard::dialog;
18use subspace_lucide::IconName;
19
20const WIDTH: f32 = 560.;
21const HEIGHT: f32 = 260.;
22
23/// Writes `title` and `detail` to standard error and shows them in a window
24/// titled `app_name` until the person quits or closes it. Returns the
25/// failure exit code where the event loop returns (Linux and Windows); on
26/// macOS quitting ends the process from inside the loop, with exit code 0.
27// qual:api - a product calls it when its own start-up checks fail.
28pub fn report_startup_failure(app_name: &str, title: &str, detail: &str) -> ExitCode {
29    eprintln!("{title}\n{detail}");
30    let (name, title, detail) = (
31        SharedString::from(app_name.to_owned()),
32        SharedString::from(title.to_owned()),
33        SharedString::from(detail.to_owned()),
34    );
35    gpui_kit::application().run(move |cx| {
36        gpui_kit::init(cx);
37        let options = WindowOptions {
38            window_bounds: Some(WindowBounds::centered(size(px(WIDTH), px(HEIGHT)), cx)),
39            titlebar: Some(TitlebarOptions {
40                title: Some(name.clone()),
41                ..Default::default()
42            }),
43            ..Default::default()
44        };
45        let opened = gpui_kit::open_window(options, cx, |_, cx| {
46            cx.new(|_| StartupFailure::new(title.clone(), detail.clone()))
47        });
48        if opened.is_err() {
49            cx.quit();
50        }
51        // Closing the only window quits too, rather than leaving a process
52        // with no window in the Dock.
53        cx.on_window_closed(|cx, _| cx.quit()).detach();
54        cx.activate(true);
55    });
56    ExitCode::FAILURE
57}
58
59/// The window's one view: what failed, the detail, and Quit.
60pub(crate) struct StartupFailure {
61    title: SharedString,
62    detail: SharedString,
63}
64
65impl StartupFailure {
66    pub(crate) fn new(title: SharedString, detail: SharedString) -> Self {
67        Self { title, detail }
68    }
69}
70
71impl Render for StartupFailure {
72    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
73        let tokens = Tokens::current(cx);
74        v_flex()
75            .id("startup-failure")
76            .test_support()
77            .size_full()
78            .p_6()
79            .gap_3()
80            .bg(tokens.bg)
81            .text_color(tokens.fg)
82            .child(
83                div()
84                    .text_size(tokens.heading_font)
85                    .font_weight(FontWeight::SEMIBOLD)
86                    .text_color(tokens.fg_strong)
87                    .child(self.title.clone()),
88            )
89            .child(div().flex_1().child(self.detail.clone()))
90            .child(
91                dialog::footer().child(
92                    dialog_button(
93                        "startup-failure-quit",
94                        Intent::Primary,
95                        Some(IconName::LogOut),
96                        "Quit",
97                    )
98                    .on_click(|_, _, cx| cx.quit()),
99                ),
100            )
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use gpui_kit::test::TestWindowExt as _;
107    use gpui_kit::{AppContext as _, TestAppContext};
108
109    use super::StartupFailure;
110
111    #[gpui_kit::test]
112    fn the_failure_says_what_and_where_and_offers_quit(cx: &mut TestAppContext) {
113        cx.update(gpui_kit::init);
114        let window = cx.add_window(|_, _| {
115            StartupFailure::new(
116                "Subspace cannot start".into(),
117                "connection.json: api_url must be HTTPS.".into(),
118            )
119        });
120        cx.update_window(window.into(), |_, window, cx| {
121            window.render_frame(cx);
122            assert!(window.try_find("startup-failure").is_some());
123            assert_eq!(window.find("startup-failure-quit").label(), Some("Quit"));
124        })
125        .unwrap();
126    }
127}