Skip to content

Commit 2ec8c44

Browse files
committed
Implement button context menu on gridview
1 parent 408113f commit 2ec8c44

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/UniGetUI/Controls/ObservablePackageCollection.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UniGetUI.Core.Classes;
2+
using UniGetUI.Interface;
23
using UniGetUI.PackageEngine.Interfaces;
34

45
namespace UniGetUI.PackageEngine.PackageClasses
@@ -26,9 +27,9 @@ public ObservablePackageCollection()
2627
/// <summary>
2728
/// Add a package to the collection
2829
/// </summary>
29-
public void Add(IPackage p)
30+
public void Add(IPackage p, AbstractPackagesPage page)
3031
{
31-
base.Add(new PackageWrapper(p));
32+
base.Add(new PackageWrapper(p, page));
3233
}
3334

3435
/// <summary>

src/UniGetUI/Controls/PackageWrapper.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.UI.Xaml.Media.Imaging;
66
using UniGetUI.Core.Classes;
77
using UniGetUI.Core.Tools;
8+
using UniGetUI.Interface;
89
using UniGetUI.Interface.Enums;
910
using UniGetUI.PackageEngine.Interfaces;
1011

@@ -58,16 +59,24 @@ public Uri? PackageIcon
5859
public IPackage Package { get; private set; }
5960
public PackageWrapper Self { get; private set; }
6061

61-
public PackageWrapper(IPackage package)
62+
private readonly AbstractPackagesPage _page;
63+
64+
public PackageWrapper(IPackage package, AbstractPackagesPage page)
6265
{
6366
Package = package;
6467
Self = this;
68+
_page = page;
6569
WhenTagHasChanged();
6670
Package.PropertyChanged += Package_PropertyChanged;
6771
UpdatePackageIcon();
6872
VersionComboString = package.IsUpgradable ? $"{package.VersionString} -> {package.NewVersionString}" : package.VersionString;
6973
}
7074

75+
public void RightClick()
76+
{
77+
_page.ShowContextMenu(this);
78+
}
79+
7180
public void Package_PropertyChanged(object? sender, PropertyChangedEventArgs e)
7281
{
7382
try

src/UniGetUI/Pages/SoftwarePages/AbstractPackagesPage.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,8 @@
912912
Padding="0"
913913
Background="Transparent"
914914
BorderThickness="0"
915-
Click="PackageItemContainer_RightTapped">
915+
Click="{x:Bind RightClick}"
916+
Tapped="ContextMenuButton_Tapped">
916917
<SymbolIcon Symbol="More" />
917918
</Button>
918919
</StackPanel>
@@ -1015,9 +1016,9 @@
10151016
Grid.Row="0"
10161017
Grid.RowSpan="3"
10171018
Grid.Column="2"
1019+
Margin="0,0,4,0"
10181020
HorizontalAlignment="Stretch"
10191021
Orientation="Vertical"
1020-
Margin="0,0,4,0"
10211022
Spacing="8">
10221023

10231024
<Grid HorizontalAlignment="Stretch">
@@ -1482,7 +1483,7 @@
14821483
<ItemsView
14831484
x:Name="PackageList"
14841485
Grid.Row="1"
1485-
Padding="4,0,4,0"
1486+
Padding="4,0"
14861487
HorizontalAlignment="Stretch"
14871488
VerticalAlignment="Stretch"
14881489
x:FieldModifier="protected"

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
using Microsoft.UI.Xaml.Media;
2323
using Windows.UI;
2424
using Windows.Graphics.Printing.PrintSupport;
25+
using System.ComponentModel;
26+
using System.Threading.Tasks;
27+
using Microsoft.UI.Xaml.Controls.Primitives;
2528

2629
// To learn more about WinUI, the WinUI project structure,
2730
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -336,6 +339,7 @@ private void Loader_PackagesChanged(object? sender, EventArgs e)
336339
_ = LoadIconsForNewPackages();
337340
}
338341

342+
339343
private void Loader_FinishedLoading(object? sender, EventArgs e)
340344
{
341345
// Ensure we are in the UI thread
@@ -732,7 +736,7 @@ public void FilterPackages()
732736
{
733737
if (VisibleSources.Contains(match.Source) || (!match.Manager.Capabilities.SupportsCustomSources && VisibleManagers.Contains(match.Manager)))
734738
{
735-
FilteredPackages.Add(match);
739+
FilteredPackages.Add(match, this);
736740
}
737741
}
738742
FilteredPackages.BlockSorting = false;
@@ -920,6 +924,16 @@ protected void PerformMainPackageAction(IPackage? package)
920924
public void FocusPackageList()
921925
{ PackageList.Focus(FocusState.Programmatic); }
922926

927+
928+
public async Task ShowContextMenu(PackageWrapper wrapper)
929+
{
930+
PackageList.Select(wrapper.Index);
931+
await Task.Delay(20);
932+
if(_lastContextMenuButtonTapped is not null)
933+
(PackageList.ContextFlyout as BetterMenu)?.ShowAt(_lastContextMenuButtonTapped, new FlyoutShowOptions { Placement = FlyoutPlacementMode.RightEdgeAlignedTop });
934+
WhenShowingContextMenu(wrapper.Package);
935+
}
936+
923937
private void PackageItemContainer_RightTapped(object sender, RightTappedRoutedEventArgs e)
924938
=> PackageItemContainer_RightTapped(sender, new RoutedEventArgs());
925939

@@ -1247,5 +1261,12 @@ private void ViewModeSelector_SelectionChanged(object sender, SelectionChangedEv
12471261
else if (ViewModeSelector.SelectedIndex == 2)
12481262
LoadGridLayout();
12491263
}
1264+
1265+
FrameworkElement _lastContextMenuButtonTapped = null;
1266+
private void ContextMenuButton_Tapped(object sender, TappedRoutedEventArgs e)
1267+
{
1268+
if (sender is FrameworkElement el)
1269+
_lastContextMenuButtonTapped = el;
1270+
}
12501271
}
12511272
}

0 commit comments

Comments
 (0)