Skip to content

Commit e19995e

Browse files
committed
Added ExecutePinItemCommand
1 parent baf9105 commit e19995e

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

JumpListManager.WinUI/ViewModels/MainPageViewModel.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public unsafe void EnumerateContextMenuItems()
140140
}
141141
else
142142
{
143-
CommandItems.Add(new CommandButtonItem("\uE718", "Pin to the list", null));
143+
CommandItems.Add(new CommandButtonItem("\uE718", "Pin to the list", new RelayCommand(ExecutePinItemCommand)));
144144
CommandItems.Add(new CommandButtonItem("\uE74D", "Remove from the list", null));
145145
}
146146

@@ -150,6 +150,21 @@ public unsafe void EnumerateContextMenuItems()
150150
}
151151
}
152152

153+
private void ExecutePinItemCommand()
154+
{
155+
var amuid = ApplicationItems.ElementAt(SelectedIndexOfApplicationItems).AppUserModelID;
156+
var flattenItems = GroupedJumpListItems.SelectMany(group => group).ToList();
157+
var selectedJumpListItem = flattenItems.ElementAt(SelectedIndexOfJumpListItems);
158+
159+
// Initialize the jump list manager
160+
using JumpList manager = JumpList.Create(amuid)
161+
?? throw new InvalidOperationException($"Failed to initialize {nameof(JumpListManager)}.");
162+
163+
manager.PinItem(selectedJumpListItem);
164+
165+
EnumerateJumpListItems();
166+
}
167+
153168
private async Task ExecuteOpenAboutDialogCommand()
154169
{
155170
var dialog = new Views.SettingsDialog

JumpListManager.WinUI/Views/MainPage.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
HorizontalAlignment="Stretch"
3030
HorizontalContentAlignment="Left"
3131
Background="Transparent"
32-
BorderThickness="0">
32+
BorderThickness="0"
33+
Command="{x:Bind Command}">
3334
<Grid ColumnSpacing="8">
3435
<Grid.ColumnDefinitions>
3536
<ColumnDefinition Width="Auto" />

JumpListManager/JumpList.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public int GetCustomDestinationsCount()
215215
if (dwCategoryCount is -1)
216216
return null;
217217

218-
JumpListGroupItem groupedCollection = new() { Key = string.Empty };
218+
JumpListGroupItem groupedCollection = new() { Key = "Tasks" };
219219

220220
for (uint dwCategoryIndex = 0U; dwCategoryIndex < (uint)dwCategoryCount; dwCategoryIndex++)
221221
{
@@ -248,6 +248,20 @@ public int GetCustomDestinationsCount()
248248
return null;
249249
}
250250

251+
public bool PinItem(JumpListItem item)
252+
{
253+
HRESULT hr = default;
254+
255+
if (item is null || item.IsPinned || item.Type is not JumpListItemType.Automatic or JumpListItemType.Custom)
256+
return false;
257+
258+
hr = _autoDestListPtr->PinItem((IUnknown*)item.NativeObjectPtr, -1);
259+
if (FAILED(hr)) return false;
260+
261+
item.IsPinned = true;
262+
return true;
263+
}
264+
251265
private IEnumerable<JumpListItem> CreateCollectionFromIObjectCollection(JumpListItemType type, IObjectCollection* pObjectCollection)
252266
{
253267
HRESULT hr = default;
@@ -311,7 +325,7 @@ private IEnumerable<JumpListItem> CreateCollectionFromIObjectCollection(JumpList
311325
IShellLinkW* pShellLinkGlobal = null;
312326
pShellLink.CopyTo(&pShellLinkGlobal);
313327

314-
return new(type, bitmapImageData, new string(pVar.Anonymous.Anonymous.Anonymous.pwszVal), IsPinned((IUnknown*)pShellLink.Get()), JumpListDataType.IShellLink, (IUnknown*)pShellLinkGlobal);
328+
return new(type, bitmapImageData, new string(pVar.Anonymous.Anonymous.Anonymous.pwszVal), IsPinned((IUnknown*)pShellLinkGlobal), JumpListDataType.IShellLink, (IUnknown*)pShellLinkGlobal);
315329
}
316330
else
317331
{

0 commit comments

Comments
 (0)