From 0cf2f28f142bfc84380d70a02928d2e022abcae7 Mon Sep 17 00:00:00 2001 From: Ethan <39577870+ethanndickson@users.noreply.github.com> Date: Mon, 12 May 2025 12:46:09 +1000 Subject: [PATCH 1/3] chore: add handler and router for `coder` scheme URIs (#145) Relates to #96. Closes https://github.com/coder/coder-desktop-macos/issues/95 --- .../Coder-Desktop/Coder_DesktopApp.swift | 19 ++- Coder-Desktop/Coder-Desktop/Info.plist | 15 +++ Coder-Desktop/Coder-Desktop/URLHandler.swift | 39 +++++++ Coder-Desktop/Resources/1024Icon.png | Bin 0 -> 18138 bytes Coder-Desktop/VPNLib/CoderRouter.swift | 64 +++++++++++ .../VPNLibTests/CoderRouterTests.swift | 108 ++++++++++++++++++ Coder-Desktop/project.yml | 5 + Makefile | 1 + scripts/build.sh | 1 + 9 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 Coder-Desktop/Coder-Desktop/URLHandler.swift create mode 100644 Coder-Desktop/Resources/1024Icon.png create mode 100644 Coder-Desktop/VPNLib/CoderRouter.swift create mode 100644 Coder-Desktop/VPNLibTests/CoderRouterTests.swift diff --git a/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift b/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift index d9cd6493..4d787355 100644 --- a/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift +++ b/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift @@ -17,8 +17,8 @@ struct DesktopApp: App { Window("Sign In", id: Windows.login.rawValue) { LoginForm() .environmentObject(appDelegate.state) - } - .windowResizability(.contentSize) + }.handlesExternalEvents(matching: Set()) // Don't handle deep links + .windowResizability(.contentSize) SwiftUI.Settings { SettingsView() .environmentObject(appDelegate.vpn) @@ -30,7 +30,7 @@ struct DesktopApp: App { .environmentObject(appDelegate.state) .environmentObject(appDelegate.fileSyncDaemon) .environmentObject(appDelegate.vpn) - } + }.handlesExternalEvents(matching: Set()) // Don't handle deep links } } @@ -40,6 +40,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { let vpn: CoderVPNService let state: AppState let fileSyncDaemon: MutagenDaemon + let urlHandler: URLHandler override init() { vpn = CoderVPNService() @@ -65,6 +66,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { await fileSyncDaemon.tryStart() } self.fileSyncDaemon = fileSyncDaemon + urlHandler = URLHandler(state: state, vpn: vpn) } func applicationDidFinishLaunching(_: Notification) { @@ -134,6 +136,17 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } + func application(_: NSApplication, open urls: [URL]) { + guard let url = urls.first else { + // We only accept one at time, for now + return + } + do { try urlHandler.handle(url) } catch { + // TODO: Push notification + print(error.description) + } + } + private func displayIconHiddenAlert() { let alert = NSAlert() alert.alertStyle = .informational diff --git a/Coder-Desktop/Coder-Desktop/Info.plist b/Coder-Desktop/Coder-Desktop/Info.plist index 5e59b253..4712604f 100644 --- a/Coder-Desktop/Coder-Desktop/Info.plist +++ b/Coder-Desktop/Coder-Desktop/Info.plist @@ -2,6 +2,21 @@ + CFBundleURLTypes + + + CFBundleTypeRole + Editor + CFBundleURLIconFile + 1024Icon + CFBundleURLName + com.coder.Coder-Desktop + CFBundleURLSchemes + + coder + + + NSAppTransportSecurity