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
//! Text that becomes a field when it is asked to.
//!
//! Whether the field is open, and what it holds when it opens, are the
//! caller's. `InlineEdit` reports that an edit was requested, committed, or
//! abandoned, and writes nothing.
//!
//! # A failed save keeps what was typed
//!
//! Losing someone's typing because the host refused the save is the worst
//! thing this component could do. So the field is built once when an editing
//! session opens and is left alone for as long as the session stays open: a
//! caller that answers a commit with [`InlineEdit::failure`] and keeps
//! `editing` true gets the typed text still there, under the reason it did not
//! save.

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use gpui::{
    App, AppContext, Entity, Focusable, Global, InteractiveElement, IntoElement, ParentElement,
    RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div,
    prelude::FluentBuilder, px,
};
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{ActiveTheme, ControlSize, Radius, Space, TypeScale};

use crate::controls::input::{self, TextInput};
use crate::controls::textarea::{self, TextArea};
use crate::foundation::{
    Disableable, FocusRing, Ident, Pressable, Sizable, StyledExt, text as foundation_text,
};
use crate::strings::{ActiveStrings, StringKey};

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

/// The field an open editing session holds.
#[derive(Clone)]
enum Editor {
    Line(Entity<TextInput>),
    Block(Entity<TextArea>),
}

impl Editor {
    fn value(&self, cx: &App) -> SharedString {
        match self {
            Self::Line(field) => field.read(cx).value().clone(),
            Self::Block(field) => field.read(cx).value().clone(),
        }
    }

    fn focus(&self, window: &mut Window, cx: &mut App) {
        let handle = match self {
            Self::Line(field) => field.read(cx).focus_handle(cx),
            Self::Block(field) => field.read(cx).focus_handle(cx),
        };
        window.focus(&handle, cx);
    }

    fn is_block(&self) -> bool {
        matches!(self, Self::Block(_))
    }
}

/// What one identity remembers between two frames.
///
/// A `RenderOnce` builder cannot carry anything across frames, so the open
/// field lives in an application global keyed by the component's identity —
/// the same arrangement [`crate::layout::measure`] and [`crate::data::DataGrid`]
/// use.
#[derive(Default)]
struct Memory {
    editor: RefCell<Option<Editor>>,
}

#[derive(Default)]
struct Memories(RefCell<HashMap<SharedString, Rc<Memory>>>);

impl Global for Memories {}

fn memory(id: &SharedString, cx: &mut App) -> Rc<Memory> {
    if !cx.has_global::<Memories>() {
        cx.set_global(Memories::default());
    }
    let mut memories = cx.global::<Memories>().0.borrow_mut();
    Rc::clone(memories.entry(id.clone()).or_default())
}

/// Text that a click or enter turns into a field.
#[derive(IntoElement)]
pub struct InlineEdit {
    ident: Ident,
    value: SharedString,
    placeholder: Option<SharedString>,
    editing: bool,
    multiline: bool,
    rows: usize,
    failure: Option<SharedString>,
    size: ControlSize,
    disabled: bool,
    on_edit: Option<EditHandler>,
    on_commit: Option<CommitHandler>,
    on_cancel: Option<EditHandler>,
}

impl std::fmt::Debug for InlineEdit {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("InlineEdit")
            .field("ident", &self.ident)
            .field("editing", &self.editing)
            .field("multiline", &self.multiline)
            .field("failed", &self.failure.is_some())
            .field("disabled", &self.disabled)
            .finish()
    }
}

impl InlineEdit {
    pub fn new(ident: impl Into<Ident>, value: impl Into<SharedString>) -> Self {
        Self {
            ident: ident.into(),
            value: value.into(),
            placeholder: None,
            editing: false,
            multiline: false,
            rows: 3,
            failure: None,
            size: ControlSize::Md,
            disabled: false,
            on_edit: None,
            on_commit: None,
            on_cancel: None,
        }
    }

