Show browser frames at one frame pixel for each screen pixel
The built-in browser looked blurry at Windows display scaling above 100%. The engine rendered sharp frames, but the host stretched each frame to the surface. The stretch resampled every pixel and blended text and images. - The engine crops the extra frame edge and sends the frame scale. - The view shows each frame at its CSS size, at a device pixel edge, on its own compositor layer, with nearest-pixel sampling. - Pointer input subtracts the frame offset. - The view sends a new scale when the display scale changes. - A new smoke test, browser/scale, measures stripe contrast at 100%, 125%, 150%, and 200%, and after a scale change. It also tests click positions. Measured contrast at 150%: 92 of 255 before, 255 after. (cherry picked from commit e98a65de536111b5365d1c6f863a897a8e38e505)
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
const current = $derived($browserState?.tabs.find(tab => tab.id === tabId));
|
||||
const blank = $derived(!current?.url || current.url === 'about:blank');
|
||||
let address = $state(''); let editing = $state(false);
|
||||
let image = $state(''); let surface: HTMLDivElement; let addressField: HTMLInputElement;
|
||||
let image = $state(''), imageBox = $state({ width: 0, height: 0, x: 0, y: 0, exact: false }); let surface: HTMLDivElement; let addressField: HTMLInputElement;
|
||||
let width = 1000, height = 700, textInput: HTMLTextAreaElement, composing = false;
|
||||
let inputQueue = Promise.resolve();
|
||||
const orderedInput = (command: string, args: Record<string, unknown>) => inputQueue = inputQueue.then(() => send(command, args));
|
||||
@@ -74,7 +74,9 @@
|
||||
if (frame.width > 0 && frame.height > 0 && ['image/png', 'image/jpeg'].includes(frame.mime)) {
|
||||
displayedFrame = frame;
|
||||
if (blank) { image = ''; acknowledgeFrame(); }
|
||||
else image = `data:${frame.mime};base64,${frame.data}`;
|
||||
else {
|
||||
image = `data:${frame.mime};base64,${frame.data}`; imageBox = frameBox(frame);
|
||||
}
|
||||
}
|
||||
});
|
||||
pageEvents.set(id, event => {
|
||||
@@ -83,6 +85,20 @@
|
||||
});
|
||||
return () => { acknowledgeFrame(); frames.delete(id); pageEvents.delete(id); closeMenu?.(); };
|
||||
});
|
||||
/**
|
||||
* Return the CSS box that shows each frame pixel on one screen pixel: the frame size in CSS pixels, and the offset
|
||||
* from the surface edge to the next device pixel edge. A stretched or misaligned frame is resampled and blurs text.
|
||||
* `exact` is true when the frame scale is the display scale. Then the image uses nearest-pixel sampling, because
|
||||
* layout rounds the CSS size to 1/64 pixel and a scale of 0.99998 still blends neighbour pixels.
|
||||
* Chromium uses nearest-pixel sampling only to scale up, so the size rounds up to the next 1/64 pixel.
|
||||
* The image has its own compositor layer. A shared layer can start at a fractional device pixel and blend the frame again.
|
||||
*/
|
||||
function frameBox(frame: BrowserFrame) {
|
||||
const scale = frame.scale || 1, ratio = devicePixelRatio || 1, rect = surface.getBoundingClientRect();
|
||||
const align = (edge: number) => (Math.ceil(edge * ratio - 0.001) - edge * ratio) / ratio;
|
||||
const size = (pixels: number) => Math.ceil(pixels / scale * 64) / 64;
|
||||
return { width: size(frame.width), height: size(frame.height), x: align(rect.left), y: align(rect.top), exact: scale === ratio };
|
||||
}
|
||||
function acknowledgeFrame() {
|
||||
const frame = displayedFrame; displayedFrame = undefined;
|
||||
if (frame) void request('frameAck', { tabId: frame.tabId, frameId: frame.frameId }).catch(() => {});
|
||||
@@ -115,7 +131,7 @@
|
||||
{ label: $t('Reload'), icon: 'refresh', run: () => void send('reload') },
|
||||
{ label: $t('Find in page'), icon: 'search', shortcut: 'Ctrl+F', run: () => void find() });
|
||||
const rect = surface.getBoundingClientRect();
|
||||
const menu = showActionMenu({ x: rect.left + context.x * rect.width / width, y: rect.top + context.y * rect.height / height,
|
||||
const menu = showActionMenu({ x: rect.left + imageBox.x + context.x, y: rect.top + imageBox.y + context.y,
|
||||
items, label: $t('Browser page actions') });
|
||||
closeMenu = menu.close;
|
||||
}
|
||||
@@ -140,7 +156,12 @@
|
||||
timer = setTimeout(syncSize, 100);
|
||||
});
|
||||
resize.observe(surface);
|
||||
return () => { mounted = false; stopScroll(); wheels.close(); closeMenu?.(); cancelAnimationFrame(moveFrame); clearTimeout(timer); clearTimeout(findTimer); resize.disconnect(); offState(); void connection.close(); };
|
||||
// A move to a monitor with a different display scale changes the pixel ratio but not always the surface size.
|
||||
let pixelRatio: MediaQueryList;
|
||||
const watchPixelRatio = () => { pixelRatio = matchMedia(`(resolution: ${devicePixelRatio}dppx)`); pixelRatio.addEventListener('change', changePixelRatio, { once: true }); };
|
||||
const changePixelRatio = () => { watchPixelRatio(); syncSize(); };
|
||||
watchPixelRatio();
|
||||
return () => { mounted = false; pixelRatio.removeEventListener('change', changePixelRatio); stopScroll(); wheels.close(); closeMenu?.(); cancelAnimationFrame(moveFrame); clearTimeout(timer); clearTimeout(findTimer); resize.disconnect(); offState(); void connection.close(); };
|
||||
});
|
||||
function syncSize() {
|
||||
const rect = surface.getBoundingClientRect();
|
||||
@@ -158,7 +179,7 @@
|
||||
const rect = surface.getBoundingClientRect();
|
||||
if (!rect.width || !rect.height) { stopScroll(); return; }
|
||||
const anchor = autoScroll.anchor;
|
||||
wheels.push({ type: 'mouseWheel', x: Math.min(width - 1, Math.round(anchor.x * width / rect.width)), y: Math.min(height - 1, Math.round(anchor.y * height / rect.height)), deltaX: -delta.x, deltaY: -delta.y });
|
||||
wheels.push({ type: 'mouseWheel', x: Math.min(width - 1, Math.round(anchor.x - imageBox.x)), y: Math.min(height - 1, Math.round(anchor.y - imageBox.y)), deltaX: -delta.x, deltaY: -delta.y });
|
||||
}
|
||||
scrollFrame = requestAnimationFrame(scrollStep);
|
||||
}
|
||||
@@ -186,7 +207,8 @@
|
||||
if (type === 'mouseUp' && event.button === 1) { autoScroll.release(); if (!autoScroll.anchor) stopScroll(); }
|
||||
if (type === 'mouseWheel') stopScroll(); else return;
|
||||
}
|
||||
const input = { type, x: Math.max(0, Math.min(width - 1, Math.round(point.x * width / rect.width))), y: Math.max(0, Math.min(height - 1, Math.round(point.y * height / rect.height))),
|
||||
// The frame has the CSS size of the page viewport. A surface point less the frame offset is the same page point.
|
||||
const input = { type, x: Math.max(0, Math.min(width - 1, Math.round(point.x - imageBox.x))), y: Math.max(0, Math.min(height - 1, Math.round(point.y - imageBox.y))),
|
||||
button: ['left', 'middle', 'right'][event.button] || 'left', clickCount: event.detail, modifiers: modifiers(event),
|
||||
...('deltaY' in event ? { deltaY: -event.deltaY * (event.deltaMode === 1 ? 40 : event.deltaMode === 2 ? height : 1), deltaX: -event.deltaX * (event.deltaMode === 1 ? 40 : event.deltaMode === 2 ? width : 1) } : {}) };
|
||||
if (type === 'mouseWheel') { wheels.push(input as Parameters<WheelQueue['push']>[0]); return; }
|
||||
@@ -261,7 +283,7 @@
|
||||
oncompositionend={event => { composing = false; if (event.data) void orderedInput('insertText', { text: event.data }); textInput.value = ''; }}
|
||||
oninput={() => { if (!composing && textInput.value) { void orderedInput('insertText', { text: textInput.value }); textInput.value = ''; } }}
|
||||
onblur={stopScroll}></textarea>
|
||||
{#if image && ready && !blank}<img src={image} alt={$t("Web page rendered by the browser")} draggable="false" onload={acknowledgeFrame} onerror={acknowledgeFrame}>{/if}
|
||||
{#if image && ready && !blank}<img src={image} class:exact={imageBox.exact} style:width={`${imageBox.width}px`} style:height={`${imageBox.height}px`} style:transform={`translate(${imageBox.x}px, ${imageBox.y}px)`} alt={$t("Web page rendered by the browser")} draggable="false" onload={acknowledgeFrame} onerror={acknowledgeFrame}>{/if}
|
||||
{#if scrollAnchor}<span class="scroll-anchor" style:left={`${scrollAnchor.x}px`} style:top={`${scrollAnchor.y}px`} aria-hidden="true">↕</span>{/if}
|
||||
</div>
|
||||
</div>
|
||||
@@ -275,7 +297,7 @@
|
||||
.surface{position:relative;flex:1;min-height:0;overflow:hidden;outline:none;background:var(--rs-bg)}.surface:focus-visible{box-shadow:inset 0 0 0 1px var(--rs-accent)}
|
||||
.browser.blank,.blank .surface{background:transparent}
|
||||
.surface.autoscroll{cursor:all-scroll}.scroll-anchor{position:absolute;transform:translate(-50%,-50%);display:grid;place-items:center;width:24px;height:24px;border:1px solid var(--rs-text);border-radius:50%;background:var(--rs-bg-elevated);color:var(--rs-text);font-size:20px;pointer-events:none;box-shadow:0 1px 4px #0006}
|
||||
img{display:block;width:100%;height:100%;pointer-events:none;user-select:none}.error{margin:0;padding:5px 10px;color:var(--rs-error);font-size:12px}
|
||||
img{display:block;pointer-events:none;user-select:none;will-change:transform}img.exact{image-rendering:pixelated}.error{margin:0;padding:5px 10px;color:var(--rs-error);font-size:12px}
|
||||
.error p{margin:4px 0}.status,.diagnostics{margin:0;padding:8px 10px;font-size:12px;color:var(--rs-text-dim)}
|
||||
.reconnect{padding:6px 12px;margin:4px 0;border:1px solid var(--rs-border);border-radius:var(--rs-control-radius);background:var(--rs-bg-elevated);color:var(--rs-text);cursor:pointer}
|
||||
.reconnect:focus-visible,summary:focus-visible{outline:2px solid var(--rs-accent);outline-offset:2px}summary{cursor:pointer}
|
||||
|
||||
@@ -13,7 +13,7 @@ export type Snapshot = {
|
||||
};
|
||||
export const state = writable<Snapshot | null>(null);
|
||||
export const pageErrors = writable<Record<string, string>>({});
|
||||
export type BrowserFrame = { tabId: string; frameId: number; mime: 'image/png' | 'image/jpeg'; data: string; width: number; height: number };
|
||||
export type BrowserFrame = { tabId: string; frameId: number; mime: 'image/png' | 'image/jpeg'; data: string; width: number; height: number; scale?: number };
|
||||
export type PageMenu = { event: string; tabId: string; token: string; x: number; y: number; linkURL: string; srcURL: string; hasImageContents: boolean; isEditable: boolean; selectionText: string; editFlags: Record<string, boolean>; url?: string };
|
||||
export const frames = new Map<string, (frame: BrowserFrame) => void>();
|
||||
export const pageEvents = new Map<string, (event: PageMenu) => void>();
|
||||
|
||||
Reference in New Issue
Block a user