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
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
//! What is currently permitted, across a set of subjects and actions.
//!
//! # A cell has four states, not two
//!
//! [`PermissionState`] keeps `NotApplicable` apart from `Denied`. "This tool
//! has no network to reach" and "this tool is refused the network" are
//! different sentences: the first says the question does not arise, the
//! second says somebody answered it no. Drawing them the same way would
//! invent a refusal nobody made, so they are different states, different
//! wording, and different published values.
//!
//! # Where a permission came from
//!
//! A permission inherited from a broader rule is not the same as one set in
//! this matrix, and a reader who cannot tell them apart cannot tell what
//! changing a cell would actually do. The component therefore *carries and
//! shows* provenance through [`PermissionSource`], and *derives* none of it:
//! working out which rule won is policy evaluation over a rule set the host
//! owns, which is exactly the kind of fact this library takes as an input
//! rather than computing. Every cell says either where it was inherited from,
//! in the host's own words, or that it was set here.
//!
//! # Read-only and editable
//!
//! A matrix with no `on_change` handler is read-only: its cells publish
//! [`Role::Cell`] and install nothing. A cell whose state is `NotApplicable`
//! installs nothing either, in an editable matrix as much as a read-only one,
//! because there is no state for it to cycle to.

use std::rc::Rc;

use gpui::{
    App, InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString,
    StatefulInteractiveElement, Styled, Window, div, px,
};
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{ActiveTheme, Elevation, Radius, Space, Surface, TextTone, Theme, TypeScale};

use crate::foundation::{FocusRing, Ident, StyledExt, text};
use crate::strings::{ActiveStrings, StringKey};

type ChangeHandler = Rc<dyn Fn(PermissionChange, &mut Window, &mut App)>;

/// What a cell says about one subject and one action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PermissionState {
    /// Goes ahead without asking.
    Allowed,
    /// Refused. Somebody decided this.
    Denied,
    /// Permitted only after somebody says so, every time.
    #[default]
    Ask,
    /// The question does not arise here. Never a refusal.
    NotApplicable,
}

impl PermissionState {
    /// The stable name published in the semantic tree.
    pub fn name(self) -> &'static str {
        match self {
            Self::Allowed => "allowed",
            Self::Denied => "denied",
            Self::Ask => "ask",
            Self::NotApplicable => "not-applicable",
        }
    }

    /// The wording a reader reads.
    pub fn label(self, cx: &App) -> SharedString {
        cx.strings().text(match self {
            Self::Allowed => StringKey::PermissionAllowed,
            Self::Denied => StringKey::PermissionDenied,
            Self::Ask => StringKey::PermissionAsk,
            Self::NotApplicable => StringKey::PermissionNotApplicable,
        })
    }

    /// What operating the cell would ask for next.
    ///
    /// `NotApplicable` has no next state: a question that does not arise
    /// cannot be answered by clicking.
    pub fn next(self) -> Option<Self> {
        match self {
            Self::Allowed => Some(Self::Ask),
            Self::Ask => Some(Self::Denied),
            Self::Denied => Some(Self::Allowed),
            Self::NotApplicable => None,
        }
    }

    fn color(self, theme: &Theme) -> gpui::Hsla {
        match self {
            Self::Allowed => theme.colors.success,
            Self::Denied => theme.colors.danger,
            Self::Ask => theme.colors.warning,
            Self::NotApplicable => theme.colors.text_faint,
        }
    }
}

/// Where a cell's state was decided.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum PermissionSource {
    /// Set on this subject, for this action.
    #[default]
    Here,
    /// Inherited from a broader rule, named in the host's own words.
    Inherited(SharedString),
}

impl PermissionSource {
    pub fn inherited(from: impl Into<SharedString>) -> Self {
        Self::Inherited(from.into())
    }

    /// The stable name published in the semantic tree.
    pub fn name(&self) -> &'static str {
        match self {
            Self::Here => "here",
            Self::Inherited(_) => "inherited",
        }
    }

    pub fn label(&self, cx: &App) -> SharedString {
        match self {
            Self::Here => cx.strings().text(StringKey::PermissionSetHere),
            Self::Inherited(from) => cx.strings().format(StringKey::PermissionInherited, &[from]),
        }
    }
}

