From a58bb59546ae3caa5d4b304ed0bd5caddc5dd01b Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 20 Jul 2025 03:37:54 +0800 Subject: [PATCH 1/2] Add Color+Platform extension --- .../Extension/Color+Platform.swift | 30 +++++++++++++++++++ .../AppearanceActionModifierExample.swift | 10 +------ .../SharedExample/View/NamespaceExample.swift | 11 +------ 3 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 Example/SharedExample/Extension/Color+Platform.swift diff --git a/Example/SharedExample/Extension/Color+Platform.swift b/Example/SharedExample/Extension/Color+Platform.swift new file mode 100644 index 000000000..5b65b4cf1 --- /dev/null +++ b/Example/SharedExample/Extension/Color+Platform.swift @@ -0,0 +1,30 @@ +// +// Color+Platform.swift +// SharedExample +// +// Created by Kyle on 2025/7/20. +// + +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif + +#if os(iOS) +import UIKit +typealias PlatformColor = UIColor +#elseif os(macOS) +import AppKit +typealias PlatformColor = NSColor +#endif + +extension Color { + init(platformColor: PlatformColor) { + #if os(iOS) + self.init(uiColor: platformColor) + #elseif os(macOS) + self.init(nsColor: platformColor) + #endif + } +} diff --git a/Example/SharedExample/View/AppearanceActionModifierExample.swift b/Example/SharedExample/View/AppearanceActionModifierExample.swift index b54fc443c..a8d98f17e 100644 --- a/Example/SharedExample/View/AppearanceActionModifierExample.swift +++ b/Example/SharedExample/View/AppearanceActionModifierExample.swift @@ -12,16 +12,8 @@ import Foundation struct AppearanceActionModifierExample: View { @State private var first = true - var color: Color { - #if os(macOS) - Color(nsColor: first ? .red : .blue) - #else - Color(uiColor: first ? .red : .blue) - #endif - } - var body: some View { - color + Color(platformColor: first ? .red : .blue) .onAppear { print("View appear") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { diff --git a/Example/SharedExample/View/NamespaceExample.swift b/Example/SharedExample/View/NamespaceExample.swift index 99f5408d4..9c442817f 100644 --- a/Example/SharedExample/View/NamespaceExample.swift +++ b/Example/SharedExample/View/NamespaceExample.swift @@ -13,17 +13,8 @@ struct NamespaceExample: View { @State private var first = true @Namespace private var id - var color: Color { - #if os(macOS) - Color.red - #else - - Color(uiColor: first ? .red : .blue) - #endif - } - var body: some View { - color + Color(platformColor: first ? .red : .blue) .onAppear { print("View appear \(id)") DispatchQueue.main.asyncAfter(deadline: .now() + 1) { From a6e6a95dd4dc39c7e31a634913131bca9c6d40bb Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 19 Jul 2025 00:04:41 +0800 Subject: [PATCH 2/2] Add ColorAnimationExample --- Example/HostingExample/ViewController.swift | 2 +- .../Animation/ColorAnimationExample.swift | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Example/SharedExample/Animation/ColorAnimationExample.swift diff --git a/Example/HostingExample/ViewController.swift b/Example/HostingExample/ViewController.swift index 5b84729f2..f7b8e69b4 100644 --- a/Example/HostingExample/ViewController.swift +++ b/Example/HostingExample/ViewController.swift @@ -66,6 +66,6 @@ class ViewController: NSViewController { struct ContentView: View { var body: some View { - EquatableDemoView(count: 0, tag: 0) + ColorAnimationExample() } } diff --git a/Example/SharedExample/Animation/ColorAnimationExample.swift b/Example/SharedExample/Animation/ColorAnimationExample.swift new file mode 100644 index 000000000..20c4a21f7 --- /dev/null +++ b/Example/SharedExample/Animation/ColorAnimationExample.swift @@ -0,0 +1,28 @@ +// +// ColorAnimationExample.swift +// SharedExample +// +// Created by Kyle on 2025/7/20. +// +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif +import Foundation + +struct ColorAnimationExample: View { + @State private var showRed = false + var body: some View { + VStack { + Color(platformColor: showRed ? .red : .blue) + .frame(width: showRed ? 200 : 400, height: showRed ? 200 : 400) + } + .animation(.easeInOut(duration: 2), value: showRed) + .onAppear { + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + showRed.toggle() + } + } + } +}