textStorage.removeAttribute(
.backgroundColor, range: NSRange(location: 0, length: textStorage.length))
- for range in highlightRanges {
+ for (index, range) in highlightRanges.enumerated() {
let nsRange = NSRange(range, in: textStorage.string)
- textStorage.addAttribute(.backgroundColor, value: NSColor.Syntax.match, range: nsRange)
+ let color = index == selectedRange ? NSColor.Syntax.highlightMatch : NSColor.Syntax.match
+ textStorage.addAttribute(.backgroundColor, value: color, range: nsRange)
}
textView.needsDisplay = true
let range = highlightRanges[selectedRange]
let nsRange = NSRange(range, in: textStorage.string)
textView.scrollRangeToVisible(nsRange)
- textView.selectedRange = nsRange
}
}
}
}
-
- private func setSelectionColor() {
- guard let textView = self.textView else { return }
-
- var selectedTextAttributes = textView.selectedTextAttributes
- selectedTextAttributes[.backgroundColor] = NSColor.yellow.withAlphaComponent(0.3)
- textView.selectedTextAttributes = selectedTextAttributes
- }
}
extension MapTextEditorController: NSTextViewDelegate {
context: NSViewControllerRepresentableContext<MapTextEditor>
) {
nsViewController.highlightRanges = highlightRanges
- nsViewController.selectedRange = selectedRange
+ if nsViewController.selectedRange != selectedRange {
+ nsViewController.selectedRange = selectedRange
+ }
}
}
CommandGroup(after: CommandGroupPlacement.pasteboard) {
Divider()
Button("Find...") {
- withAnimation {
- isSearching = isSearching != nil ? !isSearching! : true
- }
+ isSearching = isSearching != nil ? !isSearching! : true
}.keyboardShortcut(
"f", modifiers: EventModifiers([.command])
).disabled(document == nil)
@State var selectedEvolution: StageType = .behavior
@State var isSearching: Bool = false
+ private let changeDebouncer: Debouncer = Debouncer(seconds: 0.05)
+
@AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
@State var searchTerm = ""
@State var selectedTerm = 0
- var results: [Range<String.Index>] {
+ @State var results: [Range<String.Index>] = []
+
+ private func updateRanges() {
if !isSearching || searchTerm.isEmpty {
- return []
+ results = []
}
let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
var searchRange = document.text.startIndex..<document.text.endIndex
searchRange = range.upperBound..<document.text.endIndex
}
- return ranges
+ results = ranges
}
},
onDismiss: {
- withAnimation {
- isSearching = false
- }
+ isSearching = false
}
)
.onChange(
of: searchTerm,
{
- selectedTerm = 0
+ changeDebouncer.debounce {
+ updateRanges()
+ selectedTerm = 0
+ }
})
Divider()
}