gpui-box-kit 0.1.0

GPUI Box Kit design-system components and interaction primitives
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Searchable offerings aggregated across caller-owned server sources.
//!
//! This component displays what callers already know. It does not discover,
//! install, invoke, trust, permit, or connect to anything. A result is always
//! identified by both its server and offering because names and offering ids
//! are only unique within one server.

use std::rc::Rc;

use gpui::{
    AnyElement, App, InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString,
    StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px,
};
use gpui_kit_assets::{Icon, icon};
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{
    ActiveTheme, ControlSize, Elevation, Radius, Space, Surface, TextTone, TypeScale,
};

use crate::agent::server_list::{Offering, OfferingKind};
use crate::display::badge::{Badge, Tone};
use crate::display::empty::{EmptyKind, EmptyState};
use crate::display::status::StatusDot;
use crate::foundation::{Disableable, FocusRing, Ident, Pressable, Sizable, StyledExt, text};
use crate::strings::{ActiveStrings, StringKey};

type ActivateHandler = Rc<dyn Fn(OfferingIdentity, &mut Window, &mut App)>;

/// Stable identity for one server-owned offering.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OfferingIdentity {
    pub server_id: SharedString,
    pub offering_id: SharedString,
}

impl OfferingIdentity {
    pub fn new(server_id: impl Into<SharedString>, offering_id: impl Into<SharedString>) -> Self {
        Self {
            server_id: server_id.into(),
            offering_id: offering_id.into(),
        }
    }
}

/// An offering and the caller-authored text used to search it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchableOffering {
    offering: Offering,
    searchable_text: SharedString,
}

impl SearchableOffering {
    pub fn new(offering: Offering, searchable_text: impl Into<SharedString>) -> Self {
        Self {
            offering,
            searchable_text: searchable_text.into(),
        }
    }

    pub fn offering(&self) -> &Offering {
        &self.offering
    }
}

/// What is truthfully known about one source's offerings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OfferingSourceState {
    Loading,
    Empty,
    Unavailable(SharedString),
    Error(SharedString),
    Ready(Vec<SearchableOffering>),
    /// The source's last verified results remain visible beside the failure.
    Stale {
        offerings: Vec<SearchableOffering>,
        reason: SharedString,
    },
}

impl OfferingSourceState {
    pub fn name(&self) -> &'static str {
        match self {
            Self::Loading => "loading",
            Self::Empty => "empty",
            Self::Unavailable(_) => "unavailable",
            Self::Error(_) => "error",
            Self::Ready(offerings) if offerings.is_empty() => "empty",
            Self::Ready(_) => "ready",
            Self::Stale { .. } => "stale",
        }
    }

    fn offerings(&self) -> &[SearchableOffering] {
        match self {
            Self::Ready(offerings) | Self::Stale { offerings, .. } => offerings,
            _ => &[],
        }
    }
}

/// One attributed source and its independently truthful state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OfferingSource {
    id: SharedString,
    name: SharedString,
    state: OfferingSourceState,
}

impl OfferingSource {
    pub fn new(
        id: impl Into<SharedString>,
        name: impl Into<SharedString>,
        state: OfferingSourceState,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            state,
        }
    }

    pub fn id(&self) -> &SharedString {
        &self.id
    }

    pub fn state(&self) -> &OfferingSourceState {
        &self.state
    }
}

/// A searchable, kind-filterable catalog of offerings from multiple servers.
#[derive(IntoElement)]
pub struct OfferingCatalog {
    ident: Ident,
    sources: Vec<OfferingSource>,
    query: SharedString,
    kinds: Vec<OfferingKind>,
    selected: Option<OfferingIdentity>,
    size: ControlSize,
    disabled: bool,
    on_activate: Option<ActivateHandler>,
}

impl std::fmt::Debug for OfferingCatalog {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("OfferingCatalog")
            .field("ident", &self.ident)
            .field("sources", &self.sources)
            .field("query", &self.query)
            .field("kinds", &self.kinds)
            .field("selected", &self.selected)
            .field("disabled", &self.disabled)
            .finish()
    }
}

impl OfferingCatalog {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            sources: Vec::new(),
            query: SharedString::default(),
            kinds: Vec::new(),
            selected: None,
            size: ControlSize::Md,
            disabled: false,
            on_activate: None,
        }
    }

    pub fn source(mut self, source: OfferingSource) -> Self {
        self.sources.push(source);
        self
    }

    pub fn sources(mut self, sources: impl IntoIterator<Item = OfferingSource>) -> Self {
        self.sources.extend(sources);
        self
    }

    /// The caller-owned query matched against each offering's searchable text.
    pub fn query(mut self, query: impl Into<SharedString>) -> Self {
        self.query = query.into();
        self
    }

    /// Included kinds. An empty collection includes all kinds.
    pub fn kinds(mut self, kinds: impl IntoIterator<Item = OfferingKind>) -> Self {
        self.kinds = kinds.into_iter().collect();
        self
    }

    pub fn selected(mut self, identity: OfferingIdentity) -> Self {
        self.selected = Some(identity);
        self
    }

    /// Reports activation with both identities. The component performs no action.
    pub fn on_activate(
        mut self,
        handler: impl Fn(OfferingIdentity, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_activate = Some(Rc::new(handler));
        self
    }
}

