Skip to content

Commit

Permalink
Implemented browsers search results Pg Up/Down navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey M committed Aug 29, 2020
1 parent dd1a724 commit ac1d56b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
31 changes: 31 additions & 0 deletions DPackRx/Extensions/UIExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Windows.Media;

namespace DPackRx.Extensions
{
public static class UIExtensions
{
/// <summary>
/// Custom: returns child element of a given type.
/// </summary>
public static T GetChild<T>(this Visual element) where T : Visual
{
if (element == null)
return default;

if (element.GetType() == typeof(T))
return element as T;

T result = null;

for (var index = 0; index < VisualTreeHelper.GetChildrenCount(element); index++)
{
var visualElement = VisualTreeHelper.GetChild(element, index) as Visual;
result = visualElement.GetChild<T>();
if (result != null)
break;
}

return result;
}
}
}
43 changes: 37 additions & 6 deletions DPackRx/UI/Behaviors/TextBoxInputRedirectToListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,40 @@ private static void PreviewKeyDown(object sender, KeyEventArgs e)
viewItemIndex = control.SelectedIndex;
}
break;
// TODO: would like to Page Up/Down via ScrollViewer but couldn't figure out how to change the selection accordingly
// case Key.PageUp:
// case Key.PageDown:
}
case Key.PageUp:
case Key.PageDown:
var scrollViewer = control.GetChild<ScrollViewer>();
if (scrollViewer != null)
{
redirected = true;
if (e.Key == Key.PageUp)
scrollViewer.PageUp();
else
scrollViewer.PageDown();
control.UpdateLayout(); // the only way I could figure out for page up/down to register

var panel = control.GetChild<VirtualizingStackPanel>();
if (panel != null)
{
viewItemIndex = (int)panel.VerticalOffset;

if (e.Key == Key.PageUp)
{
if (viewItemIndex == control.SelectedIndex)
viewItemIndex = 0;
}
else
{
if (viewItemIndex <= control.SelectedIndex)
viewItemIndex = control.Items.Count - 1;
}

if ((viewItemIndex >= 0) && (viewItemIndex <= control.Items.Count - 1))
control.SelectedIndex = viewItemIndex;
}
}
break;
} // switch

if (redirected && (viewItemIndex >= 0) && (viewItemIndex <= control.Items.Count - 1))
control.ScrollIntoView(control.Items[viewItemIndex]);
Expand All @@ -263,8 +293,9 @@ private static void PreviewKeyDown(object sender, KeyEventArgs e)

if ((utilsService != null) && (shellStatusBarService != null))
{
utilsService.SetClipboardData(item.Name);
shellStatusBarService.SetStatusBarText($"{Path.GetFileName(item.Name)} name copied to the clipboard");
var name = Path.GetFileName(item.Name);
utilsService.SetClipboardData(name);
shellStatusBarService.SetStatusBarText($"'{name}' copied to the clipboard");

redirected = true;
e.Handled = redirected;
Expand Down

0 comments on commit ac1d56b

Please sign in to comment.