gpui-pre 0.3.11

Zed's GPU-accelerated UI framework (gpui-pre snapshot of zed@f816e1f)
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use collections::BTreeMap;
use gpui_util::post_inc;
use std::{
    cell::{Cell, RefCell},
    fmt::Debug,
    mem,
    rc::Rc,
};

/// Stores subscriptions for a single emitter.
pub(crate) struct SingleSubscriberSet<Callback>(Rc<RefCell<SingleSubscriberSetState<Callback>>>);

/// Stores subscriptions for emitters.
pub(crate) struct SubscriberSet<EmitterKey, Callback>(
    Rc<RefCell<SubscriberSetState<EmitterKey, Callback>>>,
);

impl<EmitterKey, Callback> Clone for SubscriberSet<EmitterKey, Callback> {
    fn clone(&self) -> Self {
        SubscriberSet(self.0.clone())
    }
}

struct SubscriberSetState<EmitterKey, Callback> {
    subscribers: BTreeMap<EmitterKey, BTreeMap<usize, Subscriber<Callback>>>,
    next_subscriber_id: usize,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum SubscriberState {
    Pending,
    Active,
    Dropped,
}

struct Subscriber<Callback> {
    state: Rc<Cell<SubscriberState>>,
    callback: Callback,
}

impl<EmitterKey, Callback> SubscriberSet<EmitterKey, Callback>
where
    EmitterKey: 'static + Ord + Clone + Debug,
    Callback: 'static,
{
    pub fn new() -> Self {
        Self(Rc::new(RefCell::new(SubscriberSetState {
            subscribers: Default::default(),
            next_subscriber_id: 0,
        })))
    }

    /// Inserts an immediately active subscription for the given emitter.
    pub fn insert(&self, emitter_key: EmitterKey, callback: Callback) -> Subscription {
        let (subscription, activate) = self.insert_pending(emitter_key, callback);
        activate();
        subscription
    }

    /// Inserts a pending subscription and returns its handle and activation function.
    ///
    /// Pending callbacks are skipped by `retain` and excluded from `remove` until activated.
    pub fn insert_pending(
        &self,
        emitter_key: EmitterKey,
        callback: Callback,
    ) -> (Subscription, impl FnOnce() + use<EmitterKey, Callback>) {
        let state = Rc::new(Cell::new(SubscriberState::Pending));
        let mut lock = self.0.borrow_mut();
        let subscriber_id = post_inc(&mut lock.next_subscriber_id);
        lock.subscribers
            .entry(emitter_key.clone())
            .or_default()
            .insert(
                subscriber_id,
                Subscriber {
                    state: state.clone(),
                    callback,
                },
            );
        let this = self.0.clone();

        let subscription = Subscription {
            unsubscribe: Some(Box::new({
                let state = state.clone();
                move || {
                    state.set(SubscriberState::Dropped);

                    let mut lock = this.borrow_mut();
                    let Some(subscribers) = lock.subscribers.get_mut(&emitter_key) else {
                        return;
                    };

                    subscribers.remove(&subscriber_id);
                    if subscribers.is_empty() {
                        lock.subscribers.remove(&emitter_key);
                    }
                }
            })),
        };
        (subscription, move || {
            if state.get() == SubscriberState::Pending {
                state.set(SubscriberState::Active);
            }
        })
    }

    pub fn remove(
        &self,
        emitter: &EmitterKey,
    ) -> impl IntoIterator<Item = Callback> + use<EmitterKey, Callback> {
        let subscribers = self.0.borrow_mut().subscribers.remove(emitter);
        subscribers
            .map(|s| s.into_values())
            .into_iter()
            .flatten()
            .filter_map(|subscriber| {
                if subscriber.state.get() == SubscriberState::Active {
                    Some(subscriber.callback)
                } else {
                    None
                }
            })
    }

    /// Call the given callback for each subscriber to the given emitter.
    /// If the callback returns false, the subscriber is removed.
    pub fn retain<F>(&self, emitter: &EmitterKey, mut f: F)
    where
        F: FnMut(&mut Callback) -> bool,
    {
        // We do not remove here upfront to reduce BTreeMap shuffling churn
        let Some(mut subscribers) = self
            .0
            .borrow_mut()
            .subscribers
            .get_mut(emitter)
            .map(mem::take)
        else {
            return;
        };

        subscribers.retain(|_, subscriber| subscriber.retain(&mut f));
        let mut lock = self.0.borrow_mut();

        if subscribers.is_empty() {
            if lock
                .subscribers
                .get(emitter)
                .is_some_and(|subscribers| subscribers.is_empty())
            {
                lock.subscribers.remove(emitter);
            }
            return;
        }

        // Add any new subscribers that were added while invoking the callback.
        lock.subscribers
            .entry(emitter.clone())
            .or_default()
            .append(&mut subscribers);
    }
}

struct SingleSubscriberSetState<Callback> {
    subscribers: BTreeMap<usize, Subscriber<Callback>>,
    next_subscriber_id: usize,
}

impl<Callback> Clone for SingleSubscriberSet<Callback> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<Callback: 'static> SingleSubscriberSet<Callback> {
    pub fn new() -> Self {
        Self(Rc::new(RefCell::new(SingleSubscriberSetState {
            subscribers: BTreeMap::new(),
            next_subscriber_id: 0,
        })))
    }

