Skip to content

Commit 1b49cca

Browse files
committed
Initial Support for Pinning Folders to Sidebar
1 parent b869d78 commit 1b49cca

File tree

4 files changed

+206
-2
lines changed

4 files changed

+206
-2
lines changed

Files UWP/GenericFileBrowser.xaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
<Style TargetType="controls:DataGridRow">
210210
<Setter Property="ContextFlyout">
211211
<Setter.Value>
212-
<MenuFlyout x:Name="RightClickContextMenu" MenuFlyoutPresenterStyle="{StaticResource MenuFlyoutFluentThemeResources}">
212+
<MenuFlyout Opened="RightClickContextMenu_Opened" x:Name="RightClickContextMenu" MenuFlyoutPresenterStyle="{StaticResource MenuFlyoutFluentThemeResources}">
213213
<MenuFlyout.Items>
214214
<MenuFlyoutItem Text="Open With..." Name="OpenItem">
215215
<MenuFlyoutItem.Icon>
@@ -262,6 +262,13 @@
262262
</MenuFlyoutItem>
263263

264264
<MenuFlyoutSeparator/>
265+
<MenuFlyoutItem Text="Pin to sidebar" Name="SidebarPinItem">
266+
<MenuFlyoutItem.Icon>
267+
<SymbolIcon Symbol="Pin"/>
268+
</MenuFlyoutItem.Icon>
269+
270+
</MenuFlyoutItem>
271+
265272
<MenuFlyoutItem Text="Properties" Name="PropertiesItem">
266273
<MenuFlyoutItem.Icon>
267274
<FontIcon Glyph="&#xE946;"/>

Files UWP/GenericFileBrowser.xaml.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
9595
RenameItem.Click += instanceInteraction.RenameItem_Click;
9696
CutItem.Click += instanceInteraction.CutItem_Click;
9797
CopyItem.Click += instanceInteraction.CopyItem_ClickAsync;
98+
SidebarPinItem.Click += instanceInteraction.PinItem_Click;
9899
AllView.RightTapped += instanceInteraction.AllView_RightTapped;
99100
AllView.DoubleTapped += instanceInteraction.List_ItemClick;
100101

@@ -278,6 +279,19 @@ private void AllView_DragLeave(object sender, DragEventArgs e)
278279
{
279280

280281
}
282+
283+
private void RightClickContextMenu_Opened(object sender, object e)
284+
{
285+
var selectedDataItem = instanceViewModel.FilesAndFolders[AllView.SelectedIndex];
286+
if (selectedDataItem.FileType != "Folder" || AllView.SelectedItems.Count > 1)
287+
{
288+
SidebarPinItem.IsEnabled = false;
289+
}
290+
else if (selectedDataItem.FileType == "Folder")
291+
{
292+
SidebarPinItem.IsEnabled = true;
293+
}
294+
}
281295
}
282296

283297
public class EmptyFolderTextState : INotifyPropertyChanged

Files UWP/Interacts/Interaction.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,36 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
246246

247247
}
248248

