quickgui 0.1.6

A damage-driven, GPU-accelerated desktop GUI foundation
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
use std::{
    any::{Any, TypeId},
    cell::{Cell, RefCell},
    fmt,
    rc::{Rc, Weak},
    sync::atomic::{AtomicU64, Ordering},
};

use crate::EventContext;

static NEXT_ENTITY_ID: AtomicU64 = AtomicU64::new(1);

/// Maximum distinct entities one retained window may observe in one view declaration.
pub const MAX_OBSERVED_ENTITIES_PER_WINDOW: usize = 4_096;

/// Maximum distinct entity notifications retained from one event callback.
///
/// Crossing the bound falls back to one all-window invalidation instead of dropping a state
/// change or allocating without limit.
pub const MAX_ENTITY_NOTIFICATIONS_PER_EVENT: usize = 1_024;

/// Maximum typed entity subscriptions retained by one window declaration.
pub const MAX_ENTITY_SUBSCRIPTIONS_PER_WINDOW: usize = 4_096;

/// Maximum typed entity events one callback may enqueue.
pub const MAX_ENTITY_EVENTS_PER_CALLBACK: usize = 1_024;

/// Maximum typed events retained across one deferred application effect cycle.
pub const MAX_PENDING_ENTITY_EVENTS: usize = 4_096;

/// Maximum subscriber callbacks invoked before a recursive event cycle is rejected.
pub const MAX_ENTITY_EVENT_DELIVERIES_PER_TURN: usize = 65_536;

/// Stable identity for application state shared by one or more retained views.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct EntityId(u64);

impl EntityId {
    fn next() -> Self {
        Self(NEXT_ENTITY_ID.fetch_add(1, Ordering::Relaxed).max(1))
    }
}

/// Associates an entity state type with one typed event it is allowed to emit.
///
/// An entity may implement this trait for any number of event types.
pub trait EventEmitter<E: Any>: 'static {}

pub(crate) struct SubscriptionState {
    active: Cell<bool>,
}

impl SubscriptionState {
    fn new() -> Self {
        Self {
            active: Cell::new(true),
        }
    }

    pub(crate) fn is_active(&self) -> bool {
        self.active.get()
    }

    pub(crate) fn cancel(&self) {
        self.active.set(false);
    }
}

/// An RAII view subscription that cancels entity-event or global-change delivery when dropped.
///
/// Store this handle on the subscribing view for an explicit lifetime, or call [`Self::detach`] to
/// retain the callback until its component scope is removed (or the window closes for a root
/// subscription). The handle is deliberately
/// main-thread-only, matching [`Entity`].
#[must_use = "dropping a Subscription immediately cancels it; store it or call detach()"]
pub struct Subscription {
    state: Rc<SubscriptionState>,
    cancel_on_drop: bool,
}

impl Subscription {
    pub(crate) fn new() -> (Self, Rc<SubscriptionState>) {
        let state = Rc::new(SubscriptionState::new());
        (
            Self {
                state: state.clone(),
                cancel_on_drop: true,
            },
            state,
        )
    }

    /// Keep the subscription active until its owning component scope or window is destroyed.
    pub fn detach(mut self) {
        self.cancel_on_drop = false;
    }
}

impl Drop for Subscription {
    fn drop(&mut self) {
        if self.cancel_on_drop {
            self.state.cancel();
        }
    }
}

impl fmt::Debug for Subscription {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Subscription")
            .field("active", &self.state.is_active())
            .field("detached", &!self.cancel_on_drop)
            .finish()
    }
}

pub(crate) struct EntityEvent {
    pub(crate) source: EntityId,
    pub(crate) event_type: TypeId,
    pub(crate) value: Box<dyn Any>,
    // Keep the source alive through deferred delivery. Subscriber callbacks capture only a weak
    // handle, so retaining one queued event cannot create a permanent ownership cycle.
    _source: Rc<dyn Any>,
}

impl EntityEvent {
    pub(crate) fn new<T, E>(entity: &Entity<T>, event: E) -> Self
    where
        T: EventEmitter<E>,
        E: Any,
    {
        let source: Rc<dyn Any> = entity.value.clone();
        Self {
            source: entity.id,
            event_type: TypeId::of::<E>(),
            value: Box::new(event),
            _source: source,
        }
    }
}

impl fmt::Debug for EntityEvent {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("EntityEvent")
            .field("source", &self.source)
            .field("event_type", &self.event_type)
            .finish_non_exhaustive()
    }
}

/// Main-thread application state that can be observed by independently retained windows.
///
/// `Entity` deliberately uses `Rc<RefCell<_>>`, not a cross-thread lock. QuickGUI invokes view
/// rendering and event callbacks serially on the application thread, so reads and updates stay
/// cheap. Background jobs should return `Send` data through [`crate::ViewContext::spawn`], then
/// update an entity from that job's UI-thread completion callback.
pub struct Entity<T> {
    id: EntityId,
    value: Rc<RefCell<T>>,
}

impl<T> Entity<T> {
    pub fn new(value: T) -> Self {
        Self {
            id: EntityId::next(),
            value: Rc::new(RefCell::new(value)),
        }
    }

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

    /// Read the current value without subscribing a window.
    ///
    /// Use [`crate::ViewContext::observe`] while rendering when later updates should invalidate
    /// that window automatically.
    pub fn read<R>(&self, read: impl FnOnce(&T) -> R) -> R {
        let value = self.value.try_borrow().unwrap_or_else(|_| {
            panic!(
                "entity {:?} cannot be read while it is mutably borrowed",
                self.id
            )
        });
        read(&value)
    }

