From 9588596d7c0422da103f46a4d43a0353e002ca44 Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Mon, 17 Aug 2026 12:44:38 +0200 Subject: [PATCH] Open a document at the top of its own first page The script that fits a wide page to the screen runs at document end, while the web view still has its storyboard width of 414 points. It measured the page at 826 pixels, called that wider than the screen, and pinned the viewport to that width. An iPad then lays out at 1032 - wider than the page, which therefore never needed pinning - and the re-anchor across that resize left the page 137 pixels down. Far enough to take the heading off a pdf or a Word document. The width now comes from the visual viewport, which reports the web view's own width whatever viewport we name, and it is measured again on every resize, so a rotation gets the same answer. A page that fits is handed back the viewport odrcore served. Measured on both devices: the iPad settles at width=device-width, zoom 1, scroll 0, where it sat 137 pixels down before; the iPhone keeps width=826 at zoom 0.53, which is the fitting this is for. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01E6tn8P25cBDmrA67zrhfwN --- CHANGELOG.md | 2 ++ .../DocumentViewController.swift | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d08bc83..f1a1017 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,8 @@ once the version tag exists. showed it for most of the rest. - Tapping a document sets the cursor, so an edit can be typed. The keyboard never came up before. +- A document opens at the top of its first page. On a wide screen it opened far + enough down to cut off the heading. ### Removed diff --git a/OpenDocumentReader/DocumentViewController.swift b/OpenDocumentReader/DocumentViewController.swift index 6aefc72..5477fff 100644 --- a/OpenDocumentReader/DocumentViewController.swift +++ b/OpenDocumentReader/DocumentViewController.swift @@ -108,10 +108,28 @@ class DocumentViewController: UIViewController, DocumentDelegate, UISearchBarDel return; } - var width = document.documentElement.scrollWidth; - if (width > window.innerWidth) { - meta.setAttribute('content', 'width=' + width + ',user-scalable=yes'); + var served = meta.content; + var natural = document.documentElement.scrollWidth; + + // The web view's own width, which the viewport named below does not + // change: the visual viewport is that many CSS pixels at that scale. + function available() { + var seen = window.visualViewport; + + return seen ? Math.round(seen.width * seen.scale) : window.innerWidth; } + + function fit() { + meta.setAttribute( + 'content', + natural > available() ? 'width=' + natural + ',user-scalable=yes' : served); + } + + // On every resize, not just now: this first runs before the web view + // has the width it will keep, and a page held at a width it no + // longer needs is left scrolled off its own top. + fit(); + window.addEventListener('resize', fit); })(); """ }