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(
39                None,
40                size(px(WIDTH), px(HEIGHT)),
41                cx,
42            )),
43            titlebar: Some(TitlebarOptions {
44                title: Some(name.clone()),
45                ..Default::default()
46            }),
47            ..Default::default()
48        };
49        let opened = gpui_kit::open_window(options, cx, |_, cx| {
50            cx.new(|_| StartupFailure::new(title.clone(), detail.clone()))
51        });
52        if opened.is_err() {
53            cx.quit();
54        }
55        // Closing the only window quits too, rather than leaving a process
56        // with no window in the Dock.
57        cx.on_window_closed(|cx, _| cx.quit()).detach();
58        cx.activate(true);
59    });
60    ExitCode::FAILURE
61}
62
63/// The window's one view: what failed, the detail, and Quit.
64pub(crate) struct StartupFailure {
65    title: SharedString,
66    detail: SharedString,
67}
68
69impl StartupFailure {
70    pub(crate) fn new(title: SharedString, detail: SharedString) -> Self {
71        Self { title, detail }
72    }
73}
74
75impl Render for StartupFailure {
76    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
77        let tokens = Tokens::current(cx);
78        v_flex()
79            .id("startup-failure")
80            .test_support()
81            .size_full()
82            .p_6()
83            .gap_3()
84            .bg(tokens.bg)
85            .text_color(tokens.fg)
86            .child(
87                div()
88                    .text_size(tokens.heading_font)
89                    .font_weight(FontWeight::SEMIBOLD)
90                    .text_color(tokens.fg_strong)
91                    .child(self.title.clone()),
92            )
93            .child(div().flex_1().child(self.detail.clone()))
94            .child(
95                dialog::footer().child(
96                    dialog_button(
97                        "startup-failure-quit",
98                        Intent::Primary,
99                        Some(IconName::LogOut),
100                        "Quit",
101                    )
102                    .on_click(|_, _, cx| cx.quit()),
103                ),
104            )
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use gpui_kit::test::TestWindowExt as _;
111    use gpui_kit::{AppContext as _, TestAppContext};
112
113    use super::StartupFailure;
114
115    #[gpui_kit::test]
116    fn the_failure_says_what_and_where_and_offers_quit(cx: &mut TestAppContext) {
117        cx.update(gpui_kit::init);
118        let window = cx.add_window(|_, _| {
119            StartupFailure::new(
120                "Subspace cannot start".into(),
121                "connection.json: api_url must be HTTPS.".into(),
122            )
123        });
124        cx.update_window(window.into(), |_, window, cx| {
125            window.render_frame(cx);
126            assert!(window.try_find("startup-failure").is_some());
127            assert_eq!(window.find("startup-failure-quit").label(), Some("Quit"));
128        })
129        .unwrap();
130    }
131}