/// One cell: a state, and where it came from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionEntry {
    state: PermissionState,
    source: PermissionSource,
}

impl PermissionEntry {
    /// A state decided in this matrix.
    pub fn new(state: PermissionState) -> Self {
        Self {
            state,
            source: PermissionSource::Here,
        }
    }

    /// A state a broader rule decided, naming that rule.
    pub fn inherited(state: PermissionState, from: impl Into<SharedString>) -> Self {
        Self {
            state,
            source: PermissionSource::inherited(from),
        }
    }

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

    pub fn source(&self) -> &PermissionSource {
        &self.source
    }
}

/// One column: an action, addressed by a stable key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionAction {
    key: SharedString,
    label: SharedString,
}

impl PermissionAction {
    pub fn new(key: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            key: key.into(),
            label: label.into(),
        }
    }

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

/// One row: whatever the permissions are about, and its cells.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionSubject {
    id: SharedString,
    label: SharedString,
    cells: Vec<(SharedString, PermissionEntry)>,
}

impl PermissionSubject {
    pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            cells: Vec::new(),
        }
    }

    /// States one cell. An action this subject never states is rendered
    /// `NotApplicable`, which is the honest reading of a row that does not
    /// mention it — and is still not a refusal.
    pub fn cell(mut self, action: impl Into<SharedString>, entry: PermissionEntry) -> Self {
        self.cells.push((action.into(), entry));
        self
    }

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

    fn entry(&self, action: &SharedString) -> PermissionEntry {
        self.cells
            .iter()
            .find(|(key, _)| key == action)
            .map(|(_, entry)| entry.clone())
            .unwrap_or_else(|| PermissionEntry::new(PermissionState::NotApplicable))
    }
}

/// What operating a cell asks for. Nothing is applied.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionChange {
    pub subject: SharedString,
    pub action: SharedString,
    pub next: PermissionState,
}

/// A grid of subjects against actions.
#[derive(IntoElement)]
pub struct PermissionMatrix {
    ident: Ident,
    actions: Vec<PermissionAction>,
    subjects: Vec<PermissionSubject>,
    on_change: Option<ChangeHandler>,
}

impl std::fmt::Debug for PermissionMatrix {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PermissionMatrix")
            .field("ident", &self.ident)
            .field("actions", &self.actions.len())
            .field("subjects", &self.subjects.len())
            .field("editable", &self.on_change.is_some())
            .finish()
    }
}

impl PermissionMatrix {
    pub fn new(ident: impl Into<Ident>) -> Self {
        Self {
            ident: ident.into(),
            actions: Vec::new(),
            subjects: Vec::new(),
            on_change: None,
        }
    }

    pub fn action(mut self, action: PermissionAction) -> Self {
        self.actions.push(action);
        self
    }

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

    pub fn subject(mut self, subject: PermissionSubject) -> Self {
        self.subjects.push(subject);
        self
    }

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

    /// Makes the matrix editable. Without a handler it is read-only, and a
    /// read-only cell installs nothing rather than installing a handler that
    /// does nothing.
    pub fn on_change(
        mut self,
        handler: impl Fn(PermissionChange, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_change = Some(Rc::new(handler));
        self
    }
}

impl RenderOnce for PermissionMatrix {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let heading = div()
            .row()
            .w_full()
            .gap_token(&theme, Space::Sm)
            .px_token(&theme, Space::Sm)
            .py_token(&theme, Space::Xs)
            .child(
                div().w(px(160.0)).flex_none().child(
                    text(
                        &theme,
                        TypeScale::Caption,
                        cx.strings().text(StringKey::PermissionSubjectHeading),
                    )
                    .text_tone(&theme, TextTone::Faint),
                ),
            )
            .children(self.actions.iter().map(|action| {
                div().flex_1().min_w_0().child(
                    text(&theme, TypeScale::Caption, action.label.clone())
                        .text_tone(&theme, TextTone::Faint),
                )
            }));