impl Disableable for OfferingCatalog {
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

impl Sizable for OfferingCatalog {
    fn control_size(mut self, size: ControlSize) -> Self {
        self.size = size;
        self
    }
}

impl RenderOnce for OfferingCatalog {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let state_name = self.aggregate_state();
        let busy = self
            .sources
            .iter()
            .any(|source| matches!(source.state, OfferingSourceState::Loading));
        let invalid = self
            .sources
            .iter()
            .any(|source| matches!(source.state, OfferingSourceState::Error(_)));
        let statuses: Vec<AnyElement> = self
            .sources
            .iter()
            .filter(|source| source.state.name() != "ready")
            .map(|source| self.source_status(source, &theme, cx))
            .collect();
        let body = self.results(&theme, cx);

        div()
            .id(self.ident.element_id())
            .column()
            .w_full()
            .gap_token(&theme, Space::Sm)
            .p_token(&theme, Space::Sm)
            .radius(&theme, Radius::Card)
            .frame(&theme, Surface::Panel, Elevation::Raised)
            .children(statuses)
            .child(body)
            .semantic_in(
                cx,
                NodeSpec::new(self.ident.semantic_id(), Role::Region)
                    .value(state_name)
                    .busy(busy)
                    .invalid(invalid),
            )
    }
}

