gpui-base 0.12.0

Behavior, interaction, and infrastructure foundations for GPUI applications.
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
use std::{collections::BTreeMap, ops::Range, rc::Rc};

use gpui::{App, Context, Hsla, SharedString, WeakEntity};

use super::{EditorMode, InputBaseState};

/// A decoration of one whole buffer row: a background band, a gutter icon, or both.
#[derive(Clone, Debug, PartialEq)]
pub struct LineDecoration {
    row: usize,
    background: Option<Hsla>,
    gutter_icon: Option<(SharedString, Hsla)>,
}

impl LineDecoration {
    /// Create an empty decoration for a zero-based buffer row.
    pub fn new(row: usize) -> Self {
        Self {
            row,
            background: None,
            gutter_icon: None,
        }
    }

    /// The zero-based buffer row this decoration covers.
    pub fn row(&self) -> usize {
        self.row
    }

    /// The color of the background band, if any.
    pub fn background(&self) -> Option<Hsla> {
        self.background
    }

    /// The SVG asset path and color of the gutter icon, if any.
    pub fn gutter_icon(&self) -> Option<(&SharedString, Hsla)> {
        self.gutter_icon
            .as_ref()
            .map(|(icon, color)| (icon, *color))
    }

    /// Paint a band of `color` across the whole row.
    pub fn with_background(mut self, color: Hsla) -> Self {
        self.background = Some(color);
        self
    }

    /// Paint the SVG asset at `icon`, for example `icons/plus.svg`, in `color`
    /// in the row's gutter.
    ///
    /// The icon is painted only if the provider returns `true` from
    /// [`LineDecorationProvider::has_gutter_icons`].
    pub fn with_gutter_icon(mut self, icon: impl Into<SharedString>, color: Hsla) -> Self {
        self.gutter_icon = Some((icon.into(), color));
        self
    }
}

/// Supplies the line decorations of the rows an editor paints.
///
/// The editor asks on every frame with the visible buffer rows and ignores
/// decorations outside them, so an implementation must be cheap. Colors may be
/// read from `cx`, so they follow theme changes.
pub trait LineDecorationProvider {
    /// Return the decorations for rows within `rows`.
    fn line_decorations(&self, rows: Range<usize>, cx: &App) -> Vec<LineDecoration>;

