Skip to content

Commit 7883b19

Browse files
authored
Code Quality: Merged all Win32 helpers into single class (#14947)
1 parent 68ef926 commit 7883b19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+112
-120
lines changed

src/Files.App/Actions/Content/Install/InstallFontAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public InstallFontAction()
3434
public Task ExecuteAsync()
3535
{
3636
var paths = context.SelectedItems.Select(item => item.ItemPath).ToArray();
37-
return Win32API.InstallFontsAsync(paths, false);
37+
return Win32Helper.InstallFontsAsync(paths, false);
3838
}
3939

4040
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)

src/Files.App/Actions/Content/Install/InstallInfDriverAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public InstallInfDriverAction()
3333

3434
public async Task ExecuteAsync()
3535
{
36-
await Task.WhenAll(context.SelectedItems.Select(selectedItem => Win32API.InstallInf(selectedItem.ItemPath)));
36+
await Task.WhenAll(context.SelectedItems.Select(selectedItem => Win32Helper.InstallInf(selectedItem.ItemPath)));
3737
}
3838

3939
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)

src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public RunWithPowershellAction()
3131

3232
public Task ExecuteAsync()
3333
{
34-
return Win32API.RunPowershellCommandAsync($"{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}", false);
34+
return Win32Helper.RunPowershellCommandAsync($"{context.ShellPage?.SlimContentPage?.SelectedItem?.ItemPath}", false);
3535
}
3636

3737
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/FileSystem/FormatDriveAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public FormatDriveAction()
3333

3434
public Task ExecuteAsync()
3535
{
36-
return Win32API.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
36+
return Win32Helper.OpenFormatDriveDialog(context.Folder?.ItemPath ?? string.Empty);
3737
}
3838

3939
public void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/Open/OpenInVSCodeAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public OpenInVSCodeAction()
3232

3333
public Task ExecuteAsync()
3434
{
35-
return Win32API.RunPowershellCommandAsync($"code \'{_context.ShellPage?.FilesystemViewModel.WorkingDirectory}\'", false);
35+
return Win32Helper.RunPowershellCommandAsync($"code \'{_context.ShellPage?.FilesystemViewModel.WorkingDirectory}\'", false);
3636
}
3737

3838
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/Open/OpenRepoInVSCodeAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public OpenRepoInVSCodeAction()
3333

3434
public Task ExecuteAsync()
3535
{
36-
return Win32API.RunPowershellCommandAsync($"code \'{_context.ShellPage!.InstanceViewModel.GitRepositoryPath}\'", false);
36+
return Win32Helper.RunPowershellCommandAsync($"code \'{_context.ShellPage!.InstanceViewModel.GitRepositoryPath}\'", false);
3737
}
3838

3939
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ Func<string, bool> FilterMenuItems(bool showOpenMenu)
4040
"Windows.ModernShare", "Windows.Share", "setdesktopwallpaper",
4141
"eject", "rename", "explore", "openinfiles", "extract",
4242
"copyaspath", "undelete", "empty", "format", "rotate90", "rotate270",
43-
Win32API.ExtractStringFromDLL("shell32.dll", 34593), // Add to collection
44-
Win32API.ExtractStringFromDLL("shell32.dll", 5384), // Pin to Start
45-
Win32API.ExtractStringFromDLL("shell32.dll", 5385), // Unpin from Start
46-
Win32API.ExtractStringFromDLL("shell32.dll", 5386), // Pin to taskbar
47-
Win32API.ExtractStringFromDLL("shell32.dll", 5387), // Unpin from taskbar
43+
Win32Helper.ExtractStringFromDLL("shell32.dll", 34593), // Add to collection
44+
Win32Helper.ExtractStringFromDLL("shell32.dll", 5384), // Pin to Start
45+
Win32Helper.ExtractStringFromDLL("shell32.dll", 5385), // Unpin from Start
46+
Win32Helper.ExtractStringFromDLL("shell32.dll", 5386), // Pin to taskbar
47+
Win32Helper.ExtractStringFromDLL("shell32.dll", 5387), // Unpin from taskbar
4848
};
4949

5050
bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem)
@@ -130,7 +130,7 @@ private static void LoadMenuFlyoutItem(
130130
}
131131
else if (!string.IsNullOrEmpty(menuFlyoutItem.Label) && menuFlyoutItem.SubItems is not null)
132132
{
133-
if (string.Equals(menuFlyoutItem.Label, Win32API.ExtractStringFromDLL("shell32.dll", 30312)))
133+
if (string.Equals(menuFlyoutItem.Label, Win32Helper.ExtractStringFromDLL("shell32.dll", 30312)))
134134
menuFlyoutItem.CommandString = "sendto";
135135

136136
var menuLayoutSubItem = new ContextMenuFlyoutItemViewModel()
@@ -181,21 +181,21 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
181181
switch (verb)
182182
{
183183
case "install" when isFont:
184-
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), false);
184+
await Win32Helper.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), false);
185185
break;
186186

187187
case "installAllUsers" when isFont:
188-
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), true);
188+
await Win32Helper.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), true);
189189
break;
190190

