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
1 change: 1 addition & 0 deletions Sources/COpenSwiftUI/Shims/UIKit/UIKit_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#import <UIKit/UIKit.h>

#if OPENSWIFTUI_LINK_BACKLIGHTSERVICES
#include <BacklightServices/BLSBacklightFBSSceneEnvironment.h>

Check warning on line 17 in Sources/COpenSwiftUI/Shims/UIKit/UIKit_Private.h

View workflow job for this annotation

GitHub Actions / Build for visionOS Simulator (26.6, 2024)

missing submodule 'BacklightServices.BLSBacklightFBSSceneEnvironment' [-Wincomplete-umbrella]
#endif

OPENSWIFTUI_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -55,6 +55,7 @@
@end

@interface UIView (OpenSwiftUI_SPI)
- (UITraitCollection *)_traitCollectionForChildEnvironment:(id<UITraitEnvironment>)childEnvironment OPENSWIFTUI_SWIFT_NAME(_traitCollection(forChildEnvironment:));
- (void)_noteTraitsDidChangeRecursively_openswiftui_safe_wrapper OPENSWIFTUI_SWIFT_NAME(_noteTraitsDidChangeRecursively());
@property(nonatomic, readonly, nullable) UIResponder *firstResponder_openswiftui_safe_wrapper OPENSWIFTUI_SWIFT_NAME(firstResponder);
- (nullable id<UILargeContentViewerItem>)_largeContentViewerItemAtPoint:(CGPoint)point OPENSWIFTUI_SWIFT_NAME(_largeContentViewerItem(at:));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,15 +1319,11 @@ extension NSHostingView: HitTestingLeafPlatformView {
extension NSHostingView: ViewRendererHost {
// FIXME
package func updateEnvironment() {
var environment: EnvironmentValues
if let inheritedEnvironment {
environment = inheritedEnvironment
} else if let initialInheritedEnvironment {
environment = initialInheritedEnvironment
} else {
environment = EnvironmentValues()
environment.configureForRoot()
}
let superviews = sequence(first: self as NSView, next: \.superview).dropFirst()
var environment = inheritedEnvironment
?? superviews.first(ofType: AnyPlatformViewHost.self)?.environment
?? initialInheritedEnvironment
?? EnvironmentValues().configuredForRoot()
if let environmentOverride {
environment.plist.override(with: environmentOverride.plist)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import OpenAttributeGraphShims

protocol AnyPlatformViewHost: AnyObject {
var responder: PlatformViewResponder? { get }
#if os(macOS)
var environment: EnvironmentValues { get }
var viewPhase: _GraphInputs.Phase { get }
var focusedValues: FocusedValues { get }
#endif
}

// MARK: - AnyPlatformViewProviderHost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ where Content: PlatformViewRepresentable {
}

#if os(iOS) || os(visionOS)
override func _traitCollection(forChildEnvironment childEnvironment: UITraitEnvironment) -> UITraitCollection {
if Content.isViewController, host?.as(UIViewControllerProvider.self)?.uiViewController != nil {
return super._traitCollection(forChildEnvironment: childEnvironment)
}
return traitCollection.byOverriding(
with: environment,
viewPhase: viewPhase,
focusedValues: focusedValues
)
}

override func didAddSubview(_ subview: PlatformView) {
super.didAddSubview(subview)
guard let viewController = representedViewProvider as? PlatformViewController else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ struct ViewLeafView<Content>: PrimitiveView, UnaryView where Content: PlatformVi
transaction: .init(),
environmentStorage: .lazy(environment, context)
)
let result: CGSize
var result: CGSize
if let fittingSize = content.sizeThatFits(
.init(proposedSize),
provider: representedViewProvider,
Expand All @@ -556,6 +556,11 @@ struct ViewLeafView<Content>: PrimitiveView, UnaryView where Content: PlatformVi
.fixingUnspecifiedDimensions(at: traits.idealSize)
.clamped(to: traits)
}
content.overrideSizeThatFits(
&result,
in: proposedSize,
platformView: representedViewProvider
)
}
size = result
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
//
// PlatformViewRepresentableCompatibilityTests.swift
// OpenSwiftUICompatibilityTests

#if os(iOS) || os(macOS) || os(visionOS)
import OpenSwiftUITestsSupport
import Testing
#if os(iOS) || os(visionOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif

@MainActor
struct PlatformViewRepresentableCompatibilityTests {
@Test
func nestedHostingViewInheritsFontChanges() async throws {
var inheritedFonts: [Font?] = []

struct ContentView: View {
let child: PlatformViewController
@State private var fontSize: CGFloat = 21

var body: some View {
RepresentedControllerView(controller: child)
.font(.system(size: fontSize))
.onAppear {
DispatchQueue.main.async {
fontSize = 28
}
}
}
}

try await triggerLayoutWithWindow { continuation in
var resumed = false
let child = PlatformHostingController(
rootView: FontReader { font in
inheritedFonts.append(font)
if font == .system(size: 28), !resumed {
resumed = true
continuation.resume()
}
}
)
return PlatformHostingController(rootView: ContentView(child: child))
}
#expect(inheritedFonts.first == .system(size: 21))
#expect(inheritedFonts.last == .system(size: 28))
}

@Test
func overrideSizeThatFitsAdjustsDefaultSize() async throws {
let recorder = SizingRecorder()
var measuredSizes: [CGSize] = []

struct ContentView: View {
let recorder: SizingRecorder
let onMeasure: (CGSize) -> Void
@State private var expanded = false

var body: some View {
SizingRepresentedView(
explicitSize: nil,
overrideSize: CGSize(width: expanded ? 190 : 174, height: 36),
recorder: recorder
)
.readSize(onMeasure)
.frame(width: expanded ? 220 : 200, height: 80)
.onAppear {
DispatchQueue.main.async {
expanded = true
}
}
}
}

try await triggerLayoutWithWindow { continuation in
var resumed = false
return PlatformHostingController(
rootView: ContentView(recorder: recorder) { size in
measuredSizes.append(size)
if size == CGSize(width: 190, height: 36), !resumed {
resumed = true
continuation.resume()
}
}
)
}
#expect(recorder.initialSizes.contains(CGSize(width: 80, height: 24)))
#expect(measuredSizes.contains(CGSize(width: 174, height: 36)))
#expect(measuredSizes.last == CGSize(width: 190, height: 36))
#expect(recorder.proposals.first != recorder.proposals.last)
}

@Test
func explicitSizeThatFitsBypassesOverrideSizeThatFits() async throws {
let recorder = SizingRecorder()
var measuredSize: CGSize = .zero
let root = SizingRepresentedView(
explicitSize: CGSize(width: 120, height: 30),
overrideSize: CGSize(width: 174, height: 36),
recorder: recorder
)
.readSize { measuredSize = $0 }
.frame(width: 200, height: 80)

try await triggerLayoutWithWindow(expectedCount: 0) { _ in
PlatformHostingController(rootView: root)
}
#expect(recorder.initialSizes.isEmpty)
#expect(measuredSize == CGSize(width: 120, height: 30))
}

private final class SizingRecorder {
var initialSizes: [CGSize] = []
var proposals: [_ProposedSize] = []
}

private final class IntrinsicSizedView: PlatformView {
override var intrinsicContentSize: CGSize {
CGSize(width: 80, height: 24)
}
}

private struct SizingRepresentedView: PlatformViewRepresentable {
let explicitSize: CGSize?
let overrideSize: CGSize
let recorder: SizingRecorder

private func makeView() -> IntrinsicSizedView {
let view = IntrinsicSizedView(frame: .zero)
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
view.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
view.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
return view
}

private func recordOverride(_ size: inout CGSize, in proposedSize: _ProposedSize) {
recorder.initialSizes.append(size)
recorder.proposals.append(proposedSize)
size = overrideSize
}

#if os(macOS)
func makeNSView(context: Context) -> IntrinsicSizedView { makeView() }
func updateNSView(_ nsView: IntrinsicSizedView, context: Context) {}

func sizeThatFits(
_ proposal: ProposedViewSize,
nsView: IntrinsicSizedView,
context: Context
) -> CGSize? {
explicitSize
}

func _overrideSizeThatFits(
_ size: inout CGSize,
in proposedSize: _ProposedSize,
nsView: IntrinsicSizedView
) {
recordOverride(&size, in: proposedSize)
}
#else
func makeUIView(context: Context) -> IntrinsicSizedView { makeView() }
func updateUIView(_ uiView: IntrinsicSizedView, context: Context) {}

func sizeThatFits(
_ proposal: ProposedViewSize,
uiView: IntrinsicSizedView,
context: Context
) -> CGSize? {
explicitSize
}

func _overrideSizeThatFits(
_ size: inout CGSize,
in proposedSize: _ProposedSize,
uiView: IntrinsicSizedView
) {
recordOverride(&size, in: proposedSize)
}
#endif
}

private struct RepresentedControllerView: PlatformViewRepresentable {
let controller: PlatformViewController

#if os(macOS)
func makeNSView(context: Context) -> NSView { controller.view }
func updateNSView(_ nsView: NSView, context: Context) {}
#else
func makeUIView(context: Context) -> UIView { controller.view }
func updateUIView(_ uiView: UIView, context: Context) {}
#endif
}

private struct FontReader: View {
@Environment(\.font) private var font
var read: (Font?) -> Void

var body: some View {
let _ = read(font)
Color.clear
}
}
}

extension View {
fileprivate func readSize(_ perform: @escaping (CGSize) -> Void) -> some View {
background {
GeometryReader { proxy in
let _ = perform(proxy.size)
Color.clear
}
}
}
}
#endif
Loading