Skip to content

Commit 0776807

Browse files
feat: dynamically show app in dock & cmd+tab (#224)
1 parent 7bc567a commit 0776807

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Coder-Desktop/Coder-Desktop/Coder_DesktopApp.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ struct DesktopApp: App {
2020
Window("Sign In", id: Windows.login.rawValue) {
2121
LoginForm()
2222
.environmentObject(appDelegate.state)
23+
.showDockIconWhenOpen()
2324
}.handlesExternalEvents(matching: Set()) // Don't handle deep links
2425
.windowResizability(.contentSize)
2526
SwiftUI.Settings {
2627
SettingsView<CoderVPNService>()
2728
.environmentObject(appDelegate.vpn)
2829
.environmentObject(appDelegate.state)
2930
.environmentObject(appDelegate.autoUpdater)
31+
.showDockIconWhenOpen()
3032
}
3133
.windowResizability(.contentSize)
3234
Window("Coder File Sync", id: Windows.fileSync.rawValue) {
3335
FileSyncConfig<CoderVPNService, MutagenDaemon>()
3436
.environmentObject(appDelegate.state)
3537
.environmentObject(appDelegate.fileSyncDaemon)
3638
.environmentObject(appDelegate.vpn)
39+
.showDockIconWhenOpen()
3740
}.handlesExternalEvents(matching: Set()) // Don't handle deep links
3841
}
3942
}

Coder-Desktop/Coder-Desktop/Views/Util.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,26 @@ public extension View {
4444
}
4545
}
4646
}
47+
48+
@MainActor
49+
private struct ActivationPolicyModifier: ViewModifier {
50+
func body(content: Content) -> some View {
51+
content
52+
// This lets us show and hide the app from the dock and cmd+tab
53+
// when a window is open.
54+
.onAppear {
55+
NSApp.setActivationPolicy(.regular)
56+
}
57+
.onDisappear {
58+
if NSApp.windows.filter { $0.level != .statusBar && $0.isVisible }.count <= 1 {
59+
NSApp.setActivationPolicy(.accessory)
60+
}
61+
}
62+
}
63+
}
64+
65+
public extension View {
66+
func showDockIconWhenOpen() -> some View {
67+
modifier(ActivationPolicyModifier())
68+
}
69+
}

0 commit comments

Comments
 (0)