Skip to content

Commit

Permalink
Added about menu option and window.
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Kootz <[email protected]>
  • Loading branch information
Austin Kootz committed Apr 19, 2018
1 parent fbb2a00 commit d1791e9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions EjectionSeat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = sculptr.EjectionSeat;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
Expand All @@ -293,6 +294,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
PRODUCT_BUNDLE_IDENTIFIER = sculptr.EjectionSeat;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
Expand Down
Binary file not shown.
66 changes: 56 additions & 10 deletions EjectionSeat/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotificationCenterDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotificationCenterDelegate, NSWindowDelegate {

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let menu: NSMenu = NSMenu()
var aboutWindow: NSWindow?
var lastVolume: String = ""
let version: String? = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String


func applicationDidFinishLaunching(_ aNotification: Notification) {
//statusItem.title = "EjectionSeat"
let icon = NSImage(named: NSImage.Name("USBIcon"))
statusItem.image = icon
statusItem.image = NSImage(named: NSImage.Name("USBIcon"))
statusItem.image?.isTemplate = true
statusItem.menu = menu
statusItem.menu?.delegate = self
makeMenu()
makeAboutWindow()
}

func makeMenu(){
func makeMenu() {
menu.removeAllItems()
if let subMenu = makeSubMenu() {
menu.addItem(NSMenuItem(title: "Eject All", action: #selector(AppDelegate.ejectAll(_:)), keyEquivalent: "e"))
Expand All @@ -35,14 +38,57 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotifi
menu.addItem(NSMenuItem(title: "Nothing to eject", action: nil, keyEquivalent: ""))
}
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "About", action: #selector(AppDelegate.aboutWindowDisplay(_:)), keyEquivalent: "a"))
menu.addItem(NSMenuItem(title: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "q"))
}

func menuNeedsUpdate(_ menu: NSMenu) {
makeMenu()
}

@objc func makeSubMenu() -> NSMenu?{
func makeAboutWindow(){
let aboutView = NSView(frame: NSMakeRect(0, 0, 192, 256))
let aboutImage = NSImageView(image: NSImage(named: NSImage.Name("AppIcon"))!)
let aboutTextTitle = NSTextView(frame: NSMakeRect(0, 0, 192, 16))
let aboutTextBody = NSTextView(frame: NSMakeRect(0, 0, 192, 12))

aboutView.addSubview(aboutImage)
aboutView.addSubview(aboutTextTitle)
aboutView.addSubview(aboutTextBody)

aboutImage.setFrameSize(NSSize(width: 192, height: 128))
aboutImage.setFrameOrigin(NSPoint(x:0,y:96))
aboutImage.imageAlignment = NSImageAlignment.alignCenter

aboutTextTitle.setFrameOrigin(NSPoint(x:0,y:64))
aboutTextTitle.font = NSFont.titleBarFont(ofSize: 16)
aboutTextTitle.textColor = NSColor.textColor
aboutTextTitle.alignment = NSTextAlignment.center
aboutTextTitle.backgroundColor = NSColor.clear
aboutTextTitle.insertText("EjectionSeat.app")
aboutTextTitle.isEditable = false

aboutTextBody.setFrameOrigin(NSPoint(x:0,y:40))
aboutTextBody.font = NSFont.systemFont(ofSize: 12)
aboutTextBody.textColor = NSColor.gray
aboutTextBody.alignment = NSTextAlignment.center
aboutTextBody.backgroundColor = NSColor.clear
aboutTextBody.insertText("Developed by Åustin Kootz\nVersion \(version!)")
aboutTextBody.isEditable = false

aboutWindow = NSWindow.init(contentRect: aboutView.frame, styleMask: [.titled, .closable], backing: NSWindow.BackingStoreType.buffered, defer:false)
aboutWindow?.contentView = aboutView
aboutWindow?.backgroundColor = NSColor.windowBackgroundColor
aboutWindow?.isReleasedWhenClosed = false
aboutWindow?.title = "About EjectionSeat"
}

@objc func aboutWindowDisplay(_ sender:NSMenuItem) {
aboutWindow?.cascadeTopLeft(from: NSEvent.mouseLocation)
aboutWindow?.orderFrontRegardless()
}

@objc func makeSubMenu() -> NSMenu? {
guard let urls = getURLList(), urls.count > 0 else { return nil }
let subMenu = NSMenu()
var titles: [String] = []
Expand All @@ -62,13 +108,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotifi
NSApplication.shared.terminate(self)
}

@objc func eject(_ sender: NSMenuItem){
@objc func eject(_ sender: NSMenuItem) {
guard let urls = getURLList(), urls.count > 0 else { return }
for url in urls {
if url.pathComponents[url.pathComponents.endIndex-1] == sender.title {
lastVolume = sender.title
guard (try? NSWorkspace().unmountAndEjectDevice(at: url)) != nil else {
showNotificationFailure()
showNotificationFailure()
return
}
showNotificationSuccess()
Expand Down Expand Up @@ -123,20 +169,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotifi
NSUserNotificationCenter.default.deliver(notification)
}

func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification){
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
eject(NSMenuItem(title: notification.title!, action: nil, keyEquivalent: ""))
}

func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
return true
}

func getURLList()->[URL]?{
func getURLList()->[URL]? {
let keys: [URLResourceKey] = [.volumeNameKey, .volumeIsRemovableKey, .volumeIsEjectableKey]
guard var urls = FileManager().mountedVolumeURLs(includingResourceValuesForKeys: keys, options: []) else {
return nil
}
for url in urls{
for url in urls {
let components = url.pathComponents
if components.count < 2 || components[1] != "Volumes"{
urls.remove(at: urls.index(of: url)!)
Expand Down
6 changes: 3 additions & 3 deletions EjectionSeat/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.4.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2018 Austin Kootz. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
<key>NSUserNotificationAlertStyle</key>
<string>alert</string>
</dict>
Expand Down

0 comments on commit d1791e9

Please sign in to comment.