Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Nov 5, 2023
1 parent d365a97 commit 246b030
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 58 deletions.
18 changes: 4 additions & 14 deletions EverythingToolbar/Controls/SearchBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,15 @@ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
return;
}
else if (Keyboard.Modifiers == ModifierKeys.None &&
(e.Key == Key.Down || e.Key == Key.Enter))
(e.Key == Key.Down || e.Key == Key.Enter ||
e.Key == Key.Home || e.Key == Key.End ||
e.Key == Key.PageDown || e.Key == Key.PageUp ||
e.Key == Key.Up || e.Key == Key.Down))
{
EventDispatcher.Instance.InvokeSearchResultsListViewKeyEvent(this, e);
e.Handled = true;
return;
}

if (Settings.Default.isAutoSelectFirstResult)
{
// The search bar should not lose focus in this mode, so some key events
// need to be forwarded to the SearchResultsListView
if (e.Key == Key.Home || e.Key == Key.End ||
e.Key == Key.PageDown || e.Key == Key.PageUp ||
e.Key == Key.Up)
{
EventDispatcher.Instance.InvokeSearchResultsListViewKeyEvent(this, e);
e.Handled = true;
}
}
}

private void UpdateSearchTerm(string newSearchTerm)
Expand Down
71 changes: 28 additions & 43 deletions EverythingToolbar/Controls/SearchResultsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,48 +100,48 @@ private void OnKeyPressed(object sender, KeyEventArgs e)
{
SelectedItem?.CopyPathToClipboard();
}
else if (Keyboard.Modifiers == (ModifierKeys.Control) && e.Key == Key.C)
else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C)
{
SelectedItem?.CopyToClipboard();
}
else if (e.Key == Key.Up && !Settings.Default.isAutoSelectFirstResult)
else if (e.Key == Key.Up)
{
if (SearchResultsListView.SelectedIndex == 0)
if (SearchResultsListView.SelectedIndex == 0 && !Settings.Default.isAutoSelectFirstResult)
{
SearchResultsListView.SelectedIndex = -1;
EventDispatcher.Instance.InvokeSearchBoxFocused(this, EventArgs.Empty);
e.Handled = true;
}
}

// The following key bindings are only required when forwarding events from the search box
// and should not be executed when the ListView is already focused since it handles them itself.
if (IsKeyboardFocusWithin)
return;
else
{
SelectPreviousSearchResult();
}

if (e.Key == Key.Up)
{
SelectPreviousSearchResult();
e.Handled = true;
}
else if (e.Key == Key.Down)
{
SelectNextSearchResult();
e.Handled = true;
}
else if (e.Key == Key.PageUp)
{
PageUp();
e.Handled = true;
}
else if (e.Key == Key.PageDown)
{
PageDown();
e.Handled = true;
}
else if (e.Key == Key.Home)
{
SelectFirstSearchResult();
e.Handled = true;
}
else if (e.Key == Key.End)
{
SelectLastSearchResult();
e.Handled = true;
}
}

Expand Down Expand Up @@ -176,39 +176,34 @@ private void ScrollToVerticalOffset(double verticalOffset)

private void SelectNextSearchResult()
{
if (SearchResultsListView.SelectedIndex >= SearchResultsListView.Items.Count - 1)
return;

SearchResultsListView.SelectedIndex++;
SearchResultsListView.ScrollIntoView(SearchResultsListView.SelectedItem);
FocusSelectedItem();
SelectNthSearchResult(SearchResultsListView.SelectedIndex + 1);
}

private void SelectPreviousSearchResult()
{
if (SearchResultsListView.SelectedIndex <= 0)
return;

SearchResultsListView.SelectedIndex--;
SearchResultsListView.ScrollIntoView(SelectedItem);
FocusSelectedItem();
SelectNthSearchResult(SearchResultsListView.SelectedIndex - 1);
}

public void SelectFirstSearchResult()
{
if (SearchResultsListView.Items.IsEmpty)
return;

SearchResultsListView.SelectedIndex = 0;
SearchResultsListView.ScrollIntoView(SelectedItem);
FocusSelectedItem();
SelectNthSearchResult(0);
}

private void SelectLastSearchResult()
{
SearchResultsListView.SelectedIndex = SearchResultsListView.Items.Count - 1;
SelectNthSearchResult(SearchResultsListView.Items.Count - 1);
}

private void SelectNthSearchResult(int n)
{
if (n < 0 || n >= SearchResultsListView.Items.Count)
return;

if (!Settings.Default.isAutoSelectFirstResult)
SearchResultsListView.Focus();

SearchResultsListView.SelectedIndex = n;
SearchResultsListView.ScrollIntoView(SelectedItem);
FocusSelectedItem();
}

private int GetPageSize()
Expand Down Expand Up @@ -406,15 +401,5 @@ private void OnContextMenuOpened(object sender, RoutedEventArgs e)

mi.Visibility = isExecutable ? Visibility.Visible : Visibility.Collapsed;
}

private void FocusSelectedItem()
{
if (Settings.Default.isAutoSelectFirstResult)
return;

var selectedItem = (ListViewItem)SearchResultsListView.ItemContainerGenerator.ContainerFromItem(SelectedItem);
if (selectedItem != null)
Keyboard.Focus(selectedItem);
}
}
}
2 changes: 1 addition & 1 deletion EverythingToolbar/SearchWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private SearchWindow()
Instance.Hide();
Keyboard.ClearFocus();
}
else if (e.Key == Key.Tab) // TODO: not registered
else if (e.Key == Key.Tab)
{
var offset = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) ? -1 : 1;
EverythingSearch.Instance.CycleFilters(offset);
Expand Down

0 comments on commit 246b030

Please sign in to comment.