From f0e8b86df0a3cd814b19c1fe2ffeb1144482af99 Mon Sep 17 00:00:00 2001 From: Alin Panaitiu Date: Sun, 10 Mar 2019 20:25:28 +0200 Subject: [PATCH] Add "Start at Login" menu item --- Lunar/AppDelegate.swift | 15 +++++++++++++++ Lunar/Base.lproj/Main.storyboard | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/Lunar/AppDelegate.swift b/Lunar/AppDelegate.swift index 0c7860f1..075c8bbf 100644 --- a/Lunar/AppDelegate.swift +++ b/Lunar/AppDelegate.swift @@ -80,6 +80,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate, N @IBOutlet var menu: NSMenu! @IBOutlet var stateMenuItem: NSMenuItem! @IBOutlet var toggleMenuItem: NSMenuItem! + @IBOutlet var loginMenuItem: NSMenuItem! let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) @@ -145,13 +146,23 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate, N } } + func setLoginMenuItemState(state: Bool) { + if state { + loginMenuItem?.state = .on + } else { + loginMenuItem?.state = .off + } + } + func handleDaemon() { let runningApps = NSWorkspace.shared.runningApplications let isRunning = runningApps.contains(where: { app in app.bundleIdentifier == launcherAppId }) SMLoginItemSetEnabled(launcherAppId as CFString, datastore.defaults.startAtLogin) + setLoginMenuItemState(state: datastore.defaults.startAtLogin) loginItemObserver = datastore.defaults.observe(\.startAtLogin, options: [.new], changeHandler: { _, change in SMLoginItemSetEnabled(launcherAppId as CFString, change.newValue ?? false) + self.setLoginMenuItemState(state: change.newValue ?? false) }) if isRunning { @@ -494,6 +505,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate, N brightnessAdapter.toggle() } + @IBAction func toggleStartAtLogin(sender _: Any?) { + datastore.defaults.set(!datastore.defaults.startAtLogin, forKey: "startAtLogin") + } + @IBAction func showWindow(sender _: Any?) { showWindow() } diff --git a/Lunar/Base.lproj/Main.storyboard b/Lunar/Base.lproj/Main.storyboard index 3561ac34..d1534596 100644 --- a/Lunar/Base.lproj/Main.storyboard +++ b/Lunar/Base.lproj/Main.storyboard @@ -33,6 +33,13 @@ + + + + + + + @@ -172,6 +179,7 @@ +