Skip to content

Commit 7b903e3

Browse files
authored
Fix: Fixed blank pages menu in startup settings (#11032)
1 parent e9806b6 commit 7b903e3

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

src/Files.App/Filesystem/RecentItems.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public async Task UpdateRecentFilesAsync()
8484
/// </summary>
8585
public async Task UpdateRecentFoldersAsync()
8686
{
87-
// enumerate with fulltrust process
88-
var enumeratedFolders = await Task.Run(() => ListRecentFolders());
87+
var enumeratedFolders = await Task.Run(ListRecentFoldersAsync); // run off the UI thread
8988
if (enumeratedFolders is not null)
9089
{
9190
lock (recentFolders)
@@ -115,36 +114,44 @@ public async Task<List<RecentItem>> ListRecentFilesAsync()
115114
/// <summary>
116115
/// Enumerate recently accessed folders via `Windows\Recent`.
117116
/// </summary>
118-
public List<RecentItem> ListRecentFolders()
117+
public async Task<List<RecentItem>> ListRecentFoldersAsync()
119118
{
120119
var recentItems = new List<RecentItem>();
121120
var excludeMask = FileAttributes.Hidden;
122121
var linkFilePaths = Directory.EnumerateFiles(CommonPaths.RecentItemsPath).Where(f => (new FileInfo(f).Attributes & excludeMask) == 0);
123122

124-
foreach (var linkFilePath in linkFilePaths)
123+
Task<RecentItem?> GetRecentItemFromLink(string linkPath)
125124
{
126-
try
125+
return Task.Run(() =>
127126
{
128-
using var link = new ShellLink(linkFilePath, LinkResolution.NoUIWithMsgPump, default, TimeSpan.FromMilliseconds(100));
127+
try
128+
{
129+
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, default, TimeSpan.FromMilliseconds(100));
129130

130-
if (!string.IsNullOrEmpty(link.TargetPath) && link.Target.IsFolder)
131+
if (!string.IsNullOrEmpty(link.TargetPath) && link.Target.IsFolder)
132+
{
133+
var shellLinkItem = ShellFolderExtensions.GetShellLinkItem(link);
134+
return new RecentItem(shellLinkItem);
135+
}
136+
}
137+
catch (FileNotFoundException)
131138
{
132-
var shellLinkItem = ShellFolderExtensions.GetShellLinkItem(link);
133-
recentItems.Add(new RecentItem(shellLinkItem));
139+
// occurs when shortcut or shortcut target is deleted and accessed (link.Target)
140+
// consequently, we shouldn't include the item as a recent item
134141
}
135-
}
136-
catch (FileNotFoundException)
137-
{
138-
// occurs when shortcut or shortcut target is deleted and accessed (link.Target)
139-
// consequently, we shouldn't include the item as a recent item
140-
}
141-
catch (Exception ex)
142-
{
143-
App.Logger.Warn(ex, ex.Message);
144-
}
142+
catch (Exception ex)
143+
{
144+
App.Logger.Warn(ex, ex.Message);
145+
}
146+
147+
return null;
148+
});
145149
}
146150

147-
return recentItems;
151+
var recentFolderTasks = linkFilePaths.Select(GetRecentItemFromLink);
152+
var result = await Task.WhenAll(recentFolderTasks);
153+
154+
return result.OfType<RecentItem>().ToList();
148155
}
149156

150157
/// <summary>

src/Files.App/ViewModels/SettingsViewModels/PreferencesViewModel.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,50 +150,61 @@ private void AddSupportedAppLanguages()
150150

151151
private async Task InitStartupSettingsRecentFoldersFlyout()
152152
{
153+
// create Browse and Recent flyout items
153154
var recentsItem = new MenuFlyoutSubItemViewModel("JumpListRecentGroupHeader".GetLocalizedResource());
154155
recentsItem.Items.Add(new MenuFlyoutItemViewModel("Home".GetLocalizedResource())
155156
{
156157
Command = AddPageCommand,
157158
CommandParameter = "Home".GetLocalizedResource(),
158159
Tooltip = "Home".GetLocalizedResource()
159160
});
161+
await PopulateRecentItems(recentsItem);
160162

161-
await App.RecentItemsManager.UpdateRecentFoldersAsync(); // ensure recent folders aren't stale since we don't update them with a watcher
162-
await PopulateRecentItems(recentsItem).ContinueWith(_ =>
163-
{
164-
AddFlyoutItemsSource = new List<IMenuFlyoutItemViewModel>() {
165-
new MenuFlyoutItemViewModel("Browse".GetLocalizedResource()) { Command = AddPageCommand },
166-
recentsItem,
167-
}.AsReadOnly();
168-
}, TaskScheduler.FromCurrentSynchronizationContext());
163+
// ensure recent folders aren't stale since we don't update them with a watcher
164+
// then update the items source again to actually include those items
165+
await App.RecentItemsManager.UpdateRecentFoldersAsync();
166+
await PopulateRecentItems(recentsItem);
169167
}
170168

171169
private Task PopulateRecentItems(MenuFlyoutSubItemViewModel menu)
172170
{
173171
try
174172
{
175173
var recentFolders = App.RecentItemsManager.RecentFolders;
176-
177-
// add separator
178-
if (recentFolders.Any())
174+
var currentFolderMenus = menu.Items
175+
.OfType<MenuFlyoutItemViewModel>()
176+
.Where(m => m.Text != "Home".GetLocalizedResource())
177+
.Select(m => m.Text)
178+
.ToHashSet();
179+
180+
// add separator if we need one and one wasn't added already
181+
if (recentFolders.Any() && !currentFolderMenus.Any())
179182
menu.Items.Add(new MenuFlyoutSeparatorViewModel());
180183

181-
foreach (var recentFolder in recentFolders)
184+
foreach (var folder in recentFolders)
182185
{
183-
var menuItem = new MenuFlyoutItemViewModel(recentFolder.Name)
186+
if (currentFolderMenus.Contains(folder.Name))
187+
continue;
188+
189+
menu.Items.Add(new MenuFlyoutItemViewModel(folder.Name)
184190
{
185191
Command = AddPageCommand,
186-
CommandParameter = recentFolder.RecentPath,
187-
Tooltip = recentFolder.RecentPath
188-
};
189-
menu.Items.Add(menuItem);
192+
CommandParameter = folder.RecentPath,
193+
Tooltip = folder.RecentPath
194+
});
190195
}
191196
}
192197
catch (Exception ex)
193198
{
194199
App.Logger.Info(ex, "Could not fetch recent items");
195200
}
196201

202+
// update items source
203+
AddFlyoutItemsSource = new List<IMenuFlyoutItemViewModel>() {
204+
new MenuFlyoutItemViewModel("Browse".GetLocalizedResource()) { Command = AddPageCommand },
205+
menu,
206+
}.AsReadOnly();
207+
197208
return Task.CompletedTask;
198209
}
199210

src/Files.App/Views/SettingsPages/Preferences.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
IsOpen="False">
138138
<AppBarButton Icon="Add" Label="{helpers:ResourceString Name=Add}">
139139
<AppBarButton.Flyout>
140-
<MenuFlyout helpers:MenuFlyoutHelper.ItemsSource="{x:Bind ViewModel.AddFlyoutItemsSource}" Placement="Bottom" />
140+
<MenuFlyout helpers:MenuFlyoutHelper.ItemsSource="{x:Bind ViewModel.AddFlyoutItemsSource, Mode=OneWay}" Placement="Bottom" />
141141
</AppBarButton.Flyout>
142142
</AppBarButton>
143143
<AppBarButton

0 commit comments

Comments
 (0)