Skip to repository content161 lines · 5.2 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T04:47:27.954Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
configured_api_card.rs
1use crate::{Tooltip, prelude::*};
2use gpui::{ClickEvent, ElementId, IntoElement, ParentElement, SharedString};
3
4#[derive(IntoElement, RegisterComponent)]
5pub struct ConfiguredApiCard {
6 id: ElementId,
7 label: SharedString,
8 button_label: Option<SharedString>,
9 button_tab_index: Option<isize>,
10 tooltip_label: Option<SharedString>,
11 disabled: bool,
12 on_click: Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>,
13}
14
15impl ConfiguredApiCard {
16 pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
17 Self {
18 id: id.into(),
19 label: label.into(),
20 button_label: None,
21 button_tab_index: None,
22 tooltip_label: None,
23 disabled: false,
24 on_click: None,
25 }
26 }
27
28 pub fn on_click(
29 mut self,
30 handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
31 ) -> Self {
32 self.on_click = Some(Box::new(handler));
33 self
34 }
35
36 pub fn button_label(mut self, button_label: impl Into<SharedString>) -> Self {
37 self.button_label = Some(button_label.into());
38 self
39 }
40
41 pub fn tooltip_label(mut self, tooltip_label: impl Into<SharedString>) -> Self {
42 self.tooltip_label = Some(tooltip_label.into());
43 self
44 }
45
46 pub fn disabled(mut self, disabled: bool) -> Self {
47 self.disabled = disabled;
48 self
49 }
50
51 pub fn button_tab_index(mut self, tab_index: isize) -> Self {
52 self.button_tab_index = Some(tab_index);
53 self
54 }
55}
56
57impl RenderOnce for ConfiguredApiCard {
58 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
59 let button_label = self.button_label.unwrap_or("Reset Key".into());
60 let button_id = self.id;
61
62 h_flex()
63 .min_w_0()
64 .mt_0p5()
65 .p_1()
66 .flex_wrap()
67 .justify_between()
68 .rounded_md()
69 .border_1()
70 .border_color(cx.theme().colors().border_variant)
71 .bg(cx.theme().colors().background.opacity(0.5))
72 .child(
73 h_flex()
74 .min_w_0()
75 .gap_1()
76 .child(Icon::new(IconName::Check).color(Color::Success))
77 .child(Label::new(self.label)),
78 )
79 .child(
80 Button::new(button_id, button_label)
81 .when_some(self.button_tab_index, |elem, tab_index| {
82 elem.tab_index(tab_index)
83 })
84 .label_size(LabelSize::Small)
85 .start_icon(
86 Icon::new(IconName::Undo)
87 .size(IconSize::Small)
88 .color(Color::Muted),
89 )
90 .disabled(self.disabled)
91 .when_some(self.tooltip_label, |this, label| {
92 this.tooltip(Tooltip::text(label))
93 })
94 .when_some(
95 self.on_click.filter(|_| !self.disabled),
96 |this, on_click| this.on_click(on_click),
97 ),
98 )
99 }
100}
101
102impl Component for ConfiguredApiCard {
103 fn scope() -> ComponentScope {
104 ComponentScope::Agent
105 }
106
107 fn description() -> &'static str {
108 "A card used in AI provider settings to indicate that an API has been \
109 configured, with an optional action button to manage or reconfigure it."
110 }
111
112 fn preview(_window: &mut Window, cx: &mut App) -> AnyElement {
113 let container = || {
114 v_flex()
115 .w_72()
116 .p_2()
117 .gap_2()
118 .border_1()
119 .border_color(cx.theme().colors().border_variant)
120 .bg(cx.theme().colors().panel_background)
121 };
122
123 let examples = vec![
124 single_example(
125 "Default",
126 container()
127 .child(ConfiguredApiCard::new("default", "API key is configured"))
128 .into_any_element(),
129 ),
130 single_example(
131 "Custom Button Label",
132 container()
133 .child(
134 ConfiguredApiCard::new("custom-button-label", "OpenAI API key configured")
135 .button_label("Remove Key"),
136 )
137 .into_any_element(),
138 ),
139 single_example(
140 "With Tooltip",
141 container()
142 .child(
143 ConfiguredApiCard::new("with-tooltip", "Anthropic API key configured")
144 .tooltip_label("Click to reset your API key"),
145 )
146 .into_any_element(),
147 ),
148 single_example(
149 "Disabled",
150 container()
151 .child(
152 ConfiguredApiCard::new("disabled", "API key is configured").disabled(true),
153 )
154 .into_any_element(),
155 ),
156 ];
157
158 example_group(examples).into_any_element()
159 }
160}
161