Skip to content

Commit 4593894

Browse files
committed
focus to be selected package
1 parent 8491112 commit 4593894

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

src/UniGetUI/Pages/SoftwarePages/AbstractPackagesPage.xaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
mc:Ignorable="d"
1313
Name="ABSTRACT_PAGE"
1414
NavigationCacheMode="Required"
15-
xmlns:animations="using:CommunityToolkit.WinUI.Animations" xmlns:pkgClasses="using:UniGetUI.PackageEngine.PackageClasses"
16-
CharacterReceived="{x:Bind PackageList_CharacterReceived}">
15+
xmlns:animations="using:CommunityToolkit.WinUI.Animations" xmlns:pkgClasses="using:UniGetUI.PackageEngine.PackageClasses">
1716
<!-- PreviewKeyDown="{x:Bind PackageList_PreviewKeyDown}" -->
1817

1918
<animations:Implicit.ShowAnimations>
@@ -504,7 +503,8 @@
504503
Package="{x:Bind Package}" Wrapper="{x:Bind Self}"
505504
RightTapped="PackageItemContainer_RightTapped"
506505
DoubleTapped="PackageItemContainer_DoubleTapped"
507-
KeyUp="PackageItemContainer_KeyUp">
506+
KeyUp="PackageItemContainer_KeyUp"
507+
PreviewKeyDown="PackageItemContainer_PreviewKeyDown">
508508

509509
<Grid ColumnSpacing="5" Opacity="{x:Bind ListedOpacity, Mode=OneWay}" Padding="12,3,8,3">
510510
<Grid.RowDefinitions>
@@ -907,7 +907,8 @@
907907
<ItemsView x:Name="PackageList" x:FieldModifier="protected" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1"
908908
ItemsSource="{x:Bind FilteredPackages, Mode=OneWay}"
909909
ItemTemplate="{StaticResource PackageTemplate}" Padding="4,0,4,0"
910-
CanBeScrollAnchor="False">
910+
CanBeScrollAnchor="False"
911+
CharacterReceived="{x:Bind PackageList_CharacterReceived}">
911912
<ItemsView.Layout>
912913
<StackLayout Spacing="3"/>
913914
</ItemsView.Layout>

src/UniGetUI/Pages/SoftwarePages/AbstractPackagesPage.xaml.cs

Lines changed: 84 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
using UniGetUI.PackageEngine.PackageLoader;
1414
using Windows.System;
1515
using Windows.UI.Core;
16+
using CommunityToolkit.WinUI;
1617
using UniGetUI.Interface.Pages;
1718
using UniGetUI.Interface.Telemetry;
1819
using UniGetUI.Pages.DialogPages;
20+
using DispatcherQueuePriority = Microsoft.UI.Dispatching.DispatcherQueuePriority;
1921

2022
// To learn more about WinUI, the WinUI project structure,
2123
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -480,14 +482,61 @@ protected async Task LoadPackages(ReloadReason reason)
480482
Loader_PackagesChanged(this, EventArgs.Empty);
481483
}
482484

483-
private void SelectAndScrollTo(int index)
485+
private void SelectAndScrollTo(int index, bool focus)
484486
{
487+
if (index < 0 || index >= FilteredPackages.Count)
488+
return;
489+
485490
PackageList.Select(index);
486-
PackageList.ScrollView?.ScrollTo(0, Math.Max(0, (index - 3) * 39), new ScrollingScrollOptions
487-
(
488-
ScrollingAnimationMode.Disabled,
489-
ScrollingSnapPointsMode.Ignore
490-
));
491+
492+
if (PackageList.ScrollView?.VerticalOffset > index * 39)
493+
{
494+
PackageList.ScrollView.ScrollTo(0, index * 39, new ScrollingScrollOptions(
495+
ScrollingAnimationMode.Disabled,
496+
ScrollingSnapPointsMode.Ignore
497+
));
498+
}
499+
else if (PackageList.ScrollView?.VerticalOffset + PackageList.ScrollView?.ViewportHeight < (index + 1) * 39)
500+
{
501+
PackageList.ScrollView?.ScrollTo(0, (index + 1) * 39 - PackageList.ScrollView.ViewportHeight, new ScrollingScrollOptions(
502+
ScrollingAnimationMode.Disabled,
503+
ScrollingSnapPointsMode.Ignore
504+
));
505+
}
506+
507+
if (focus)
508+
Focus(FilteredPackages[index].Package);
509+
}
510+
511+
private void Focus(IPackage packageToFocus, int retryCount = 0)
512+
{
513+
if (retryCount > 20)
514+
return;
515+
516+
DispatcherQueue.TryEnqueue(
517+
DispatcherQueuePriority.Low,
518+
() =>
519+
{
520+
PackageItemContainer? containerToFocus = PackageList.FindDescendant<PackageItemContainer>(c => c.Package?.Equals(packageToFocus) == true);
521+
if (containerToFocus == null)
522+
{
523+
Focus(packageToFocus, ++retryCount);
524+
return;
525+
}
526+
527+
if (!containerToFocus.IsSelected)
528+
{
529+
PackageItemContainer? selectedContainer = PackageList.FindDescendant<PackageItemContainer>(c => c.IsSelected);
530+
if (selectedContainer?.Package?.Equals(packageToFocus) == true)
531+
containerToFocus = selectedContainer;
532+
else
533+
{
534+
Focus(packageToFocus, ++retryCount);
535+
return;
536+
}
537+
}
538+
containerToFocus.Focus(FocusState.Keyboard);
539+
});
491540
}
492541

