From 205218b01ab0b6a16aa92944c80069d49b18cfdd Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 13 Oct 2023 15:28:56 -0400 Subject: [PATCH] Completion popup wrapping for arrow key navigation --- .../Controls/CodeCompletion/CompletionList.cs | 4 ++-- .../CodeCompletion/CompletionListBox.cs | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs index 9150209b..79f2b766 100644 --- a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs +++ b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionList.cs @@ -202,11 +202,11 @@ public class CompletionList : TemplatedControl { case Key.Down: e.Handled = true; - _listBox.SelectIndex(_listBox.SelectedIndex + 1); + _listBox.SelectNextIndexWithLoop(); break; case Key.Up: e.Handled = true; - _listBox.SelectIndex(_listBox.SelectedIndex - 1); + _listBox.SelectPreviousIndexWithLoop(); break; case Key.PageDown: e.Handled = true; diff --git a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListBox.cs b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListBox.cs index 4313be17..90f40a28 100644 --- a/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListBox.cs +++ b/StabilityMatrix.Avalonia/Controls/CodeCompletion/CompletionListBox.cs @@ -91,6 +91,28 @@ public class CompletionListBox : ListBox SelectedIndex = -1; } + /// + /// Selects the next item. If the last item is already selected, selects the first item. + /// + public void SelectNextIndexWithLoop() + { + if (ItemCount <= 0) + return; + + SelectIndex((SelectedIndex + 1) % ItemCount); + } + + /// + /// Selects the previous item. If the first item is already selected, selects the last item. + /// + public void SelectPreviousIndexWithLoop() + { + if (ItemCount <= 0) + return; + + SelectIndex((SelectedIndex - 1 + ItemCount) % ItemCount); + } + /// /// Selects the item with the specified index and scrolls it into view. ///