| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml; |
| 4 | using Microsoft.Windows.AppLifecycle; |
| 5 | using System.Runtime.CompilerServices; |
| 6 | using WslSettings.Contracts.Services; |
| 7 | using WslSettings.ViewModels.OOBE; |
| 8 | using WslSettings.ViewModels.Settings; |
| 9 | |
| 10 | namespace WslSettings.Activation; |
| 11 | |
| 12 | public class ProtocolActivationHandler : ActivationHandler<LaunchActivatedEventArgs> |
| 13 | { |
| 14 | public ProtocolActivationHandler(INavigationService navigationService) : base(navigationService) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | protected override bool CanHandleInternal(LaunchActivatedEventArgs args) |
| 19 | { |
| 20 | // None of the ActivationHandlers has handled the activation. |
| 21 | return AppInstance.GetCurrent().GetActivatedEventArgs().Kind == ExtendedActivationKind.Protocol && |
| 22 | _navigationService.Frame?.Content == null; |
| 23 | } |
| 24 | |
| 25 | private static IWindowService.WindowId ResolveWindowId(Uri uri) |
| 26 | { |
| 27 | IWindowService.WindowId windowId = IWindowService.WindowId.MainWindow; |
| 28 | switch (uri.Host.ToLower()) |
| 29 | { |
| 30 | case "settings": |
| 31 | windowId = IWindowService.WindowId.MainWindow; |
| 32 | break; |
| 33 | case "oobe": |
| 34 | windowId = IWindowService.WindowId.OOBEWindow; |
| 35 | break; |
| 36 | default: |
| 37 | windowId = IWindowService.WindowId.MainWindow; |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | return windowId; |
| 42 | } |
| 43 | |
| 44 | private static string ResolvePageKey(Uri uri, IWindowService.WindowId windowId) |
| 45 | { |
| 46 | string pageName = uri.LocalPath.ToLower().Trim('/'); |
| 47 | string pageKey = typeof(MemAndProcViewModel).FullName!; |
| 48 | switch (windowId) |
| 49 | { |
| 50 | case IWindowService.WindowId.MainWindow: |
| 51 | switch (pageName) |
| 52 | { |
| 53 | case "memandproc": |
| 54 | pageKey = typeof(MemAndProcViewModel).FullName!; |
| 55 | break; |
| 56 | case "filesystem": |
| 57 | pageKey = typeof(FileSystemViewModel).FullName!; |
| 58 | break; |
| 59 | case "networking": |
| 60 | pageKey = typeof(NetworkingViewModel).FullName!; |
| 61 | break; |
| 62 | case "optfeatures": |
| 63 | pageKey = typeof(OptionalFeaturesViewModel).FullName!; |
| 64 | break; |
| 65 | case "developer": |
| 66 | pageKey = typeof(DeveloperViewModel).FullName!; |
| 67 | break; |
| 68 | case "about": |
| 69 | pageKey = typeof(AboutViewModel).FullName!; |
| 70 | break; |
| 71 | default: |
| 72 | pageKey = typeof(MemAndProcViewModel).FullName!; |
| 73 | break; |
| 74 | } |
| 75 | break; |
| 76 | case IWindowService.WindowId.OOBEWindow: |
| 77 | switch (pageName) |
| 78 | { |
| 79 | case "general": |
| 80 | pageKey = typeof(GeneralViewModel).FullName!; |
| 81 | break; |
| 82 | case "crossosfiles": |
| 83 | pageKey = typeof(WorkingAcrossFileSystemsViewModel).FullName!; |
| 84 | break; |
| 85 | case "guiapps": |
| 86 | pageKey = typeof(GUIAppsViewModel).FullName!; |
| 87 | break; |
| 88 | case "vscodeint": |
| 89 | pageKey = typeof(VSCodeIntegrationViewModel).FullName!; |
| 90 | break; |
| 91 | case "vsint": |
| 92 | pageKey = typeof(VSIntegrationViewModel).FullName!; |
| 93 | break; |
| 94 | case "gpuaccel": |
| 95 | pageKey = typeof(GPUAccelerationViewModel).FullName!; |
| 96 | break; |
| 97 | case "dockerint": |
| 98 | pageKey = typeof(DockerDesktopIntegrationViewModel).FullName!; |
| 99 | break; |
| 100 | case "networkingint": |
| 101 | pageKey = typeof(NetworkingIntegrationViewModel).FullName!; |
| 102 | break; |
| 103 | case "distromgmt": |
| 104 | pageKey = typeof(DistroManagementViewModel).FullName!; |
| 105 | break; |
| 106 | default: |
| 107 | pageKey = typeof(GeneralViewModel).FullName!; |
| 108 | break; |
| 109 | } |
| 110 | break; |
| 111 | default: |
| 112 | pageKey = typeof(MemAndProcViewModel).FullName!; |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | return pageKey; |
| 117 | } |
| 118 | |
| 119 | protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args) |
| 120 | { |
| 121 | Windows.ApplicationModel.Activation.IProtocolActivatedEventArgs eventArgs = |
| 122 | (Windows.ApplicationModel.Activation.IProtocolActivatedEventArgs)AppInstance.GetCurrent().GetActivatedEventArgs().Data; |
| 123 | Uri uri = eventArgs.Uri; |
| 124 | IWindowService.WindowId windowId = ResolveWindowId(uri); |
| 125 | |
| 126 | Navigate(windowId, ResolvePageKey(uri, windowId), args); |
| 127 | |
| 128 | await Task.CompletedTask; |
| 129 | } |
| 130 | } |