Skip to content

Commit

Permalink
get right click handlers hooked up, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 12, 2021
1 parent 7d3fd49 commit ffc3e37
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 52 deletions.
4 changes: 2 additions & 2 deletions whilom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.1.2;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -301,7 +301,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.1.2;
MARKETING_VERSION = 0.1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
122 changes: 72 additions & 50 deletions whilom/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@
// AppDelegate.swift
// whilom
//
// Created by Julian Weiss on 8/28/20.
// Copyright © 2020 Julian Weiss. All rights reserved.
// Created by Julian Weiss on 8/28/20, 3/11/21.
// Copyright © 2021 Snowcode, LLC. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - properties
// MARK: menu, list items
let menu: NSMenu = {
return NSMenu()
// MARK: left click menu item
let whilomStatusItem: NSStatusItem = {
var statusItem = NSStatusItem()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.title = "🪄"
return statusItem
}()

let whilomStatusItem: NSStatusItem = {
var statusItem = NSStatusItem()
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.title = "🪄"
return statusItem
// MARK: right click menu items
let menu: NSMenu = {
return NSMenu()
}()

let titleMenuItem: NSMenuItem = {
let versionString = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
return NSMenuItem(title: "🪄 whilom \(versionString)", action: nil, keyEquivalent: "")
}()

let openMenuItem: NSMenuItem = {
let item = NSMenuItem()
item.title = "🎩 Turn Sleep Off"
return item
let versionString = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
return NSMenuItem(title: "💝 whilom \(versionString)", action: nil, keyEquivalent: "")
}()

let quitMenuItem: NSMenuItem = {
let quitMenuItem = NSMenuItem()
quitMenuItem.title = "Quit"
return quitMenuItem
let quitMenuItem = NSMenuItem()
quitMenuItem.title = "Quit"
return quitMenuItem
}()

// MARK: scripts
// MARK: scripts, states
var isSleepEnabled = false

let enableSleepScript: NSAppleScript? = {
let myAppleScript = """
do shell script "sudo pmset -a disablesleep 0" with administrator privileges
Expand Down Expand Up @@ -69,63 +66,88 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// MARK: - runtime
func applicationDidFinishLaunching(_ aNotification: Notification) {
menu.delegate = self
whilomStatusItem.menu = menu

whilomStatusItem.button?.action = #selector(whilomClicked(_:))
whilomStatusItem.button?.sendAction(on: [.leftMouseUp, .rightMouseUp])
}

func applicationWillTerminate(_ aNotification: Notification) {
// bye bye!
}

// MARK: - left or right click handler
@objc func whilomClicked(_ sender: NSStatusBarButton) {
guard let event = NSApp.currentEvent else {
return
}

if event.type == .rightMouseUp {
createWhilomMenu()
} else {
toggleSleep()
}
}

// MARK: - right click handlers
func createWhilomMenu() {
titleMenuItem.isEnabled = false
menu.addItem(titleMenuItem)

menu.addItem(NSMenuItem.separator())

openMenuItem.action = #selector(disableSleep)
openMenuItem.target = self
menu.addItem(openMenuItem)

menu.addItem(NSMenuItem.separator())

quitMenuItem.action = #selector(quitWhilom(_:))
quitMenuItem.target = self
menu.addItem(quitMenuItem)

whilomStatusItem.menu = menu

let point = NSApp.currentEvent?.window?.frame.origin ?? .zero
menu.popUp(positioning: titleMenuItem, at: point, in: nil)
}

func applicationWillTerminate(_ aNotification: Notification) {
}


@objc func quitWhilom(_ sender: Any) -> Bool {
NSApplication.shared.terminate(self)
return true
}

@objc func disableSleep() -> Bool {

// MARK: - left click handler
private func toggleSleep() {
if isSleepEnabled {
let _ = disableSleep()
} else {
let _ = enableSleep()
}
}

// MARK: execute scripts based on state
@objc private func disableSleep() -> Bool {
var error: NSDictionary?
disableSleepScript?.executeAndReturnError(&error)

if let error = error {
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
alert.runModal()
return false
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
alert.runModal()
return false
}

openMenuItem.title = "😴 Turn Sleep On"
openMenuItem.action = #selector(enableSleep)
whilomStatusItem.button?.title = "😴"
isSleepEnabled = false
return true
}

@objc func enableSleep() -> Bool {
@objc private func enableSleep() -> Bool {
var error: NSDictionary?
enableSleepScript?.executeAndReturnError(&error)

if let error = error {
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
alert.runModal()
return false
let alert = NSAlert(error: NSError(domain: "com.insanj.whilom", code: 0, userInfo: [NSLocalizedDescriptionKey: error["NSAppleScriptErrorMessage"]!]))
alert.runModal()
return false
}

openMenuItem.title = "🎩 Turn Sleep Off"
openMenuItem.action = #selector(disableSleep)
whilomStatusItem.button?.title = "🪄"
isSleepEnabled = true
return true
}

}

extension AppDelegate: NSMenuDelegate, NSMenuItemValidation {
Expand Down

0 comments on commit ffc3e37

Please sign in to comment.