Skip to content

Commit

Permalink
36: ios compile fixes, switching NSColor for background colour to Swi…
Browse files Browse the repository at this point in the history
…ft Color for

ZeeZide#36
  • Loading branch information
SoylentGraham committed Nov 18, 2024
1 parent c836d95 commit d84964a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Sources/CodeEditor/CodeEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public struct CodeEditor: View {
inset : CGSize? = nil,
allowsUndo : Bool = true,
autoscroll : Bool = true,
backgroundColor: NSColor? = nil,
backgroundColor: Color? = nil,
highlightr : Highlightr? = nil

)
Expand Down Expand Up @@ -299,7 +299,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 Down Expand Up @@ -328,7 +328,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
5 changes: 4 additions & 1 deletion Sources/CodeEditor/UXCodeTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
//

import Highlightr
import SwiftUI // Color

#if os(macOS)
import AppKit

typealias UXTextView = NSTextView
typealias UXTextViewDelegate = NSTextViewDelegate
typealias UColor = NSColor
#else
import UIKit

typealias UXTextView = UITextView
typealias UXTextViewDelegate = UITextViewDelegate
typealias UColor = UIColor
#endif

/**
Expand All @@ -31,7 +34,7 @@ final class UXCodeTextView: UXTextView {
// highlightr now provided from higher level
var highlightr : Highlightr?

var customBackgroundColor: NSColor? = nil
var customBackgroundColor: UColor? = nil

private var hlTextStorage : CodeAttributedString? {
return textStorage as? CodeAttributedString
Expand Down
37 changes: 31 additions & 6 deletions Sources/CodeEditor/UXCodeTextViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
inset : CGSize,
allowsUndo : Bool,
autoscroll : Bool,
backgroundColor: NSColor? = nil,
backgroundColor: Color? = nil,
highlightr : Highlightr?
)
{
Expand All @@ -76,11 +76,11 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
self.highlightr = highlightr
}

private var highlightr : Highlightr? /* do not instantiate a default here, will be created on every view render */
private var highlightr : Highlightr? /* do not instantiate a default here, will be created on every view render */
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 All @@ -90,6 +90,29 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
private let autoPairs : [ String : String ]
private let autoscroll : Bool

#if os(macOS)
private var customBackgroundNSColor : NSColor?
{
guard let customBackgroundColor else
{
return nil
}
// 11 onwards is light/dark mode env variance
if #available(macOS 11.0, *)
{
return NSColor(customBackgroundColor)
}
else
{
// todo: find a conversion approach pre macos11
// no resolve
// no cgcolour extraction
return nil
}
}
#endif


// The inner `value` is true, exactly when execution is inside
// the `updateTextView(_:)` method. The `Coordinator` can use this
// value to guard against update cycles.
Expand Down Expand Up @@ -241,12 +264,14 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
textView.isEditable = flags.contains(.editable)
textView.isSelectable = flags.contains(.selectable)
}

@Environment(\.self) var environment


#if os(macOS)
public func makeNSView(context: Context) -> NSScrollView {
// instantiate here, once, if user didn't provide a Highlightr
let textView = UXCodeTextView(highlightr:self.highlightr ?? Highlightr())
textView.customBackgroundColor = customBackgroundColor
textView.customBackgroundColor = customBackgroundNSColor
textView.autoresizingMask = [ .width, .height ]
textView.delegate = context.coordinator
textView.allowsUndo = allowsUndo
Expand All @@ -268,7 +293,7 @@ struct UXCodeTextViewRepresentable : UXViewRepresentable {
if textView.delegate !== context.coordinator {
textView.delegate = context.coordinator
}
textView.customBackgroundColor = customBackgroundColor
textView.customBackgroundColor = customBackgroundNSColor
textView.textContainerInset = inset
updateTextView(textView)
}
Expand Down

0 comments on commit d84964a

Please sign in to comment.