-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Code Quality: Introduced the new WindowsStorable #17002
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
yaira2
merged 2 commits into
files-community:main
from
0x5bfa:5bfa/CQ-WindowsBulkOperations
Apr 22, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 7 additions & 8 deletions
15
src/Files.App.CsWin32/HRESULT.Extensions.cs → src/Files.App.CsWin32/HRESULT.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using Windows.Win32.Foundation; | ||
|
||
namespace Windows.Win32 | ||
namespace Windows.Win32.Foundation | ||
{ | ||
public static class HRESULTExtensions | ||
[DebuggerDisplay("{" + nameof(Value) + ",h}")] | ||
public readonly partial struct HRESULT | ||
{ | ||
/// <summary> | ||
/// Throws an exception if the <see cref="HRESULT"/> indicates a failure in debug mode. Otherwise, it returns the original <see cref="HRESULT"/>. | ||
/// </summary> | ||
/// <param name="hr">Represents the result of an operation, indicating success or failure.</param> | ||
/// <returns>Returns the original <see cref="HRESULT"/> value regardless of the operation's success.</returns> | ||
public static HRESULT ThrowIfFailedOnDebug(this HRESULT hr) | ||
public readonly HRESULT ThrowIfFailedOnDebug() | ||
{ | ||
#if DEBUG | ||
if (hr.Failed) | ||
Marshal.ThrowExceptionForHR(hr.Value); | ||
if (Failed) Marshal.ThrowExceptionForHR(Value); | ||
#endif | ||
|
||
return hr; | ||
return this; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
using Windows.Win32; | ||
|
||
namespace Files.App.Storage.Storables | ||
{ | ||
public partial class HomeFolder : IHomeFolder | ||
{ | ||
public string Id => "Home"; // Will be "files://Home" in the future. | ||
|
||
public string Name => "Home"; | ||
|
||
/// <inheritdoc/> | ||
public async IAsyncEnumerable<IStorableChild> GetItemsAsync(StorableType type = StorableType.Folder, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
await foreach (var folder in GetQuickAccessFolderAsync(cancellationToken)) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
yield return folder; | ||
} | ||
|
||
await foreach (var drive in GetLogicalDrivesAsync(cancellationToken)) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
yield return drive; | ||
} | ||
|
||
await foreach (var location in GetNetworkLocationsAsync(cancellationToken)) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
yield return location; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IAsyncEnumerable<IStorableChild> GetQuickAccessFolderAsync(CancellationToken cancellationToken = default) | ||
{ | ||
IFolder folder = new WindowsFolder(new Guid("3936e9e4-d92c-4eee-a85a-bc16d5ea0819")); | ||
return folder.GetItemsAsync(StorableType.Folder, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async IAsyncEnumerable<IStorableChild> GetLogicalDrivesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
var availableDrives = PInvoke.GetLogicalDrives(); | ||
if (availableDrives is 0) | ||
yield break; | ||
|
||
int count = BitOperations.PopCount(availableDrives); | ||
var driveLetters = new char[count]; | ||
|
||
count = 0; | ||
char driveLetter = 'A'; | ||
while (availableDrives is not 0) | ||
{ | ||
if ((availableDrives & 1) is not 0) | ||
driveLetters[count++] = driveLetter; | ||
|
||
availableDrives >>= 1; | ||
driveLetter++; | ||
} | ||
|
||
foreach (int letter in driveLetters) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if (WindowsStorable.TryParse($"{letter}:\\") is not IWindowsStorable driveRoot) | ||
throw new InvalidOperationException(); | ||
|
||
yield return new WindowsFolder(driveRoot.ThisPtr); | ||
await Task.Yield(); | ||
} | ||
|
||
} | ||
|
||
/// <inheritdoc/> | ||
public IAsyncEnumerable<IStorableChild> GetNetworkLocationsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
Guid FOLDERID_NetHood = new("{C5ABBF53-E17F-4121-8900-86626FC2C973}"); | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IFolder folder = new WindowsFolder(FOLDERID_NetHood); | ||
return folder.GetItemsAsync(StorableType.Folder, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IAsyncEnumerable<IStorableChild> GetRecentFilesAsync(CancellationToken cancellationToken = default) | ||
{ | ||
Guid FOLDERID_NetHood = new("{AE50C081-EBD2-438A-8655-8A092E34987A}"); | ||
IFolder folder = new WindowsFolder(FOLDERID_NetHood); | ||
return folder.GetItemsAsync(StorableType.Folder, cancellationToken); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
namespace Files.App.Storage.Storables | ||
{ | ||
public partial interface IHomeFolder : IFolder | ||
{ | ||
/// <summary> | ||
/// Gets quick access folders. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>A list of the collection.</returns> | ||
public IAsyncEnumerable<IStorableChild> GetQuickAccessFolderAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets available logical drives. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>A list of the collection.</returns> | ||
public IAsyncEnumerable<IStorableChild> GetLogicalDrivesAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets network locations(shortcuts). | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>A list of the collection.</returns> | ||
public IAsyncEnumerable<IStorableChild> GetNetworkLocationsAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets recent files. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>A list of the collection.</returns> | ||
public IAsyncEnumerable<IStorableChild> GetRecentFilesAsync(CancellationToken cancellationToken = default); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Files.App.Storage/Storables/WindowsStorage/IWindowsStorable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
using Windows.Win32; | ||
using Windows.Win32.UI.Shell; | ||
|
||
namespace Files.App.Storage | ||
{ | ||
public interface IWindowsStorable | ||
{ | ||
ComPtr<IShellItem> ThisPtr { get; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Files.App.Storage/Storables/WindowsStorage/JumpListItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
namespace Files.App.Storage | ||
{ | ||
public partial class JumpListItem | ||
{ | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Files.App.Storage/Storables/WindowsStorage/JumpListItemType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
namespace Files.App.Storage | ||
{ | ||
public enum JumpListItemType | ||
{ | ||
Item, | ||
|
||
Separator, | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Files.App.Storage/Storables/WindowsStorage/JumpListManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) Files Community | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Concurrent; | ||
using Windows.Win32; | ||
using Windows.Win32.Foundation; | ||
using Windows.Win32.System.Com; | ||
using Windows.Win32.UI.Shell; | ||
using Windows.Win32.UI.Shell.Common; | ||
|
||
namespace Files.App.Storage | ||
{ | ||
public unsafe class JumpListManager : IDisposable | ||
{ | ||
private ComPtr<ICustomDestinationList> pCustomDestinationList = default; | ||
|
||
private static string? AppId | ||
{ | ||
get | ||
{ | ||
PWSTR pszAppId = default; | ||
HRESULT hr = PInvoke.GetCurrentProcessExplicitAppUserModelID(&pszAppId); | ||
if (hr == HRESULT.E_FAIL) | ||
hr = HRESULT.S_OK; | ||
|
||
hr.ThrowIfFailedOnDebug(); | ||
|
||
return pszAppId.ToString(); | ||
} | ||
} | ||
|
||
public ConcurrentBag<JumpListItem> JumpListItems { get; private set; } = []; | ||
|
||
public ConcurrentBag<JumpListItem> RemovedItems { get; private set; } = []; | ||
|
||
public ConcurrentBag<JumpListItem> RejectedItems { get; private set; } = []; | ||
|
||
// A special "Frequent" category managed by Windows | ||
public bool ShowFrequentCategory { get; set; } | ||
|
||
// A special "Recent" category managed by Windows | ||
public bool ShowRecentCategory { get; set; } | ||
|
||
private static JumpListManager? _Default = null; | ||
public static JumpListManager Default { get; } = _Default ??= new JumpListManager(); | ||
|
||
public JumpListManager() | ||
{ | ||
Guid CLSID_CustomDestinationList = typeof(DestinationList).GUID; | ||
Guid IID_ICustomDestinationList = ICustomDestinationList.IID_Guid; | ||
HRESULT hr = PInvoke.CoCreateInstance( | ||
&CLSID_CustomDestinationList, | ||
null, | ||
CLSCTX.CLSCTX_INPROC_SERVER, | ||
&IID_ICustomDestinationList, | ||
(void**)pCustomDestinationList.GetAddressOf()); | ||
|
||
// Should not happen but as a sanity check at an early stage | ||
hr.ThrowOnFailure(); | ||
} | ||
|
||
public HRESULT Save() | ||
{ | ||
Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA); | ||
|
||
HRESULT hr = pCustomDestinationList.Get()->SetAppID(AppId); | ||
|
||
uint cMinSlots = 0; | ||
ComPtr<IObjectArray> pDeletedItemsObjectArray = default; | ||
Guid IID_IObjectArray = IObjectArray.IID_Guid; | ||
|
||
hr = pCustomDestinationList.Get()->BeginList(&cMinSlots, &IID_IObjectArray, (void**)pDeletedItemsObjectArray.GetAddressOf()); | ||
|
||
// TODO: Validate items | ||
|
||
// TODO: Group them as categories | ||
|
||
// TODO: Append a custom category or to the Tasks | ||
|
||
if (ShowFrequentCategory) | ||
pCustomDestinationList.Get()->AppendKnownCategory(KNOWNDESTCATEGORY.KDC_FREQUENT); | ||
|
||
if (ShowRecentCategory) | ||
pCustomDestinationList.Get()->AppendKnownCategory(KNOWNDESTCATEGORY.KDC_RECENT); | ||
|
||
return HRESULT.S_OK; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
pCustomDestinationList.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.