From 5425905c92677e3820761abe9419ac26ed65b645 Mon Sep 17 00:00:00 2001 From: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Date: Thu, 27 Apr 2023 12:30:50 +0900 Subject: [PATCH] Refactored ContextMenu --- src/Files.App/Shell/ContextMenu.cs | 35 ++++++++++++------------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/Files.App/Shell/ContextMenu.cs b/src/Files.App/Shell/ContextMenu.cs index 4a2ababf5b29..f1e406b63ae9 100644 --- a/src/Files.App/Shell/ContextMenu.cs +++ b/src/Files.App/Shell/ContextMenu.cs @@ -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(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 @@ -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 { @@ -300,24 +292,25 @@ void LoadSubMenu(HMENU hSubMenu) public async Task LoadSubMenu(List subItems) { - return await owningThread.PostMethod(() => + if (loadSubMenuActions.Remove(subItems, out var loadSubMenuAction)) { - var result = loadSubMenuActions.Remove(subItems, out var loadSubMenuAction); - - if (result) + return await owningThread.PostMethod(() => { 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)