WIn_RustTools/RustTools/Services/ActivationService.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2024-07-14 11:24:10 +00:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using RustTools.Activation;
using RustTools.Contracts.Services;
using RustTools.Views;
namespace RustTools.Services;
public class ActivationService : IActivationService
{
private readonly ActivationHandler<LaunchActivatedEventArgs> _defaultHandler;
private readonly IEnumerable<IActivationHandler> _activationHandlers;
private readonly IThemeSelectorService _themeSelectorService;
private UIElement? _shell = null;
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService)
{
_defaultHandler = defaultHandler;
_activationHandlers = activationHandlers;
_themeSelectorService = themeSelectorService;
}
public async Task ActivateAsync(object activationArgs)
{
// Execute tasks before activation.
await InitializeAsync();
// Set the MainWindow Content.
if (App.MainWindow.Content == null)
{
_shell = App.GetService<ShellPage>();
App.MainWindow.Content = _shell ?? new Frame();
}
// Handle activation via ActivationHandlers.
await HandleActivationAsync(activationArgs);
// Activate the MainWindow.
App.MainWindow.Activate();
// Execute tasks after activation.
await StartupAsync();
}
private async Task HandleActivationAsync(object activationArgs)
{
var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle(activationArgs));
if (activationHandler != null)
{
await activationHandler.HandleAsync(activationArgs);
}
if (_defaultHandler.CanHandle(activationArgs))
{
await _defaultHandler.HandleAsync(activationArgs);
}
}
private async Task InitializeAsync()
{
await _themeSelectorService.InitializeAsync().ConfigureAwait(false);
await Task.CompletedTask;
}
private async Task StartupAsync()
{
await _themeSelectorService.SetRequestedThemeAsync();
await Task.CompletedTask;
}
}