|
| 1 | +// |
| 2 | +// AppKitColorConversions.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Status: Complete |
| 6 | +// ID: 7137BB7EE57FAC34F81DC437C151F7AB (SwiftUI) |
| 7 | + |
| 8 | +#if canImport(AppKit) |
| 9 | + |
| 10 | +public import OpenSwiftUICore |
| 11 | +public import AppKit |
| 12 | +import COpenSwiftUI |
| 13 | + |
| 14 | +// MARK: - NSColor Conversions [6.5.4] |
| 15 | + |
| 16 | +@available(iOS, unavailable) |
| 17 | +@available(macOS, introduced: 10.15, deprecated: 100000.0, message: "Use Color(nsColor:) when converting a NSColor, or create a standard Color directly") |
| 18 | +@available(tvOS, unavailable) |
| 19 | +@available(watchOS, unavailable) |
| 20 | +@available(visionOS, unavailable) |
| 21 | +extension Color { |
| 22 | + /// Creates a color from an AppKit color. |
| 23 | + /// |
| 24 | + /// Use this method to create a SwiftUI color from an |
| 25 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) instance. |
| 26 | + /// The new color preserves the adaptability of the original. |
| 27 | + /// For example, you can create a rectangle using |
| 28 | + /// [linkColor](https://developer.apple.com/documentation/AppKit/NSColor/linkColor) |
| 29 | + /// to see how the shade adjusts to match the user's system settings: |
| 30 | + /// |
| 31 | + /// struct Box: View { |
| 32 | + /// var body: some View { |
| 33 | + /// Color(NSColor.linkColor) |
| 34 | + /// .frame(width: 200, height: 100) |
| 35 | + /// } |
| 36 | + /// } |
| 37 | + /// |
| 38 | + /// The `Box` view defined above automatically changes its |
| 39 | + /// appearance when the user turns on Dark Mode. With the light and dark |
| 40 | + /// appearances placed side by side, you can see the subtle difference |
| 41 | + /// in shades: |
| 42 | + /// |
| 43 | + ///  |
| 46 | + /// |
| 47 | + /// > Note: Use this initializer only if you need to convert an existing |
| 48 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) to a |
| 49 | + /// SwiftUI color. Otherwise, create a OpenSwiftUI ``Color`` using an |
| 50 | + /// initializer like ``init(_:red:green:blue:opacity:)``, or use a system |
| 51 | + /// color like ``ShapeStyle/blue``. |
| 52 | + /// |
| 53 | + /// - Parameter color: An |
| 54 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) instance |
| 55 | + /// from which to create a color. |
| 56 | + @_disfavoredOverload |
| 57 | + nonisolated public init(_ color: NSColor) { |
| 58 | + self.init(nsColor: color) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +@available(OpenSwiftUI_v3_0, *) |
| 63 | +@available(iOS, unavailable) |
| 64 | +@available(tvOS, unavailable) |
| 65 | +@available(watchOS, unavailable) |
| 66 | +@available(visionOS, unavailable) |
| 67 | +extension Color { |
| 68 | + /// Creates a color from an AppKit color. |
| 69 | + /// |
| 70 | + /// Use this method to create a SwiftUI color from an |
| 71 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) instance. |
| 72 | + /// The new color preserves the adaptability of the original. |
| 73 | + /// For example, you can create a rectangle using |
| 74 | + /// [linkColor](https://developer.apple.com/documentation/AppKit/NSColor/linkColor) |
| 75 | + /// to see how the shade adjusts to match the user's system settings: |
| 76 | + /// |
| 77 | + /// struct Box: View { |
| 78 | + /// var body: some View { |
| 79 | + /// Color(nsColor: .linkColor) |
| 80 | + /// .frame(width: 200, height: 100) |
| 81 | + /// } |
| 82 | + /// } |
| 83 | + /// |
| 84 | + /// The `Box` view defined above automatically changes its |
| 85 | + /// appearance when the user turns on Dark Mode. With the light and dark |
| 86 | + /// appearances placed side by side, you can see the subtle difference |
| 87 | + /// in shades: |
| 88 | + /// |
| 89 | + ///  |
| 92 | + /// |
| 93 | + /// > Note: Use this initializer only if you need to convert an existing |
| 94 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) to a |
| 95 | + /// SwiftUI color. Otherwise, create a OpenSwiftUI ``Color`` using an |
| 96 | + /// initializer like ``init(_:red:green:blue:opacity:)``, or use a system |
| 97 | + /// color like ``ShapeStyle/blue``. |
| 98 | + /// |
| 99 | + /// - Parameter color: An |
| 100 | + /// [NSColor](https://developer.apple.com/documentation/AppKit/NSColor) instance |
| 101 | + /// from which to create a color. |
| 102 | + nonisolated public init(nsColor: NSColor) { |
| 103 | + self.init(provider: nsColor) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +private let dynamicColorCache: NSMapTable<ObjcColor, NSColor> = NSMapTable.strongToWeakObjects() |
| 108 | + |
| 109 | +extension NSColor: ColorProvider { |
| 110 | + @available(OpenSwiftUI_v2_0, *) |
| 111 | + @available(iOS, unavailable) |
| 112 | + @available(tvOS, unavailable) |
| 113 | + @available(watchOS, unavailable) |
| 114 | + @available(visionOS, unavailable) |
| 115 | + convenience public init(_ color: Color) { |
| 116 | + if let color = color.provider.as(NSColor.self) { |
| 117 | + self.init(color__openSwiftUI__: color) |
| 118 | + } else if let cgColor = color.provider.staticColor { |
| 119 | + self.init(cgColor: cgColor)! |
| 120 | + } else { |
| 121 | + let objCColor = ObjcColor(color) |
| 122 | + let cache = dynamicColorCache |
| 123 | + if let cachedColor = cache.object(forKey: objCColor) { |
| 124 | + self.init(color__openSwiftUI__: cachedColor) |
| 125 | + } else { |
| 126 | + self.init(name: nil) { appearance in |
| 127 | + var environment = EnvironmentValues() |
| 128 | + appearance.apply(to: &environment, vibrantBlendingStyle: ._1) |
| 129 | + environment.allowsVibrantBlending = false |
| 130 | + let resolved = color.resolve(in: environment) |
| 131 | + return resolved.kitColor as! NSColor |
| 132 | + } |
| 133 | + cache.setObject(self, forKey: objCColor) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + package func resolve(in environment: EnvironmentValues) -> Color.Resolved { |
| 139 | + if _NSColorDependsOnAppearance(self) { |
| 140 | + return withColorAppearance(in: environment) { |
| 141 | + Color.Resolved(platformColor: self) ?? .clear |
| 142 | + } |
| 143 | + } else { |
| 144 | + return Color.Resolved(cgColor) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private func withColorAppearance(in environment: EnvironmentValues, _ body: () -> Color.Resolved) -> Color.Resolved { |
| 149 | + let appearance = NSAppearance.appearance(from: environment, allowsVibrantBlending: false) |
| 150 | + var color = Color.Resolved.clear |
| 151 | + if let appearance { |
| 152 | + appearance.performAsCurrentDrawingAppearance { |
| 153 | + color = body() |
| 154 | + } |
| 155 | + } else { |
| 156 | + color = body() |
| 157 | + } |
| 158 | + return color |
| 159 | + } |
| 160 | + |
| 161 | + package var staticColor: CGColor? { |
| 162 | + if _NSColorDependsOnAppearance(self) { |
| 163 | + nil |
| 164 | + } else { |
| 165 | + cgColor |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#endif |
0 commit comments