Skip to content

Commit 6945b77

Browse files
author
i2cjak
authored
Merge pull request #606 from BackplaneWorks/backplane/scroll-valid-range-2
Timeline scrolling: no document-wide non-passive wheel listener; a scroll inside the plan's valid range keeps the plan
2 parents 36e6a10 + 8437afe commit 6945b77

8 files changed

Lines changed: 213 additions & 15 deletions

File tree

‎LAWS.bend‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14638,3 +14638,19 @@ law view_page_splice_default_off:
1463814638
law view_page_splice_setting_on:
1463914639
{CHATSIDE.Memo.page.on(M.State.setting(M.State.empty(), "view.splice.page", "on")) == True{} : Bool}
1464014640
# END page splice (H)
14641+
14642+
# The timeline's valid range (timeline-virtual.bend's Valid.range): at the
14643+
# end it is empty, so every scroll there plans (follow and atEnd stay
14644+
# exact); a row that does not take part keeps an edge as it was
14645+
law timeline_valid_end_empty:
14646+
for e: TLVIRTUAL.Edges
14647+
for +ov: TLVIRTUAL.Px
14648+
for +view: TLVIRTUAL.Px
14649+
for +total: TLVIRTUAL.Px
14650+
{TLVIRTUAL.Valid.range(True{}, e, ov, view, total) == TLVIRTUAL.Span{TLVIRTUAL.Px.int(1n), TLVIRTUAL.Px.zero()} : TLVIRTUAL.Span}
14651+
14652+
law timeline_valid_edge_untaken:
14653+
for m: Maybe<&2, TLVIRTUAL.Px>
14654+
for +x: TLVIRTUAL.Px
14655+
for max: Bool
14656+
{TLVIRTUAL.Edge.pick(False{}, m, x, max) == m : Maybe<&2, TLVIRTUAL.Px>}

‎PROOF.bend‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ import ./test/sidebar_full_branch_test.bend as FULLBRANCH
251251
import ./test/sidebar_draft_image_test.bend as SIDEDRAFTPROOF
252252

253253
import ./test/secret_proof.bend as SECPROOF
254+
254255
import ./src/core/landing.bend as LAND
255256

256257
# Lemmas
@@ -14892,14 +14893,30 @@ def Laws.land_timeline_only():
1489214893

1489314894
# BEGIN page splice (H)
1489414895
def Laws.view_memo_key_page_last(seq, info, p, anchor, view, web, thread): {==}
14896+
1489514897
def Laws.view_page_memo_same_step(): {==}
14898+
1489614899
def Laws.view_page_memo_next_step(): {==}
14900+
1489714901
def Laws.view_page_memo_other_inputs(): {==}
14902+
1489814903
def Laws.view_page_memo_not_fitted(): {==}
14904+
1489914905
def Laws.view_page_still_same_step(): {==}
14906+
1490014907
def Laws.view_page_still_next_step(): {==}
14908+
1490114909
def Laws.view_page_still_not_fitted(): {==}
14910+
1490214911
def Laws.view_page_splice_off_none(ms, base, clock, now): {==}
14912+
1490314913
def Laws.view_page_splice_default_off(): {==}
14914+
1490414915
def Laws.view_page_splice_setting_on(): {==}
1490514916
# END page splice (H)
14917+
14918+
def Laws.timeline_valid_end_empty(e, ov, view, total):
14919+
{==}
14920+
14921+
def Laws.timeline_valid_edge_untaken(m, x, max):
14922+
{==}

‎src/core/timeline-virtual.bend‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,76 @@ def Items.json(xs: List<&2, Item>, acc: List<&2, J.Json>) -> J.Json:
431431
case Nil{}: J.Json.arr(List.reverse(&2, J.Json, acc))
432432
case r <> t: Items.json(t, Item.json(r) <> acc)
433433

