subspace-dashboard 0.2.0

Subspace Framework board kit: the Dashboard trait, shared widgets and design tokens. Internal crate; apps depend on `subspace-framework`.
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Loading, empty and error states (REQ-13), including the list skeleton.

use gpui_kit::component::{h_flex, v_flex};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
    App, ClickEvent, Div, ElementId, Hsla, ParentElement, SharedString, Styled, Window, div, px,
};

use crate::action::{Intent, action_button};
use crate::rich;
use crate::row_menu::MenuChat;
use crate::tokens::Tokens;
use subspace_lucide::IconName;

const RETRY_MARGIN_PX: f32 = 8.;
const BANNER_VERTICAL_PADDING_PX: f32 = 6.;
const STATE_TOP_PADDING_PX: f32 = 120.;
const STATE_SIDE_PADDING_PX: f32 = 16.;
const STATE_MESSAGE_MAX_WIDTH_PX: f32 = 520.;
/// Preserve the existing fixed-pixel list skeleton geometry at every zoom level.
const SKELETON_BAR_HEIGHT_PX: f32 = 8.;
const SKELETON_LEADING_INSET_PX: f32 = 26.;
const SKELETON_ID_WIDTH_PX: f32 = 48.;
const SKELETON_TITLE_WIDTHS_PX: [f32; 8] = [220., 300., 180., 260., 240., 150., 280., 200.];
const SKELETON_PARENT_INTERVAL: usize = 4;
const SKELETON_BAR_OPACITY: f32 = 0.6;

/// Stable identity for one selectable part of a shared empty or error surface.
///
/// `instance` is the caller's identity, so two surfaces with the same title in
/// one view do not share an element id.
pub fn text_key(
    instance: &'static str,
    kind: &'static str,
    part: &'static str,
    title: &str,
) -> String {
    format!("feedback-{instance}-{kind}-{part}-{title}")
}

/// Placeholder rows while a list loads for the first time.
pub fn skeleton(tokens: &Tokens) -> Div {
    let bar = |width: f32| {
        div()
            .h(px(SKELETON_BAR_HEIGHT_PX))
            .w(px(width))
            .rounded(tokens.control_radius)
            .bg(tokens.line.opacity(SKELETON_BAR_OPACITY))
    };
    v_flex().w_full().children(
        SKELETON_TITLE_WIDTHS_PX
            .iter()
            .enumerate()
            .map(|(i, width)| {
                let parent = i % SKELETON_PARENT_INTERVAL == 0;
                h_flex()
                    .h(tokens.row_height)
                    .gap_2()
                    .pl(if parent {
                        px(SKELETON_LEADING_INSET_PX)
                    } else {
                        px(SKELETON_LEADING_INSET_PX) + tokens.indent
                    })
                    .border_b_1()
                    .border_color(tokens.hair)
                    .when(parent, |row| row.bg(tokens.pane))
                    .child(bar(SKELETON_ID_WIDTH_PX))
                    .child(bar(*width))
            }),
    )
}

/// The words of a state or banner, and whether their text menu offers the
/// chat. A board whose content stays out of chat sets `chat` to
/// [`MenuChat::Off`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Words {
    pub title: SharedString,
    pub message: SharedString,
    pub chat: MenuChat,
}

impl Words {
    pub fn new(title: impl Into<SharedString>, message: impl Into<SharedString>) -> Self {
        Self {
            title: title.into(),
            message: message.into(),
            chat: MenuChat::Offered,
        }
    }
}

/// A centred box with a title and a dimmed message.
fn state_box(tokens: &Tokens, instance: &'static str, words: &Words, title_color: Hsla) -> Div {
    let title_key = text_key(instance, "state", "title", &words.title);
    let message_key = text_key(instance, "state", "message", &words.title);
    v_flex()
        .w_full()
        .items_center()
        .pt(px(STATE_TOP_PADDING_PX))
        .px(px(STATE_SIDE_PADDING_PX))
        .gap_2()
        .text_size(tokens.row_font)
        .child(
            div()
                .text_size(tokens.heading_font)
                .text_color(title_color)
                .child(rich::plain_text_with(title_key, &words.title, words.chat)),
        )
        .child(
            div()
                .max_w(px(STATE_MESSAGE_MAX_WIDTH_PX))
                .text_color(tokens.muted)
                .child(rich::plain_text_with(
                    message_key,
                    &words.message,
                    words.chat,
                )),
        )
}

