subspace-dashboard 0.1.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
//! The type-erased dashboard handle the app shell holds, and the
//! object-safe part of [`Dashboard`] behind it.

use gpui_kit::{
    AnyElement, AnyView, App, AppContext, Entity, FocusHandle, SharedString, Subscription, Window,
};

use crate::{AgentContext, Dashboard, DashboardEvent, KeyHelp, Modal, PaletteCommand, Row, Search};

/// Receives a dashboard's events in the app.
pub type EventSink = Box<dyn Fn(&DashboardEvent, &mut Window, &mut App)>;

/// The object-safe part of [`Dashboard`], for [`DashboardHandle`].
// qual:allow(coupling, sit) reason: "The generic Entity<D> impl erases each concrete dashboard for the shell's heterogeneous handle list; Dashboard has associated constants and is not object-safe."
trait AnyDashboard {
    fn view(&self) -> AnyView;
    fn focus_handle(&self, cx: &App) -> FocusHandle;
    fn activate(&self, window: &mut Window, cx: &mut App);
    fn deactivate(&self, window: &mut Window, cx: &mut App);
    fn covered(&self, covered: bool, window: &mut Window, cx: &mut App);
    fn open_notification(&self, target: u64, window: &mut Window, cx: &mut App);
    fn window_title(&self, cx: &App) -> Option<SharedString>;
    fn diagnostic_config_path(&self, cx: &App) -> Option<std::path::PathBuf>;
    fn background_refresh(&self, cx: &mut App);
    fn render_toolbar(&self, window: &mut Window, cx: &mut App) -> Option<AnyElement>;
    fn status(&self, cx: &App) -> Option<SharedString>;
    fn badge(&self, cx: &App) -> Option<SharedString>;
    fn busy(&self, cx: &App) -> bool;
    fn subject(&self, cx: &App) -> Option<Row>;
    fn agent_context(&self, cx: &App) -> Option<AgentContext>;
    fn commands(&self, cx: &mut App) -> Vec<PaletteCommand>;
    fn search(&self, cx: &App) -> Option<Search>;
    fn open_search_result(&self, id: &str, window: &mut Window, cx: &mut App);
    fn open_link(&self, url: &str, cx: &mut App) -> bool;
    fn open_indexed_result_fenced(
        &self,
        hit: &subspace_search::Hit,
        permit: subspace_search::ReadPermit,
        window: &mut Window,
        cx: &mut App,
    );
    fn modal(&self, cx: &App) -> Option<Modal>;
    fn dismiss(&self, window: &mut Window, cx: &mut App);
    fn keys(&self, cx: &App) -> &'static [KeyHelp];
    fn help_foot(&self, cx: &App) -> Option<SharedString>;
    fn quit_warnings(&self, cx: &App) -> Vec<SharedString>;
    fn reload(&self, window: &mut Window, cx: &mut App);
    fn subscribe(&self, window: &mut Window, cx: &mut App, sink: EventSink) -> Subscription;
}

impl<D: Dashboard> AnyDashboard for Entity<D> {
    fn open_notification(&self, target: u64, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| {
            dashboard.open_notification(target, window, cx)
        });
    }
    fn view(&self) -> AnyView {
        self.clone().into()
    }

    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.read(cx).focus_handle(cx)
    }

    fn activate(&self, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.activate(window, cx));
    }

    fn deactivate(&self, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.deactivate(window, cx));
    }

    fn covered(&self, covered: bool, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.covered(covered, window, cx));
    }

    fn window_title(&self, cx: &App) -> Option<SharedString> {
        self.read(cx).window_title(cx)
    }

    fn diagnostic_config_path(&self, cx: &App) -> Option<std::path::PathBuf> {
        self.read(cx).diagnostic_config_path(cx)
    }

    fn background_refresh(&self, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.background_refresh(cx));
    }

    fn render_toolbar(&self, window: &mut Window, cx: &mut App) -> Option<AnyElement> {
        self.update(cx, |dashboard, cx| dashboard.render_toolbar(window, cx))
    }

    fn status(&self, cx: &App) -> Option<SharedString> {
        self.read(cx).status(cx)
    }

    fn badge(&self, cx: &App) -> Option<SharedString> {
        self.read(cx).badge(cx)
    }

    fn busy(&self, cx: &App) -> bool {
        self.read(cx).busy(cx)
    }

    fn subject(&self, cx: &App) -> Option<Row> {
        self.read(cx).subject(cx)
    }

    fn agent_context(&self, cx: &App) -> Option<AgentContext> {
        self.read(cx).agent_context(cx)
    }

    fn commands(&self, cx: &mut App) -> Vec<PaletteCommand> {
        self.update(cx, |dashboard, cx| dashboard.commands(cx))
    }

    fn search(&self, cx: &App) -> Option<Search> {
        self.read(cx).search(cx)
    }

    fn open_search_result(&self, id: &str, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| {
            dashboard.open_search_result(id, window, cx);
        });
    }

    fn open_link(&self, url: &str, cx: &mut App) -> bool {
        self.update(cx, |dashboard, cx| dashboard.open_link(url, cx))
    }

    fn open_indexed_result_fenced(
        &self,
        hit: &subspace_search::Hit,
        permit: subspace_search::ReadPermit,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.update(cx, |dashboard, cx| {
            dashboard.open_indexed_result_fenced(hit, permit, window, cx)
        });
    }

    fn modal(&self, cx: &App) -> Option<Modal> {
        self.read(cx).modal(cx)
    }

    fn dismiss(&self, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.dismiss(window, cx));
    }

    fn keys(&self, cx: &App) -> &'static [KeyHelp] {
        self.read(cx).keys()
    }

    fn help_foot(&self, cx: &App) -> Option<SharedString> {
        self.read(cx).help_foot()
    }

    fn quit_warnings(&self, cx: &App) -> Vec<SharedString> {
        self.read(cx).quit_warnings(cx)
    }

    fn reload(&self, window: &mut Window, cx: &mut App) {
        self.update(cx, |dashboard, cx| dashboard.reload(window, cx));
    }

    fn subscribe(&self, window: &mut Window, cx: &mut App, sink: EventSink) -> Subscription {
        window.subscribe(self, cx, move |_, event: &DashboardEvent, window, cx| {
            sink(event, window, cx);
        })
    }
}

