Skip to content

Commit

Permalink
TextEditorEX // Remove (buggy) tooltip binding support.
Browse files Browse the repository at this point in the history
- This binding causes responsiveness issues towards the editor.
  • Loading branch information
ShikiSuen committed Dec 20, 2022
1 parent 495270c commit ac6b53b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public struct VwrPhraseEditorUI: View {
}
}

TextEditorEX(text: $txtContent, tooltip: $textEditorTooltip)
TextEditorEX(text: $txtContent)
.disabled(selInputMode == .imeModeNULL || isLoading)
.frame(minWidth: 320, minHeight: 240)
.backport.onChange(of: fileChangeIndicator.id) { _ in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,37 @@ public struct VisualEffectView: NSViewRepresentable {
/// A much faster alternative than Apple official TextEditor.
public struct TextEditorEX: NSViewRepresentable {
@Binding var text: String
@Binding var tooltip: String

public init(text: Binding<String>, tooltip: Binding<String>) {
public init(text: Binding<String>) {
_text = text
_tooltip = tooltip
}

public func makeNSView(context: Context) -> NSScrollView {
context.coordinator.createTextViewStack()
}

public func updateNSView(_ nsView: NSScrollView, context _: Context) {
if let textArea = nsView.documentView as? NSTextView {
if textArea.string != text { textArea.string = text }
if textArea.toolTip != tooltip { textArea.toolTip = tooltip }
if let textArea = nsView.documentView as? NSTextView, textArea.string != self.text {
textArea.string = text
}
}

public func makeCoordinator() -> Coordinator {
Coordinator(text: $text, tooltip: $tooltip)
Coordinator(text: $text)
}

public class Coordinator: NSObject, NSTextViewDelegate {
public var text: Binding<String>
public var tooltip: Binding<String>

public init(text: Binding<String>, tooltip: Binding<String>) {
public init(text: Binding<String>) {
self.text = text
self.tooltip = tooltip
}

public func textView(_ textView: NSTextView, shouldChangeTextIn range: NSRange, replacementString text: String?)
-> Bool
{
defer {
self.text.wrappedValue = (textView.string as NSString).replacingCharacters(in: range, with: text!)
self.tooltip.wrappedValue = (textView.toolTip as? NSString ?? "").replacingCharacters(in: range, with: text!)
}
return true
}
Expand Down

0 comments on commit ac6b53b

Please sign in to comment.