impl OfferingCatalog {
    fn aggregate_state(&self) -> &'static str {
        let Some(first) = self.sources.first().map(|source| source.state.name()) else {
            return "empty";
        };
        if self
            .sources
            .iter()
            .all(|source| source.state.name() == first)
        {
            first
        } else {
            "mixed"
        }
    }

    fn source_status(
        &self,
        source: &OfferingSource,
        theme: &gpui_kit_theme::Theme,
        cx: &mut App,
    ) -> AnyElement {
        let ident = self
            .ident
            .child("source")
            .child(encoded_segment(source.id.as_ref()));
        let (label_key, reason, tone) = match &source.state {
            OfferingSourceState::Loading => (StringKey::OfferingSourceLoading, None, Tone::Info),
            OfferingSourceState::Empty | OfferingSourceState::Ready(_) => {
                (StringKey::OfferingSourceEmpty, None, Tone::Neutral)
            }
            OfferingSourceState::Unavailable(reason) => (
                StringKey::OfferingSourceUnavailable,
                Some(reason.clone()),
                Tone::Neutral,
            ),
            OfferingSourceState::Error(reason) => (
                StringKey::OfferingSourceError,
                Some(reason.clone()),
                Tone::Danger,
            ),
            OfferingSourceState::Stale { reason, .. } => (
                StringKey::OfferingSourceStale,
                Some(reason.clone()),
                Tone::Warning,
            ),
        };
        let label = cx.strings().format(label_key, &[source.name.as_ref()]);
        div()
            .row()
            .w_full()
            .items_start()
            .gap_token(theme, Space::Sm)
            .p_token(theme, Space::Sm)
            .radius(theme, Radius::Control)
            .bg(tone.color(theme).opacity(0.12))
            .child(div().mt(px(4.0)).child(StatusDot::new(tone)))
            .child(
                div()
                    .column()
                    .min_w_0()
                    .child(
                        text(theme, TypeScale::Caption, label.clone())
                            .text_color(tone.color(theme)),
                    )
                    .children(reason.clone().map(|reason| {
                        text(theme, TypeScale::Body, reason).text_color(tone.color(theme))
                    })),
            )
            .semantic_in(
                cx,
                NodeSpec::new(ident.semantic_id(), Role::Status)
                    .parent(self.ident.semantic_id())
                    .text(label)
                    .value(source.state.name())
                    .description(reason.unwrap_or_default())
                    .busy(matches!(source.state, OfferingSourceState::Loading))
                    .invalid(matches!(source.state, OfferingSourceState::Error(_))),
            )
            .into_any_element()
    }

    fn results(&self, theme: &gpui_kit_theme::Theme, cx: &mut App) -> AnyElement {
        let query = self.query.to_lowercase();
        let filtered: Vec<(&OfferingSource, &SearchableOffering)> = self
            .sources
            .iter()
            .flat_map(|source| {
                source
                    .state
                    .offerings()
                    .iter()
                    .map(move |offering| (source, offering))
            })
            .filter(|(_, result)| {
                self.kinds.is_empty() || self.kinds.contains(&result.offering.kind())
            })
            .filter(|(_, result)| {
                query.is_empty() || result.searchable_text.to_lowercase().contains(&query)
            })
            .collect();
        if filtered.is_empty() {
            let key = if self
                .sources
                .iter()
                .any(|source| !source.state.offerings().is_empty())
            {
                StringKey::OfferingCatalogNoMatch
            } else {
                StringKey::OfferingCatalogEmpty
            };
            return EmptyState::new(self.ident.child("empty"), cx.strings().text(key))
                .kind(EmptyKind::Empty)
                .into_any_element();
        }

        let list_ident = self.ident.child("results");
        div()
            .column()
            .w_full()
            .gap_token(theme, Space::Xs)
            .children(
                filtered
                    .iter()
                    .map(|(source, result)| self.result(source, result, &list_ident, theme, cx)),
            )
            .semantic_in(
                cx,
                NodeSpec::new(list_ident.semantic_id(), Role::List)
                    .parent(self.ident.semantic_id())
                    .value(filtered.len().to_string()),
            )
            .into_any_element()
    }

    fn result(
        &self,
        source: &OfferingSource,
        result: &SearchableOffering,
        list_ident: &Ident,
        theme: &gpui_kit_theme::Theme,
        cx: &mut App,
    ) -> AnyElement {
        let identity = OfferingIdentity::new(source.id.clone(), result.offering.id().clone());
        let ident = list_ident
            .child(encoded_segment(identity.server_id.as_ref()))
            .child(encoded_segment(identity.offering_id.as_ref()));
        let selected = self.selected.as_ref() == Some(&identity);
        let actionable = !self.disabled && self.on_activate.is_some();
        let metrics = theme.control.get(self.size);
        let kind = result.offering.kind();
        let glyph = match kind {
            OfferingKind::Tool => Icon::Tuning,
            OfferingKind::Skill => Icon::Command,
            OfferingKind::Resource => Icon::Document,
        };
        let mut row = div()
            .id(ident.element_id())
            .row()
            .w_full()
            .items_start()
            .gap_token(theme, Space::Sm)
            .p_token(theme, Space::Sm)
            .radius(theme, Radius::Control)
            .when(selected, |element| element.bg(theme.colors.selected))
            .when(actionable, |element| {
                element
                    .cursor_pointer()
                    .tab_index(0)
                    .pressable(cx)
                    .when(!selected, |element| {
                        element.hover(|style| style.bg(theme.colors.hover))
                    })
                    .focus_ring(theme)
            })
            .child(
                icon(glyph)
                    .size(px(metrics.icon_size))
                    .text_color(theme.colors.text_faint),
            )
            .child(
                div()
                    .column()
                    .flex_1()
                    .min_w_0()
                    .child(text(
                        theme,
                        TypeScale::Label,
                        result.offering.name().clone(),
                    ))
                    .when_some(
                        result.offering.summary_text().cloned(),
                        |element, summary| {
                            element.child(
                                text(theme, TypeScale::Body, summary)
                                    .text_tone(theme, TextTone::Muted),
                            )
                        },
                    )
                    .when_some(
                        result.offering.qualifier_text().cloned(),
                        |element, qualifier| {
                            element.child(
                                text(theme, TypeScale::Code, qualifier)
                                    .text_tone(theme, TextTone::Faint)
                                    .font_family(theme.typography.mono.clone()),
                            )
                        },
                    ),
            )
            .child(
                div()
                    .column()
                    .items_end()
                    .gap_token(theme, Space::Xs)
                    .child(Badge::new(kind.name()).tone(Tone::Neutral))
                    .child(
                        text(theme, TypeScale::Caption, source.name.clone())
                            .text_tone(theme, TextTone::Muted)
                            .semantic_in(
                                cx,
                                NodeSpec::new(ident.child("server").semantic_id(), Role::Status)
                                    .parent(ident.semantic_id())
                                    .text(source.name.clone())
                                    .value(source.id.clone()),
                            ),
                    ),
            );
        if let (true, Some(handler)) = (actionable, self.on_activate.clone()) {
            row = row.on_click(move |_, window, cx| handler(identity.clone(), window, cx));
        }
        row.semantic_in(
            cx,
            NodeSpec::new(ident.semantic_id(), Role::Row)
                .parent(list_ident.semantic_id())
                .text(result.offering.name().clone())
                .value(kind.name())
                .selected(selected)
                .disabled(self.disabled),
        )
        .into_any_element()
    }
}

fn encoded_segment(value: &str) -> String {
    value.replace('%', "%25").replace('.', "%2E")
}