Skip to content

Commit

Permalink
Add "Start at Login" menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
alin23 committed Mar 10, 2019
1 parent beb3984 commit f0e8b86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Lunar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
8 changes: 8 additions & 0 deletions Lunar/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
<action selector="toggleBrightnessAdapterWithSender:" target="Voe-Tx-rLC" id="WZa-gt-6uZ"/>
</connections>
</menuItem>
<menuItem title="Start at Login" state="on" id="HeU-1X-veM">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="toggleStartAtLoginWithSender:" target="Voe-Tx-rLC" id="UeX-aW-1MF"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" title="Intensity" id="hyT-YG-TrU"/>
<menuItem title="0%" keyEquivalent="0" id="9Jm-7d-C6T">
<attributedString key="attributedTitle"/>
Expand Down Expand Up @@ -172,6 +179,7 @@
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Lunar" customModuleProvider="target">
<connections>
<outlet property="loginMenuItem" destination="HeU-1X-veM" id="T04-59-iC5"/>
<outlet property="menu" destination="uQy-DD-JDr" id="Vtf-ap-8J3"/>
<outlet property="stateMenuItem" destination="YMI-RQ-F1Z" id="cCj-Nl-eqb"/>
<outlet property="toggleMenuItem" destination="cb4-qT-5Fw" id="aWt-SV-OwO"/>
Expand Down

0 comments on commit f0e8b86

Please sign in to comment.