Skip to content
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
97b0926
portal: invalidate the split-divider hit-test cache on nested subview…
ejc3 Jul 21, 2026
dfb0bad
portal: compare structure snapshots without intermediate arrays
ejc3 Jul 25, 2026
988f507
portal: cover deep-container split insertion and content-root replace…
ejc3 Jul 31, 2026
7346268
portal: validate the divider cache with a root-keyed full-tree split …
ejc3 Jul 31, 2026
679c016
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 4, 2026
93de123
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
ce464fb
portal: prove cache hits skip large hierarchy traversal
austinywang Aug 7, 2026
f9cd05c
portal: invalidate divider caches at hierarchy mutations
austinywang Aug 7, 2026
84884a5
portal: scope hierarchy invalidation to cache roots
austinywang Aug 7, 2026
2dfe036
portal: nest cache invalidation helpers
austinywang Aug 7, 2026
57606a4
portal: give hierarchy mutation routing one owner
austinywang Aug 7, 2026
6c1a17b
portal: hook every hierarchy mutation entrypoint
austinywang Aug 7, 2026
3138477
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
8c0dac3
test: align OMP resume environment expectation
austinywang Aug 7, 2026
42dccd8
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
1fb78d7
test: keep install command fixture inert
austinywang Aug 7, 2026
3534745
test: cover app-host config path aliases
austinywang Aug 7, 2026
a86438f
ci: canonicalize app-host config evidence paths
austinywang Aug 7, 2026
85d13af
fix: resolve Xcode 26.3 Swift warnings
austinywang Aug 7, 2026
92ec9a6
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
438bde3
fix: preserve Xcode 16 delegate compatibility
austinywang Aug 7, 2026
92fdb4c
test: bound portal hierarchy mutation hook work
austinywang Aug 7, 2026
37eb01e
portal: make hierarchy invalidation generation-based
austinywang Aug 7, 2026
585cbd2
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
f57b7d7
test: cover no-op sorts and detached portal roots
austinywang Aug 7, 2026
f99e858
portal: preserve cache validity across sort and reattach
austinywang Aug 7, 2026
aecb63c
test: cover detached nested portal subtree mutation
austinywang Aug 7, 2026
342b48d
test: calibrate portal mutation work against AppKit
austinywang Aug 7, 2026
053695f
portal: distrust detached subtree hierarchy state
austinywang Aug 7, 2026
cab9967
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 7, 2026
f87e5f2
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 8, 2026
9b66708
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 8, 2026
21f7c99
fix: fail closed when portal hierarchy hooks are unavailable
austinywang Aug 9, 2026
332aaec
test: drop stale OMP path override from portal branch
austinywang Aug 9, 2026
947a99e
test: cover split-free subviews replacement
austinywang Aug 9, 2026
7ea0c07
portal: preserve split-free subviews fast path
austinywang Aug 9, 2026
fc3dfe7
test: validate every warmed portal cache
austinywang Aug 9, 2026
c1e4e7d
test: cover arranged split pane mutations
austinywang Aug 9, 2026
02aecd7
portal: track arranged split pane mutations
austinywang Aug 9, 2026
53dae4a
test: cover detached subtree parking path
austinywang Aug 9, 2026
c7b568d
portal: revoke proofs at unindexed hierarchy boundaries
austinywang Aug 9, 2026
aaa4600
test: isolate arranged pane mutation cases
austinywang Aug 9, 2026
c1bcede
Merge remote-tracking branch 'origin/main' into fix/portal-hittest-ca…
austinywang Aug 9, 2026
b267f3c
test: cover wrapped detached portal subtree
austinywang Aug 9, 2026
be1e22b
portal: track detached indexed subtree mutations
austinywang Aug 9, 2026
27427a5
portal: keep mutation snapshots with their owner
austinywang Aug 9, 2026
d0d9126
test: cover inactive portal proof lifetime
austinywang Aug 9, 2026
0edb762
portal: revoke proofs between cache lifetimes
austinywang Aug 9, 2026
6b8fd11
test: avoid arranged-pane teardown KVO trap
austinywang Aug 9, 2026
779aa42
test: cover detached portal reorder mutations
austinywang Aug 9, 2026
e3be8ba
test: restore split arrangement before teardown
austinywang Aug 9, 2026
99968cc
test: document detached cache boundary
austinywang Aug 9, 2026
dfe3fd7
test: use valid arranged-pane lifecycles
austinywang Aug 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Sources/BrowserWindowPortal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ final class WindowBrowserHostView: NSView {
private var cachedSidebarDividerX: CGFloat?
private var sidebarDividerMissCount = 0
private var cachedSplitDividerRegions: [DividerRegion]?
private var cachedSplitDividerRootSubviewIds: [ObjectIdentifier]?
private weak var cachedSplitDividerRootView: NSView?
private let splitDividerCacheInvalidator = PortalSplitDividerCacheInvalidator()
private var splitDividerResizeObserver: NSObjectProtocol?
private var trackingArea: NSTrackingArea?
Expand Down Expand Up @@ -1034,15 +1034,23 @@ final class WindowBrowserHostView: NSView {
return (pageFrame, inspectorFrame)
}
private func splitDividerRegions() -> [DividerRegion] {
guard let rootView = dividerSearchRootView() else { cachedSplitDividerRegions = []; cachedSplitDividerRootSubviewIds = nil; return [] }
let rootSubviewIds = rootView.subviews.map { ObjectIdentifier($0) }
if let regions = cachedSplitDividerRegions, cachedSplitDividerRootSubviewIds == rootSubviewIds, PortalSplitDividerRegion.allLive(regions) { return regions }
guard let rootView = dividerSearchRootView() else {
invalidateSplitDividerRegionCache()
return []
}
if let regions = cachedSplitDividerRegions,
cachedSplitDividerRootView === rootView,
splitDividerCacheInvalidator.isHierarchyCurrent(for: rootView),
PortalSplitDividerRegion.allLive(regions) {
return regions
}
let collected = PortalSplitDividerRegion.collect(in: rootView, hostView: self)
cachedSplitDividerRegions = collected.regions
cachedSplitDividerRootSubviewIds = rootSubviewIds
cachedSplitDividerRootView = rootView
splitDividerCacheInvalidator.observe(
rootView: rootView,
geometryViews: collected.geometryObservedViews,
structureViews: collected.structureObservedViews
hierarchyNodes: collected.hierarchyNodes
) { [weak self] in
guard let self else { return }
self.invalidateSplitDividerRegionCache()
Expand All @@ -1053,7 +1061,7 @@ final class WindowBrowserHostView: NSView {

private func invalidateSplitDividerRegionCache() {
cachedSplitDividerRegions = nil
cachedSplitDividerRootSubviewIds = nil
cachedSplitDividerRootView = nil
splitDividerCacheInvalidator.invalidate()
}

Expand Down Expand Up @@ -1753,6 +1761,18 @@ final class WindowBrowserSlotView: NSView {
overlayContainer.addSubview(dropZoneOverlayView, positioned: .above, relativeTo: nil)
}

var previousPriority = Int.min
var needsReordering = false
for subview in subviews {
let priority = interactionLayerPriority(of: subview)
if priority < previousPriority {
needsReordering = true
break
}
previousPriority = priority
}
guard needsReordering else { return }

let context = Unmanaged.passUnretained(self).toOpaque()
sortSubviews({ lhs, rhs, context in
guard let context else { return .orderedSame }
Expand Down
171 changes: 161 additions & 10 deletions Sources/PortalSplitDividerCacheInvalidator.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,178 @@
import AppKit
import ObjectiveC

private typealias PortalSubviewComparator = @convention(c) (
NSView,
NSView,
UnsafeMutableRawPointer?
) -> ComparisonResult

private extension NSView {
@objc(cmux_portalAddSubview:)
func cmux_portalAddSubview(_ subview: NSView) {
let previousWindow = subview.window
cmux_portalAddSubview(subview)
PortalViewHierarchyMutationTracker.recordInsertion(
parentView: self,
insertedView: subview,
previousWindow: previousWindow
)
}

@objc(cmux_portalAddSubview:positioned:relativeTo:)
func cmux_portalAddSubview(
_ subview: NSView,
positioned place: NSWindow.OrderingMode,
relativeTo otherView: NSView?
) {
let previousWindow = subview.window
cmux_portalAddSubview(subview, positioned: place, relativeTo: otherView)
PortalViewHierarchyMutationTracker.recordInsertion(
parentView: self,
insertedView: subview,
previousWindow: previousWindow
)
}

@objc(cmux_portalSetSubviews:)
func cmux_portalSetSubviews(_ newSubviews: [NSView]) {
let replacementState = PortalViewHierarchyMutationTracker.subviewsBeforeReplacement(
parentView: self,
newSubviews: newSubviews
)
cmux_portalSetSubviews(newSubviews)
guard let replacementState else { return }
PortalViewHierarchyMutationTracker.recordSubviewsReplacement(
parentView: self,
newSubviews: newSubviews,
replacementState: replacementState
)
}

@objc func cmux_portalRemoveFromSuperview() {
let oldSuperview = superview
let previousWindow = window
cmux_portalRemoveFromSuperview()
if let oldSuperview {
PortalViewHierarchyMutationTracker.recordRemoval(
parentView: oldSuperview,
removedView: self,
previousWindow: previousWindow
)
}
}

@objc func cmux_portalRemoveFromSuperviewWithoutNeedingDisplay() {
let oldSuperview = superview
let previousWindow = window
cmux_portalRemoveFromSuperviewWithoutNeedingDisplay()
if let oldSuperview {
PortalViewHierarchyMutationTracker.recordRemoval(
parentView: oldSuperview,
removedView: self,
previousWindow: previousWindow
)
}
}

@objc func cmux_portalReplaceSubview(_ oldView: NSView, with newView: NSView) {
let parentWindow = window
let newViewPreviousWindow = newView.window
cmux_portalReplaceSubview(oldView, with: newView)
PortalViewHierarchyMutationTracker.recordReplacement(
parentView: self,
oldView: oldView,
newView: newView,
newViewPreviousWindow: newViewPreviousWindow,
parentWindow: parentWindow
)
}

@objc func cmux_portalSortSubviews(
_ compare: PortalSubviewComparator,
context: UnsafeMutableRawPointer?
) {
let parentWindow = window
let sortState = PortalViewHierarchyMutationTracker.subviewOrderBeforeSort(
parentView: self,
parentWindow: parentWindow
)
cmux_portalSortSubviews(compare, context: context)
guard let sortState else { return }
PortalViewHierarchyMutationTracker.recordSortIfNeeded(
parentView: self,
sortState: sortState
)
}
}

@MainActor
final class PortalSplitDividerCacheInvalidator {
/// AppKit does not document `subviews` as KVO-compliant. Hook every public
/// hierarchy mutation entrypoint once so the window tracker can advance its
/// generation without a pointer-time tree walk.
private static let hierarchyMutationHooksInstalled: Bool = {
let selectorPairs: [(original: Selector, replacement: Selector)] = [
(#selector(NSView.addSubview(_:)), #selector(NSView.cmux_portalAddSubview(_:))),
(
#selector(NSView.addSubview(_:positioned:relativeTo:)),
#selector(NSView.cmux_portalAddSubview(_:positioned:relativeTo:))
),
(#selector(setter: NSView.subviews), #selector(NSView.cmux_portalSetSubviews(_:))),
(#selector(NSView.removeFromSuperview), #selector(NSView.cmux_portalRemoveFromSuperview)),
(
#selector(NSView.removeFromSuperviewWithoutNeedingDisplay),
#selector(NSView.cmux_portalRemoveFromSuperviewWithoutNeedingDisplay)
),
(
#selector(NSView.replaceSubview(_:with:)),
#selector(NSView.cmux_portalReplaceSubview(_:with:))
),
(#selector(NSView.sortSubviews(_:context:)), #selector(NSView.cmux_portalSortSubviews(_:context:))),
]

var methodPairs: [(original: Method, replacement: Method)] = []
for selectors in selectorPairs {
guard let original = class_getInstanceMethod(NSView.self, selectors.original),
let replacement = class_getInstanceMethod(NSView.self, selectors.replacement) else {
assertionFailure("Unable to install portal view-hierarchy mutation hook for \(selectors.original)")
return false
}
methodPairs.append((original, replacement))
}
for methods in methodPairs {
method_exchangeImplementations(methods.original, methods.replacement)
}
return true
}()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Observer tokens are assigned/cleared from main-thread AppKit paths. Swift
// deinit is nonisolated, so the teardown helper needs nonisolated access
// after all main-thread use has ceased.
private nonisolated(unsafe) var observations: [NSKeyValueObservation] = []
private nonisolated(unsafe) var notificationObservers: [NSObjectProtocol] = []
private var hierarchyRegistration: PortalViewHierarchyMutationRegistration?

init() {
_ = Self.hierarchyMutationHooksInstalled
}

deinit {
invalidateObservations()
}

func observe(
rootView: NSView,
geometryViews: [NSView],
structureViews: [NSView],
hierarchyNodes: [(view: NSView, containsSplitView: Bool)],
onChange: @escaping @MainActor () -> Void
) {
invalidate()
let geometryViews = Self.uniqueViews(geometryViews)
let subviewObservedViews = Self.uniqueViews(geometryViews + structureViews)
hierarchyRegistration = PortalViewHierarchyMutationTracker.register(
rootView: rootView,
hierarchyNodes: hierarchyNodes
)

for view in geometryViews {
// These NSView flags are shared; do not restore them per observer or
Expand All @@ -42,14 +195,6 @@ final class PortalSplitDividerCacheInvalidator {
MainActor.assumeIsolated { onChange() }
}
}
// Nested splits can be inserted under known layout containers after cache
// warm-up. Keep this bounded to root/direct/split-related containers, not
// arbitrary descendants such as WebKit or terminal internals.
observations.append(contentsOf: subviewObservedViews.map { view in
view.observe(\.subviews, options: [.new]) { _, _ in
MainActor.assumeIsolated { onChange() }
}
})
}

private static func uniqueViews(_ views: [NSView]) -> [NSView] {
Expand All @@ -62,9 +207,15 @@ final class PortalSplitDividerCacheInvalidator {
}

func invalidate() {
hierarchyRegistration = nil
invalidateObservations()
}

func isHierarchyCurrent(for rootView: NSView) -> Bool {
guard Self.hierarchyMutationHooksInstalled else { return false }
return hierarchyRegistration?.isCurrent(for: rootView) == true
}

private nonisolated func invalidateObservations() {
observations.removeAll()
notificationObservers.forEach(NotificationCenter.default.removeObserver)
Expand Down
39 changes: 19 additions & 20 deletions Sources/PortalSplitDividerRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ final class PortalSplitDividerRegion {
static func collect(
in rootView: NSView,
hostView: NSView? = nil
) -> (regions: [PortalSplitDividerRegion], geometryObservedViews: [NSView], structureObservedViews: [NSView]) {
) -> (
regions: [PortalSplitDividerRegion],
geometryObservedViews: [NSView],
hierarchyNodes: [(view: NSView, containsSplitView: Bool)]
) {
var regions: [PortalSplitDividerRegion] = []
var geometryObservedViews: [NSView] = []
var geometryObservedIds = Set<ObjectIdentifier>()
var structureObservedViews: [NSView] = []
var structureObservedIds = Set<ObjectIdentifier>()
var hierarchyNodes: [(view: NSView, containsSplitView: Bool)] = []
var ancestorStack: [NSView] = []
appendObserved(rootView, to: &geometryObservedViews, ids: &geometryObservedIds)
appendObserved(rootView, to: &structureObservedViews, ids: &structureObservedIds)
for subview in rootView.subviews {
appendObserved(subview, to: &geometryObservedViews, ids: &geometryObservedIds)
appendObserved(subview, to: &structureObservedViews, ids: &structureObservedIds)
}
collect(
in: rootView,
Expand All @@ -160,12 +161,12 @@ final class PortalSplitDividerRegion {
into: &regions,
geometryObservedViews: &geometryObservedViews,
geometryObservedIds: &geometryObservedIds,
structureObservedViews: &structureObservedViews,
structureObservedIds: &structureObservedIds
hierarchyNodes: &hierarchyNodes
)
return (regions, geometryObservedViews, structureObservedViews)
return (regions, geometryObservedViews, hierarchyNodes)
}

@discardableResult
private static func collect(
in view: NSView,
hostView: NSView?,
Expand All @@ -174,21 +175,16 @@ final class PortalSplitDividerRegion {
into result: inout [PortalSplitDividerRegion],
geometryObservedViews: inout [NSView],
geometryObservedIds: inout Set<ObjectIdentifier>,
structureObservedViews: inout [NSView],
structureObservedIds: inout Set<ObjectIdentifier>
) {
hierarchyNodes: inout [(view: NSView, containsSplitView: Bool)]
) -> Bool {
let isHidden = ancestorHidden || view.isHidden
var containsSplitView = view is NSSplitView

if let splitView = view as? NSSplitView {
for ancestor in ancestorStack {
appendObserved(ancestor, to: &geometryObservedViews, ids: &geometryObservedIds)
appendObserved(ancestor, to: &structureObservedViews, ids: &structureObservedIds)
}
appendObserved(splitView, to: &geometryObservedViews, ids: &geometryObservedIds)
appendObserved(splitView, to: &structureObservedViews, ids: &structureObservedIds)
for arrangedSubview in splitView.arrangedSubviews {
appendObserved(arrangedSubview, to: &structureObservedViews, ids: &structureObservedIds)
}
if !isHidden {
appendDividerRegions(for: splitView, hostView: hostView, into: &result)
}
Expand All @@ -198,18 +194,21 @@ final class PortalSplitDividerRegion {
defer { ancestorStack.removeLast() }

for subview in view.subviews {
collect(
if collect(
in: subview,
hostView: hostView,
ancestorHidden: isHidden,
ancestorStack: &ancestorStack,
into: &result,
geometryObservedViews: &geometryObservedViews,
geometryObservedIds: &geometryObservedIds,
structureObservedViews: &structureObservedViews,
structureObservedIds: &structureObservedIds
)
hierarchyNodes: &hierarchyNodes
) {
containsSplitView = true
}
}
hierarchyNodes.append((view: view, containsSplitView: containsSplitView))
return containsSplitView
}

private static func appendObserved(_ view: NSView, to observedViews: inout [NSView], ids: inout Set<ObjectIdentifier>) {
Expand Down
27 changes: 27 additions & 0 deletions Sources/PortalViewHierarchyMutationRegistration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import AppKit

/// Weak lifetime token connecting one live divider cache to its window tracker.
@MainActor
final class PortalViewHierarchyMutationRegistration: NSObject {
private weak var tracker: PortalViewHierarchyMutationTracker?
private weak var rootView: NSView?
private let generation: UInt64

init(
tracker: PortalViewHierarchyMutationTracker,
rootView: NSView,
generation: UInt64
) {
self.tracker = tracker
self.rootView = rootView
self.generation = generation
}

func isCurrent(for rootView: NSView) -> Bool {
tracker?.isCurrent(
registrationGeneration: generation,
registeredRootView: self.rootView,
requestedRootView: rootView
) == true
}
}
Loading
Loading