Gui startup optimizations

This commit is contained in:
2023-08-30 09:25:02 +02:00
parent 8292a66eb1
commit a398fd27b7
5 changed files with 58 additions and 39 deletions

View File

@@ -70,7 +70,8 @@ public class TabPersistenceService : ITabPersistenceService
foreach (var (requestedTabNumber, nativePath) in _tabsToOpen.TabsToOpen)
{
if (await _timelessContentProvider.GetItemByNativePathAsync(nativePath) is not IContainer container) continue;
if (await _timelessContentProvider.GetItemByNativePathAsync(nativePath) is not IContainer container)
continue;
containers.Add((requestedTabNumber, container));
}
@@ -111,14 +112,16 @@ public class TabPersistenceService : ITabPersistenceService
return Task.CompletedTask;
}
private async Task<IEnumerable<ITabViewModel>> LoadStatesAsync(bool createEmptyIfNecessary, CancellationToken token = default)
private async Task<IEnumerable<ITabViewModel>> LoadStatesAsync(
bool createEmptyIfNecessary,
CancellationToken token = default)
{
if (!File.Exists(_settingsPath) || !_tabPersistenceSettings.LoadState)
{
if (createEmptyIfNecessary)
{
var tabViewModel = await CreateEmptyTab();
return new[] {tabViewModel};
return new[] { tabViewModel };
}
return Enumerable.Empty<ITabViewModel>();
@@ -142,7 +145,7 @@ public class TabPersistenceService : ITabPersistenceService
if (createEmptyIfNecessary)
{
var tabViewModel = await CreateEmptyTab();
return new[] {tabViewModel};
return new[] { tabViewModel };
}
return Enumerable.Empty<ITabViewModel>();
@@ -162,7 +165,8 @@ public class TabPersistenceService : ITabPersistenceService
// ignored
}
var tab = await _serviceProvider.GetAsyncInitableResolver<IContainer>(currentDirectory ?? _localContentProvider)
var tab = await _serviceProvider
.GetAsyncInitableResolver<IContainer>(currentDirectory ?? _localContentProvider)
.GetRequiredServiceAsync<ITab>();
var tabViewModel = _serviceProvider.GetInitableResolver(tab, 1).GetRequiredService<ITabViewModel>();
@@ -265,8 +269,7 @@ public class TabPersistenceService : ITabPersistenceService
foreach (var tab in _appState.Tabs)
{
var currentLocation = tab.CurrentLocation.Value;
if (currentLocation is null) continue;
var path = currentLocation.FullName!.Path;
if (currentLocation?.FullName?.Path is not { } path) continue;
if (currentLocation.GetExtension<RealContainerProviderExtension>()?.RealContainer() is { } realPath)
{

View File

@@ -85,7 +85,11 @@ public partial class MainWindowViewModel : IMainWindowViewModel
_modalService.AllModalClosed += (_, _) => FocusDefaultElement?.Invoke();
_instanceMessageHandler.ShowWindow += () => ShowWindow?.Invoke();
Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
Task.Run(async () =>
{
await Task.Delay(100);
await _lifecycleService.InitStartupHandlersAsync();
});
}
public void ProcessKeyDown(KeyEventArgs e)

View File

@@ -51,39 +51,46 @@ public partial class MainWindow : Window, IUiAccessor
_initializer = initializer;
}
private void OnWindowOpened(object sender, EventArgs e)
private async void OnWindowOpened(object sender, EventArgs e)
{
if (DataContext is not MainWindowViewModel && !Design.IsDesignMode)
try
{
_initializer?.Invoke();
_logger = DI.ServiceProvider.GetService<ILogger<MainWindow>>();
_modalService = DI.ServiceProvider.GetRequiredService<IModalService>();
DI.ServiceProvider.GetRequiredService<SystemClipboardService>().UiAccessor = this;
ReadInputContainer.PropertyChanged += ReadInputContainerOnPropertyChanged;
_logger?.LogInformation(
$"{nameof(MainWindow)} opened, starting {nameof(MainWindowViewModel)} initialization...");
try
if (DataContext is not MainWindowViewModel && !Design.IsDesignMode)
{
var viewModel = DI.ServiceProvider.GetRequiredService<MainWindowViewModel>();
viewModel.FocusDefaultElement = () => Focus();
viewModel.ShowWindow = Activate;
ViewModel = viewModel;
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error initializing {ViewModel}", nameof(MainWindowViewModel));
if (DataContext is IMainWindowViewModelBase mainWindowViewModelBase)
await Task.Delay(100);
_initializer?.Invoke();
_logger = DI.ServiceProvider.GetService<ILogger<MainWindow>>();
_modalService = DI.ServiceProvider.GetRequiredService<IModalService>();
DI.ServiceProvider.GetRequiredService<SystemClipboardService>().UiAccessor = this;
ReadInputContainer.PropertyChanged += ReadInputContainerOnPropertyChanged;
_logger?.LogInformation(
$"{nameof(MainWindow)} opened, starting {nameof(MainWindowViewModel)} initialization...");
try
{
mainWindowViewModelBase.FatalError.SetValueSafe(
$"Error initializing {nameof(MainWindowViewModel)}: " + ex.Message
);
var viewModel = DI.ServiceProvider.GetRequiredService<MainWindowViewModel>();
viewModel.FocusDefaultElement = () => Focus();
viewModel.ShowWindow = Activate;
ViewModel = viewModel;
}
catch (Exception ex)
{
_logger?.LogError(ex, "Error initializing {ViewModel}", nameof(MainWindowViewModel));
if (DataContext is IMainWindowViewModelBase mainWindowViewModelBase)
{
mainWindowViewModelBase.FatalError.SetValueSafe(
$"Error initializing {nameof(MainWindowViewModel)}: " + ex.Message
);
}
}
}
}
catch
{
}
}
private void OnKeyDown(object sender, KeyEventArgs e)

View File

@@ -16,6 +16,7 @@ using FileTime.Server;
using FileTime.Tools.Compression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
namespace FileTime.GuiApp;
@@ -23,6 +24,7 @@ public class Application : Avalonia.Application
{
private static void InitializeApp()
{
Log.Logger.Information("App initialization starting...");
var configuration = Startup.CreateConfiguration();
var services = DependencyInjection
.RegisterDefaultServices(configuration: configuration)
@@ -62,6 +64,7 @@ public class Application : Avalonia.Application
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Log.Logger.Information("Creating MainWindow instance...");
desktop.MainWindow = new MainWindow(InitializeApp)
{
DataContext = new MainWindowLoadingViewModel(),

View File

@@ -22,7 +22,7 @@ public static class Program
public static string EnvironmentName { get; private set; } = null!;
private static ILogger _logger = null!;
internal static List<string> DirectoriesToOpen { get; } = new();
private static void InitLogging()
@@ -41,7 +41,8 @@ public static class Program
.WriteTo.File(
Path.Combine(logFolder, "appLog.log"),
fileSizeLimitBytes: 10 * 1024 * 1024,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true)
.CreateBootstrapLogger();
@@ -128,15 +129,16 @@ public static class Program
{
Log.CloseAndFlush();
}
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<Application>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToTrace();
#if DEBUG
.LogToTrace()
#endif
;
private static void OnTaskSchedulerUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
=> HandleUnhandledException(sender, e.Exception);