Skip to content

Commit

Permalink
Disable perception tracking when creating UI/NS views/controllers in …
Browse files Browse the repository at this point in the history
…a representable.
  • Loading branch information
mbrandonw committed Sep 4, 2024
1 parent c981121 commit 134d1b2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Sources/AppKitNavigation/SwiftUI/Representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
>: NSViewControllerRepresentable {
private let base: NSViewControllerType
public init(_ base: () -> NSViewControllerType) {
self.base = base()
self.base = _PerceptionLocals.$skipPerceptionChecking.withValue(true) {
base()
}
}
public func makeNSViewController(context _: Context) -> NSViewControllerType { base }
public func updateNSViewController(_: NSViewControllerType, context _: Context) {}
Expand All @@ -24,7 +26,9 @@
public struct NSViewRepresenting<NSViewType: NSView>: NSViewRepresentable {
private let base: NSViewType
public init(_ base: () -> NSViewType) {
self.base = base()
self.base = _PerceptionLocals.$skipPerceptionChecking.withValue(true) {
base()
}
}
public func makeNSView(context _: Context) -> NSViewType { base }
public func updateNSView(_: NSViewType, context _: Context) {}
Expand Down
8 changes: 6 additions & 2 deletions Sources/UIKitNavigation/SwiftUI/Representable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
>: UIViewControllerRepresentable {
private let base: UIViewControllerType
public init(_ base: () -> UIViewControllerType) {
self.base = base()
self.base = _PerceptionLocals.$skipPerceptionChecking.withValue(true) {
base()
}
}
public func makeUIViewController(context _: Context) -> UIViewControllerType { base }
public func updateUIViewController(_: UIViewControllerType, context _: Context) {}
Expand All @@ -27,7 +29,9 @@
public struct UIViewRepresenting<UIViewType: UIView>: UIViewRepresentable {
private let base: UIViewType
public init(_ base: () -> UIViewType) {
self.base = base()
self.base = _PerceptionLocals.$skipPerceptionChecking.withValue(true) {
base()
}
}
public func makeUIView(context _: Context) -> UIViewType { base }
public func updateUIView(_: UIViewType, context _: Context) {}
Expand Down
32 changes: 32 additions & 0 deletions Tests/UIKitNavigationTests/ObserveTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if canImport(UIKit)
import UIKitNavigation
import SwiftUI
import XCTest

@available(iOS 16.0, *)
class ObserveTests: XCTestCase {
@MainActor
func testCompiles() {
Expand All @@ -11,5 +13,35 @@
}
XCTAssertEqual(count, 1)
}

@MainActor
func testPerceptionCheckingInNavigationStackController() async throws {
struct RootView: View {
@UIBindable var model = AppModel()
var body: some View {
UIViewControllerRepresenting {
NavigationStackController(path: $model.path) {
UIViewController()
}
}
}
}

try await render(RootView())
}

@MainActor
private func render(_ view: some View) async throws {
let image = ImageRenderer(content: view).cgImage
_ = image
try await Task.sleep(for: .seconds(0.1))
}
}

//extension NavigationStackController

@Perceptible
private class AppModel: HashableObject {
var path: [Int] = []
}
#endif

0 comments on commit 134d1b2

Please sign in to comment.