    /// Inserts an immediately active subscription.
    pub fn insert(&self, callback: Callback) -> Subscription {
        let (subscription, activate) = self.insert_pending(callback);
        activate();
        subscription
    }

    /// Inserts a pending subscription and returns its handle and activation function.
    ///
    /// Pending callbacks are skipped by `retain` and excluded from `remove` until activated.
    pub fn insert_pending(
        &self,
        callback: Callback,
    ) -> (Subscription, impl FnOnce() + use<Callback>) {
        let state = Rc::new(Cell::new(SubscriberState::Pending));
        let mut lock = self.0.borrow_mut();
        let subscriber_id = post_inc(&mut lock.next_subscriber_id);
        lock.subscribers.insert(
            subscriber_id,
            Subscriber {
                state: state.clone(),
                callback,
            },
        );
        let this = self.0.clone();

        let subscription = Subscription {
            unsubscribe: Some(Box::new({
                let state = state.clone();
                move || {
                    state.set(SubscriberState::Dropped);
                    this.borrow_mut().subscribers.remove(&subscriber_id);
                }
            })),
        };
        (subscription, move || {
            if state.get() == SubscriberState::Pending {
                state.set(SubscriberState::Active);
            }
        })
    }

    /// Removes all subscriptions and returns the active callbacks in registration order.
    pub fn remove(&self) -> impl IntoIterator<Item = Callback> + use<Callback> {
        let subscribers = mem::take(&mut self.0.borrow_mut().subscribers);
        subscribers.into_values().filter_map(|subscriber| {
            if subscriber.state.get() == SubscriberState::Active {
                Some(subscriber.callback)
            } else {
                None
            }
        })
    }

    /// Invokes each active callback, removing subscriptions for which `f` returns false.
    pub fn retain(&self, mut f: impl FnMut(&mut Callback) -> bool) {
        let mut subscribers = mem::take(&mut self.0.borrow_mut().subscribers);
        subscribers.retain(|_, subscriber| subscriber.retain(&mut f));
        self.0.borrow_mut().subscribers.append(&mut subscribers);
    }
}

impl<Callback> Subscriber<Callback> {
    fn retain(&mut self, f: &mut impl FnMut(&mut Callback) -> bool) -> bool {
        match self.state.get() {
            SubscriberState::Pending => true,
            SubscriberState::Dropped => false,
            SubscriberState::Active => {
                let keep = f(&mut self.callback);
                keep && self.state.get() != SubscriberState::Dropped
            }
        }
    }
}

/// A handle to a subscription created by GPUI. When dropped, the subscription
/// is cancelled and the callback will no longer be invoked.
#[must_use]
pub struct Subscription {
    unsubscribe: Option<Box<dyn FnOnce() + 'static>>,
}

