Skip to content

Commit

Permalink
Add possibility to nest command
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmeusel committed Oct 30, 2017
1 parent b71dbaa commit 60fa474
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions StatusBarRun/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13196"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down
45 changes: 40 additions & 5 deletions StatusBarRun/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Foundation

class StatusMenuController: NSObject, NSApplicationDelegate {

let zeroWidthSpace = "";

// Create status item in system status bar
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
// Status menu
Expand All @@ -22,6 +24,7 @@ class StatusMenuController: NSObject, NSApplicationDelegate {
let URL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".status-bar-run.json", isDirectory: false)
// Config data
var json = JSON.null
var map: [String:JSON] = [:]

@IBAction func quit(_ sender: NSMenuItem) {
NSApplication.shared().terminate(self)
Expand All @@ -48,18 +51,50 @@ class StatusMenuController: NSObject, NSApplicationDelegate {
func updateMenu() {
// Load items from config
reloadConfig()
map = [:]
// Clear items in menu
statusMenu.removeAllItems()
// Add items to menu
for (title, _) : (String, JSON) in json {
let item = NSMenuItem(title: title, action: #selector(StatusMenuController.run(sender:)), keyEquivalent: "")
item.target = self
statusMenu.addItem(item)
for (title, options) : (String, JSON) in json {
statusMenu.addItem(generateMenu(title: title, options: options, nesting: 1))
}
json = JSON.null
// Add status bar run sub menu
statusMenu.addItem(statusBarRunMenuItem)
}

func generateMenu(title: String, options: JSON, nesting: Int) -> NSMenuItem {
if (options["launchPath"] == JSON.null) {
// MenuItem with submenu
let item = NSMenuItem(title: title, action: nil, keyEquivalent: "")
item.submenu = NSMenu(title: title)
for (nestedTitle, nestedOptions) : (String, JSON) in options {
item.submenu?.addItem(generateMenu(title: nestedTitle, options: nestedOptions, nesting: nesting + 1))
}
return item;
} else {
// MenuItem without submenu
let prefixedTitle = getPrefix(nesting: nesting) + title.replacingOccurrences(of: zeroWidthSpace, with: "");
// Save action
map[prefixedTitle] = options
// Create item
let item = NSMenuItem(title: prefixedTitle, action: #selector(StatusMenuController.run(sender:)), keyEquivalent: "")
item.target = self
return item;
}
}

func getPrefix(nesting: Int) -> String {
if (nesting == 1) {
return "";
}
var prefix = zeroWidthSpace;
for _ in 2..<nesting {
prefix += zeroWidthSpace;
}
return prefix;
}

func reloadConfig() {
do {
// Create config file if not already existing
Expand All @@ -83,7 +118,7 @@ class StatusMenuController: NSObject, NSApplicationDelegate {
}

func run(sender: NSMenuItem) {
let options = json[sender.title]
let options = map[sender.title]!
let process = Process();
process.launchPath = options["launchPath"].stringValue
process.arguments = options["arguments"].arrayObject as? [String]
Expand Down

0 comments on commit 60fa474

Please sign in to comment.