gpui-fast 0.1.10

gpui: GPUI with gpui-fast rendering and layout optimizations
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
// Modified by Longbridge for gpui-fast.
//! Accessibility nodes drawn again from the last frame.
//!
//! While assistive technology is attached, a window builds an AccessKit tree
//! every frame and hands the whole of it to the platform: each element with
//! an id and a role pushes a node as it prepaints, its children's nodes
//! hang off it, and a focusable element says whether it holds the focus.
//! A retained subtree drawn again from the last frame is not prepainted, so
//! none of that would happen for it.
//!
//! Instead, a frame logs what building its tree did, in order — a node
//! pushed, a leaf added, a node finished, an element made focusable, an
//! active descendant claimed — and keeps where in the log each stretch of
//! the frame starts and ends, as it keeps where its hitboxes do
//! ([`crate::window::PrepaintStateIndex::fast_a11y_index`]). Drawing a
//! stretch again from the last frame does again what its part of the log
//! did, through the same builder: nodes are pushed where the stretch lands,
//! so they hang off whatever node is open there; a finished node is last
//! frame's node with the children this frame gave it, so a view rebuilt
//! inside a stretch drawn again (see [`crate::fast::splice`]) hangs its own
//! nodes off the nodes around it; and the focus is asked again, so a node
//! is focused if its element's focus handle is focused now. The finished
//! nodes are read from the tree last sent, which the window keeps anyway
//! for debugging. A stretch drawn again logs what it did as the frame it
//! lands in logs anything, so it can be drawn again on the next frame.
//!
//! The listeners an element registers for accessibility actions as it
//! paints are logged too, and a stretch of paint drawn again moves them
//! from last frame's into this frame's, as it moves mouse listeners.
//!
//! A scroll layer's content (see [`crate::fast::layers`]) is composited at
//! another offset than it was painted at: its nodes are kept, as painted,
//! in an [`A11yStretch`], and done again moved by the scroll since, as its
//! hitboxes are.

use crate::{
    Bounds, FocusId, PaintIndex, Pixels, Point, PrepaintStateIndex, Window,
    window::a11y::{A11y, A11yActionListener},
};
use accesskit::{Action, NodeId};
use collections::FxHashMap;
use std::{mem, ops::Range, rc::Rc};

/// One thing building a frame's accessibility tree did.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Op {
    /// A node pushed: it hangs off the node open then and is open until
    /// its [`Op::Pop`].
    Push(NodeId),
    /// A leaf added under the node open, finished at this index of the
    /// frame's nodes.
    Leaf(usize),
    /// The node open finished, at this index of the frame's nodes.
    Pop(usize),
    /// A node made focusable with a focus handle, focused if the handle is.
    Focusable(NodeId, FocusId),
    /// A node claiming to be the active descendant of the focused node.
    ActiveDescendant(NodeId),
}

/// What a window's accessibility tree builder logs, and last frame's log
/// to draw stretches again from. Lives in the builder.
#[derive(Default)]
pub(crate) struct A11yLog {
    /// Whether the frame being drawn logs: accessibility was active as it
    /// began.
    recording: bool,
    /// Whether the last frame was logged, so it can be drawn again from.
    previous_recorded: bool,
    /// Whether a stretch drawn again this frame logged other ops than it
    /// did last frame (a node id pushed twice is not pushed), so that the
    /// indices of the stretches after it do not point at their ops: the
    /// next frame does not draw again from this one.
    diverged: bool,
    ops: Vec<Op>,
    previous_ops: Vec<Op>,
    /// The accessibility action listeners registered while painting, in
    /// order, by the node they listen on.
    actions: Vec<(NodeId, Action)>,
    /// Last frame's, with where each listener is among those of its node.
    previous_actions: Vec<(NodeId, Action, usize)>,
    previous_listeners: FxHashMap<NodeId, Vec<Option<(Action, A11yActionListener)>>>,
    previous_bounds: FxHashMap<NodeId, Bounds<Pixels>>,
    #[cfg(debug_assertions)]
    previous_info: FxHashMap<NodeId, crate::window::a11y::debug::NodeDebugInfo>,
    /// For each node pushed again and not yet finished, whether it was
    /// pushed. Kept through the frame: a view drawn again around a nested
    /// view built again finishes its nodes after the nested view.
    replay_open: Vec<bool>,
}