191191
case "mount":
192192
var vhdPath = contextMenu.ItemsPath[0];
193-
await Win32API.MountVhdDisk(vhdPath);
193+
await Win32Helper.MountVhdDisk(vhdPath);
194194
break;
195195

196196
case "format":
197197
var drivePath = contextMenu.ItemsPath[0];
198-
await Win32API.OpenFormatDriveDialog(drivePath);
198+
await Win32Helper.OpenFormatDriveDialog(drivePath);
199199
break;
200200

201201
default:

src/Files.App/Helpers/Interop/NativeWinApiHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,6 @@ public static bool IsHasThreadAccessPropertyPresent
257257
}
258258

259259
public static Task<string> GetFileAssociationAsync(string filePath)
260-
=> Win32API.GetFileAssociationAsync(filePath, true);
260+
=> Win32Helper.GetFileAssociationAsync(filePath, true);
261261
}
262262
}

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, b
292292
selectedItems.Count > 1 &&
293293
selectedItems.All(x => x.PrimaryItemAttribute == StorageItemTypes.File && !x.IsExecutable && !x.IsShortcut))
294294
{
295-
opened = await Win32Helpers.InvokeWin32ComponentAsync(string.Join('|', selectedItems.Select(x => x.ItemPath)), associatedInstance);
295+
opened = await Win32Helper.InvokeWin32ComponentAsync(string.Join('|', selectedItems.Select(x => x.ItemPath)), associatedInstance);
296296
}
297297

298298
if (opened)
@@ -319,7 +319,7 @@ public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInsta
319319
return;
320320

321321
var arguments = string.Join(" ", items.Select(item => $"\"{item.Path}\""));
322-
await Win32Helpers.InvokeWin32ComponentAsync(executablePath, associatedInstance, arguments);
322+
await Win32Helper.InvokeWin32ComponentAsync(executablePath, associatedInstance, arguments);
323323
}
324324

325325
/// <summary>
@@ -469,7 +469,7 @@ private static async Task<FilesystemResult> OpenDirectory(string path, IShellPag
469469
{
470470
if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
471471
{
472-
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance);
472+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance);
473473
opened = (FilesystemResult)true;
474474
}
475475
else
@@ -498,7 +498,7 @@ private static async Task<FilesystemResult> OpenDirectory(string path, IShellPag
498498
if (opened)
499499
await OpenPath(forceOpenInNewTab, UserSettingsService.FoldersSettingsService.OpenFoldersInNewTab, path, associatedInstance, selectItems);
500500
else
501-
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance);
501+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance);
502502
}
503503
return opened;
504504
}
@@ -513,7 +513,7 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
513513
{
514514
if (string.IsNullOrEmpty(shortcutInfo.TargetPath))
515515
{
516-
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
516+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
517517
}
518518
else
519519
{
@@ -524,13 +524,13 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
524524
if (childFile?.Item is SystemStorageFile)
525525
App.RecentItemsManager.AddToRecentItems(childFile.Path);
526526
}
527-
await Win32Helpers.InvokeWin32ComponentAsync(shortcutInfo.TargetPath, associatedInstance, $"{args} {shortcutInfo.Arguments}", shortcutInfo.RunAsAdmin, shortcutInfo.WorkingDirectory);
527+
await Win32Helper.InvokeWin32ComponentAsync(shortcutInfo.TargetPath, associatedInstance, $"{args} {shortcutInfo.Arguments}", shortcutInfo.RunAsAdmin, shortcutInfo.WorkingDirectory);
528528
}
529529
opened = (FilesystemResult)true;
530530
}
531531
else if (isHiddenItem)
532532
{
533-
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
533+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
534534
}
535535
else
536536
{
@@ -630,7 +630,7 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
630630
}
631631

632632
if (!launchSuccess)
633-
await Win32Helpers.InvokeWin32ComponentAsync(path, associatedInstance, args);
633+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
634634
}
635635
});
636636
}

src/Files.App/Helpers/UI/UIHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static IconFileInfo GetSidebarIconResourceInfo(int index)
163163
private static IEnumerable<IconFileInfo> LoadSidebarIconResources()
164164
{
165165
string imageres = Path.Combine(Constants.UserEnvironmentPaths.SystemRootPath, "System32", "imageres.dll");
166-
var imageResList = Win32API.ExtractSelectedIconsFromDLL(imageres, new List<int>() {
166+
var imageResList = Win32Helper.ExtractSelectedIconsFromDLL(imageres, new List<int>() {
167167
Constants.ImageRes.RecycleBin,
168168
Constants.ImageRes.NetworkDrives,
169169
Constants.ImageRes.Libraries,
@@ -179,7 +179,7 @@ private static IEnumerable<IconFileInfo> LoadSidebarIconResources()
179179
private static IconFileInfo LoadShieldIconResource()
180180
{
181181
string imageres = Path.Combine(Constants.UserEnvironmentPaths.SystemRootPath, "System32", "imageres.dll");
182-
var imageResList = Win32API.ExtractSelectedIconsFromDLL(imageres, new List<int>() {
182+
var imageResList = Win32Helper.ExtractSelectedIconsFromDLL(imageres, new List<int>() {
183183
Constants.ImageRes.ShieldIcon
184184
}, 16);
185185

0 commit comments

Comments
 (0)