1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Add-tool panel: browse the registry or add a plugin from a source.
use gpui::prelude::*;
use gpui::{Context, IntoElement, ParentElement, Styled, Window, div, px};
use gpui_component::ActiveTheme;
use gpui_component::button::Button;
use gpui_component::input::Input;
use gpui_component::scroll::ScrollableElement;
use crate::model::{AddSource, BackendChoice};
use crate::ui::RootView;
use crate::ui::root::AddTab;
impl RootView {
pub(crate) fn render_add_modal(
&mut self,
window: &mut Window,
cx: &mut Context<'_, Self>,
) -> impl IntoElement {
div()
.absolute()
.size_full()
.flex()
.items_center()
.justify_center()
.bg(gpui::rgba(0x0000_0088))
.occlude()
.child(
div()
.w(px(560.0))
.max_h(px(520.0))
.bg(cx.theme().background)
.rounded(px(8.0))
.shadow_lg()
.p_4()
.child(self.render_add_panel(window, cx)),
)
}
pub(crate) fn render_add_panel(
&mut self,
_window: &mut Window,
cx: &mut Context<'_, Self>,
) -> impl IntoElement {
let tab = self.add_tab;
div()
.flex()
.flex_col()
.gap_2()
.p_4()
.child(
div()
.flex()
.items_center()
.justify_between()
.child(div().text_lg().child("Add a tool"))
.child(
Button::new("add-close")
.label("Close")
.on_click(cx.listener(|this, _ev, _window, cx| {
this.show_add = false;
cx.notify();
})),
),
)
.child(
div()
.flex()
.gap_1()
.child(Button::new("tab-registry").label("From registry").on_click(
cx.listener(|this, _ev, _window, cx| {
this.add_tab = AddTab::Registry;
cx.notify();
}),
))
.child(
Button::new("tab-source")
.label("From source")
.on_click(cx.listener(|this, _ev, _window, cx| {
this.add_tab = AddTab::Source;
cx.notify();
})),
),
)
.child(match tab {
AddTab::Registry => self.render_add_registry(cx).into_any_element(),
AddTab::Source => self.render_add_source(cx).into_any_element(),
})
}
fn render_add_registry(&mut self, cx: &mut Context<'_, Self>) -> impl IntoElement + use<> {
let query = self.add_filter_input.read(cx).value().to_lowercase();
let names: Vec<String> = self
.add_registry
.iter()
.filter(|n| query.is_empty() || n.to_lowercase().contains(&query))
.cloned()
.collect();
// Build rows with a for-loop into a Vec (NOT `.children(map(...))`) so the
// `cx.listener` calls don't double-borrow `cx` under Rust 2024 — see detail.rs.
let mut rows = Vec::with_capacity(names.len());
for (ix, name) in names.into_iter().enumerate() {
let add_name = name.clone();
rows.push(
div()
.id(("registry", ix))
.flex()
.items_center()
.justify_between()
.py_1()
.child(div().child(name))
.child(
Button::new(("add-reg", ix))
.label("Add")
.on_click(cx.listener(move |this, _ev, window, cx| {
let n = add_name.clone();
this.show_add = false;
let key = format!("add:{n}");
this.run_action(key, window, cx, move |svc| {
svc.add(AddSource::Registry { name: n.clone() })
.map(|()| format!("Added {n}"))
});
})),
),
);
}
div()
.flex()
.flex_col()
.gap_1()
.child(Input::new(&self.add_filter_input))
.child(
div()
.id("add-registry-scroll")
.max_h(px(320.0))
.overflow_y_scrollbar()
.flex()
.flex_col()
.gap_1()
.children(rows),
)
}
fn render_add_source(&mut self, cx: &mut Context<'_, Self>) -> impl IntoElement + use<> {
let backend = self.add_backend;
div()
.flex()
.flex_col()
.gap_2()
.child(Input::new(&self.add_source_input))
.child(Input::new(&self.add_alias_input))
.child(
div()
.flex()
.gap_1()
.child(div().child(format!("backend: {}", backend_label(backend))))
.child(
Button::new("backend-lua")
.label("Lua")
.on_click(cx.listener(|this, _ev, _window, cx| {
this.add_backend = BackendChoice::Lua;
cx.notify();
})),
)
.child(
Button::new("backend-wasi")
.label("Wasi")
.on_click(cx.listener(|this, _ev, _window, cx| {
this.add_backend = BackendChoice::Wasi;
cx.notify();
})),
),
)
.child(
Button::new("add-source-submit")
.label("Add")
.on_click(cx.listener(move |this, _ev, window, cx| {
let source = this.add_source_input.read(cx).value().to_string();
let alias_raw = this.add_alias_input.read(cx).value().to_string();
let alias = if alias_raw.trim().is_empty() {
None
} else {
Some(alias_raw)
};
if source.trim().is_empty() {
return;
}
this.show_add = false;
let backend = this.add_backend;
let key = format!("add-source:{source}");
this.run_action(key, window, cx, move |svc| {
svc.add(AddSource::Source {
source: source.clone(),
alias,
backend,
})
.map(|()| format!("Added from {source}"))
});
})),
)
}
}
fn backend_label(backend: BackendChoice) -> &'static str {
match backend {
BackendChoice::Lua => "Lua",
BackendChoice::Wasi => "Wasi",
}
}