Przeglądaj źródła

Refactored IndexOrNext/Previous with direction parameter

CPKreuz 1 miesiąc temu
rodzic
commit
3e62ef7235

+ 13 - 0
src/PixiEditor/Helpers/Extensions/EnumerableExtensions.cs

@@ -95,4 +95,17 @@ internal static class EnumerableExtensions
 
 
         return IndexOrNext(collection, predicate, index, false);
         return IndexOrNext(collection, predicate, index, false);
     }
     }
+
+    public static T IndexOrNextInDirection<T>(this IEnumerable<T> collection, Predicate<T> predicate, int index, NextToDirection direction, bool overrun = true) => direction switch
+    {
+        NextToDirection.Forwards => IndexOrNext(collection, predicate, index, overrun),
+        NextToDirection.Backwards => IndexOrPrevious(collection, predicate, index, overrun),
+        _ => throw new ArgumentOutOfRangeException(nameof(direction)),
+    };
+}
+
+enum NextToDirection
+{
+    Forwards = 1,
+    Backwards = -1
 }
 }

+ 5 - 9
src/PixiEditor/Views/Main/CommandSearch/CommandSearchControl.axaml.cs

@@ -184,11 +184,11 @@ internal partial class CommandSearchControl : UserControl, INotifyPropertyChange
         }
         }
         else if (e.Key is Key.Down or Key.PageDown)
         else if (e.Key is Key.Down or Key.PageDown)
         {
         {
-            MoveSelection(1);
+            MoveSelection(NextToDirection.Forwards);
         }
         }
         else if (e.Key is Key.Up or Key.PageUp)
         else if (e.Key is Key.Up or Key.PageUp)
         {
         {
-            MoveSelection(-1);
+            MoveSelection(NextToDirection.Backwards);
         }
         }
         else if (e.Key == Key.Escape ||
         else if (e.Key == Key.Escape ||
                  CommandController.Current.Commands["PixiEditor.Search.Toggle"].Shortcut
                  CommandController.Current.Commands["PixiEditor.Search.Toggle"].Shortcut
@@ -266,22 +266,18 @@ internal partial class CommandSearchControl : UserControl, INotifyPropertyChange
         }
         }
     }
     }
 
 
-    private void MoveSelection(int delta)
+    private void MoveSelection(NextToDirection direction)
     {
     {
-        if (delta == 0)
-            return;
         if (SelectedResult is null)
         if (SelectedResult is null)
         {
         {
             SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
             SelectedResult = Results.FirstOrDefault(x => x.CanExecute);
             return;
             return;
         }
         }
 
 
-        int newIndex = Results.IndexOf(SelectedResult) + delta;
+        var newIndex = Results.IndexOf(SelectedResult) + (int)direction;
         newIndex = (newIndex % Results.Count + Results.Count) % Results.Count;
         newIndex = (newIndex % Results.Count + Results.Count) % Results.Count;
 
 
-        SelectedResult = delta > 0
-            ? Results.IndexOrNext(x => x.CanExecute, newIndex)
-            : Results.IndexOrPrevious(x => x.CanExecute, newIndex);
+        SelectedResult = Results.IndexOrNextInDirection(x => x.CanExecute, newIndex, direction);
 
 
         newIndex = Results.IndexOf(SelectedResult);
         newIndex = Results.IndexOf(SelectedResult);
         itemscontrol.ContainerFromIndex(newIndex)?.BringIntoView();
         itemscontrol.ContainerFromIndex(newIndex)?.BringIntoView();