249+
public async void PinItem_Click(object sender, RoutedEventArgs e)
250+
{
251+
if (typeof(PageType) == typeof(GenericFileBrowser))
252+
{
253+
var selectedDataItem = (type as GenericFileBrowser).instanceViewModel.FilesAndFolders[(type as GenericFileBrowser).AllView.SelectedIndex];
254+
StorageFolder cacheFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
255+
256+
try
257+
{
258+
List<string> items = new List<string>();
259+
items.Add(selectedDataItem.FilePath);
260+
var ListFile = await cacheFolder.GetFileAsync("PinnedItems.txt");
261+
await FileIO.AppendLinesAsync(ListFile, items);
262+
}
263+
catch (FileNotFoundException)
264+
{
265+
List<string> items = new List<string>();
266+
items.Add(selectedDataItem.FilePath);
267+
var createdListFile = await cacheFolder.CreateFileAsync("PinnedItems.txt");
268+
await FileIO.WriteLinesAsync(createdListFile, items);
269+
}
270+
}
271+
else if(typeof(PageType) == typeof(PhotoAlbum))
272+
{
273+
274+
}
275+
var CurrentInstance = ItemViewModel<PhotoAlbum>.GetCurrentSelectedTabInstance<ProHome>();
276+
CurrentInstance.PopulatePinnedSidebarItems();
277+
}
278+
249279
public void GetPath_Click(object sender, RoutedEventArgs e)
250280
{
251281
Clipboard.Clear();

Files UWP/ProHome.xaml.cs

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Files.Navigation;
44
using Microsoft.UI.Xaml.Controls;
55
using System;
6+
using System.Collections.Generic;
67
using System.Collections.ObjectModel;
78
using System.Diagnostics;
89
using System.IO;
@@ -79,6 +80,8 @@ public ProHome()
7980
accessiblePasteButton = PasteButton;
8081
LocationsList.SelectedIndex = 0;
8182
RibbonTeachingTip = RibbonTip;
83+
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
84+
PopulatePinnedSidebarItems();
8285
PopulateNavViewWithExternalDrives();
8386
BackButton.Click += NavigationActions.Back_Click;
8487
ForwardButton.Click += NavigationActions.Forward_Click;
@@ -90,7 +93,6 @@ public ProHome()
9093
}
9194

9295
// Overwrite paths for common locations if Custom Locations setting is enabled
93-
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
9496
if (localSettings.Values["customLocationsSetting"].Equals(true))
9597
{
9698
DesktopPath = localSettings.Values["DesktopLocation"].ToString();
@@ -103,6 +105,127 @@ public ProHome()
103105

104106
}
105107

108+
List<string> LinesToRemoveFromFile = new List<string>();
109+
110+
public async void PopulatePinnedSidebarItems()
111+
{
112+
StorageFolder cacheFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
113+
var ListFile = await cacheFolder.GetFileAsync("PinnedItems.txt");
114+
if(ListFile != null)
115+
{
116+
var ListFileLines = await FileIO.ReadLinesAsync(ListFile);
117+
foreach (string s in ListFileLines)
118+
{
119+
try
120+
{
121+
StorageFolder fol = await StorageFolder.GetFolderFromPathAsync(s);
122+
var name = fol.DisplayName;
123+
var content = name;
124+
var icon = "\uE8B7";
125+
126+
FontFamily fontFamily = new FontFamily("Segoe MDL2 Assets");
127+
FontIcon fontIcon = new FontIcon()
128+
{
129+
FontSize = 16,
130+
FontFamily = fontFamily,
131+
Glyph = icon
132+
};
133+
134+
TextBlock text = new TextBlock()
135+
{
136+
Text = content,
137+
FontSize = 12
138+
};
139+
140+
StackPanel stackPanel = new StackPanel()
141+
{
142+
Spacing = 15,
143+
Orientation = Orientation.Horizontal
144+
};
145+
146+
stackPanel.Children.Add(fontIcon);
147+
stackPanel.Children.Add(text);
148+
MenuFlyout flyout = new MenuFlyout();
149+
MenuFlyoutItem flyoutItem = new MenuFlyoutItem()
150+
{
151+
Text = "Unpin item"
152+
};
153+
flyoutItem.Click += FlyoutItem_Click;
154+
flyout.Items.Add(flyoutItem);
155+
bool isDuplicate = false;
156+
foreach (ListViewItem lvi in LocationsList.Items)
157+
{
158+
if (lvi.Tag.ToString() == s)
159+
{
160+
isDuplicate = true;
161+
162+
}
163+
}
164+
165+
if (!isDuplicate)
166+
{
167+
ListViewItem newItem = new ListViewItem();
168+
newItem.Content = stackPanel;
169+
newItem.Tag = s;
170+
newItem.ContextFlyout = flyout;
171+
newItem.IsRightTapEnabled = true;
172+
newItem.RightTapped += NewItem_RightTapped;
173+
LocationsList.Items.Add(newItem);
174+
}
175+
}
176+
catch (UnauthorizedAccessException e)
177+
{
178+
Debug.WriteLine(e.Message);
179+
}
180+
catch (FileNotFoundException e)
181+
{
182+
Debug.WriteLine("Pinned item was deleted and will be removed from the file lines list soon: " + e.Message);
183+
LinesToRemoveFromFile.Add(s);
184+
}
185+
catch (System.Runtime.InteropServices.COMException e)
186+
{
187+
Debug.WriteLine("Pinned item's drive was ejected and will be removed from the file lines list soon: " + e.Message);
188+
LinesToRemoveFromFile.Add(s);
189+
}
190+
}
191+
192+
foreach (string path in LinesToRemoveFromFile)
193+
{
194+
ListFileLines.Remove(path);
195+
}
196+
await FileIO.WriteLinesAsync(ListFile, ListFileLines);
197+
ListFileLines = await FileIO.ReadLinesAsync(ListFile);
198+
199+
// Remove unpinned items from sidebar
200+
foreach (ListViewItem location in LocationsList.Items)
201+
{
202+
if (!(location.Tag.ToString() == "Favorites" || location.Tag.ToString() == "Desktop" || location.Tag.ToString() == "Documents" || location.Tag.ToString() == "Downloads" || location.Tag.ToString() == "Pictures" || location.Tag.ToString() == "Music" || location.Tag.ToString() == "Videos"))
203+
{
204+
205+
if (!ListFileLines.Contains(location.Tag.ToString()))
206+
{
207+
if(LocationsList.SelectedItem == location)
208+
{
209+
LocationsList.SelectedIndex = 0;
210+
accessibleContentFrame.Navigate(typeof(YourHome));
211+
PathText.Text = "Favorites";
212+
LayoutItems.isEnabled = false;
213+
}
214+
LocationsList.Items.Remove(location);
215+
}
216+
}
217+
}
218+
LinesToRemoveFromFile.Clear();
219+
}
220+
}
221+
222+
ListViewItem rightClickedItem;
223+
224+
private void NewItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
225+
{
226+
rightClickedItem = sender as ListViewItem;
227+
}
228+
106229
public async void PopulateNavViewWithExternalDrives()
107230
{
108231
var knownRemDevices = new ObservableCollection<string>();
@@ -168,6 +291,22 @@ public async void PopulateNavViewWithExternalDrives()
168291
});
169292
}
170293