434-
def Plan.output(+items: List<&2, Item>, +total: Px, +top: Px, +view: Px, preferred: Anchor, +trackOffset: Px) -> J.Json:
434+
# The valid range: the tops (logical) over which a scroll plan would mount
435+
# the same rows and say the same atEnd. The mounted rows are those
436+
# overlapping [top - ov, top + view + ov]; that set changes only when an
437+
# edge crosses a row's start or end, so the range runs from the nearest
438+
# such crossing below to the nearest above (1 px in from each, where a row
439+
# joins at equality), within [0, the first top that is atEnd). The adapter
440+
# skips the plan for a scroll inside it (law timeline_valid_*).
441+
type Edges is Data:
442+
Edges{endBelow: Maybe<&2, Px>, startIn: Maybe<&2, Px>, endIn: Maybe<&2, Px>, startAbove: Maybe<&2, Px>}
443+
444+
def Edge.max(m: Maybe<&2, Px>, +x: Px) -> Maybe<&2, Px>:
445+
match m:
446+
case None{}: Some{x}
447+
case Some{y}: Some{Px.max(x, y)}
448+
449+
def Edge.min(m: Maybe<&2, Px>, +x: Px) -> Maybe<&2, Px>:
450+
match m:
451+
case None{}: Some{x}
452+
case Some{y}: Some{Px.min(x, y)}
453+
454+
def Edge.pick(take: Bool, m: Maybe<&2, Px>, +x: Px, max: Bool) -> Maybe<&2, Px>:
455+
match take max:
456+
case False{} _: m
457+
case True{} True{}: Edge.max(m, x)
458+
case True{} False{}: Edge.min(m, x)
459+
460+
# one row's start and end against lo and hi
461+
def Edges.step(e: Edges, +start: Px, +end: Px, +lo: Px, +hi: Px) -> Edges:
462+
match e:
463+
case Edges{eb, si, ei, sa}:
464+
Edges{Edge.pick(Bool.not(Px.le(lo, end)), eb, end, True{}), Edge.pick(Px.le(start, hi), si, start, True{}),
465+
Edge.pick(Px.le(lo, end), ei, end, False{}), Edge.pick(Bool.not(Px.le(start, hi)), sa, start, False{})}
466+
467+
def Edges.scan(xs: List<&2, Item>, +lo: Px, +hi: Px, e: Edges) -> Edges:
468+
match xs:
469+
case Nil{}: e
470+
case +r <> t: Edges.scan(t, lo, hi, Edges.step(e, Item.start(r), Item.end(r), lo, hi))
471+
472+
def Valid.from(m: Maybe<&2, Px>, +shift: Px, +d: Px) -> Px:
473+
match m:
474+
case None{}: d
475+
case Some{x}: Px.add(x, shift)
476+
477+
type Span is Data:
478+
Span{lo: Px, hi: Px}
479+
480+
def Valid.range(atEnd: Bool, e: Edges, +ov: Px, +view: Px, +total: Px) -> Span:
481+
match atEnd e:
482+
case True{} _: Span{Px.int(1n), Px.zero()}
483+
case False{} Edges{eb, si, ei, sa}:
484+
+far = Px.sub(total, Px.add(view, Px.int(5n)))
485+
+down = Px.neg(Px.add(view, ov))
486+
Span{Px.max(Px.zero(), Px.max(Valid.from(eb, Px.add(ov, Px.int(1n)), Px.zero()), Valid.from(si, down, Px.zero()))),
487+
Px.min(far, Px.min(Valid.from(ei, ov, far), Valid.from(sa, Px.sub(down, Px.int(1n)), far)))}
488+
489+
def Valid.json(r: Span, +trackOffset: Px) -> J.Json:
490+
match r:
491+
case Span{lo, hi}: J.Json.arr([Px.json(Px.add(lo, trackOffset)), Px.json(Px.add(hi, trackOffset))])
492+
493+
def Plan.output(+items: List<&2, Item>, +total: Px, +top: Px, +view: Px, preferred: Anchor, +trackOffset: Px, +extra: Px) -> J.Json:
435494
+a = Anchor.prefer(Items.find(items, Anchor.key(preferred)), top, view,
436495
Anchor.capture(items, top, Anchor{"", Px.zero()}))
437-
+overscan = Px.add(view, Px.int(720n))
496+
+overscan = Px.add(Px.add(view, Px.int(720n)), Px.max(Px.zero(), extra))
438497
+visible = Window.visible(items, Anchor.key(a), Px.sub(top, overscan), Px.add(Px.add(top, view), overscan))
439498
J.Json.obj([J.KV{"items", Items.json(items, Nil{})}, J.KV{"visible", Items.json(visible, Nil{})},
440499
J.KV{"total", Px.json(total)}, J.KV{"top", Px.json(Px.add(top, trackOffset))}, J.KV{"view", Px.json(view)},
441500
J.KV{"physicalTotal", Px.json(Px.ceil(Px.add(Px.max(total, Px.add(top, view)), trackOffset)))},
442501
J.KV{"logicalTop", Px.json(top)}, J.KV{"trackOffset", Px.json(trackOffset)},
443-
J.KV{"atEnd", J.Flag{End.at(total, top, view)}}, J.KV{"anchor", J.Str{Anchor.key(a)}}, J.KV{"offset", Px.json(Anchor.offset(a))}])
502+
J.KV{"atEnd", J.Flag{End.at(total, top, view)}}, J.KV{"anchor", J.Str{Anchor.key(a)}}, J.KV{"offset", Px.json(Anchor.offset(a))},
503+
J.KV{"valid", Valid.json(Valid.range(End.at(total, top, view), Edges.scan(items, Px.sub(top, overscan), Px.add(Px.add(top, view), overscan), Edges{None{}, None{}, None{}, None{}}), overscan, view, total), trackOffset)}])
444504

