Skip to content

Commit

Permalink
Added single drive ejection
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 18, 2018
1 parent 4cd7706 commit 7a9db51
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file not shown.
66 changes: 53 additions & 13 deletions EjectionSeat/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,70 @@
import Cocoa

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

@IBOutlet weak var statusMenu: NSMenu!
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var menu: NSMenu = NSMenu()

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

func makeMenu(){
menu.removeAllItems()
menu.addItem(NSMenuItem(title: "Eject All", action: #selector(AppDelegate.ejectAll(_:)), keyEquivalent: "e"))
menu.addItem(NSMenuItem(title: "Eject", action: nil, keyEquivalent: ""))
menu.setSubmenu(makeSubMenu(), for: (menu.item(withTitle: "Eject"))!)
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "q"))

}

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

@objc func makeSubMenu() -> NSMenu?{
guard let urls = getURLList(), urls.count > 0 else {
return nil
}
let subMenu = NSMenu()
var numKey = 0
for url in urls {
numKey += 1
subMenu.addItem(NSMenuItem(title: url.pathComponents[url.pathComponents.endIndex-1], action: #selector(AppDelegate.eject(_:)), keyEquivalent: "\(numKey)"))
}
return subMenu
}

@objc func eject(_ sender: NSMenuItem){
print(sender.title)
guard let urls = getURLList(), urls.count > 0 else {
return
}
for url in urls {
FileManager().unmountVolume(at: url, options: [.allPartitionsAndEjectDisk, .withoutUI], completionHandler: ejectionhandle)
if url.pathComponents[url.pathComponents.endIndex-1] == sender.title {
FileManager().unmountVolume(at: url, options: [.allPartitionsAndEjectDisk, .withoutUI], completionHandler: ejectionhandle)
}
}
}

@objc func ejectAll(_ sender: NSMenuItem) {
guard let urls = getURLList(), urls.count > 0 else {
return
}
for url in urls {
FileManager().unmountVolume(at: url, options: [.allPartitionsAndEjectDisk, .withoutUI], completionHandler: ejectionhandle)
}
}

@IBAction func quit(_ sender: Any) {
@objc func quit(_ sender: NSMenuItem) {
NSApplication.shared.terminate(self)
}

Expand All @@ -51,14 +99,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}


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

func applicationWillTerminate(_ aNotification: Notification) {
}

Expand Down
29 changes: 4 additions & 25 deletions EjectionSeat/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,12 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
<connections>
<outlet property="delegate" destination="Voe-Tx-rLC" id="GzC-gU-4Uq"/>
<outlet property="delegate" destination="AppDelegateSwift" id="AppDelegateOutlet"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="EjectionSeat" customModuleProvider="target">
<connections>
<outlet property="statusMenu" destination="mSa-yX-AxQ" id="FE9-Se-KJE"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<menu id="mSa-yX-AxQ">
<items>
<menuItem title="Eject All" id="6fd-JU-EzO">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="ejectAll:" target="Voe-Tx-rLC" id="CX8-aT-Bgp"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="HQH-6W-PP6"/>
<menuItem title="Quit" id="9Ao-gE-A8p" userLabel="Quit">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="quit:" target="Voe-Tx-rLC" id="7km-V4-M3o"/>
</connections>
</menuItem>
</items>
</menu>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="AppDelegateSwift" customClass="AppDelegate" customModule="EjectionSeat" customModuleProvider="target"/>
<customObject id="ESMenuNSFontManager" customClass="NSFontManager"/>
</objects>
</document>

0 comments on commit 7a9db51

Please sign in to comment.