From: Ruben Beltran del Rio Date: Mon, 16 Sep 2024 18:56:23 +0000 (+0200) Subject: Add some debouncing X-Git-Url: https://git.r.bdr.sh/rbdr/map/commitdiff_plain?ds=inline Add some debouncing --- diff --git a/Map/Presentation/Base Components/MapTextEditor.swift b/Map/Presentation/Base Components/MapTextEditor.swift index 2e4f279..ff98203 100644 --- a/Map/Presentation/Base Components/MapTextEditor.swift +++ b/Map/Presentation/Base Components/MapTextEditor.swift @@ -89,10 +89,11 @@ class MapTextEditorController: NSViewController { 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 @@ -108,19 +109,10 @@ class MapTextEditorController: NSViewController { 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 { @@ -259,6 +251,8 @@ struct MapTextEditor: NSViewControllerRepresentable { context: NSViewControllerRepresentableContext ) { nsViewController.highlightRanges = highlightRanges - nsViewController.selectedRange = selectedRange + if nsViewController.selectedRange != selectedRange { + nsViewController.selectedRange = selectedRange + } } } diff --git a/Map/Presentation/Commands/MapCommands.swift b/Map/Presentation/Commands/MapCommands.swift index c04cb73..35250e2 100644 --- a/Map/Presentation/Commands/MapCommands.swift +++ b/Map/Presentation/Commands/MapCommands.swift @@ -67,9 +67,7 @@ struct MapCommands: Commands { 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) diff --git a/Map/Presentation/MapEditor.swift b/Map/Presentation/MapEditor.swift index d62d4ad..5cda8f9 100644 --- a/Map/Presentation/MapEditor.swift +++ b/Map/Presentation/MapEditor.swift @@ -20,6 +20,8 @@ struct MapEditor: View { @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 @@ -28,9 +30,11 @@ struct MapEditor: View { @State var searchTerm = "" @State var selectedTerm = 0 - var results: [Range] { + @State var results: [Range] = [] + + private func updateRanges() { if !isSearching || searchTerm.isEmpty { - return [] + results = [] } let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive] var searchRange = document.text.startIndex..