impl A11yLog {
    #[inline]
    pub(crate) fn push(log: &mut Self, id: NodeId) {
        if log.recording {
            log.ops.push(Op::Push(id));
        }
    }

    #[inline]
    pub(crate) fn leaf(log: &mut Self, at: usize) {
        if log.recording {
            log.ops.push(Op::Leaf(at));
        }
    }

    #[inline]
    pub(crate) fn pop(log: &mut Self, at: usize) {
        if log.recording {
            log.ops.push(Op::Pop(at));
        }
    }

    #[inline]
    pub(crate) fn focusable(log: &mut Self, node: NodeId, focus: FocusId) {
        if log.recording {
            log.ops.push(Op::Focusable(node, focus));
        }
    }

    #[inline]
    pub(crate) fn active_descendant(log: &mut Self, node: NodeId) {
        if log.recording {
            log.ops.push(Op::ActiveDescendant(node));
        }
    }

    #[inline]
    pub(crate) fn action(log: &mut Self, node: NodeId, action: Action) {
        if log.recording {
            log.actions.push((node, action));
        }
    }

    /// Ends the frame's log, which becomes the last frame's.
    pub(crate) fn end_frame(log: &mut Self) {
        mem::swap(&mut log.ops, &mut log.previous_ops);
        log.ops.clear();
    }

    /// Starts a frame, whether accessibility is active or not.
    pub(crate) fn new_frame(log: &mut Self) {
        log.previous_recorded = log.recording && !mem::take(&mut log.diverged);
        log.recording = false;
    }
}

/// Starts logging a frame in which accessibility is active, keeping what
/// the last frame left that is about to be cleared: the bounds of its
/// nodes, its action listeners and, in debug builds, where its nodes came
/// from.
pub(crate) fn begin_frame(a11y: &mut A11y) {
    let log = &mut a11y.nodes.fast;
    log.recording = true;
    log.replay_open.clear();
    log.ops.clear();
    mem::swap(&mut log.previous_bounds, &mut a11y.node_bounds);
    let mut counts: FxHashMap<NodeId, usize> = FxHashMap::default();
    log.previous_actions.clear();
    for (node, action) in log.actions.drain(..) {
        let count = counts.entry(node).or_default();
        log.previous_actions.push((node, action, *count));
        *count += 1;
    }
    log.previous_listeners = mem::take(&mut a11y.action_listeners)
        .into_iter()
        .map(|(node, listeners)| (node, listeners.into_iter().map(Some).collect()))
        .collect();
    #[cfg(debug_assertions)]
    {
        log.previous_info = mem::take(&mut a11y.nodes.node_info);
    }
}

/// Where the frame being drawn stands in its log, for a stretch of its
/// prepaint to know where its part of the log lies.
#[inline]
pub(crate) fn ops_index(window: &Window) -> usize {
    window.a11y.nodes.fast.ops.len()
}

/// Where the frame being drawn stands in its log of action listeners.
#[inline]
pub(crate) fn actions_index(window: &Window) -> usize {
    window.a11y.nodes.fast.actions.len()
}

/// Whether accessibility is active but the last frame was drawn without
/// it, so it logged nothing to draw anything again from.
#[inline]
pub(crate) fn stale(a11y: &A11y) -> bool {
    a11y.is_active() && !a11y.nodes.fast.previous_recorded
}

