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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! Web-search tool output: a query and its results with sources.
use crate::control::{PressReleaseExt as _, composed_button};
use crate::decoration::{DecoratedExt as _, Decoration};
use crate::handlers::SharedHandler;
use crate::motion::{ArrivalRoster, MotionTokens};
use crate::surface::CardFrameExt as _;
use crate::surface::{initial_badge, initial_of};
use crate::theme::SemanticStyledExt as _;
use gpui::{
App, ClickEvent, ElementId, InteractiveElement as _, IntoElement, ParentElement as _,
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled,
Window, div, prelude::FluentBuilder as _,
};
use gpui_component::{
ActiveTheme as _, Icon, IconName, Sizable as _, StyledExt as _, h_flex, spinner::Spinner,
v_flex,
};
/// One search hit inside [`SearchResults`].
#[derive(Debug, Clone)]
pub struct SearchResult {
id: SharedString,
title: SharedString,
domain: Option<SharedString>,
}
impl SearchResult {
/// Creates a result with its display title.
pub fn new(id: impl Into<SharedString>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
domain: None,
}
}
/// Sets the source domain shown after the title (`example.com`).
pub fn domain(mut self, domain: impl Into<SharedString>) -> Self {
self.domain = Some(domain.into());
self
}
fn opened_event(&self) -> SearchResultsEvent {
SearchResultsEvent::Opened {
id: self.id.clone(),
}
}
}
/// An interaction emitted by [`SearchResults`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SearchResultsEvent {
/// A result was selected.
Opened {
/// Stable result identifier.
id: SharedString,
},
}
/// The output of a web-search tool call: the query, a live "searching"
/// state, and the results once they arrive.
///
/// # Example
///
/// ```
/// # use gpui_ai::prelude::*;
/// SearchResults::new("search-1", "gpui wasm support")
/// .results([
/// SearchResult::new("gallery", "GPUI Component — Web Gallery")
/// .domain("longbridge.github.io"),
/// SearchResult::new("gpui-web", "zed-industries/zed: crates/gpui_web")
/// .domain("github.com"),
/// ])
/// .on_event(|event, _, _| { /* open result */ });
/// ```
#[derive(IntoElement)]
pub struct SearchResults {
id: ElementId,
style: StyleRefinement,
decoration: Decoration,
query: SharedString,
searching: bool,
results: Vec<SearchResult>,
on_event: Option<SharedHandler<SearchResultsEvent>>,
}
impl SearchResults {
/// Layers painted into this card's frame: one under the content, one
/// over it, both clipped to its own shape and neither affecting layout.
///
/// This crate ships no effects of its own — what goes in a decoration is
/// the application's expression.
pub fn decoration(mut self, decoration: Decoration) -> Self {
self.decoration = decoration;
self
}
/// Creates the component for a search `query`.
pub fn new(id: impl Into<ElementId>, query: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
style: StyleRefinement::default(),
decoration: Decoration::default(),
query: query.into(),
searching: false,
results: Vec::new(),
on_event: None,
}
}
/// Marks the search as still running; the header shows a spinner.
pub fn searching(mut self, searching: bool) -> Self {
self.searching = searching;
self
}
/// Sets the results.
pub fn results(mut self, results: impl IntoIterator<Item = SearchResult>) -> Self {
self.results = results.into_iter().collect();
self
}
/// Handles typed result interactions.
pub fn on_event(
mut self,
handler: impl Fn(&SearchResultsEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_event = Some(std::rc::Rc::new(handler));
self
}
}
impl Styled for SearchResults {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for SearchResults {
fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = cx.theme().semantic_tokens();
// Taken once: each layer is placed at most once, and a
// component with no decoration adds no elements at all.
let mut decoration = std::mem::take(&mut self.decoration);
let header: SharedString = if self.searching {
format!("Searching \u{201c}{}\u{201d}\u{2026}", self.query).into()
} else {
format!(
"{} result{} for \u{201c}{}\u{201d}",
self.results.len(),
if self.results.len() == 1 { "" } else { "s" },
self.query
)
.into()
};
let handler = self.on_event;
let root_id = self.id.clone();
// Results the list has already shown stay at rest; stable IDs
// streamed into a mounted list settle in on the capped cascade, and
// the initial set joins at rest.
let motion = MotionTokens::read(cx).clone();
let roster = window.use_keyed_state((root_id.clone(), "arrivals"), cx, |_, _| {
ArrivalRoster::new()
});
roster.update(cx, |roster, cx| {
roster.note(
self.results.iter().map(|result| {
ElementId::Name(SharedString::from(format!("result-{}", result.id)))
}),
true,
&motion,
cx.background_executor().now(),
);
});
v_flex()
.id(self.id)
.role(Role::Search)
.aria_label(header.clone())
.card_frame(cx)
.decoration_under(&mut decoration)
.overflow_hidden()
.child(
h_flex()
.items_center()
.gap(tokens.spacing.sm)
.px(tokens.spacing.md)
.py(tokens.spacing.xs)
.text_token(tokens.typography.xs)
.text_color(cx.theme().muted_foreground)
.child(if self.searching {
Spinner::new()
.xsmall()
.color(cx.theme().muted_foreground)
.into_any_element()
} else {
Icon::new(IconName::Search)
.xsmall()
.text_color(cx.theme().muted_foreground)
.into_any_element()
})
.child(header),
)
.when(!self.results.is_empty(), |this| {
this.child(
v_flex()
.border_t_1()
.border_color(crate::surface::hairline(cx))
// Rows are inset from the card that frames them, so
// a hover fill sits inside the card's rounded edge
// instead of running out to it.
.p(tokens.spacing.xs)
.gap(tokens.spacing.xxs)
.children(self.results.into_iter().map(|result| {
let event = result.opened_event();
let result_id = result.id.clone();
let arrival = roster.update(cx, |roster, cx| {
roster.progress(
&ElementId::Name(SharedString::from(format!(
"result-{}",
result.id
))),
window,
cx,
)
});
let accessibility_label = result.title.clone();
let accessibility_description = result.domain.clone();
let row = h_flex()
.w_full()
.items_center()
.gap(tokens.spacing.sm)
.child(match &result.domain {
Some(domain) => {
initial_badge(initial_of(domain), cx).into_any_element()
}
None => Icon::new(IconName::Globe)
.xsmall()
.text_color(cx.theme().muted_foreground)
.into_any_element(),
})
.child(
div()
.flex_1()
.truncate()
.text_token(tokens.typography.sm)
.text_color(cx.theme().foreground)
.child(result.title),
)
.when_some(result.domain, |this, domain| {
this.child(
crate::surface::hint(domain, cx)
.flex_none()
.font_family(cx.theme().mono_font_family.clone()),
)
});
let row = match arrival {
Some(progress) => row.opacity(progress).top(
tokens.spacing.xxs
* (1.0 - progress)
* crate::motion::travel(cx),
),
None => row,
};
match handler.clone() {
Some(handler) => {
composed_button(result.id.clone(), accessibility_label)
.w_full()
.justify_start()
.px(tokens.spacing.sm)
.py(tokens.spacing.xs)
.border_1()
.border_color(cx.theme().transparent)
.rounded(crate::surface::nested_radius(
tokens.radius.lg,
tokens.spacing.xs,
tokens.radius.sm,
))
.hover(|style| style.bg(cx.theme().accent.opacity(0.6)))
.active(|style| style.bg(cx.theme().accent))
.focus_visible(|style| style.border_color(cx.theme().ring))
.press_release(
ElementId::from((
ElementId::from(result.id.clone()),
"press",
)),
tokens.radius.sm,
window,
cx,
)
.when_some(
accessibility_description,
|this, description| this.aria_description(description),
)
.child(row)
.on_click(move |_: &ClickEvent, window, cx| {
handler(&event, window, cx)
})
.into_any_element()
}
None => div()
.id(result_id)
.px(tokens.spacing.sm)
.py(tokens.spacing.xs)
.role(Role::ListItem)
.aria_label(accessibility_label)
.when_some(accessibility_description, |this, description| {
this.aria_description(description)
})
.w_full()
.px(tokens.spacing.md)
.py(tokens.spacing.xs)
.child(row)
.into_any_element(),
}
})),
)
})
.decoration_over(&mut decoration)
.refine_style(&self.style)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn duplicate_titles_emit_distinct_stable_ids() {
let first = SearchResult::new("first", "Same title");
let second = SearchResult::new("second", "Same title");
assert_eq!(
first.opened_event(),
SearchResultsEvent::Opened { id: "first".into() }
);
assert_eq!(
second.opened_event(),
SearchResultsEvent::Opened {
id: "second".into()
}
);
}
}