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. Returns the failure exit code.
25// qual:api - a product calls it when its own start-up checks fail.
26pub fn report_startup_failure(app_name: &str, title: &str, detail: &str) -> ExitCode {
27    eprintln!("{title}\n{detail}");
28    let (name, title, detail) = (
29        SharedString::from(app_name.to_owned()),
30        SharedString::from(title.to_owned()),
31        SharedString::from(detail.to_owned()),
32    );
33    gpui_kit::application().run(move |cx| {
34        gpui_kit::init(cx);
35        let options = WindowOptions {
36            window_bounds: Some(WindowBounds::centered(size(px(WIDTH), px(HEIGHT)), cx)),
37            titlebar: Some(TitlebarOptions {
38                title: Some(name.clone()),
39                ..Default::default()
40            }),
41            ..Default::default()
42        };
43        let opened = gpui_kit::open_window(options, cx, |_, cx| {
44            cx.new(|_| StartupFailure::new(title.clone(), detail.clone()))
45        });
46        if opened.is_err() {
47            cx.quit();
48        }
49        cx.activate(true);
50    });
51    ExitCode::FAILURE
52}
53
54/// The window's one view: what failed, the detail, and Quit.
55pub(crate) struct StartupFailure {
56    title: SharedString,
57    detail: SharedString,
58}
59
60impl StartupFailure {
61    pub(crate) fn new(title: SharedString, detail: SharedString) -> Self {
62        Self { title, detail }
63    }
64}
65
66impl Render for StartupFailure {
67    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
68        let tokens = Tokens::current(cx);
69        v_flex()
70            .id("startup-failure")
71            .test_support()
72            .size_full()
73            .p_6()
74            .gap_3()
75            .bg(tokens.bg)
76            .text_color(tokens.fg)
77            .child(
78                div()
79                    .text_size(tokens.heading_font)
80                    .font_weight(FontWeight::SEMIBOLD)
81                    .text_color(tokens.fg_strong)
82                    .child(self.title.clone()),
83            )
84            .child(div().flex_1().child(self.detail.clone()))
85            .child(
86                dialog::footer().child(
87                    dialog_button(
88                        "startup-failure-quit",
89                        Intent::Primary,
90                        Some(IconName::LogOut),
91                        "Quit",
92                    )
93                    .on_click(|_, _, cx| cx.quit()),
94                ),
95            )
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use gpui_kit::test::TestWindowExt as _;
102    use gpui_kit::{AppContext as _, TestAppContext};
103
104    use super::StartupFailure;
105
106    #[gpui_kit::test]
107    fn the_failure_says_what_and_where_and_offers_quit(cx: &mut TestAppContext) {
108        cx.update(gpui_kit::init);
109        let window = cx.add_window(|_, _| {
110            StartupFailure::new(
111                "Subspace cannot start".into(),
112                "connection.json: api_url must be HTTPS.".into(),
113            )
114        });
115        cx.update_window(window.into(), |_, window, cx| {
116            window.render_frame(cx);
117            assert!(window.try_find("startup-failure").is_some());
118            assert_eq!(window.find("startup-failure-quit").label(), Some("Quit"));
119        })
120        .unwrap();
121    }
122}