#1090 (emo172) asked for back-to-top and back-to-bottom buttons on the main pages. Once a page is scrolled more than one window down, a small capsule with two buttons, Scroll to top and Scroll to bottom, fades in. It sits in the page's right margin when a wide window leaves one, and otherwise over the page's corner, above Providers' sticky Add buttons. It stays hidden near the top of the page, while a dialog is open, and (over the corner) at the page's foot, where the last row can't scroll out from under it, unless it has the pointer or keyboard focus. The page's own height never changes for it, because app.js's scroll guard measures that height. A press goes through scrollOnPurpose, so the window's scroll guard takes it as the reader's own scroll. It scrolls smoothly, or at once under reduced motion, and the button at the end it already reached is aria-disabled. Both buttons are real buttons, reachable by Tab, with a label and tooltip in en, zh, zh-TW, ja and de. They are drawn from the window's tokens in both themes, with no gradient and no coloured edge. scrollends.js is imported on the window's first scroll, so the core scripts carry one line in app.js, paid for by two comments cut from the scroll guard's. No budget was raised. old-webkit lists scrollends.js among the lazy scripts. docs/subsystems/gui-shell.md says where the capsule sits and when it shows. Run on mote at this head (on2c90922b0, with the next commit): - view-ends.test.cjs, 32 of 32: Chromium and WebKit, light and dark, 420 and 1180 wide, every language. It checks that the capsule shows only past one window, that Scroll to bottom reaches the foot and stays there, and that the page is never taller under the capsule. It checks that the capsule is beside every row, or steps aside at the foot, and that Tab plus Enter on Scroll to top reaches 0 and stays. - click-scroll, i18n-keys and old-webkit: ok. - go test -run Budget ./internal/gui, gofmt -l, and go vet with GOOS=linux -tags nogui and with GOOS=windows: ok. - The whole GUI suite at the pre-rebase head (5f0a84a00+ this): 18,675 tests, 18,370 pass, 305 fail in 51 files. All 51 files were run again at5f0a84a00without this change, and 48 were red there too (297 fails). - The other three, import-row, provider-detect-models and routing-many-keys, and every test red at the head but not at the base, were run again alone at the head. They passed, or failed in a different engine or language than the first run, in the same tests that were red at the base. These are known load reds: #4389, #3488, #4585, #3415, #4127, #4126, #4244, #4082, #4619, #4431 and #4035. - old-webkit's red ("profilerename.js is not loaded by index.html") is on main since1cb4d4301and is fixed in the next commit. - go test ./internal/gui: red only at TestMainWindowCorners on mote's macOS 27 (#3949), the same as on main. Broken by hand: - At the old code, view-ends is red: the capsule's ::after room made the page taller ("the page grew under the capsule"). That room had also turned add-button, library-discover-jump and account-eye red in the whole suite, which is why the capsule steps aside instead. - Without the step-aside at the foot, view-ends is red. - Without the import hook in app.js, every case is red. - Without scrollOnPurpose, "the page stays at its foot" is red, because the guard puts the scroll back. Not tried: the real Wails app on macOS, Windows and Linux (WebKitGTK), and the tray panel, which has no long page to show it on.
75 lines
4.0 KiB
JavaScript
75 lines
4.0 KiB
JavaScript
// Scroll to top and Scroll to bottom (#1090, emo172): a small capsule at the
|
|
// lower right of the open page, shown once the page is scrolled a screen
|
|
// down and gone again near its top, like a long page's in Safari. app.js
|
|
// imports this on the window's first scroll, so it weighs nothing on the
|
|
// core. A press is the reader's: it goes through scrollOnPurpose, and
|
|
// arrived() keeps the page where it ended. Home and End still scroll the
|
|
// page as they always did; the buttons are for a mouse, and Tab reaches
|
|
// them too. The tray panel has none.
|
|
(() => {
|
|
if (document.body.classList.contains("panel")) return;
|
|
const box = el("div", "view-ends");
|
|
const up = el("button", "ve-up"), down = el("button", "ve-down");
|
|
up.type = down.type = "button";
|
|
up.append(svg("m4.5 9.5 3.5-3.5 3.5 3.5", 14, 1.7));
|
|
down.append(svg("m4.5 6.5 3.5 3.5 3.5-3.5", 14, 1.7));
|
|
box.append(up, el("span", "ve-sep"), down);
|
|
document.body.append(box);
|
|
let lang = null;
|
|
const open = () => [...document.querySelectorAll(".view")].find((v) => !v.hidden);
|
|
const atEnd = (v) => v.scrollTop + v.clientHeight >= v.scrollHeight - 2;
|
|
function paint() {
|
|
if (lang !== locale) {
|
|
lang = locale;
|
|
for (const [b, s] of [[up, "Scroll to top"], [down, "Scroll to bottom"]]) b.title = t(s), b.setAttribute("aria-label", t(s));
|
|
}
|
|
const v = open(), modal = document.getElementById("modal");
|
|
const held = box.contains(document.activeElement) || box.matches(":hover");
|
|
let on = !!v && (!modal || modal.hidden) && v.scrollTop > v.clientHeight;
|
|
if (!v || !on && !held) { box.classList.remove("on"); return; }
|
|
const r = v.getBoundingClientRect(), bar = v.offsetWidth - v.clientWidth, pad = parseFloat(getComputedStyle(v).paddingRight) || 0;
|
|
// in the page's right margin when a wide window leaves one; else over the
|
|
// page's corner, and out of the way at the page's foot, where its last
|
|
// row can't be scrolled out from under it. The page's own height is
|
|
// never changed for it: app.js's "where the reader is" measures it.
|
|
const gutter = pad >= box.offsetWidth + 16;
|
|
if (!gutter && v.scrollHeight - v.clientHeight - v.scrollTop < box.offsetHeight + 24) on = false;
|
|
box.classList.toggle("on", on || held);
|
|
let bottom = innerHeight - r.bottom + 16;
|
|
// above a page's own sticky foot (Providers' Add buttons)
|
|
const foot = v.querySelector(":scope > .after-list:not(.away)");
|
|
if (foot && getComputedStyle(foot).position === "sticky") bottom = Math.max(bottom, innerHeight - foot.getBoundingClientRect().top + 8);
|
|
box.style.right = Math.max(8, innerWidth - r.right + bar + (gutter ? (pad - box.offsetWidth) / 2 : 10)) + "px";
|
|
box.style.bottom = bottom + "px";
|
|
up.setAttribute("aria-disabled", v.scrollTop < 1);
|
|
down.setAttribute("aria-disabled", atEnd(v));
|
|
}
|
|
let queued = false;
|
|
const later = () => { if (!queued) { queued = true; requestAnimationFrame(() => { queued = false; paint(); }); } };
|
|
addEventListener("scroll", later, { capture: true, passive: true });
|
|
addEventListener("resize", later);
|
|
box.addEventListener("focusout", later);
|
|
box.addEventListener("pointerleave", later);
|
|
new MutationObserver(later).observe(document.body, { subtree: true, attributes: true, attributeFilter: ["hidden"] });
|
|
function go(e, top) {
|
|
const v = open();
|
|
if (!v || e.currentTarget.getAttribute("aria-disabled") === "true") return;
|
|
if (!scrollOnPurpose(e, 2500)) return;
|
|
const to = top ? 0 : v.scrollHeight - v.clientHeight;
|
|
v.scrollTo({ top: to, behavior: calm() ? "auto" : "smooth" });
|
|
// done when it has stood still a few frames (WebKit has no scrollend)
|
|
let last = -1, still = 0;
|
|
const until = performance.now() + 2400;
|
|
const settle = () => {
|
|
if (v.scrollTop === last) still++; else still = 0, last = v.scrollTop;
|
|
if (still < 3 && performance.now() < until) return requestAnimationFrame(settle);
|
|
arrived(v);
|
|
paint();
|
|
};
|
|
requestAnimationFrame(settle);
|
|
}
|
|
up.onclick = (e) => go(e, true);
|
|
down.onclick = (e) => go(e, false);
|
|
paint();
|
|
})();
|