    /// Whether this provider's decorations have gutter icons. Default is false.
    ///
    /// While a provider says it has them and the line numbers are shown, the
    /// gutter reserves the column of the gutter icons for every row, so the
    /// gutter keeps its width as rows with an icon scroll in and out of view.
    /// The gutter icons of other providers are not painted.
    fn has_gutter_icons(&self) -> bool {
        false
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct LineDecorationCollectionId(usize);

/// An independently managed collection of line decorations, supplied by a
/// [`LineDecorationProvider`].
///
/// Clones address the same collection. Dropping a handle does not clear it; use
/// [`Self::clear`] to stop painting it or [`Self::dispose`] to release it permanently.
/// Operations on a disposed collection or a dropped editor are harmless no-ops.
#[derive(Clone, Debug)]
pub struct LineDecorationCollection {
    state: WeakEntity<InputBaseState<EditorMode>>,
    id: LineDecorationCollectionId,
}

impl LineDecorationCollection {
    /// Replace this collection's provider. The next frame asks the new one.
    pub fn set_provider(&self, provider: Rc<dyn LineDecorationProvider>, cx: &mut App) {
        self.update(Some(provider), cx);
    }

    /// Stop painting this collection without invalidating its handles.
    pub fn clear(&self, cx: &mut App) {
        self.update(None, cx);
    }

    /// Release this collection, invalidating all of its cloned handles.
    pub fn dispose(&self, cx: &mut App) {
        let _ = self.state.update(cx, |state, cx| {
            if state.extras.line_decorations.remove(self.id) {
                cx.notify();
            }
        });
    }

    /// Whether this collection has a provider; `false` once cleared, disposed or dropped.
    pub fn has_provider(&self, cx: &App) -> bool {
        self.state
            .read_with(cx, |state, _| {
                state.extras.line_decorations.has_provider(self.id)
            })
            .unwrap_or(false)
    }

    fn update(&self, provider: Option<Rc<dyn LineDecorationProvider>>, cx: &mut App) {
        let _ = self.state.update(cx, |state, cx| {
            if state.extras.line_decorations.set(self.id, provider) {
                cx.notify();
            }
        });
    }
}

/// The line decoration providers of one editor, in creation order.
#[derive(Default)]
pub(crate) struct LineDecorationProviders {
    entries: BTreeMap<LineDecorationCollectionId, Option<Rc<dyn LineDecorationProvider>>>,
    next_id: usize,
}

impl LineDecorationProviders {
    fn create(&mut self, provider: Rc<dyn LineDecorationProvider>) -> LineDecorationCollectionId {
        let id = LineDecorationCollectionId(self.next_id);
        self.next_id += 1;
        self.entries.insert(id, Some(provider));
        id
    }

    fn set(
        &mut self,
        id: LineDecorationCollectionId,
        provider: Option<Rc<dyn LineDecorationProvider>>,
    ) -> bool {
        let Some(entry) = self.entries.get_mut(&id) else {
            return false;
        };
        *entry = provider;
        true
    }

    fn remove(&mut self, id: LineDecorationCollectionId) -> bool {
        self.entries.remove(&id).is_some()
    }

    fn has_provider(&self, id: LineDecorationCollectionId) -> bool {
        self.entries.get(&id).is_some_and(Option::is_some)
    }

    pub(crate) fn has_any_provider(&self) -> bool {
        self.entries.values().any(Option::is_some)
    }

    pub(crate) fn has_gutter_icons(&self) -> bool {
        self.entries
            .values()
            .flatten()
            .any(|provider| provider.has_gutter_icons())
    }

    /// Ask every provider about `rows`, in creation order, keeping only the
    /// decorations that fall within `rows`, and their gutter icons only from a
    /// provider that has gutter icons.
    pub(crate) fn query(&self, rows: Range<usize>, cx: &App) -> Vec<LineDecoration> {
        if rows.is_empty() {
            return Vec::new();
        }
        let mut decorations = Vec::new();
        for provider in self.entries.values().flatten() {
            let gutter_icons = provider.has_gutter_icons();
            decorations.extend(
                provider
                    .line_decorations(rows.clone(), cx)
                    .into_iter()
                    .filter(|decoration| rows.contains(&decoration.row))
                    .map(|mut decoration| {
                        if !gutter_icons {
                            decoration.gutter_icon = None;
                        }
                        decoration
                    }),
            );
        }
        decorations
    }
}

impl InputBaseState<EditorMode> {
    /// Create an independently owned collection of line decorations, supplied by
    /// `provider`.
    ///
    /// The provider is asked for the visible rows on every frame. Unlike text and
    /// range decorations, rows are not tracked across edits: the provider answers
    /// for the text as it is when asked.
    ///
    /// A background spans the whole row, gutter included, across its soft wraps,
    /// under the active line, indent guides, selections and text. A gutter icon
    /// is painted in a column of its own, [`GutterColumn::Icons`](crate::input::GutterColumn::Icons),
    /// only while the line numbers are shown and its provider says it has
    /// gutter icons: square, sized from the line height and centred on its row.
    /// In the default order the column sits between the line numbers and the
    /// outer edge of the gutter; [`InputBaseState::gutter_order`] can move it
    /// nearer the text. Later collections paint over earlier ones. Neither
    /// affects hit testing or focus. Collections live until explicitly disposed
    /// or the editor is dropped.
    pub fn create_line_decorations_collection(
        &mut self,
        provider: Rc<dyn LineDecorationProvider>,
        cx: &mut Context<Self>,
    ) -> LineDecorationCollection {
        let id = self.extras.line_decorations.create(provider);
        cx.notify();
        LineDecorationCollection {
            state: cx.entity().downgrade(),
            id,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;

    use gpui::{AppContext as _, TestAppContext, hsla};

    use super::*;
    use crate::input::EditorState;

    struct EveryNth {
        every: usize,
        color: Hsla,
        asked: RefCell<Vec<Range<usize>>>,
    }

    impl EveryNth {
        fn new(every: usize, color: Hsla) -> Rc<Self> {
            Rc::new(Self {
                every,
                color,
                asked: RefCell::default(),
            })
        }
    }

    impl LineDecorationProvider for EveryNth {
        fn line_decorations(&self, rows: Range<usize>, _: &App) -> Vec<LineDecoration> {
            self.asked.borrow_mut().push(rows.clone());
            (rows.start.saturating_sub(1)..rows.end + 1)
                .filter(|row| row % self.every == 0)
                .map(|row| LineDecoration::new(row).with_background(self.color))
                .collect()
        }
    }

    fn editor(cx: &mut TestAppContext) -> gpui::Entity<EditorState> {
        cx.update(crate::init);
        let window = cx.add_empty_window();
        window.update(|window, cx| cx.new(|cx| EditorState::new(window, cx)))
    }

    fn red() -> Hsla {
        hsla(0., 0.5, 0.5, 0.2)
    }

    fn green() -> Hsla {
        hsla(0.3, 0.5, 0.5, 0.2)
    }

    #[test]
    fn a_decoration_is_built_from_its_parts() {
        let color = hsla(0.3, 0.5, 0.5, 0.2);
        let decoration = LineDecoration::new(7);
        assert_eq!(decoration.row(), 7);
        assert_eq!(decoration.background(), None);
        assert_eq!(decoration.gutter_icon(), None);

        let decoration = decoration
            .with_background(color)
            .with_gutter_icon("icons/plus.svg", red());
        assert_eq!(decoration.background(), Some(color));
        assert_eq!(
            decoration.gutter_icon(),
            Some((&SharedString::from("icons/plus.svg"), red()))
        );
    }

    #[gpui::test]
    fn line_decoration_collection_round_trips(cx: &mut TestAppContext) {
        let editor = editor(cx);
        let provider = EveryNth::new(1, green());

        let collection = editor.update(cx, |state, cx| {
            assert!(state.extras.line_decorations.entries.is_empty());
            state.create_line_decorations_collection(provider.clone(), cx)
        });
        cx.read(|cx| assert!(collection.has_provider(cx)));

        cx.update(|cx| collection.clear(cx));
        cx.read(|cx| {
            assert!(!collection.has_provider(cx));
            let state = editor.read(cx);
            assert!(state.extras.line_decorations.query(0..4, cx).is_empty());
        });

        cx.update(|cx| collection.clone().set_provider(provider.clone(), cx));
        cx.read(|cx| {
            assert!(collection.has_provider(cx));
            let state = editor.read(cx);
            assert_eq!(state.extras.line_decorations.query(0..2, cx).len(), 2);
        });

        cx.update(|cx| {
            collection.dispose(cx);
            collection.set_provider(provider.clone(), cx);
        });
        cx.read(|cx| {
            assert!(!collection.has_provider(cx));
            let state = editor.read(cx);
            assert!(state.extras.line_decorations.query(0..4, cx).is_empty());
        });
    }

    #[gpui::test]
    fn collections_are_independent_and_asked_in_creation_order(cx: &mut TestAppContext) {
        let editor = editor(cx);
        let removed = EveryNth::new(2, red());
        let added = EveryNth::new(3, green());

        let (first, second) = editor.update(cx, |state, cx| {
            (
                state.create_line_decorations_collection(removed.clone(), cx),
                state.create_line_decorations_collection(added.clone(), cx),
            )
        });

        let query = |cx: &mut TestAppContext, rows: Range<usize>| {
            cx.read(|cx| {
                editor
                    .read(cx)
                    .extras
                    .line_decorations
                    .query(rows, cx)
                    .into_iter()
                    .map(|decoration| (decoration.row(), decoration.background().unwrap()))
                    .collect::<Vec<_>>()
            })
        };

        assert_eq!(
            query(cx, 1..7),
            vec![
                (2, red()),
                (4, red()),
                (6, red()),
                (3, green()),
                (6, green()),
            ]
        );
        assert_eq!(*removed.asked.borrow(), vec![1..7]);
        assert_eq!(*added.asked.borrow(), vec![1..7]);

        assert!(query(cx, 3..3).is_empty());
        assert_eq!(removed.asked.borrow().len(), 1);

        cx.update(|cx| first.dispose(cx));
        assert_eq!(query(cx, 0..4), vec![(0, green()), (3, green())]);
        cx.read(|cx| assert!(second.has_provider(cx)));

        let third = editor.update(cx, |state, cx| {
            state.create_line_decorations_collection(removed.clone(), cx)
        });
        assert_ne!(third.id, first.id);
        cx.read(|cx| assert!(!first.has_provider(cx)));
    }

    #[gpui::test]
    fn a_dropped_editor_makes_its_collections_no_ops(cx: &mut TestAppContext) {
        let editor = editor(cx);
        let collection = editor.update(cx, |state, cx| {
            state.create_line_decorations_collection(EveryNth::new(1, red()), cx)
        });
        drop(editor);
        cx.run_until_parked();

        cx.update(|cx| {
            collection.set_provider(EveryNth::new(1, green()), cx);
            collection.clear(cx);
            collection.dispose(cx);
            assert!(!collection.has_provider(cx));
        });
    }
}