Skip to content

Commit bd221c4

Browse files
authored
feat: add support for URI activations for coder scheme (#72)
relates to #52 Adds support for Coder Desktop to handle the `coder:/` URI scheme by registering for this scheme and forwarding activations to the single instance of Coder Desktop. Also removes the `Package.appxmanifest`. It is unused since Coder Desktop is not a packaged app.
1 parent a5ab4f5 commit bd221c4

File tree

3 files changed

+47
-59
lines changed

3 files changed

+47
-59
lines changed

App/App.xaml.cs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using Coder.Desktop.App.Models;
@@ -13,6 +14,8 @@
1314
using Microsoft.Extensions.Hosting;
1415
using Microsoft.UI.Xaml;
1516
using Microsoft.Win32;
17+
using Microsoft.Windows.AppLifecycle;
18+
using Windows.ApplicationModel.Activation;
1619

1720
namespace Coder.Desktop.App;
1821

@@ -82,7 +85,7 @@ public async Task ExitApplication()
8285
Environment.Exit(0);
8386
}
8487

85-
protected override void OnLaunched(LaunchActivatedEventArgs args)
88+
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
8689
{
8790
// Start connecting to the manager in the background.
8891
var rpcController = _services.GetRequiredService<IRpcController>();
@@ -138,4 +141,24 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
138141
trayWindow.AppWindow.Hide();
139142
};
140143
}
144+
145+
public void OnActivated(object? sender, AppActivationArguments args)
146+
{
147+
switch (args.Kind)
148+
{
149+
case ExtendedActivationKind.Protocol:
150+
var protoArgs = args.Data as IProtocolActivatedEventArgs;
151+
HandleURIActivation(protoArgs.Uri);
152+
break;
153+
154+
default:
155+
// TODO: log
156+
break;
157+
}
158+
}
159+
160+
public void HandleURIActivation(Uri uri)
161+
{
162+
// TODO: handle
163+
}
141164
}

App/Package.appxmanifest

-52
This file was deleted.

App/Program.cs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using System.Runtime.InteropServices;
34
using System.Threading;
45
using Microsoft.UI.Dispatching;
@@ -26,7 +27,23 @@ private static void Main(string[] args)
2627
try
2728
{
2829
ComWrappersSupport.InitializeComWrappers();
29-
if (!CheckSingleInstance()) return;
30+
AppInstance mainInstance = GetMainInstance();
31+
if (!mainInstance.IsCurrent)
32+
{
33+
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
34+
mainInstance.RedirectActivationToAsync(activationArgs).AsTask().Wait();
35+
return;
36+
}
37+
38+
// Register for URI handling (known as "protocol activation")
39+
#if DEBUG
40+
const string scheme = "coder-debug";
41+
#else
42+
const string scheme = "coder";
43+
#endif
44+
var thisBin = Assembly.GetExecutingAssembly().Location;
45+
ActivationRegistrationManager.RegisterForProtocolActivation(scheme, thisBin + ",1", "Coder Desktop", "");
46+
3047
Application.Start(p =>
3148
{
3249
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
@@ -38,6 +55,9 @@ private static void Main(string[] args)
3855
e.Handled = true;
3956
ShowExceptionAndCrash(e.Exception);
4057
};
58+
59+
// redirections via RedirectActivationToAsync above get routed to the App
60+
mainInstance.Activated += app.OnActivated;
4161
});
4262
}
4363
catch (Exception e)
@@ -46,20 +66,17 @@ private static void Main(string[] args)
4666
}
4767
}
4868

49-
[STAThread]
50-
private static bool CheckSingleInstance()
69+
private static AppInstance GetMainInstance()
5170
{
5271
#if !DEBUG
5372
const string appInstanceName = "Coder.Desktop.App";
5473
#else
5574
const string appInstanceName = "Coder.Desktop.App.Debug";
5675
#endif
5776

58-
var instance = AppInstance.FindOrRegisterForKey(appInstanceName);
59-
return instance.IsCurrent;
77+
return AppInstance.FindOrRegisterForKey(appInstanceName);
6078
}
6179

62-
[STAThread]
6380
private static void ShowExceptionAndCrash(Exception e)
6481
{
6582
const string title = "Coder Desktop Fatal Error";

0 commit comments

Comments
 (0)