/// A created dashboard, with its type erased so the app can hold a list.
pub struct DashboardHandle {
    pub id: &'static str,
    pub title: SharedString,
    pub description: SharedString,
    pub icon: Option<&'static str>,
    /// [`Dashboard::HELP_MARKDOWN`], empty when the board has no help.
    pub help: &'static str,
    pub help_images: &'static [crate::HelpImage],
    pub searchable: bool,
    inner: Box<dyn AnyDashboard>,
}

/// The dashboard types whose keys are bound.
#[derive(Default)]
struct BoundKeys(std::collections::HashSet<std::any::TypeId>);

impl gpui_kit::Global for BoundKeys {}

/// Records `D` as bound and reports whether it was not bound before.
fn first_creation<D: Dashboard>(cx: &mut App) -> bool {
    cx.default_global::<BoundKeys>()
        .0
        .insert(std::any::TypeId::of::<D>())
}

impl DashboardHandle {
    // qual:api - shell notification navigation adapter.
    pub fn open_notification(&self, target: u64, window: &mut Window, cx: &mut App) {
        self.inner.open_notification(target, window, cx);
    }
    /// Create dashboard `D`. Settings can create a board again after it
    /// disables it, so `D`'s keys are bound once per process.
    pub fn new<D: Dashboard>(window: &mut Window, cx: &mut App) -> Self {
        first_creation::<D>(cx).then(|| D::bind_keys(cx));
        let entity = cx.new(|cx| D::new(window, cx));
        Self::from_entity(entity)
    }

    /// Wrap a dashboard the caller created itself, such as one of several
    /// configured instances of the same type. The registry replaces the
    /// type's identity with the instance's.
    pub fn from_entity<D: Dashboard>(entity: Entity<D>) -> Self {
        Self {
            id: D::ID,
            title: D::TITLE.into(),
            description: D::DESCRIPTION.into(),
            icon: D::ICON,
            help: D::HELP_MARKDOWN,
            help_images: D::HELP_IMAGES,
            searchable: D::SEARCHABLE,
            inner: Box::new(entity),
        }
    }

    pub fn view(&self) -> AnyView {
        self.inner.view()
    }

