Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Example/HostingExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ class ViewController: NSViewController {

struct ContentView: View {
var body: some View {
ColorRepresentableExample()
ColorViewControllerRepresentableExample()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// PlatformViewControllerRepresentableUITests.swift
// OpenSwiftUIUITests

import Testing
import SnapshotTesting

#if os(iOS)
import UIKit
typealias PlatformViewControllerRepresentable = UIViewControllerRepresentable
#elseif os(macOS)
import AppKit
typealias PlatformViewControllerRepresentable = NSViewControllerRepresentable
#endif

@MainActor
@Suite(.snapshots(record: .never, diffTool: diffTool))
struct PlatformViewControllerRepresentableUITests {
@Test
func plainColorView() {
struct PlainColorView: PlatformViewControllerRepresentable {
#if os(iOS)
func makeUIViewController(context: Context) -> some UIViewController {
let vc = UIViewController()
vc.view.backgroundColor = .red
return vc
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
#elseif os(macOS)
func makeNSViewController(context: Context) -> some NSViewController {
let vc = NSViewController()
vc.view.wantsLayer = true
vc.view.layer?.backgroundColor = NSColor.red.cgColor
return vc
}

func updateNSViewController(_ nsViewController: NSViewControllerType, context: Context) {}
#endif
}
struct ContentView: View {
var body: some View {
PlainColorView()
}
}
openSwiftUIAssertSnapshot(of: ContentView())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct PlatformViewRepresentableUITests {
return v
}

func updateNSView(_ uiView: NSViewType, context: Context) {}
func updateNSView(_ nsView: NSViewType, context: Context) {}
#endif
}
struct ContentView: View {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ColorRepresentable.swift
// ColorRepresentableExample.swift
// SharedExample

#if OPENSWIFTUI
Expand All @@ -11,12 +11,14 @@ import SwiftUI
#if os(iOS)
import UIKit
typealias PlatformViewRepresentable = UIViewRepresentable
typealias PlatformViewControllerRepresentable = UIViewControllerRepresentable
#elseif os(macOS)
import AppKit
typealias PlatformViewRepresentable = NSViewRepresentable
typealias PlatformViewControllerRepresentable = NSViewControllerRepresentable
#endif

struct ColorRepresentableExample: PlatformViewRepresentable {
struct ColorViewRepresentableExample: PlatformViewRepresentable {
#if os(iOS)
func makeUIView(context: Context) -> some UIView {
let v = UIView()
Expand All @@ -33,6 +35,26 @@ struct ColorRepresentableExample: PlatformViewRepresentable {
return v
}

func updateNSView(_ uiView: NSViewType, context: Context) {}
func updateNSView(_ nsView: NSViewType, context: Context) {}
#endif
}

struct ColorViewControllerRepresentableExample: PlatformViewControllerRepresentable {
#if os(iOS)
func makeUIViewController(context: Context) -> some UIViewController {
let vc = UIViewController()
vc.view.backgroundColor = .red
return vc
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
#elseif os(macOS)
func makeNSViewController(context: Context) -> some NSViewController {
let vc = NSViewController()
vc.view.wantsLayer = true
vc.view.layer?.backgroundColor = NSColor.red.cgColor
return vc
}

func updateNSViewController(_ nsViewController: NSViewControllerType, context: Context) {}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ open class NSHostingView<Content>: NSView, XcodeViewDebugDataProvider where Cont
needsUpdateConstraints = true
needsLayout = true
}

@objc(swiftui_addRenderedSubview:positioned:relativeTo:) // FIXME: ViewUpdater -> AppKitAddSubview
private func openswiftui_addRenderedSubview(_ view: NSView, positioned place: NSWindow.OrderingMode, relativeTo otherView: NSView?) {
isInsertingRenderedSubview = true
addSubview(view, positioned: place, relativeTo: otherView)
isInsertingRenderedSubview = false
}
}

@available(iOS, unavailable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// IsInHostingConfiguration.swift
// OpenSwiftUI
//
// Audited for 6.5.4
// Status: Complete

import OpenSwiftUICore

struct IsInHostingConfiguration: ViewInputBoolFlag {}

extension _GraphInputs {
@inline(__always)
var isInHostingConfiguration: Bool {
get { IsInHostingConfiguration.evaluate(inputs: self) }
set { self[IsInHostingConfiguration.self] = newValue }
}
}

extension _ViewInputs {
@inline(__always)
var isInHostingConfiguration: Bool {
get { base.isInHostingConfiguration }
set { base.isInHostingConfiguration = newValue }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ open class _UIHostingView<Content>: UIView, XcodeViewDebugDataProvider where Con
inputs.base.options.insert(.supportsVariableFrameDuration)
}
}

@objc(swiftui_insertRenderedSubview:atIndex:) // FIXME: ViewUpdater -> CoreViewAddSubview
private func openswiftui_insertRenderedSubview(_ view: UIView, at index: Int) {
isInsertingRenderedSubview = true
insertSubview(view, at: index)
isInsertingRenderedSubview = false
}
}

extension _UIHostingView {
Expand Down Expand Up @@ -470,8 +477,12 @@ extension _UIHostingView {
extension _UIHostingView: ViewRendererHost {
package func `as`<T>(_ type: T.Type) -> T? {
guard let value = base.as(type) else {
// TODO
return nil
// FocusHost
if UIViewControllerProvider.self == type {
return unsafeBitCast(self as any UIViewControllerProvider, to: T.self)
} else { // TODO
return nil
}
}
return value
}
Expand Down Expand Up @@ -690,4 +701,12 @@ extension _UIHostingView: UIHostingViewBaseDelegate {
}
}

// MARK: - _UIHostingView + UIViewControllerProvider [6.5.4]

extension _UIHostingView: UIViewControllerProvider {
var uiViewController: UIViewController? {
viewController
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// UIViewControllerProvider.swift
// OpenSwiftUI
//
// Audited for 6.5.4
// Status: Complete

#if os(iOS)

import UIKit

protocol UIViewControllerProvider: AnyObject {
var uiViewController: UIViewController? { get }
}

extension UIViewControllerProvider {
var containingViewController: UIViewController? {
if let uiViewController {
return uiViewController
} else if let view = self as? UIView {
return view._viewControllerForAncestor
} else {
return nil
}
}
}

#endif
Loading
Loading