-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ShellContextFlyoutHelper.cs
408 lines (349 loc) · 15.8 KB
/
ShellContextFlyoutHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
using CommunityToolkit.WinUI.UI;
using Files.App.Helpers.ContextFlyouts;
using Files.Shared.Helpers;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using System.IO;
using Vanara.PInvoke;
using Windows.System;
using Windows.UI.Core;
using static Vanara.PInvoke.Kernel32;
namespace Files.App.Helpers
{
public static class ShellContextFlyoutFactory
{
public static IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
public static async Task<List<ContextMenuFlyoutItemViewModel>> GetShellContextmenuAsync(bool showOpenMenu, bool shiftPressed, string workingDirectory, List<ListedItem>? selectedItems, CancellationToken cancellationToken)
{
bool IsItemSelected = selectedItems?.Count > 0;
var menuItemsList = new List<ContextMenuFlyoutItemViewModel>();
var filePaths = IsItemSelected
? selectedItems!.Select(x => x.ItemPath).ToArray()
: new[] { workingDirectory };
Func<string, bool> FilterMenuItems(bool showOpenMenu)
{
var knownItems = new HashSet<string>()
{
"opennew", "opencontaining", "opennewprocess",
"runas", "runasuser", "pintohome", "PinToStartScreen",
"cut", "copy", "paste", "delete", "properties", "link",
"Windows.ModernShare", "Windows.Share", "setdesktopwallpaper",
"eject", "rename", "explore", "openinfiles", "extract",
"copyaspath", "undelete", "empty", "format", "rotate90", "rotate270",
Win32Helper.ExtractStringFromDLL("shell32.dll", 34593), // Add to collection
Win32Helper.ExtractStringFromDLL("shell32.dll", 5384), // Pin to Start
Win32Helper.ExtractStringFromDLL("shell32.dll", 5385), // Unpin from Start
Win32Helper.ExtractStringFromDLL("shell32.dll", 5386), // Pin to taskbar
Win32Helper.ExtractStringFromDLL("shell32.dll", 5387), // Unpin from taskbar
};
bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem)
&& (knownItems.Contains(menuItem) || (!showOpenMenu && menuItem.Equals("open", StringComparison.OrdinalIgnoreCase)));
return filterMenuItemsImpl;
}
var contextMenu = await ContextMenu.GetContextMenuForFiles(filePaths,
shiftPressed ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu));
if (contextMenu is not null)
LoadMenuFlyoutItem(menuItemsList, contextMenu, contextMenu.Items, cancellationToken, true);
if (cancellationToken.IsCancellationRequested)
menuItemsList.Clear();
return menuItemsList;
}
private static void LoadMenuFlyoutItem(
IList<ContextMenuFlyoutItemViewModel> menuItemsListLocal,
ContextMenu contextMenu,
IEnumerable<Win32ContextMenuItem> menuFlyoutItems,
CancellationToken cancellationToken,
bool showIcons = true,
int itemsBeforeOverflow = int.MaxValue)
{
if (cancellationToken.IsCancellationRequested)
return;
var itemsCount = 0; // Separators do not count for reaching the overflow threshold
var menuItems = menuFlyoutItems.TakeWhile(x => x.Type == MenuItemType.MFT_SEPARATOR || ++itemsCount <= itemsBeforeOverflow).ToList();
var overflowItems = menuFlyoutItems.Except(menuItems).ToList();
if (overflowItems.Where(x => x.Type != MenuItemType.MFT_SEPARATOR).Any())
{
var moreItem = menuItemsListLocal.Where(x => x.ID == "ItemOverflow").FirstOrDefault();
if (moreItem is null)
{
var menuLayoutSubItem = new ContextMenuFlyoutItemViewModel()
{
Text = "ShowMoreOptions".GetLocalizedResource(),
Glyph = "\xE712",
};
LoadMenuFlyoutItem(menuLayoutSubItem.Items, contextMenu, overflowItems, cancellationToken, showIcons);
menuItemsListLocal.Insert(0, menuLayoutSubItem);
}
else
{
LoadMenuFlyoutItem(moreItem.Items, contextMenu, overflowItems, cancellationToken, showIcons);
}
}
foreach (var menuFlyoutItem in menuItems
.SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR) // Remove leading separators
.Reverse()
.SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR)) // Remove trailing separators
{
if (cancellationToken.IsCancellationRequested)
break;
// Avoid duplicate separators
if ((menuFlyoutItem.Type == MenuItemType.MFT_SEPARATOR) && (menuItemsListLocal.FirstOrDefault()?.ItemType == ContextMenuFlyoutItemType.Separator))
continue;
BitmapImage? image = null;
if (showIcons && menuFlyoutItem.Icon is { Length: > 0 })
{
image = new BitmapImage();
using var ms = new MemoryStream(menuFlyoutItem.Icon);
image.SetSourceAsync(ms.AsRandomAccessStream()).AsTask().Wait(10);
}
if (menuFlyoutItem.Type is MenuItemType.MFT_SEPARATOR)
{
var menuLayoutItem = new ContextMenuFlyoutItemViewModel()
{
ItemType = ContextMenuFlyoutItemType.Separator,
Tag = menuFlyoutItem
};
menuItemsListLocal.Insert(0, menuLayoutItem);
}
else if (!string.IsNullOrEmpty(menuFlyoutItem.Label) && menuFlyoutItem.SubItems is not null)
{
if (string.Equals(menuFlyoutItem.Label, Win32Helper.ExtractStringFromDLL("shell32.dll", 30312)))
menuFlyoutItem.CommandString = "sendto";
var menuLayoutSubItem = new ContextMenuFlyoutItemViewModel()
{
Text = menuFlyoutItem.Label.Replace("&", "", StringComparison.Ordinal),
Tag = menuFlyoutItem,
BitmapIcon = image,
Items = new List<ContextMenuFlyoutItemViewModel>(),
};
if (menuFlyoutItem.SubItems.Any())
{
LoadMenuFlyoutItem(menuLayoutSubItem.Items, contextMenu, menuFlyoutItem.SubItems, cancellationToken, showIcons);
}
else
{
menuLayoutSubItem.LoadSubMenuAction = async () =>
{
if (await contextMenu.LoadSubMenu(menuFlyoutItem.SubItems))
LoadMenuFlyoutItem(menuLayoutSubItem.Items, contextMenu, menuFlyoutItem.SubItems, cancellationToken, showIcons);
};
}
menuItemsListLocal.Insert(0, menuLayoutSubItem);
}
else if (!string.IsNullOrEmpty(menuFlyoutItem.Label))
{
var menuLayoutItem = new ContextMenuFlyoutItemViewModel
{
Text = menuFlyoutItem.Label.Replace("&", "", StringComparison.Ordinal),
Tag = menuFlyoutItem,
BitmapIcon = image,
Command = new AsyncRelayCommand<object>(x => InvokeShellMenuItemAsync(contextMenu, x)),
CommandParameter = menuFlyoutItem
};
menuItemsListLocal.Insert(0, menuLayoutItem);
}
}
async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
{
if (tag is not Win32ContextMenuItem menuItem)
return;
var menuId = menuItem.ID;
var isFont = FileExtensionHelpers.IsFontFile(contextMenu.ItemsPath[0]);
var verb = menuItem.CommandString;
switch (verb)
{
case "install" when isFont:
await Win32Helper.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), false);
break;
case "installAllUsers" when isFont:
await Win32Helper.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), true);
break;
case "mount":
var vhdPath = contextMenu.ItemsPath[0];
await Win32Helper.MountVhdDisk(vhdPath);
break;
case "format":
var drivePath = contextMenu.ItemsPath[0];
await Win32Helper.OpenFormatDriveDialog(drivePath);
break;
default:
await contextMenu.InvokeItem(menuId);
break;
}
//contextMenu.Dispose(); // Prevents some menu items from working (TBC)
}
}
public static List<ContextMenuFlyoutItemViewModel>? GetOpenWithItems(List<ContextMenuFlyoutItemViewModel> flyout)
{
var item = flyout.FirstOrDefault(x => x.Tag is Win32ContextMenuItem { CommandString: "openas" });
if (item is not null)
flyout.Remove(item);
return item?.Items;
}
public static List<ContextMenuFlyoutItemViewModel>? GetSendToItems(List<ContextMenuFlyoutItemViewModel> flyout)
{
var item = flyout.FirstOrDefault(x => x.Tag is Win32ContextMenuItem { CommandString: "sendto" });
if (item is not null)
flyout.Remove(item);
return item?.Items;
}
public static async Task LoadShellMenuItemsAsync(
string path,
CommandBarFlyout itemContextMenuFlyout,
ContextMenuOptions? options = null,
bool showOpenWithMenu = false,
bool showSendToMenu = false)
{
try
{
if (options is not null && !options.IsLocationItem)
return;
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var shellMenuItems = await ContentPageContextFlyoutFactory.GetItemContextShellCommandsAsync(
workingDir: null,
new List<ListedItem>() { new ListedItem(null) { ItemPath = path } },
shiftPressed: shiftPressed,
showOpenMenu: false,
default);
var openWithItem = showOpenWithMenu ? shellMenuItems.Where(x => (x.Tag as Win32ContextMenuItem)?.CommandString == "openas").ToList().FirstOrDefault() : null;
if (openWithItem is not null)
shellMenuItems.Remove(openWithItem);
var sendToItem = shellMenuItems.Where(x => (x.Tag as Win32ContextMenuItem)?.CommandString == "sendto").ToList().FirstOrDefault();
if (sendToItem is not null &&
(showSendToMenu || !UserSettingsService.GeneralSettingsService.ShowSendToMenu))
shellMenuItems.Remove(sendToItem);
var turnOnBitLocker = shellMenuItems.FirstOrDefault(x =>
x.Tag is Win32ContextMenuItem menuItem &&
(menuItem.CommandString?.StartsWith("encrypt-bde") ?? false));
if (turnOnBitLocker is not null)
shellMenuItems.Remove(turnOnBitLocker);
ContentPageContextFlyoutFactory.SwapPlaceholderWithShellOption(
itemContextMenuFlyout,
"TurnOnBitLockerPlaceholder",
turnOnBitLocker,
itemContextMenuFlyout.SecondaryCommands.Count - 2
);
var manageBitLocker = shellMenuItems.FirstOrDefault(x => x.Tag is Win32ContextMenuItem { CommandString: "manage-bde" });
if (manageBitLocker is not null)
shellMenuItems.Remove(manageBitLocker);
var lastItem = shellMenuItems.LastOrDefault();
while (lastItem?.ItemType is ContextMenuFlyoutItemType.Separator)
{
shellMenuItems.Remove(lastItem);
lastItem = shellMenuItems.LastOrDefault();
}
ContentPageContextFlyoutFactory.SwapPlaceholderWithShellOption(
itemContextMenuFlyout,
"ManageBitLockerPlaceholder",
manageBitLocker,
itemContextMenuFlyout.SecondaryCommands.Count - 2
);
sendToItem = showSendToMenu && UserSettingsService.GeneralSettingsService.ShowSendToMenu ? sendToItem : null;
if (!UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu)
{
var (_, secondaryElements) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(shellMenuItems);
if (secondaryElements.Any())
{
var openedPopups = Microsoft.UI.Xaml.Media.VisualTreeHelper.GetOpenPopups(MainWindow.Instance);
var secondaryMenu = openedPopups.FirstOrDefault(popup => popup.Name == "OverflowPopup");
var itemsControl = secondaryMenu?.Child.FindDescendant<ItemsControl>();
if (itemsControl is not null)
{
var maxWidth = itemsControl.ActualWidth - Constants.UI.ContextMenuLabelMargin;
secondaryElements.OfType<FrameworkElement>()
.ForEach(x => x.MaxWidth = maxWidth); // Set items max width to current menu width (#5555)
}
secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));
}
else if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") is AppBarSeparator overflowSeparator)
{
overflowSeparator.Visibility = Visibility.Collapsed;
}
if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is AppBarButton overflowItem)
overflowItem.Visibility = Visibility.Collapsed;
}
else
{
var overflowItems = ContextFlyoutModelToElementHelper.GetMenuFlyoutItemsFromModel(shellMenuItems);
if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is not AppBarButton overflowItem
|| itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") is not AppBarSeparator overflowSeparator)
return;
var flyoutItems = (overflowItem.Flyout as MenuFlyout)?.Items;
if (flyoutItems is not null)
overflowItems?.ForEach(i => flyoutItems.Add(i));
overflowItem.Visibility = overflowItems?.Any() ?? false ? Visibility.Visible : Visibility.Collapsed;
overflowSeparator.Visibility = overflowItems?.Any() ?? false ? Visibility.Visible : Visibility.Collapsed;
overflowItem.Label = "ShowMoreOptions".GetLocalizedResource();
overflowItem.IsEnabled = true;
}
// Add items to openwith dropdown
if (openWithItem?.LoadSubMenuAction is not null)
{
await openWithItem.LoadSubMenuAction();
openWithItem.OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenWith",
};
var (_, openWithItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { openWithItem });
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "OpenWithPlaceholder")).FirstOrDefault() as AppBarButton;
if (placeholder is not null)
placeholder.Visibility = Visibility.Collapsed;
itemContextMenuFlyout.SecondaryCommands.Insert(0, openWithItems.FirstOrDefault());
}
// Add items to sendto dropdown
if (sendToItem?.LoadSubMenuAction is not null)
{
await sendToItem.LoadSubMenuAction();
var (_, sendToItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { sendToItem });
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "SendToPlaceholder")).FirstOrDefault() as AppBarButton;
if (placeholder is not null)
placeholder.Visibility = Visibility.Collapsed;
itemContextMenuFlyout.SecondaryCommands.Insert(1, sendToItems.FirstOrDefault());
}
// Add items to shell submenu
shellMenuItems.Where(x => x.LoadSubMenuAction is not null).ForEach(async x =>
{
await x.LoadSubMenuAction();
if (!UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu)
{
AddItemsToMainMenu(itemContextMenuFlyout.SecondaryCommands, x);
}
else if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is AppBarButton overflowItem)
{
AddItemsToOverflowMenu(overflowItem, x);
}
});
}
catch { }
}
public static void AddItemsToMainMenu(IEnumerable<ICommandBarElement> mainMenu, ContextMenuFlyoutItemViewModel viewModel)
{
var appBarButton = mainMenu.FirstOrDefault(x => (x as AppBarButton)?.Tag == viewModel.Tag) as AppBarButton;
if (appBarButton is not null)
{
var ctxFlyout = new MenuFlyout();
ContextFlyoutModelToElementHelper.GetMenuFlyoutItemsFromModel(viewModel.Items)?.ForEach(i => ctxFlyout.Items.Add(i));
appBarButton.Flyout = ctxFlyout;
appBarButton.Visibility = Visibility.Collapsed;
appBarButton.Visibility = Visibility.Visible;
}
}
public static void AddItemsToOverflowMenu(AppBarButton? overflowItem, ContextMenuFlyoutItemViewModel viewModel)
{
if (overflowItem?.Flyout is MenuFlyout flyout)
{
var flyoutSubItem = flyout.Items.FirstOrDefault(x => x.Tag == viewModel.Tag) as MenuFlyoutSubItem;
if (flyoutSubItem is not null)
{
viewModel.Items.ForEach(i => flyoutSubItem.Items.Add(ContextFlyoutModelToElementHelper.GetMenuItem(i)));
flyout.Items[flyout.Items.IndexOf(flyoutSubItem) + 1].Visibility = Visibility.Collapsed;
flyoutSubItem.Visibility = Visibility.Visible;
}
}
}
}
}