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 _defaultHandler; private readonly IEnumerable _activationHandlers; private readonly IThemeSelectorService _themeSelectorService; private UIElement? _shell = null; public ActivationService(ActivationHandler defaultHandler, IEnumerable 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(); 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; } }