445505
def Plan.layout(layout: Layout, +input: J.Json) -> J.Json:
446506
match layout:
@@ -452,7 +512,7 @@ def Plan.layout(layout: Layout, +input: J.Json) -> J.Json:
452512
+inputTop = Px.sub(Px.get(input, "top", "0"), oldOffset)
453513
+a = Plan.preferred(mode, input, inputTop, view)
454514
+top = Plan.top(mode, input, items, total, inputTop, view, previous, a)
455-
Plan.output(items, total, top, view, a, Bool.pick(Px, String.eq(mode, "init"), Px.zero(), oldOffset))
515+
Plan.output(items, total, top, view, a, Bool.pick(Px, String.eq(mode, "init"), Px.zero(), oldOffset), Px.get(input, "overscan", "0"))
456516

457517
# A readback corrects only the physical effect of a rounded/clamped scroll
458518
# write. It uses the previous logical geometry without following or selecting
@@ -468,7 +528,7 @@ def Plan.readback(+input: J.Json) -> J.Json:
468528
+residual = Readback.offset(J.Json.get_nat(input, "readbackAttempt", 1n), Px.add(oldOffset, Px.sub(Px.get(input, "top", "0"), requested)))
469529
Plan.output(Items.read(J.Json.elems(J.Json.get_obj(previous, "items")), Nil{}),
470530
Px.get(previous, "total", "0"), logicalTop, Px.get(previous, "view", "0"),
471-
Anchor{J.Json.get_str(previous, "anchor", ""), Px.get(previous, "offset", "0")}, residual)
531+
Anchor{J.Json.get_str(previous, "anchor", ""), Px.get(previous, "offset", "0")}, residual, Px.get(input, "overscan", "0"))
472532

473533
def Plan.mode(readback: Bool, +input: J.Json) -> J.Json:
474534
match readback:

