From e5e6f21ce3126297744de8da402ac0e0db044b8a Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 11:56:42 +0800 Subject: [PATCH 1/7] Code Quality: Removed Vanara.PInvoke.Shell32.CMF import --- .../Factories/ShellContextFlyoutHelper.cs | 4 +--- src/Files.App/Data/Items/ContextMenu.cs | 21 +++++++++++++++++++ src/Files.App/Utils/Shell/ContextMenu.cs | 12 +++++------ src/Files.App/Utils/Shell/LaunchHelper.cs | 4 ++-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs index 64b3ddf39e90..046dc7cf3581 100644 --- a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs +++ b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs @@ -9,10 +9,8 @@ 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 { @@ -55,7 +53,7 @@ bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem) } var contextMenu = await ContextMenu.GetContextMenuForFiles(filePaths, - shiftPressed ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu)); + shiftPressed ? CMF.CMF_EXTENDEDVERBS : CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu)); if (contextMenu is not null) LoadMenuFlyoutItem(menuItemsList, contextMenu, contextMenu.Items, cancellationToken, true); diff --git a/src/Files.App/Data/Items/ContextMenu.cs b/src/Files.App/Data/Items/ContextMenu.cs index b110d4b61209..cb6fa03f9c3a 100644 --- a/src/Files.App/Data/Items/ContextMenu.cs +++ b/src/Files.App/Data/Items/ContextMenu.cs @@ -32,6 +32,27 @@ public enum HBITMAP_HMENU : long HBMMENU_SYSTEM = 1 } + // Same definition of Vanara.PInvoke.Shell32.CMF + public enum CMF : uint + { + CMF_NORMAL = 0x00000000, + CMF_DEFAULTONLY = 0x00000001, + CMF_VERBSONLY = 0x00000002, + CMF_EXPLORE = 0x00000004, + CMF_NOVERBS = 0x00000008, + CMF_CANRENAME = 0x00000010, + CMF_NODEFAULT = 0x00000020, + CMF_EXTENDEDVERBS = 0x00000100, + CMF_INCLUDESTATIC = 0x00000040, + CMF_ITEMMENU = 0x00000080, + CMF_DISABLEDVERBS = 0x00000200, + CMF_ASYNCVERBSTATE = 0x00000400, + CMF_OPTIMIZEFORINVOKE = 0x00000800, + CMF_SYNCCASCADEMENU = 0x00001000, + CMF_DONOTPICKDEFAULT = 0x00002000, + CMF_RESERVED = 0xffff0000, + } + public class Win32ContextMenu { public List Items { get; set; } diff --git a/src/Files.App/Utils/Shell/ContextMenu.cs b/src/Files.App/Utils/Shell/ContextMenu.cs index dbef74c3696c..51e5151878e1 100644 --- a/src/Files.App/Utils/Shell/ContextMenu.cs +++ b/src/Files.App/Utils/Shell/ContextMenu.cs @@ -43,7 +43,7 @@ private ContextMenu(Shell32.IContextMenu cMenu, User32.SafeHMENU hMenu, IEnumera public async static Task InvokeVerb(string verb, params string[] filePaths) { - using var cMenu = await GetContextMenuForFiles(filePaths, Shell32.CMF.CMF_DEFAULTONLY); + using var cMenu = await GetContextMenuForFiles(filePaths, CMF.CMF_DEFAULTONLY); return cMenu is not null && await cMenu.InvokeVerb(verb); } @@ -112,7 +112,7 @@ public async Task InvokeItem(int itemID) return false; } - public async static Task GetContextMenuForFiles(string[] filePathList, Shell32.CMF flags, Func? itemFilter = null) + public async static Task GetContextMenuForFiles(string[] filePathList, CMF flags, Func? itemFilter = null) { var owningThread = new ThreadWithMessageQueue(); @@ -140,14 +140,14 @@ public async Task InvokeItem(int itemID) }); } - public async static Task GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, Func? itemFilter = null) + public async static Task GetContextMenuForFiles(ShellItem[] shellItems, CMF flags, Func? itemFilter = null) { var owningThread = new ThreadWithMessageQueue(); return await owningThread.PostMethod(() => GetContextMenuForFiles(shellItems, flags, owningThread, itemFilter)); } - private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, ThreadWithMessageQueue owningThread, Func? itemFilter = null) + private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, CMF flags, ThreadWithMessageQueue owningThread, Func? itemFilter = null) { if (!shellItems.Any()) return null; @@ -159,7 +159,7 @@ public async Task InvokeItem(int itemID) Shell32.IContextMenu menu = sf.GetChildrenUIObjects(default, shellItems); var hMenu = User32.CreatePopupMenu(); - menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags); + menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, (Shell32.CMF)flags); var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread, itemFilter); contextMenu.EnumMenuItems(hMenu, contextMenu.Items); @@ -174,7 +174,7 @@ public async Task InvokeItem(int itemID) public static async Task WarmUpQueryContextMenuAsync() { - using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, Shell32.CMF.CMF_NORMAL); + using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, CMF.CMF_NORMAL); } private void EnumMenuItems(HMENU hMenu, List menuItemsResult, bool loadSubenus = false) diff --git a/src/Files.App/Utils/Shell/LaunchHelper.cs b/src/Files.App/Utils/Shell/LaunchHelper.cs index 049a0b741dea..195f8e406aa3 100644 --- a/src/Files.App/Utils/Shell/LaunchHelper.cs +++ b/src/Files.App/Utils/Shell/LaunchHelper.cs @@ -160,7 +160,7 @@ private static async Task HandleApplicationLaunch(string application, stri if (!group.Any()) continue; - using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), Shell32.CMF.CMF_DEFAULTONLY); + using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), CMF.CMF_DEFAULTONLY); if (cMenu is not null) await cMenu.InvokeVerb(Shell32.CMDSTR_OPEN); @@ -176,7 +176,7 @@ private static async Task HandleApplicationLaunch(string application, stri { opened = await Win32Helper.StartSTATask(async () => { - using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, Shell32.CMF.CMF_DEFAULTONLY); + using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, CMF.CMF_DEFAULTONLY); if (cMenu is not null) await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1); From 7c0197ae9cf0facaede378b3599790da89ecc1a2 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 12:00:02 +0800 Subject: [PATCH 2/7] Code Quality: Removed uesless Vanara import --- .../Utils/Storage/Enumerators/Win32StorageEnumerator.cs | 1 - .../ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs | 1 - src/Files.App/ViewModels/Settings/AdvancedViewModel.cs | 1 - src/Files.App/ViewModels/Settings/GeneralViewModel.cs | 1 - 4 files changed, 4 deletions(-) diff --git a/src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs b/src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs index 610af9ab439b..64bf1c0e2915 100644 --- a/src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs +++ b/src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs @@ -4,7 +4,6 @@ using Files.App.Services.SizeProvider; using Files.Shared.Helpers; using System.IO; -using Vanara.PInvoke; using Windows.Storage; using static Files.App.Helpers.Win32Helper; using FileAttributes = System.IO.FileAttributes; diff --git a/src/Files.App/ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs b/src/Files.App/ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs index 3fd8e2e92f78..735e54fd22cd 100644 --- a/src/Files.App/ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs +++ b/src/Files.App/ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs @@ -3,7 +3,6 @@ using System.IO; using System.Windows.Input; -using Vanara.PInvoke; using Windows.Storage; namespace Files.App.ViewModels.Dialogs diff --git a/src/Files.App/ViewModels/Settings/AdvancedViewModel.cs b/src/Files.App/ViewModels/Settings/AdvancedViewModel.cs index 71716a3c73d6..e0c36c168ccf 100644 --- a/src/Files.App/ViewModels/Settings/AdvancedViewModel.cs +++ b/src/Files.App/ViewModels/Settings/AdvancedViewModel.cs @@ -7,7 +7,6 @@ using System.IO; using System.Text; using System.Windows.Input; -using Vanara.PInvoke; using Windows.ApplicationModel; using Windows.Storage; using Windows.Storage.Pickers; diff --git a/src/Files.App/ViewModels/Settings/GeneralViewModel.cs b/src/Files.App/ViewModels/Settings/GeneralViewModel.cs index 004d51fa6a09..0ede5c4fcaf6 100644 --- a/src/Files.App/ViewModels/Settings/GeneralViewModel.cs +++ b/src/Files.App/ViewModels/Settings/GeneralViewModel.cs @@ -3,7 +3,6 @@ using System.Collections.Specialized; using System.Globalization; -using Vanara.PInvoke; using Windows.Globalization; using Windows.Storage; using Windows.Storage.Pickers; From 2d2e891e8f6f249d0e6d106cb672fe5c53ac96fa Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 12:00:44 +0800 Subject: [PATCH 3/7] Code Quality: Removed Vanara.PInvokeVanara.PInvoke.RECT import --- .../UserControls/FilePreviews/ShellPreview.xaml.cs | 9 ++++----- .../UserControls/Previews/ShellPreviewViewModel.cs | 11 +++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs b/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs index d0f09e7aed50..cf81234ceec6 100644 --- a/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs +++ b/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs @@ -1,7 +1,6 @@ using Files.App.ViewModels.Previews; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Vanara.PInvoke; using Windows.Foundation; namespace Files.App.UserControls.FilePreviews @@ -32,15 +31,15 @@ private void PreviewHost_SizeChanged(object sender, SizeChangedEventArgs e) ViewModel.SizeChanged(GetPreviewSize()); } - private RECT GetPreviewSize() + private Rect GetPreviewSize() { var source = contentPresenter.TransformToVisual(XamlRoot.Content); var physicalSize = contentPresenter.RenderSize; var physicalPos = source.TransformPoint(new Point(0, 0)); var scale = XamlRoot.RasterizationScale; - var result = new RECT(); - result.Left = (int)(physicalPos.X * scale + 0.5); - result.Top = (int)(physicalPos.Y * scale + 0.5); + var result = new Rect(); + result.X = (int)(physicalPos.X * scale + 0.5); + result.Y = (int)(physicalPos.Y * scale + 0.5); result.Width = (int)(physicalSize.Width * scale + 0.5); result.Height = (int)(physicalSize.Height * scale + 0.5); return result; diff --git a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs index 243971dcf247..f9fdc03d06ae 100644 --- a/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs +++ b/src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs @@ -77,15 +77,18 @@ public async override Task> LoadPreviewAndDetailsAsync() } } - public void SizeChanged(RECT size) + public void SizeChanged(Windows.Foundation.Rect size) { + var width = (int)size.Width; + var height = (int)size.Height; + if (hwnd != HWND.NULL) - SetWindowPos(hwnd, HWND.HWND_TOP, size.Left, size.Top, size.Width, size.Height, SetWindowPosFlags.SWP_NOACTIVATE); + SetWindowPos(hwnd, HWND.HWND_TOP, (int)size.Left, (int)size.Top, width, height, SetWindowPosFlags.SWP_NOACTIVATE); - currentHandler?.ResetBounds(new(0, 0, size.Width, size.Height)); + currentHandler?.ResetBounds(new(0, 0, width, height)); if (outputLink is not null) - outputLink.PlacementVisual.Size = new(size.Width, size.Height); + outputLink.PlacementVisual.Size = new(width, height); } private nint WndProc(HWND hwnd, uint msg, nint wParam, nint lParam) From f4a4674258bdc184a20d82c685eff79b56ba805c Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 12:06:34 +0800 Subject: [PATCH 4/7] Code Quality: Replace Vanara with CsWin32 --- src/Files.App.CsWin32/NativeMethods.txt | 1 + src/Files.App/Utils/Shell/LaunchHelper.cs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 3b611b024af5..caa14aa9d4a5 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -156,3 +156,4 @@ IApplicationDestinations ApplicationDestinations IApplicationDocumentLists ApplicationDocumentLists +IApplicationActivationManager diff --git a/src/Files.App/Utils/Shell/LaunchHelper.cs b/src/Files.App/Utils/Shell/LaunchHelper.cs index 195f8e406aa3..ac426bc5b23c 100644 --- a/src/Files.App/Utils/Shell/LaunchHelper.cs +++ b/src/Files.App/Utils/Shell/LaunchHelper.cs @@ -6,6 +6,7 @@ using System.IO; using Vanara.PInvoke; using Vanara.Windows.Shell; +using Windows.Win32.UI.Shell; namespace Files.App.Utils.Shell { @@ -16,12 +17,12 @@ public static class LaunchHelper { public static void LaunchSettings(string page) { - var appActiveManager = new Shell32.IApplicationActivationManager(); + var appActiveManager = new IApplicationActivationManager(); appActiveManager.ActivateApplication( "windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel", page, - Shell32.ACTIVATEOPTIONS.AO_NONE, + ACTIVATEOPTIONS.AO_NONE, out _); } From 41b6b19d6a81cc2c40710395683313c85b9996f7 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 12:21:12 +0800 Subject: [PATCH 5/7] Code Quality: Removed useless convert --- .../UserControls/FilePreviews/ShellPreview.xaml.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs b/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs index cf81234ceec6..762aebf2550c 100644 --- a/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs +++ b/src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs @@ -38,10 +38,10 @@ private Rect GetPreviewSize() var physicalPos = source.TransformPoint(new Point(0, 0)); var scale = XamlRoot.RasterizationScale; var result = new Rect(); - result.X = (int)(physicalPos.X * scale + 0.5); - result.Y = (int)(physicalPos.Y * scale + 0.5); - result.Width = (int)(physicalSize.Width * scale + 0.5); - result.Height = (int)(physicalSize.Height * scale + 0.5); + result.X = physicalPos.X * scale + 0.5; + result.Y = physicalPos.Y * scale + 0.5; + result.Width = physicalSize.Width * scale + 0.5; + result.Height = physicalSize.Height * scale + 0.5; return result; } From baba9419e85625e5b96fb4d970437478f0ca12fe Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 13:47:46 +0800 Subject: [PATCH 6/7] Code Quality: Replaced Files.App.Data.Items.CMF with CsWin32 --- src/Files.App.CsWin32/NativeMethods.txt | 2 ++ .../Factories/ShellContextFlyoutHelper.cs | 3 ++- src/Files.App/Data/Items/ContextMenu.cs | 21 ------------------- src/Files.App/Utils/Shell/ContextMenu.cs | 11 +++++----- src/Files.App/Utils/Shell/LaunchHelper.cs | 5 +++-- 5 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index caa14aa9d4a5..8a7384521bdd 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -152,6 +152,8 @@ BHID_SFUIObject IContextMenu CMF_NORMAL CMF_OPTIMIZEFORINVOKE +CMF_EXTENDEDVERBS +CMF_DEFAULTONLY IApplicationDestinations ApplicationDestinations IApplicationDocumentLists diff --git a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs index 046dc7cf3581..d291c0245cfa 100644 --- a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs +++ b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs @@ -11,6 +11,7 @@ using System.IO; using Windows.System; using Windows.UI.Core; +using Windows.Win32; namespace Files.App.Helpers { @@ -53,7 +54,7 @@ bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem) } var contextMenu = await ContextMenu.GetContextMenuForFiles(filePaths, - shiftPressed ? CMF.CMF_EXTENDEDVERBS : CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu)); + shiftPressed ? PInvoke.CMF_EXTENDEDVERBS : PInvoke.CMF_NORMAL, FilterMenuItems(showOpenMenu)); if (contextMenu is not null) LoadMenuFlyoutItem(menuItemsList, contextMenu, contextMenu.Items, cancellationToken, true); diff --git a/src/Files.App/Data/Items/ContextMenu.cs b/src/Files.App/Data/Items/ContextMenu.cs index cb6fa03f9c3a..b110d4b61209 100644 --- a/src/Files.App/Data/Items/ContextMenu.cs +++ b/src/Files.App/Data/Items/ContextMenu.cs @@ -32,27 +32,6 @@ public enum HBITMAP_HMENU : long HBMMENU_SYSTEM = 1 } - // Same definition of Vanara.PInvoke.Shell32.CMF - public enum CMF : uint - { - CMF_NORMAL = 0x00000000, - CMF_DEFAULTONLY = 0x00000001, - CMF_VERBSONLY = 0x00000002, - CMF_EXPLORE = 0x00000004, - CMF_NOVERBS = 0x00000008, - CMF_CANRENAME = 0x00000010, - CMF_NODEFAULT = 0x00000020, - CMF_EXTENDEDVERBS = 0x00000100, - CMF_INCLUDESTATIC = 0x00000040, - CMF_ITEMMENU = 0x00000080, - CMF_DISABLEDVERBS = 0x00000200, - CMF_ASYNCVERBSTATE = 0x00000400, - CMF_OPTIMIZEFORINVOKE = 0x00000800, - CMF_SYNCCASCADEMENU = 0x00001000, - CMF_DONOTPICKDEFAULT = 0x00002000, - CMF_RESERVED = 0xffff0000, - } - public class Win32ContextMenu { public List Items { get; set; } diff --git a/src/Files.App/Utils/Shell/ContextMenu.cs b/src/Files.App/Utils/Shell/ContextMenu.cs index 51e5151878e1..846f8c0f7ec7 100644 --- a/src/Files.App/Utils/Shell/ContextMenu.cs +++ b/src/Files.App/Utils/Shell/ContextMenu.cs @@ -6,6 +6,7 @@ using Vanara.InteropServices; using Vanara.PInvoke; using Vanara.Windows.Shell; +using Windows.Win32; namespace Files.App.Utils.Shell { @@ -43,7 +44,7 @@ private ContextMenu(Shell32.IContextMenu cMenu, User32.SafeHMENU hMenu, IEnumera public async static Task InvokeVerb(string verb, params string[] filePaths) { - using var cMenu = await GetContextMenuForFiles(filePaths, CMF.CMF_DEFAULTONLY); + using var cMenu = await GetContextMenuForFiles(filePaths, PInvoke.CMF_DEFAULTONLY); return cMenu is not null && await cMenu.InvokeVerb(verb); } @@ -112,7 +113,7 @@ public async Task InvokeItem(int itemID) return false; } - public async static Task GetContextMenuForFiles(string[] filePathList, CMF flags, Func? itemFilter = null) + public async static Task GetContextMenuForFiles(string[] filePathList, uint flags, Func? itemFilter = null) { var owningThread = new ThreadWithMessageQueue(); @@ -140,14 +141,14 @@ public async Task InvokeItem(int itemID) }); } - public async static Task GetContextMenuForFiles(ShellItem[] shellItems, CMF flags, Func? itemFilter = null) + public async static Task GetContextMenuForFiles(ShellItem[] shellItems, uint flags, Func? itemFilter = null) { var owningThread = new ThreadWithMessageQueue(); return await owningThread.PostMethod(() => GetContextMenuForFiles(shellItems, flags, owningThread, itemFilter)); } - private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, CMF flags, ThreadWithMessageQueue owningThread, Func? itemFilter = null) + private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, uint flags, ThreadWithMessageQueue owningThread, Func? itemFilter = null) { if (!shellItems.Any()) return null; @@ -174,7 +175,7 @@ public async Task InvokeItem(int itemID) public static async Task WarmUpQueryContextMenuAsync() { - using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, CMF.CMF_NORMAL); + using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, PInvoke.CMF_NORMAL); } private void EnumMenuItems(HMENU hMenu, List menuItemsResult, bool loadSubenus = false) diff --git a/src/Files.App/Utils/Shell/LaunchHelper.cs b/src/Files.App/Utils/Shell/LaunchHelper.cs index ac426bc5b23c..715349b2f6dc 100644 --- a/src/Files.App/Utils/Shell/LaunchHelper.cs +++ b/src/Files.App/Utils/Shell/LaunchHelper.cs @@ -6,6 +6,7 @@ using System.IO; using Vanara.PInvoke; using Vanara.Windows.Shell; +using Windows.Win32; using Windows.Win32.UI.Shell; namespace Files.App.Utils.Shell @@ -161,7 +162,7 @@ private static async Task HandleApplicationLaunch(string application, stri if (!group.Any()) continue; - using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), CMF.CMF_DEFAULTONLY); + using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), PInvoke.CMF_DEFAULTONLY); if (cMenu is not null) await cMenu.InvokeVerb(Shell32.CMDSTR_OPEN); @@ -177,7 +178,7 @@ private static async Task HandleApplicationLaunch(string application, stri { opened = await Win32Helper.StartSTATask(async () => { - using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, CMF.CMF_DEFAULTONLY); + using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, PInvoke.CMF_DEFAULTONLY); if (cMenu is not null) await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1); From 972651c24c6b3909d483237b305dd53097e7577d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 26 Nov 2024 15:59:07 +0800 Subject: [PATCH 7/7] Code Quality: Replaced Files.App.Data.Items.MenuItemType with Windows.Win32.UI.WindowsAndMessaging.MENU_ITEM_TYPE --- src/Files.App.CsWin32/NativeMethods.txt | 1 + .../Data/Factories/ShellContextFlyoutHelper.cs | 13 +++++++------ src/Files.App/Data/Items/ContextMenu.cs | 18 +++--------------- src/Files.App/Utils/Shell/ContextMenu.cs | 9 +++++---- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 8a7384521bdd..68837251b7f1 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -159,3 +159,4 @@ ApplicationDestinations IApplicationDocumentLists ApplicationDocumentLists IApplicationActivationManager +MENU_ITEM_TYPE diff --git a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs index d291c0245cfa..7d545c5c76df 100644 --- a/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs +++ b/src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs @@ -12,6 +12,7 @@ using Windows.System; using Windows.UI.Core; using Windows.Win32; +using Windows.Win32.UI.WindowsAndMessaging; namespace Files.App.Helpers { @@ -77,10 +78,10 @@ private static void LoadMenuFlyoutItem( 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 menuItems = menuFlyoutItems.TakeWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR || ++itemsCount <= itemsBeforeOverflow).ToList(); var overflowItems = menuFlyoutItems.Except(menuItems).ToList(); - if (overflowItems.Any(x => x.Type != MenuItemType.MFT_SEPARATOR)) + if (overflowItems.Any(x => x.Type != MENU_ITEM_TYPE.MFT_SEPARATOR)) { var moreItem = menuItemsListLocal.FirstOrDefault(x => x.ID == "ItemOverflow"); if (moreItem is null) @@ -100,15 +101,15 @@ private static void LoadMenuFlyoutItem( } foreach (var menuFlyoutItem in menuItems - .SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR) // Remove leading separators + .SkipWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR) // Remove leading separators .Reverse() - .SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR)) // Remove trailing separators + .SkipWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR)) // Remove trailing separators { if (cancellationToken.IsCancellationRequested) break; // Avoid duplicate separators - if ((menuFlyoutItem.Type == MenuItemType.MFT_SEPARATOR) && (menuItemsListLocal.FirstOrDefault()?.ItemType == ContextMenuFlyoutItemType.Separator)) + if ((menuFlyoutItem.Type == MENU_ITEM_TYPE.MFT_SEPARATOR) && (menuItemsListLocal.FirstOrDefault()?.ItemType == ContextMenuFlyoutItemType.Separator)) continue; BitmapImage? image = null; @@ -119,7 +120,7 @@ private static void LoadMenuFlyoutItem( image.SetSourceAsync(ms.AsRandomAccessStream()).AsTask().Wait(10); } - if (menuFlyoutItem.Type is MenuItemType.MFT_SEPARATOR) + if (menuFlyoutItem.Type is MENU_ITEM_TYPE.MFT_SEPARATOR) { var menuLayoutItem = new ContextMenuFlyoutItemViewModel() { diff --git a/src/Files.App/Data/Items/ContextMenu.cs b/src/Files.App/Data/Items/ContextMenu.cs index b110d4b61209..d5475758f0c6 100644 --- a/src/Files.App/Data/Items/ContextMenu.cs +++ b/src/Files.App/Data/Items/ContextMenu.cs @@ -1,22 +1,10 @@ // Copyright (c) 2024 Files Community // Licensed under the MIT License. See the LICENSE. +using Windows.Win32.UI.WindowsAndMessaging; + namespace Files.App.Data.Items { - // Same definition of Vanara.PInvoke.User32.MenuItemType - public enum MenuItemType : uint - { - MFT_STRING = 0, - MFT_BITMAP = 4, - MFT_MENUBARBREAK = 32, - MFT_MENUBREAK = 64, - MFT_OWNERDRAW = 256, - MFT_RADIOCHECK = 512, - MFT_SEPARATOR = 2048, - MFT_RIGHTORDER = 8192, - MFT_RIGHTJUSTIFY = 16384 - } - public enum HBITMAP_HMENU : long { HBMMENU_CALLBACK = -1, @@ -43,7 +31,7 @@ public class Win32ContextMenuItem public int ID { get; set; } // Valid only in current menu to invoke item public string Label { get; set; } public string CommandString { get; set; } - public MenuItemType Type { get; set; } + public MENU_ITEM_TYPE Type { get; set; } public List SubItems { get; set; } } } diff --git a/src/Files.App/Utils/Shell/ContextMenu.cs b/src/Files.App/Utils/Shell/ContextMenu.cs index 846f8c0f7ec7..040e0d8f6642 100644 --- a/src/Files.App/Utils/Shell/ContextMenu.cs +++ b/src/Files.App/Utils/Shell/ContextMenu.cs @@ -7,6 +7,7 @@ using Vanara.PInvoke; using Vanara.Windows.Shell; using Windows.Win32; +using Windows.Win32.UI.WindowsAndMessaging; namespace Files.App.Utils.Shell { @@ -178,7 +179,7 @@ public static async Task WarmUpQueryContextMenuAsync() using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, PInvoke.CMF_NORMAL); } - private void EnumMenuItems(HMENU hMenu, List menuItemsResult, bool loadSubenus = false) + private void EnumMenuItems(Vanara.PInvoke.HMENU hMenu, List menuItemsResult, bool loadSubenus = false) { var itemCount = User32.GetMenuItemCount(hMenu); @@ -212,12 +213,12 @@ private void EnumMenuItems(HMENU hMenu, List menuItemsResu continue; } - menuItem.Type = (MenuItemType)menuItemInfo.fType; + menuItem.Type = (MENU_ITEM_TYPE)menuItemInfo.fType; // wID - idCmdFirst menuItem.ID = (int)(menuItemInfo.wID - 1); - if (menuItem.Type == MenuItemType.MFT_STRING) + if (menuItem.Type == MENU_ITEM_TYPE.MFT_STRING) { Debug.WriteLine("Item {0} ({1}): {2}", index, menuItemInfo.wID, menuItemInfo.dwTypeData); @@ -245,7 +246,7 @@ private void EnumMenuItems(HMENU hMenu, List menuItemsResu } } - if (menuItemInfo.hSubMenu != HMENU.NULL) + if (menuItemInfo.hSubMenu != Vanara.PInvoke.HMENU.NULL) { Debug.WriteLine("Item {0}: has submenu", index); var subItems = new List();