rotulus-layout 0.1.0

The layout engine behind the Rotulus chat view: message model, wrapping, height index, scroll anchoring
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
//! The height index: pixel position ↔ row, in O(log n).
//!
//! This is the structure that makes variable-height rows possible.
//! xtext could skip it because every row was `fontsize × subline_count`,
//! so "what is at pixel Y" was a division (`gtk_xtext_find_char`,
//! xtext.c:1339) and the scroll adjustment's unit was fractional *lines*
//! (`page_size = height / fontsize`, xtext.c:919). Both assumptions have
//! to go for an image to be able to be 240 pixels tall.
//!
//! **Why chunked prefix sums and not a Fenwick tree.** A Fenwick tree is
//! the textbook answer for prefix sums with point updates, and it is the
//! wrong shape here: it is indexed from a fixed origin, and this buffer
//! grows at *both* ends (chat-history backfill prepends) and shrinks at
//! the front (scrollback trim). Every prepend would renumber the whole
//! tree.
//!
//! Instead: rows live in fixed-target-size chunks in a `VecDeque`, each
//! chunk caching its own summed height, with a lazily-repaired running
//! prefix over the chunks. Front insert pushes a chunk; trim pops one;
//! neither touches the rest. A lookup binary-searches the chunk prefixes
//! and then scans within one chunk, which is bounded by [`CHUNK_TARGET`].
//!
//! **Estimated heights.** A row that has never been laid out reports an
//! estimate rather than forcing a measure, so a resize costs O(visible)
//! instead of xtext's O(entire scrollback) (`gtk_xtext_calc_lines`,
//! xtext.c:4395, walks every entry on every width change). The honest
//! cost is that the scrollbar's extent is approximate until the estimates
//! are replaced. That is survivable precisely because scroll position is
//! anchored to a row rather than to a pixel value — see
//! [`crate::anchor`].

use std::collections::VecDeque;

/// Rows per chunk. Chunks are allowed to drift from this after middle
/// inserts; [`SPLIT_AT`] bounds the drift.
const CHUNK_TARGET: usize = 128;
/// A chunk this large is split on the next insert, so the within-chunk
/// scan stays bounded.
const SPLIT_AT: usize = CHUNK_TARGET * 2;

#[derive(Clone, Debug)]
struct Chunk {
    /// Per-row heights in pixels.
    heights: Vec<u32>,
    /// Whether each height came from a real layout pass or is an
    /// estimate. Parallel to `heights`.
    measured: Vec<bool>,
    /// Cached sum of `heights`.
    sum: u64,
}

impl Chunk {
    fn new() -> Chunk {
        Chunk {
            heights: Vec::with_capacity(CHUNK_TARGET),
            measured: Vec::with_capacity(CHUNK_TARGET),
            sum: 0,
        }
    }

    fn len(&self) -> usize {
        self.heights.len()
    }

    fn recompute(&mut self) {
        self.sum = self.heights.iter().map(|h| u64::from(*h)).sum();
    }
}

/// Where a pixel offset lands.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Hit {
    /// Row position (0-based, from the top of the buffer).
    pub row: usize,
    /// Pixels into that row.
    pub offset: u32,
}

/// Chunked prefix-sum index over row heights.
#[derive(Debug, Default)]
pub struct HeightIndex {
    chunks: VecDeque<Chunk>,
    /// `prefix[i]` is the total height of chunks `0..i`. Valid only up
    /// to `prefix_valid`; entries at or past it are stale.
    prefix: Vec<u64>,
    prefix_valid: usize,
    len: usize,
}

impl HeightIndex {
    pub fn new() -> HeightIndex {
        HeightIndex::default()
    }