‎src/web/host.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ function setAttrs(el, oldAttrs, attrs) {
188188
} else if (el.getAttribute(a.name) !== a.value) {
189189
el.setAttribute(a.name, a.value);
190190
if (a.name === "data-cat") el.src = catUri(a.value);
191+
else if (a.name === "data-wheel") wheelHook(el);
191192
}
192193
} else {
193194
const name = "data-on-" + a.event;
@@ -1900,13 +1901,22 @@ document.addEventListener("click", (e) => {
19001901
frameFlush(el);
19011902
dispatch(el.getAttribute("data-pt"), th(e.clientX - r.left, r.width) + " " + th(e.clientY - r.top, r.height));
19021903
}, true);
1903-
document.addEventListener("wheel", (e) => {
1904-
const el = e.target.closest?.("[data-wheel]");
1905-
if (!el) return;
1906-
e.preventDefault();
1907-
frameFlush(el);
1908-
dispatch(el.getAttribute("data-wheel"), String(Math.round(Math.max(-600, Math.min(600, e.deltaY)))));
1909-
}, { passive: false });
1904+
// A [data-wheel] element (a browser frame, a terminal) takes the wheel
1905+
// itself. Its listener is its own (setAttrs hooks it), never the
1906+
// document's: a non-passive wheel listener on the document makes every
1907+
// wheel on the page wait for the main thread before it may scroll (the
1908+
// timeline's wheel→frame went from about 10 to 30-50 ms).
1909+
const wheelHooked = new WeakSet();
1910+
function wheelHook(el) {
1911+
if (wheelHooked.has(el)) return;
1912+
wheelHooked.add(el);
1913+
el.addEventListener("wheel", (e) => {
1914+
if (!el.hasAttribute("data-wheel") || e.target.closest?.("[data-wheel]") !== el) return;
1915+
e.preventDefault();
1916+
frameFlush(el);
1917+
dispatch(el.getAttribute("data-wheel"), String(Math.round(Math.max(-600, Math.min(600, e.deltaY)))));
1918+
}, { passive: false });
1919+
}
19101920
const FRAME_KEYS = { Enter: 1, Backspace: 1, Tab: 1, Escape: 1, Delete: 1, ArrowLeft: 1, ArrowRight: 1, ArrowUp: 1, ArrowDown: 1, Home: 1, End: 1, PageUp: 1, PageDown: 1 };
19111921
document.addEventListener("keydown", (e) => {
19121922
const el = e.target.closest?.("[data-keyto]");

‎src/web/timeline-scroller.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ export class TimelineScroller {
3030
this.track.className = 'timeline-virtual-track';
3131
Object.assign(this.track.style, { position: 'relative', width: '100%' });
3232
el.appendChild(this.track);
33-
this._onScroll = () => this._request('scroll');
33+
this._onScroll = () => this._scrolled();
34+
this._stale = false; // a scroll inside the plan's valid range left it as it was
35+
}
36+
37+
// A scroll inside the last plan's valid range (timeline-virtual.bend's
38+
// Valid.range: the tops over which a plan mounts the same rows and says
39+
// the same atEnd) keeps that plan: no plan, no render, no measure. Its
40+
// anchor and offset are then stale until the next plan (position() plans
41+
// first). The host still hears of the scroll (onChange).
42+
_scrolled() {
43+
const p = this.lastPlan, top = this.el.scrollTop;
44+
if (p?.valid && this._initialized && !this._pending && !this._busy && top >= p.valid[0] && top <= p.valid[1]) {
45+
RR?.n("tl.scroll.kept");
46+
this._stale = true;
47+
this.callbacks.onChange(this);
48+
return;
49+
}
50+
this._request('scroll');
3451
}
3552

3653
get followEnabled() { return this._follow; }
@@ -171,6 +188,7 @@ export class TimelineScroller {
171188
// Report the triggering operation, not a subsequent measurement pass.
172189
changeMode ??= request.mode;
173190
RR?.n("tl.plan." + request.mode);
191+
this._stale = false;
174192
const plan = this.callbacks.plan({
175193
keys: this._keys.slice(), sizes: { ...this.sizes }, estimate: this._estimate(),
176194
previous: this.lastPlan, top: this.el.scrollTop,
@@ -282,6 +300,7 @@ export class TimelineScroller {
282300
}
283301

284302
position() {
303+
if (this._stale && !this._destroyed) this._request('scroll');
285304
const p = this.lastPlan;
286305
return p ? { top: p.logicalTop ?? p.top, atEnd: p.atEnd, anchor: p.anchor, offset: p.offset }
287306
: { top: this.el.scrollTop, atEnd: true, anchor: null, offset: 0 };

‎test/timeline_virtual_test.bend‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ def view(request: J.Json, size: String) -> J.Json:
2424
def readback(previous: J.Json, top: String) -> J.Json:
2525
V.plan(J.Json.obj([J.KV{"mode", J.Str{"readback"}}, J.KV{"previous", previous}, J.KV{"top", J.Num{top}}]))
2626

27+
28+
# the valid range (law timeline_valid_*): a long list of 100 px rows
29+
def many.keys(+n: Nat, acc: List<&2, J.Json>) -> List<&2, J.Json>:
30+
match n:
31+
case 0n: acc
32+
case 1n+k: many.keys(k, J.Str{"r" ++ Nat.show(k)} <> acc)
33+
34+
def many.sizes(+n: Nat, acc: List<&2, J.KV>) -> List<&2, J.KV>:
35+
match n:
36+
case 0n: acc
37+
case 1n+k: many.sizes(k, J.KV{"r" ++ Nat.show(k), J.Num{"100"}} <> acc)
38+
39+
def many.plan(top: String, +previous: J.Json) -> J.Json:
40+
V.plan(J.Json.obj([J.KV{"keys", J.Json.arr(many.keys(200n, Nil{}))}, J.KV{"sizes", J.Json.obj(many.sizes(200n, Nil{}))}, J.KV{"previous", previous},
41+
J.KV{"top", J.Num{top}}, J.KV{"view", J.Num{"100"}}, J.KV{"mode", J.Str{"scroll"}}, J.KV{"follow", J.Flag{False{}}}]))
42+
43+
def keys.go(xs: List<&2, J.Json>, acc: String) -> String:
44+
match xs:
45+
case Nil{}: acc
46+
case j <> t: keys.go(t, acc ++ "," ++ J.Json.get_str(j, "key", ""))
47+
48+
# the rows a plan mounts, as one string
49+
def mounted(+p: J.Json) -> String:
50+
keys.go(J.Json.elems(J.Json.get_obj(p, "visible")), "")
51+
52+
def valid.at(+p: J.Json, i: Nat) -> String:
53+
V.Px.show(V.Px.read(J.Json.as_raw(List.head(&2, J.Json, List.drop(&2, J.Json, J.Json.elems(J.Json.get_obj(p, "valid")), i)), "0")))
54+
2755
def main() -> IO(Unit):
2856
do IO<Unit>:
2957
num("decimal beyond float32 precision", V.Px.add(V.Px.read("16777216.015625"), V.Px.read("0.015625")), V.Px.read("16777216.03125"))
@@ -113,3 +141,13 @@ def main() -> IO(Unit):
113141
field("zero height pill contributes no gap or track change", pill, "total", "104.125")
114142
C.Check.str("anchor forced into window", Bool.show(V.Window.keep(True{}, False{})), "True")
115143
C.Check.str("only bottom can follow", Bool.show(V.Follow.allowed(True{}, False{})), "False")
144+
+mid : J.Json = many.plan("5000", J.Null{})
145+
+lo : String = valid.at(mid, 0n)
146+
+hi : String = valid.at(mid, 1n)
147+
C.Check.str("valid range: low end mounts the same rows", mounted(many.plan(lo, mid)), mounted(mid))
148+
C.Check.str("valid range: high end mounts the same rows", mounted(many.plan(hi, mid)), mounted(mid))
149+
C.Check.str("valid range: a pixel below it mounts others", Bool.show(String.eq(mounted(many.plan(V.Px.show(V.Px.sub(V.Px.read(lo), V.Px.int(1n))), mid)), mounted(mid))), "False")
150+
C.Check.str("valid range: a pixel above it mounts others", Bool.show(String.eq(mounted(many.plan(V.Px.show(V.Px.add(V.Px.read(hi), V.Px.int(1n))), mid)), mounted(mid))), "False")
151+
C.Check.str("valid range holds the top it was planned at", Bool.show(Bool.and(V.Px.le(V.Px.read(lo), V.Px.read("5000")), V.Px.le(V.Px.read("5000"), V.Px.read(hi)))), "True")
152+
+bottom : J.Json = many.plan("21921", J.Null{})
153+
C.Check.str("at the end the range is empty (every scroll plans)", Bool.show(V.Px.le(V.Px.read(valid.at(bottom, 0n)), V.Px.read(valid.at(bottom, 1n)))), "False")

‎test/tools/rerender/instrument.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@
250250
const hub = !!window.__bpRender.ahead?.(up ? "older" : "newer");
251251
wheels.push({ at: e.timeStamp, key: viewKey(), ahead: room || hub, frames: 0, moved: null });
252252
}, { capture: true, passive: true });
253+
// a scroll event the browser dispatched after the wheel: the view moved
254+
// (scrolled on the compositor, the position read in a frame may lag; the
255+
// scroll event is the browser's own word that it moved)
256+
addEventListener("scroll", (e) => {
257+
if (!W.on || e.target?.id !== "timeline") return;
258+
const t = now();
259+
for (const w of wheels) if (w.moved === null && t >= w.at) w.moved = t - w.at;
260+
}, { capture: true, passive: true });
253261
function wheelFrames() {
254262
const k = viewKey(), t = now();
255263
for (const w of wheels) if (w.moved === null) { w.frames++; if (k !== w.key) w.moved = t - w.at; }

0 commit comments

Comments
 (0)