From 40e7af4725bdb75629ca3f57e292941653d276e3 Mon Sep 17 00:00:00 2001 From: Ethan Dickson Date: Tue, 12 Aug 2025 13:28:09 +1000 Subject: [PATCH] feat: dynamically show app in dock & cmd+tab --- .../Coder-Desktop/Coder_DesktopApp.swift | 3 +++ Coder-Desktop/Coder-Desktop/Views/Util.swift | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift b/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift index d3deab21..eab01ea2 100644 --- a/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift +++ b/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift @@ -20,6 +20,7 @@ struct DesktopApp: App { Window("Sign In", id: Windows.login.rawValue) { LoginForm() .environmentObject(appDelegate.state) + .showDockIconWhenOpen() }.handlesExternalEvents(matching: Set()) // Don't handle deep links .windowResizability(.contentSize) SwiftUI.Settings { @@ -27,6 +28,7 @@ struct DesktopApp: App { .environmentObject(appDelegate.vpn) .environmentObject(appDelegate.state) .environmentObject(appDelegate.autoUpdater) + .showDockIconWhenOpen() } .windowResizability(.contentSize) Window("Coder File Sync", id: Windows.fileSync.rawValue) { @@ -34,6 +36,7 @@ struct DesktopApp: App { .environmentObject(appDelegate.state) .environmentObject(appDelegate.fileSyncDaemon) .environmentObject(appDelegate.vpn) + .showDockIconWhenOpen() }.handlesExternalEvents(matching: Set()) // Don't handle deep links } } diff --git a/Coder-Desktop/Coder-Desktop/Views/Util.swift b/Coder-Desktop/Coder-Desktop/Views/Util.swift index 69981a25..10d07479 100644 --- a/Coder-Desktop/Coder-Desktop/Views/Util.swift +++ b/Coder-Desktop/Coder-Desktop/Views/Util.swift @@ -44,3 +44,26 @@ public extension View { } } } + +@MainActor +private struct ActivationPolicyModifier: ViewModifier { + func body(content: Content) -> some View { + content + // This lets us show and hide the app from the dock and cmd+tab + // when a window is open. + .onAppear { + NSApp.setActivationPolicy(.regular) + } + .onDisappear { + if NSApp.windows.filter { $0.level != .statusBar && $0.isVisible }.count <= 1 { + NSApp.setActivationPolicy(.accessory) + } + } + } +} + +public extension View { + func showDockIconWhenOpen() -> some View { + modifier(ActivationPolicyModifier()) + } +}