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
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
buildConfiguration = "OpenSwiftUIDebug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:OpenSwiftUIUITests/OpenSwiftUIUITests.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
<Testables>
<TestableReference
skipped = "NO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
buildConfiguration = "OpenSwiftUIDebug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:OpenSwiftUIUITests/OpenSwiftUIUITests.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
buildConfiguration = "OpenSwiftUIDebug"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
buildConfiguration = "SwiftUIDebug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:OpenSwiftUIUITests/OpenSwiftUIUITests.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
<Testables>
<TestableReference
skipped = "NO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
buildConfiguration = "SwiftUIDebug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:OpenSwiftUIUITests/OpenSwiftUIUITests.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
buildConfiguration = "SwiftUIDebug"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// OpenSwiftUIUITests

import Testing
import TestingHost
import SnapshotTesting

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// OpenSwiftUIUITests

import Foundation
import TestingHost

struct AnimationTestModel: Hashable {
var intervals: [Double]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,14 @@

import SnapshotTesting
import Testing
import TestingHost
import Foundation

#if os(macOS)
import AppKit
typealias PlatformHostingController = NSHostingController
typealias PlatformHostingView = NSHostingView
typealias PlatformViewController = NSViewController
typealias PlatformView = NSView
typealias PlatformImage = NSImage
typealias PlatformColor = NSColor
extension Color {
init(platformColor: PlatformColor) {
self.init(nsColor: platformColor)
}
}
#elseif os(iOS) || os(visionOS)
import UIKit
typealias PlatformHostingController = UIHostingController
typealias PlatformHostingView = _UIHostingView
typealias PlatformViewController = UIViewController
typealias PlatformView = UIView
typealias PlatformImage = UIImage
typealias PlatformColor = UIColor
extension Color {
init(platformColor: PlatformColor) {
self.init(uiColor: platformColor)
}
}
#endif

let defaultSize = CGSize(width: 200, height: 200)

#if os(macOS)
import AppKit

extension Snapshotting where Value == NSViewController, Format == NSImage {
/// drawHierarchyInKeyWindow is iOS only parameter, here for compatibility
static func image(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Foundation
import SnapshotTesting
import Testing
import TestingHost

@MainActor
@Suite(.snapshots(record: .never, diffTool: diffTool))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ImageConversionsUITests.swift
// OpenSwiftUIUITests

import SnapshotTesting
import Testing
@testable import TestingHost

@MainActor
@Suite(.snapshots(record: .never, diffTool: diffTool))
struct ImageConversionsUITests {
@Test("Test Image(platformImage:) with named image")
func platformImageNamed() {
openSwiftUIAssertSnapshot(of: ImageConversionsExample())
}

@Test("Test Image(platformImage:) with system image")
func platformImageSystem() {
openSwiftUIAssertSnapshot(of: ImageConversionsSystemImageExample())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
{
"filename" : "logo.png",
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"filename" : "logo_dark.png",
"idiom" : "universal"
}
],
"info" : {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Example/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import SwiftUI

struct ContentView: View {
var body: some View {
NamedColorExample()
ImageConversionsExample()
}
}
30 changes: 0 additions & 30 deletions Example/Shared/Extension/Color+Platform.swift

This file was deleted.

50 changes: 50 additions & 0 deletions Example/Shared/Extension/Platform+TypeAlias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Platform+TypeAlias.swift
// Shared
//
// Created by Kyle on 2025/7/20.
//

#if OPENSWIFTUI
public import OpenSwiftUI
#else
public import SwiftUI
#endif

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
public import AppKit
public typealias PlatformColor = NSColor
public typealias PlatformImage = NSImage
public typealias PlatformView = NSView
public typealias PlatformViewController = NSViewController
public typealias PlatformHostingController = NSHostingController
public typealias PlatformHostingView = NSHostingView
#elseif canImport(UIKit)
public import UIKit
public typealias PlatformColor = UIColor
public typealias PlatformImage = UIImage
public typealias PlatformView = UIView
public typealias PlatformViewController = UIViewController
public typealias PlatformHostingController = UIHostingController
public typealias PlatformHostingView = _UIHostingView
#endif

extension Color {
public init(platformColor: PlatformColor) {
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
self.init(nsColor: platformColor)
#elseif canImport(UIKit)
self.init(uiColor: platformColor)
#endif
}
}

extension Image {
public init(platformImage: PlatformImage) {
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
self.init(nsImage: platformImage)
#elseif canImport(UIKit)
self.init(uiImage: platformImage)
#endif
}
}
76 changes: 76 additions & 0 deletions Example/Shared/View/Image/ImageConversionsExample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// ImageConversionsExample.swift
// Shared

#if OPENSWIFTUI
import OpenSwiftUI
#else
import SwiftUI
#endif

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif

struct ImageConversionsExample: View {
var body: some View {
HStack(spacing: 0) {
VStack(spacing: 0) {
Image("logo")
.resizable()
.frame(width: 100, height: 100)
Image(platformImage: PlatformImage(named: "logo")!)
.resizable()
.frame(width: 100, height: 100)
}.environment(\.colorScheme, .light)
VStack(spacing: 0) {
Image("logo")
.resizable()
.frame(width: 100, height: 100)
Image(platformImage: PlatformImage(named: "logo")!)
.resizable()
.frame(width: 100, height: 100)
}.environment(\.colorScheme, .dark)
}
}
}

struct ImageConversionsSystemImageExample: View {
private var image: PlatformImage {
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
NSImage(named: NSImage.addTemplateName)!
#elseif canImport(UIKit)
UIImage(systemName: "star.fill")!
#endif
}

var body: some View {
HStack(spacing: 0) {
VStack(spacing: 0) {
Image(platformImage: image)
.resizable()
.frame(width: 100, height: 100)
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
Image(platformImage: image)
.resizable()
.frame(width: 100, height: 100)
#else
Image(platformImage: image.withRenderingMode(.alwaysTemplate))
.resizable()
.frame(width: 100, height: 100)
#endif
}.environment(\.colorScheme, .light)
VStack(spacing: 0) {
Image(platformImage: image)
.resizable()
.frame(width: 100, height: 100)
Image(platformImage: image)
.resizable()
.renderingMode(.template)
.frame(width: 100, height: 100)
}.environment(\.colorScheme, .dark)
}.foregroundStyle(.red)
}
}
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Sources/COpenSwiftUI/Shims/AppKit/AppKit_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ typedef OPENSWIFTUI_ENUM(NSInteger, NSViewVibrantBlendingStyle) {
+ (void)_setUsingModernDocuments_openswiftui_safe_wrapper:(BOOL)value OPENSWIFTUI_SWIFT_NAME(_setUsingModernDocuments(_:));
@end

@interface NSImage (OpenSwiftUI_SPI)
@property (nullable, copy) NSString *_defaultAccessibilityDescription;
@end

OPENSWIFTUI_ASSUME_NONNULL_END

#endif /* __has_include(<AppKit/AppKit.h>) */
Expand Down
9 changes: 9 additions & 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 / Execute compatibility tests for iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute compatibility tests for iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute compatibility tests for iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute compatibility tests for iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute tests on iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute tests on iOS (macos-15, 16.4, 2024, 18.5)

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

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

View workflow job for this annotation

GitHub Actions / Execute tests on iOS (macos-15, 16.4, 2024, 18.5)

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

OPENSWIFTUI_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -88,6 +88,15 @@
#endif
@end

@interface UIImage (OpenSwiftUI_SPI)
@property (nonatomic, readonly) BOOL _hasImageAsset;
@property (nonatomic, readonly, nullable) IOSurfaceRef ioSurface;
@property (nonatomic, readonly) NSDirectionalEdgeInsets contentInsets;
+ (nullable UIImage *)imageNamed:(NSString *)name inBundle:(nullable NSBundle *)bundle OPENSWIFTUI_SWIFT_NAME(init(named:in:));
+ (nullable UIImage *)_systemImageNamed:(NSString *)name OPENSWIFTUI_SWIFT_NAME(init(_systemName:));
+ (nullable UIImage *)_systemImageNamed:(NSString *)name variableValue:(double)value withConfiguration:(nullable UIImageConfiguration *)configuration OPENSWIFTUI_SWIFT_NAME(init(_systemName:variableValue:configuration:));
@end

@interface UITraitCollection (OpenSwiftUI_SPI)
@property (nonatomic, readonly, nullable) NSObject *_environmentWrapper_openswiftui_safe_wrapper OPENSWIFTUI_SWIFT_NAME(_environmentWrapper);
@end
Expand Down
9 changes: 9 additions & 0 deletions Sources/OpenSwiftUI/Data/Environment/UIKitEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ extension UITraitCollection {
return resolvedTraitCollection(with: environment, wrapper: wrapper)
}

@inline(__always)
package func resolvedImageAssetOnlyTraitCollection(environment: EnvironmentValues) -> UITraitCollection {
resolvedTraitCollection(
with: environment,
wrapper: unsafeBitCast(_environmentWrapper, to: EnvironmentWrapper?.self),
forImageAssetsOnly: true
)
}

private func resolvedTraitCollection(
with environment: EnvironmentValues,
wrapper: EnvironmentWrapper?,
Expand Down
Loading
Loading