294+
private async void FlyoutItem_Click(object sender, RoutedEventArgs e)
295+
{
296+
StorageFolder cacheFolder = Windows.Storage.ApplicationData.Current.LocalCacheFolder;
297+
var ListFile = await cacheFolder.GetFileAsync("PinnedItems.txt");
298+
var ListFileLines = await FileIO.ReadLinesAsync(ListFile);
299+
foreach (string s in ListFileLines)
300+
{
301+
if(s == rightClickedItem.Tag.ToString())
302+
{
303+
LinesToRemoveFromFile.Add(s);
304+
PopulatePinnedSidebarItems();
305+
return;
306+
}
307+
}
308+
}
309+
171310
private void NameDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
172311
{
173312
inputForRename = inputFromRename.Text;
@@ -468,6 +607,20 @@ private void LocationsList_ItemClick(object sender, ItemClickEventArgs e)
468607
}
469608
LayoutItems.isEnabled = true;
470609
}
610+
else
611+
{
612+
ItemDisplayFrame.Navigate(typeof(GenericFileBrowser), clickedItem.Tag);
613+
PathText.Text = clickedItem.Tag.ToString();
614+
instance = (this.accessibleContentFrame.Content as GenericFileBrowser).instanceViewModel;
615+
HomeItems.isEnabled = false;
616+
ShareItems.isEnabled = false;
617+
if (DrivesList.SelectedItem != null)
618+
{
619+
DrivesList.SelectedItem = null;
620+
LayoutItems.isEnabled = false;
621+
}
622+
LayoutItems.isEnabled = true;
623+
}
471624

472625
}
473626

0 commit comments

Comments
 (0)