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
//! Scroll boxes, as the app reads and drives them: how far one is scrolled, scrolling to a
//! place, staying at the end as content grows (a chat, a log), bringing a box or a long list's
//! row into view. Also a box's room (to fit content to), the wheel over an app's own box, and
//! long lists of rows of varying height.
//!
//! Requests are carried out where the library scrolls boxes, after layout (`Ui::scroll`): so
//! they use this run's positions, and the window is described once more before anything is
//! drawn. What the person does to a box comes before the app's wish that it stay at its end:
//! scrolled back, it stays where put.
use crate::geom::Size;
use crate::id::{ById, Id, hash};
use crate::layout::{Length, NONE};
use crate::ui::{Measure, Response, Ui};
use std::ops::Range;
/// Tells a box's room request apart from its own id.
const ROOM: u64 = 0x726f_6f6d;
/// How far a scroll box is scrolled ([`Ui::scrolled`]).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Scrolled {
/// How far it is scrolled: across and down, in units from its start.
pub by: [f32; 2],
/// How far it can scroll each way: to where its content's end shows.
pub most: [f32; 2],
}
impl Scrolled {
/// Whether it shows its content's vertical end (as one with no more content than it shows
/// does).
pub fn at_end(&self) -> bool {
self.by[1] >= self.most[1] - 0.5
}
}
/// Where the app asked a box to scroll: a place per axis, or `NAN` to leave that axis; and
/// whether the person's scrolling goes first.
#[derive(Clone, Copy)]
pub(crate) struct Wish {
to: [f32; 2],
yields: bool,
}
impl Wish {
/// Where the box goes along `axis` for this wish, the person having scrolled it `by`
/// meanwhile: nothing where the wish leaves that axis.
pub(crate) fn to(&self, axis: usize, by: f32) -> Option<f32> {
(!self.to[axis].is_nan() && !(self.yields && by != 0.0)).then_some(self.to[axis])
}
}
/// A box that stays at its end ([`Ui::stay_at_end`]): whether the last run left it there, and
/// whether this run asked again.
struct End {
id: Id,
at: bool,
asked: bool,
}
/// A long list of rows of varying height as this run describes it: the box holding its rows,
/// the key of its kept heights, the first visible row and whether it starts above the view (and
/// the box scrolling the list, to keep the view still when that row's height changes).
#[derive(Clone, Copy)]
pub(crate) struct Uneven {
rows: u32,
id: Id,
first: usize,
cut: bool,
within: u32,
/// The height assumed for a row never laid out.
about: f32,
}
/// What the app asked of scroll boxes and of the next text box, until done; and what is kept of
/// boxes staying at their end and of long lists with rows of varying height.
#[derive(Default)]
pub(crate) struct Asks {
/// Where boxes are to scroll, by id.
pub wishes: Vec<(Id, Wish)>,
/// The box to bring into view; 0 for none.
pub into_view: Id,
/// The row the next long list is to show, kept for the run (rows measured meanwhile may
/// move it out of view again); and whether a list took it in this pass.
row: Option<usize>,
row_taken: bool,
ends: Vec<End>,
/// The long lists of varying row height described in this pass; and each row's last
/// laid-out height (0 until laid out), by list id.
pub lists: Vec<Uneven>,
heights: ById<Vec<f32>>,
/// The next multi-line text box leaves Enter to the app ([`leave_enter`](Ui::leave_enter)).
pub enter_left: bool,
/// Code set by the first call needing it, so a program that never asks carries none of it
/// (as `paint::Shows` does for drawings): after layout, measuring rows of varying height
/// ([`uneven_rows`](Ui::uneven_rows)); before scrolling, bringing a box into view
/// ([`scroll_into_view`](Ui::scroll_into_view)); after scrolling, noting whether a box
/// staying at its end is there ([`stay_at_end`](Ui::stay_at_end)); and at the end of a run,
/// forgetting what was not asked (both).
pub measured: Option<fn(&mut Ui)>,
pub bring: Option<fn(&mut Ui)>,
pub ended: Option<fn(&mut Ui, u32, Id, f32)>,
forget: Option<fn(&mut Asks)>,
/// Which boxes are spell checked, what is known of their words, and the code for it:
/// nothing until a box asks ([`check_spelling`](Ui::check_spelling)).
pub spelling: crate::spell::Spelling,
}
impl Asks {
/// Where box `id` was asked to scroll, if it was: the wish is then done.
pub(crate) fn take_wish(&mut self, id: Id) -> Option<Wish> {
let at = self.wishes.iter().position(|(asked, _)| *asked == id)?;
Some(self.wishes.swap_remove(at).1)
}
/// As the app starts describing the window again.
pub(crate) fn begin(&mut self) {
self.lists.clear();
self.row_taken = false;
}
/// At the end of a run: requests for boxes not described are dropped; a box no longer
/// asking to stay at its end is forgotten, as are the heights of a long list not described.
pub(crate) fn settled(&mut self) {
(self.into_view, self.row) = (0, None);
self.wishes.clear();
if let Some(forget) = self.forget {
forget(self);
}
}
/// What `settled` forgets of boxes staying at their end, and of long lists.
fn forget(&mut self) {
self.ends.retain_mut(|end| std::mem::take(&mut end.asked));
let lists = &self.lists;
self.heights.retain(|id, _| lists.iter().any(|list| list.id == *id));
}
}
/// Scrolling: to a place, to a box, kept at the end; and lists of rows of varying height.
impl Ui {
/// How far `of`'s box (a scroll box described this run) is scrolled and can scroll, as of
/// the last layout. Zero for a box that does not scroll.
pub fn scrolled(&self, of: &Response) -> Scrolled {
let Some(at) = self.box_of(of.id).filter(|at| self.tree.places.len() > *at as usize) else { return Scrolled::default() };
let most = [0, 1].map(|axis| if self.tree.scrolls(at, axis) { self.tree.scroll_end(at, axis) } else { 0.0 });
Scrolled { by: self.tree.specs[at as usize].offset, most }
}
/// Scrolls `of`'s box (a scroll box) to `to`: across and down, in units from its start, as
/// [`Scrolled::by`] gives them. `f32::INFINITY` goes to that axis's end, `f32::NAN` leaves
/// it: `[f32::NAN, 0.0]` goes back to the top. Done before anything is drawn.
pub fn scroll_to(&mut self, of: &Response, to: [f32; 2]) {
self.asks.wishes.push((of.id, Wish { to, yields: false }));
}
/// Keeps `of`'s box (scrolling vertically) at its end while it is there, as its content
/// grows: a chat, a log, a program's output. It starts at its end. Scrolled back by the
/// person, it stays where put as more comes; scrolled to the end again, it stays there
/// again ([`ui.scroll_to(&box, [f32::NAN, f32::INFINITY])`](Ui::scroll_to) takes it there).
/// Say it every run, as long as the box should do so.
pub fn stay_at_end(&mut self, of: &Response) {
let end = match self.asks.ends.iter_mut().find(|end| end.id == of.id) {
Some(end) => end,
None => {
self.asks.ends.push(End { id: of.id, at: true, asked: false });
self.asks.ends.last_mut().expect("just put in")
}
};
end.asked = true;
if end.at {
self.asks.wishes.push((of.id, Wish { to: [f32::NAN, f32::INFINITY], yields: true }));
}
self.asks.ended = Some(Ui::note_end);
self.asks.forget = Some(Asks::forget);
}
/// After a box that may stay at its end (`at`, id `id`) scrolled to `offset`: whether it is
/// at its end now, for the next run.
fn note_end(&mut self, at: u32, id: Id, offset: f32) {
let end = self.tree.scroll_end(at, 1);
self.asks.ends.iter_mut().filter(|end| end.id == id).for_each(|kept| kept.at = offset >= end - 0.5);
}
/// Scrolls the boxes around box `id` so it shows (its start, if it is larger than the
/// view). The box must be described this run; it comes into view before anything is drawn.
/// (A long list's row out of view is not described: use [`show_row`](Self::show_row).)
pub fn scroll_into_view(&mut self, id: Id) {
self.asks.into_view = id;
self.asks.bring = Some(Ui::bring_into_view);
}
/// Brings the requested box into view once described: see `show`.
fn bring_into_view(&mut self) {
let id = self.asks.into_view;
let Some(at) = self.tree.specs.iter().position(|spec| spec.id == id).filter(|_| id != 0) else { return };
self.asks.into_view = 0;
self.show(at as u32, self.tree.rect_of(at as u32));
}
/// Has the next long list (`rows`, `list`, [`uneven_rows`](Self::uneven_rows),
/// [`uneven_list`](Self::uneven_list)) bring row `row` into view: a search hit, a new item.
pub fn show_row(&mut self, row: usize) {
self.asks.row = Some(row);
}
/// The row a long list should bring into view, if one was asked for and no earlier list
/// took it this pass.
pub(crate) fn row_asked(&mut self) -> Option<usize> {
let asked = self.asks.row.filter(|_| !self.asks.row_taken);
self.asks.row_taken |= asked.is_some();
asked
}
/// Scrolls box `within`, which shows `room` of a long list from `above` down, so the
/// stretch from `top` that is `high` tall shows. Gives back the list's new top in view.
/// (Rows out of view are not described, so the list is laid out for this at once and
/// described again before drawing.)
pub(crate) fn list_into_view(&mut self, within: u32, above: f64, room: f32, top: f64, high: f64) -> f64 {
let to = match () {
_ if top < above => top,
_ if top + high > above + room as f64 => (top + high - room as f64).min(top),
_ => return above,
};
let spec = &mut self.tree.specs[within as usize];
spec.offset[1] = (spec.offset[1] as f64 + to - above).max(0.0) as f32;
self.scrolls.insert(spec.id, spec.offset);
self.again = true;
to
}
/// The room inside the open box: its size less its padding (across, also less its scroll
/// bar's track; down, no more than the window shows when no ancestor scrolls vertically).
/// Its content can be fitted to it: a picture as large as fits, as many items as fit, a
/// chart as wide as the box. It is this run's layout: where that differs from the last
/// run's, the window is described again before drawing. (Before the box's first layout, it
/// is the window's size.)
pub fn room(&mut self) -> Size {
let Some(open) = self.open.last() else { return self.size() };
let (at, id) = (open.at, hash((self.tree.specs[open.at as usize].id, ROOM)));
// A box never asked before is laid out in this pass, to be sure.
let read = match self.rooms.get(&id) {
Some(read) => *read,
None => {
self.stale = true;
[self.size().w, self.size().h, 0.0]
}
};
self.measures.push(Measure { at, within: at, id, read });
Size { w: read[0], h: read[1] }
}
/// How far the wheel turned over `of`'s box since the last run, across and down, in units
/// (as scroll boxes take it): for the app's own drawing, a map, a picture. It is consumed,
/// so nothing scrolls by it; nothing where it did not turn there (or where a scroll box
/// inside, under the pointer, takes it). (Control+wheel zooms: [`ui.zoom`](Ui::zoom).)
pub fn wheel(&mut self, of: &Response) -> Option<[f32; 2]> {
let (wheel, pointer) = (self.input.wheel, self.input.pointer?);
if !of.over || wheel == [0.0; 2] {
return None;
}
let r = of.rect;
let inside = |s: &crate::survey::Scroller| s.rect.x >= r.x && s.rect.y >= r.y && s.rect.right() <= r.right() && s.rect.bottom() <= r.bottom() && s.id != of.id;
if self.survey.scrollers.iter().any(|s| inside(s) && s.seen.contains(pointer) && (self.tree.overflows(s.at, 0) || self.tree.overflows(s.at, 1))) {
return None;
}
self.input.wheel = [0.0; 2];
self.again = true;
Some(wheel)
}
/// Starts `count` rows of a long list, each as tall as its content (wrapping messages, log
/// lines), inside a scroll box, and gives back the indices of those in view. Describe just
/// those, one box each, then [`close`](Self::close) the rows. A row not laid out yet is
/// assumed the others' average height (`about` before any was); after that the list keeps
/// its measured height and remeasures it whenever it shows. 100,000 rows cost what a
/// screenful does, plus a pass over their heights. (Where a row starting above the view
/// changes height, the view is kept still.)
#[cfg_attr(debug_assertions, track_caller)]
pub fn uneven_rows(&mut self, count: usize, about: f32) -> Range<usize> {
let (id, page) = (self.next_id(), self.open.first().map_or(0, |open| open.at));
// The nearest enclosing box scrolling vertically: what it shows is in view.
let within = self.open.iter().rev().map(|open| open.at).find(|at| self.tree.specs[*at as usize].frame.scroll[1]).unwrap_or(page);
let read = self.rooms.get(&id).copied().unwrap_or([0.0, self.size().h, 0.0]);
let mut heights = std::mem::take(self.asks.heights.entry(id).or_default());
heights.resize(count, 0.0);
// A row not laid out yet is assumed the average height.
let (known, sum) = heights.iter().filter(|high| **high > 0.0).fold((0, 0.0), |(known, sum), high| (known + 1, sum + *high as f64));
let about = if known > 0 { sum / known as f64 } else { about.max(1.0) as f64 };
let tall = |row: usize| if heights[row] > 0.0 { heights[row] as f64 } else { about };
let mut above = self.tree.specs[within as usize].offset[1] as f64 - read[2] as f64;
if let Some(row) = self.row_asked().filter(|row| *row < count) {
let top: f64 = (0..row).map(tall).sum();
above = self.list_into_view(within, above, read[1], top, tall(row));
}
// The first visible row and its top; then the rows down to the view's end.
let (mut first, mut top) = (0, 0.0);
while first < count && top + tall(first) <= above {
(first, top) = (first + 1, top + tall(first));
}
let (mut end, mut bottom) = (first, top);
while end < count && bottom < above + read[1] as f64 {
(end, bottom) = (end + 1, bottom + tall(end));
}
let total = bottom + (end..count).map(tall).sum::<f64>();
self.asks.heights.insert(id, heights);
// A box as tall as all rows, so the scroll bar is right; in it, empty room for the rows
// above the visible ones, then those.
let rows = self.tree.specs.len() as u32;
// Where the whole list shows, where it starts in its scroll box does not matter: only
// that box's room is measured. A few rows at the bottom of their box then move down as
// one is added, without describing again.
let fits = first == 0 && end == count && bottom <= read[1] as f64;
self.measures.push(Measure { at: if fits { within } else { rows }, within, id, read });
self.column().h(Length::Px(total as f32)).open();
self.el().h(Length::Px(top as f32)).show();
// Rows count their places from the first row overall, so a row keeps its identity
// wherever the list is scrolled.
if let Some(open) = self.open.last_mut() {
open.children += first as u32;
}
self.asks.lists.push(Uneven { rows, id, first, cut: top < above, within, about: about as f32 });
self.asks.measured = Some(Ui::measure_rows);
self.asks.forget = Some(Asks::forget);
first..end
}
/// Starts a long list of rows of varying height in a scroll box of its own:
/// [`uneven_rows`](Self::uneven_rows) and the box around them. Describe the rows it gives
/// back, then [`close`](Self::close) it.
#[cfg_attr(debug_assertions, track_caller)]
pub fn uneven_list(&mut self, count: usize, about: f32) -> Range<usize> {
self.column().grow().scroll().open();
self.join_next = true;
self.uneven_rows(count, about)
}
/// After layout: each visible row's height in lists of varying row height. Where any
/// differs from what the list had, the window is described again; and where the row
/// starting above the view changed, the list's scroll box scrolls by as much, so the view
/// stays still.
fn measure_rows(&mut self) {
for at in 0..self.asks.lists.len() {
let (grew, Uneven { cut, within, .. }) = (self.measure_list(at), self.asks.lists[at]);
if cut && grew != 0.0 && within != NONE {
let spec = &mut self.tree.specs[within as usize];
spec.offset[1] = (spec.offset[1] + grew).max(0.0);
self.scrolls.insert(spec.id, spec.offset);
}
}
}
/// The heights of the visible rows of this pass's list `at`, kept with the list. Gives back
/// how much taller its first visible row came out than the list had it.
fn measure_list(&mut self, at: usize) -> f32 {
let Ui { tree, asks, again, .. } = self;
let list = &asks.lists[at];
let Some(heights) = asks.heights.get_mut(&list.id) else { return 0.0 };
let mut first = 0.0;
for (nth, child) in tree.children(list.rows).skip(1).enumerate() {
let (high, row) = (tree.places[child as usize].dim[1], list.first + nth);
let Some(kept) = heights.get_mut(row).filter(|kept| (**kept - high).abs() >= 0.5) else { continue };
// Compared against its assumed height: a row laid out for the first time at the
// average height describes nothing again.
let was = if *kept > 0.0 { *kept } else { list.about };
first = if nth == 0 { high - was } else { first };
*again |= (was - high).abs() >= 0.5;
*kept = high;
}
first
}
/// Where box `id` is in this run's box list: an open one at once, any other by searching.
pub(crate) fn box_of(&self, id: Id) -> Option<u32> {
if let Some(open) = self.open.iter().rev().find(|open| self.tree.specs[open.at as usize].id == id) {
return Some(open.at);
}
self.tree.specs.iter().rposition(|spec| spec.id == id).map(|at| at as u32)
}
}