    /// What to show where an empty value would be, so a blank line is still
    /// something to aim at.
    /// The placeholder the host gave, or the built-in default word for a
    /// value that is not there.
    fn resolved_placeholder(&self, cx: &App) -> SharedString {
        self.placeholder
            .clone()
            .unwrap_or_else(|| cx.strings().text(StringKey::InlineEditPlaceholder))
    }

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

    /// Whether the caller has opened the field. The component never opens it
    /// on its own; a click reports the request.
    pub fn editing(mut self, editing: bool) -> Self {
        self.editing = editing;
        self
    }

    /// Edits over a [`TextArea`] rather than a [`TextInput`], so enter inserts
    /// a line and the platform modifier plus enter commits.
    pub fn multiline(mut self, multiline: bool) -> Self {
        self.multiline = multiline;
        self
    }

    /// How many rows a multi-line field opens at.
    pub fn rows(mut self, rows: usize) -> Self {
        self.rows = rows.max(1);
        self
    }

    /// Why the last save did not take, in the host's own words. The typed text
    /// stays exactly as it was.
    pub fn failure(mut self, failure: impl Into<SharedString>) -> Self {
        self.failure = Some(failure.into());
        self
    }

    /// Reports that the typist asked to edit.
    pub fn on_edit(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_edit = Some(Rc::new(handler));
        self
    }

    /// Reports the text the field held when the edit ended.
    pub fn on_commit(
        mut self,
        handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_commit = Some(Rc::new(handler));
        self
    }

    /// Reports that the edit was abandoned. Nothing was typed as far as the
    /// host is concerned.
    pub fn on_cancel(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_cancel = Some(Rc::new(handler));
        self
    }

    /// The field for the open session, built once when the session opens.
    fn editor(&self, state: &Rc<Memory>, window: &mut Window, cx: &mut App) -> Editor {
        let existing = state
            .editor
            .borrow()
            .clone()
            .filter(|editor| editor.is_block() == self.multiline);
        if let Some(editor) = existing {
            return editor;
        }

        let ident = self.ident.child("field");
        let editor = if self.multiline {
            let rows = self.rows;
            let value = self.value.clone();
            let placeholder = self.resolved_placeholder(cx);
            Editor::Block(cx.new(|cx| {
                TextArea::new(ident, window, cx)
                    .text(value)
                    .placeholder(placeholder)
                    .rows(rows)
            }))
        } else {
            let value = self.value.clone();
            let placeholder = self.resolved_placeholder(cx);
            Editor::Line(cx.new(|cx| {
                TextInput::new(ident, window, cx)
                    .text(value)
                    .placeholder(placeholder)
                    .bare(true)
            }))
        };
        *state.editor.borrow_mut() = Some(editor.clone());
        editor.focus(window, cx);
        editor
    }
}

impl Disableable for InlineEdit {
    /// Refuses editing entirely: no click, no key, and no field, whatever the
    /// caller says about `editing`.
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

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

impl RenderOnce for InlineEdit {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme().clone();
        let metrics = theme.control.get(self.size);
        let state = memory(&self.ident.semantic_id(), cx);
        let editing = self.editing && !self.disabled;

        if !editing {
            // The session is over; the next one starts from the caller's value
            // rather than from whatever the last one left behind.
            state.editor.borrow_mut().take();
        }

        let failure = self.failure.clone().map(|reason| {
            foundation_text(&theme, TypeScale::Body, reason.clone())
                .text_color(theme.colors.danger)
                .semantic_in(
                    cx,
                    NodeSpec::new(self.ident.child("failure").semantic_id(), Role::Status)
                        .parent(self.ident.semantic_id())
                        .invalid(true)
                        .text(reason),
                )
        });

