Skip to content

Feature: Improved performance when creating new files #11114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private IServiceProvider ConfigureServices()
.AddSingleton<IThreadingService, ThreadingService>()
.AddSingleton<ILocalizationService, LocalizationService>()
.AddSingleton<ICloudDetector, CloudDetector>()
.AddSingleton<IAddItemService, AddItemService>()
#if SIDELOAD
.AddSingleton<IUpdateService, SideloadUpdateService>()
#else
Expand Down Expand Up @@ -180,6 +181,7 @@ private static async Task StartAppCenter()
private static async Task InitializeAppComponentsAsync()
{
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
var addItemService = Ioc.Default.GetRequiredService<IAddItemService>();
var preferencesSettingsService = userSettingsService.PreferencesSettingsService;

// Start off a list of tasks we need to run before we can continue startup
Expand All @@ -197,7 +199,7 @@ await Task.WhenAll(
);
await Task.WhenAll(
JumpList.InitializeAsync(),
ContextFlyoutItemHelper.CachedNewContextMenuEntries
addItemService.GetNewEntriesAsync()
);
FileTagsHelper.UpdateTagsDb();
});
Expand Down
9 changes: 6 additions & 3 deletions src/Files.App/Dialogs/AddItemDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Files.App.Extensions;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.Backend.Services;
using Files.Backend.ViewModels.Dialogs;
using Files.Backend.ViewModels.Dialogs.AddItemDialog;
using Files.Shared.Enums;
Expand All @@ -13,6 +14,8 @@ namespace Files.App.Dialogs
{
public sealed partial class AddItemDialog : ContentDialog, IDialog<AddItemDialogViewModel>
{
private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();

public AddItemDialogViewModel ViewModel
{
get => (AddItemDialogViewModel)DataContext;
Expand All @@ -34,8 +37,8 @@ private void ListView_ItemClick(object sender, ItemClickEventArgs e)

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

// Focus on the list view so users can use keyboard navigation
AddItemsListView.Focus(FocusState.Programmatic);
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Files.App.Filesystem;
using Files.App.Interacts;
using Files.App.ViewModels;
using Files.Backend.Services;
using Files.Backend.Services.Settings;
using Files.Shared;
using Files.Shared.Enums;
Expand All @@ -23,7 +24,7 @@ namespace Files.App.Helpers
{
public static class ContextFlyoutItemHelper
{
public static Task<List<ShellNewEntry>> CachedNewContextMenuEntries = ShellNewEntryExtensions.GetNewContextMenuEntries();
private static readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();

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

if (canCreateFileInPage)
{
var cachedNewContextMenuEntries = CachedNewContextMenuEntries.IsCompletedSuccessfully ? CachedNewContextMenuEntries.Result : null;
var cachedNewContextMenuEntries = addItemService.GetNewEntriesAsync().Result;
cachedNewContextMenuEntries?.ForEach(i =>
{
if (!string.IsNullOrEmpty(i.IconBase64))
Expand Down
22 changes: 22 additions & 0 deletions src/Files.App/ServicesImplementation/AddItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Files.App.Extensions;
using Files.Backend.Services;
using Files.Shared;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Files.App.ServicesImplementation
{
/// <inheritdoc cref="IAddItemService"/>
internal sealed class AddItemService : IAddItemService
{
private List<ShellNewEntry> _cached;

public async Task<List<ShellNewEntry>> GetNewEntriesAsync()
{
if (_cached is null || _cached.Count == 0)
_cached = await ShellNewEntryExtensions.GetNewContextMenuEntries();

return _cached;
}
}
}
5 changes: 4 additions & 1 deletion src/Files.App/UserControls/InnerNavigationToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Files.App.DataModels;
using Files.App.Helpers;
using Files.App.ViewModels;
using Files.Backend.Services;
using Files.Backend.Services.Settings;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -25,6 +26,8 @@ public InnerNavigationToolbar()

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

private readonly IAddItemService addItemService = Ioc.Default.GetRequiredService<IAddItemService>();

public AppModel AppModel => App.AppModel;

public PreviewPaneViewModel PreviewPaneViewModel => App.PreviewPaneViewModel;
Expand Down Expand Up @@ -97,7 +100,7 @@ private void NewEmptySpace_Opening(object sender, object e)
shell.ForEach(x => NewEmptySpace.Items.Remove(x));
return;
}
var cachedNewContextMenuEntries = ContextFlyoutItemHelper.CachedNewContextMenuEntries.IsCompletedSuccessfully ? ContextFlyoutItemHelper.CachedNewContextMenuEntries.Result : null;
var cachedNewContextMenuEntries = addItemService.GetNewEntriesAsync().Result;
if (cachedNewContextMenuEntries is null)
return;
if (!NewEmptySpace.Items.Any(x => (x.Tag as string) == "CreateNewFile"))
Expand Down
18 changes: 18 additions & 0 deletions src/Files.Backend/Services/IAddItemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Files.Shared;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Files.Backend.Services
{
/// <summary>
/// A service to retrieve available item types
/// </summary>
public interface IAddItemService
{
/// <summary>
/// Gets a list of the available item types
/// </summary>
/// <returns>List of the available item types</returns>
Task<List<ShellNewEntry>> GetNewEntriesAsync();
}
}