Skip to content

Commit

Permalink
about window w icon, fix right click forcing menu over & over
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Mar 12, 2021
1 parent 664ed11 commit aa8819a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 11 deletions.
8 changes: 4 additions & 4 deletions whilom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 24X8MVUSCN;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = whilom/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.1.6;
MARKETING_VERSION = 0.1.7;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -299,15 +299,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 24X8MVUSCN;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = whilom/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.1.6;
MARKETING_VERSION = 0.1.7;
PRODUCT_BUNDLE_IDENTIFIER = com.insanj.whilom;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
46 changes: 39 additions & 7 deletions whilom/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return NSMenuItem(title: "💝 whilom \(versionString)", action: nil, keyEquivalent: "")
}()

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

let quitMenuItem: NSMenuItem = {
let quitMenuItem = NSMenuItem()
quitMenuItem.title = "Quit"
Expand Down Expand Up @@ -91,6 +97,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

whilomStatusItem.button?.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil)
themeifyMenuItems()

setupRightClickMenu()
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand All @@ -111,29 +119,48 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

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

menu.addItem(NSMenuItem.separator())

aboutMenuItem.action = #selector(aboutWhilom(_:))
aboutMenuItem.target = self
menu.addItem(aboutMenuItem)

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

}

private func createWhilomMenu() {
whilomStatusItem.menu = menu

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

private func destroyWhilomMenu() {
whilomStatusItem.menu = nil
}

@objc func aboutWhilom(_ sender: Any) {
let aboutAlert = NSAlert()
aboutAlert.icon = NSImage(named: NSImage.Name("whilom"))
aboutAlert.messageText = "whilom"
aboutAlert.informativeText = "🪄 keep your mac awake even when the lid is closed\n\n💭 whi·lom, meaning \"at times,\" having once been\n\n🎉 thanks for checking out our app! follow us @SnowcodeDesign\n\n© 2021 Snowcode, LLC"
aboutAlert.addButton(withTitle: "👋 Sweet dreams!")
aboutAlert.runModal()
}

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

// MARK: - left click handler
private func toggleSleep() {
func toggleSleep() {
if isSleepEnabled {
let _ = disableSleep()
} else {
Expand All @@ -142,7 +169,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// MARK: execute scripts based on state
@objc private func disableSleep() -> Bool {
@objc func disableSleep() -> Bool {
if !isJustMessingAround {
var error: NSDictionary?
disableSleepScript?.executeAndReturnError(&error)
Expand All @@ -159,7 +186,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return true
}

@objc private func enableSleep() -> Bool {
@objc func enableSleep() -> Bool {
if !isJustMessingAround {
var error: NSDictionary?
enableSleepScript?.executeAndReturnError(&error)
Expand Down Expand Up @@ -205,7 +232,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// MARK: - dark/light handling
static func isDarkMode(_ statusItem: NSStatusItem) -> Bool {
static private func isDarkMode(_ statusItem: NSStatusItem) -> Bool {
let effectiveAppearance = statusItem.button?.effectiveAppearance
let themeName = effectiveAppearance?.name.rawValue.lowercased()
let containsDark = themeName?.contains("dark")
Expand Down Expand Up @@ -239,8 +266,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}


extension AppDelegate: NSMenuDelegate, NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
return true
}

func menuDidClose(_ menu: NSMenu) {
destroyWhilomMenu()
}
}
Binary file modified whilom/Assets.xcassets/hattie on 3.imageset/Artboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified whilom/Assets.xcassets/hattie on 3.imageset/Artboard@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified whilom/Assets.xcassets/hattie on 3.imageset/Artboard@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions whilom/Assets.xcassets/whilom.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "Artboard.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Artboard@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Artboard@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

0 comments on commit aa8819a

Please sign in to comment.