Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions electron/preload/frameScrollbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// (frameQueries.ts) and Studio's preview client: hides the page's own scrollbar once the app says so, reports the
// page's vertical scroll so the canvas can draw its own bar, and scrolls when that bar is dragged.
//
// Until the app's word arrives, the page keeps Chromium's scrollbar and every report says so (`chromium: true`),
// so the canvas never draws a second bar beside a frame that still shows its own. A frame with no app (the phone
// preview, a page opened on its own) is never told and keeps its scrollbar.
// A canvas frame (its URL carries #avb-design, PreviewFrame.tsx) hides the page's bar before its first paint, without
// waiting for the app's word: the word comes on the frame's `load`, after every picture and font, and the page would
// show Chromium's bar until then. Every canvas frame has the canvas's own bar beside it (SizedFrame.tsx,
// CanvasView.tsx), so the page is never left without one. Any other frame keeps Chromium's scrollbar until the app's
// word arrives, and every report says so (`chromium: true`), so the canvas never draws a second bar beside a frame that
// still shows its own. A frame with no app (the phone preview, a page opened on its own) is never told and keeps its
// scrollbar.
//
// The scrolling root is <html> (the document's scrolling element) on nearly every page. A page that holds <html> still
// and scrolls <body> instead (`html { overflow: hidden } body { overflow: auto; height: 100% }`) is read and scrolled
Expand All @@ -21,6 +25,8 @@ import {
} from '../../shared/properties/canvasScrollbar';

const STYLE_ID = 'avb-canvas-scrollbar';
/** The mark the app's canvas loads every page with (PreviewFrame.tsx), read the way markFrameMode reads it. */
const CANVAS_HASH_MARK = 'avb-design';
/** The `overflow-y` values under which a <body> taller than its box scrolls itself. */
const SCROLLING_OVERFLOWS: readonly string[] = ['auto', 'scroll', 'overlay'];

Expand All @@ -38,6 +44,7 @@ export function installPageScrollReport(send: (message: unknown) => void): void
return;
}
post = send;
hideCanvasPageScrollbarEarly();
// Capture on the document: a scrolling <body> fires `scroll` at itself, and element scroll events do not bubble.
document.addEventListener('scroll', scheduleReport, { capture: true, passive: true });
window.addEventListener('resize', scheduleReport);
Expand Down Expand Up @@ -71,6 +78,24 @@ function observeRootWhenReady(): void {
window.addEventListener('DOMContentLoaded', observe);
}

// A canvas frame hides the page's bar now, before the page is parsed or painted. The rule goes into the document's
// adopted style sheets: no element is added to the page, so nothing that watches its DOM (the column guide, the
// outlines, the canvas patcher) sees a change, and a document whose <html> does not exist yet takes it as well. The
// app's word on `load` still writes the <style> element, as for any frame; both hold the same rule.
function hideCanvasPageScrollbarEarly(): void {
if (!window.location.hash.includes(CANVAS_HASH_MARK)) {
return; // Not the canvas's frame: it waits for the app's word.
}
if (!('adoptedStyleSheets' in document)) {
return; // A document without constructable style sheets waits for the app's word too.
}
const sheet = new CSSStyleSheet();
sheet.replaceSync(CANVAS_SCROLLBAR_CSS);
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
told = true;
assert(document.adoptedStyleSheets.includes(sheet), 'hideCanvasPageScrollbarEarly: the canvas frame draws no bar of its own');
}

