fix(#278): restore swipe-to-exit gesture in posts without breaking image galleries#306
fix(#278): restore swipe-to-exit gesture in posts without breaking image galleries#306mozzerai wants to merge 1 commit into
Conversation
…breaking image galleries The interactive pop gesture was lost in post details because navigationBarBackButtonHidden(true) disables it, while posts opened from search results kept the gesture and conflicted with image galleries. MMWebView now restores the pop gesture even with a hidden back button and suspends it while an in-page image gallery (PhotoSwipe/Powerkit, fancybox, magnific) is open, detected via an injected MutationObserver that reports lightbox state to a script message handler. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
cassio-rossi
left a comment
There was a problem hiding this comment.
Thanks for this — the approach is exactly right (MutationObserver for gallery state + a UIKit bridge to re-enable the pop gesture), and it follows the existing DisqusNewWindowHandler pattern nicely. Build, SwiftLint strict, and conventions all check out.
Three issues to address before merge, each with a suggested fix inline: (1) isGalleryOpen gets stuck true across page reloads, (2) the message handler registration is discarded on a WebPageCache hit, and (3) the gesture delegate isn't restored if the bridge deallocates without viewWillDisappear.
Please also give this a manual pass on device: open a post, open a gallery, swipe both directions, close it, swipe to exit — the selector list (.pswp--open, .fancybox-container, #fancybox-overlay, .mfp-wrap) is the load-bearing assumption here.
| page: $page, | ||
| reloadTrigger: reloadID | ||
| ) | ||
| .interactivePopGesture(enabled: !isGalleryOpen) |
There was a problem hiding this comment.
Bug: isGalleryOpen gets stuck true across page reloads, permanently disabling the gesture this PR restores.
onChange(of: colorScheme) calls page?.reload() (and this can fire automatically, e.g. the sunset dark-mode switch). The new document's observer starts with lastState = false and only posts on change — so if a gallery was open at reload time, no "closed" message ever arrives and the native side keeps isGalleryOpen == true for the rest of that screen's life.
The cleanest fix is in the script: initialize lastState to null and run check() once right after observing, so every new document unconditionally posts its initial state. This also covers WebKit content-process crash reloads:
var lastState = null;
function check() {
var open = !!document.querySelector(selectors);
if (open !== lastState) {
lastState = open;
window.webkit.messageHandlers.mmGalleryState.postMessage(open);
}
}
var observer = new MutationObserver(check);
observer.observe(document.documentElement, { ... });
check();Optionally also reset on the native side next to the existing reload triggers:
.onChange(of: colorScheme) {
isGalleryOpen = false
page?.reload()
}
.onChange(of: removeAds) {
if let cacheKey {
WebPageCache.shared.removePage(for: cacheKey)
}
isGalleryOpen = false
page = nil
reloadID = UUID()
}| contentController.addUserScript(MMWebViewUserScripts.disableNewGallery) | ||
| contentController.addUserScript(MMWebViewUserScripts.removeBackToBlog) | ||
| contentController.addUserScript(MMWebViewUserScripts.galleryStateObserver) | ||
| contentController.add(galleryStateHandler, name: GalleryStateMessageHandler.handlerName) |
There was a problem hiding this comment.
Bug: on a WebPageCache hit, this handler registration is thrown away.
makePage() builds a fresh configuration (including this add), but when WebPageCache.shared.page(for:) finds a cached page, the configuration is discarded — the cached page still holds the handler from the first view instance, whose onGalleryStateChange closure writes into that dead view's @State. Gallery messages then silently update nothing.
Today only the Live/Instagram root tabs use cacheKey, where the pop gesture is gated off by viewControllers.count > 1 anyway — but this becomes a real bug the moment cacheKey is used on a pushed view. (The same staleness already exists for the navigationDecider closures, so this PR replicates a latent pattern rather than introducing it.)
Suggested fix — cache the handler alongside the page and rebind its closure on every hit:
// WebPageCache
private var galleryHandlers: [String: GalleryStateMessageHandler] = [:]
func page(
for key: String,
configurationProvider: () -> WebPage.Configuration,
navigationDecider: some WebPage.NavigationDeciding,
galleryStateHandler: GalleryStateMessageHandler
) -> WebPage {
if let existing = cache[key] { return existing }
let page = WebPage(configuration: configurationProvider(),
navigationDecider: navigationDecider)
cache[key] = page
galleryHandlers[key] = galleryStateHandler
return page
}
func galleryHandler(for key: String) -> GalleryStateMessageHandler? {
galleryHandlers[key]
}
func removePage(for key: String) {
cache.removeValue(forKey: key)
galleryHandlers.removeValue(forKey: key)
}// MMWebView.makePage()
if let cacheKey {
page = WebPageCache.shared.page(
for: cacheKey,
configurationProvider: { configuration },
navigationDecider: navigationDecider,
galleryStateHandler: galleryStateHandler
)
// Rebind the registered handler to this view instance on cache hits.
WebPageCache.shared.galleryHandler(for: cacheKey)?
.onGalleryStateChange = { [self] isOpen in isGalleryOpen = isOpen }
}| gesture.delegate = self | ||
| } | ||
|
|
||
| override func viewWillDisappear(_ animated: Bool) { |
There was a problem hiding this comment.
Hardening: the system delegate is never restored if this controller deallocates without viewWillDisappear.
UIGestureRecognizer.delegate is weak, so if BridgeViewController dies on a teardown path that skips appearance callbacks, the pop gesture is left with a nil delegate — completely ungated. An edge swipe on a root view controller can then begin a pop with nothing to pop, which is the classic frozen-navigation bug.
Since the project is Swift 6.2, isolated deinit (SE-0371) makes this a clean fix — extract the restore logic and call it from both places:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
restoreDelegate()
}
isolated deinit {
restoreDelegate()
}
private func restoreDelegate() {
if let popGesture, popGesture.delegate === self {
popGesture.delegate = originalDelegate
}
popGesture = nil
originalDelegate = nil
}
Problema (#278)
O gesto de deslizar para sair de posts foi perdido na v5 e, ao mesmo tempo, continuava ativo em posts abertos pela busca — conflitando com as galerias de imagens.
Causa raiz (dois lados):
NewsViewusa.navigationBarBackButtonHidden(true)para o botão de voltar customizado — isso desativa silenciosamente o gesto interativo de pop do SwiftUI. Por isso o gesto sumiu nos posts.SearchViewnunca escondeu o back button — o gesto continuou vivo lá e conflitava com as galerias, exatamente como relatado.disableGallery/disableNewGallerysó anulam ohrefdos links, mas a galeria do Powerkit (PhotoSwipe) registra handlers de clique via JS — então as galerias continuam abrindo no app.Solução
InteractivePopGesture.swift(novo): bridge UIKit que restaura ointeractivePopGestureRecognizermesmo com o back button escondido. Protegido: só dispara com a stack de navegação acima da raiz e sem transição em andamento; restaura o delegate original ao desaparecer.MMWebViewUserScripts.galleryStateObserver(novo script): MutationObserver que detecta lightbox aberto (.pswp--openPhotoSwipe/Powerkit, fancybox, magnific) e notifica o lado nativo.GalleryStateMessageHandler.swift(novo): recebe as mensagens de estado da galeria.MMWebView: galeria aberta ⇒ gesto suspenso; galeria fechada ⇒ deslizar para sair funciona.Comportamento unificado em: detalhe de notícias, resultados de busca e links internos aninhados. WebViews na raiz (Live, Instagram) não são afetadas pelo guard de profundidade da stack.
Testes
Fixes #278
🤖 Generated with Claude Code