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 tertiary call-to-action button.
 85    pub fn dismiss_action(mut self, action: impl IntoElement) -> Self {
 86        self.dismiss_action = Some(action.into_any_element());
 87        self
 88    }
 89
 90    /// Sets a custom line height for the callout content.
 91    pub fn line_height(mut self, line_height: Pixels) -> Self {
 92        self.line_height = Some(line_height);
 93        self
 94    }
 95
 96    /// Sets the border position in the callout.
 97    pub fn border_position(mut self, border_position: BorderPosition) -> Self {
 98        self.border_position = border_position;
 99        self
100    }
101}
102
103impl RenderOnce for Callout {
104    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
105        let line_height = self.line_height.unwrap_or(window.line_height());
106
107        let has_actions = self.actions_slot.is_some() || self.dismiss_action.is_some();
108
109        let (icon, icon_color, bg_color) = match self.severity {
110            Severity::Info => (
111                IconName::Info,
112                Color::Muted,
113                cx.theme().colors().panel_background.opacity(0.),
114            ),
115            Severity::Success => (
116                IconName::Check,
117                Color::Success,
118                cx.theme().status().success.opacity(0.1),
119            ),
120            Severity::Warning => (
121                IconName::Warning,
122                Color::Warning,
123                cx.theme().status().warning_background.opacity(0.2),
124            ),
125            Severity::Error => (
126                IconName::XCircle,
127                Color::Error,
128                cx.theme().status().error.opacity(0.08),
129            ),
130        };
131
132        h_flex()
133            .min_w_0()
134            .p_2()
135            .gap_2()
136            .items_start()
137            .map(|this| match self.border_position {
138                BorderPosition::Top => this.border_t_1(),
139                BorderPosition::Bottom => this.border_b_1(),
140            })
141            .border_color(cx.theme().colors().border)
142            .bg(bg_color)
143            .overflow_x_hidden()
144            .when(self.icon.is_some(), |this| {
145                this.child(
146                    h_flex()
147                        .h(line_height)
148                        .justify_center()
149                        .child(Icon::new(icon).size(IconSize::Small).color(icon_color)),
150                )
151            })
152            .child(
153                v_flex()
154                    .min_w_0()
155                    .w_full()
156                    .child(
157                        h_flex()
158                            .min_h(line_height)
159                            .w_full()
160                            .gap_1()
161                            .justify_between()
162                            .flex_wrap()
163                            .when_some(self.title, |this, title| {
164                                this.child(h_flex().child(Label::new(title).size(LabelSize::Small)))
165                            })
166                            .when(has_actions, |this| {
167                                this.child(
168                                    h_flex()
169                                        .gap_0p5()
170                                        .when_some(self.actions_slot, |this, action| {
171                                            this.child(action)
172                                        })
173                                        .when_some(self.dismiss_action, |this, action| {
174                                            this.child(action)
175                                        }),
176                                )
177                            }),
178                    )
179                    .when_some(self.description, |this, description| {
180                        this.child(
181                            div()
182                                .w_full()
183                                .flex_1()
184                                .text_ui_sm(cx)
185                                .text_color(cx.theme().colors().text_muted)
186                                .child(description),
187                        )
188                    }),
189            )
190    }
191}
192
193impl Component for Callout {
194    fn scope() -> ComponentScope {
195        ComponentScope::DataDisplay
196    }
197
198    fn description() -> Option<&'static str> {
199        Some(
200            "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.",
201        )
202    }
203
204    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
205        let single_action = || Button::new("got-it", "Got it").label_size(LabelSize::Small);
206        let multiple_actions = || {
207            h_flex()
208                .gap_0p5()
209                .child(Button::new("update", "Backup & Update").label_size(LabelSize::Small))
210                .child(Button::new("dismiss", "Dismiss").label_size(LabelSize::Small))
211        };
212
213        let basic_examples = vec![
214            single_example(
215                "Simple with Title Only",
216                Callout::new()
217                    .icon(IconName::Info)
218                    .title("System maintenance scheduled for tonight")
219                    .actions_slot(single_action())
220                    .into_any_element(),
221            )
222            .width(px(580.)),
223            single_example(
224                "With Title and Description",
225                Callout::new()
226                    .icon(IconName::Warning)
227                    .title("Your settings contain deprecated values")
228                    .description(
229                        "We'll backup your current settings and update them to the new format.",
230                    )
231                    .actions_slot(single_action())
232                    .into_any_element(),
233            )
234            .width(px(580.)),
235            single_example(
236                "Error with Multiple Actions",
237                Callout::new()
238                    .icon(IconName::Close)
239                    .title("Thread reached the token limit")
240                    .description("Start a new thread from a summary to continue the conversation.")
241                    .actions_slot(multiple_actions())
242                    .into_any_element(),
243            )
244            .width(px(580.)),
245            single_example(
246                "Multi-line Description",
247                Callout::new()
248                    .icon(IconName::Sparkle)
249                    .title("Upgrade to Pro")
250                    .description("• Unlimited threads\n• Priority support\n• Advanced analytics")
251                    .actions_slot(multiple_actions())
252                    .into_any_element(),
253            )
254            .width(px(580.)),
255        ];
256
257        let severity_examples = vec![
258            single_example(
259                "Info",
260                Callout::new()
261                    .icon(IconName::Info)
262                    .title("System maintenance scheduled for tonight")
263                    .actions_slot(single_action())
264                    .into_any_element(),
265            ),
266            single_example(
267                "Warning",
268                Callout::new()
269                    .severity(Severity::Warning)
270                    .icon(IconName::Triangle)
271                    .title("System maintenance scheduled for tonight")
272                    .actions_slot(single_action())
273                    .into_any_element(),
274            ),
275            single_example(
276                "Error",
277                Callout::new()
278                    .severity(Severity::Error)
279                    .icon(IconName::XCircle)
280                    .title("System maintenance scheduled for tonight")
281                    .actions_slot(single_action())
282                    .into_any_element(),
283            ),
284            single_example(
285                "Success",
286                Callout::new()
287                    .severity(Severity::Success)
288                    .icon(IconName::Check)
289                    .title("System maintenance scheduled for tonight")
290                    .actions_slot(single_action())
291                    .into_any_element(),
292            ),
293        ];
294
295        Some(
296            v_flex()
297                .gap_4()
298                .child(example_group(basic_examples).vertical())
299                .child(example_group_with_title("Severity", severity_examples).vertical())
300                .into_any_element(),
301        )
302    }
303}