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