/// What drawing a stretch again draws from: a log, the finished nodes its
/// indices point into, and the bounds and debug information of its nodes.
struct Source<'a> {
    ops: &'a [Op],
    nodes: &'a [(NodeId, accesskit::Node)],
    bounds: &'a FxHashMap<NodeId, Bounds<Pixels>>,
    #[cfg(debug_assertions)]
    info: &'a FxHashMap<NodeId, crate::window::a11y::debug::NodeDebugInfo>,
}

/// Does again what building the accessibility tree did over `range` of
/// the last frame's prepaint, which the frame being drawn copies.
#[inline]
pub(crate) fn reuse_prepaint(window: &mut Window, range: &Range<PrepaintStateIndex>) {
    let ops = range.start.fast_a11y_index..range.end.fast_a11y_index;
    if ops.is_empty() || !window.a11y.is_active() {
        return;
    }
    reuse_ops(window, ops);
}

#[inline(never)]
fn reuse_ops(window: &mut Window, range: Range<usize>) {
    let log = &mut window.a11y.nodes.fast;
    if !log.previous_recorded {
        debug_assert!(
            false,
            "drew again from a frame that logged no accessibility"
        );
        return;
    }
    let ops = mem::take(&mut log.previous_ops);
    let bounds = mem::take(&mut log.previous_bounds);
    #[cfg(debug_assertions)]
    let info = mem::take(&mut log.previous_info);
    let update = window.a11y.debug.last_tree_update.take();
    if let Some(ops_range) = ops.get(range) {
        let source = Source {
            ops: ops_range,
            nodes: update.as_ref().map_or(&[], |update| &update.nodes),
            bounds: &bounds,
            #[cfg(debug_assertions)]
            info: &info,
        };
        replay(window, &source, Point::default());
    } else {
        debug_assert!(
            false,
            "a stretch reaches past last frame's accessibility log"
        );
    }
    window.a11y.debug.last_tree_update = update;
    let log = &mut window.a11y.nodes.fast;
    log.previous_ops = ops;
    log.previous_bounds = bounds;
    #[cfg(debug_assertions)]
    {
        log.previous_info = info;
    }
}

/// Does again what `source` logged, its nodes moved by `delta`.
fn replay(window: &mut Window, source: &Source, delta: Point<Pixels>) {
    let scale = window.scale_factor();
    let shift = (delta != Point::default())
        .then(|| ((delta.x.0 * scale) as f64, (delta.y.0 * scale) as f64));
    let moved = |node: &accesskit::Node| {
        let mut node = node.clone();
        if let Some((dx, dy)) = shift
            && let Some(rect) = node.bounds()
        {
            node.set_bounds(accesskit::Rect {
                x0: rect.x0 + dx,
                y0: rect.y0 + dy,
                x1: rect.x1 + dx,
                y1: rect.y1 + dy,
            });
        }
        node
    };
    let logged = window.a11y.nodes.fast.ops.len();
    for op in source.ops {
        match *op {
            Op::Push(id) => {
                let pushed = window.a11y.nodes.push(id, accesskit::Node::default());
                window.a11y.nodes.fast.replay_open.push(pushed);
                if pushed {
                    if let Some(bounds) = source.bounds.get(&id) {
                        let bounds = Bounds::new(bounds.origin + delta, bounds.size);
                        window.a11y.node_bounds.insert(id, bounds);
                    }
                    #[cfg(debug_assertions)]
                    if let Some(info) = source.info.get(&id) {
                        window.a11y.nodes.record_node_info(id, info.clone());
                    }
                }
            }
            Op::Leaf(at) => {
                let Some((id, node)) = source.nodes.get(at) else {
                    debug_assert!(false, "a leaf missing from last frame's tree");
                    continue;
                };
                let _pushed = window.a11y.nodes.push_leaf(*id, moved(node));
                #[cfg(debug_assertions)]
                if _pushed && let Some(info) = source.info.get(id) {
                    window.a11y.nodes.record_node_info(*id, info.clone());
                }
            }
            Op::Pop(at) => {
                if window.a11y.nodes.fast.replay_open.pop() != Some(true) {
                    continue;
                }
                let mut node = match source.nodes.get(at) {
                    Some((_, node)) => moved(node),
                    None => {
                        debug_assert!(false, "a node missing from last frame's tree");
                        accesskit::Node::default()
                    }
                };
                if let Some(open) = window.a11y.nodes.nodes_stack.last_mut() {
                    // Its children are this frame's: those pushed again and
                    // those of nested views built again.
                    if open.children() != node.children() {
                        if open.children().is_empty() {
                            node.clear_children();
                        } else {
                            node.set_children(open.children().to_vec());
                        }
                    }
                    *open = node;
                }
                window.a11y.nodes.pop();
            }
            Op::Focusable(node, focus) => {
                window.a11y.set_focusable(node, focus);
                if focus.is_focused(window) {
                    window.a11y.set_focus(node);
                }
            }
            Op::ActiveDescendant(node) => window.a11y.set_active_descendant(node),
        }
    }
    let log = &mut window.a11y.nodes.fast;
    log.diverged |= log.ops.len() - logged != source.ops.len();
}

