Skip to content

Commit b43578d

Browse files
authored
Merge pull request #20 from akuligowski9/launch-at-login
Add Launch at Login toggle
2 parents 33cc0c4 + 5ca8926 commit b43578d

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

macos-app/Sources/KestrelBar/AppDelegate.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AppKit
22
import KestrelBarLib
3+
import ServiceManagement
34

45
class AppDelegate: NSObject, NSApplicationDelegate {
56
private var statusItem: NSStatusItem!
@@ -276,6 +277,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
276277

277278
settingsSub.addItem(NSMenuItem.separator())
278279

280+
// Launch at Login
281+
let launchAtLogin = SMAppService.mainApp.status == .enabled
282+
let launchCheck = launchAtLogin ? "\u{2713} " : " "
283+
let launchItem = NSMenuItem(
284+
title: "\(launchCheck)Launch at Login",
285+
action: #selector(toggleLaunchAtLogin),
286+
keyEquivalent: ""
287+
)
288+
launchItem.target = self
289+
launchItem.attributedTitle = NSAttributedString(
290+
string: "\(launchCheck)Launch at Login",
291+
attributes: [.font: NSFont.systemFont(ofSize: 12, weight: launchAtLogin ? .semibold : .regular)]
292+
)
293+
settingsSub.addItem(launchItem)
294+
295+
settingsSub.addItem(NSMenuItem.separator())
296+
279297
// Project link
280298
let linkItem = NSMenuItem(
281299
title: "About Kestrel...",
@@ -561,6 +579,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {
561579
buildMenu()
562580
}
563581

582+
@objc private func toggleLaunchAtLogin() {
583+
do {
584+
if SMAppService.mainApp.status == .enabled {
585+
try SMAppService.mainApp.unregister()
586+
} else {
587+
try SMAppService.mainApp.register()
588+
}
589+
} catch {
590+
print("[KestrelBar] launch at login toggle failed: \(error)")
591+
}
592+
buildMenu()
593+
}
594+
564595
@objc private func openProject() {
565596
NSWorkspace.shared.open(URL(string: "https://github.com/akuligowski9/kestrel")!)
566597
}

0 commit comments

Comments
 (0)