| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml; |
| 4 | using WslSettings.Contracts.Services; |
| 5 | |
| 6 | namespace WslSettings.Activation; |
| 7 | |
| 8 | // Extend this class to implement new ActivationHandlers. See DefaultActivationHandler for an example. |
| 9 | // https://github.com/microsoft/TemplateStudio/blob/main/docs/WinUI/activation.md |
| 10 | public abstract class ActivationHandler<T> : IActivationHandler |
| 11 | where T : class |
| 12 | { |
| 13 | protected readonly INavigationService _navigationService; |
| 14 | |
| 15 | public ActivationHandler(INavigationService navigationService) |
| 16 | { |
| 17 | _navigationService = navigationService; |
| 18 | } |
| 19 | |
| 20 | // Override this method to add the logic for whether to handle the activation. |
| 21 | protected virtual bool CanHandleInternal(T args) => true; |
| 22 | |
| 23 | // Override this method to add the logic for your activation handler. |
| 24 | protected abstract Task HandleInternalAsync(T args); |
| 25 | |
| 26 | protected void Navigate(IWindowService.WindowId windowId, string pageKey, LaunchActivatedEventArgs args) |
| 27 | { |
| 28 | WindowEx window = App.GetService<IWindowService>().CreateOrGetWindow(windowId); |
| 29 | |
| 30 | _navigationService.NavigateTo(pageKey, args.Arguments); |
| 31 | |
| 32 | window.Activate(); |
| 33 | } |
| 34 | |
| 35 | public bool CanHandle(object args) => args is T && CanHandleInternal((args as T)!); |
| 36 | |
| 37 | public async Task HandleAsync(object args) => await HandleInternalAsync((args as T)!); |
| 38 | } |