Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build on MacCatalyst #37

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/CodeEditor/CodeEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down
57 changes: 54 additions & 3 deletions Sources/CodeEditor/UXCodeTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Highlightr
import SwiftUI

#if os(macOS)
import AppKit
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
}
5 changes: 3 additions & 2 deletions Sources/CodeEditor/UXCodeTextViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -75,7 +75,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
private var source : Binding<String>
private var selection : Binding<Range<String.Index>>?
private var fontSize : Binding<CGFloat>?
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
Expand Down Expand Up @@ -284,6 +284,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
textView.autocorrectionType = .no
textView.spellCheckingType = .no
textView.smartQuotesType = .no
textView.customBackgroundColor = customBackgroundColor
#endif
updateTextView(textView)
return textView
Expand Down