Skip to content

Commit 20ef76a

Browse files
authored
Code Quality: Replace Vanara with CsWin32 (#16536)
1 parent ba19377 commit 20ef76a

File tree

11 files changed

+47
-53
lines changed

11 files changed

+47
-53
lines changed

src/Files.App.CsWin32/NativeMethods.txt

+4
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ BHID_SFUIObject
152152
IContextMenu
153153
CMF_NORMAL
154154
CMF_OPTIMIZEFORINVOKE
155+
CMF_EXTENDEDVERBS
156+
CMF_DEFAULTONLY
155157
IApplicationDestinations
156158
ApplicationDestinations
157159
IApplicationDocumentLists
158160
ApplicationDocumentLists
161+
IApplicationActivationManager
162+
MENU_ITEM_TYPE

src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
using Microsoft.UI.Xaml.Controls;
1010
using Microsoft.UI.Xaml.Media.Imaging;
1111
using System.IO;
12-
using Vanara.PInvoke;
1312
using Windows.System;
1413
using Windows.UI.Core;
15-
using static Vanara.PInvoke.Kernel32;
14+
using Windows.Win32;
15+
using Windows.Win32.UI.WindowsAndMessaging;
1616

1717
namespace Files.App.Helpers
1818
{
@@ -55,7 +55,7 @@ bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem)
5555
}
5656

5757
var contextMenu = await ContextMenu.GetContextMenuForFiles(filePaths,
58-
shiftPressed ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL, FilterMenuItems(showOpenMenu));
58+
shiftPressed ? PInvoke.CMF_EXTENDEDVERBS : PInvoke.CMF_NORMAL, FilterMenuItems(showOpenMenu));
5959

6060
if (contextMenu is not null)
6161
LoadMenuFlyoutItem(menuItemsList, contextMenu, contextMenu.Items, cancellationToken, true);
@@ -78,10 +78,10 @@ private static void LoadMenuFlyoutItem(
7878
return;
7979

8080
var itemsCount = 0; // Separators do not count for reaching the overflow threshold
81-
var menuItems = menuFlyoutItems.TakeWhile(x => x.Type == MenuItemType.MFT_SEPARATOR || ++itemsCount <= itemsBeforeOverflow).ToList();
81+
var menuItems = menuFlyoutItems.TakeWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR || ++itemsCount <= itemsBeforeOverflow).ToList();
8282
var overflowItems = menuFlyoutItems.Except(menuItems).ToList();
8383

