Move common things to AppCore from GuiApp
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using FileTime.App.Core.Configuration;
|
||||
using FileTime.App.Core.UserCommand;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FileTime.App.Core.Services;
|
||||
|
||||
public class KeyboardConfigurationService : IKeyboardConfigurationService
|
||||
{
|
||||
public IReadOnlyList<CommandBindingConfiguration> CommandBindings { get; }
|
||||
public IReadOnlyList<CommandBindingConfiguration> UniversalCommandBindings { get; }
|
||||
public IReadOnlyList<CommandBindingConfiguration> AllShortcut { get; }
|
||||
|
||||
public KeyboardConfigurationService(IOptions<KeyBindingConfiguration> keyBindingConfiguration)
|
||||
{
|
||||
IEnumerable<CommandBindingConfiguration> keyBindings = keyBindingConfiguration.Value.KeyBindings;
|
||||
|
||||
if (keyBindingConfiguration.Value.UseDefaultBindings)
|
||||
{
|
||||
keyBindings = keyBindings.Concat(keyBindingConfiguration.Value.DefaultKeyBindings);
|
||||
}
|
||||
|
||||
var commandBindings = new List<CommandBindingConfiguration>();
|
||||
var universalCommandBindings = new List<CommandBindingConfiguration>();
|
||||
foreach (var keyBinding in keyBindings)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(keyBinding.Command))
|
||||
{
|
||||
throw new FormatException($"No command is set in keybinding for keys '{keyBinding.KeysDisplayText}'");
|
||||
}
|
||||
else if (keyBinding.Keys.Count == 0)
|
||||
{
|
||||
throw new FormatException($"No keys set in keybinding for command '{keyBinding.Command}'.");
|
||||
}
|
||||
|
||||
if (IsUniversal(keyBinding))
|
||||
{
|
||||
universalCommandBindings.Add(keyBinding);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandBindings.Add(keyBinding);
|
||||
}
|
||||
}
|
||||
|
||||
CommandBindings = commandBindings.AsReadOnly();
|
||||
UniversalCommandBindings = universalCommandBindings.AsReadOnly();
|
||||
AllShortcut = new List<CommandBindingConfiguration>(UniversalCommandBindings.Concat(CommandBindings)).AsReadOnly();
|
||||
}
|
||||
|
||||
private static bool IsUniversal(CommandBindingConfiguration keyMapping)
|
||||
=> keyMapping.Command is
|
||||
GoUpCommand.CommandName
|
||||
or OpenSelectedCommand.CommandName
|
||||
or MoveCursorDownCommand.CommandName
|
||||
or MoveCursorDownPageCommand.CommandName
|
||||
or MoveCursorUpCommand.CommandName
|
||||
or MoveCursorUpPageCommand.CommandName
|
||||
or IdentifiableRunOrOpenCommand.CommandName;
|
||||
}
|
||||
65
src/AppCommon/FileTime.App.Core/Services/LifecycleService.cs
Normal file
65
src/AppCommon/FileTime.App.Core/Services/LifecycleService.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using FileTime.Core.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FileTime.App.Core.Services;
|
||||
|
||||
public class LifecycleService : ILifecycleService
|
||||
{
|
||||
private readonly IEnumerable<IExitHandler> _exitHandlers;
|
||||
private readonly IEnumerable<IStartupHandler> _startupHandlers;
|
||||
private readonly ILogger<LifecycleService> _logger;
|
||||
|
||||
public LifecycleService(
|
||||
IEnumerable<IStartupHandler> startupHandlers,
|
||||
IEnumerable<IExitHandler> exitHandlers,
|
||||
ILogger<LifecycleService> logger)
|
||||
{
|
||||
_exitHandlers = exitHandlers;
|
||||
_startupHandlers = startupHandlers;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InitStartupHandlersAsync()
|
||||
{
|
||||
foreach (var startupHandler in _startupHandlers)
|
||||
{
|
||||
try
|
||||
{
|
||||
await startupHandler.InitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while running startup handler {Handler}", startupHandler?.GetType().FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExitAsync()
|
||||
{
|
||||
var exitCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
var exitHandlerTasks = _exitHandlers.Select(e =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return e.ExitAsync(exitCancellation.Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while running exit handler {Handler}", e.GetType().FullName);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
await Task.WhenAll(exitHandlerTasks).TimeoutAfter(10000);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
exitCancellation.Cancel();
|
||||
}
|
||||
}
|
||||
38
src/AppCommon/FileTime.App.Core/Services/ModalService.cs
Normal file
38
src/AppCommon/FileTime.App.Core/Services/ModalService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using DynamicData;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FileTime.App.Core.Services;
|
||||
|
||||
public class ModalService : IModalService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly SourceList<IModalViewModel> _openModals = new();
|
||||
public IObservable<IChangeSet<IModalViewModel>> OpenModals { get; }
|
||||
public event EventHandler? AllModalClosed;
|
||||
|
||||
public ModalService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
OpenModals = _openModals.Connect().StartWithEmpty();
|
||||
}
|
||||
|
||||
public void OpenModal(IModalViewModel modalToOpen) => _openModals.Add(modalToOpen);
|
||||
|
||||
public void CloseModal(IModalViewModel modalToClose)
|
||||
{
|
||||
_openModals.Remove(modalToClose);
|
||||
if (_openModals.Count == 0)
|
||||
{
|
||||
AllModalClosed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public T OpenModal<T>() where T : IModalViewModel
|
||||
{
|
||||
var modal = _serviceProvider.GetRequiredService<T>();
|
||||
OpenModal(modal);
|
||||
|
||||
return modal;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user