/// Moves the accessibility action listeners last frame's paint of `range`
/// registered into the frame being drawn, which copies that paint.
#[inline]
pub(crate) fn reuse_paint(window: &mut Window, range: &Range<PaintIndex>) {
    let actions = range.start.fast.a11y_actions..range.end.fast.a11y_actions;
    if actions.is_empty() || !window.a11y.is_active() {
        return;
    }
    reuse_actions(window, actions);
}

#[inline(never)]
fn reuse_actions(window: &mut Window, range: Range<usize>) {
    let a11y = &mut window.a11y;
    let log = &mut a11y.nodes.fast;
    let Some(actions) = log.previous_actions.get(range) else {
        debug_assert!(
            false,
            "a stretch reaches past last frame's action listeners"
        );
        return;
    };
    for &(node, action, index) in actions {
        let listener = log
            .previous_listeners
            .get_mut(&node)
            .and_then(|listeners| listeners.get_mut(index))
            .and_then(Option::take);
        log.actions.push((node, action));
        if let Some(listener) = listener {
            a11y.action_listeners
                .entry(node)
                .or_default()
                .push(listener);
        }
    }
}

/// What building the accessibility tree did over a stretch of prepaint,
/// kept apart from the frame it was drawn in: a scroll layer's content, to
/// be done again wherever the layer is composited.
pub(crate) struct A11yStretch {
    ops: Vec<Op>,
    nodes: Vec<(NodeId, accesskit::Node)>,
    bounds: FxHashMap<NodeId, Bounds<Pixels>>,
    #[cfg(debug_assertions)]
    info: FxHashMap<NodeId, crate::window::a11y::debug::NodeDebugInfo>,
    /// Whether a synthetic node (see [`crate::Element::a11y_synthetic_children`])
    /// has bounds. Its element's code placed it, and nothing says it moves
    /// with its element: the content is painted again rather than moved.
    placed_synthetic: bool,
}

