Skip to content

Commit 82f3d19

Browse files
committed
Added ExecuteRemoveItemCommand
1 parent f105a8a commit 82f3d19

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

JumpListManager.WinUI/ViewModels/MainPageViewModel.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ public class MainPageViewModel : ObservableObject
2929

3030
public int SelectedIndexOfJumpListItems { get; set; } = 0;
3131

32+
public JumpListItem? SelectedJumpListItem
33+
{
34+
get
35+
{
36+
if (SelectedIndexOfJumpListItems is -1)
37+
return null;
38+
var flattenItems = GroupedJumpListItems.SelectMany(group => group).ToList();
39+
return flattenItems.ElementAt(SelectedIndexOfJumpListItems);
40+
}
41+
}
42+
43+
public string? SelectedApplicationUserModelID
44+
{
45+
get
46+
{
47+
if (SelectedIndexOfApplicationItems is -1)
48+
return null;
49+
return ApplicationItems.ElementAt(SelectedIndexOfApplicationItems).AppUserModelID;
50+
}
51+
}
52+
3253
public ICommand OpenAboutDialogCommand { get; }
3354

3455
public MainPageViewModel()
@@ -141,7 +162,7 @@ public unsafe void EnumerateContextMenuItems()
141162
else
142163
{
143164
CommandItems.Add(new CommandButtonItem("\uE718", "Pin to the list", new RelayCommand(ExecutePinItemCommand)));
144-
CommandItems.Add(new CommandButtonItem("\uE74D", "Remove from the list", null));
165+
CommandItems.Add(new CommandButtonItem("\uE74D", "Remove from the list", new RelayCommand(ExecuteRemoveItemCommand)));
145166
}
146167

147168
CommandItems.Add(new CommandSeparatorItem());
@@ -178,6 +199,16 @@ private void ExecuteUnpinItemCommand()
178199
EnumerateJumpListItems();
179200
}
180201

202+
private void ExecuteRemoveItemCommand()
203+
{
204+
using JumpList manager = JumpList.Create(SelectedApplicationUserModelID!)
205+
?? throw new InvalidOperationException($"Failed to initialize {nameof(JumpListManager)}.");
206+
207+
manager.RemoveItem(SelectedJumpListItem!);
208+
209+
EnumerateJumpListItems();
210+
}
211+
181212
private async Task ExecuteOpenAboutDialogCommand()
182213
{
183214
var dialog = new Views.SettingsDialog

JumpListManager/IAutomaticDestinationList.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Runtime.CompilerServices;
66
using Windows.Win32.Foundation;
7+
using Windows.Win32.UI.Shell;
78

89
namespace Windows.Win32.System.Com
910
{
@@ -85,6 +86,33 @@ public HRESULT GetPinIndex(IUnknown* pObj, int* pIndex)
8586
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, IUnknown*, int*, int>)lpVtbl[8])
8687
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), pObj, pIndex);
8788

89+
/// <summary>
90+
/// Removes a destination from the automatic destinations list.
91+
/// </summary>
92+
/// <param name="psi">The destination to remove from the automatic destinations list.</param>
93+
/// <returns>Returns <see cref="HRESULT.S_OK"/> if successful, or an error value otherwise.
94+
public HRESULT RemoveDestination(IUnknown* psi)
95+
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, IUnknown*, int>)lpVtbl[9])
96+
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), psi);
97+
98+
public HRESULT SetUsageData(IUnknown* pItem, float* a2, long* pFileTime)
99+
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, IUnknown*,float*, long*, int>)lpVtbl[10])
100+
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), pItem, a2, pFileTime);
101+
102+
public HRESULT GetUsageData(IUnknown* pItem, float* a2, long* pFileTime)
103+
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, IUnknown*, float*, long*, int>)lpVtbl[11])
104+
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), pItem, a2, pFileTime);
105+
106+
public HRESULT ResolveDestination(HWND hWnd, int a2, IShellItem* pShellItem, Guid* riid, void** ppvObject)
107+
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, HWND, int, IShellItem*, Guid*, void**, int>)lpVtbl[12])
108+
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), hWnd, a2, pShellItem, riid, ppvObject);
109+
110+
public HRESULT ClearList(int iListType)
111+
=> (HRESULT)((delegate* unmanaged[MemberFunction]<IAutomaticDestinationList*, int, int>)lpVtbl[13])
112+
((IAutomaticDestinationList*)Unsafe.AsPointer(ref this), iListType);
113+
114+
115+
88116
[GuidRVAGen.Guid("E9C5EF8D-FD41-4F72-BA87-EB03BAD5817C")]
89117
public static partial ref readonly Guid Guid { get; }
90118
}

JumpListManager/JumpList.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public int GetCustomDestinationsCount()
250250

251251
public bool PinItem(JumpListItem item)
252252
{
253-
if (item is null || item.IsPinned || item.Type is not JumpListItemType.Automatic or JumpListItemType.Custom)
253+
if (item is null || item.IsPinned || item.Type is JumpListItemType.Task)
254254
return false;
255255

256256
HRESULT hr = default;
@@ -264,7 +264,7 @@ public bool PinItem(JumpListItem item)
264264

265265
public bool UnpinItem(JumpListItem item)
266266
{
267-
if (item is null || !item.IsPinned || item.Type is not JumpListItemType.Automatic or JumpListItemType.Custom)
267+
if (item is null || !item.IsPinned || item.Type is JumpListItemType.Task)
268268
return false;
269269

270270
HRESULT hr = default;
@@ -276,6 +276,27 @@ public bool UnpinItem(JumpListItem item)
276276
return true;
277277
}
278278

279+
public bool RemoveItem(JumpListItem item)
280+
{
281+
if (item is null || item.Type is JumpListItemType.Task)
282+
return false;
283+
284+
HRESULT hr = default;
285+
286+
if (item.Type is JumpListItemType.Automatic)
287+
{
288+
hr = _autoDestListPtr->RemoveDestination((IUnknown*)item.NativeObjectPtr);
289+
if (FAILED(hr)) return false;
290+
}
291+
else if (item.Type is JumpListItemType.Custom)
292+
{
293+
hr = _customDestList2Ptr->RemoveDestination((IUnknown*)item.NativeObjectPtr);
294+
if (FAILED(hr)) return false;
295+
}
296+
297+
return true;
298+
}
299+
279300
private IEnumerable<JumpListItem> CreateCollectionFromIObjectCollection(JumpListItemType type, IObjectCollection* pObjectCollection)
280301
{
281302
HRESULT hr = default;

0 commit comments

Comments
 (0)