Keep the reader's place in an article across a rotation

The recorded scroll position is window.scrollY, an absolute pixel offset
baked into the rendered page. Rotating reflows the article at a new width,
so that offset points somewhere else. The web view also scrolls on its own
during the transition, overwriting the stored position before anything can
restore it.

Record the element at the top of the viewport along with its offset, stop
recording while the view changes size, and scroll back to that element once
the transition ends.

Verified on an iPhone Duo. The same loss happens on iPad, which is where I
first noticed it.

Written with help from Claude Code.
This commit is contained in:
Dave Marquard committed 2026-11-03 14:02:00 -08:00
1 parent d9ec7df6f9
commit fc2a39f5ff
1 file changed
+46 -1
+46 -1
View File
@@ -84,6 +84,7 @@ final class WebViewController: UIViewController {
}
}
private var restoreWindowScrollY: Int?
private var isChangingSize = false
private var isArticleContentJavascriptEnabled = AppDefaults.isArticleContentJavascriptEnabled
override func viewDidLoad() {
@@ -110,6 +111,22 @@ final class WebViewController: UIViewController {
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Reflowing at a new width moves whatever the reader was looking at, and the offsets
// the web view reports mid-transition aren't where they were. Stop recording until
// it settles, then scroll back to the last place we knew about.
isChangingSize = true
coordinator.animate(alongsideTransition: nil) { [weak self] _ in
guard let self else {
return
}
self.restoreScrollAnchor()
self.isChangingSize = false
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause in-flight media before the view goes away. Leaving a video playing during
@@ -565,12 +582,25 @@ extension WebViewController: WKScriptMessageHandler {
extension WebViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !isChangingSize else {
return
}
scrollPositionQueue.add(self, #selector(scrollPositionDidChange))
}
@objc func scrollPositionDidChange() {
let articleIDWhenAsked = article?.articleID
webView?.evaluateJavaScript("window.scrollY") { (scrollY, error) in
// Note the element at the top of the viewport along with the offset. The offset alone
// means nothing once the article reflows at a different width.
let javascript = """
(function() {
const element = document.elementFromPoint(window.innerWidth / 2, 1);
window.nnwScrollAnchor = element;
window.nnwScrollAnchorTop = element ? element.getBoundingClientRect().top : 0;
return window.scrollY;
})();
"""
webView?.evaluateJavaScript(javascript) { (scrollY, error) in
guard error == nil else { return }
// A late callback must not record the previous article’s offset.
// <https://github.com/Ranchero-Software/NetNewsWire/issues/5243>
@@ -705,6 +735,21 @@ private extension WebViewController {
webView.loadHTMLString(html, baseURL: URL(string: rendering.baseURL))
}
/// Scrolls back to the element that was at the top of the viewport before the article
/// reflowed, so rotating doesn't lose the reader's place.
func restoreScrollAnchor() {
let javascript = """
(function() {
const element = window.nnwScrollAnchor;
if (!element || !element.isConnected) {
return;
}
window.scrollTo(0, window.scrollY + element.getBoundingClientRect().top - window.nnwScrollAnchorTop);
})();
"""
webView?.evaluateJavaScript(javascript, completionHandler: nil)
}
func finalScrollPosition(scrollingUp: Bool) -> CGFloat {
guard let webView = webView else { return 0 }