84-
if (overflowItems.Any(x => x.Type != MenuItemType.MFT_SEPARATOR))
84+
if (overflowItems.Any(x => x.Type != MENU_ITEM_TYPE.MFT_SEPARATOR))
8585
{
8686
var moreItem = menuItemsListLocal.FirstOrDefault(x => x.ID == "ItemOverflow");
8787
if (moreItem is null)
@@ -101,15 +101,15 @@ private static void LoadMenuFlyoutItem(
101101
}
102102

103103
foreach (var menuFlyoutItem in menuItems
104-
.SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR) // Remove leading separators
104+
.SkipWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR) // Remove leading separators
105105
.Reverse()
106-
.SkipWhile(x => x.Type == MenuItemType.MFT_SEPARATOR)) // Remove trailing separators
106+
.SkipWhile(x => x.Type == MENU_ITEM_TYPE.MFT_SEPARATOR)) // Remove trailing separators
107107
{
108108
if (cancellationToken.IsCancellationRequested)
109109
break;
110110

111111
// Avoid duplicate separators
112-
if ((menuFlyoutItem.Type == MenuItemType.MFT_SEPARATOR) && (menuItemsListLocal.FirstOrDefault()?.ItemType == ContextMenuFlyoutItemType.Separator))
112+
if ((menuFlyoutItem.Type == MENU_ITEM_TYPE.MFT_SEPARATOR) && (menuItemsListLocal.FirstOrDefault()?.ItemType == ContextMenuFlyoutItemType.Separator))
113113
continue;
114114

115115
BitmapImage? image = null;
@@ -120,7 +120,7 @@ private static void LoadMenuFlyoutItem(
120120
image.SetSourceAsync(ms.AsRandomAccessStream()).AsTask().Wait(10);
121121
}
122122

123-
if (menuFlyoutItem.Type is MenuItemType.MFT_SEPARATOR)
123+
if (menuFlyoutItem.Type is MENU_ITEM_TYPE.MFT_SEPARATOR)
124124
{
125125
var menuLayoutItem = new ContextMenuFlyoutItemViewModel()
126126
{

src/Files.App/Data/Items/ContextMenu.cs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Windows.Win32.UI.WindowsAndMessaging;
5+
46
namespace Files.App.Data.Items
57
{
6-
// Same definition of Vanara.PInvoke.User32.MenuItemType
7-
public enum MenuItemType : uint
8-
{
9-
MFT_STRING = 0,
10-
MFT_BITMAP = 4,
11-
MFT_MENUBARBREAK = 32,
12-
MFT_MENUBREAK = 64,
13-
MFT_OWNERDRAW = 256,
14-
MFT_RADIOCHECK = 512,
15-
MFT_SEPARATOR = 2048,
16-
MFT_RIGHTORDER = 8192,
17-
MFT_RIGHTJUSTIFY = 16384
18-
}
19-
208
public enum HBITMAP_HMENU : long
219
{
2210
HBMMENU_CALLBACK = -1,
@@ -43,7 +31,7 @@ public class Win32ContextMenuItem
4331
public int ID { get; set; } // Valid only in current menu to invoke item
4432
public string Label { get; set; }
4533
public string CommandString { get; set; }
46-
public MenuItemType Type { get; set; }
34+
public MENU_ITEM_TYPE Type { get; set; }
4735
public List<Win32ContextMenuItem> SubItems { get; set; }
4836
}
4937
}

src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Files.App.ViewModels.Previews;
22
using Microsoft.UI.Xaml;
33
using Microsoft.UI.Xaml.Controls;
4-
using Vanara.PInvoke;
54
using Windows.Foundation;
65

76
namespace Files.App.UserControls.FilePreviews
@@ -32,17 +31,17 @@ private void PreviewHost_SizeChanged(object sender, SizeChangedEventArgs e)
3231
ViewModel.SizeChanged(GetPreviewSize());
3332
}
3433

35-
private RECT GetPreviewSize()
34+
private Rect GetPreviewSize()
3635
{
3736
var source = contentPresenter.TransformToVisual(XamlRoot.Content);
3837
var physicalSize = contentPresenter.RenderSize;
3938
var physicalPos = source.TransformPoint(new Point(0, 0));
4039
var scale = XamlRoot.RasterizationScale;
41-
var result = new RECT();
42-
result.Left = (int)(physicalPos.X * scale + 0.5);
43-
result.Top = (int)(physicalPos.Y * scale + 0.5);
44-
result.Width = (int)(physicalSize.Width * scale + 0.5);
45-
result.Height = (int)(physicalSize.Height * scale + 0.5);
40+
var result = new Rect();
41+
result.X = physicalPos.X * scale + 0.5;
42+
result.Y = physicalPos.Y * scale + 0.5;
43+
result.Width = physicalSize.Width * scale + 0.5;
44+
result.Height = physicalSize.Height * scale + 0.5;
4645
return result;
4746
}
4847

src/Files.App/Utils/Shell/ContextMenu.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using Vanara.InteropServices;
77
using Vanara.PInvoke;
88
using Vanara.Windows.Shell;
9+
using Windows.Win32;
10+
using Windows.Win32.UI.WindowsAndMessaging;
911

1012
namespace Files.App.Utils.Shell
1113
{
@@ -43,7 +45,7 @@ private ContextMenu(Shell32.IContextMenu cMenu, User32.SafeHMENU hMenu, IEnumera
4345

4446
public async static Task<bool> InvokeVerb(string verb, params string[] filePaths)
4547
{
46-
using var cMenu = await GetContextMenuForFiles(filePaths, Shell32.CMF.CMF_DEFAULTONLY);
48+
using var cMenu = await GetContextMenuForFiles(filePaths, PInvoke.CMF_DEFAULTONLY);
4749

4850
return cMenu is not null && await cMenu.InvokeVerb(verb);
4951
}
@@ -112,7 +114,7 @@ public async Task<bool> InvokeItem(int itemID)
112114
return false;
113115
}
114116

115-
public async static Task<ContextMenu?> GetContextMenuForFiles(string[] filePathList, Shell32.CMF flags, Func<string, bool>? itemFilter = null)
117+
public async static Task<ContextMenu?> GetContextMenuForFiles(string[] filePathList, uint flags, Func<string, bool>? itemFilter = null)
116118
{
117119
var owningThread = new ThreadWithMessageQueue();
118120

@@ -140,14 +142,14 @@ public async Task<bool> InvokeItem(int itemID)
140142
});
141143
}
142144

143-
public async static Task<ContextMenu?> GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, Func<string, bool>? itemFilter = null)
145+
public async static Task<ContextMenu?> GetContextMenuForFiles(ShellItem[] shellItems, uint flags, Func<string, bool>? itemFilter = null)
144146
{
145147
var owningThread = new ThreadWithMessageQueue();
146148

147149
return await owningThread.PostMethod<ContextMenu>(() => GetContextMenuForFiles(shellItems, flags, owningThread, itemFilter));
148150
}
149151

