From e0e3918c24f9595dd581ec8963d614c0c6dcf2a0 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Tue, 22 Apr 2025 09:16:41 +0400 Subject: [PATCH] feat: add support for URI activations for coder scheme --- App/App.xaml.cs | 25 ++++++++++++++++++- App/Package.appxmanifest | 52 ---------------------------------------- App/Program.cs | 29 +++++++++++++++++----- 3 files changed, 47 insertions(+), 59 deletions(-) delete mode 100644 App/Package.appxmanifest diff --git a/App/App.xaml.cs b/App/App.xaml.cs index 4a35a0f..44f59b6 100644 --- a/App/App.xaml.cs +++ b/App/App.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using System.Threading; using System.Threading.Tasks; using Coder.Desktop.App.Models; @@ -13,6 +14,8 @@ using Microsoft.Extensions.Hosting; using Microsoft.UI.Xaml; using Microsoft.Win32; +using Microsoft.Windows.AppLifecycle; +using Windows.ApplicationModel.Activation; namespace Coder.Desktop.App; @@ -82,7 +85,7 @@ public async Task ExitApplication() Environment.Exit(0); } - protected override void OnLaunched(LaunchActivatedEventArgs args) + protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) { // Start connecting to the manager in the background. var rpcController = _services.GetRequiredService(); @@ -138,4 +141,24 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) trayWindow.AppWindow.Hide(); }; } + + public void OnActivated(object? sender, AppActivationArguments args) + { + switch (args.Kind) + { + case ExtendedActivationKind.Protocol: + var protoArgs = args.Data as IProtocolActivatedEventArgs; + HandleURIActivation(protoArgs.Uri); + break; + + default: + // TODO: log + break; + } + } + + public void HandleURIActivation(Uri uri) + { + // TODO: handle + } } diff --git a/App/Package.appxmanifest b/App/Package.appxmanifest deleted file mode 100644 index e3ad480..0000000 --- a/App/Package.appxmanifest +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - Coder Desktop (Package) - Coder Technologies Inc. - Images\StoreLogo.png - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/App/Program.cs b/App/Program.cs index 2918caa..2ad863d 100644 --- a/App/Program.cs +++ b/App/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using Microsoft.UI.Dispatching; @@ -26,7 +27,23 @@ private static void Main(string[] args) try { ComWrappersSupport.InitializeComWrappers(); - if (!CheckSingleInstance()) return; + AppInstance mainInstance = GetMainInstance(); + if (!mainInstance.IsCurrent) + { + var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs(); + mainInstance.RedirectActivationToAsync(activationArgs).AsTask().Wait(); + return; + } + + // Register for URI handling (known as "protocol activation") +#if DEBUG + const string scheme = "coder-debug"; +#else + const string scheme = "coder"; +#endif + var thisBin = Assembly.GetExecutingAssembly().Location; + ActivationRegistrationManager.RegisterForProtocolActivation(scheme, thisBin + ",1", "Coder Desktop", ""); + Application.Start(p => { var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread()); @@ -38,6 +55,9 @@ private static void Main(string[] args) e.Handled = true; ShowExceptionAndCrash(e.Exception); }; + + // redirections via RedirectActivationToAsync above get routed to the App + mainInstance.Activated += app.OnActivated; }); } catch (Exception e) @@ -46,8 +66,7 @@ private static void Main(string[] args) } } - [STAThread] - private static bool CheckSingleInstance() + private static AppInstance GetMainInstance() { #if !DEBUG const string appInstanceName = "Coder.Desktop.App"; @@ -55,11 +74,9 @@ private static bool CheckSingleInstance() const string appInstanceName = "Coder.Desktop.App.Debug"; #endif - var instance = AppInstance.FindOrRegisterForKey(appInstanceName); - return instance.IsCurrent; + return AppInstance.FindOrRegisterForKey(appInstanceName); } - [STAThread] private static void ShowExceptionAndCrash(Exception e) { const string title = "Coder Desktop Fatal Error";