/// An empty state with caller-supplied actions, its words' chat choice kept.
pub fn empty_words(
    tokens: &Tokens,
    instance: &'static str,
    words: Words,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    state_box(tokens, instance, &words, tokens.fg_strong).child(
        h_flex()
            .mt(px(RETRY_MARGIN_PX))
            .gap_2()
            .flex_wrap()
            .justify_center()
            .children(actions),
    )
}

/// A load failure with caller-supplied actions, its words' chat choice kept.
pub fn error_box_words(
    tokens: &Tokens,
    instance: &'static str,
    words: Words,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    state_box(tokens, instance, &words, tokens.red).child(
        h_flex()
            .mt(px(RETRY_MARGIN_PX))
            .gap_2()
            .flex_wrap()
            .children(actions),
    )
}

/// A muted, selectable one-line loading message for an in-panel fetch.
pub fn loading_line(
    tokens: &Tokens,
    id: impl Into<ElementId>,
    message: impl Into<SharedString>,
) -> Div {
    let message = message.into();
    div()
        .text_size(tokens.secondary_size())
        .text_color(tokens.muted)
        .child(rich::plain_text(id, &message))
}

/// A muted, selectable one-line note that a part of a view loaded with
/// nothing to show, such as a list group whose filter matches none of its
/// records. An alias of [`loading_line`]: the same treatment under a name
/// that says what it shows, so it can take that line's place when the load
/// ends.
pub fn empty_line(
    tokens: &Tokens,
    id: impl Into<ElementId>,
    message: impl Into<SharedString>,
) -> Div {
    loading_line(tokens, id, message)
}

/// Nothing to show: the dashboard's message and hint.
pub fn empty(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    hint: impl Into<SharedString>,
) -> Div {
    state_box(tokens, instance, &Words::new(title, hint), tokens.fg_strong)
}

/// An empty setup surface with an in-app configuration action.
pub fn configuration(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    hint: impl Into<SharedString>,
) -> Div {
    empty(tokens, instance, title, hint).child(
        h_flex()
            .mt(px(RETRY_MARGIN_PX))
            .child(configure_action(format!("{instance}-configure"))),
    )
}

/// An empty setup surface with caller-supplied actions, such as opening
/// Settings and copying setup instructions.
// qual:api
pub fn empty_with_actions(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    hint: impl Into<SharedString>,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    empty_words(tokens, instance, Words::new(title, hint), actions)
}

/// Open the visible board's settings without depending on the app shell.
pub fn configure_action(id: impl Into<ElementId>) -> crate::icon_button::IconButton {
    recovery_action(
        id,
        Intent::Neutral,
        IconName::Settings,
        "Configure",
        |_, window, cx| window.dispatch_action(Box::new(crate::ConfigureBoard), cx),
    )
}