        if !editing {
            let actionable = !self.disabled && self.on_edit.is_some();
            let empty = self.value.is_empty();
            let mut reading = div()
                .id(self.ident.element_id())
                .row()
                .min_h(px(metrics.height))
                .px(px(theme.space(Space::Xs)))
                .radius(&theme, Radius::Control)
                .child(
                    foundation_text(
                        &theme,
                        TypeScale::Label,
                        if empty {
                            self.resolved_placeholder(cx)
                        } else {
                            self.value.clone()
                        },
                    )
                    .text_size(px(metrics.font_size))
                    .text_color(if self.disabled || empty {
                        theme.colors.text_faint
                    } else {
                        theme.colors.text
                    }),
                )
                .when(actionable, |element| {
                    element
                        .cursor_pointer()
                        .tab_index(0)
                        .pressable(cx)
                        .hover(|style| style.bg(theme.colors.hover))
                        .focus_ring(&theme)
                });

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

            let mut spec = NodeSpec::new(
                self.ident.semantic_id(),
                if actionable { Role::Button } else { Role::Text },
            )
            .disabled(!actionable)
            .invalid(self.failure.is_some())
            .value(if empty { "empty" } else { "reading" });
            if empty {
                spec = spec.placeholder(self.resolved_placeholder(cx));
            } else {
                spec = spec.text(self.value.clone());
            }

            return div()
                .column()
                .w_full()
                .gap(px(theme.space(Space::Xs)))
                .child(reading)
                .children(failure)
                .semantic_in(cx, spec)
                .into_any_element();
        }

        let editor = self.editor(&state, window, cx);
        let reading = editor.clone();
        let commit = self.on_commit.clone().map(|handler| {
            let editor = reading.clone();
            move |window: &mut Window, cx: &mut App| {
                handler(editor.value(cx), window, cx);
            }
        });
        let cancel = self.on_cancel.clone();

        let mut frame = div()
            .id(self.ident.element_id())
            .column()
            .w_full()
            .gap(px(theme.space(Space::Xs)));

        if let Some(commit) = commit.clone() {
            // A press anywhere else ends the edit the way leaving a field
            // does, which is the other half of "enter or blur commits".
            frame = frame.on_mouse_down_out(move |_, window, cx| commit(window, cx));
        }

        // The field's own bindings dispatch before an ancestor's key listener,
        // so the two chords are taken as actions in the capture phase rather
        // than as keystrokes that never arrive.
        if let Some(commit) = commit {
            let line = commit.clone();
            frame = frame
                .capture_action::<input::Submit>(move |_, window, cx| {
                    line(window, cx);
                    cx.stop_propagation();
                })
                .capture_action::<textarea::Submit>(move |_, window, cx| {
                    commit(window, cx);
                    cx.stop_propagation();
                });
        }

        if let Some(cancel) = cancel {
            let line = Rc::clone(&cancel);
            frame = frame
                .capture_action::<input::Cancel>(move |_, window, cx| {
                    line(window, cx);
                    cx.stop_propagation();
                })
                .capture_action::<textarea::Cancel>(move |_, window, cx| {
                    cancel(window, cx);
                    cx.stop_propagation();
                });
        }

        let field = match editor {
            Editor::Line(field) => div()
                .w_full()
                .min_h(px(metrics.height))
                .px(px(theme.space(Space::Xs)))
                .radius(&theme, Radius::Control)
                .well(&theme)
                .when(self.failure.is_some(), |element| {
                    element.border_color(theme.colors.danger)
                })
                .shadow(theme.focus_ring())
                .text_size(px(metrics.font_size))
                .child(field),
            Editor::Block(field) => div().w_full().child(field),
        };

        frame
            .child(field)
            .children(failure)
            .semantic_in(
                cx,
                NodeSpec::new(self.ident.semantic_id(), Role::Group)
                    .text(self.value.clone())
                    .invalid(self.failure.is_some())
                    .value(if self.failure.is_some() {
                        "failed"
                    } else {
                        "editing"
                    }),
            )
            .into_any_element()
    }
}