Skip to content

fix(#278): restore swipe-to-exit gesture in posts without breaking image galleries#306

Open
mozzerai wants to merge 1 commit into
MacMagazine:release/v5from
mozzerai:fix/278-swipe-to-exit-gesture
Open

fix(#278): restore swipe-to-exit gesture in posts without breaking image galleries#306
mozzerai wants to merge 1 commit into
MacMagazine:release/v5from
mozzerai:fix/278-swipe-to-exit-gesture

Conversation

@mozzerai

@mozzerai mozzerai commented Jun 9, 2026

Copy link
Copy Markdown

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):

  • NewsView usa .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.
  • SearchView nunca escondeu o back button — o gesto continuou vivo lá e conflitava com as galerias, exatamente como relatado.
  • Os scripts disableGallery/disableNewGallery só anulam o href dos 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 o interactivePopGestureRecognizer mesmo 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--open PhotoSwipe/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

  • Build completo: ✅ (iPhone 17 Pro simulator)
  • Testes do MacMagazineUILibrary: ✅ 58/58
  • SwiftLint strict nos arquivos alterados: ✅ 0 violações
  • Testado em iPhone 12 físico (iOS 26.5): deslizar para sair funciona; dentro da galeria, swipe navega imagens sem sair do post; comportamento idêntico via busca.

Fixes #278

🤖 Generated with Claude Code

…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 cassio-rossi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Verificar se conseguimos trazer de volta o gesto de deslizar para sair de posts

3 participants