Skip to content

Commit 4f8aebf

Browse files
0x5bfayaira2
andauthored
Code Quality: Refactor context flyout helpers 1 (#14609)
Co-authored-by: Yair <[email protected]>
1 parent 5bd39d5 commit 4f8aebf

File tree

9 files changed

+165
-178
lines changed

9 files changed

+165
-178
lines changed

src/Files.App/Helpers/MenuFlyout/ContextFlyoutItemHelper.cs renamed to src/Files.App/Data/Factories/ContentPageContextFlyoutFactory.cs

Lines changed: 119 additions & 116 deletions
Large diffs are not rendered by default.

src/Files.App/Helpers/MenuFlyout/ShellContextMenuHelper.cs renamed to src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
namespace Files.App.Helpers
1717
{
18-
public static class ShellContextmenuHelper
18+
public static class ShellContextFlyoutFactory
1919
{
2020
public static IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
21+
2122
public static async Task<List<ContextMenuFlyoutItemViewModel>> GetShellContextmenuAsync(bool showOpenMenu, bool shiftPressed, string workingDirectory, List<ListedItem>? selectedItems, CancellationToken cancellationToken)
2223
{
2324
bool IsItemSelected = selectedItems?.Count > 0;
@@ -63,12 +64,13 @@ bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem)
6364
return menuItemsList;
6465
}
6566

66-
private static void LoadMenuFlyoutItem(IList<ContextMenuFlyoutItemViewModel> menuItemsListLocal,
67-
ContextMenu contextMenu,
68-
IEnumerable<Win32ContextMenuItem> menuFlyoutItems,
69-
CancellationToken cancellationToken,
70-
bool showIcons = true,
71-
int itemsBeforeOverflow = int.MaxValue)
67+
private static void LoadMenuFlyoutItem(
68+
IList<ContextMenuFlyoutItemViewModel> menuItemsListLocal,
69+
ContextMenu contextMenu,
70+
IEnumerable<Win32ContextMenuItem> menuFlyoutItems,
71+
CancellationToken cancellationToken,
72+
bool showIcons = true,
73+
int itemsBeforeOverflow = int.MaxValue)
7274
{
7375
if (cancellationToken.IsCancellationRequested)
7476
return;
@@ -215,6 +217,7 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
215217
var item = flyout.FirstOrDefault(x => x.Tag is Win32ContextMenuItem { CommandString: "openas" });
216218
if (item is not null)
217219
flyout.Remove(item);
220+
218221
return item?.Items;
219222
}
220223

@@ -223,6 +226,7 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
223226
var item = flyout.FirstOrDefault(x => x.Tag is Win32ContextMenuItem { CommandString: "sendto" });
224227
if (item is not null)
225228
flyout.Remove(item);
229+
226230
return item?.Items;
227231
}
228232

@@ -239,7 +243,7 @@ public static async Task LoadShellMenuItemsAsync(
239243
return;
240244

241245
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
242-
var shellMenuItems = await ContextFlyoutItemHelper.GetItemContextShellCommandsAsync(
246+
var shellMenuItems = await ContentPageContextFlyoutFactory.GetItemContextShellCommandsAsync(
243247
workingDir: null,
244248
new List<ListedItem>() { new ListedItem(null) { ItemPath = path } },
245249
shiftPressed: shiftPressed,
@@ -262,7 +266,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
262266
if (turnOnBitLocker is not null)
263267
shellMenuItems.Remove(turnOnBitLocker);
264268

265-
ContextFlyoutItemHelper.SwapPlaceholderWithShellOption(
269+
ContentPageContextFlyoutFactory.SwapPlaceholderWithShellOption(
266270
itemContextMenuFlyout,
267271
"TurnOnBitLockerPlaceholder",
268272
turnOnBitLocker,
@@ -273,7 +277,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
273277
if (manageBitLocker is not null)
274278
shellMenuItems.Remove(manageBitLocker);
275279

276-
ContextFlyoutItemHelper.SwapPlaceholderWithShellOption(
280+
ContentPageContextFlyoutFactory.SwapPlaceholderWithShellOption(
277281
itemContextMenuFlyout,
278282
"ManageBitLockerPlaceholder",
279283
manageBitLocker,
@@ -284,7 +288,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
284288

285289
if (!UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu)
286290
{
287-
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(shellMenuItems);
291+
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(shellMenuItems);
288292
if (secondaryElements.Any())
289293
{
290294
var openedPopups = Microsoft.UI.Xaml.Media.VisualTreeHelper.GetOpenPopups(MainWindow.Instance);
@@ -310,7 +314,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
310314
}
311315
else
312316
{
313-
var overflowItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(shellMenuItems);
317+
var overflowItems = ContextFlyoutModelToElementHelper.GetMenuFlyoutItemsFromModel(shellMenuItems);
314318
if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is not AppBarButton overflowItem
315319
|| itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") is not AppBarSeparator overflowSeparator)
316320
return;
@@ -334,7 +338,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
334338
{
335339
OpacityIconStyle = "ColorIconOpenWith",
336340
};
337-
var (_, openWithItems) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { openWithItem });
341+
var (_, openWithItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { openWithItem });
338342
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "OpenWithPlaceholder")).FirstOrDefault() as AppBarButton;
339343
if (placeholder is not null)
340344
placeholder.Visibility = Visibility.Collapsed;
@@ -346,7 +350,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
346350
{
347351
await sendToItem.LoadSubMenuAction();
348352

349-
var (_, sendToItems) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { sendToItem });
353+
var (_, sendToItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { sendToItem });
350354
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "SendToPlaceholder")).FirstOrDefault() as AppBarButton;
351355
if (placeholder is not null)
352356
placeholder.Visibility = Visibility.Collapsed;
@@ -378,7 +382,7 @@ public static void AddItemsToMainMenu(IEnumerable<ICommandBarElement> mainMenu,
378382
if (appBarButton is not null)
379383
{
380384
var ctxFlyout = new MenuFlyout();
381-
ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(viewModel.Items)?.ForEach(i => ctxFlyout.Items.Add(i));
385+
ContextFlyoutModelToElementHelper.GetMenuFlyoutItemsFromModel(viewModel.Items)?.ForEach(i => ctxFlyout.Items.Add(i));
382386
appBarButton.Flyout = ctxFlyout;
383387
appBarButton.Visibility = Visibility.Collapsed;
384388
appBarButton.Visibility = Visibility.Visible;
@@ -392,7 +396,7 @@ public static void AddItemsToOverflowMenu(AppBarButton? overflowItem, ContextMen
392396
var flyoutSubItem = flyout.Items.FirstOrDefault(x => x.Tag == viewModel.Tag) as MenuFlyoutSubItem;
393397
if (flyoutSubItem is not null)
394398
{
395-
viewModel.Items.ForEach(i => flyoutSubItem.Items.Add(ItemModelListToContextFlyoutHelper.GetMenuItem(i)));
399+
viewModel.Items.ForEach(i => flyoutSubItem.Items.Add(ContextFlyoutModelToElementHelper.GetMenuItem(i)));
396400
flyout.Items[flyout.Items.IndexOf(flyoutSubItem) + 1].Visibility = Visibility.Collapsed;
397401
flyoutSubItem.Visibility = Visibility.Visible;
398402
}

src/Files.App/Helpers/MenuFlyout/ItemModelListToContextFlyoutHelper.cs renamed to src/Files.App/Helpers/MenuFlyout/ContextFlyoutModelToElementHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
namespace Files.App.Helpers.ContextFlyouts
99
{
1010
/// <summary>
11-
/// This helper class is used to convert ContextMenuFlyoutItemViewModels into a control that can be displayed to the user.
12-
/// This is for use in scenarios where XAML templates and data binding will not suffice.
13-
/// <see cref="Files.App.ViewModels.ContextMenuFlyoutItemViewModel"/>
11+
/// Provides static helper for conversion from ContextMenuFlyoutItemViewModels into a control.
1412
/// </summary>
15-
public static class ItemModelListToContextFlyoutHelper
13+
public static class ContextFlyoutModelToElementHelper
1614
{
1715
public static List<MenuFlyoutItemBase>? GetMenuFlyoutItemsFromModel(List<ContextMenuFlyoutItemViewModel>? items)
1816
{

src/Files.App/Helpers/MenuFlyout/MenuFlyoutHelper.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using CommunityToolkit.WinUI;
5-
using Files.App.Extensions;
64
using Microsoft.UI.Xaml;
75
using Microsoft.UI.Xaml.Controls;
8-
using System;
9-
using System.Collections.Generic;
10-
using System.Threading.Tasks;
116
using System.Windows.Input;
127

138
namespace Files.App.Helpers
149
{
10+
[Obsolete("Must not use this helper to generate menu flyout any longer.")]
1511
public class MenuFlyoutHelper : DependencyObject
1612
{
17-
#region View Models
18-
1913
public interface IMenuFlyoutItemViewModel { }
2014

2115
public class MenuFlyoutSeparatorViewModel : IMenuFlyoutItemViewModel { }
@@ -56,10 +50,6 @@ public MenuFlyoutFactoryItemViewModel(Func<MenuFlyoutItemBase> factoryFunc)
5650
=> Build = factoryFunc;
5751
}
5852

59-
#endregion View Models
60-
61-
#region ItemsSource
62-
6353
public static IEnumerable<IMenuFlyoutItemViewModel> GetItemsSource(DependencyObject obj) => obj.GetValue(ItemsSourceProperty) as IEnumerable<IMenuFlyoutItemViewModel>;
6454

6555
public static void SetItemsSource(DependencyObject obj, IEnumerable<IMenuFlyoutItemViewModel> value) => obj.SetValue(ItemsSourceProperty, value);
@@ -68,10 +58,6 @@ public MenuFlyoutFactoryItemViewModel(Func<MenuFlyoutItemBase> factoryFunc)
6858

6959
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => SetupItemsAsync(d as MenuFlyout);
7060

71-
#endregion ItemsSource
72-
73-
#region IsVisible
74-
7561
public static bool GetIsVisible(DependencyObject d) => (bool)d.GetValue(IsVisibleProperty);
7662

7763
public static void SetIsVisible(DependencyObject d, bool value) => d.SetValue(IsVisibleProperty, value);
@@ -81,9 +67,7 @@ public MenuFlyoutFactoryItemViewModel(Func<MenuFlyoutItemBase> factoryFunc)
8167
private static void OnIsVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
8268
{
8369
if (d is not MenuFlyout flyout)
84-
{
8570
return;
86-
}
8771

8872
var boolValue = (bool)e.NewValue;
8973

@@ -92,8 +76,6 @@ private static void OnIsVisiblePropertyChanged(DependencyObject d, DependencyPro
9276
flyout.Hide();
9377
}
9478

95-
#endregion IsVisible
96-
9779
private static async Task SetupItemsAsync(MenuFlyout menu)
9880
{
9981
if (menu is null || Windows.ApplicationModel.DesignMode.DesignModeEnabled)
@@ -153,4 +135,4 @@ private static void AddItems(IList<MenuFlyoutItemBase> menu, IEnumerable<IMenuFl
153135
}
154136
}
155137
}
156-
}
138+
}

src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void AdaptiveGridView_RightTapped(object sender, RightTappedRoutedEventA
129129

130130
// Get items for the flyout
131131
var menuItems = GetItemMenuItems(item, QuickAccessService.IsItemPinned(item.Path), item.IsFolder);
132-
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
132+
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(menuItems);
133133

134134
// Set max width of the flyout
135135
secondaryElements
@@ -143,7 +143,7 @@ private void AdaptiveGridView_RightTapped(object sender, RightTappedRoutedEventA
143143
itemContextMenuFlyout.ShowAt(element, new() { Position = e.GetPosition(element) });
144144

145145
// Load shell menu items
146-
_ = ShellContextmenuHelper.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
146+
_ = ShellContextFlyoutFactory.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
147147

148148
e.Handled = true;
149149
}

src/Files.App/UserControls/Widgets/HomePageWidget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
6868

6969
// Get items for the flyout
7070
var menuItems = GetItemMenuItems(item, QuickAccessService.IsItemPinned(item.Path));
71-
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
71+
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(menuItems);
7272

7373
// Set max width of the flyout
7474
secondaryElements
@@ -82,7 +82,7 @@ public void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)
8282
itemContextMenuFlyout.ShowAt(widgetCardItem, new() { Position = e.GetPosition(widgetCardItem) });
8383

8484
// Load shell menu items
85-
_ = ShellContextmenuHelper.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
85+
_ = ShellContextFlyoutFactory.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
8686

8787
e.Handled = true;
8888
}

src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void ListView_RightTapped(object sender, RightTappedRoutedEventArgs e)
134134

135135
// Get items for the flyout
136136
var menuItems = GetItemMenuItems(item, QuickAccessService.IsItemPinned(item.Path));
137-
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
137+
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(menuItems);
138138

139139
// Set max width of the flyout
140140
secondaryElements
@@ -148,7 +148,7 @@ private void ListView_RightTapped(object sender, RightTappedRoutedEventArgs e)
148148
itemContextMenuFlyout.ShowAt(element, new() { Position = e.GetPosition(element) });
149149

150150
// Load shell menu items
151-
_ = ShellContextmenuHelper.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
151+
_ = ShellContextFlyoutFactory.LoadShellMenuItemsAsync(FlyoutItemPath, itemContextMenuFlyout);
152152
}
153153

154154
public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned, bool isFolder = false)

src/Files.App/ViewModels/UserControls/SidebarViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public async void HandleItemContextInvokedAsync(object sender, ItemContextInvoke
702702
itemContextMenuFlyout.Closed += (sender, e) => RightClickedItemChanged?.Invoke(this, null);
703703

704704
var menuItems = GetLocationItemMenuItems(item, itemContextMenuFlyout);
705-
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
705+
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(menuItems);
706706

707707
secondaryElements
708708
.OfType<FrameworkElement>()
@@ -722,7 +722,7 @@ private async void ItemContextMenuFlyout_Opened(object? sender, object e)
722722
return;
723723

724724
itemContextMenuFlyout.Opened -= ItemContextMenuFlyout_Opened;
725-
await ShellContextmenuHelper.LoadShellMenuItemsAsync(rightClickedItem.Path, itemContextMenuFlyout, rightClickedItem.MenuOptions);
725+
await ShellContextFlyoutFactory.LoadShellMenuItemsAsync(rightClickedItem.Path, itemContextMenuFlyout, rightClickedItem.MenuOptions);
726726
}
727727

728728
public async void HandleItemInvokedAsync(object item, PointerUpdateKind pointerUpdateKind)

0 commit comments

Comments
 (0)