subspace_shell/
startup_failure.rs1use 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
23pub 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 cx.on_window_closed(|cx, _| cx.quit()).detach();
54 cx.activate(true);
55 });
56 ExitCode::FAILURE
57}
58
59pub(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}