From a4bdd410219b146bfbe7a946cdcc22340ef3163d Mon Sep 17 00:00:00 2001 From: tkgstrator <29420801+tkgstrator@users.noreply.github.com> Date: Sun, 25 Oct 2026 09:17:41 +0900 Subject: [PATCH] fix(ui): preserve native tab safe areas Co-Authored-By: Codex --- Interceptor/ContentView.swift | 15 -- .../Views/TabNavigationAlignment.swift | 195 ------------------ 2 files changed, 210 deletions(-) delete mode 100644 Interceptor/Views/TabNavigationAlignment.swift diff --git a/Interceptor/ContentView.swift b/Interceptor/ContentView.swift index d9586ef..6e21d17 100644 --- a/Interceptor/ContentView.swift +++ b/Interceptor/ContentView.swift @@ -16,7 +16,6 @@ struct ContentView: View { @EnvironmentObject private var client: Tuberose @Environment(\.scenePhase) private var scenePhase @State private var selectedTab = 0 - @StateObject private var tabNavigationAlignment = TabNavigationAlignment() @StateObject private var fileDestinations = PluginFileDestinationController() @AppStorage(CaptureAuthorization.key, store: CaptureAuthorization.defaults) private var consentVersion = 0 @@ -72,11 +71,6 @@ struct ContentView: View { NavigationView(content: { HomeView() }) - .background { - if #available(iOS 27, *) { - TabNavigationAlignmentObserver(alignment: tabNavigationAlignment) - } - } .navigationBarTitleDisplayMode(.inline) .introspect(.navigationSplitView, on: .iOS(.v17...), customize: { controller in controller.preferredDisplayMode = .oneBesideSecondary @@ -93,11 +87,6 @@ struct ContentView: View { description: Text("EMPTY_SETTINGS_DESCRIPTION")) .navigationBarTitleDisplayMode(.inline) }) - .background { - if #available(iOS 27, *) { - TabNavigationAlignmentObserver(alignment: tabNavigationAlignment) - } - } .navigationBarTitleDisplayMode(.inline) .introspect(.navigationSplitView, on: .iOS(.v17...), customize: { controller in controller.preferredDisplayMode = .oneBesideSecondary @@ -109,10 +98,6 @@ struct ContentView: View { } .tag(1) }) - .onChange(of: selectedTab) { tabNavigationAlignment.update() } - .introspect(.tabView, on: .iOS(.v27)) { tabView in - tabNavigationAlignment.configure(tabView) - } .introspect(.tabView, on: .iOS(.v17, .v18, .v26, .v27)) { tabView in // Change only selected tab items; the shared accent still colors primary buttons. let selectedColor = UIColor { traits in diff --git a/Interceptor/Views/TabNavigationAlignment.swift b/Interceptor/Views/TabNavigationAlignment.swift deleted file mode 100644 index 6bdabd5..0000000 --- a/Interceptor/Views/TabNavigationAlignment.swift +++ /dev/null @@ -1,195 +0,0 @@ -import SwiftUI -import UIKit - -/// Removes the extra top inset applied to a tab's split navigation on iPadOS 27. -/// The tab controller keeps ownership of the floating tabs and their position. -@MainActor -final class TabNavigationAlignment: ObservableObject { - @MainActor - private final class Insets { - weak var controller: UIViewController? - var originalTop: CGFloat - var lastWritten: CGFloat? - - init(_ controller: UIViewController) { - self.controller = controller - originalTop = controller.additionalSafeAreaInsets.top - } - } - - private weak var tabView: UITabBarController? - private var insets: [ObjectIdentifier: Insets] = [:] - private final class Observation { - weak var view: AlignmentObservationView? - init(_ view: AlignmentObservationView) { self.view = view } - } - - private var observations: [Observation] = [] - private var updating = false - private var waitingForTransition = false - - func configure(_ controller: UITabBarController) { - if tabView !== controller { - restore() - tabView = controller - } - update() - } - - func observe(_ view: AlignmentObservationView) { - observations.removeAll { $0.view == nil } - observations.append(Observation(view)) - update(from: view) - } - - func update() { - for observation in observations { - if let view = observation.view { update(from: view) } - } - } - - func update(from observer: AlignmentObservationView) { - guard #available(iOS 27, *) else { return } - guard !updating else { observer.setNeedsLayout(); return } - guard let tabView, let window = tabView.viewIfLoaded?.window else { return } - updating = true - defer { updating = false } - - let tolerance = 0.5 / window.screen.scale - insets = insets.filter { $0.value.controller != nil } - guard tabView.traitCollection.userInterfaceIdiom == .pad, - tabView.traitCollection.horizontalSizeClass == .regular, - !tabView.isTabBarHidden - else { - restore(tolerance: tolerance) - return - } - - if let transition = tabView.transitionCoordinator, transition.isAnimated { - if !waitingForTransition { - waitingForTransition = true - transition.animate(alongsideTransition: nil) { [weak self] _ in - self?.waitingForTransition = false - self?.update() - } - } - return - } - - guard let child = tabView.selectedViewController ?? tabView.selectedTab?.viewController, - let view = child.viewIfLoaded, view.window === window, - !view.bounds.isEmpty, observer.window === window, - observer.isDescendant(of: view) - else { return } - observer.boundController = child - - // Detached tabs can report a zero safe area. Never calculate from them. - for state in insets.values where state.controller !== child { - restore(state, tolerance: tolerance) - } - let key = ObjectIdentifier(child) - let state = insets[key] ?? Insets(child) - insets[key] = state - let current = child.additionalSafeAreaInsets.top - if state.lastWritten == nil { - state.originalTop = current - } else if let written = state.lastWritten, abs(current - written) > tolerance { - // Respect another owner changing this inset, including UIKit resets. - state.originalTop = current - state.lastWritten = nil - } - - let inheritedTop = view.safeAreaInsets.top - current - let extraTop = max(0, inheritedTop - window.safeAreaInsets.top) - let top = state.originalTop - extraTop - guard abs(current - top) > tolerance else { return } - state.lastWritten = top - var adjusted = child.additionalSafeAreaInsets - adjusted.top = top - child.additionalSafeAreaInsets = adjusted - } - - func restore() { - restore(tolerance: 0) - } - - func detach(_ view: AlignmentObservationView) { - if let child = view.boundController, let state = insets[ObjectIdentifier(child)] { - restore(state, tolerance: 0) - } - view.boundController = nil - } - - private func restore(tolerance: CGFloat) { - for state in insets.values { restore(state, tolerance: tolerance) } - } - - private func restore(_ state: Insets, tolerance: CGFloat) { - guard let child = state.controller, let written = state.lastWritten else { return } - if abs(child.additionalSafeAreaInsets.top - written) <= tolerance { - var restored = child.additionalSafeAreaInsets - restored.top = state.originalTop - state.lastWritten = nil - child.additionalSafeAreaInsets = restored - } else { - state.originalTop = child.additionalSafeAreaInsets.top - state.lastWritten = nil - } - } -} - -struct TabNavigationAlignmentObserver: UIViewRepresentable { - let alignment: TabNavigationAlignment - - func makeCoordinator() -> TabNavigationAlignment { alignment } - - func makeUIView(context: Context) -> AlignmentObservationView { - let view = AlignmentObservationView() - view.backgroundColor = .clear - view.isOpaque = false - view.isUserInteractionEnabled = false - view.accessibilityElementsHidden = true - view.onUpdate = { [weak alignment, weak view] in - if let view { alignment?.update(from: view) } - } - view.onDetach = { [weak alignment, weak view] in - if let view { alignment?.detach(view) } - } - view.registerForTraitChanges([UITraitHorizontalSizeClass.self, UITraitUserInterfaceIdiom.self]) { - (view: AlignmentObservationView, _: UITraitCollection) in view.onUpdate?() - } - alignment.observe(view) - return view - } - - func updateUIView(_ view: AlignmentObservationView, context: Context) { - alignment.update(from: view) - } - - static func dismantleUIView(_ view: AlignmentObservationView, coordinator: TabNavigationAlignment) { - view.onUpdate = nil - view.onDetach = nil - coordinator.detach(view) - } -} - -final class AlignmentObservationView: UIView { - weak var boundController: UIViewController? - var onUpdate: (() -> Void)? - var onDetach: (() -> Void)? - - override func didMoveToWindow() { - super.didMoveToWindow() - if window == nil { onDetach?() } else { onUpdate?() } - } - - override func safeAreaInsetsDidChange() { - super.safeAreaInsetsDidChange() - onUpdate?() - } - - override func layoutSubviews() { - super.layoutSubviews() - onUpdate?() - } -}