Skip to content

Feature: Improved performance for the Quick Access widget #17088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/Files.App.CsWin32/ComPtr`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.System.Com;
using Windows.Win32.System.WinRT;

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

public bool IsNull
=> _ptr == null;
public readonly bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _ptr is null;
}

// Constructors

public ComPtr(T* ptr)
{
Expand All @@ -27,6 +33,9 @@ public ComPtr(T* ptr)
((IUnknown*)ptr)->AddRef();
}

// Methods

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Attach(T* other)
{
if (_ptr is not null)
Expand All @@ -35,6 +44,14 @@ public void Attach(T* other)
_ptr = other;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T* Detach()
{
T* ptr = _ptr;
_ptr = null;
return ptr;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T* Get()
{
Expand All @@ -48,19 +65,50 @@ public void Attach(T* other)
}

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly HRESULT As<U>(U** other) where U : unmanaged, IComIID
{
return ((IUnknown*)_ptr)->QueryInterface((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in U.Guid)), (void**)other);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly HRESULT As<U>(Guid* riid, IUnknown** other) where U : unmanaged, IComIID
{
return ((IUnknown*)_ptr)->QueryInterface(riid, (void**)other);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly HRESULT CoCreateInstance(Guid* rclsid, IUnknown* pUnkOuter = null, CLSCTX dwClsContext = CLSCTX.CLSCTX_LOCAL_SERVER)
{
return PInvoke.CoCreateInstance(rclsid, pUnkOuter, dwClsContext, (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in T.Guid)), (void**)this.GetAddressOf());
}

// Conversion operators

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ComPtr<T>(T* other)
{
ComPtr<T> ptr = default;
ptr.Attach(other);
return ptr;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T*(ComPtr<T> other)
{
return other._ptr;
}

// Disposer

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App.CsWin32/ManualGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static Guid* IID_IStorageProviderStatusUISourceFactory

[GuidRVAGen.Guid("2E941141-7F97-4756-BA1D-9DECDE894A3D")]
public static partial Guid* IID_IApplicationActivationManager { get; }

[GuidRVAGen.Guid("00021500-0000-0000-C000-000000000046")]
public static partial Guid* IID_IQueryInfo { get; }
}

public static unsafe partial class CLSID
Expand Down
4 changes: 4 additions & 0 deletions src/Files.App.CsWin32/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
GetKeyState
CreateDirectoryFromApp
WNetCancelConnection2
NET_USE_CONNECT_FLAGS

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found
NETRESOURCEW
WNetAddConnection3
CREDENTIALW
Expand Down Expand Up @@ -78,7 +78,7 @@
SetEntriesInAcl
ACL_SIZE_INFORMATION
DeleteAce
EXPLICIT_ACCESS

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, x64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?
ACCESS_ALLOWED_ACE
LookupAccountSid
GetComputerName
Expand Down Expand Up @@ -134,7 +134,7 @@
CoTaskMemFree
QueryDosDevice
DeviceIoControl
GetLastError

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Release, x64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error
CreateFile
GetVolumeInformation
COMPRESSION_FORMAT
Expand Down Expand Up @@ -222,3 +222,7 @@
GdipCreateBitmapFromScan0
BITMAP
GetObject
_SICHINTF
RoGetAgileReference
IQueryInfo
QITIPF_FLAGS
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Files.App.Storage
{
public abstract class WindowsStorable : IWindowsStorable, IStorableChild
public abstract class WindowsStorable : IWindowsStorable, IStorableChild, IEquatable<IWindowsStorable>
{
public ComPtr<IShellItem> ThisPtr { get; protected set; }

Expand Down Expand Up @@ -65,6 +65,17 @@ public abstract class WindowsStorable : IWindowsStorable, IStorableChild
return Task.FromResult<IFolder?>(new WindowsFolder(pParentFolder));
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
return Equals(obj as IWindowsStorable);
}

public override int GetHashCode()
{
return HashCode.Combine(Id, Name);
}

/// <inheritdoc/>
public void Dispose()
{
Expand All @@ -76,5 +87,20 @@ public override string ToString()
{
return this.GetDisplayName();
}

/// <inheritdoc/>
public unsafe bool Equals(IWindowsStorable? other)
{
if (other is null)
return false;

return ThisPtr.Get()->Compare(other.ThisPtr.Get(), (uint)_SICHINTF.SICHINT_DISPLAY, out int order).Succeeded && order is 0;
}

public static bool operator ==(WindowsStorable left, WindowsStorable right)
=> left.Equals(right);

public static bool operator !=(WindowsStorable left, WindowsStorable right)
=> !(left == right);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using System.Runtime.CompilerServices;
using System.Text;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.System.SystemServices;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.Shell.PropertiesSystem;
using Windows.Win32.UI.WindowsAndMessaging;

namespace Files.App.Storage
{
Expand All @@ -13,14 +17,27 @@ public static partial class WindowsStorableHelpers
public unsafe static HRESULT GetPropertyValue<TValue>(this IWindowsStorable storable, string propKey, out TValue value)
{
using ComPtr<IShellItem2> pShellItem2 = default;
var shellItem2Iid = typeof(IShellItem2).GUID;
HRESULT hr = storable.ThisPtr.Get()->QueryInterface(&shellItem2Iid, (void**)pShellItem2.GetAddressOf());
hr = PInvoke.PSGetPropertyKeyFromName(propKey, out var originalPathPropertyKey);
hr = pShellItem2.Get()->GetString(originalPathPropertyKey, out var szOriginalPath);
HRESULT hr = storable.ThisPtr.Get()->QueryInterface(IID.IID_IShellItem2, (void**)pShellItem2.GetAddressOf());

PROPERTYKEY propertyKey = default;
fixed (char* pszPropertyKey = propKey)
hr = PInvoke.PSGetPropertyKeyFromName(pszPropertyKey, &propertyKey);

if (typeof(TValue) == typeof(string))
{
value = (TValue)(object)szOriginalPath.ToString();
ComHeapPtr<PWSTR> szPropertyValue = default;
hr = pShellItem2.Get()->GetString(&propertyKey, szPropertyValue.Get());
value = (TValue)(object)szPropertyValue.Get()->ToString();

return hr;
}
if (typeof(TValue) == typeof(bool))
{
bool propertyValue = false;
hr = pShellItem2.Get()->GetBool(propertyKey, out var fPropertyValue);
propertyValue = fPropertyValue;
value = Unsafe.As<bool, TValue>(ref propertyValue);

return hr;
}
else
Expand Down Expand Up @@ -51,5 +68,80 @@ public unsafe static string GetDisplayName(this IWindowsStorable storable, SIGDN
? new string((char*)pszName.Get()) // this is safe as it gets memcpy'd internally
: string.Empty;
}

public unsafe static HRESULT TryInvokeContextMenuVerb(this IWindowsStorable storable, string verbName)
{
Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA);

using ComPtr<IContextMenu> pContextMenu = default;
HRESULT hr = storable.ThisPtr.Get()->BindToHandler(null, BHID.BHID_SFUIObject, IID.IID_IContextMenu, (void**)pContextMenu.GetAddressOf());
HMENU hMenu = PInvoke.CreatePopupMenu();
hr = pContextMenu.Get()->QueryContextMenu(hMenu, 0, 1, 0x7FFF, PInvoke.CMF_OPTIMIZEFORINVOKE);

CMINVOKECOMMANDINFO cmici = default;
cmici.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
cmici.nShow = (int)SHOW_WINDOW_CMD.SW_HIDE;

fixed (byte* pszVerbName = Encoding.ASCII.GetBytes(verbName))
{
cmici.lpVerb = new(pszVerbName);
hr = pContextMenu.Get()->InvokeCommand(cmici);

if (!PInvoke.DestroyMenu(hMenu))
return HRESULT.E_FAIL;

return hr;
}
}

public unsafe static HRESULT TryInvokeContextMenuVerbs(this IWindowsStorable storable, string[] verbNames, bool earlyReturnOnSuccess)
{
Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA);

using ComPtr<IContextMenu> pContextMenu = default;
HRESULT hr = storable.ThisPtr.Get()->BindToHandler(null, BHID.BHID_SFUIObject, IID.IID_IContextMenu, (void**)pContextMenu.GetAddressOf());
HMENU hMenu = PInvoke.CreatePopupMenu();
hr = pContextMenu.Get()->QueryContextMenu(hMenu, 0, 1, 0x7FFF, PInvoke.CMF_OPTIMIZEFORINVOKE);

CMINVOKECOMMANDINFO cmici = default;
cmici.cbSize = (uint)sizeof(CMINVOKECOMMANDINFO);
cmici.nShow = (int)SHOW_WINDOW_CMD.SW_HIDE;

foreach (var verbName in verbNames)
{
fixed (byte* pszVerbName = Encoding.ASCII.GetBytes(verbName))
{
cmici.lpVerb = new(pszVerbName);
hr = pContextMenu.Get()->InvokeCommand(cmici);

if (!PInvoke.DestroyMenu(hMenu))
return HRESULT.E_FAIL;

if (hr.Succeeded && earlyReturnOnSuccess)
return hr;
}
}

return hr;
}

public unsafe static HRESULT TryGetShellTooltip(this IWindowsStorable storable, out string? tooltip)
{
tooltip = null;

using ComPtr<IQueryInfo> pQueryInfo = default;
HRESULT hr = storable.ThisPtr.Get()->BindToHandler(null, BHID.BHID_SFUIObject, IID.IID_IQueryInfo, (void**)pQueryInfo.GetAddressOf());
if (hr.ThrowIfFailedOnDebug().Failed)
return hr;

pQueryInfo.Get()->GetInfoTip((uint)QITIPF_FLAGS.QITIPF_DEFAULT, out var pszTip);
if (hr.ThrowIfFailedOnDebug().Failed)
return hr;

tooltip = pszTip.ToString();
PInvoke.CoTaskMemFree(pszTip);

return HRESULT.S_OK;
}
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Data/Contexts/HomePage/HomePageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public sealed partial class HomePageContext : ObservableObject, IHomePageContext

public bool IsAnyItemRightClicked => rightClickedItem is not null;

public IHomeFolder HomeFolder { get; } = new HomeFolder();

private WidgetCardItem? rightClickedItem = null;
public WidgetCardItem? RightClickedItem => rightClickedItem;

Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/Data/Contexts/HomePage/IHomePageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ public interface IHomePageContext
/// Tells whether any item has been right clicked
/// </summary>
bool IsAnyItemRightClicked { get; }

/// <summary>
/// Gets the instance of <see cref="IHomeFolder"/>.
/// </summary>
IHomeFolder HomeFolder { get; }
}
}
Loading
Loading