    /// Mutate the value and notify every currently observing window exactly once.
    ///
    /// The callback runs synchronously on the application thread. Notifications are collected in
    /// the current event context and coalesced before any redraw is requested.
    pub fn update<R>(
        &self,
        cx: &mut EventContext,
        update: impl FnOnce(&mut T, &mut EventContext) -> R,
    ) -> R {
        let result = {
            let mut value = self.value.try_borrow_mut().unwrap_or_else(|_| {
                panic!(
                    "entity {:?} cannot be updated while it is already borrowed",
                    self.id
                )
            });
            update(&mut value, cx)
        };
        cx.notify(self);
        result
    }

    /// Create a non-owning reference suitable for callbacks and cyclic state graphs.
    pub fn downgrade(&self) -> WeakEntity<T> {
        WeakEntity {
            id: self.id,
            value: Rc::downgrade(&self.value),
        }
    }
}

impl<T: 'static> Entity<T> {
    /// Emit one typed event to every retained subscriber after the current callback releases its
    /// application-state borrow.
    ///
    /// Events preserve emission order and are not coalesced. `false` reports that the current
    /// callback reached its hard queue limit and the event was not retained.
    pub fn emit<E: Any>(&self, cx: &mut EventContext, event: E) -> bool
    where
        T: EventEmitter<E>,
    {
        cx.emit(self, event)
    }
}

impl<T> Clone for Entity<T> {
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            value: self.value.clone(),
        }
    }
}

impl<T> fmt::Debug for Entity<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Entity")
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

impl<T> PartialEq for Entity<T> {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl<T> Eq for Entity<T> {}

/// A non-owning reference to an [`Entity`].
pub struct WeakEntity<T> {
    id: EntityId,
    value: Weak<RefCell<T>>,
}

impl<T> WeakEntity<T> {
    pub fn id(&self) -> EntityId {
        self.id
    }

    pub fn upgrade(&self) -> Option<Entity<T>> {
        Some(Entity {
            id: self.id,
            value: self.value.upgrade()?,
        })
    }

    pub fn is_alive(&self) -> bool {
        self.value.strong_count() != 0
    }
}

impl<T> Clone for WeakEntity<T> {
    fn clone(&self) -> Self {
        Self {
            id: self.id,
            value: self.value.clone(),
        }
    }
}

impl<T> fmt::Debug for WeakEntity<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WeakEntity")
            .field("id", &self.id)
            .field("alive", &self.is_alive())
            .finish()
    }
}

impl<T> PartialEq for WeakEntity<T> {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl<T> Eq for WeakEntity<T> {}

#[cfg(test)]
mod tests {
    use super::*;

    struct Publisher;

    #[derive(Debug, Eq, PartialEq)]
    struct Published(u16);

    impl EventEmitter<Published> for Publisher {}

    #[test]
    fn clones_share_state_and_updates_notify_once() {
        let entity = Entity::new(3_u32);
        let clone = entity.clone();
        let mut cx = EventContext::default();

        entity.update(&mut cx, |value, cx| {
            *value += 4;
            cx.notify(&clone);
        });

        assert_eq!(clone.read(|value| *value), 7);
        assert_eq!(cx.entity_notifications, [entity.id()]);
        assert!(!cx.notify_all_entities);
    }

    #[test]
    fn weak_entities_do_not_extend_lifetime() {
        let entity = Entity::new(String::from("shared"));
        let weak = entity.downgrade();
        assert_eq!(weak.upgrade().unwrap().read(Clone::clone), "shared");
        drop(entity);
        assert!(!weak.is_alive());
        assert!(weak.upgrade().is_none());
    }

    #[test]
    fn notification_overflow_falls_back_without_growing_the_queue() {
        let mut cx = EventContext::default();
        for _ in 0..=MAX_ENTITY_NOTIFICATIONS_PER_EVENT {
            cx.notify(&Entity::new(()));
        }

        assert!(cx.notify_all_entities);
        assert!(cx.entity_notifications.is_empty());
    }

    #[test]
    fn typed_events_preserve_order_and_keep_the_source_alive_through_delivery() {
        let entity = Entity::new(Publisher);
        let weak = entity.downgrade();
        let mut cx = EventContext::default();

        assert!(entity.emit(&mut cx, Published(7)));
        assert!(cx.emit(&entity, Published(11)));
        drop(entity);

        assert!(weak.is_alive());
        assert_eq!(cx.entity_events.len(), 2);
        assert_eq!(cx.entity_events[0].event_type, TypeId::of::<Published>());
        assert_eq!(
            cx.entity_events[0].value.downcast_ref::<Published>(),
            Some(&Published(7))
        );
        assert_eq!(
            cx.entity_events[1].value.downcast_ref::<Published>(),
            Some(&Published(11))
        );

        drop(cx);
        assert!(!weak.is_alive());
    }

    #[test]
    fn typed_event_count_is_hard_bounded_per_callback() {
        let entity = Entity::new(Publisher);
        let mut cx = EventContext::default();

        for value in 0..MAX_ENTITY_EVENTS_PER_CALLBACK {
            assert!(entity.emit(&mut cx, Published(value as u16)));
        }
        assert!(!entity.emit(&mut cx, Published(u16::MAX)));
        assert_eq!(cx.entity_events.len(), MAX_ENTITY_EVENTS_PER_CALLBACK);
    }

    #[test]
    fn subscription_drop_cancels_while_detach_preserves_delivery() {
        let (subscription, state) = Subscription::new();
        assert!(state.is_active());
        drop(subscription);
        assert!(!state.is_active());

        let (subscription, detached_state) = Subscription::new();
        subscription.detach();
        assert!(detached_state.is_active());
    }
}