Skip to content

Commit 326ebb2

Browse files
authored
Added ability to pin folders to the start menu (#4059)
1 parent 3bd4281 commit 326ebb2

File tree

14 files changed

+168
-6
lines changed

14 files changed

+168
-6
lines changed

Files/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ sealed partial class App : Application
6060

6161
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
6262

63+
public static SecondaryTileHelper SecondaryTileHelper { get; private set; } = new SecondaryTileHelper();
64+
6365
public static class AppData
6466
{
6567
// Get the extensions that are available for this host.

Files/Assets/tile-0-250x250.png

1.62 KB
Loading

Files/Assets/tile-0-300x300.png

1.51 KB
Loading

Files/BaseLayout.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
666666
{
667667
UnloadMenuFlyoutItemByName("SidebarPinItem");
668668
UnloadMenuFlyoutItemByName("SidebarUnpinItem");
669+
UnloadMenuFlyoutItemByName("PinItemToStart");
670+
UnloadMenuFlyoutItemByName("UnpinItemFromStart");
669671
UnloadMenuFlyoutItemByName("OpenInNewTab");
670672
UnloadMenuFlyoutItemByName("OpenInNewWindowItem");
671673
UnloadMenuFlyoutItemByName("OpenInNewPane");
@@ -742,6 +744,7 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
742744
if (SelectedItems.Any(x => x.IsShortcutItem))
743745
{
744746
UnloadMenuFlyoutItemByName("SidebarPinItem");
747+
UnloadMenuFlyoutItemByName("PinItemToStart");
745748
UnloadMenuFlyoutItemByName("CreateShortcut");
746749
}
747750
else if (SelectedItems.Count == 1)
@@ -772,6 +775,17 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
772775
LoadMenuFlyoutItemByName("SidebarPinItem");
773776
UnloadMenuFlyoutItemByName("SidebarUnpinItem");
774777
}
778+
779+
if (selectedItems.All(x => x.IsItemPinnedToStart))
780+
{
781+
UnloadMenuFlyoutItemByName("PinItemToStart");
782+
LoadMenuFlyoutItemByName("UnpinItemFromStart");
783+
}
784+
else
785+
{
786+
LoadMenuFlyoutItemByName("PinItemToStart");
787+
UnloadMenuFlyoutItemByName("UnpinItemFromStart");
788+
}
775789
}
776790

777791
if (SelectedItems.Count <= 5 && SelectedItems.Count > 0)
@@ -796,6 +810,13 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
796810
{
797811
UnloadMenuFlyoutItemByName("OpenInNewPane");
798812
}
813+
814+
//Shift key is not held, remove extras here
815+
if(!Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down))
816+
{
817+
UnloadMenuFlyoutItemByName("PinItemToStart");
818+
UnloadMenuFlyoutItemByName("UnpinItemFromStart");
819+
}
799820
}
800821

801822
//check the file extension of the selected item
@@ -1083,6 +1104,21 @@ public void BaseLayout_PointerWheelChanged(object sender, PointerRoutedEventArgs
10831104
}
10841105
}
10851106

1107+
public async void PinItemToStart_Click(object sender, RoutedEventArgs e)
1108+
{
1109+
foreach (ListedItem listedItem in SelectedItems)
1110+
{
1111+
await App.SecondaryTileHelper.TryPinFolderAsync(listedItem.ItemPath, listedItem.ItemName);
1112+
}
1113+
}
1114+
public async void UnpinItemFromStart_Click(object sender, RoutedEventArgs e)
1115+
{
1116+
foreach (ListedItem listedItem in SelectedItems)
1117+
{
1118+
await App.SecondaryTileHelper.UnpinFromStartAsync(listedItem.ItemPath);
1119+
}
1120+
}
1121+
10861122
public abstract void Dispose();
10871123
}
10881124
}

Files/Files.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
<Compile Include="Interacts\BaseLayoutCommandsViewModel.cs" />
240240
<Compile Include="Interacts\IBaseLayoutCommandImplementationModel.cs" />
241241
<Compile Include="Interacts\IStatusCenterActions.cs" />
242+
<Compile Include="Helpers\SecondaryTileHelper.cs" />
242243
<Compile Include="UserControls\FilePreviews\BasicPreview.xaml.cs">
243244
<DependentUpon>BasicPreview.xaml</DependentUpon>
244245
</Compile>
@@ -619,6 +620,8 @@
619620
<Content Include="Assets\StoreLogo.scale-200_contrast-white.png" />
620621
<Content Include="Assets\StoreLogo.scale-400_contrast-black.png" />
621622
<Content Include="Assets\StoreLogo.scale-400_contrast-white.png" />
623+
<Content Include="Assets\tile-0-250x250.png" />
624+
<Content Include="Assets\tile-0-300x300.png" />
622625
<Content Include="Assets\Wide310x150Logo.scale-100_contrast-black.png" />
623626
<Content Include="Assets\Wide310x150Logo.scale-100_contrast-white.png" />
624627
<Content Include="Assets\Wide310x150Logo.scale-125_contrast-black.png" />

