Merge branch 'bug/fullscreen-scroll-position-5542'
CI / SwiftLint (push) Canceled after 0s
CI / macOS Tests (push) Canceled after 0s
CI / iOS Simulator Tests (push) Canceled after 0s

This commit is contained in:
Brent Simmons committed 2026-11-27 22:05:42 -08:00
commit b5f17b40a5
1 file changed
+39 -9
+39 -9
View File
@@ -87,6 +87,7 @@ final class WebViewController: UIViewController {
}
private var restoreWindowScrollY: Int?
private var isChangingSize = false
private var lastLayoutWidth: CGFloat = 0
private var isArticleContentJavascriptEnabled = AppDefaults.isArticleContentJavascriptEnabled
override func viewDidLoad() {
@@ -115,17 +116,20 @@ final class WebViewController: UIViewController {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
beginSizeChange(coordinator: coordinator)
}
// Ignore mid-transition offsets, then scroll back to the reader’s place.
isChangingSize = true
coordinator.animate(alongsideTransition: nil) { [weak self] _ in
guard let self else {
return
}
restoreScrollAnchor()
scrollPositionDidChange()
isChangingSize = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Split view display mode changes and full-screen modal dismissals change the
// article’s width without calling viewWillTransition.
// <https://github.com/Ranchero-Software/NetNewsWire/issues/5542>
let width = view.safeAreaLayoutGuide.layoutFrame.width
if lastLayoutWidth > 0 && width != lastLayoutWidth {
beginSizeChange(coordinator: transitionCoordinator)
}
lastLayoutWidth = width
}
override func viewWillDisappear(_ animated: Bool) {
@@ -621,6 +625,9 @@ extension WebViewController: UIScrollViewDelegate {
}
@objc func scrollPositionDidChange() {
guard !isChangingSize else {
return
}
let articleIDWhenAsked = article?.articleID
webView?.evaluateJavaScript("recordScrollAnchor(); window.scrollY") { (scrollY, error) in
guard error == nil else {
@@ -761,6 +768,29 @@ private extension WebViewController {
webView.loadHTMLString(html, baseURL: URL(string: rendering.baseURL))
}
/// Ignores mid-reflow offsets, then scrolls back to the reader’s place once the new width is in effect.
private func beginSizeChange(coordinator: UIViewControllerTransitionCoordinator?) {
guard !isChangingSize else {
return
}
isChangingSize = true
if let coordinator {
coordinator.animate(alongsideTransition: nil) { [weak self] _ in
self?.endSizeChange()
}
} else {
DispatchQueue.main.async { [weak self] in
self?.endSizeChange()
}
}
}
private func endSizeChange() {
isChangingSize = false
restoreScrollAnchor()
scrollPositionDidChange()
}
/// 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() {