diff --git a/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift b/Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift index d3deab2..eab01ea 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 69981a2..10d0747 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()) + } +}