diff --git a/Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift b/Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift index f367c712c..7e239f7ec 100644 --- a/Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift +++ b/Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift @@ -4,7 +4,7 @@ import SwiftUI struct AttributedStringRenderer { struct State { var font: MarkdownStyle.Font - var foregroundColor: MarkdownStyle.Color + var foregroundColor: SwiftUI.Color var paragraphSpacing: CGFloat var headIndent: CGFloat = 0 var tailIndent: CGFloat = 0 @@ -343,7 +343,7 @@ extension AttributedStringRenderer { attributes: [ .font: state.font.resolve(), .strikethroughStyle: NSUnderlineStyle.single.rawValue, - .strikethroughColor: MarkdownStyle.Color.separator.platformColor!, + .strikethroughColor: PlatformColor.separator, ] ) ) @@ -425,7 +425,7 @@ extension AttributedStringRenderer { string: text, attributes: [ .font: state.font.resolve(), - .foregroundColor: state.foregroundColor.platformColor!, + .foregroundColor: PlatformColor(state.foregroundColor), ] ) } @@ -549,3 +549,15 @@ extension NSTextAlignment { } } } + +// MARK: - PlatformColor + +#if os(macOS) + private typealias PlatformColor = NSColor + + extension NSColor { + fileprivate static var separator: NSColor { .separatorColor } + } +#elseif os(iOS) || os(tvOS) + private typealias PlatformColor = UIColor +#endif diff --git a/Sources/MarkdownUI/Style/Color.swift b/Sources/MarkdownUI/Style/Color.swift deleted file mode 100644 index 963955621..000000000 --- a/Sources/MarkdownUI/Style/Color.swift +++ /dev/null @@ -1,145 +0,0 @@ -import SwiftUI - -extension MarkdownStyle { - /// A representation of a color that you can use to style a ``Markdown`` view. - /// - /// This type mimics the `SwiftUI.Color` API and provides the `Markdown` view implementation - /// access to the underlying platform color. - public struct Color: Hashable { - var platformColor: PlatformColor? - } -} - -extension MarkdownStyle.Color { - /// A context-dependent red color suitable for use in UI elements. - public static let red = Self(platformColor: .systemRed) - - /// A context-dependent orange color suitable for use in UI elements. - public static let orange = Self(platformColor: .systemOrange) - - /// A context-dependent yellow color suitable for use in UI elements. - public static let yellow = Self(platformColor: .systemYellow) - - /// A context-dependent green color suitable for use in UI elements. - public static let green = Self(platformColor: .systemGreen) - - /// A context-dependent teal color suitable for use in UI elements. - public static let teal = Self(platformColor: .systemTeal) - - /// A context-dependent blue color suitable for use in UI elements. - public static let blue = Self(platformColor: .systemBlue) - - /// A context-dependent indigo color suitable for use in UI elements. - public static let indigo = Self(platformColor: .systemIndigo) - - /// A context-dependent purple color suitable for use in UI elements. - public static let purple = Self(platformColor: .systemPurple) - - /// A context-dependent pink color suitable for use in UI elements. - public static let pink = Self(platformColor: .systemPink) - - /// A white color suitable for use in UI elements. - public static let white = Self(platformColor: .white) - - /// A context-dependent gray color suitable for use in UI elements. - public static let gray = Self(platformColor: .systemGray) - - /// A black color suitable for use in UI elements. - public static let black = Self(platformColor: .black) - - /// A clear color suitable for use in UI elements. - public static let clear = Self(platformColor: .clear) - - #if os(macOS) - /// The color to use for primary content. - public static let primary = Self(platformColor: .labelColor) - #elseif os(iOS) || os(tvOS) - /// The color to use for primary content. - public static let primary = Self(platformColor: .label) - #endif - - #if os(macOS) - /// The color to use for secondary content. - public static let secondary = Self(platformColor: .secondaryLabelColor) - #elseif os(iOS) || os(tvOS) - /// The color to use for secondary content. - public static let secondary = Self(platformColor: .secondaryLabel) - #endif - - #if os(macOS) - /// The color to use for separators between different sections of content. - public static let separator = Self(platformColor: .separatorColor) - #elseif os(iOS) || os(tvOS) - /// The color to use for separators between different sections of content. - public static let separator = Self(platformColor: .separator) - #endif - - /// Creates a color from a Core Graphics color. - public init(cgColor: CGColor) { - self.init(platformColor: .init(cgColor: cgColor)) - } - - /// Creates a constant color from red, green, and blue component values. - public init(red: CGFloat, green: CGFloat, blue: CGFloat, opacity: CGFloat = 1) { - self.init(platformColor: .init(red: red, green: green, blue: blue, alpha: opacity)) - } - - /// Creates a constant grayscale color. - public init(white: CGFloat, opacity: CGFloat = 1) { - self.init(platformColor: .init(white: white, alpha: opacity)) - } - - /// Creates a constant color from hue, saturation, and brightness values. - public init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, opacity: CGFloat = 1) { - self.init( - platformColor: .init( - hue: hue, saturation: saturation, brightness: brightness, alpha: opacity - ) - ) - } - - /// Creates a color from a color set that you indicate by name. - public init(_ name: String, bundle: Bundle? = nil) { - #if os(macOS) - self.init(platformColor: .init(named: name, bundle: bundle)) - #elseif os(iOS) || os(tvOS) - self.init(platformColor: .init(named: name, in: bundle, compatibleWith: nil)) - #endif - } - - public func opacity(_ opacity: CGFloat) -> MarkdownStyle.Color { - .init(platformColor: self.platformColor?.withAlphaComponent(opacity)) - } -} - -#if os(macOS) - @available(macOS 11.0, *) - @available(iOS, unavailable) - @available(tvOS, unavailable) - extension MarkdownStyle.Color { - /// Creates a color from an AppKit color. - public init(nsColor: NSColor) { - self.init(platformColor: nsColor) - } - } -#endif - -#if os(iOS) || os(tvOS) - @available(macOS, unavailable) - @available(iOS 14.0, *) - @available(tvOS 14.0, *) - extension MarkdownStyle.Color { - /// Creates a color from an UIKit color. - public init(uiColor: UIColor) { - self.init(platformColor: uiColor) - } - } -#endif - -// MARK: - PlatformColor - -#if os(macOS) - typealias PlatformColor = NSColor -#elseif os(iOS) || os(tvOS) - typealias PlatformColor = UIColor -#endif diff --git a/Sources/MarkdownUI/Style/MarkdownStyle.swift b/Sources/MarkdownUI/Style/MarkdownStyle.swift index ce75a87e5..43cd6ef70 100644 --- a/Sources/MarkdownUI/Style/MarkdownStyle.swift +++ b/Sources/MarkdownUI/Style/MarkdownStyle.swift @@ -6,7 +6,7 @@ public struct MarkdownStyle: Hashable { public var font: MarkdownStyle.Font /// The text color. - public var foregroundColor: MarkdownStyle.Color + public var foregroundColor: SwiftUI.Color /// The measurements of the Markdown elements. public var measurements: Measurements @@ -18,7 +18,7 @@ public struct MarkdownStyle: Hashable { /// - measurements: The measurements of the Markdown elements. public init( font: MarkdownStyle.Font = .body, - foregroundColor: MarkdownStyle.Color = .primary, + foregroundColor: SwiftUI.Color = .primary, measurements: MarkdownStyle.Measurements = .init() ) { self.font = font diff --git a/Tests/MarkdownUITests/ColorTests.swift b/Tests/MarkdownUITests/ColorTests.swift deleted file mode 100644 index 2a38b5cc0..000000000 --- a/Tests/MarkdownUITests/ColorTests.swift +++ /dev/null @@ -1,66 +0,0 @@ -import XCTest - -@testable import MarkdownUI - -final class ColorTests: XCTestCase { - func testStandardColors() { - XCTAssertEqual(MarkdownStyle.Color.red.platformColor, .systemRed) - XCTAssertEqual(MarkdownStyle.Color.orange.platformColor, .systemOrange) - XCTAssertEqual(MarkdownStyle.Color.yellow.platformColor, .systemYellow) - XCTAssertEqual(MarkdownStyle.Color.green.platformColor, .systemGreen) - XCTAssertEqual(MarkdownStyle.Color.teal.platformColor, .systemTeal) - XCTAssertEqual(MarkdownStyle.Color.blue.platformColor, .systemBlue) - XCTAssertEqual(MarkdownStyle.Color.indigo.platformColor, .systemIndigo) - XCTAssertEqual(MarkdownStyle.Color.purple.platformColor, .systemPurple) - XCTAssertEqual(MarkdownStyle.Color.pink.platformColor, .systemPink) - XCTAssertEqual(MarkdownStyle.Color.white.platformColor, .white) - XCTAssertEqual(MarkdownStyle.Color.gray.platformColor, .systemGray) - XCTAssertEqual(MarkdownStyle.Color.black.platformColor, .black) - XCTAssertEqual(MarkdownStyle.Color.clear.platformColor, .clear) - #if os(macOS) - XCTAssertEqual(MarkdownStyle.Color.primary.platformColor, .labelColor) - XCTAssertEqual(MarkdownStyle.Color.secondary.platformColor, .secondaryLabelColor) - #else - XCTAssertEqual(MarkdownStyle.Color.primary.platformColor, .label) - XCTAssertEqual(MarkdownStyle.Color.secondary.platformColor, .secondaryLabel) - #endif - } - - func testColorCreation() { - XCTAssertEqual( - MarkdownStyle.Color(cgColor: .init(gray: 0.5, alpha: 1)).platformColor, - .init(cgColor: .init(gray: 0.5, alpha: 1)) - ) - XCTAssertEqual( - MarkdownStyle.Color(red: 1, green: 0, blue: 0, opacity: 0.5).platformColor, - .init(red: 1, green: 0, blue: 0, alpha: 0.5) - ) - XCTAssertEqual( - MarkdownStyle.Color(white: 0.25, opacity: 0.5).platformColor, - .init(white: 0.25, alpha: 0.5) - ) - XCTAssertEqual( - MarkdownStyle.Color(hue: 0.25, saturation: 0.5, brightness: 0.75, opacity: 0.5) - .platformColor, - .init(hue: 0.25, saturation: 0.5, brightness: 0.75, alpha: 0.5) - ) - #if os(macOS) - XCTAssertEqual( - MarkdownStyle.Color("test", bundle: .module).platformColor!, - .init(named: "test", bundle: .module)! - ) - #else - XCTAssertEqual( - MarkdownStyle.Color("test", bundle: .module).platformColor!, - .init(named: "test", in: .module, compatibleWith: nil)! - ) - #endif - } - - func testOpacity() { - XCTAssertEqual( - MarkdownStyle.Color.white.opacity(0.25).platformColor, - .white.withAlphaComponent(0.25) - ) - } -}