Skip to content

Render pages at the display's pixel ratio with --scale - #41

Open
phaberest wants to merge 1 commit into
codr1:mainfrom
phaberest:feat/device-scale
Open

phaberest wants to merge 1 commit into
codr1:mainfrom
phaberest:feat/device-scale

Conversation

@phaberest

Copy link
Copy Markdown

Hello wonderful people!

I noticed that pages in Termium looked really tiny on my mac's retina display. After digging into it a bit, I found that the terminal reports its size in physical pixels, and Termium is using those dimensions directly as the browser's CSS viewport. So the browser was basically laying out pages for a screen twice as wide as it should be, making everything look half the size and there was no way to work around it.

I thought the simplest fix would be to let it know about the display scale, so I added a --scale option that accepts values from 1 to 4 and defaults to 1.
On a Retina display, you'd typically use --scale 2.

Getting the viewport right turned out to be only part of the job, though. Mouse clicks and wheel input also need to account for the scale, and Chromium's screenshot capture session needs the same viewport settings. Otherwise, the screenshots end up with the wrong dimensions. I updated those parts too, while keeping the default behaviour unchanged and making sure older clients still work.

I went with integer scales mainly to avoid changing behaviour outside the scope of this fix. The client already accepts only frames that exactly match the requested size, and since screenshots always span whole CSS pixels, rounding the viewport down to a multiple of an integer scale keeps that match exact.
Supporting fractional scales would have meant reworking how frames are accepted and drawn, which felt like a separate change.

Since the capture session now sends Emulation.setDeviceMetricsOverride before taking a screenshot when the scale isn't 1, I also made the existing retry for a briefly inactive compositor recognise the "Not attached to an active page" error from either command, not just from Page.captureScreenshot.

I've run lint, typecheck, the server unit tests, Go race tests, and the integration suite locally on my Apple Silicon and they all pass. I also added a test using a real Chromium instance with a 1601×1001 viewport to check the CSS dimensions, devicePixelRatio, screenshot size, and mouse and wheel input.
Removing either the capture-session override or the wheel conversion makes the test fail, which seems like a good sign that those cases are covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant