Skip to content

fix: ⌘+/⌘− zoom reflows the page instead of scaling a fixed layout - #103

Open
YankoFelipe wants to merge 1 commit into
hermes-webui:mainfrom
YankoFelipe:fix/zoom-reflows-page
Open

YankoFelipe wants to merge 1 commit into
hermes-webui:mainfrom
YankoFelipe:fix/zoom-reflows-page

Conversation

@YankoFelipe

Copy link
Copy Markdown

Problem

⌘+ / ⌘− (View → Zoom In / Zoom Out / Actual Size) scale the window's contents
instead of the page: zooming in pushes the composer below the bottom of the
window, and zooming out leaves unused gutters on the right and at the bottom.
Lines never re-wrap and the layout never re-flows.

That also makes ⌘+/⌘− useless as a readability control on large displays. The
WebUI's Settings → Appearance → Font size scale tops out below what a 4K display
needs (Extra large is still small), so client-level page zoom is the practical
way to scale the UI. The two settings compose — page zoom scales the chosen font
size rather than replacing it — so ⌘+/⌘− has to do real page zoom to be useful.

Cause

AppDelegate.zoomIn/zoomOut/zoomReset set WKWebView.magnification, the
scroll-view scale used by pinch gestures. It enlarges an already-laid-out page
inside a viewport that keeps its pre-zoom size, so nothing re-flows: above 1 the
bottom-anchored composer lands outside the window, below 1 the scaled page sits
in the top-left with empty space on the right and bottom.

Two details kept the wrong state sticky:

  • the saved value was re-applied as magnification in webView(_:didFinish:),
    i.e. after every navigation, so a reload restored it;
  • allowsMagnification = true kept the trackpad pinch on the same non-reflowing
    path, and magnification composes multiplicatively with page zoom.

Fix

  • Zoom is applied through WKWebView.pageZoom, which changes the CSS viewport,
    so text re-wraps and flex/grid layouts rearrange — Safari's ⌘+/⌘− behaviour.
  • The zoom policy moved into a WebKit-free HermesAgentCore target so
    swift test exercises the real type instead of a copy:
    • 0.1 steps across the existing 0.5–3.0 range;
    • quantized in integer tenths, so N presses up and N presses down return
      exactly to 100% (plain factor + 0.1 drifts to 1.2000000000000002);
    • ZoomLevel(restoring:) rejects the 0 that UserDefaults reports for a
      missing key, and anything outside the range;
    • the stored level migrates from webViewMagnification to webViewPageZoom
      on first read, so existing users keep their zoom level.
  • allowsMagnification = false: one zoom mechanism, no composition surprises.
  • The saved zoom is applied before the first paint and re-asserted after a
    navigation (a recreated WebContent process starts at 100%).

Tests

swift test passes on macOS 14 arm64 (87 tests, 0 failures), including 18 new
ones:

  • ZoomLevelTests — steps, drift, clamping, quantization, restore validation.
  • ZoomPreferencesTests — round-trip, legacy-key migration, key precedence.
  • PageZoomWiringTests — source-level guard that the View actions use
    pageZoom and native magnification stays off (no UI-test harness exists for
    this app; same approach as LongConversationCompatibilityTests).

Manual check: ⌘+, ⌘−, ⌘0, ⌘R and relaunch, including with Appearance → Font size
set to Extra large.

Behaviour change

Pinch-to-zoom no longer magnifies. It was the same non-reflowing mechanism and
would compose with page zoom, leaving ⌘0 not actually at 100%. Listed under
Changed in the CHANGELOG; mapping pinch onto page-zoom steps is a possible
follow-up.

Alternatives considered

  • Raise the WebUI font-size scale — lives in hermes-webui, is CSS-level,
    and still wouldn't make ⌘+/⌘− zoom.
  • Keep magnification — no reflow; that's the bug.
  • Resize the window — changes the frame, not the scale.

Checklist

  • Branch + PR, not a push to main
  • CHANGELOG entry under [Unreleased]
  • No Info.plist keys added (build.sh / CI parity untouched)
  • ATS, navigation delegates, Sparkle signing, SSH tunnel untouched
  • swift test green

The View menu set WKWebView.magnification — the scroll-view scale used by
pinch gestures. It enlarges an already-laid-out page inside a viewport that
keeps its pre-zoom size, so ⌘+ pushed the composer below the window and ⌘−
left unused gutters on the right and bottom.

Zoom now goes through WKWebView.pageZoom, which changes the CSS viewport and
reflows the layout. The policy lives in a WebKit-free HermesAgentCore target
(0.1 steps, 0.5–3.0 clamp, integer-tenths quantization, validation and
migration of the stored key). allowsMagnification is disabled so the two
zoom mechanisms cannot compose.

Tests: ZoomLevelTests, ZoomPreferencesTests, PageZoomWiringTests.
@nesquena-hermes

Copy link
Copy Markdown
Collaborator

Review at 91ba3cf93ab7b94f300ebdafd261dbcd2c5f35bc: two changes, then ready

Thanks @YankoFelipe, and welcome. Moving the zoom from magnification to pageZoom is the right fix: it reflows the page the way ⌘+/⌘− does in Safari. The ZoomLevel design holds up:

  • integer tenths, so steps don't drift;
  • clamping before the Int conversion and an isFinite check, so nothing can trap;
  • object(forKey:) as? NSNumber, so a missing key isn't read as 0;
  • persistence tests on an isolated UserDefaults suite;
  • a WebKit-free target that swift test exercises directly.

I found no leftover zoomKey or .magnification callers, and build.sh and build-release.yml only reference the HermesAgent product, so packaging is unaffected.

Review of 91ba3cf:

  • Codex and Fable: both say ship with one high-value change each, and each named a different one. Both are below.
  • Neither could build or run it: the reviews read the code against current WebUI CSS; this host has no Swift toolchain.
  • CI: it can't run right now, and that's on our side. test.yml uses runs-on: macos-14, which GitHub now rejects ("The configuration 'macos-14-arm64-us-default' is not supported"). I approved and re-ran the job and it was cancelled the same way. We'll move the workflow to a supported runner separately. Until then, please paste your local swift test result with the next push.

1. Higher zoom can hide the sidebar menu in native-tab mode [high value]

At ≤900 CSS px, WebUI switches to its narrow layout: the sidebar becomes a drawer, opened from the hamburger in .app-titlebar (core static/style.css, the max-width:900px block that shows .app-titlebar-hamburger). The app hides the whole .app-titlebar whenever native tabs are showing (BrowserWindowController.swift:659). Under pageZoom, a 1280-point window crosses 900 CSS px at about 143% zoom, so with two tabs open there's no way to reach the sidebar. Before this PR that needed a window narrower than 900 points; now it happens at ordinary window sizes.

This follows from reading the code; we haven't reproduced it on a Mac. Fix: keep the web titlebar in the narrow layout by scoping the injected rule:

s.textContent = '@media (min-width: 901px) { body.hermes-mac-tabbed .app-titlebar { display: none !important; } }';

Check: two native tabs at 150%, 200% and 300%, and the sidebar is still reachable each time.

2. Keep pinch-to-zoom [high value; a product call, and our recommendation]

Turning off allowsMagnification also removes pinch and smart-magnify, which the app has had since before v1.1.0 and which trackpad users rely on. Safari keeps both: pinch magnifies, and ⌘+/⌘− changes page zoom. The concern you raise, that pinch composes with page zoom so ⌘0 might not land on 100%, has a small fix:

  • BrowserWindowController.swift:590-591: keep webView.allowsMagnification = true.
  • AppDelegate.swift, in zoomReset():
    @objc func zoomReset() {
        // Pinch magnification composes with pageZoom; clear it so ⌘0 is a true 100%.
        keyBrowserWindow?.webViewForZoom?.magnification = 1.0
        applyZoom(.identity)
    }
  • ZoomLevelTests.swift:167-170 and :184-187: drop the ban on .magnification = in AppDelegate and the ban on allowsMagnification = true. Keep the pageZoom assertions and the controller's .magnification = ban, which are what guard the original bug.
  • CHANGELOG.md: remove the "Changed: Pinch-to-zoom no longer magnifies" entry.

Optional

  • Menu items at the bounds: isMinimum/isMaximum are only used by tests, so Zoom In and Zoom Out stay enabled at 300% and 50% and silently do nothing. Either add validateMenuItem in AppDelegate or remove the two properties.
  • Traffic-light reserve: --traffic-light-width is injected in CSS px equal to window points. Under pageZoom a CSS px renders as zoom points, so at roughly 80% and below the web title bar may slide under the traffic lights. Use let px = Int((reserve / max(webView.pageZoom, 0.1)).rounded(.up)) and re-inject from applyZoom. This is fine as a follow-up.
  • "Migrated on first read" wording: load() falls back to the legacy key until the first save and never writes or deletes it. The behaviour is correct; something like "Legacy zoom values are still read; the next zoom change saves under the new key" describes it better.

When you push, both reviews will run on the new head. A Mac build (./build.sh) and a quick check of item 1 with native tabs will complete it.

This branch has not been deployed

No deployments
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.

2 participants