Skip to content

Commit

Permalink
Filling out ViewControllerRepresentable
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Mar 27, 2024
1 parent 747eec4 commit 5dd270d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Sources/NSUI/ViewControllerRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public protocol NSUIViewControllerRepresentable: NSViewControllerRepresentable {

@MainActor
func updateNSUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context)

@MainActor
static func dismantleNSUIViewController(_ viewController: Self.NSUIViewControllerType, coordinator: Self.Coordinator)

@available(macOS 13.0, *)
@MainActor
func sizeThatFits(_ proposal: ProposedViewSize, nsUIViewController: Self.NSUIViewControllerType, context: Self.Context) -> CGSize?
}

public extension NSUIViewControllerRepresentable {
Expand All @@ -21,7 +28,20 @@ public extension NSUIViewControllerRepresentable {
func updateNSViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context) {
updateNSUIViewController(viewController, context: context)
}

@MainActor
static func dismantleNSViewController(_ viewController: Self.NSUIViewControllerType, coordinator: Self.Coordinator) {
Self.dismantleNSUIViewController(viewController, coordinator: coordinator)
}

@available(macOS 13.0, *)
@MainActor
func sizeThatFits(_ proposal: ProposedViewSize, nsViewController: Self.NSUIViewControllerType, context: Self.Context) -> CGSize? {
sizeThatFits(proposal, nsUIViewController: nsViewController, context: context)
}

}

#elseif canImport(UIKit)
public protocol NSUIViewControllerRepresentable: UIViewControllerRepresentable {
associatedtype NSUIViewControllerType: NSUIViewController
Expand All @@ -31,6 +51,13 @@ public protocol NSUIViewControllerRepresentable: UIViewControllerRepresentable {

@MainActor
func updateNSUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context)

@MainActor
static func dismantleNSUIViewController(_ viewController: Self.NSUIViewControllerType, coordinator: Self.Coordinator)

@available(iOS 16.0, tvOS 16.0, *)
@MainActor
func sizeThatFits(_ proposal: ProposedViewSize, nsUIViewController: Self.NSUIViewControllerType, context: Self.Context) -> CGSize?
}

public extension NSUIViewControllerRepresentable {
Expand All @@ -43,6 +70,31 @@ public extension NSUIViewControllerRepresentable {
func updateUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context) {
updateNSUIViewController(viewController, context: context)
}

@MainActor
static func dismantleUIViewController(_ viewController: Self.NSUIViewControllerType, coordinator: Self.Coordinator) {
Self.dismantleNSUIViewController(viewController, coordinator: coordinator)
}

@MainActor
func sizeThatFits(
_ proposal: ProposedViewSize,

Check failure on line 81 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS,variant=Mac Catalyst)

'ProposedViewSize' is only available in Mac Catalyst 16.0 or newer

Check failure on line 81 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS,variant=Mac Catalyst)

'ProposedViewSize' is only available in Mac Catalyst 16.0 or newer

Check failure on line 81 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'ProposedViewSize' is only available in tvOS 16.0 or newer

Check failure on line 81 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'ProposedViewSize' is only available in tvOS 16.0 or newer
uiViewController: Self.NSUIViewControllerType,
context: Self.Context
) -> CGSize? {
sizeThatFits(proposal, nsUIViewController: uiViewController, context: context)

Check failure on line 85 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=macOS,variant=Mac Catalyst)

'sizeThatFits(_:nsUIViewController:context:)' is only available in Mac Catalyst 16.0 or newer

Check failure on line 85 in Sources/NSUI/ViewControllerRepresentable.swift

View workflow job for this annotation

GitHub Actions / Test (platform=tvOS Simulator,name=Apple TV)

'sizeThatFits(_:nsUIViewController:context:)' is only available in tvOS 16.0 or newer
}
}
#endif

extension NSUIViewControllerRepresentable {
@MainActor
static func dismantleNSUIViewController(_ viewController: Self.NSUIViewControllerType, coordinator: Self.Coordinator) {
}

@available(iOS 16.0, tvOS 16.0, macOS 13.0, *)
@MainActor
func sizeThatFits(_ proposal: ProposedViewSize, nsUIViewController: Self.NSUIViewControllerType, context: Self.Context) -> CGSize? {
nil
}
}
28 changes: 28 additions & 0 deletions Tests/NSUITests/ViewControllerRepresentableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import XCTest
@testable import NSUI

@MainActor
struct RepresentedViewController: NSUIViewControllerRepresentable {
typealias NSUIViewControllerType = NSUIViewController

func makeNSUIViewController(context: Context) -> NSUIViewControllerType {
NSUIViewControllerType(nibName: nil, bundle: nil)
}

func updateNSUIViewController(_ viewController: NSUIViewControllerType, context: Context) {
}

final class Coordinator {}

func makeCoordinator() -> Coordinator {
Coordinator()
}
}

final class ViewControllerRepresentableTests: XCTestCase {
@MainActor
func testViewStructDefinition() throws {
let _ = RepresentedViewController()
}
}

0 comments on commit 5dd270d

Please sign in to comment.