1use gpui::{ClickEvent, IntoElement, ParentElement, SharedString};
2use ui::{Tooltip, prelude::*};
3
4#[derive(IntoElement)]
5pub struct ConfiguredApiCard {
6 label: SharedString,
7 button_label: Option<SharedString>,
8 tooltip_label: Option<SharedString>,
9 disabled: bool,
10 on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
11}
12
13impl ConfiguredApiCard {
14 pub fn new(label: impl Into<SharedString>) -> Self {
15 Self {
16 label: label.into(),
17 button_label: None,
18 tooltip_label: None,
19 disabled: false,
20 on_click: None,
21 }
22 }
23
24 pub fn on_click(
25 mut self,
26 handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
27 ) -> Self {
28 self.on_click = Some(Box::new(handler));
29 self
30 }
31
32 pub fn button_label(mut self, button_label: impl Into<SharedString>) -> Self {
33 self.button_label = Some(button_label.into());
34 self
35 }
36
37 pub fn tooltip_label(mut self, tooltip_label: impl Into<SharedString>) -> Self {
38 self.tooltip_label = Some(tooltip_label.into());
39 self
40 }
41
42 pub fn disabled(mut self, disabled: bool) -> Self {
43 self.disabled = disabled;
44 self
45 }
46}
47
48impl RenderOnce for ConfiguredApiCard {
49 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
50 let button_label = self.button_label.unwrap_or("Reset Key".into());
51 let button_id = SharedString::new(format!("id-{}", button_label));
52
53 h_flex()
54 .mt_0p5()
55 .p_1()
56 .justify_between()
57 .rounded_md()
58 .border_1()
59 .border_color(cx.theme().colors().border)
60 .bg(cx.theme().colors().background)
61 .child(
62 h_flex()
63 .flex_1()
64 .min_w_0()
65 .gap_1()
66 .child(Icon::new(IconName::Check).color(Color::Success))
67 .child(Label::new(self.label).truncate()),
68 )
69 .child(
70 Button::new(button_id, button_label)
71 .label_size(LabelSize::Small)
72 .icon(IconName::Undo)
73 .icon_size(IconSize::Small)
74 .icon_color(Color::Muted)
75 .icon_position(IconPosition::Start)
76 .disabled(self.disabled)
77 .when_some(self.tooltip_label, |this, label| {
78 this.tooltip(Tooltip::text(label))
79 })
80 .when_some(
81 self.on_click.filter(|_| !self.disabled),
82 |this, on_click| this.on_click(on_click),
83 ),
84 )
85 }
86}