Skip to content

Commit

Permalink
Fix: Refactored ContextMenu (#12217)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu authored Apr 27, 2023
1 parent f21737e commit ea2bdc7
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/Files.App/Shell/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,7 @@ public async Task InvokeItem(int itemID)

public static async Task WarmUpQueryContextMenuAsync()
{
var thread = new ThreadWithMessageQueue();
await thread.PostMethod(() =>
{
// Create a dummy context menu for warming up
var shellItem = ShellFolderExtensions.GetShellItemFromPathOrPidl("C:\\");
Shell32.IContextMenu menu = shellItem.Parent.GetChildrenUIObjects<Shell32.IContextMenu>(default, shellItem);
menu.QueryContextMenu(User32.CreatePopupMenu(), 0, 1, 0x7FFF, Shell32.CMF.CMF_NORMAL);
});
thread.Dispose();
using var cMenu = await GetContextMenuForFiles(new string[] { "C:\\" }, Shell32.CMF.CMF_NORMAL);
}

#endregion FactoryMethods
Expand Down Expand Up @@ -262,18 +254,18 @@ private void EnumMenuItems(

if (loadSubenus)
{
LoadSubMenu(hSubMenu);
LoadSubMenu();
}
else
{
loadSubMenuActions.Add(subItems, () => LoadSubMenu(hSubMenu));
loadSubMenuActions.Add(subItems, LoadSubMenu);
}

menuItem.SubItems = subItems;

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

void LoadSubMenu(HMENU hSubMenu)
void LoadSubMenu()
{
try
{
Expand All @@ -300,24 +292,25 @@ void LoadSubMenu(HMENU hSubMenu)

public async Task<bool> LoadSubMenu(List<Win32ContextMenuItem> subItems)
{
return await owningThread.PostMethod<bool>(() =>
if (loadSubMenuActions.Remove(subItems, out var loadSubMenuAction))
{
var result = loadSubMenuActions.Remove(subItems, out var loadSubMenuAction);

if (result)
return await owningThread.PostMethod<bool>(() =>
{
try
{
loadSubMenuAction!();
return true;
}
catch (COMException)
{
result = false;
return false;
}
}

return result;
});
});
}
else
{
return false;
}
}

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

0 comments on commit ea2bdc7

Please sign in to comment.