Skip to content

Commit f36a19d

Browse files
authored
Code Quality: Improved WindowsStorables (#17191)
1 parent 1c5ca4a commit f36a19d

25 files changed

+674
-328
lines changed

src/Files.App.CsWin32/ComPtr`1.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,6 @@ public void Attach(T* other)
6464
return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this));
6565
}
6666

67-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68-
[Obsolete("Use `HRESULT As<U>(U** other)` instead.")]
69-
public readonly ComPtr<U> As<U>() where U : unmanaged, IComIID
70-
{
71-
ComPtr<U> ptr = default;
72-
((IUnknown*)_ptr)->QueryInterface((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in U.Guid)), (void**)ptr.GetAddressOf());
73-
return ptr;
74-
}
75-
7667
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7768
public readonly HRESULT As<U>(U** other) where U : unmanaged, IComIID
7869
{
@@ -91,22 +82,6 @@ public readonly HRESULT CoCreateInstance(Guid* rclsid, IUnknown* pUnkOuter = nul
9182
return PInvoke.CoCreateInstance(rclsid, pUnkOuter, dwClsContext, (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (void**)this.GetAddressOf());
9283
}
9384

94-
// Conversion operators
95-
96-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97-
public static implicit operator ComPtr<T>(T* other)
98-
{
99-
ComPtr<T> ptr = default;
100-
ptr.Attach(other);
101-
return ptr;
102-
}
103-
104-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105-
public static implicit operator T*(ComPtr<T> other)
106-
{
107-
return other._ptr;
108-
}
109-
11085
// Disposer
11186

11287
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/Files.App.CsWin32/ManualGuid.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ public static Guid* IID_IStorageProviderStatusUISourceFactory
4141

4242
[GuidRVAGen.Guid("00021500-0000-0000-C000-000000000046")]
4343
public static partial Guid* IID_IQueryInfo { get; }
44+
45+
[GuidRVAGen.Guid("BCC18B79-BA16-442F-80C4-8A59C30C463B")]
46+
public static partial Guid* IID_IShellItemImageFactory { get; }
47+
48+
[GuidRVAGen.Guid("000214F9-0000-0000-C000-000000000046")]
49+
public static partial Guid* IID_IShellLinkW { get; }
50+
51+
[GuidRVAGen.Guid("B63EA76D-1F85-456F-A19C-48159EFA858B")]
52+
public static partial Guid* IID_IShellItemArray { get; }
53+
54+
[GuidRVAGen.Guid("7F9185B0-CB92-43C5-80A9-92277A4F7B54")]
55+
public static partial Guid* IID_IExecuteCommand { get; }
56+
57+
[GuidRVAGen.Guid("1C9CD5BB-98E9-4491-A60F-31AACC72B83C")]
58+
public static partial Guid* IID_IObjectWithSelection { get; }
59+
60+
[GuidRVAGen.Guid("000214E8-0000-0000-C000-000000000046")]
61+
public static partial Guid* IID_IShellExtInit { get; }
62+
63+
[GuidRVAGen.Guid("000214F4-0000-0000-C000-000000000046")]
64+
public static partial Guid* IID_IContextMenu2 { get; }
4465
}
4566

4667
public static unsafe partial class CLSID
@@ -59,6 +80,15 @@ public static unsafe partial class CLSID
5980

6081
[GuidRVAGen.Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
6182
public static partial Guid* CLSID_ApplicationActivationManager { get; }
83+
84+
[GuidRVAGen.Guid("B455F46E-E4AF-4035-B0A4-CF18D2F6F28E")]
85+
public static partial Guid* CLSID_PinToFrequentExecute { get; }
86+
87+
[GuidRVAGen.Guid("EE20EEBA-DF64-4A4E-B7BB-2D1C6B2DFCC1")]
88+
public static partial Guid* CLSID_UnPinFromFrequentExecute { get; }
89+
90+
[GuidRVAGen.Guid("D969A300-E7FF-11d0-A93B-00A0C90F2719")]
91+
public static partial Guid* CLSID_NewMenu { get; }
6292
}
6393

6494
public static unsafe partial class BHID

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,14 @@ QITIPF_FLAGS
225225
GetKeyboardState
226226
MapVirtualKey
227227
GetKeyboardLayout
228+
S_FALSE
229+
IExecuteCommand
230+
IObjectWithSelection
231+
SHCreateShellItemArrayFromShellItem
232+
IShellExtInit
233+
IContextMenu2
234+
GetSubMenu
235+
GetMenuItemCount
236+
GetMenuItemInfo
237+
IsWow64Process2
238+
GetCurrentProcess

src/Files.App.Storage/Storables/HomeFolder/HomeFolder.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Files.App.Storage.Storables
99
{
10-
public partial class HomeFolder : IHomeFolder
10+
public unsafe partial class HomeFolder : IHomeFolder
1111
{
1212
public string Id => "Home"; // Will be "files://Home" in the future.
1313

@@ -48,38 +48,36 @@ public IAsyncEnumerable<IStorableChild> GetQuickAccessFolderAsync(CancellationTo
4848
/// <inheritdoc/>
4949
public IAsyncEnumerable<IStorableChild> GetLogicalDrivesAsync(CancellationToken cancellationToken = default)
5050
{
51-
return GetLogicalDrives().ToAsyncEnumerable();
51+
var availableDrives = PInvoke.GetLogicalDrives();
52+
if (availableDrives is 0)
53+
return Enumerable.Empty<IStorableChild>().ToAsyncEnumerable();
5254

53-
IEnumerable<IStorableChild> GetLogicalDrives()
54-
{
55-
var availableDrives = PInvoke.GetLogicalDrives();
56-
if (availableDrives is 0)
57-
yield break;
58-
59-
int count = BitOperations.PopCount(availableDrives);
60-
var driveLetters = new char[count];
55+
int count = BitOperations.PopCount(availableDrives);
56+
var driveLetters = new char[count];
6157

62-
count = 0;
63-
char driveLetter = 'A';
64-
while (availableDrives is not 0)
65-
{
66-
if ((availableDrives & 1) is not 0)
67-
driveLetters[count++] = driveLetter;
58+
count = 0;
59+
char driveLetter = 'A';
60+
while (availableDrives is not 0)
61+
{
62+
if ((availableDrives & 1) is not 0)
63+
driveLetters[count++] = driveLetter;
6864

69-
availableDrives >>= 1;
70-
driveLetter++;
71-
}
65+
availableDrives >>= 1;
66+
driveLetter++;
67+
}
7268

73-
foreach (char letter in driveLetters)
74-
{
75-
cancellationToken.ThrowIfCancellationRequested();
69+
List<IStorableChild> driveItems = [];
70+
foreach (char letter in driveLetters)
71+
{
72+
cancellationToken.ThrowIfCancellationRequested();
7673

77-
if (WindowsStorable.TryParse($"{letter}:\\") is not IWindowsStorable driveRoot)
78-
throw new InvalidOperationException();
74+
if (WindowsStorable.TryParse($"{letter}:\\") is not IWindowsStorable driveRoot)
75+
throw new InvalidOperationException();
7976

80-
yield return new WindowsFolder(driveRoot.ThisPtr);
81-
}
77+
driveItems.Add(new WindowsFolder(driveRoot.ThisPtr));
8278
}
79+
80+
return driveItems.ToAsyncEnumerable();
8381
}
8482

8583
/// <inheritdoc/>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Storage
5+
{
6+
/// <summary>
7+
/// Represents a Windows Shell ContextMenu item.
8+
/// </summary>
9+
public partial class ContextMenuItem
10+
{
11+
public ContextMenuType Type { get; set; }
12+
13+
public uint Id { get; set; }
14+
15+
public byte[]? Icon { get; set; }
16+
17+
public string? Name { get; set; }
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Storage
5+
{
6+
public enum ContextMenuType
7+
{
8+
Normal = 0x00000000,
9+
10+
Disabled = 0x00000003,
11+
12+
Checked = 0x00000008,
13+
14+
Highlighted = 0x00000080,
15+
16+
Default = 0x00001000,
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.App.Storage
8+
{
9+
public interface IFolderSettings
10+
{
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Storage
5+
{
6+
public interface IWindowsFile : IWindowsStorable, IChildFile
7+
{
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Windows.Win32.UI.Shell;
5+
6+
namespace Files.App.Storage
7+
{
8+
public unsafe interface IWindowsFolder : IWindowsStorable, IChildFolder
9+
{
10+
/// <summary>
11+
/// Gets or sets the cached <see cref="IContextMenu"/> for the ShellNew context menu.
12+
/// </summary>
13+
public IContextMenu* ShellNewMenu { get; set; }
14+
}
15+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using Windows.Win32;
54
using Windows.Win32.UI.Shell;
65

76
namespace Files.App.Storage
87
{
9-
public interface IWindowsStorable : IDisposable
8+
public unsafe interface IWindowsStorable : IStorableChild, IEquatable<IWindowsStorable>, IDisposable
109
{
11-
ComPtr<IShellItem> ThisPtr { get; }
10+
IShellItem* ThisPtr { get; set; }
11+
12+
IContextMenu* ContextMenu { get; set; }
1213
}
1314
}

0 commit comments

Comments
 (0)