Skip to content

Commit 954b1fc

Browse files
committed
Req
1 parent 1e64430 commit 954b1fc

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public void Attach(T* ptr)
5555
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5656
public bool Allocate(nuint cch)
5757
{
58-
_ptr = (T*)NativeMemory.Alloc(cch); // malloc()
58+
_ptr = (T*)NativeMemory.Alloc(cch * (nuint)sizeof(T)); // malloc()
5959
return _ptr is not null;
6060
}
6161

6262
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6363
public bool Reallocate(nuint cch)
6464
{
65-
T* ptr = (T*)NativeMemory.Realloc(_ptr, cch); // realloc()
65+
T* ptr = (T*)NativeMemory.Realloc(_ptr, cch * (nuint)sizeof(T)); // realloc()
6666
if (ptr is null) return false;
6767
_ptr = ptr;
6868
return true;

src/Files.App.Storage/Windows/Helpers/WindowsStorageHelpers.System.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static partial class WindowsStorableHelpers
1616
bool fRes = pszBuffer.Allocate(1024U);
1717
if (!fRes) return null;
1818

19-
uint cchBuffer = PInvoke.GetEnvironmentVariable((PCWSTR)Unsafe.AsPointer(ref Unsafe.AsRef(in name.GetPinnableReference())), pszBuffer.Get(), (uint)name.Length + 1);
19+
uint cchBuffer = PInvoke.GetEnvironmentVariable((PCWSTR)Unsafe.AsPointer(ref Unsafe.AsRef(in name.GetPinnableReference())), pszBuffer.Get(), 1024U);
2020
return cchBuffer is 0U ? null : new(pszBuffer.Get());
2121
}
2222

src/Files.App.Storage/Windows/Managers/JumpListManager.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace Files.App.Storage
2323
/// </remarks>
2424
public unsafe class JumpListManager : IDisposable
2525
{
26+
private static readonly Lock _updateJumpListLock = new();
27+
2628
private string _aumid = null!;
2729
private string _exeAlias = null!;
2830
private string _recentCategoryName = null!;
@@ -36,14 +38,14 @@ public unsafe class JumpListManager : IDisposable
3638

3739
private JumpListManager() { }
3840

39-
public static JumpListManager? Create(string amuid, string exeAlias)
41+
public static JumpListManager? Create(string aumid, string exeAlias)
4042
{
4143
var categoryName = WindowsStorableHelpers.ResolveIndirectString($"@{{{WindowsStorableHelpers.GetEnvironmentVariable("SystemRoot")}\\SystemResources\\Windows.UI.ShellCommon\\Windows.UI.ShellCommon.pri? ms-resource://Windows.UI.ShellCommon/JumpViewUI/JumpViewCategoryType_Recent}}");
4244
if (categoryName is null) return null;
4345

4446
return new JumpListManager()
4547
{
46-
_aumid = amuid,
48+
_aumid = aumid,
4749
_exeAlias = exeAlias,
4850
_recentCategoryName = categoryName,
4951
};
@@ -111,12 +113,12 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
111113
if (FAILED(hr)) continue;
112114

113115
// Get an instance of IShellLinkW from the IShellItem instance
114-
IShellLinkW* psl = default;
115-
hr = CreateLinkFromItem(psi.Get(), &psl);
116+
using ComPtr<IShellLinkW> psl = default;
117+
hr = CreateLinkFromItem(psi.Get(), psl.GetAddressOf());
116118
if (FAILED(hr)) continue;
117119

118120
// Pin it to the Files' Automatic Destinations
119-
hr = pFilesADL.Get()->PinItem((IUnknown*)psl, pinIndex);
121+
hr = pFilesADL.Get()->PinItem((IUnknown*)psl.Get(), pinIndex);
120122
if (FAILED(hr)) continue;
121123
}
122124

@@ -147,12 +149,12 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
147149
if (SUCCEEDED(hr)) continue; // If not pinned, HRESULT is E_NOT_SET
148150

149151
// Get an instance of IShellLinkW from the IShellItem instance
150-
IShellLinkW* psl = default;
151-
hr = CreateLinkFromItem(psi.Get(), &psl);
152+
using ComPtr<IShellLinkW> psl = default;
153+
hr = CreateLinkFromItem(psi.Get(), psl.GetAddressOf());
152154
if (FAILED(hr)) continue;
153155

154156
// Add it to the Files' Custom Destinations
155-
hr = pNewObjectCollection.Get()->AddObject((IUnknown*)psl);
157+
hr = pNewObjectCollection.Get()->AddObject((IUnknown*)psl.Get());
156158
if (FAILED(hr)) continue;
157159
}
158160

@@ -224,7 +226,8 @@ public HRESULT PushJumpListToExplorer(int maxCount = 200)
224226

225227
// Get the count of categories in the Files' Custom Destinations
226228
uint count = 0U;
227-
pFilesICDL.Get()->GetCategoryCount(&count);
229+
hr = pFilesICDL.Get()->GetCategoryCount(&count);
230+
if (FAILED(hr)) return hr;
228231

