Skip to content

Commit ea2bdc7

Browse files
authored
Fix: Refactored ContextMenu (#12217)
1 parent f21737e commit ea2bdc7

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/Files.App/Shell/ContextMenu.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,7 @@ public async Task InvokeItem(int itemID)
165165

166166
public static async Task WarmUpQueryContextMenuAsync()
167167
{
168-
var thread = new ThreadWithMessageQueue();
169-
await thread.PostMethod(() =>
170-
{
171-
// Create a dummy context menu for warming up
172-
var shellItem = ShellFolderExtensions.GetShellItemFromPathOrPidl("C:\\");
173-
Shell32.IContextMenu menu = shellItem.Parent.GetChildrenUIObjects<Shell32.IContextMenu>(default, shellItem);
174-
menu.QueryContextMenu(User32.CreatePopupMenu(), 0, 1, 0x7FFF, Shell32.CMF.CMF_NORMAL);
175-
});
176-
thread.Dispose();
168+
using var cMenu = await GetContextMenuForFiles(new string[] { "C:\\" }, Shell32.CMF.CMF_NORMAL);
177169
}
178170

179171
#endregion FactoryMethods
@@ -262,18 +254,18 @@ private void EnumMenuItems(
262254

263255
if (loadSubenus)
264256
{
265-
LoadSubMenu(hSubMenu);
257+
LoadSubMenu();
266258
}
267259
else
268260
{
269-
loadSubMenuActions.Add(subItems, () => LoadSubMenu(hSubMenu));
261+
loadSubMenuActions.Add(subItems, LoadSubMenu);
270262
}
271263

272264
menuItem.SubItems = subItems;
273265

274266
Debug.WriteLine("Item {0}: done submenu", ii);
275267

276-
void LoadSubMenu(HMENU hSubMenu)
268+
void LoadSubMenu()
277269
{
278270
try
279271
{
@@ -300,24 +292,25 @@ void LoadSubMenu(HMENU hSubMenu)
300292

301293
public async Task<bool> LoadSubMenu(List<Win32ContextMenuItem> subItems)
302294
{
303-
return await owningThread.PostMethod<bool>(() =>
295+
if (loadSubMenuActions.Remove(subItems, out var loadSubMenuAction))
304296
{
305-
var result = loadSubMenuActions.Remove(subItems, out var loadSubMenuAction);
306-
307-
if (result)
297+
return await owningThread.PostMethod<bool>(() =>
308298
{
309299
try
310300
{
311301
loadSubMenuAction!();
302+
return true;
312303
}
313304
catch (COMException)
314305
{
315-
result = false;
306+
return false;
316307
}
317-
}
318-
319-
return result;
320-
});
308+
});
309+
}
310+
else
311+
{
312+
return false;
313+
}
321314
}
322315

323316
private static string? GetCommandString(Shell32.IContextMenu cMenu, uint offset, Shell32.GCS flags = Shell32.GCS.GCS_VERBW)

0 commit comments

Comments
 (0)