/// Keeps what building the accessibility tree did over `range` of the
/// frame being drawn's prepaint, if accessibility is active.
pub(crate) fn capture(
    window: &Window,
    range: &Range<PrepaintStateIndex>,
) -> Option<Rc<A11yStretch>> {
    if !window.a11y.is_active() {
        return None;
    }
    let builder = &window.a11y.nodes;
    let ops = builder
        .fast
        .ops
        .get(range.start.fast_a11y_index..range.end.fast_a11y_index)?;
    let mut stretch = A11yStretch {
        ops: Vec::with_capacity(ops.len()),
        nodes: Vec::new(),
        bounds: FxHashMap::default(),
        #[cfg(debug_assertions)]
        info: FxHashMap::default(),
        placed_synthetic: false,
    };
    for op in ops {
        let op = match *op {
            Op::Leaf(at) | Op::Pop(at) => {
                let node = builder.all_nodes.get(at)?.clone();
                let local = stretch.nodes.len();
                #[cfg(debug_assertions)]
                if matches!(op, Op::Leaf(_))
                    && let Some(info) = builder.node_info.get(&node.0)
                {
                    stretch.info.insert(node.0, info.clone());
                }
                stretch.placed_synthetic |= matches!(op, Op::Leaf(_)) && node.1.bounds().is_some();
                stretch.nodes.push(node);
                if matches!(op, Op::Leaf(_)) {
                    Op::Leaf(local)
                } else {
                    Op::Pop(local)
                }
            }
            Op::Push(id) => {
                if let Some(bounds) = window.a11y.node_bounds.get(&id) {
                    stretch.bounds.insert(id, *bounds);
                }
                #[cfg(debug_assertions)]
                if let Some(info) = builder.node_info.get(&id) {
                    stretch.info.insert(id, info.clone());
                }
                Op::Push(id)
            }
            other => other,
        };
        stretch.ops.push(op);
    }
    Some(Rc::new(stretch))
}

/// Does again what `stretch` kept, its nodes moved by `delta`, if
/// accessibility is active.
pub(crate) fn carry(window: &mut Window, stretch: Option<&A11yStretch>, delta: Point<Pixels>) {
    let Some(stretch) = stretch else {
        return;
    };
    if !window.a11y.is_active() {
        return;
    }
    let source = Source {
        ops: &stretch.ops,
        nodes: &stretch.nodes,
        bounds: &stretch.bounds,
        #[cfg(debug_assertions)]
        info: &stretch.info,
    };
    replay(window, &source, delta);
}

/// Whether a scroll layer whose content kept `stretch` has to be painted
/// again rather than composited: accessibility is active, and the content
/// was painted while it was not, or holds synthetic nodes placed by code
/// that may not move them with the scroll.
#[inline]
pub(crate) fn layer_needs_repaint(window: &Window, stretch: Option<&A11yStretch>) -> bool {
    window.a11y.is_active() && stretch.is_none_or(|stretch| stretch.placed_synthetic)
}

#[cfg(test)]
impl Window {
    /// Makes the window build its accessibility tree from the next frame on
    /// as though assistive technology had asked for it, or stop.
    pub(crate) fn set_a11y_active_for_tests(&mut self, active: bool) {
        crate::fast::a11y::set_active_flag(&self.a11y, active);
        self.refresh();
    }
}

/// Starts a frame of `window`, whether accessibility is active or not.
#[inline]
pub(crate) fn new_frame(window: &mut Window) {
    #[cfg(any(test, feature = "test-support"))]
    if forced_by_env() {
        set_active_flag(&window.a11y, true);
    }
    A11yLog::new_frame(&mut window.a11y.nodes.fast);
}

/// Whether `GPUI_A11Y_ACTIVE=1` asks every window to build its
/// accessibility tree as though assistive technology had asked for it, for
/// benchmarks (`gpui_perf`) to measure frames drawn with it.
#[cfg(any(test, feature = "test-support"))]
fn forced_by_env() -> bool {
    static FORCED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *FORCED.get_or_init(|| std::env::var("GPUI_A11Y_ACTIVE").is_ok_and(|value| value == "1"))
}

#[cfg(any(test, feature = "test-support"))]
pub(crate) fn set_active_flag(a11y: &A11y, active: bool) {
    a11y.active_flag
        .store(active, std::sync::atomic::Ordering::SeqCst);
}

/// The accessibility tree last sent to the platform, for tests to compare.
#[cfg(test)]
pub(crate) fn last_tree(window: &Window) -> Option<&accesskit::TreeUpdate> {
    window.a11y.debug.last_tree_update.as_ref()
}