Skip to content

Commit 78060fb

Browse files
committed
The slide back moves the right way (the web view is laid out by bounds and centre, not frame, under its transform) and never shows a picture taken before the page's last route
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
1 parent c8a6a7e commit 78060fb

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎ios-shell/OpenBotShell/BackTransition.swift‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ final class BackTransition {
4949
/// taking one when `will-navigate` arrives is too late: the page waits 120 ms at most, and a
5050
/// snapshot on the simulator took longer, so it showed the new page (run 46165013120).
5151
private var latest: UIImage?
52+
/// When `latest` was asked for, and when the page last reported a route: a picture asked for
53+
/// before the last route is of another page and is not used.
54+
private var latestAskedAt = Date.distantPast
55+
private var lastRouteAt = Date.distantPast
5256
private var refreshWork: DispatchWorkItem?
5357

5458
func refresh(after seconds: TimeInterval) {
@@ -57,8 +61,11 @@ final class BackTransition {
5761
guard let self = self, let webView = self.webView, self.under == nil, self.cover == nil else { return }
5862
let config = WKSnapshotConfiguration()
5963
config.afterScreenUpdates = false
64+
let asked = Date()
6065
webView.takeSnapshot(with: config) { [weak self] image, _ in
61-
if let image = image { self?.latest = image }
66+
guard let self = self, let image = image, asked >= self.lastRouteAt else { return }
67+
self.latest = image
68+
self.latestAskedAt = asked
6269
}
6370
}
6471
refreshWork = item
@@ -69,14 +76,16 @@ final class BackTransition {
6976
/// at once, so the page can go ahead.
7077
func willPush(tab: String, completion: @escaping () -> Void) {
7178
refreshWork?.cancel()
72-
stacks[tab, default: []].append(latest)
79+
// No fresh picture of this page: no slide for it later (better than the wrong page).
80+
stacks[tab, default: []].append(latestAskedAt >= lastRouteAt ? latest : nil)
7381
latest = nil
7482
completion()
7583
}
7684

7785
/// A `route` with `depth`: drop or pad snapshots so this tab holds depth - 1 entries.
7886
func route(tab: String, depth: Int?) {
7987
currentTab = tab
88+
lastRouteAt = Date()
8089
if let depth = depth {
8190
var stack = stacks[tab] ?? []
8291
let wanted = max(0, depth - 1)
@@ -96,7 +105,7 @@ final class BackTransition {
96105
func begin() {
97106
guard let webView = webView, let container = webView.superview, under == nil,
98107
let image = stacks[currentTab]?.last ?? nil else { return }
99-
let view = UIView(frame: webView.frame)
108+
let view = UIView(frame: restingFrame(webView))
100109
view.clipsToBounds = true
101110
let picture = UIImageView(image: image)
102111
picture.frame = view.bounds
@@ -154,11 +163,17 @@ final class BackTransition {
154163
end(through: true, back: back)
155164
}
156165

166+
/// The web view's frame without the slide's transform.
167+
private func restingFrame(_ view: UIView) -> CGRect {
168+
CGRect(x: view.center.x - view.bounds.width / 2, y: view.center.y - view.bounds.height / 2,
169+
width: view.bounds.width, height: view.bounds.height)
170+
}
171+
157172
private func showCover() {
158173
guard let webView = webView, let container = webView.superview,
159174
let image = stacks[currentTab]?.last ?? nil else { return }
160175
let view = UIImageView(image: image)
161-
view.frame = webView.frame
176+
view.frame = restingFrame(webView)
162177
view.contentMode = .scaleToFill
163178
container.insertSubview(view, aboveSubview: webView)
164179
cover?.removeFromSuperview()

‎ios-shell/OpenBotShell/ShellContentView.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ final class ShellContentView: UIView, UITabBarDelegate {
137137
webBottom = bounds.height - (showBar ? barHeight : bottomInset)
138138
}
139139
let webFrame = CGRect(x: 0, y: 0, width: bounds.width, height: max(webBottom, 0))
140-
if webView.frame != webFrame { webView.frame = webFrame }
140+
// Bounds and centre, not frame: the slide back moves the web view with a transform, and
141+
// setting the frame of a transformed view moves its centre (the page slid the wrong way).
142+
let size = CGSize(width: webFrame.width, height: webFrame.height)
143+
let center = CGPoint(x: webFrame.midX, y: webFrame.midY)
144+
if webView.bounds.size != size { webView.bounds = CGRect(origin: webView.bounds.origin, size: size) }
145+
if webView.center != center { webView.center = center }
141146
if lastReported == nil || lastReported!.0 != barHeight || lastReported!.1 != webFrame {
142147
lastReported = (barHeight, webFrame)
143148
let report = onLayout

0 commit comments

Comments
 (0)