quickgui 0.1.4

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use super::*;
use crate::ElementUpdate;

const MAX_COMPONENT_SCOPES: usize = 262_144;

type ScopeCallback = Rc<RefCell<dyn FnMut(&mut ViewContext<'_, ()>) -> ScopeOutput>>;

pub(super) enum ScopeOutput {
    Element(Element),
    Update(ElementUpdate),
}

pub(super) struct ComponentScope {
    parent: Option<ElementId>,
    callback: Option<ScopeCallback>,
    entities: HashSet<EntityId>,
    globals: HashSet<TypeId>,
    children: HashSet<ElementId>,
    order: u64,
}

pub(super) struct PendingComponent {
    pub id: ElementId,
    parent: Option<ElementId>,
    callback: Option<ScopeCallback>,
}

#[derive(Default)]
pub(super) struct ComponentScopes {
    entries: HashMap<ElementId, ComponentScope>,
    entities: HashMap<EntityId, HashSet<ElementId>>,
    globals: HashMap<TypeId, HashSet<ElementId>>,
    dirty: HashSet<ElementId>,
    next_order: u64,
}

impl ComponentScopes {
    pub(super) fn observe_entity(&mut self, scope: ElementId, entity: EntityId) {
        assert!(
            self.entities.contains_key(&entity)
                || self.entities.len() < MAX_OBSERVED_ENTITIES_PER_WINDOW,
            "too many scoped entity dependencies"
        );
        self.entries
            .get_mut(&scope)
            .expect("mounted component scope")
            .entities
            .insert(entity);
        self.entities.entry(entity).or_default().insert(scope);
    }

    pub(super) fn observe_global(&mut self, scope: ElementId, global: TypeId) {
        assert!(
            self.globals.contains_key(&global)
                || self.globals.len() < MAX_OBSERVED_GLOBALS_PER_WINDOW,
            "too many scoped global dependencies"
        );
        self.entries
            .get_mut(&scope)
            .expect("mounted component scope")
            .globals
            .insert(global);
        self.globals.entry(global).or_default().insert(scope);
    }

    pub(super) fn invalidate_entities(&mut self, entities: &[EntityId]) -> bool {
        let mut changed = false;
        for entity in entities {
            if let Some(scopes) = self.entities.get(entity) {
                changed |= !scopes.is_empty();
                self.dirty.extend(scopes);
            }
        }
        changed
    }

    pub(super) fn invalidate_globals(&mut self, globals: &[TypeId]) -> bool {
        let mut changed = false;
        for global in globals {
            if let Some(scopes) = self.globals.get(global) {
                changed |= !scopes.is_empty();
                self.dirty.extend(scopes);
            }
        }
        changed
    }

    pub(super) fn invalidate(&mut self, ids: &[ElementId]) -> bool {
        if !ids.iter().all(|id| self.entries.contains_key(id)) {
            return false;
        }
        self.dirty.extend(ids);
        true
    }

    pub(super) fn pending(&self) -> bool {
        !self.dirty.is_empty()
    }

    fn subtree(&self, owner: ElementId) -> Vec<ElementId> {
        let mut ids = vec![owner];
        let mut index = 0;
        while index < ids.len() {
            if let Some(scope) = self.entries.get(&ids[index]) {
                ids.extend(&scope.children);
            }
            index += 1;
        }
        ids
    }

    pub(super) fn take_dirty_roots(&mut self) -> Vec<ElementId> {
        let dirty = std::mem::take(&mut self.dirty);
        let mut roots: Vec<_> = dirty
            .iter()
            .copied()
            .filter(|id| {
                let mut parent = self.entries.get(id).and_then(|scope| scope.parent);
                while let Some(id) = parent {
                    if dirty.contains(&id) {
                        return false;
                    }
                    parent = self.entries.get(&id).and_then(|scope| scope.parent);
                }
                true
            })
            .collect();
        roots.sort_unstable_by_key(|id| self.entries.get(id).map(|scope| scope.order));
        roots
    }

    fn remove(&mut self, id: ElementId) {
        if let Some(scope) = self.entries.remove(&id) {
            if let Some(parent) = scope
                .parent
                .and_then(|parent| self.entries.get_mut(&parent))
            {
                parent.children.remove(&id);
            }
            for entity in scope.entities {
                if let Some(scopes) = self.entities.get_mut(&entity) {
                    scopes.remove(&id);
                    if scopes.is_empty() {
                        self.entities.remove(&entity);
                    }
                }
            }
            for global in scope.globals {
                if let Some(scopes) = self.globals.get_mut(&global) {
                    scopes.remove(&id);
                    if scopes.is_empty() {
                        self.globals.remove(&global);
                    }
                }
            }
        }
        self.dirty.remove(&id);
    }
}

impl ListenerRegistry {
    pub(super) fn needs_scoped_replacement(&self, updates: &[ElementUpdate]) -> bool {
        // An external replacement has no declaration context in which to register or retire
        // scoped callbacks. Re-enter the owning renderer instead of leaving detached subscribers
        // active or retaining callbacks that captured the previous component values.
        !self.scopes.entries.is_empty()
            && updates
                .iter()
                .any(|update| matches!(update, ElementUpdate::Replace { .. }))
    }

    pub(super) fn prepare_component_updates(&mut self) -> Vec<PendingComponent> {
        let pending: Vec<_> = self
            .scopes
            .take_dirty_roots()
            .into_iter()
            .filter_map(|id| {
                let scope = self.scopes.entries.get(&id)?;
                Some(PendingComponent {
                    id,
                    parent: scope.parent,
                    callback: scope.callback.clone(),
                })
            })
            .collect();
        // Retire all old roots before declaring any new root. A keyed child may move from a
        // later root to an earlier one in the same batch without a transient duplicate listener.
        for scope in &pending {
            self.clear_component(scope.id);
        }
        pending
    }

    fn remove_component(&mut self, id: ElementId) {
        self.clear_scope_listeners(id);
        self.scopes.remove(id);
    }

    fn clear_component(&mut self, owner: ElementId) {
        for id in self.scopes.subtree(owner).into_iter().rev() {
            self.remove_component(id);
        }
    }

    pub(super) fn retain_components(&mut self, root: &Element, owner: Option<ElementId>) {
        fn collect(element: &Element, ids: &mut HashSet<ElementId>) {
            if let Some(id) = element.explicit_id {
                ids.insert(id);
            }
            for child in &element.children {
                collect(child, ids);
            }
        }
        if self.scopes.entries.is_empty() {
            return;
        }
        let mut mounted = HashSet::new();
        collect(root, &mut mounted);
        let candidates = owner
            .map(|owner| self.scopes.subtree(owner))
            .unwrap_or_else(|| self.scopes.entries.keys().copied().collect());
        let removed: Vec<_> = candidates
            .into_iter()
            .filter(|id| !mounted.contains(id))
            .collect();
        for id in removed {
            self.remove_component(id);
        }
    }
}

impl<V: 'static> ViewContext<'_, V> {
    /// Declare a restartable component. Entity/global reads inside this callback belong to this
    /// component; notifying them re-executes only affected scopes. Capture retained `Entity`
    /// handles rather than a snapshot of their value. The returned root receives `id`: return an
    /// unkeyed root or one with that same ID, and put nested components in its children.
    pub fn component<E: IntoElement>(
        &mut self,
        id: impl Into<ElementId>,
        mut render: impl FnMut(&mut ViewContext<'_, V>) -> E + 'static,
    ) -> Element {
        let id = id.into();
        let callback: ScopeCallback = Rc::new(RefCell::new(move |cx: &mut ViewContext<'_, ()>| {
            ScopeOutput::Element(cx.with_type::<V, _>(|cx| render(cx).into_element()))
        }));
        self.declare_component(id, Some(callback.clone()));
        let parent = self.listeners.current_scope.replace(id);
        let ScopeOutput::Element(element) = self.with_type::<(), _>(|cx| callback.borrow_mut()(cx))
        else {
            unreachable!()
        };
        self.listeners.current_scope = parent;
        element.id(id)
    }

    /// Attach a value binding to an explicitly identified element. The callback returns a direct
    /// text/color/opacity/transform update for that element. Its dependencies restart this binding
    /// without rebuilding the containing component; the update determines the affected phase.
    pub fn bind(
        &mut self,
        mut element: Element,
        mut read: impl FnMut(&mut ViewContext<'_, V>) -> ElementUpdate + 'static,
    ) -> Element {
        let id = element
            .explicit_id
            .expect("a bound element needs an explicit ID");
        let callback: ScopeCallback = Rc::new(RefCell::new(move |cx: &mut ViewContext<'_, ()>| {
            ScopeOutput::Update(cx.with_type::<V, _>(|cx| read(cx)))
        }));
        self.declare_component(id, Some(callback.clone()));
        let parent = self.listeners.current_scope.replace(id);
        let ScopeOutput::Update(update) = self.with_type::<(), _>(|cx| callback.borrow_mut()(cx))
        else {
            unreachable!()
        };
        self.listeners.current_scope = parent;
        assert!(
            update.apply_to_declaration(&mut element),
            "a binding must update its own element with a direct property"
        );
        self.assert_value_binding(id);
        element
    }

    /// Give an embedding renderer ownership of the listeners and dependencies declared inside
    /// this subtree. Implement `View::render_scope` to rebuild it after `invalidate_elements`.
    pub fn with_scope<E: IntoElement>(
        &mut self,
        id: impl Into<ElementId>,
        render: impl FnOnce(&mut Self) -> E,
    ) -> Element {
        let id = id.into();
        self.declare_component(id, None);
        let parent = self.listeners.current_scope.replace(id);
        let element = render(self).into_element().id(id);
        self.listeners.current_scope = parent;
        element
    }

    fn declare_component(&mut self, id: ElementId, callback: Option<ScopeCallback>) {
        let scopes = &mut self.listeners.scopes;
        assert!(
            scopes.entries.len() < MAX_COMPONENT_SCOPES,
            "too many component scopes in one window"
        );
        assert!(
            !scopes.entries.contains_key(&id),
            "component scope {id:?} was declared twice"
        );
        let scope = ComponentScope {
            parent: self.listeners.current_scope,
            callback,
            entities: HashSet::new(),
            globals: HashSet::new(),
            children: HashSet::new(),
            order: scopes.next_order,
        };
        if let Some(parent) = scope.parent {
            scopes
                .entries
                .get_mut(&parent)
                .expect("declaring component parent")
                .children
                .insert(id);
        }
        scopes.next_order = scopes
            .next_order
            .checked_add(1)
            .expect("component order exhausted");
        scopes.entries.insert(id, scope);
    }

    fn assert_value_binding(&self, id: ElementId) {
        assert!(
            self.listeners
                .scope_listeners
                .get(&id)
                .is_none_or(Vec::is_empty)
                && self.listeners.scopes.entries[&id].children.is_empty(),
            "value bindings cannot declare listeners or child components"
        );
    }

    pub(super) fn refresh_component(
        &mut self,
        pending: PendingComponent,
        render_external: impl FnOnce(&mut Self) -> Option<Element>,
    ) -> Option<ElementUpdate> {
        let PendingComponent {
            id,
            parent,
            callback,
        } = pending;
        self.listeners.current_scope = parent;
        let output = if let Some(callback) = callback {
            self.declare_component(id, Some(callback.clone()));
            self.listeners.current_scope = Some(id);
            self.with_type::<(), _>(|cx| callback.borrow_mut()(cx))
        } else {
            // The embedding renderer calls with_scope again while rebuilding this root.
            ScopeOutput::Element(render_external(self)?)
        };
        self.listeners.current_scope = None;
        Some(match output {
            ScopeOutput::Element(element) => {
                let element = element.id(id);
                self.listeners.retain_components(&element, Some(id));
                ElementUpdate::Replace {
                    id,
                    element: Box::new(element),
                }
            }
            ScopeOutput::Update(update) => {
                assert_eq!(update.id(), id, "a binding cannot change another element");
                assert!(
                    !matches!(update, ElementUpdate::Replace { .. }),
                    "use a component for structural changes"
                );
                self.assert_value_binding(id);
                update
            }
        })
    }

    pub(super) fn with_type<T: 'static, R>(
        &mut self,
        call: impl FnOnce(&mut ViewContext<'_, T>) -> R,
    ) -> R {
        let mut cx = ViewContext::<T> {
            size: self.size,
            scale_factor: self.scale_factor,
            metrics: self.metrics,
            focused: self.focused,
            focused_path: self.focused_path.clone(),
            request_animation_frame: false,
            repaint_deadline: None,
            listeners: self.listeners,
            window: self.window,
            window_state: self.window_state,
            displays: self.displays,
            keyboard_layout: self.keyboard_layout,
            assets: self.assets,
            app_info: self.app_info,
            app_paths: self.app_paths,
            system_info: self.system_info,
            system_preferences: self.system_preferences,
            background_tasks: self.background_tasks,
            foreground_tasks: self.foreground_tasks,
            globals: self.globals,
            event_proxy: self.event_proxy,
            marker: PhantomData,
        };
        let result = call(&mut cx);
        self.request_animation_frame |= cx.request_animation_frame;
        self.repaint_deadline = self
            .repaint_deadline
            .into_iter()
            .chain(cx.repaint_deadline)
            .min();
        result
    }
}