Skip to content

Commit 8e6f41d

Browse files
committed
Update
1 parent a44e480 commit 8e6f41d

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Windows.Win32;
77
using Windows.Win32.Foundation;
88
using Windows.Win32.System.Com;
9+
using Windows.Win32.System.WinRT;
910

1011
namespace Windows.Win32
1112
{
@@ -16,8 +17,13 @@ public unsafe struct ComPtr<T> : IDisposable where T : unmanaged, IComIID
1617
{
1718
private T* _ptr;
1819

19-
public bool IsNull
20-
=> _ptr == null;
20+
public readonly bool IsNull
21+
{
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23+
get => _ptr is null;
24+
}
25+
26+
// Constructors
2127

2228
public ComPtr(T* ptr)
2329
{
@@ -27,6 +33,9 @@ public ComPtr(T* ptr)
2733
((IUnknown*)ptr)->AddRef();
2834
}
2935

36+
// Methods
37+
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3039
public void Attach(T* other)
3140
{
3241
if (_ptr is not null)
@@ -35,6 +44,14 @@ public void Attach(T* other)
3544
_ptr = other;
3645
}
3746

47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
public T* Detach()
49+
{
50+
T* ptr = _ptr;
51+
_ptr = null;
52+
return ptr;
53+
}
54+
3855
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3956
public readonly T* Get()
4057
{
@@ -48,19 +65,62 @@ public void Attach(T* other)
4865
}
4966

5067
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68+
[Obsolete("Use `HRESULT As<U>(U** other)` instead.")]
5169
public readonly ComPtr<U> As<U>() where U : unmanaged, IComIID
5270
{
5371
ComPtr<U> ptr = default;
5472
((IUnknown*)_ptr)->QueryInterface((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in U.Guid)), (void**)ptr.GetAddressOf());
5573
return ptr;
5674
}
5775

76+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
77+
public readonly HRESULT As<U>(U** other) where U : unmanaged, IComIID
78+
{
79+
return ((IUnknown*)_ptr)->QueryInterface((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in U.Guid)), (void**)other);
80+
}
81+
82+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
83+
public readonly HRESULT As<U>(Guid* riid, IUnknown** other) where U : unmanaged, IComIID
84+
{
85+
return ((IUnknown*)_ptr)->QueryInterface(riid, (void**)other);
86+
}
87+
5888
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5989
public readonly HRESULT CoCreateInstance(Guid* rclsid, IUnknown* pUnkOuter = null, CLSCTX dwClsContext = CLSCTX.CLSCTX_LOCAL_SERVER)
6090
{
6191
return PInvoke.CoCreateInstance(rclsid, pUnkOuter, dwClsContext, (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (void**)this.GetAddressOf());
6292
}
6393

94+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95+
public readonly HRESULT GetAgileReference(ref ComPtr<IAgileReference> pAgileReference)
96+
{
97+
return PInvoke.RoGetAgileReference(AgileReferenceOptions.AGILEREFERENCE_DEFAULT, (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (IUnknown*)_ptr, pAgileReference.GetAddressOf());
98+
}
99+
100+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101+
public readonly HRESULT ResolveAgileReference(ref ComPtr<IAgileReference> pAgileReference, ref ComPtr<T> other)
102+
{
103+
return pAgileReference.Get()->Resolve((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (void**)other.GetAddressOf());
104+
}
105+
106+
// Conversion operators
107+
108+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109+
public static implicit operator ComPtr<T>(T* other)
110+
{
111+
ComPtr<T> ptr = default;
112+
ptr.Attach(other);
113+
return ptr;
114+
}
115+
116+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117+
public static implicit operator T*(ComPtr<T> other)
118+
{
119+
return other._ptr;
120+
}
121+
122+
// Disposer
123+
64124
[MethodImpl(MethodImplOptions.AggressiveInlining)]
65125
public void Dispose()
66126
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed partial class WidgetFolderCardItem : WidgetCardItem, IWidgetCardIt
2626

2727
public WidgetFolderCardItem(IWindowsStorable item, string text, bool isPinned)
2828
{
29-
AutomationProperties = Text;
29+
AutomationProperties = text;
3030
Item = item;
3131
Text = text;
3232
IsPinned = isPinned;

src/Files.App/ViewModels/UserControls/Widgets/QuickAccessWidgetViewModel.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ public override async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
191191

192192
unsafe
193193
{
194-
hr = PInvoke.RoGetAgileReference(
195-
AgileReferenceOptions.AGILEREFERENCE_DEFAULT,
196-
IID.IID_IShellItem,
197-
(IUnknown*)folderCardItem.Item.ThisPtr.Get(),
198-
pAgileReference.GetAddressOf());
194+
hr = PInvoke.RoGetAgileReference(AgileReferenceOptions.AGILEREFERENCE_DEFAULT, IID.IID_IShellItem, (IUnknown*)folderCardItem.Item.ThisPtr.Get(), pAgileReference.GetAddressOf());
199195
}
200196

201197
// Pin to Quick Access on Windows
@@ -214,19 +210,20 @@ public override async Task ExecutePinToSidebarCommand(WidgetCardItem? item)
214210
if (hr.ThrowIfFailedOnDebug().Failed)
215211
return;
216212

217-
// Remove it from the collection
218-
Items.Remove(folderCardItem);
219-
220213
// Add this to right before the last pinned item
221-
var theLastPinnedItem = Items.LastOrDefault(x => x.IsPinned);
222-
Items.Insert(theLastPinnedItem is not null ? Items.IndexOf(theLastPinnedItem) : 0, folderCardItem);
214+
var lastPinnedItemIndex = Items.LastOrDefault(x => x.IsPinned) is { } lastPinnedItem ? Items.IndexOf(lastPinnedItem) : 0;
215+
var currentPinnedItemIndex = Items.IndexOf(folderCardItem);
216+
if (lastPinnedItemIndex != currentPinnedItemIndex && currentPinnedItemIndex is not -1)
217+
Items.Move(currentPinnedItemIndex, lastPinnedItemIndex + 1);
223218
}
224219

225220
public override async Task ExecuteUnpinFromSidebarCommand(WidgetCardItem? item)
226221
{
227222
if (item is not WidgetFolderCardItem folderCardItem || item.Path is null)
228223
return;
229224

225+
// Undocumented verb, which calls an undocumented COM class, windows.storage.dll!CPinToFrequentExecute CRemoveFromFrequentPlacesExecute CRemoveFromRecentPlacesExecute
226+
230227
// Unpin from Quick Access on Windows
231228
HRESULT hr = await STATask.Run(() => folderCardItem.Item.TryInvokeContextMenuVerbs(["unpinfromhome", "remove"], true));
232229
if (hr.ThrowIfFailedOnDebug().Failed)

0 commit comments

Comments
 (0)