    /// Number of rows.
    pub fn len(&self) -> usize {
        self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Total height of every row, in pixels.
    pub fn total_height(&mut self) -> u64 {
        self.repair_prefix();
        self.prefix.last().copied().unwrap_or(0)
    }

    /// Height of one row.
    pub fn height_at(&self, row: usize) -> u32 {
        match self.locate_chunk(row) {
            Some((ci, within)) => self.chunks[ci].heights[within],
            None => 0,
        }
    }

    /// Whether row `row`'s height came from a real layout pass.
    pub fn is_measured(&self, row: usize) -> bool {
        match self.locate_chunk(row) {
            Some((ci, within)) => self.chunks[ci].measured[within],
            None => false,
        }
    }

    /// How many rows still carry an estimated height.
    ///
    /// The view uses this to decide whether an idle-time measure backfill
    /// is worth scheduling.
    pub fn unmeasured_count(&self) -> usize {
        self.chunks
            .iter()
            .map(|c| c.measured.iter().filter(|m| !**m).count())
            .sum()
    }

    /// Append a row.
    pub fn push_back(&mut self, height: u32, measured: bool) {
        let need_new = match self.chunks.back() {
            None => true,
            Some(c) => c.len() >= CHUNK_TARGET,
        };
        if need_new {
            self.chunks.push_back(Chunk::new());
        }
        let ci = self.chunks.len() - 1;
        let c = &mut self.chunks[ci];
        c.heights.push(height);
        c.measured.push(measured);
        c.sum += u64::from(height);
        self.len += 1;
        self.invalidate_from(ci);
    }

    /// Prepend a row — the chat-history backfill path.
    pub fn push_front(&mut self, height: u32, measured: bool) {
        let need_new = match self.chunks.front() {
            None => true,
            Some(c) => c.len() >= CHUNK_TARGET,
        };
        if need_new {
            self.chunks.push_front(Chunk::new());
        }
        let c = &mut self.chunks[0];
        c.heights.insert(0, height);
        c.measured.insert(0, true & measured);
        c.sum += u64::from(height);
        self.len += 1;
        self.invalidate_from(0);
    }

    /// Insert at an arbitrary row position.
    ///
    /// Used by the Load-Older path, which inserts before a saved divider
    /// that is generally *not* at the front (server notices precede it,
    /// live messages follow it). O(chunk length) rather than O(n): only
    /// the containing chunk shifts, and the chunk prefixes past it are
    /// marked stale rather than recomputed.
    pub fn insert(&mut self, row: usize, height: u32, measured: bool) {
        if row >= self.len {
            self.push_back(height, measured);
            return;
        }
        let (ci, within) = match self.locate_chunk(row) {
            Some(x) => x,
            None => {
                self.push_back(height, measured);
                return;
            }
        };
        {
            let c = &mut self.chunks[ci];
            c.heights.insert(within, height);
            c.measured.insert(within, measured);
            c.sum += u64::from(height);
        }
        self.len += 1;
        if self.chunks[ci].len() >= SPLIT_AT {
            self.split_chunk(ci);
        }
        self.invalidate_from(ci);
    }

    /// Remove a row, returning its height.
    pub fn remove(&mut self, row: usize) -> Option<u32> {
        let (ci, within) = self.locate_chunk(row)?;
        let h = {
            let c = &mut self.chunks[ci];
            let h = c.heights.remove(within);
            c.measured.remove(within);
            c.sum -= u64::from(h);
            h
        };
        self.len -= 1;
        if self.chunks[ci].len() == 0 && self.chunks.len() > 1 {
            self.chunks.remove(ci);
        }
        self.invalidate_from(ci.min(self.chunks.len().saturating_sub(1)));
        Some(h)
    }

    /// Drop the first `n` rows — the scrollback trim.
    pub fn drain_front(&mut self, n: usize) {
        let mut left = n.min(self.len);
        while left > 0 {
            let front_len = match self.chunks.front() {
                Some(c) => c.len(),
                None => break,
            };
            if front_len <= left {
                let c = self.chunks.pop_front().expect("front exists");
                left -= front_len;
                self.len -= front_len;
                let _ = c;
            } else {
                let c = self.chunks.front_mut().expect("front exists");
                c.heights.drain(..left);
                c.measured.drain(..left);
                c.recompute();
                self.len -= left;
                left = 0;
            }
        }
        self.invalidate_from(0);
    }

    /// Replace a row's height, e.g. when a real layout pass supersedes an
    /// estimate or an image texture finally lands.
    pub fn set_height(&mut self, row: usize, height: u32, measured: bool) {
        let Some((ci, within)) = self.locate_chunk(row) else {
            return;
        };
        let c = &mut self.chunks[ci];
        let old = c.heights[within];
        if old == height && c.measured[within] == measured {
            return;
        }
        c.sum = c.sum - u64::from(old) + u64::from(height);
        c.heights[within] = height;
        c.measured[within] = measured;
        self.invalidate_from(ci);
    }

    /// Mark every row unmeasured, keeping the current heights as
    /// estimates.
    ///
    /// This is a width or font change. Note what it does *not* do:
    /// re-measure anything. The old heights stay as the estimate so the
    /// scrollbar remains plausible, and rows are re-laid-out lazily as
    /// they become visible. That is the whole reason resize is O(visible)
    /// here and O(scrollback) in xtext.
    pub fn invalidate_all_measurements(&mut self) {
        for c in &mut self.chunks {
            c.measured.fill(false);
        }
    }

    /// Pixel offset of the top of row `row`.
    pub fn offset_of(&mut self, row: usize) -> u64 {
        if row == 0 || self.len == 0 {
            return 0;
        }
        self.repair_prefix();
        let mut remaining = row.min(self.len);
        let mut acc = 0u64;
        for (i, c) in self.chunks.iter().enumerate() {
            if remaining >= c.len() {
                acc = self.prefix[i + 1];
                remaining -= c.len();
                if remaining == 0 {
                    return acc;
                }
            } else {
                for h in &c.heights[..remaining] {
                    acc += u64::from(*h);
                }
                return acc;
            }
        }
        acc
    }

    /// Which row contains pixel offset `y`, and how far into it.
    ///
    /// `y` past the end clamps to the last row's bottom edge, so a caller
    /// that over-scrolls gets a sane answer rather than `None`.
    pub fn locate(&mut self, y: u64) -> Option<Hit> {
        if self.len == 0 {
            return None;
        }
        self.repair_prefix();
        let total = self.prefix.last().copied().unwrap_or(0);
        if y >= total {
            let row = self.len - 1;
            return Some(Hit {
                row,
                offset: self.height_at(row),
            });
        }
        // Binary search for the chunk whose span contains y.
        let mut lo = 0usize;
        let mut hi = self.chunks.len();
        while lo + 1 < hi {
            let mid = lo + (hi - lo) / 2;
            if self.prefix[mid] <= y {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        let ci = lo;
        let mut acc = self.prefix[ci];
        let start_row = self.chunk_start_row(ci);
        for (i, h) in self.chunks[ci].heights.iter().enumerate() {
            let h64 = u64::from(*h);
            if acc + h64 > y {
                return Some(Hit {
                    row: start_row + i,
                    offset: (y - acc) as u32,
                });
            }
            acc += h64;
        }
        // Only reachable on a zero-height tail; clamp.
        Some(Hit {
            row: self.len - 1,
            offset: 0,
        })
    }

    // ---- internals ------------------------------------------------

    fn chunk_start_row(&self, ci: usize) -> usize {
        self.chunks.iter().take(ci).map(|c| c.len()).sum()
    }

    fn locate_chunk(&self, row: usize) -> Option<(usize, usize)> {
        if row >= self.len {
            return None;
        }
        let mut acc = 0usize;
        for (i, c) in self.chunks.iter().enumerate() {
            if row < acc + c.len() {
                return Some((i, row - acc));
            }
            acc += c.len();
        }
        None
    }

    fn split_chunk(&mut self, ci: usize) {
        let at = self.chunks[ci].len() / 2;
        let tail_h = self.chunks[ci].heights.split_off(at);
        let tail_m = self.chunks[ci].measured.split_off(at);
        self.chunks[ci].recompute();
        let mut tail = Chunk {
            heights: tail_h,
            measured: tail_m,
            sum: 0,
        };
        tail.recompute();
        self.chunks.insert(ci + 1, tail);
    }

    fn invalidate_from(&mut self, ci: usize) {
        self.prefix_valid = self.prefix_valid.min(ci);
    }

    /// Rebuild the stale tail of the chunk prefix sums.
    ///
    /// Amortised: a batch of appends invalidates once and repairs once,
    /// on the next query, rather than repairing per append.
    fn repair_prefix(&mut self) {
        let n = self.chunks.len();
        if self.prefix.len() != n + 1 {
            self.prefix.resize(n + 1, 0);
            self.prefix_valid = self.prefix_valid.min(n);
        }
        if self.prefix_valid >= n {
            return;
        }
        let start = self.prefix_valid;
        let mut acc = self.prefix[start];
        for i in start..n {
            acc += self.chunks[i].sum;
            self.prefix[i + 1] = acc;
        }
        self.prefix_valid = n;
    }
}