|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.IO;
|
8 | 8 | using System.Linq;
|
9 |
| -using System.Text; |
10 | 9 | using System.Threading.Tasks;
|
11 | 10 | using Windows.Storage;
|
12 | 11 | using Windows.UI.StartScreen;
|
13 | 12 |
|
14 |
| -namespace Files.App.ServicesImplementation; |
15 |
| -public class JumpListService : IJumpListService |
| 13 | +namespace Files.App.ServicesImplementation |
16 | 14 | {
|
17 |
| - private const string JumpListRecentGroupHeader = "ms-resource:///Resources/JumpListRecentGroupHeader"; |
18 |
| - private const string JumpListPinnedGroupHeader = "ms-resource:///Resources/JumpListPinnedGroupHeader"; |
19 |
| - |
20 |
| - public async Task AddFolderAsync(string path) |
| 15 | + public class JumpListService : IJumpListService |
21 | 16 | {
|
22 |
| - if (JumpList.IsSupported()) |
23 |
| - { |
24 |
| - var instance = await JumpList.LoadCurrentAsync(); |
25 |
| - // Disable automatic jumplist. It doesn't work. |
26 |
| - instance.SystemGroupKind = JumpListSystemGroupKind.None; |
| 17 | + private const string JumpListRecentGroupHeader = "ms-resource:///Resources/JumpListRecentGroupHeader"; |
| 18 | + private const string JumpListPinnedGroupHeader = "ms-resource:///Resources/JumpListPinnedGroupHeader"; |
27 | 19 |
|
28 |
| - // Saving to jumplist may fail randomly with error: ERROR_UNABLE_TO_REMOVE_REPLACED |
29 |
| - // In that case app should just catch the error and proceed as usual |
30 |
| - try |
| 20 | + public async Task AddFolderAsync(string path) |
| 21 | + { |
| 22 | + if (JumpList.IsSupported()) |
31 | 23 | {
|
32 |
| - if (instance is not null) |
| 24 | + var instance = await JumpList.LoadCurrentAsync(); |
| 25 | + // Disable automatic jumplist. It doesn't work. |
| 26 | + instance.SystemGroupKind = JumpListSystemGroupKind.None; |
| 27 | + |
| 28 | + // Saving to jumplist may fail randomly with error: ERROR_UNABLE_TO_REMOVE_REPLACED |
| 29 | + // In that case app should just catch the error and proceed as usual |
| 30 | + try |
33 | 31 | {
|
34 |
| - AddFolder(path, JumpListRecentGroupHeader, instance); |
35 |
| - await instance.SaveAsync(); |
| 32 | + if (instance is not null) |
| 33 | + { |
| 34 | + AddFolder(path, JumpListRecentGroupHeader, instance); |
| 35 | + await instance.SaveAsync(); |
| 36 | + } |
36 | 37 | }
|
| 38 | + catch { } |
37 | 39 | }
|
38 |
| - catch { } |
39 | 40 | }
|
40 |
| - } |
41 | 41 |
|
42 |
| - public async Task<IEnumerable<string>> GetFoldersAsync() |
43 |
| - { |
44 |
| - if (JumpList.IsSupported()) |
| 42 | + public async Task<IEnumerable<string>> GetFoldersAsync() |
45 | 43 | {
|
46 |
| - try |
| 44 | + if (JumpList.IsSupported()) |
47 | 45 | {
|
48 |
| - var instance = await JumpList.LoadCurrentAsync(); |
49 |
| - // Disable automatic jumplist. It doesn't work. |
50 |
| - instance.SystemGroupKind = JumpListSystemGroupKind.None; |
| 46 | + try |
| 47 | + { |
| 48 | + var instance = await JumpList.LoadCurrentAsync(); |
| 49 | + // Disable automatic jumplist. It doesn't work. |
| 50 | + instance.SystemGroupKind = JumpListSystemGroupKind.None; |
51 | 51 |
|
52 |
| - return instance.Items.Select(item => item.Arguments).ToList(); |
| 52 | + return instance.Items.Select(item => item.Arguments).ToList(); |
| 53 | + } |
| 54 | + catch |
| 55 | + { |
| 56 | + return Enumerable.Empty<string>(); |
| 57 | + } |
53 | 58 | }
|
54 |
| - catch |
| 59 | + else |
55 | 60 | {
|
56 | 61 | return Enumerable.Empty<string>();
|
57 | 62 | }
|
58 | 63 | }
|
59 |
| - else |
60 |
| - { |
61 |
| - return Enumerable.Empty<string>(); |
62 |
| - } |
63 |
| - } |
64 | 64 |
|
65 |
| - public async Task RefreshPinnedFoldersAsync() |
66 |
| - { |
67 |
| - try |
| 65 | + public async Task RefreshPinnedFoldersAsync() |
68 | 66 | {
|
69 |
| - if (JumpList.IsSupported()) |
| 67 | + try |
70 | 68 | {
|
71 |
| - var instance = await JumpList.LoadCurrentAsync(); |
72 |
| - // Disable automatic jumplist. It doesn't work with Files UWP. |
73 |
| - instance.SystemGroupKind = JumpListSystemGroupKind.None; |
| 69 | + if (JumpList.IsSupported()) |
| 70 | + { |
| 71 | + var instance = await JumpList.LoadCurrentAsync(); |
| 72 | + // Disable automatic jumplist. It doesn't work with Files UWP. |
| 73 | + instance.SystemGroupKind = JumpListSystemGroupKind.None; |
74 | 74 |
|
75 |
| - if (instance is null) |
76 |
| - return; |
| 75 | + if (instance is null) |
| 76 | + return; |
77 | 77 |
|
78 |
| - var itemsToRemove = instance.Items.Where(x => string.Equals(x.GroupName, JumpListPinnedGroupHeader, StringComparison.OrdinalIgnoreCase)).ToList(); |
79 |
| - itemsToRemove.ForEach(x => instance.Items.Remove(x)); |
80 |
| - App.QuickAccessManager.Model.FavoriteItems.ForEach(x => AddFolder(x, JumpListPinnedGroupHeader, instance)); |
81 |
| - await instance.SaveAsync(); |
| 78 | + var itemsToRemove = instance.Items.Where(x => string.Equals(x.GroupName, JumpListPinnedGroupHeader, StringComparison.OrdinalIgnoreCase)).ToList(); |
| 79 | + itemsToRemove.ForEach(x => instance.Items.Remove(x)); |
| 80 | + App.QuickAccessManager.Model.FavoriteItems.ForEach(x => AddFolder(x, JumpListPinnedGroupHeader, instance)); |
| 81 | + await instance.SaveAsync(); |
| 82 | + } |
82 | 83 | }
|
83 |
| - } |
84 |
| - catch { |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - public async Task RemoveFolderAsync(string path) |
89 |
| - { |
90 |
| - if (JumpList.IsSupported()) |
91 |
| - { |
92 |
| - try |
| 84 | + catch |
93 | 85 | {
|
94 |
| - var instance = await JumpList.LoadCurrentAsync(); |
95 |
| - // Disable automatic jumplist. It doesn't work. |
96 |
| - instance.SystemGroupKind = JumpListSystemGroupKind.None; |
97 |
| - |
98 |
| - var itemToRemove = instance.Items.Where(x => x.Arguments == path).Select(x => x).FirstOrDefault(); |
99 |
| - instance.Items.Remove(itemToRemove); |
100 |
| - await instance.SaveAsync(); |
101 | 86 | }
|
102 |
| - catch { } |
103 | 87 | }
|
104 |
| - } |
105 | 88 |
|
106 |
| - private void AddFolder(string path, string group, JumpList instance) |
107 |
| - { |
108 |
| - if (instance is not null) |
| 89 | + public async Task RemoveFolderAsync(string path) |
109 | 90 | {
|
110 |
| - string displayName = null; |
111 |
| - if (path.EndsWith("\\")) |
| 91 | + if (JumpList.IsSupported()) |
112 | 92 | {
|
113 |
| - // Jumplist item argument can't end with a slash so append a character that can't exist in a directory name to support listing drives. |
114 |
| - var drive = App.DrivesManager.Drives.Where(drive => drive.Path == path).FirstOrDefault(); |
115 |
| - if (drive is null) |
116 |
| - return; |
| 93 | + try |
| 94 | + { |
| 95 | + var instance = await JumpList.LoadCurrentAsync(); |
| 96 | + // Disable automatic jumplist. It doesn't work. |
| 97 | + instance.SystemGroupKind = JumpListSystemGroupKind.None; |
117 | 98 |
|
118 |
| - displayName = drive.Text; |
119 |
| - path += '?'; |
| 99 | + var itemToRemove = instance.Items.Where(x => x.Arguments == path).Select(x => x).FirstOrDefault(); |
| 100 | + instance.Items.Remove(itemToRemove); |
| 101 | + await instance.SaveAsync(); |
| 102 | + } |
| 103 | + catch { } |
120 | 104 | }
|
| 105 | + } |
121 | 106 |
|
122 |
| - if (displayName is null) |
| 107 | + private void AddFolder(string path, string group, JumpList instance) |
| 108 | + { |
| 109 | + if (instance is not null) |
123 | 110 | {
|
124 |
| - if (path.Equals(CommonPaths.DesktopPath, StringComparison.OrdinalIgnoreCase)) |
125 |
| - displayName = "ms-resource:///Resources/Desktop"; |
126 |
| - else if (path.Equals(CommonPaths.DownloadsPath, StringComparison.OrdinalIgnoreCase)) |
127 |
| - displayName = "ms-resource:///Resources/Downloads"; |
128 |
| - else if (path.Equals(CommonPaths.RecycleBinPath, StringComparison.OrdinalIgnoreCase)) |
| 111 | + string displayName = null; |
| 112 | + if (path.EndsWith("\\")) |
129 | 113 | {
|
130 |
| - var localSettings = ApplicationData.Current.LocalSettings; |
131 |
| - displayName = localSettings.Values.Get("RecycleBin_Title", "Recycle Bin"); |
| 114 | + // Jumplist item argument can't end with a slash so append a character that can't exist in a directory name to support listing drives. |
| 115 | + var drive = App.DrivesManager.Drives.Where(drive => drive.Path == path).FirstOrDefault(); |
| 116 | + if (drive is null) |
| 117 | + return; |
| 118 | + |
| 119 | + displayName = drive.Text; |
| 120 | + path += '?'; |
132 | 121 | }
|
133 |
| - else if (App.LibraryManager.TryGetLibrary(path, out LibraryLocationItem library)) |
| 122 | + |
| 123 | + if (displayName is null) |
134 | 124 | {
|
135 |
| - var libName = Path.GetFileNameWithoutExtension(library.Path); |
136 |
| - switch (libName) |
| 125 | + if (path.Equals(CommonPaths.DesktopPath, StringComparison.OrdinalIgnoreCase)) |
| 126 | + displayName = "ms-resource:///Resources/Desktop"; |
| 127 | + else if (path.Equals(CommonPaths.DownloadsPath, StringComparison.OrdinalIgnoreCase)) |
| 128 | + displayName = "ms-resource:///Resources/Downloads"; |
| 129 | + else if (path.Equals(CommonPaths.RecycleBinPath, StringComparison.OrdinalIgnoreCase)) |
137 | 130 | {
|
138 |
| - case "Documents": |
139 |
| - case "Pictures": |
140 |
| - case "Music": |
141 |
| - case "Videos": |
142 |
| - // Use localized name |
143 |
| - displayName = $"ms-resource:///Resources/{libName}"; |
144 |
| - break; |
145 |
| - |
146 |
| - default: |
147 |
| - // Use original name |
148 |
| - displayName = library.Text; |
149 |
| - break; |
| 131 | + var localSettings = ApplicationData.Current.LocalSettings; |
| 132 | + displayName = localSettings.Values.Get("RecycleBin_Title", "Recycle Bin"); |
150 | 133 | }
|
| 134 | + else if (App.LibraryManager.TryGetLibrary(path, out LibraryLocationItem library)) |
| 135 | + { |
| 136 | + var libName = Path.GetFileNameWithoutExtension(library.Path); |
| 137 | + switch (libName) |
| 138 | + { |
| 139 | + case "Documents": |
| 140 | + case "Pictures": |
| 141 | + case "Music": |
| 142 | + case "Videos": |
| 143 | + // Use localized name |
| 144 | + displayName = $"ms-resource:///Resources/{libName}"; |
| 145 | + break; |
| 146 | + |
| 147 | + default: |
| 148 | + // Use original name |
| 149 | + displayName = library.Text; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + else |
| 154 | + displayName = Path.GetFileName(path); |
151 | 155 | }
|
152 |
| - else |
153 |
| - displayName = Path.GetFileName(path); |
154 |
| - } |
155 | 156 |
|
156 |
| - var jumplistItem = JumpListItem.CreateWithArguments(path, displayName); |
157 |
| - jumplistItem.Description = jumplistItem.Arguments; |
158 |
| - jumplistItem.GroupName = group; |
159 |
| - jumplistItem.Logo = new Uri("ms-appx:///Assets/FolderIcon.png"); |
| 157 | + var jumplistItem = JumpListItem.CreateWithArguments(path, displayName); |
| 158 | + jumplistItem.Description = jumplistItem.Arguments; |
| 159 | + jumplistItem.GroupName = group; |
| 160 | + jumplistItem.Logo = new Uri("ms-appx:///Assets/FolderIcon.png"); |
160 | 161 |
|
161 |
| - if (string.Equals(group, JumpListRecentGroupHeader, StringComparison.OrdinalIgnoreCase)) |
162 |
| - { |
163 |
| - // Keep newer items at the top. |
164 |
| - instance.Items.Remove(instance.Items.FirstOrDefault(x => x.Arguments.Equals(path, StringComparison.OrdinalIgnoreCase))); |
165 |
| - instance.Items.Insert(0, jumplistItem); |
166 |
| - } |
167 |
| - else |
168 |
| - { |
169 |
| - var pinnedItemsCount = instance.Items.Where(x => x.GroupName == JumpListPinnedGroupHeader).Count(); |
170 |
| - instance.Items.Insert(pinnedItemsCount, jumplistItem); |
| 162 | + if (string.Equals(group, JumpListRecentGroupHeader, StringComparison.OrdinalIgnoreCase)) |
| 163 | + { |
| 164 | + // Keep newer items at the top. |
| 165 | + instance.Items.Remove(instance.Items.FirstOrDefault(x => x.Arguments.Equals(path, StringComparison.OrdinalIgnoreCase))); |
| 166 | + instance.Items.Insert(0, jumplistItem); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + var pinnedItemsCount = instance.Items.Where(x => x.GroupName == JumpListPinnedGroupHeader).Count(); |
| 171 | + instance.Items.Insert(pinnedItemsCount, jumplistItem); |
| 172 | + } |
171 | 173 | }
|
172 | 174 | }
|
173 | 175 | }
|
|
0 commit comments