diff --git a/Sources/CodeEditor/CodeEditor.swift b/Sources/CodeEditor/CodeEditor.swift index e88f87c..3db525a 100644 --- a/Sources/CodeEditor/CodeEditor.swift +++ b/Sources/CodeEditor/CodeEditor.swift @@ -241,7 +241,7 @@ public struct CodeEditor: View { inset : CGSize? = nil, allowsUndo : Bool = true, autoscroll : Bool = true, - backgroundColor: NSColor? = nil) + backgroundColor: Color? = nil) { self.source = source self.selection = selection @@ -294,7 +294,7 @@ public struct CodeEditor: View { autoPairs : [ String : String ]? = nil, inset : CGSize? = nil, allowsUndo : Bool = true, - backgroundColor: NSColor? = nil) + backgroundColor: Color? = nil) { assert(!flags.contains(.editable), "Editing requires a Binding") self.init(source : .constant(source), @@ -320,7 +320,7 @@ public struct CodeEditor: View { private let inset : CGSize private let allowsUndo : Bool private let autoscroll : Bool - private let backgroundColor : NSColor? + private let backgroundColor : Color? public var body: some View { UXCodeTextViewRepresentable(source : source, diff --git a/Sources/CodeEditor/UXCodeTextView.swift b/Sources/CodeEditor/UXCodeTextView.swift index 7c680ae..f314134 100644 --- a/Sources/CodeEditor/UXCodeTextView.swift +++ b/Sources/CodeEditor/UXCodeTextView.swift @@ -7,6 +7,7 @@ // import Highlightr +import SwiftUI #if os(macOS) import AppKit @@ -30,7 +31,7 @@ final class UXCodeTextView: UXTextView { fileprivate let highlightr = Highlightr() - var customBackgroundColor: NSColor? = nil + var customBackgroundColor: Color? = nil private var hlTextStorage : CodeAttributedString? { return textStorage as? CodeAttributedString @@ -247,7 +248,12 @@ final class UXCodeTextView: UXTextView { guard let highlightr = highlightr, highlightr.setTheme(to: newTheme.rawValue), let theme = highlightr.theme else { return false } - self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor + let bgColor = customBackgroundColor ?? Color(theme.themeBackgroundColor) + #if os(macOS) + self.backgroundColor = bgColor.nsColor() + #else + self.backgroundColor = bgColor.uiColor() + #endif if let font = theme.codeFont, font !== self.font { self.font = font } return true } @@ -266,7 +272,12 @@ final class UXCodeTextView: UXTextView { theme.codeFont = theme.codeFont? .withSize(newSize) theme.boldCodeFont = theme.boldCodeFont? .withSize(newSize) theme.italicCodeFont = theme.italicCodeFont?.withSize(newSize) - self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor + let bgColor = customBackgroundColor ?? Color(theme.themeBackgroundColor) + #if os(macOS) + self.backgroundColor = bgColor.nsColor() + #else + self.backgroundColor = bgColor.uiColor() + #endif if let font = theme.codeFont, font !== self.font { self.font = font } return true } @@ -343,3 +354,43 @@ extension UXTextView { var codeTextStorage : NSTextStorage? { return textStorage } } #endif // iOS + +extension Color { + #if os(iOS) + + func uiColor() -> UIColor { + + if #available(iOS 14.0, *) { + return UIColor(self) + } + + let components = self.components() + return UIColor(red: components.r, green: components.g, blue: components.b, alpha: components.a) + } + #else + func nsColor() -> NSColor { + + if #available(macOS 11.0, *) { + return NSColor(self) + } + let components = self.components() + return NSColor(red: components.r, green: components.g, blue: components.b, alpha: components.a) + } + + #endif + private func components() -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) { + + let scanner = Scanner(string: self.description.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)) + var hexNumber: UInt64 = 0 + var r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0, a: CGFloat = 0.0 + + let result = scanner.scanHexInt64(&hexNumber) + if result { + r = CGFloat((hexNumber & 0xff000000) >> 24) / 255 + g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255 + b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255 + a = CGFloat(hexNumber & 0x000000ff) / 255 + } + return (r, g, b, a) + } +} diff --git a/Sources/CodeEditor/UXCodeTextViewRepresentable.swift b/Sources/CodeEditor/UXCodeTextViewRepresentable.swift index d9a1321..826d0a1 100644 --- a/Sources/CodeEditor/UXCodeTextViewRepresentable.swift +++ b/Sources/CodeEditor/UXCodeTextViewRepresentable.swift @@ -56,7 +56,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable { inset : CGSize, allowsUndo : Bool, autoscroll : Bool, - backgroundColor: NSColor? = nil) + backgroundColor: Color? = nil) { self.source = source self.selection = selection @@ -75,7 +75,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable { private var source : Binding private var selection : Binding>? private var fontSize : Binding? - private var customBackgroundColor : NSColor? = nil + private var customBackgroundColor : Color? = nil private let language : CodeEditor.Language? private let themeName : CodeEditor.ThemeName private let flags : CodeEditor.Flags @@ -284,6 +284,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable { textView.autocorrectionType = .no textView.spellCheckingType = .no textView.smartQuotesType = .no + textView.customBackgroundColor = customBackgroundColor #endif updateTextView(textView) return textView