Files/Filesystem/ListedItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public BitmapImage FileImage
9999
}
100100
}
101101

102+
public bool IsItemPinnedToStart => App.SecondaryTileHelper.CheckFolderPinned(ItemPath);
103+
102104
private BitmapImage iconOverlay;
103105

104106
[JsonIgnore]

Files/Filesystem/StorageEnumerators/UniversalStorageEnumerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ private static async Task<ListedItem> AddFolderAsync(StorageFolder folder, Stora
176176
LoadUnknownTypeGlyph = false,
177177
FileSize = null,
178178
FileSizeBytes = 0
179-
//FolderTooltipText = tooltipString,
180179
};
181180
}
182181
return null;

Files/Filesystem/StorageEnumerators/Win32StorageEnumerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ CancellationToken cancellationToken
162162
LoadUnknownTypeGlyph = false,
163163
FileSize = null,
164164
FileSizeBytes = 0,
165-
ContainsFilesOrFolders = FolderHelpers.CheckForFilesFolders(itemPath),
166-
//FolderTooltipText = tooltipString,
165+
ContainsFilesOrFolders = FolderHelpers.CheckForFilesFolders(itemPath)
167166
};
168167
}
169168

Files/Helpers/SecondaryTileHelper.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Files.Filesystem;
2+
using Microsoft.Graphics.Canvas;
3+
using Microsoft.Graphics.Canvas.Svg;
4+
using Microsoft.Graphics.Canvas.Text;
5+
using Microsoft.Toolkit.Uwp.Notifications;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Diagnostics;
9+
using System.Linq;
10+
using System.Numerics;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
using Windows.Foundation;
14+
using Windows.Storage;
15+
using Windows.UI;
16+
using Windows.UI.Notifications;
17+
using Windows.UI.StartScreen;
18+
using TileSize = Windows.UI.StartScreen.TileSize;
19+
20+
namespace Files.Helpers
21+
{
22+
public class SecondaryTileHelper
23+
{
24+
public bool CheckFolderPinned(string path)
25+
{
26+
return SecondaryTile.Exists(GetTileID(path));
27+
}
28+
29+
/// <summary>
30+
/// Gets a tile-id to be used from a folder path
31+
/// </summary>
32+
/// <param name="path"></param>
33+
/// <returns></returns>
34+
private string GetTileID(string path)
35+
{
36+
// Remove symbols because windows doesn't like them in the ID, and will blow up
37+
return $"folder-{new string(path.Where(c => char.IsLetterOrDigit(c)).ToArray())}";
38+
}
39+
40+
public async Task<bool> TryPinFolderAsync(string path, string name)
41+
{
42+
var result = false;
43+
try
44+
{
45+
Uri Path150x150 = new Uri("ms-appx:///Assets/tile-0-300x300.png");
46+
Uri Path71x71 = new Uri("ms-appx:///Assets/tile-0-250x250.png");
47+
48+
SecondaryTile tile = new SecondaryTile(
49+
GetTileID(path),
50+
name,
51+
path,
52+
Path150x150,
53+
TileSize.Square150x150);
54+
55+
tile.VisualElements.Square71x71Logo = Path71x71;
56+
tile.VisualElements.ShowNameOnSquare150x150Logo = true;
57+
result = await tile.RequestCreateAsync();
58+
}
59+
catch (Exception e)
60+
{
61+
Debug.WriteLine(GetTileID(path));
62+
Debug.WriteLine(e.ToString());
63+
}
64+
65+
return result;
66+
}
67+
68+
public async Task<bool> UnpinFromStartAsync(string path)
69+
{
70+
return await StartScreenManager.GetDefault().TryRemoveSecondaryTileAsync(GetTileID(path));
71+
}
72+
}
73+
}

Files/Strings/en-US/Resources.resw

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,4 +1944,11 @@
19441944
<data name="FullTrustStatusTeachingTip.Title" xml:space="preserve">
19451945
<value>Administrator</value>
19461946
</data>
1947-
</root>
1947+
<data name="PinItemToStart.Text" xml:space="preserve">
1948+
<value>Pin to the Start Menu</value>
1949+
</data>
1950+
<data name="UnpinItemFromStart.Text" xml:space="preserve">
1951+
<value>Unpin from the Start Menu</value>
1952+
</data>
1953+
1954+
</root>

0 commit comments

Comments
 (0)