        let rows: Vec<_> = self
            .subjects
            .iter()
            .map(|subject| {
                let row_ident = self.ident.child(subject.id.as_ref());
                div()
                    .row()
                    .items_start()
                    .w_full()
                    .gap_token(&theme, Space::Sm)
                    .px_token(&theme, Space::Sm)
                    .py_token(&theme, Space::Xs)
                    .child(
                        div()
                            .w(px(160.0))
                            .flex_none()
                            .child(text(&theme, TypeScale::Label, subject.label.clone()))
                            .semantic_in(
                                cx,
                                NodeSpec::new(row_ident.semantic_id(), Role::Row)
                                    .text(subject.label.clone())
                                    .parent(self.ident.semantic_id()),
                            ),
                    )
                    .children(self.actions.iter().map(|action| {
                        cell(
                            &theme,
                            &row_ident,
                            subject,
                            action,
                            self.on_change.clone(),
                            cx,
                        )
                    }))
            })
            .collect();

        div()
            .column()
            .w_full()
            .radius(&theme, Radius::Card)
            .frame(&theme, Surface::Panel, Elevation::Raised)
            .child(heading)
            .children(rows)
            .semantic_in(
                cx,
                NodeSpec::new(self.ident.semantic_id(), Role::Table)
                    .value(SharedString::from(self.subjects.len().to_string())),
            )
    }
}

fn cell(
    theme: &Theme,
    row_ident: &Ident,
    subject: &PermissionSubject,
    action: &PermissionAction,
    on_change: Option<ChangeHandler>,
    cx: &App,
) -> gpui::AnyElement {
    let entry = subject.entry(&action.key);
    let state = entry.state();
    let ident = row_ident.child(action.key.as_ref());
    let name = cx.strings().format(
        StringKey::PermissionCellName,
        &[&action.label, &state.label(cx)],
    );
    let next = state.next();
    let handler = on_change.zip(next);

    let mark = div()
        .row()
        .gap_token(theme, Space::Xs)
        .child(
            div()
                .flex_none()
                .size(px(7.0))
                .rounded_full()
                .bg(state.color(theme)),
        )
        .child(text(theme, TypeScale::Label, state.label(cx)));

    // Provenance is shown on every cell that has a state, so "set here" is a
    // statement rather than the absence of one.
    let source = (state != PermissionState::NotApplicable).then(|| {
        text(theme, TypeScale::Caption, entry.source().label(cx))
            .text_tone(theme, TextTone::Faint)
            .semantic_in(
                cx,
                NodeSpec::new(ident.child("source").semantic_id(), Role::Text)
                    .text(entry.source().label(cx))
                    .value(SharedString::new_static(entry.source().name()))
                    .parent(ident.semantic_id()),
            )
    });

    let body = div()
        .column()
        .gap_token(theme, Space::Xs)
        .child(mark)
        .children(source);

    let spec = NodeSpec::new(
        ident.semantic_id(),
        if handler.is_some() {
            Role::Button
        } else {
            Role::Cell
        },
    )
    .text(name)
    .value(SharedString::new_static(state.name()))
    .parent(row_ident.semantic_id());

    // Both arms carry a border so the two matrices line up; only the editable
    // one is drawn. Hover and the focus ring arrive too late to answer "can I
    // change this?", and for a permission that question has to be answerable
    // at rest.
    let frame = div()
        .flex_1()
        .min_w_0()
        .px_token(theme, Space::Sm)
        .py_token(theme, Space::Xs)
        .hairline(theme)
        .radius(theme, Radius::Control);

    match handler {
        Some((handler, next)) => {
            let subject_id = subject.id.clone();
            let action_key = action.key.clone();
            frame
                .id(ident.element_id())
                .tab_index(0)
                .cursor_pointer()
                .hover(|style| style.bg(theme.colors.hover))
                .focus_ring(theme)
                .on_click(move |_event, window, cx| {
                    handler(
                        PermissionChange {
                            subject: subject_id.clone(),
                            action: action_key.clone(),
                            next,
                        },
                        window,
                        cx,
                    );
                })
                .child(body)
                .semantic_in(cx, spec)
                .into_any_element()
        }
        None => frame
            .border_color(gpui::transparent_black())
            .child(body)
            .semantic_in(cx, spec)
            .into_any_element(),
    }
}