gpui-box-kit 0.1.1

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
//! A navigation rail of sectioned places, expanded or collapsed to icons.
//!
//! Where the typist is, is caller-owned. The sidebar reports the place that was
//! picked and marks whatever the caller says is current, so a host that refuses
//! a move keeps the place that still holds highlighted.
//!
//! Collapsing is a change of drawing, never of substance. A collapsed rail
//! shows glyphs and reaches the label through a [`Tooltip`](crate::overlay::Tooltip), and every item
//! still publishes its full name, so nothing becomes unaddressable by being
//! made narrow.

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, Radius, Space, TextTone, Theme, TypeScale};

use crate::display::badge::Badge;
use crate::foundation::{
    Disableable, FocusRing, Ident, Pressable, Sizable, StyledExt, text as foundation_text,
};
use crate::motion::{Flipping, flip};
use crate::overlay::Tooltipped;

/// How wide the rail is expanded, and how wide it is collapsed to glyphs.
/// Neither value repeats anywhere else.
const EXPANDED_WIDTH: f32 = 240.0;
const COLLAPSED_WIDTH: f32 = 52.0;

/// How far a nested item is indented from its parent in the expanded rail.
const NESTING_INDENT: f32 = 16.0;

type SelectHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;

/// One place in the rail.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SidebarItem {
    id: SharedString,
    label: SharedString,
    icon: Option<Icon>,
    badge: Option<SharedString>,
    disabled: bool,
    children: Vec<SidebarItem>,
}

impl SidebarItem {
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            icon: None,
            badge: None,
            disabled: false,
            children: Vec::new(),
        }
    }

    pub fn icon(mut self, glyph: Icon) -> Self {
        self.icon = Some(glyph);
        self
    }

    /// A count or a state shown next to the label, such as how many runs a
    /// place holds.
    pub fn badge(mut self, badge: impl Into<SharedString>) -> Self {
        self.badge = Some(badge.into());
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Nested places, one level deep.
    ///
    /// A rail deeper than that stops being a rail, so anything nested inside a
    /// child is dropped here rather than drawn at a depth the sidebar cannot
    /// lay out or publish.
    pub fn children(mut self, children: impl IntoIterator<Item = SidebarItem>) -> Self {
        self.children = children
            .into_iter()
            .map(|child| SidebarItem {
                children: Vec::new(),
                ..child
            })
            .collect();
        self
    }

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

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

/// A titled run of places.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SidebarSection {
    id: SharedString,
    title: Option<SharedString>,
    items: Vec<SidebarItem>,
}

impl SidebarSection {
    pub fn new(id: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            title: None,
            items: Vec::new(),
        }
    }

    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn item(mut self, item: SidebarItem) -> Self {
        self.items.push(item);
        self
    }

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

/// A collapsible navigation rail.
#[derive(IntoElement)]
pub struct Sidebar {
    ident: Ident,
    sections: Vec<SidebarSection>,
    active: Option<SharedString>,
    collapsed: bool,
    header: Option<AnyElement>,
    footer: Option<AnyElement>,
    disabled: bool,
    size: ControlSize,
    on_select: Option<SelectHandler>,
}

impl std::fmt::Debug for Sidebar {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Sidebar")
            .field("ident", &self.ident)
            .field("sections", &self.sections.len())
            .field("active", &self.active)
            .field("collapsed", &self.collapsed)
            .field("disabled", &self.disabled)
            .field("has_handler", &self.on_select.is_some())
            .finish()
    }
}