493542
public void PackageList_CharacterReceived(object sender, CharacterReceivedRoutedEventArgs e)
@@ -514,7 +563,7 @@ public void PackageList_CharacterReceived(object sender, CharacterReceivedRouted
514563
{
515564
if (FilteredPackages[i].Package.Name.ToLower().StartsWith(TypeQuery))
516565
{
517-
SelectAndScrollTo(i);
566+
SelectAndScrollTo(i, true);
518567
SelectedPackage = true;
519568
break;
520569
}
@@ -566,11 +615,11 @@ public void PackageList_CharacterReceived(object sender, CharacterReceivedRouted
566615
}
567616
}
568617

569-
SelectAndScrollTo(FirstIdx + (IndexOffset % (LastIdx - FirstIdx + 1)));
618+
SelectAndScrollTo(FirstIdx + (IndexOffset % (LastIdx - FirstIdx + 1)), true);
570619
}
571620
else if (QueryIndex > -1)
572621
{
573-
SelectAndScrollTo(QueryIndex);
622+
SelectAndScrollTo(QueryIndex, true);
574623
}
575624
}
576625
}
@@ -696,12 +745,7 @@ public void FilterPackages()
696745
{
697746
if (FilteredPackages[i].Package.Equals(previousSelection.Package))
698747
{
699-
PackageList.Select(i);
700-
PackageList.ScrollView?.ScrollTo(0, Math.Max(0, (i - 3) * 39), new ScrollingScrollOptions
701-
(
702-
ScrollingAnimationMode.Disabled,
703-
ScrollingSnapPointsMode.Ignore
704-
));
748+
SelectAndScrollTo(i, false);
705749
break;
706750
}
707751
}
@@ -887,6 +931,7 @@ private void PackageItemContainer_RightTapped(object sender, RightTappedRoutedEv
887931
if (sender is PackageItemContainer container && container.Package is not null)
888932
{
889933
PackageList.Select(container.Wrapper.Index);
934+
container.Focus(FocusState.Keyboard);
890935
WhenShowingContextMenu(container.Package);
891936
}
892937
}
@@ -896,6 +941,7 @@ private void PackageItemContainer_DoubleTapped(object sender, DoubleTappedRouted
896941
if (sender is PackageItemContainer container && container.Package is not null)
897942
{
898943
PackageList.Select(container.Wrapper.Index);
944+
container.Focus(FocusState.Keyboard);
899945

900946
TEL_InstallReferral referral = TEL_InstallReferral.ALREADY_INSTALLED;
901947
if (PAGE_NAME == "Bundles") referral = TEL_InstallReferral.FROM_BUNDLE;
@@ -1069,5 +1115,28 @@ public void OnLeave()
10691115
Visibility = Visibility.Collapsed;
10701116
IsEnabled = false;
10711117
}
1118+
1119+
private void PackageItemContainer_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
1120+
{
1121+
if (e.Key is not (VirtualKey.Up or VirtualKey.Down or VirtualKey.Home or VirtualKey.End) ||
1122+
sender is not PackageItemContainer packageItemContainer)
1123+
{
1124+
return;
1125+
}
1126+
1127+
int index = FilteredPackages.IndexOf(packageItemContainer.Wrapper);
1128+
switch (e.Key)
1129+
{
1130+
case VirtualKey.Up when index > 0:
1131+
SelectAndScrollTo(index - 1, true); break;
1132+
case VirtualKey.Down when index < FilteredPackages.Count - 1:
1133+
SelectAndScrollTo(index + 1, true); break;
1134+
case VirtualKey.Home when index > 0:
1135+
SelectAndScrollTo(0, true); break;
1136+
case VirtualKey.End when index < FilteredPackages.Count - 1:
1137+
SelectAndScrollTo(FilteredPackages.Count - 1, true); break;
1138+
}
1139+
e.Handled = true;
1140+
}
10721141
}
10731142
}

0 commit comments

Comments
 (0)