]> git.r.bdr.sh - rbdr/map/blobdiff - Map/Presentation/MapEditor.swift
Add some debouncing
[rbdr/map] / Map / Presentation / MapEditor.swift
index d7c93dda3ce597fce10ec123b32a3befd71d03f5..5cda8f961c48c65488f9f969a6a4735083179a27 100644 (file)
@@ -1,19 +1,17 @@
-/*
- Copyright (C) 2024 Rubén Beltrán del Río
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see https://map.tranquil.systems.
- */
+// Copyright (C) 2024 Rubén Beltrán del Río
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see https://map.tranquil.systems.
 import SwiftUI
 
 struct MapEditor: View {
 import SwiftUI
 
 struct MapEditor: View {
@@ -22,6 +20,8 @@ struct MapEditor: View {
   @State var selectedEvolution: StageType = .behavior
   @State var isSearching: Bool = false
 
   @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
   @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
 
   let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
@@ -30,9 +30,11 @@ struct MapEditor: View {
   @State var searchTerm = ""
   @State var selectedTerm = 0
 
   @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 {
     if !isSearching || searchTerm.isEmpty {
-      return []
+      results = []
     }
     let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
     var searchRange = document.text.startIndex..<document.text.endIndex
     }
     let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
     var searchRange = document.text.startIndex..<document.text.endIndex
@@ -43,7 +45,7 @@ struct MapEditor: View {
       searchRange = range.upperBound..<document.text.endIndex
     }
 
       searchRange = range.upperBound..<document.text.endIndex
     }
 
-    return ranges
+    results = ranges
 
   }
 
 
   }
 
@@ -74,25 +76,26 @@ struct MapEditor: View {
 
           },
           onDismiss: {
 
           },
           onDismiss: {
-            withAnimation {
-              isSearching = false
-            }
+            isSearching = false
           }
         )
         .onChange(
           of: searchTerm,
           {
           }
         )
         .onChange(
           of: searchTerm,
           {
-            selectedTerm = 0
+            changeDebouncer.debounce {
+              updateRanges()
+              selectedTerm = 0
+            }
           })
         Divider()
       }
       adaptiveStack {
         ZStack(alignment: .topLeading) {
           MapTextEditor(document: $document, highlightRanges: results, selectedRange: selectedTerm)
           })
         Divider()
       }
       adaptiveStack {
         ZStack(alignment: .topLeading) {
           MapTextEditor(document: $document, highlightRanges: results, selectedRange: selectedTerm)
-            .background(Color.ui.background)
-            .foregroundColor(Color.ui.foreground)
+            .background(Color.UI.background)
+            .foregroundColor(Color.UI.foreground)
             .frame(minHeight: 96.0)
             .frame(minHeight: 96.0)
-        }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius(
+        }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.UI.background).cornerRadius(
           5.0)
         GeometryReader { geometry in
           ScrollView([.horizontal, .vertical]) {
           5.0)
         GeometryReader { geometry in
           ScrollView([.horizontal, .vertical]) {
@@ -101,7 +104,7 @@ struct MapEditor: View {
             ).scaleEffect(zoom, anchor: .center).frame(
               width: (Dimensions.mapSize.width + 2 * Dimensions.mapPadding) * zoom,
               height: (Dimensions.mapSize.height + 2 * Dimensions.mapPadding) * zoom)
             ).scaleEffect(zoom, anchor: .center).frame(
               width: (Dimensions.mapSize.width + 2 * Dimensions.mapPadding) * zoom,
               height: (Dimensions.mapSize.height + 2 * Dimensions.mapPadding) * zoom)
-          }.background(Color.ui.background)
+          }.background(Color.UI.background)
             .gesture(
               MagnificationGesture()
                 .onChanged { value in
             .gesture(
               MagnificationGesture()
                 .onChanged { value in
@@ -122,16 +125,16 @@ struct MapEditor: View {
           value: $zoom, in: zoomRange, step: 0.1,
           label: {
             Text(formatZoom(zoom))
           value: $zoom, in: zoomRange, step: 0.1,
           label: {
             Text(formatZoom(zoom))
-              .font(.theme.smallControl)
+              .font(.Theme.smallControl)
           },
           minimumValueLabel: {
             Image(systemName: "minus.magnifyingglass")
           },
           minimumValueLabel: {
             Image(systemName: "minus.magnifyingglass")
-              .font(.theme.smallControl)
+              .font(.Theme.smallControl)
               .help("Zoom Out (⌘-)")
           },
           maximumValueLabel: {
             Image(systemName: "plus.magnifyingglass")
               .help("Zoom Out (⌘-)")
           },
           maximumValueLabel: {
             Image(systemName: "plus.magnifyingglass")
-              .font(.theme.smallControl)
+              .font(.Theme.smallControl)
               .help("Zoom In (⌘+)")
           }
         ).frame(width: 200).padding(.trailing, 10.0)
               .help("Zoom In (⌘+)")
           }
         ).frame(width: 200).padding(.trailing, 10.0)