callout.rs

  1use gpui::AnyElement;
  2
  3use crate::prelude::*;
  4
  5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
  6pub enum BorderPosition {
  7    Top,
  8    Bottom,
  9}
 10
 11/// A callout component for displaying important information that requires user attention.
 12///
 13/// # Usage Example
 14///
 15/// ```
 16/// use ui::{Callout};
 17///
 18/// Callout::new()
 19///     .severity(Severity::Warning)
 20///     .icon(IconName::Warning)
 21///     .title(Label::new("Be aware of your subscription!"))
 22///     .description(Label::new("Your subscription is about to expire. Renew now!"))
 23///     .actions_slot(Button::new("renew", "Renew Now"))
 24/// ```
 25///
 26#[derive(IntoElement, RegisterComponent)]
 27pub struct Callout {
 28    severity: Severity,
 29    icon: Option<IconName>,
 30    title: Option<SharedString>,
 31    description: Option<SharedString>,
 32    actions_slot: Option<AnyElement>,
 33    dismiss_action: Option<AnyElement>,
 34    line_height: Option<Pixels>,
 35    border_position: BorderPosition,
 36}
 37
 38impl Callout {
 39    /// Creates a new `Callout` component with default styling.
 40    pub fn new() -> Self {
 41        Self {
 42            severity: Severity::Info,
 43            icon: None,
 44            title: None,
 45            description: None,
 46            actions_slot: None,
 47            dismiss_action: None,
 48            line_height: None,
 49            border_position: BorderPosition::Top,
 50        }
 51    }
 52
 53    /// Sets the severity of the callout.
 54    pub fn severity(mut self, severity: Severity) -> Self {
 55        self.severity = severity;
 56        self
 57    }
 58
 59    /// Sets the icon to display in the callout.
 60    pub fn icon(mut self, icon: IconName) -> Self {
 61        self.icon = Some(icon);
 62        self
 63    }
 64
 65    /// Sets the title of the callout.
 66    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
 67        self.title = Some(title.into());
 68        self
 69    }
 70
 71    /// Sets the description of the callout.
 72    /// The description can be single or multi-line text.
 73    pub fn description(mut self, description: impl Into<SharedString>) -> Self {
 74        self.description = Some(description.into());
 75        self
 76    }
 77
 78    /// Sets the primary call-to-action button.
 79    pub fn actions_slot(mut self, action: impl IntoElement) -> Self {
 80        self.actions_slot = Some(action.into_any_element());
 81        self
 82    }
 83
 84    /// Sets an optional dismiss button, which is usually an icon button with a close icon.
 85    /// This button is always rendered as the last one to the far right.
 86    pub fn dismiss_action(mut self, action: impl IntoElement) -> Self {
 87        self.dismiss_action = Some(action.into_any_element());
 88        self
 89    }
 90
 91    /// Sets a custom line height for the callout content.
 92    pub fn line_height(mut self, line_height: Pixels) -> Self {
 93        self.line_height = Some(line_height);
 94        self
 95    }
 96
 97    /// Sets the border position in the callout.
 98    pub fn border_position(mut self, border_position: BorderPosition) -> Self {
 99        self.border_position = border_position;
100        self
101    }
102}
103
104impl RenderOnce for Callout {
105    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
106        let line_height = self.line_height.unwrap_or(window.line_height());
107
108        let has_actions = self.actions_slot.is_some() || self.dismiss_action.is_some();
109
110        let (icon, icon_color, bg_color) = match self.severity {
111            Severity::Info => (
112                IconName::Info,
113                Color::Muted,
114                cx.theme().colors().panel_background.opacity(0.),
115            ),
116            Severity::Success => (
117                IconName::Check,
118                Color::Success,
119                cx.theme().status().success.opacity(0.1),
120            ),
121            Severity::Warning => (
122                IconName::Warning,
123                Color::Warning,
124                cx.theme().status().warning_background.opacity(0.2),
125            ),
126            Severity::Error => (
127                IconName::XCircle,
128                Color::Error,
129                cx.theme().status().error.opacity(0.08),
130            ),
131        };
132
133        h_flex()
134            .min_w_0()
135            .p_2()
136            .gap_2()
137            .items_start()
138            .map(|this| match self.border_position {
139                BorderPosition::Top => this.border_t_1(),
140                BorderPosition::Bottom => this.border_b_1(),
141            })
142            .border_color(cx.theme().colors().border)
143            .bg(bg_color)
144            .overflow_x_hidden()
145            .when(self.icon.is_some(), |this| {
146                this.child(
147                    h_flex()
148                        .h(line_height)
149                        .justify_center()
150                        .child(Icon::new(icon).size(IconSize::Small).color(icon_color)),
151                )
152            })
153            .child(
154                v_flex()
155                    .min_w_0()
156                    .w_full()
157                    .child(
158                        h_flex()
159                            .min_h(line_height)
160                            .w_full()
161                            .gap_1()
162                            .justify_between()
163                            .flex_wrap()
164                            .when_some(self.title, |this, title| {
165                                this.child(h_flex().child(Label::new(title).size(LabelSize::Small)))
166                            })
167                            .when(has_actions, |this| {
168                                this.child(
169                                    h_flex()
170                                        .gap_0p5()
171                                        .when_some(self.actions_slot, |this, action| {
172                                            this.child(action)
173                                        })
174                                        .when_some(self.dismiss_action, |this, action| {
175                                            this.child(action)
176                                        }),
177                                )
178                            }),
179                    )
180                    .when_some(self.description, |this, description| {
181                        this.child(
182                            div()
183                                .w_full()
184                                .flex_1()
185                                .text_ui_sm(cx)
186                                .text_color(cx.theme().colors().text_muted)
187                                .child(description),
188                        )
189                    }),
190            )
191    }
192}
193
194impl Component for Callout {
195    fn scope() -> ComponentScope {
196        ComponentScope::DataDisplay
197    }
198
199    fn description() -> Option<&'static str> {
200        Some(
201            "Used to display a callout for situations where the user needs to know some information, and likely make a decision. This might be a thread running out of tokens, or running out of prompts on a plan and needing to upgrade.",
202        )
203    }
204
205    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
206        let single_action = || Button::new("got-it", "Got it").label_size(LabelSize::Small);
207        let multiple_actions = || {
208            h_flex()
209                .gap_0p5()
210                .child(Button::new("update", "Backup & Update").label_size(LabelSize::Small))
211                .child(Button::new("dismiss", "Dismiss").label_size(LabelSize::Small))
212        };
213
214        let basic_examples = vec![
215            single_example(
216                "Simple with Title Only",
217                Callout::new()
218                    .icon(IconName::Info)
219                    .title("System maintenance scheduled for tonight")
220                    .actions_slot(single_action())
221                    .into_any_element(),
222            )
223            .width(px(580.)),
224            single_example(
225                "With Title and Description",
226                Callout::new()
227                    .icon(IconName::Warning)
228                    .title("Your settings contain deprecated values")
229                    .description(
230                        "We'll backup your current settings and update them to the new format.",
231                    )
232                    .actions_slot(single_action())
233                    .into_any_element(),
234            )
235            .width(px(580.)),
236            single_example(
237                "Error with Multiple Actions",
238                Callout::new()
239                    .icon(IconName::Close)
240                    .title("Thread reached the token limit")
241                    .description("Start a new thread from a summary to continue the conversation.")
242                    .actions_slot(multiple_actions())
243                    .into_any_element(),
244            )
245            .width(px(580.)),
246            single_example(
247                "Multi-line Description",
248                Callout::new()
249                    .icon(IconName::Sparkle)
250                    .title("Upgrade to Pro")
251                    .description("• Unlimited threads\n• Priority support\n• Advanced analytics")
252                    .actions_slot(multiple_actions())
253                    .into_any_element(),
254            )
255            .width(px(580.)),
256        ];
257
258        let severity_examples = vec![
259            single_example(
260                "Info",
261                Callout::new()
262                    .icon(IconName::Info)
263                    .title("System maintenance scheduled for tonight")
264                    .actions_slot(single_action())
265                    .into_any_element(),
266            ),
267            single_example(
268                "Warning",
269                Callout::new()
270                    .severity(Severity::Warning)
271                    .icon(IconName::Triangle)
272                    .title("System maintenance scheduled for tonight")
273                    .actions_slot(single_action())
274                    .into_any_element(),
275            ),
276            single_example(
277                "Error",
278                Callout::new()
279                    .severity(Severity::Error)
280                    .icon(IconName::XCircle)
281                    .title("System maintenance scheduled for tonight")
282                    .actions_slot(single_action())
283                    .into_any_element(),
284            ),
285            single_example(
286                "Success",
287                Callout::new()
288                    .severity(Severity::Success)
289                    .icon(IconName::Check)
290                    .title("System maintenance scheduled for tonight")
291                    .actions_slot(single_action())
292                    .into_any_element(),
293            ),
294        ];
295
296        Some(
297            v_flex()
298                .gap_4()
299                .child(example_group(basic_examples).vertical())
300                .child(example_group_with_title("Severity", severity_examples).vertical())
301                .into_any_element(),
302        )
303    }
304}