Skip to content

Commit 974c6b2

Browse files
Feature: Improved performance when creating new files (#11114)
1 parent a49a679 commit 974c6b2

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

src/Files.App/App.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private IServiceProvider ConfigureServices()
122122
.AddSingleton<IThreadingService, ThreadingService>()
123123
.AddSingleton<ILocalizationService, LocalizationService>()
124124
.AddSingleton<ICloudDetector, CloudDetector>()
125+
.AddSingleton<IAddItemService, AddItemService>()
125126
#if SIDELOAD
126127
.AddSingleton<IUpdateService, SideloadUpdateService>()
127128
#else
@@ -180,6 +181,7 @@ private static async Task StartAppCenter()
180181
private static async Task InitializeAppComponentsAsync()
181182
{
182183
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
184+
var addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
183185
var preferencesSettingsService = userSettingsService.PreferencesSettingsService;
184186

185187
// Start off a list of tasks we need to run before we can continue startup
@@ -197,7 +199,7 @@ await Task.WhenAll(
197199
);
198200
await Task.WhenAll(
199201
JumpList.InitializeAsync(),
200-
ContextFlyoutItemHelper.CachedNewContextMenuEntries
202+
addItemService.GetNewEntriesAsync()
201203
);
202204
FileTagsHelper.UpdateTagsDb();
203205
});

src/Files.App/Dialogs/AddItemDialog.xaml.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Files.App.Extensions;
1+
using CommunityToolkit.Mvvm.DependencyInjection;
2+
using Files.Backend.Services;
23
using Files.Backend.ViewModels.Dialogs;
34
using Files.Backend.ViewModels.Dialogs.AddItemDialog;
45
using Files.Shared.Enums;
@@ -13,6 +14,8 @@ namespace Files.App.Dialogs
1314
{
1415
public sealed partial class AddItemDialog : ContentDialog, IDialog<AddItemDialogViewModel>
1516
{
17+
private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
18+
1619
public AddItemDialogViewModel ViewModel
1720
{
1821
get => (AddItemDialogViewModel)DataContext;
@@ -34,8 +37,8 @@ private void ListView_ItemClick(object sender, ItemClickEventArgs e)
3437

3538
private async void AddItemDialog_Loaded(object sender, RoutedEventArgs e)
3639
{
37-
var itemTypes = await ShellNewEntryExtensions.GetNewContextMenuEntries();
38-
await ViewModel.AddItemsToList(itemTypes); // TODO(i): This is a very cheap way of doing it, consider adding a service to retrieve the itemTypes list.
40+
var itemTypes = await addItemService.GetNewEntriesAsync();
41+
await ViewModel.AddItemsToList(itemTypes);
3942

4043
// Focus on the list view so users can use keyboard navigation
4144
AddItemsListView.Focus(FocusState.Programmatic);

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Files.App.Filesystem;
55
using Files.App.Interacts;
66
using Files.App.ViewModels;
7+
using Files.Backend.Services;
78
using Files.Backend.Services.Settings;
89
using Files.Shared;
910
using Files.Shared.Enums;
@@ -23,7 +24,7 @@ namespace Files.App.Helpers
2324
{
2425
public static class ContextFlyoutItemHelper
2526
{
26-
public static Task<List<ShellNewEntry>> CachedNewContextMenuEntries = ShellNewEntryExtensions.GetNewContextMenuEntries();
27+
private static readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
2728

2829
public static List<ContextMenuFlyoutItemViewModel> GetItemContextCommandsWithoutShellItems(CurrentInstanceViewModel currentInstanceViewModel, string workingDir, List<ListedItem> selectedItems, BaseLayoutCommandsViewModel commandsViewModel, bool shiftPressed, bool showOpenMenu, SelectedItemsPropertiesViewModel selectedItemsPropertiesViewModel)
2930
{
@@ -1122,7 +1123,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetNewItemItems(BaseLayoutCom
11221123

11231124
if (canCreateFileInPage)
11241125
{
1125-
var cachedNewContextMenuEntries = CachedNewContextMenuEntries.IsCompletedSuccessfully ? CachedNewContextMenuEntries.Result : null;
1126+
var cachedNewContextMenuEntries = addItemService.GetNewEntriesAsync().Result;
11261127
cachedNewContextMenuEntries?.ForEach(i =>
11271128
{
11281129
if (!string.IsNullOrEmpty(i.IconBase64))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Files.App.Extensions;
2+
using Files.Backend.Services;
3+
using Files.Shared;
4+
using System.Collections.Generic;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.App.ServicesImplementation
8+
{
9+
/// <inheritdoc cref="IAddItemService"/>
10+
internal sealed class AddItemService : IAddItemService
11+
{
12+
private List<ShellNewEntry> _cached;
13+
14+
public async Task<List<ShellNewEntry>> GetNewEntriesAsync()
15+
{
16+
if (_cached is null || _cached.Count == 0)
17+
_cached = await ShellNewEntryExtensions.GetNewContextMenuEntries();
18+
19+
return _cached;
20+
}
21+
}
22+
}

src/Files.App/UserControls/InnerNavigationToolbar.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Files.App.DataModels;
33
using Files.App.Helpers;
44
using Files.App.ViewModels;
5+
using Files.Backend.Services;
56
using Files.Backend.Services.Settings;
67
using Microsoft.UI.Xaml;
78
using Microsoft.UI.Xaml.Controls;
@@ -25,6 +26,8 @@ public InnerNavigationToolbar()
2526

2627
public IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
2728

29+
private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
30+
2831
public AppModel AppModel => App.AppModel;
2932

3033
public PreviewPaneViewModel PreviewPaneViewModel => App.PreviewPaneViewModel;
@@ -97,7 +100,7 @@ private void NewEmptySpace_Opening(object sender, object e)
97100
shell.ForEach(x => NewEmptySpace.Items.Remove(x));
98101
return;
99102
}
100-
var cachedNewContextMenuEntries = ContextFlyoutItemHelper.CachedNewContextMenuEntries.IsCompletedSuccessfully ? ContextFlyoutItemHelper.CachedNewContextMenuEntries.Result : null;
103+
var cachedNewContextMenuEntries = addItemService.GetNewEntriesAsync().Result;
101104
if (cachedNewContextMenuEntries is null)
102105
return;
103106
if (!NewEmptySpace.Items.Any(x => (x.Tag as string) == "CreateNewFile"))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Files.Shared;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace Files.Backend.Services
6+
{
7+
/// <summary>
8+
/// A service to retrieve available item types
9+
/// </summary>
10+
public interface IAddItemService
11+
{
12+
/// <summary>
13+
/// Gets a list of the available item types
14+
/// </summary>
15+
/// <returns>List of the available item types</returns>
16+
Task<List<ShellNewEntry>> GetNewEntriesAsync();
17+
}
18+
}

0 commit comments

Comments
 (0)