    pub fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.inner.focus_handle(cx)
    }

    pub fn activate(&self, window: &mut Window, cx: &mut App) {
        self.inner.activate(window, cx);
    }

    pub fn deactivate(&self, window: &mut Window, cx: &mut App) {
        self.inner.deactivate(window, cx);
    }

    /// See [`Dashboard::covered`].
    pub fn covered(&self, covered: bool, window: &mut Window, cx: &mut App) {
        self.inner.covered(covered, window, cx);
    }

    // qual:api - the app shell titles the window from it.
    pub fn window_title(&self, cx: &App) -> Option<SharedString> {
        self.inner.window_title(cx)
    }

    /// Active source for the issue form's explicit Include paths consent.
    pub fn diagnostic_config_path(&self, cx: &App) -> Option<std::path::PathBuf> {
        self.inner.diagnostic_config_path(cx)
    }

    pub fn background_refresh(&self, cx: &mut App) {
        self.inner.background_refresh(cx);
    }

    // qual:api - the app shell draws the dashboard toolbar with it.
    pub fn render_toolbar(&self, window: &mut Window, cx: &mut App) -> Option<AnyElement> {
        self.inner.render_toolbar(window, cx)
    }

    pub fn status(&self, cx: &App) -> Option<SharedString> {
        self.inner.status(cx)
    }

    /// The mark the switcher tab shows, if the dashboard offers one.
    // qual:api - the app shell draws it on the switcher tab.
    pub fn badge(&self, cx: &App) -> Option<SharedString> {
        self.inner.badge(cx)
    }

    pub fn busy(&self, cx: &App) -> bool {
        self.inner.busy(cx)
    }

    pub fn subject(&self, cx: &App) -> Option<Row> {
        self.inner.subject(cx)
    }

    // qual:api - GPUI adapter; pure context conversion is tested in dashboard crates.
    pub fn agent_context(&self, cx: &App) -> Option<AgentContext> {
        self.inner.agent_context(cx)
    }

    pub fn commands(&self, cx: &mut App) -> Vec<PaletteCommand> {
        self.inner.commands(cx)
    }

    pub fn search(&self, cx: &App) -> Option<Search> {
        self.inner.search(cx)
    }

    // qual:api - the app shell opens a palette hit with it.
    pub fn open_search_result(&self, id: &str, window: &mut Window, cx: &mut App) {
        self.inner.open_search_result(id, window, cx);
    }

    pub fn open_link(&self, url: &str, cx: &mut App) -> bool {
        self.inner.open_link(url, cx)
    }

    pub fn open_indexed_result_fenced(
        &self,
        hit: &subspace_search::Hit,
        permit: subspace_search::ReadPermit,
        window: &mut Window,
        cx: &mut App,
    ) {
        self.inner
            .open_indexed_result_fenced(hit, permit, window, cx);
    }

    pub fn modal(&self, cx: &App) -> Option<Modal> {
        self.inner.modal(cx)
    }

    pub fn dismiss(&self, window: &mut Window, cx: &mut App) {
        self.inner.dismiss(window, cx);
    }

    pub fn keys(&self, cx: &App) -> &'static [KeyHelp] {
        self.inner.keys(cx)
    }

    // qual:api - the app shell adds a footnote to the help sheet.
    pub fn help_foot(&self, cx: &App) -> Option<SharedString> {
        self.inner.help_foot(cx)
    }

    pub fn quit_warnings(&self, cx: &App) -> Vec<SharedString> {
        self.inner.quit_warnings(cx)
    }

    // qual:api - the app shell reloads the dashboard with it.
    pub fn reload(&self, window: &mut Window, cx: &mut App) {
        self.inner.reload(window, cx);
    }

    /// Call `sink` with each event the dashboard emits, while the returned
    /// subscription lives.
    pub fn subscribe(&self, window: &mut Window, cx: &mut App, sink: EventSink) -> Subscription {
        self.inner.subscribe(window, cx, sink)
    }
}

#[cfg(test)]
mod tests {
    use gpui_kit::{
        App, Context, EventEmitter, FocusHandle, Focusable, Global, IntoElement, Render,
        TestAppContext, Window, div,
    };

    use super::DashboardHandle;
    use crate::{Dashboard, DashboardEvent};

    #[derive(Default)]
    struct Binds(usize);
    impl Global for Binds {}

    struct Board {
        focus: FocusHandle,
        source: Option<std::path::PathBuf>,
    }
    impl EventEmitter<DashboardEvent> for Board {}
    impl Focusable for Board {
        fn focus_handle(&self, _: &App) -> FocusHandle {
            self.focus.clone()
        }
    }
    impl Render for Board {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            div()
        }
    }
    impl Dashboard for Board {
        const ID: &'static str = "board";
        const TITLE: &'static str = "Board";
        fn new(_: &mut Window, cx: &mut Context<Self>) -> Self {
            Self {
                focus: cx.focus_handle(),
                source: None,
            }
        }
        fn bind_keys(cx: &mut App) {
            cx.default_global::<Binds>().0 += 1;
        }
        fn diagnostic_config_path(&self, _: &App) -> Option<std::path::PathBuf> {
            self.source.clone()
        }
        fn reload(&mut self, _: &mut Window, _: &mut Context<Self>) {}
    }

    #[gpui_kit::test]
    fn creating_a_board_again_binds_its_keys_once(cx: &mut TestAppContext) {
        let window = cx.add_window(Board::new);
        window
            .update(cx, |_, window, cx| {
                let first = DashboardHandle::new::<Board>(window, cx);
                let second = DashboardHandle::new::<Board>(window, cx);
                assert_ne!(first.focus_handle(cx), second.focus_handle(cx));
                assert_eq!(cx.global::<Binds>().0, 1);
            })
            .unwrap();
    }

    #[gpui_kit::test]
    fn diagnostic_path_capability_reads_the_current_board_without_loading_a_file(
        cx: &mut TestAppContext,
    ) {
        let (board, cx) = cx.add_window_view(Board::new);
        let handle = DashboardHandle::from_entity(board.clone());
        cx.update(|_, cx| {
            assert_eq!(handle.diagnostic_config_path(cx), None);
            board.update(cx, |board, _| {
                board.source = Some("/fixture/config/active.json".into());
            });
            assert_eq!(
                handle.diagnostic_config_path(cx),
                Some(std::path::PathBuf::from("/fixture/config/active.json")),
            );
        });
    }
}