Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Prototype "Open at Login"
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Feb 13, 2024
1 parent 44e0756 commit df350dd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Aware/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Cocoa
import ServiceManagement

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -29,12 +30,27 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return defaultsValue ?? type(of: self).defaultUserIdleSeconds
}

let launchAtLoginMenuItem = NSMenuItem(
title: "Open at Login",
action: #selector(openAtLogin),
keyEquivalent: ""
)

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

func applicationDidFinishLaunching(_: Notification) {
userIdleSeconds = readUserIdleSeconds()

if SMAppService.mainApp.status == .enabled {
launchAtLoginMenuItem.title = "Open at Login"
launchAtLoginMenuItem.action = #selector(openAtLogin)
} else {
launchAtLoginMenuItem.title = "Don't Open at Login"
launchAtLoginMenuItem.action = #selector(removeOpenAtLogin)
}

let menu = NSMenu()
menu.addItem(launchAtLoginMenuItem)
menu.addItem(
NSMenuItem(
title: "Quit Aware",
Expand All @@ -52,6 +68,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: nil) { _ in self.resetTimer() }
}

@objc func openAtLogin() {
try! SMAppService.mainApp.register()
launchAtLoginMenuItem.title = "Don't Open at Login"
launchAtLoginMenuItem.action = #selector(removeOpenAtLogin)
}

@objc func removeOpenAtLogin() {
try! SMAppService.mainApp.unregister()
launchAtLoginMenuItem.title = "Open at Login"
launchAtLoginMenuItem.action = #selector(openAtLogin)
}

func resetTimer() {
timerStart = Date()
updateButton()
Expand Down

0 comments on commit df350dd

Please sign in to comment.