/// A board load failure with both retry and connection settings.
pub fn error_box_with_settings(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    message: impl Into<SharedString>,
    on_retry: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Div {
    error_box_with_actions(
        tokens,
        instance,
        title,
        message,
        [
            retry_action(format!("{instance}-retry"), on_retry),
            configure_action(format!("{instance}-configure")),
        ],
    )
}

/// A load failed: the title, the message and a Retry button.
pub fn error_box(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    message: impl Into<SharedString>,
    on_retry: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Div {
    let title = title.into();
    let retry = text_key(instance, "state", "retry", &title);
    error_box_with_actions(
        tokens,
        instance,
        title,
        message,
        [retry_action(retry, on_retry)],
    )
}

/// A load failure with caller-supplied recovery controls.
///
/// Use this when recovery is more specific than retrying, for example opening
/// settings or copying safe diagnostics. The caller remains responsible for
/// confirmation before any privileged action.
pub fn error_box_with_actions(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    message: impl Into<SharedString>,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    error_box_words(tokens, instance, Words::new(title, message), actions)
}

/// A one-line error above a list that keeps its last data.
pub fn error_banner(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    message: impl Into<SharedString>,
    on_retry: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Div {
    let title = title.into();
    let retry = text_key(instance, "banner", "retry", &title);
    error_banner_with_actions(
        tokens,
        instance,
        title,
        message,
        [retry_action(retry, on_retry)],
    )
}

/// A compact load failure with caller-supplied recovery controls.
pub fn error_banner_with_actions(
    tokens: &Tokens,
    instance: &'static str,
    title: impl Into<SharedString>,
    message: impl Into<SharedString>,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    error_banner_words(tokens, instance, Words::new(title, message), actions)
}

/// A compact load failure, its words' chat choice kept.
pub fn error_banner_words(
    tokens: &Tokens,
    instance: &'static str,
    words: Words,
    actions: impl IntoIterator<Item = impl gpui_kit::IntoElement>,
) -> Div {
    let title_key = text_key(instance, "banner", "title", &words.title);
    let message_key = text_key(instance, "banner", "message", &words.title);
    h_flex()
        .w_full()
        .gap_2()
        .px(tokens.pad_x)
        .py(px(BANNER_VERTICAL_PADDING_PX))
        .border_b_1()
        .border_color(tokens.red)
        .text_size(tokens.row_font)
        .child(div().text_color(tokens.red).child(rich::plain_text_with(
            title_key,
            &words.title,
            words.chat,
        )))
        .child(
            rich::plain_text_with(message_key, &words.message, words.chat)
                .flex_1()
                .min_w_0()
                .text_color(tokens.muted),
        )
        .children(actions)
}

/// The standard Retry action used by error boxes, banners and inline errors.
pub fn retry_action(
    id: impl Into<ElementId>,
    on_retry: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> crate::icon_button::IconButton {
    recovery_action(id, Intent::Primary, IconName::RotateCw, "Retry", on_retry)
}

/// The standard glyph-bearing action for empty and error states, such as
/// Retry, Restart or New tab.
pub fn recovery_action(
    id: impl Into<ElementId>,
    intent: Intent,
    icon: subspace_lucide::SvgIcon,
    label: impl Into<SharedString>,
    on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> crate::icon_button::IconButton {
    action_button(id, intent, Some(icon), label).on_click(on_click)
}

#[cfg(test)]
#[path = "feedback/chat_tests.rs"]
mod chat_tests;

#[cfg(test)]
mod tests {
    use super::{error_banner_with_actions, text_key};
    use crate::Tokens;
    use crate::links;
    use gpui_kit::base::{TextSelection, TextSelectionLayer};
    use gpui_kit::component::button::Button;
    use gpui_kit::component::v_flex;
    use gpui_kit::test::{TestSupportExt as _, TestWindowExt as _};
    use gpui_kit::{
        App, Bounds, Context, FocusHandle, InteractiveElement as _, IntoElement,
        ParentElement as _, Pixels, Render, Styled as _, TestAppContext, Window, point, px,
    };

    struct Configuration {
        opened: bool,
        focus: FocusHandle,
    }

    impl Render for Configuration {
        fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            v_flex()
                .track_focus(&self.focus)
                .child(super::configuration(
                    &Tokens::one_dark(),
                    "setup-test",
                    "No repositories configured",
                    "Add repositories in Settings.",
                ))
                .on_action(cx.listener(|this, _: &crate::ConfigureBoard, _, _| {
                    this.opened = true;
                }))
        }
    }

    #[gpui_kit::test]
    fn configure_dispatches_an_in_app_settings_action(cx: &mut TestAppContext) {
        cx.update(gpui_kit::component::init);
        let (view, cx) = cx.add_window_view(|window, cx| {
            let focus = cx.focus_handle();
            focus.focus(window, cx);
            Configuration {
                opened: false,
                focus,
            }
        });
        cx.update(|window, cx| {
            window.render_frame(cx);
            window.render_frame(cx);
            window.click("setup-test-configure", cx);
        });
        assert!(view.read_with(cx, |view, _| view.opened));
    }

    #[test]
    fn feedback_identities_are_stable_and_distinct() {
        let load = text_key("list-error", "state", "message", "Could not load");
        let empty = text_key("list-empty", "state", "message", "Nothing here");
        let banner = text_key("list-banner", "banner", "message", "Could not load");
        let title = text_key("list-error", "state", "title", "Could not load");
        let retry = text_key("list-error", "state", "retry", "Could not load");
        assert_eq!(load, "feedback-list-error-state-message-Could not load");
        assert_eq!(
            load,
            text_key("list-error", "state", "message", "Could not load")
        );
        assert_ne!(load, empty);
        assert_ne!(load, banner);
        assert_ne!(load, title);
        assert_ne!(load, retry);
        let keys = [load, empty, banner, title, retry];
        let unique = keys.iter().collect::<std::collections::HashSet<_>>();
        assert_eq!(unique.len(), keys.len());
    }

    #[test]
    fn same_title_instances_keep_distinct_identities() {
        let first = text_key("list-empty", "state", "title", "Could not load");
        let second = text_key("list-error", "state", "title", "Could not load");
        assert_ne!(first, second);
        assert_eq!(first, "feedback-list-empty-state-title-Could not load");
        assert_eq!(second, "feedback-list-error-state-title-Could not load");
    }

    struct TwoBanners;

    impl Render for TwoBanners {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let tokens = Tokens::one_dark();
            let none = std::iter::empty::<Button>();
            v_flex()
                .child(TextSelectionLayer)
                .child(
                    v_flex()
                        .id("banner-a")
                        .test_support()
                        .flex_none()
                        .w(px(640.))
                        .child(error_banner_with_actions(
                            &tokens,
                            "feedback-a",
                            "Could not load",
                            "ALPHA unique selectable message",
                            none.clone(),
                        )),
                )
                .child(
                    v_flex()
                        .id("banner-b")
                        .test_support()
                        .flex_none()
                        .w(px(640.))
                        .child(error_banner_with_actions(
                            &tokens,
                            "feedback-b",
                            "Could not load",
                            "BRAVO unique selectable message",
                            none,
                        )),
                )
        }
    }

    fn drag_across(window: &mut Window, cx: &mut App, bounds: Bounds<Pixels>) {
        let y = bounds.origin.y + bounds.size.height / 2.;
        let start = point(bounds.origin.x + bounds.size.width * 0.20, y);
        let end = point(bounds.origin.x + bounds.size.width * 0.70, y);
        window.drag(start, end, cx);
    }

    fn copied_selection(window: &mut Window, cx: &mut App) -> String {
        let selected = TextSelection::selected_text(window, cx);
        links::copy(selected.clone(), cx);
        let copied = cx
            .read_from_clipboard()
            .and_then(|item| item.text().map(|text| text.to_string()));
        assert_eq!(copied.as_deref(), Some(selected.as_str()));
        selected
    }

    #[gpui_kit::test]
    fn same_title_banners_select_and_copy_distinct_text(cx: &mut TestAppContext) {
        let (_, cx) = cx.add_window_view(|_, _| TwoBanners);
        cx.update(|window, cx| {
            window.render_frame(cx);
            window.render_frame(cx);
            drag_across(window, cx, window.find("banner-a").bounds());
            let first_text = copied_selection(window, cx);
            assert!(first_text.contains("ALPHA"), "first {first_text:?}");
            assert!(!first_text.contains("BRAVO"), "first {first_text:?}");
            window.render_frame(cx);
            let second_bounds = window.find("banner-b").bounds();
            drag_across(window, cx, second_bounds);
            let second_text = copied_selection(window, cx);
            assert!(
                second_text.contains("BRAVO"),
                "second {second_text:?} bounds={second_bounds:?}"
            );
            assert!(!second_text.contains("ALPHA"), "second {second_text:?}");
        });
    }
}