150-
private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, ThreadWithMessageQueue owningThread, Func<string, bool>? itemFilter = null)
152+
private static ContextMenu? GetContextMenuForFiles(ShellItem[] shellItems, uint flags, ThreadWithMessageQueue owningThread, Func<string, bool>? itemFilter = null)
151153
{
152154
if (!shellItems.Any())
153155
return null;
@@ -159,7 +161,7 @@ public async Task<bool> InvokeItem(int itemID)
159161

160162
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(default, shellItems);
161163
var hMenu = User32.CreatePopupMenu();
162-
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, flags);
164+
menu.QueryContextMenu(hMenu, 0, 1, 0x7FFF, (Shell32.CMF)flags);
163165
var contextMenu = new ContextMenu(menu, hMenu, shellItems.Select(x => x.ParsingName), owningThread, itemFilter);
164166
contextMenu.EnumMenuItems(hMenu, contextMenu.Items);
165167

@@ -174,10 +176,10 @@ public async Task<bool> InvokeItem(int itemID)
174176

175177
public static async Task WarmUpQueryContextMenuAsync()
176178
{
177-
using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, Shell32.CMF.CMF_NORMAL);
179+
using var cMenu = await GetContextMenuForFiles(new string[] { $@"{Constants.UserEnvironmentPaths.SystemDrivePath}\" }, PInvoke.CMF_NORMAL);
178180
}
179181

180-
private void EnumMenuItems(HMENU hMenu, List<Win32ContextMenuItem> menuItemsResult, bool loadSubenus = false)
182+
private void EnumMenuItems(Vanara.PInvoke.HMENU hMenu, List<Win32ContextMenuItem> menuItemsResult, bool loadSubenus = false)
181183
{
182184
var itemCount = User32.GetMenuItemCount(hMenu);
183185

@@ -211,12 +213,12 @@ private void EnumMenuItems(HMENU hMenu, List<Win32ContextMenuItem> menuItemsResu
211213
continue;
212214
}
213215

214-
menuItem.Type = (MenuItemType)menuItemInfo.fType;
216+
menuItem.Type = (MENU_ITEM_TYPE)menuItemInfo.fType;
215217

216218
// wID - idCmdFirst
217219
menuItem.ID = (int)(menuItemInfo.wID - 1);
218220

219-
if (menuItem.Type == MenuItemType.MFT_STRING)
221+
if (menuItem.Type == MENU_ITEM_TYPE.MFT_STRING)
220222
{
221223
Debug.WriteLine("Item {0} ({1}): {2}", index, menuItemInfo.wID, menuItemInfo.dwTypeData);
222224

@@ -244,7 +246,7 @@ private void EnumMenuItems(HMENU hMenu, List<Win32ContextMenuItem> menuItemsResu
244246
}
245247
}
246248

247-
if (menuItemInfo.hSubMenu != HMENU.NULL)
249+
if (menuItemInfo.hSubMenu != Vanara.PInvoke.HMENU.NULL)
248250
{
249251
Debug.WriteLine("Item {0}: has submenu", index);
250252
var subItems = new List<Win32ContextMenuItem>();

src/Files.App/Utils/Shell/LaunchHelper.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.IO;
77
using Vanara.PInvoke;
88
using Vanara.Windows.Shell;
9+
using Windows.Win32;
10+
using Windows.Win32.UI.Shell;
911

1012
namespace Files.App.Utils.Shell
1113
{
@@ -16,12 +18,12 @@ public static class LaunchHelper
1618
{
1719
public static void LaunchSettings(string page)
1820
{
19-
var appActiveManager = new Shell32.IApplicationActivationManager();
21+
var appActiveManager = new IApplicationActivationManager();
2022

2123
appActiveManager.ActivateApplication(
2224
"windows.immersivecontrolpanel_cw5n1h2txyewy!microsoft.windows.immersivecontrolpanel",
2325
page,
24-
Shell32.ACTIVATEOPTIONS.AO_NONE,
26+
ACTIVATEOPTIONS.AO_NONE,
2527
out _);
2628
}
2729

@@ -160,7 +162,7 @@ private static async Task<bool> HandleApplicationLaunch(string application, stri
160162
if (!group.Any())
161163
continue;
162164

163-
using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), Shell32.CMF.CMF_DEFAULTONLY);
165+
using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), PInvoke.CMF_DEFAULTONLY);
164166

165167
if (cMenu is not null)
166168
await cMenu.InvokeVerb(Shell32.CMDSTR_OPEN);
@@ -176,7 +178,7 @@ private static async Task<bool> HandleApplicationLaunch(string application, stri
176178
{
177179
opened = await Win32Helper.StartSTATask(async () =>
178180
{
179-
using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, Shell32.CMF.CMF_DEFAULTONLY);
181+
using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, PInvoke.CMF_DEFAULTONLY);
180182

181183
if (cMenu is not null)
182184
await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1);

src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Files.App.Services.SizeProvider;
55
using Files.Shared.Helpers;
66
using System.IO;
7-
using Vanara.PInvoke;
87
using Windows.Storage;
98
using static Files.App.Helpers.Win32Helper;
109
using FileAttributes = System.IO.FileAttributes;

src/Files.App/ViewModels/Dialogs/DecompressArchiveDialogViewModel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.IO;
55
using System.Windows.Input;
6-
using Vanara.PInvoke;
76
using Windows.Storage;
87

98
namespace Files.App.ViewModels.Dialogs

src/Files.App/ViewModels/Settings/AdvancedViewModel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO;
88
using System.Text;
99
using System.Windows.Input;
10-
using Vanara.PInvoke;
1110
using Windows.ApplicationModel;
1211
using Windows.Storage;
1312
using Windows.Storage.Pickers;

src/Files.App/ViewModels/Settings/GeneralViewModel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Specialized;
55
using System.Globalization;
6-
using Vanara.PInvoke;
76
using Windows.Globalization;
87
using Windows.Storage;
98
using Windows.Storage.Pickers;

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ public async override Task<List<FileProperty>> LoadPreviewAndDetailsAsync()
7777
}
7878
}
7979

80-
public void SizeChanged(RECT size)
80+
public void SizeChanged(Windows.Foundation.Rect size)
8181
{
82+
var width = (int)size.Width;
83+
var height = (int)size.Height;
84+
8285
if (hwnd != HWND.NULL)
83-
SetWindowPos(hwnd, HWND.HWND_TOP, size.Left, size.Top, size.Width, size.Height, SetWindowPosFlags.SWP_NOACTIVATE);
86+
SetWindowPos(hwnd, HWND.HWND_TOP, (int)size.Left, (int)size.Top, width, height, SetWindowPosFlags.SWP_NOACTIVATE);
8487

85-
currentHandler?.ResetBounds(new(0, 0, size.Width, size.Height));
88+
currentHandler?.ResetBounds(new(0, 0, width, height));
8689

8790
if (outputLink is not null)
88-
outputLink.PlacementVisual.Size = new(size.Width, size.Height);
91+
outputLink.PlacementVisual.Size = new(width, height);
8992
}
9093

9194
private nint WndProc(HWND hwnd, uint msg, nint wParam, nint lParam)

0 commit comments

Comments
 (0)