229232
// Find the "Recent" category index
230233
uint indexOfRecentCategory = 0U;
@@ -254,6 +257,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
254257
// Get the items in the "Recent" category
255258
using ComPtr<IObjectCollection> poc = default;
256259
hr = pFilesICDL.Get()->EnumerateCategoryDestinations(indexOfRecentCategory, IID.IID_IObjectCollection, (void**)poc.GetAddressOf());
260+
if (FAILED(hr)) return hr;
257261

258262
// Get the count of items in the "Recent" category
259263
uint countOfItems = 0U;
@@ -274,6 +278,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
274278
using ComHeapPtr<char> pszParseablePath = default;
275279
pszParseablePath.Allocate(PInvoke.MAX_PATH);
276280
hr = psl.Get()->GetArguments(pszParseablePath.Get(), (int)PInvoke.MAX_PATH);
281+
if (FAILED(hr)) continue;
277282

278283
using ComHeapPtr<IShellItem> psi = default;
279284
hr = PInvoke.SHCreateItemFromParsingName(pszParseablePath.Get(), null, IID.IID_IShellItem, (void**)psi.GetAddressOf());
@@ -308,6 +313,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
308313
using ComHeapPtr<char> pszParseablePath = default;
309314
pszParseablePath.Allocate(PInvoke.MAX_PATH);
310315
hr = psl.Get()->GetArguments(pszParseablePath.Get(), (int)PInvoke.MAX_PATH);
316+
if (FAILED(hr)) continue;
311317

312318
using ComHeapPtr<IShellItem> psi = default;
313319
hr = PInvoke.SHCreateItemFromParsingName(pszParseablePath.Get(), null, IID.IID_IShellItem, (void**)psi.GetAddressOf());
@@ -491,33 +497,42 @@ private HRESULT GetFolderIconLocation(IShellItem* psi, PWSTR* pIconFilePath, uin
491497

492498
private void ExplorerJumpListWatcher_Changed(object sender, FileSystemEventArgs e)
493499
{
494-
ExplorerJumpListChanged?.Invoke(this, EventArgs.Empty);
495-
PullJumpListFromExplorer();
500+
lock (_updateJumpListLock)
501+
{
502+
ExplorerJumpListChanged?.Invoke(this, EventArgs.Empty);
503+
PullJumpListFromExplorer();
504+
}
496505
}
497506

498507
private void FilesJumpListWatcher_Changed(object sender, FileSystemEventArgs e)
499508
{
500-
FilesJumpListChanged?.Invoke(this, EventArgs.Empty);
501-
PushJumpListToExplorer();
509+
lock (_updateJumpListLock)
510+
{
511+
FilesJumpListChanged?.Invoke(this, EventArgs.Empty);
512+
PushJumpListToExplorer();
513+
}
502514
}
503515

504516
public void Dispose()
505517
{
506518
if (_explorerADLStoreFileWatcher is not null)
507519
{
508520
_explorerADLStoreFileWatcher.EnableRaisingEvents = false;
521+
_explorerADLStoreFileWatcher.Changed -= ExplorerJumpListWatcher_Changed;
509522
_explorerADLStoreFileWatcher.Dispose();
510523
}
511524

512525
if (_filesADLStoreFileWatcher is not null)
513526
{
514527
_filesADLStoreFileWatcher.EnableRaisingEvents = false;
528+
_filesADLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
515529
_filesADLStoreFileWatcher.Dispose();
516530
}
517531

518532
if (_filesCDLStoreFileWatcher is not null)
519533
{
520534
_filesCDLStoreFileWatcher.EnableRaisingEvents = false;
535+
_filesCDLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
521536
_filesCDLStoreFileWatcher.Dispose();
522537
}
523538
}

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ await Task.WhenAll(
167167
if (JumpListManager is not null)
168168
{
169169
HRESULT hr = JumpListManager.PullJumpListFromExplorer();
170-
if (hr.Failed) App.Logger.LogWarning("Failed to synchronizing jump list unexpectedly.");
170+
if (hr.Failed) App.Logger.LogWarning("Failed to synchronize jump list unexpectedly.");
171171

172172
bool result = JumpListManager.WatchJumpListChanges(AppUserModelIdCrcHash);
173173
if (!result) App.Logger.LogWarning("Failed to watch jump list unexpectedly.");

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,13 @@ public async Task SetWorkingDirectoryAsync(string? value)
228228
if (value == "Home" || value == "ReleaseNotes" || value == "Settings")
229229
currentStorageFolder = null;
230230
else
231-
AppLifecycleHelper.JumpListManager?.AddFolderToRecentCategory(value);
231+
{
232+
_ = STATask.Run(() =>
233+
{
234+
AppLifecycleHelper.JumpListManager?.AddFolderToRecentCategory(value);
235+
},
236+
App.Logger);
237+
}
232238

233239
WorkingDirectory = value;
234240

0 commit comments

Comments
 (0)