/** Applies an app message if it is one (hide the page's bar, or a scroll); false when it was something else. */
export function receiveCanvasScrollbar(input: unknown): boolean {
if (parseCanvasScrollbarMessage(input) !== undefined) {
Expand Down
27 changes: 17 additions & 10 deletions test/electron/preload/canvasScrollbarChromium.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
//
// node test/electron/preload/canvasScrollbarChromium.test.js
//
// Goal: a canvas frame the app has told lays its page out at the frame's full width at every breakpoint, as a phone or
// a tablet does (a 320px frame is 320px of page, not 305px beside a classic scrollbar; a 1440px frame is 1440px), and
// an older app's width threshold in the message no longer brings Chromium's bar back. A frame nobody told keeps the
// bar Chromium draws. Each frame reports which bar is drawn, and the canvas's own bar scrolls it.
// Goal: a canvas frame lays its page out at the frame's full width at every breakpoint, as a phone or a tablet does (a
// 320px frame is 320px of page, not 305px beside a classic scrollbar; a 1440px frame is 1440px), from before its first
// paint, not only once the app's word arrives on load; an older app's width threshold in the message no longer brings
// Chromium's bar back. A frame that is not the canvas's keeps the bar Chromium draws. Each frame reports which bar is
// drawn, and the canvas's own bar scrolls it.
//
// Method: a real Electron window (jsdom has no layout and draws no scrollbars) serves a host page over localhost with
// three canvas iframes at `#avb-design`, the built preload running in each as in the app: a 320px phone frame told to
// hide its bar, a 1440px desktop frame told the same with an older app's `chromiumFromPx: 1023` (now ignored), and a
// 1440px control frame that is told nothing. The pages are taller than their frames, so each scrolls. A computer that
// three iframes, the built preload running in each as in the app: a 320px phone frame and a 1440px desktop frame at
// `#avb-design`, as the canvas loads them, later told to hide the bar (the desktop one with an older app's
// `chromiumFromPx: 1023`, now ignored), and a 1440px control frame without the mark that is told nothing. The pages are taller than their frames, so each scrolls. A computer that
// draws overlay scrollbars (macOS with a trackpad) gives the control frame no bar at all; the checks compare a frame's
// layout width against its own viewport, so they hold on either kind of computer and still catch a frame that loses
// width to a bar. Every wait is a bounded poll for what the frames report (test/helpers/waitFor.js), never a fixed
Expand Down Expand Up @@ -40,7 +41,7 @@ const HOST_PAGE =
'<!doctype html><html><head><meta charset="utf-8"><style>body{margin:0}iframe{border:0;height:600px;display:block}</style></head><body>' +
'<iframe id="phone" style="width:320px" src="/frame.html#avb-design"></iframe>' +
'<iframe id="desktop" style="width:1440px" src="/frame.html#avb-design"></iframe>' +
'<iframe id="control" style="width:1440px" src="/frame.html#avb-design"></iframe>' +
'<iframe id="control" style="width:1440px" src="/frame.html"></iframe>' +
'<script>window.reports = {};' +
'addEventListener("message", (event) => {' +
' if (!event.data || event.data.type !== "avb:page-scroll") { return; }' +
Expand Down Expand Up @@ -103,7 +104,7 @@ const tell = (id, message) => `document.getElementById(${JSON.stringify(id)}).co
const run = (code) => win.webContents.executeJavaScript(code);
const read = () => run(${JSON.stringify(READ_FRAMES)});
await win.loadURL('http://127.0.0.1:${port}/host.html');
// Each frame reports once its preload runs: Chromium's bar until the app's word arrives.
// Each frame reports once its preload runs: a canvas frame has hidden its bar already, the control frame has not.
const before = await waitFor({ what: 'every frame reports', read, until: (frames) => frames.phone.report && frames.desktop.report && frames.control.report });
await run(${JSON.stringify(tell('phone', { type: 'avb:canvas-scrollbar' }))});
await run(${JSON.stringify(tell('desktop', { type: 'avb:canvas-scrollbar', chromiumFromPx: 1023 }))});
Expand Down Expand Up @@ -143,7 +144,13 @@ const tell = (id, message) => `document.getElementById(${JSON.stringify(id)}).co
} else {
const { before, told, scrolled } = JSON.parse(line);
const detail = (frame) => JSON.stringify(frame);
check('before the app’s word, every frame says Chromium draws its bar', before.phone.report.chromium && before.control.report.chromium, detail(before));
check(
'before the app’s word, a canvas frame has hidden its bar already and says so',
before.phone.report.chromium === false && before.desktop.report.chromium === false,
detail(before),
);
check('and the 320px phone frame lays its page out at 320px from the start', before.phone.clientWidth === 320, detail(before.phone));
check('a frame that is not the canvas’s says Chromium draws its bar', before.control.report.chromium === true, detail(before.control));
check('a 320px phone frame lays its page out at 320px', told.phone.innerWidth === 320 && told.phone.clientWidth === 320, detail(told.phone));
check('and says the canvas draws its bar', told.phone.report.chromium === false, detail(told.phone));
check(
Expand Down
71 changes: 66 additions & 5 deletions test/electron/preload/frameScrollbar.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Goal: the frame half of the canvas scrollbar (electron/preload/frameScrollbar.ts) hides the page's own scrollbar once
// the app says so, at every width (one behaviour, no setting), tells the truth about which bar is drawn (Chromium's
// until the app's word arrives), reports the page's scroll at most once per animation frame and only when it changed,
// the app says so, at every width (one behaviour, no setting), and in a canvas frame (#avb-design) before the page
// paints, through an adopted style sheet that adds nothing to the page; tells the truth about which bar is drawn
// (Chromium's until it is hidden), reports the page's scroll at most once per animation frame and only when it changed,
// follows a page that scrolls <body> instead of <html>, and scrolls when the canvas's bar asks.
// Method: the real module, bundled with esbuild, runs in a fresh jsdom window per test (its state is per page, as in a
// frame). jsdom has no layout, so the test owns the root's and the body's scroll position and heights; animation
Expand Down Expand Up @@ -29,10 +30,23 @@ function moduleSource() {
return bundle;
}

// A frame holding a page `heightPx` tall that scrolls <html>, the module loaded and reporting into `sent`.
function frame({ heightPx }) {
const dom = new JSDOM('<!doctype html><html><head></head><body></body></html>');
// A constructable style sheet as Chromium has one, keeping its rule as text. jsdom has neither these nor a document's
// adopted sheets, so a frame asked for them gets this stand-in; the real browser's is held by canvasScrollbarChromium.
class ConstructedSheet {
replaceSync(text) {
this.text = text;
}
}

// A frame holding a page `heightPx` tall that scrolls <html>, the module loaded and reporting into `sent`. `url` is the
// frame's address: the app's canvas loads every page with #avb-design. `adoptedSheets` gives the document Chromium's
// adopted style sheets.
function frame({ heightPx, url = 'http://localhost/', adoptedSheets = false }) {
const dom = new JSDOM('<!doctype html><html><head></head><body></body></html>', { url });
const window = dom.window;
if (adoptedSheets) {
Object.defineProperty(window.document, 'adoptedStyleSheets', { configurable: true, writable: true, value: [] });
}
const root = window.document.documentElement;
const page = { heightPx, topPx: 0 };
Object.defineProperty(root, 'scrollTop', { configurable: true, get: () => page.topPx });
Expand All @@ -49,6 +63,7 @@ function frame({ heightPx }) {
window,
document: window.document,
HTMLStyleElement: window.HTMLStyleElement,
CSSStyleSheet: ConstructedSheet,
requestAnimationFrame: (callback) => {
queued.push(callback);
return queued.length;
Expand Down Expand Up @@ -116,6 +131,52 @@ test('the app’s word hides the page’s own bar at every width, and the canvas
assert.equal(view.window.document.querySelectorAll('#avb-canvas-scrollbar').length, 1, 'one stylesheet, however often told');
});

test('a canvas frame hides the page’s bar before its first paint, without waiting for the app’s word', (context) => {
const view = frame({ heightPx: 4000, url: 'http://localhost/about/#avb-design', adoptedSheets: true });
context.after(view.close);
const childrenBefore = view.window.document.querySelectorAll('*').length;
view.install();
// Array.from: the module's array comes from its own context, so it is copied before a strict comparison.
assert.deepEqual(
Array.from(view.window.document.adoptedStyleSheets, (sheet) => sheet.text),
['html, body { scrollbar-width: none !important; }'],
'hidden as the module installs',
);
assert.equal(view.window.document.querySelectorAll('*').length, childrenBefore, 'no element added to the page, which other frame code watches');
view.flush();
assert.deepEqual(
view.reports(),
[{ type: 'avb:page-scroll', topPx: 0, heightPx: 4000, viewHeightPx: 800, chromium: false }],
'the first report has no Chromium bar',
);
// The app's word still comes on load and writes the <style> element, as for any frame: the same rule.
assert.equal(view.module.receiveCanvasScrollbar({ type: 'avb:canvas-scrollbar' }), true);
assert.equal(view.sheet(), 'html, body { scrollbar-width: none !important; }');
assert.equal(view.window.document.adoptedStyleSheets.length, 1, 'adopted once');
});

test('a frame without the canvas’s mark keeps its bar until the app’s word', (context) => {
const plain = frame({ heightPx: 4000, url: 'http://localhost/about/#section', adoptedSheets: true });
context.after(plain.close);
plain.install();
plain.flush();
assert.equal(plain.window.document.adoptedStyleSheets.length, 0, 'a page opened on its own keeps its bar');
assert.equal(plain.sheet(), undefined);
assert.equal(plain.reports().at(-1).chromium, true);
});

test('a canvas frame in a document without adopted style sheets waits for the app’s word', (context) => {
const view = frame({ heightPx: 4000, url: 'http://localhost/#avb-design' });
context.after(view.close);
view.install();
view.flush();
assert.equal(view.sheet(), undefined, 'nothing written into the page');
assert.equal(view.reports().at(-1).chromium, true, 'and the report says Chromium still draws its bar');
assert.equal(view.module.receiveCanvasScrollbar({ type: 'avb:canvas-scrollbar' }), true);
view.flush();
assert.equal(view.reports().at(-1).chromium, false);
});

test('a page that holds <html> still and scrolls <body> is read and scrolled through <body>', (context) => {
const view = frame({ heightPx: 800 }); // <html> is exactly the view: it does not scroll.
context.after(view.close);
Expand Down
Loading