impl Subscription {
    /// Creates a new subscription with a callback that gets invoked when
    /// this subscription is dropped.
    pub fn new(unsubscribe: impl 'static + FnOnce()) -> Self {
        Self {
            unsubscribe: Some(Box::new(unsubscribe)),
        }
    }

    /// Detaches the subscription from this handle. The callback will
    /// continue to be invoked until the entities it has been
    /// subscribed to are dropped
    pub fn detach(mut self) {
        self.unsubscribe.take();
    }

    /// Joins two subscriptions into a single subscription. Detach will
    /// detach both interior subscriptions.
    pub fn join(mut subscription_a: Self, mut subscription_b: Self) -> Self {
        let a_unsubscribe = subscription_a.unsubscribe.take();
        let b_unsubscribe = subscription_b.unsubscribe.take();
        Self {
            unsubscribe: Some(Box::new(move || {
                if let Some(self_unsubscribe) = a_unsubscribe {
                    self_unsubscribe();
                }
                if let Some(other_unsubscribe) = b_unsubscribe {
                    other_unsubscribe();
                }
            })),
        }
    }
}

impl Drop for Subscription {
    fn drop(&mut self) {
        if let Some(unsubscribe) = self.unsubscribe.take() {
            unsubscribe();
        }
    }
}

impl std::fmt::Debug for Subscription {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Subscription").finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Global, TestApp};

    #[test]
    fn test_immediate_and_pending_insertions() {
        let subscribers = SubscriberSet::new();
        let first = subscribers.insert(1usize, 10);
        let (second, activate_second) = subscribers.insert_pending(1, 20);
        let mut visited = Vec::new();
        subscribers.retain(&1, |callback| {
            visited.push(*callback);
            subscribers.insert(1, 30).detach();
            true
        });
        assert_eq!(visited, [10]);
        activate_second();
        assert_eq!(
            subscribers.remove(&1).into_iter().collect::<Vec<_>>(),
            [10, 20, 30]
        );
        drop(first);
        drop(second);

        let (pending, _activate) = subscribers.insert_pending(1, 40);
        assert_eq!(subscribers.remove(&1).into_iter().count(), 0);
        drop(pending);
    }

    #[test]
    fn test_single_immediate_and_pending_insertions() {
        let subscribers = SingleSubscriberSet::new();
        let first = subscribers.insert(10);
        let (second, activate_second) = subscribers.insert_pending(20);
        let mut visited = Vec::new();
        subscribers.retain(|callback| {
            visited.push(*callback);
            subscribers.insert(30).detach();
            true
        });
        assert_eq!(visited, [10]);
        activate_second();
        assert_eq!(
            subscribers.remove().into_iter().collect::<Vec<_>>(),
            [10, 20, 30]
        );
        drop(first);
        drop(second);

        let (pending, _activate) = subscribers.insert_pending(40);
        assert_eq!(subscribers.remove().into_iter().count(), 0);
        drop(pending);
    }

    #[test]
    fn test_activation_after_drop_during_retain() {
        let subscribers = SubscriberSet::new();
        let (first, activate_first) = subscribers.insert_pending(1usize, 10);
        let (second, activate_second) = subscribers.insert_pending(1, 20);
        subscribers.retain(&1, |_| panic!("pending subscriber invoked"));
        activate_first();
        let mut second = Some((second, activate_second));
        let mut visited = Vec::new();

        subscribers.retain(&1, |callback| {
            visited.push(*callback);
            let (subscription, activate) = second.take().expect("only the first callback runs");
            drop(subscription);
            activate();
            true
        });

        assert_eq!(visited, [10]);
        assert_eq!(subscribers.remove(&1).into_iter().collect::<Vec<_>>(), [10]);
        drop(first);
    }

    #[test]
    fn test_single_activation_after_drop_during_retain() {
        let subscribers = SingleSubscriberSet::new();
        let (first, activate_first) = subscribers.insert_pending(10);
        let (second, activate_second) = subscribers.insert_pending(20);
        subscribers.retain(|_| panic!("pending subscriber invoked"));
        activate_first();
        let mut second = Some((second, activate_second));
        let mut visited = Vec::new();

        subscribers.retain(|callback| {
            visited.push(*callback);
            let (subscription, activate) = second.take().expect("only the first callback runs");
            drop(subscription);
            activate();
            true
        });

        assert_eq!(visited, [10]);
        assert_eq!(subscribers.remove().into_iter().collect::<Vec<_>>(), [10]);
        drop(first);
    }