impl Sidebar {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            sections: Vec::new(),
            active: None,
            collapsed: false,
            header: None,
            footer: None,
            disabled: false,
            size: ControlSize::Md,
            on_select: None,
        }
    }

    pub fn section(mut self, section: SidebarSection) -> Self {
        self.sections.push(section);
        self
    }

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

    /// The place the caller says is current. The sidebar marks it and never
    /// moves it.
    pub fn active(mut self, id: impl Into<SharedString>) -> Self {
        self.active = Some(id.into());
        self
    }

    /// Draws glyphs only, with each label reachable as hover help.
    pub fn collapsed(mut self, collapsed: bool) -> Self {
        self.collapsed = collapsed;
        self
    }

    pub fn header(mut self, header: impl IntoElement) -> Self {
        self.header = Some(header.into_any_element());
        self
    }

    /// The slot at the bottom of the rail, kept in both widths.
    pub fn footer(mut self, footer: impl IntoElement) -> Self {
        self.footer = Some(footer.into_any_element());
        self
    }

    pub fn on_select(
        mut self,
        handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_select = Some(Rc::new(handler));
        self
    }

    fn item_element(
        &self,
        item: &SidebarItem,
        level: u32,
        theme: &Theme,
        window: &mut Window,
        cx: &mut App,
    ) -> AnyElement {
        let metrics = theme.control.get(self.size);
        let ident = self.ident.child(item.id.as_ref());
        let active = self.active.as_ref() == Some(&item.id);
        let disabled = self.disabled || item.disabled;
        let actionable = !disabled && self.on_select.is_some();
        let color = if disabled {
            theme.colors.text_faint
        } else if active {
            theme.colors.text
        } else {
            theme.colors.text_muted
        };
        let indent = if self.collapsed || level == 1 {
            0.0
        } else {
            NESTING_INDENT
        };
        let glyph_slot = flip(ident.child("glyph").semantic_id(), cx);

        let mut row = div()
            .id(ident.element_id())
            .flex()
            .flex_row()
            .items_center()
            .h(px(metrics.height))
            .w_full()
            .gap(px(theme.space(Space::Sm)))
            .pl(px(theme.space(Space::Sm) + indent))
            .pr(px(theme.space(Space::Sm)))
            .radius(theme, Radius::Control)
            .when(self.collapsed, |element| element.justify_center())
            .when(active, |element| element.bg(theme.colors.selected))
            .when(disabled, |element| element.opacity(theme.opacity.disabled))
            .child(
                // The glyph is the one thing that survives collapsing, so it
                // travels to its narrow position rather than being redrawn
                // there.
                div()
                    .flex()
                    .flex_none()
                    .w(px(metrics.icon_size))
                    .justify_center()
                    .children(
                        item.icon
                            .map(|glyph| icon(glyph).size(px(metrics.icon_size)).text_color(color)),
                    )
                    .flip(&glyph_slot, window, cx),
            )
            .when(!self.collapsed, |element| {
                element
                    .child(
                        div().flex_1().overflow_hidden().child(
                            foundation_text(theme, TypeScale::Label, item.label.clone())
                                .text_size(px(metrics.font_size))
                                .text_color(color),
                        ),
                    )
                    .children(item.badge.clone().map(|badge| Badge::new(badge).neutral()))
            })
            .when(actionable, |element| {
                element
                    .cursor_pointer()
                    .tab_index(0)
                    .pressable(cx)
                    .hover(|style| style.bg(theme.colors.hover))
                    .focus_ring(theme)
            });

        // A narrow rail hides the wording, so the wording has to reach the
        // typist some other way.
        if self.collapsed {
            row = row.tip(ident.clone(), item.label.clone());
        }

        if let (true, Some(handler)) = (actionable, self.on_select.clone()) {
            let id = item.id.clone();
            let click = Rc::clone(&handler);
            let clicked = id.clone();
            row = row
                .on_click(move |_, window, cx| click(clicked.clone(), window, cx))
                .on_key_down(move |event, window, cx| {
                    if matches!(event.keystroke.key.as_str(), "enter" | "space") {
                        handler(id.clone(), window, cx);
                        cx.stop_propagation();
                    }
                });
        }

        let mut spec = NodeSpec::new(ident.semantic_id(), Role::Link)
            .parent(self.ident.semantic_id())
            .selected(active)
            .disabled(disabled)
            .level(level)
            // A collapsed rail draws a glyph and still says what it is.
            .text(item.label.clone());
        if let Some(badge) = item.badge.clone() {
            spec = spec.value(badge);
        }

        row.semantic_in(cx, spec).into_any_element()
    }
}

impl Disableable for Sidebar {
    /// Freezes the whole rail. A frozen rail installs no handler at all.
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

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

impl RenderOnce for Sidebar {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let mut body = div()
            .flex()
            .flex_col()
            .flex_1()
            .gap(px(theme.space(Space::Md)))
            .overflow_hidden();

        for section in &self.sections {
            let section_ident = self.ident.child(section.id.as_ref());
            let mut rows: Vec<AnyElement> = Vec::new();
            for item in &section.items {
                rows.push(self.item_element(item, 1, &theme, window, cx));
                for child in &item.children {
                    rows.push(self.item_element(child, 2, &theme, window, cx));
                }
            }

            // A collapsed rail has no room for a caption, so the run is
            // separated by a rule instead of titled.
            let heading = section
                .title
                .clone()
                .filter(|_| !self.collapsed)
                .map(|title| {
                    foundation_text(&theme, TypeScale::Caption, title.clone())
                        .px(px(theme.space(Space::Sm)))
                        .text_tone(&theme, TextTone::Faint)
                        .semantic_in(
                            cx,
                            NodeSpec::new(section_ident.semantic_id(), Role::Heading)
                                .parent(self.ident.semantic_id())
                                .level(1)
                                .text(title),
                        )
                });

            body = body.child(
                div()
                    .flex()
                    .flex_col()
                    .gap(px(theme.space(Space::Xs)))
                    .children(heading)
                    .children(rows),
            );
        }

        div()
            .id(self.ident.element_id())
            .flex()
            .flex_col()
            .h_full()
            .w(px(if self.collapsed {
                COLLAPSED_WIDTH
            } else {
                EXPANDED_WIDTH
            }))
            .gap(px(theme.space(Space::Md)))
            .p(px(theme.space(Space::Sm)))
            .bg(theme.colors.panel)
            .children(self.header)
            .child(body)
            .children(self.footer)
            .semantic_in(
                cx,
                NodeSpec::new(self.ident.semantic_id(), Role::List).expanded(!self.collapsed),
            )
    }
}