From 5ff9ac2a1e878e3a45dea40603f9348727316293 Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Tue, 4 Mar 2025 20:16:35 +0400 Subject: [PATCH 1/6] Update CodeEditor.swift --- Sources/CodeEditor/CodeEditor.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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, From 2e74df6a29dc0b1627443e82168bf8131aef378e Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Tue, 4 Mar 2025 20:17:31 +0400 Subject: [PATCH 2/6] Update UXCodeTextView.swift --- Sources/CodeEditor/UXCodeTextView.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/CodeEditor/UXCodeTextView.swift b/Sources/CodeEditor/UXCodeTextView.swift index 7c680ae..b93e44f 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,11 @@ 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 + #if os(macOS) + self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor + #else + self.backgroundColor = UIColor(customBackgroundColor ?? Color(theme.themeBackgroundColor.cgColor)) + #endif if let font = theme.codeFont, font !== self.font { self.font = font } return true } @@ -266,7 +271,11 @@ 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 + #if os(macOS) + self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor + #else + self.backgroundColor = UIColor(customBackgroundColor ?? Color(theme.themeBackgroundColor.cgColor)) + #endif if let font = theme.codeFont, font !== self.font { self.font = font } return true } From c7194d241b1cd5d0f754161620997a5db74fb7c1 Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Tue, 4 Mar 2025 20:18:24 +0400 Subject: [PATCH 3/6] Update UXCodeTextViewRepresentable.swift --- Sources/CodeEditor/UXCodeTextViewRepresentable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CodeEditor/UXCodeTextViewRepresentable.swift b/Sources/CodeEditor/UXCodeTextViewRepresentable.swift index d9a1321..64eafef 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 From dd35560c3c32dc8938f89d02b5d7950f906039ba Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Mon, 10 Mar 2025 12:06:07 +0400 Subject: [PATCH 4/6] fix NSColor bug --- Sources/CodeEditor/UXCodeTextView.swift | 57 ++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/Sources/CodeEditor/UXCodeTextView.swift b/Sources/CodeEditor/UXCodeTextView.swift index b93e44f..fa60aca 100644 --- a/Sources/CodeEditor/UXCodeTextView.swift +++ b/Sources/CodeEditor/UXCodeTextView.swift @@ -248,11 +248,12 @@ final class UXCodeTextView: UXTextView { guard let highlightr = highlightr, highlightr.setTheme(to: newTheme.rawValue), let theme = highlightr.theme else { return false } - #if os(macOS) - self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor - #else - self.backgroundColor = UIColor(customBackgroundColor ?? Color(theme.themeBackgroundColor.cgColor)) - #endif + 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 } @@ -271,10 +272,12 @@ final class UXCodeTextView: UXTextView { theme.codeFont = theme.codeFont? .withSize(newSize) theme.boldCodeFont = theme.boldCodeFont? .withSize(newSize) theme.italicCodeFont = theme.italicCodeFont?.withSize(newSize) + let bgColor = customBackgroundColor ?? Color(theme.themeBackgroundColor) #if os(macOS) - self.backgroundColor = customBackgroundColor ?? theme.themeBackgroundColor + bgColor + self.backgroundColor = bgColor.nsColor() #else - self.backgroundColor = UIColor(customBackgroundColor ?? Color(theme.themeBackgroundColor.cgColor)) + self.backgroundColor = bgColor.uiColor() #endif if let font = theme.codeFont, font !== self.font { self.font = font } return true @@ -352,3 +355,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) + } +} From faf7987d28ea7770c196f2ac8dff89112bfec400 Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Mon, 10 Mar 2025 12:07:50 +0400 Subject: [PATCH 5/6] override customBackgroundColor in iOS --- Sources/CodeEditor/UXCodeTextViewRepresentable.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/CodeEditor/UXCodeTextViewRepresentable.swift b/Sources/CodeEditor/UXCodeTextViewRepresentable.swift index 64eafef..826d0a1 100644 --- a/Sources/CodeEditor/UXCodeTextViewRepresentable.swift +++ b/Sources/CodeEditor/UXCodeTextViewRepresentable.swift @@ -284,6 +284,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable { textView.autocorrectionType = .no textView.spellCheckingType = .no textView.smartQuotesType = .no + textView.customBackgroundColor = customBackgroundColor #endif updateTextView(textView) return textView From 1aa960ae79dbf4016aab7228e81f05a53adba7be Mon Sep 17 00:00:00 2001 From: hossinasaadi Date: Mon, 10 Mar 2025 12:20:58 +0400 Subject: [PATCH 6/6] update UXCodeTextView --- Sources/CodeEditor/UXCodeTextView.swift | 35 ++++++++++++------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Sources/CodeEditor/UXCodeTextView.swift b/Sources/CodeEditor/UXCodeTextView.swift index fa60aca..f314134 100644 --- a/Sources/CodeEditor/UXCodeTextView.swift +++ b/Sources/CodeEditor/UXCodeTextView.swift @@ -274,7 +274,6 @@ final class UXCodeTextView: UXTextView { theme.italicCodeFont = theme.italicCodeFont?.withSize(newSize) let bgColor = customBackgroundColor ?? Color(theme.themeBackgroundColor) #if os(macOS) - bgColor self.backgroundColor = bgColor.nsColor() #else self.backgroundColor = bgColor.uiColor() @@ -359,24 +358,24 @@ extension UXTextView { 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) - } + 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) - } + 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) {