ui/widgets/status.rs
1//! Status surfaces — the step row and its output, and the alert strips.
2//!
3//! A catalog trait, like every widget group: import it to unlock
4//! `theme.step_row(..)`, `theme.step_output(..)`, `theme.error_strip(..)`.
5
6use gpui::{Div, SharedString, div, prelude::*, px};
7use icons::Icon;
8use theme::{TextStyle, Theme, ThemeExt, Typeset};
9
10use crate::{stack, widgets::Layout};
11
12/// Padding inside a [`Status::step_row`] and the [`Status::step_output`] under
13/// it — the two have to agree or the output's first character sits left of the
14/// title.
15const STEP_PAD_X: f32 = 10.0;
16const STEP_PAD_Y: f32 = 6.0;
17/// How tall an output may get before it scrolls (`ToolCard.svelte`'s
18/// `max-h-64`).
19const STEP_OUTPUT_MAX: f32 = 256.0;
20
21pub trait Status: ThemeExt {
22 /// One operation, as a row: an icon, what it was, and how it went.
23 ///
24 /// A tool call in a transcript, a step in a CI run, a file in a migration —
25 /// the shape is the same everywhere, which is why this takes strings and
26 /// not a type that knows what any of them mean. `detail` is the truncating
27 /// middle (a query, a path, a `· 3` count), `meta` the right-aligned figure
28 /// that never truncates (a duration, a size, a row count).
29 ///
30 /// `expanded` is `None` when there is nothing under the row, and the
31 /// chevron is simply absent — a disclosure that opens onto nothing is worse
32 /// than no disclosure. `Some` renders it, and the caller owns the flag.
33 ///
34 /// Returns a plain `Div` like the rest of this module: the caller adds
35 /// `.id(..)` and `.on_click(..)` **to this row**, never to a wrapper around
36 /// it, or the hitbox ends up narrower than what it paints. Hover is
37 /// caller-owned (gpui panics on a second hover); the default wash is
38 /// [`Theme::element_hover`].
39 fn step_row(
40 &self,
41 icon: impl Into<Icon>,
42 title: impl Into<SharedString>,
43 detail: Option<SharedString>,
44 meta: Option<SharedString>,
45 failed: bool,
46 expanded: Option<bool>,
47 ) -> Div {
48 let theme = self.theme();
49 stack::row()
50 .px(px(STEP_PAD_X))
51 .py(px(STEP_PAD_Y))
52 .cursor_pointer()
53 .child(
54 // Tinted here rather than on the row: gpui reads an svg's colour
55 // off its own style, so a colour set on the parent never arrives.
56 crate::icons::icon(icon)
57 .size(px(14.0))
58 .text_color(if failed {
59 theme.danger
60 } else {
61 theme.text_muted
62 }),
63 )
64 .child(
65 div()
66 .flex_none()
67 .text_style(TextStyle::Callout)
68 .text_color(theme.text)
69 .child(title.into()),
70 )
71 .when_some(detail, |row, detail| {
72 row.child(
73 div()
74 .min_w_0()
75 .truncate()
76 .text_style(TextStyle::Callout)
77 .text_color(theme.text_muted)
78 .child(detail),
79 )
80 })
81 .child(
82 div()
83 .ml_auto()
84 .flex_none()
85 .flex()
86 .flex_row()
87 .items_center()
88 .gap(px(6.0))
89 .when_some(meta, |cluster, meta| {
90 cluster.child(
91 div()
92 .font_family(theme.font_mono.clone())
93 .text_style(TextStyle::Callout)
94 .text_color(theme.text_muted)
95 .child(meta),
96 )
97 })
98 .when_some(expanded, |cluster, expanded| {
99 cluster.child(Layout::disclosure(theme, expanded))
100 }),
101 )
102 }
103
104 /// What a [`Self::step_row`] opens onto: its output, verbatim.
105 ///
106 /// Monospaced and capped, because the thing being shown is a program's
107 /// stdout and the row it hangs off is one line tall — a 900-line stack
108 /// trace pushing the next step off screen is the failure this cap exists
109 /// for. Past the cap it scrolls, which is why it takes an id.
110 ///
111 /// No scrollbar: the wheel reaches it regardless, and a bar would need a
112 /// `ScrollHandle` and a `ScrollbarState` from every caller for a box that
113 /// is usually four lines long. Wrap it in `div().relative()` with
114 /// [`crate::scroll::scrollbar`] over it if a particular one earns the bar.
115 fn step_output(
116 &self,
117 id: impl Into<gpui::ElementId>,
118 text: impl Into<SharedString>,
119 ) -> gpui::Stateful<Div> {
120 let theme = self.theme();
121 crate::scroll::pane(id, crate::scroll::Axes::Vertical)
122 .max_h(px(STEP_OUTPUT_MAX))
123 .border_t_1()
124 .border_color(theme.border)
125 .px(px(STEP_PAD_X))
126 .py(px(STEP_PAD_Y))
127 .font_family(theme.font_mono.clone())
128 .text_style(TextStyle::Callout)
129 .text_color(theme.text_muted)
130 .child(text.into())
131 }
132
133 /// The dismissible red error strip (`flex items-start gap-2 rounded-xl
134 /// border border-red-400/20 bg-red-400/[0.06] text-red-300/90` with a
135 /// leading `DangerTriangle mt-0.5 size-4`).
136 fn error_strip(&self, message: impl Into<SharedString>) -> Div {
137 let theme = self.theme();
138 let red = theme.danger; // red-400
139 let red_text = theme.danger_muted; // red-300
140 div()
141 .mt(px(16.0))
142 .px(px(16.0))
143 .py(px(12.0))
144 .rounded(px(Theme::surface_radius()))
145 .border_1()
146 .border_color(red.opacity(0.2))
147 .bg(red.opacity(0.06))
148 .text_style(TextStyle::Callout)
149 .text_color(red_text.opacity(0.9))
150 .flex()
151 .flex_row()
152 .items_start()
153 .gap(px(Theme::SPACE))
154 .child(
155 div().flex_none().mt(px(2.0)).child(
156 crate::icons::icon(crate::icons::glyph::TriangleAlert)
157 .size(px(16.0))
158 .text_color(red_text.opacity(0.9)),
159 ),
160 )
161 .child(div().min_w_0().child(message.into()))
162 }
163
164 /// The amber warning strip (`flex items-start gap-2 border-amber-400/20
165 /// bg-amber-400/[0.06] text-amber-200/90` with a leading `DangerTriangle
166 /// mt-0.5 size-3.5`).
167 fn warning_strip(&self, message: impl Into<SharedString>) -> Div {
168 let theme = self.theme();
169 let amber = theme.warning; // amber-400
170 let amber_text = theme.warning_muted; // amber-200
171 div()
172 .mt(px(8.0))
173 .px(px(16.0))
174 .py(px(10.0))
175 .rounded(px(Theme::surface_radius()))
176 .border_1()
177 .border_color(amber.opacity(0.2))
178 .bg(amber.opacity(0.06))
179 .text_style(TextStyle::Callout)
180 .text_color(amber_text.opacity(0.9))
181 .flex()
182 .flex_row()
183 .items_start()
184 .gap(px(Theme::SPACE))
185 .child(
186 div().flex_none().mt(px(2.0)).child(
187 crate::icons::icon(crate::icons::glyph::TriangleAlert)
188 .size(px(14.0))
189 .text_color(amber_text.opacity(0.9)),
190 ),
191 )
192 .child(div().min_w_0().child(message.into()))
193 }
194}
195
196impl Status for Theme {}