    #[test]
    fn test_unsubscribe_during_callback_with_insert() {
        struct TestGlobal;
        impl Global for TestGlobal {}

        let mut app = TestApp::new();
        app.set_global(TestGlobal);

        let observer_a_count = Rc::new(Cell::new(0usize));
        let observer_b_count = Rc::new(Cell::new(0usize));

        let sub_a: Rc<RefCell<Option<Subscription>>> = Default::default();
        let sub_b: Rc<RefCell<Option<Subscription>>> = Default::default();

        // Observer A fires first (lower subscriber_id). It drops itself and
        // inserts a new observer for the same global.
        *sub_a.borrow_mut() = Some(app.update({
            let count = observer_a_count.clone();
            let sub_a = sub_a.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |cx| {
                    count.set(count.get() + 1);
                    sub_a.borrow_mut().take();
                    cx.observe_global::<TestGlobal>(|_| {}).detach();
                })
            }
        }));

        // Observer B fires second. It just drops itself.
        *sub_b.borrow_mut() = Some(app.update({
            let count = observer_b_count.clone();
            let sub_b = sub_b.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |_cx| {
                    count.set(count.get() + 1);
                    sub_b.borrow_mut().take();
                })
            }
        }));

        // Both fire once.
        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(observer_a_count.get(), 1);
        assert_eq!(observer_b_count.get(), 1);

        // Neither should fire again — both dropped their subscriptions.
        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(observer_a_count.get(), 1);
        assert_eq!(observer_b_count.get(), 1, "orphaned subscriber fired again");
    }

    #[test]
    fn test_callback_dropped_by_earlier_callback_does_not_fire() {
        struct TestGlobal;
        impl Global for TestGlobal {}

        let mut app = TestApp::new();
        app.set_global(TestGlobal);

        let observer_b_count = Rc::new(Cell::new(0usize));
        let sub_b: Rc<RefCell<Option<Subscription>>> = Default::default();

        // Observer A fires first and drops B's subscription.
        app.update({
            let sub_b = sub_b.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |_cx| {
                    sub_b.borrow_mut().take();
                })
                .detach();
            }
        });

        // Observer B fires second — but A already dropped it.
        *sub_b.borrow_mut() = Some(app.update({
            let count = observer_b_count.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |_cx| {
                    count.set(count.get() + 1);
                })
            }
        }));

        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(
            observer_b_count.get(),
            0,
            "B should not fire — A dropped its subscription"
        );
    }

    #[test]
    fn test_self_drop_during_callback() {
        struct TestGlobal;
        impl Global for TestGlobal {}

        let mut app = TestApp::new();
        app.set_global(TestGlobal);

        let count = Rc::new(Cell::new(0usize));
        let sub: Rc<RefCell<Option<Subscription>>> = Default::default();

        *sub.borrow_mut() = Some(app.update({
            let count = count.clone();
            let sub = sub.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |_cx| {
                    count.set(count.get() + 1);
                    sub.borrow_mut().take();
                })
            }
        }));

        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(count.get(), 1);

        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(count.get(), 1, "should not fire after self-drop");
    }

    #[test]
    fn test_subscription_drop() {
        struct TestGlobal;
        impl Global for TestGlobal {}

        let mut app = TestApp::new();
        app.set_global(TestGlobal);

        let count = Rc::new(Cell::new(0usize));

        let subscription = app.update({
            let count = count.clone();
            move |cx| {
                cx.observe_global::<TestGlobal>(move |_cx| {
                    count.set(count.get() + 1);
                })
            }
        });

        drop(subscription);

        app.update(|cx| cx.set_global(TestGlobal));
        assert_eq!(count.get(), 0, "should not fire after drop");
    }
}