Skip to content

Commit

Permalink
Add ability to create top-level menus
Browse files Browse the repository at this point in the history
  • Loading branch information
richinfante committed Mar 1, 2019
1 parent 80accd1 commit 2218c79
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 73 deletions.
68 changes: 48 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,65 @@ In a file named `~/.config/shbar/shbar.json`, add a file using the following str
[
{
"titleRefreshInterval" : 120,
"title" : "IP Address",
"mode" : "RefreshingItem",
"titleScript" : {
"bin" : "/bin/sh",
"title" : "IP Address",
"actionScript" : {
"bin" : "\/bin\/sh",
"args" : [
"-c",
"echo IP: $(curl https://api.ipify.org)"
"open https:\/\/api.ipify.org"
],
"env" : {
"PATH" : "/usr/bin:/usr/local/bin:/sbin:/bin"
"PATH" : "\/usr\/bin:\/usr\/local\/bin:\/sbin:\/bin"
}
}
}, {
"autostartJob" : true,
"jobScript" : {
"bin" : "/bin/bash",
},
"titleScript" : {
"bin" : "\/bin\/sh",
"args" : [
"-c",
"ssh user@example.com -nNT -L 8080:localhost:8080"
"echo $(curl https:\/\/api.ipify.org) | tr '\n' ' '"
],
"env" : {
"PATH" : "/usr/bin:/usr/local/bin:/sbin:/bin"
"PATH" : "\/usr\/bin:\/usr\/local\/bin:\/sbin:\/bin"
}
},
"title" : "SSH Tunnel",
"mode" : "JobStatus",
"reloadJob" : false
}, {
"mode" : "ApplicationQuit",
"title" : "Quit",
"shortcutKey" : "q"
}
},
{
"reloadJob" : false,
"autostartJob" : false,
"title" : "~:$",
"actionShowsConsole" : false,
"mode" : "RefreshingItem",
"children" : [
{
"autostartJob" : false,
"mode" : "RefreshingItem",
"title" : "Setup Help",
"actionScript" : {
"bin" : "\/bin\/sh",
"args" : [
"-c",
"open https:\/\/github.com\/richinfante\/shbar"
],
"env" : {
"PATH" : "\/usr\/bin:\/usr\/local\/bin:\/sbin:\/bin"
}
},
"children" : [

],
"actionShowsConsole" : false,
"reloadJob" : false
},
{
"shortcutKey" : "q",
"autostartJob" : false,
"mode" : "ApplicationQuit",
"title" : "Quit",
"actionShowsConsole" : false,
"reloadJob" : false
}
]
}
]

Expand Down
12 changes: 12 additions & 0 deletions shbar2.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
9B45A9A2221B7B2600305441 /* ItemConfigMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B45A9A1221B7B2600305441 /* ItemConfigMode.swift */; };
9B45A9A4221B7B4000305441 /* JobStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B45A9A3221B7B4000305441 /* JobStatus.swift */; };
9B45A9A6221B7B6000305441 /* ItemConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B45A9A5221B7B6000305441 /* ItemConfig.swift */; };
9B4912D2221CCAD2006E6C73 /* LabelProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B4912D1221CCAD2006E6C73 /* LabelProtocol.swift */; };
9B53792522296C9100D88798 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 9B53792422296C9000D88798 /* README.md */; };
9B53792722296C9800D88798 /* LICENSE.md in Resources */ = {isa = PBXBuildFile; fileRef = 9B53792622296C9800D88798 /* LICENSE.md */; };
9BDEE78222163235006BA354 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BDEE78122163235006BA354 /* AppDelegate.swift */; };
9BDEE78622163235006BA354 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9BDEE78522163235006BA354 /* Assets.xcassets */; };
9BDEE78922163235006BA354 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9BDEE78722163235006BA354 /* Main.storyboard */; };
Expand All @@ -32,6 +35,9 @@
9B45A9A1221B7B2600305441 /* ItemConfigMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemConfigMode.swift; sourceTree = "<group>"; };
9B45A9A3221B7B4000305441 /* JobStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JobStatus.swift; sourceTree = "<group>"; };
9B45A9A5221B7B6000305441 /* ItemConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemConfig.swift; sourceTree = "<group>"; };
9B4912D1221CCAD2006E6C73 /* LabelProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelProtocol.swift; sourceTree = "<group>"; };
9B53792422296C9000D88798 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
9B53792622296C9800D88798 /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = "<group>"; };
9BDEE77E22163235006BA354 /* shbar2.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = shbar2.app; sourceTree = BUILT_PRODUCTS_DIR; };
9BDEE78122163235006BA354 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9BDEE78522163235006BA354 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -64,6 +70,8 @@
9BDEE77522163234006BA354 = {
isa = PBXGroup;
children = (
9B53792422296C9000D88798 /* README.md */,
9B53792622296C9800D88798 /* LICENSE.md */,
9BDEE78022163235006BA354 /* shbar2 */,
9BDEE79322163235006BA354 /* shbar2Tests */,
9BDEE77F22163235006BA354 /* Products */,
Expand Down Expand Up @@ -91,6 +99,7 @@
9BDEE78722163235006BA354 /* Main.storyboard */,
9BDEE78A22163235006BA354 /* Info.plist */,
9BDEE78B22163235006BA354 /* shbar2.entitlements */,
9B4912D1221CCAD2006E6C73 /* LabelProtocol.swift */,
);
path = shbar2;
sourceTree = "<group>";
Expand Down Expand Up @@ -193,6 +202,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9B53792722296C9800D88798 /* LICENSE.md in Resources */,
9B53792522296C9100D88798 /* README.md in Resources */,
9BDEE78622163235006BA354 /* Assets.xcassets in Resources */,
9BDEE78922163235006BA354 /* Main.storyboard in Resources */,
);
Expand All @@ -216,6 +227,7 @@
9B45A9A4221B7B4000305441 /* JobStatus.swift in Sources */,
9B45A9A0221B7B0B00305441 /* Script.swift in Sources */,
9BDEE78222163235006BA354 /* AppDelegate.swift in Sources */,
9B4912D2221CCAD2006E6C73 /* LabelProtocol.swift in Sources */,
9B45A9A6221B7B6000305441 /* ItemConfig.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
82 changes: 58 additions & 24 deletions shbar2/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,39 @@ class AppDelegate: NSObject, NSApplicationDelegate {
title: "IP Address",
titleScript: Script(
bin: "/bin/sh",
args: ["-c", "echo IP: $(curl https://api.ipify.org)"],
args: ["-c", "echo $(curl https://api.ipify.org) | tr '\n' ' '"],
env: [
"PATH": "/usr/bin:/usr/local/bin:/sbin:/bin"
]),
titleRefreshInterval: 120
),
ItemConfig(
title: "Setup Help",
titleRefreshInterval: 120,
actionScript: Script(
bin: "/bin/sh",
args: ["-c", "open https://github.com/richinfante/shbar"],
args: ["-c", "open https://api.ipify.org"],
env: [
"PATH": "/usr/bin:/usr/local/bin:/sbin:/bin"
])
),
ItemConfig(
mode: .ApplicationQuit,
title: "Quit",
shortcutKey: "q"
)
title: "~:$",
children: [
ItemConfig(
title: "Setup Help",
actionScript: Script(
bin: "/bin/sh",
args: ["-c", "open https://github.com/richinfante/shbar"],
env: [
"PATH": "/usr/bin:/usr/local/bin:/sbin:/bin"
])
),
ItemConfig(
mode: .ApplicationQuit,
title: "Quit",
shortcutKey: "q"
)
])
]

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var statusItems : [NSStatusItem] = []

static var userHomeDirectoryPath : String {
let pw = getpwuid(getuid())
Expand Down Expand Up @@ -81,35 +91,59 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let data = try? jsonEncoder.encode(menuItems)
print(String(data: data!, encoding: .utf8)!)
// Insert code here to initialize your application
let menu = NSMenu(title: "shbar")
menu.items = []


for item in menuItems {
// Add to actual menu
menu.items.append(item.createMenuItem(self))
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
// Set main title
statusItem.menu = item.createSubMenu(self)
item.menuItem = statusItem.button!
item.initializeTitle()

if item.actionScript != nil {
statusItem.button!.action = #selector(ItemConfig.dispatchAction)
statusItem.button!.target = item
}

// if item.menuItem?.title == "SHBAR" {
// item.menuItem?.title = ""
// let image = NSImage(named: "Image-1")
//// image!.size = NSSize(width: NSStatusItem.squareLength, height: NSStatusItem.squareLength)
// statusItem.length = NSStatusItem.squareLength
// statusItem.button!.image = image
// }

statusItems.append(statusItem)
}

// Set main title
statusItem.button!.title = "shbar"
statusItem.menu = menu

print("launched.")
}

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
self.terminateRemainingJobs()
return NSApplication.TerminateReply.terminateNow
}

func applicationWillTerminate(_ aNotification: Notification) {

// Insert code here to tear down your application
self.terminateRemainingJobs()
}

@objc func terminateMenuBarApp(_ sender: NSMenuItem?) {
func terminateRemainingJobs() {
print("terminating remaining jobs...")

// Terminate jobs
for item in menuItems {
item.currentJob?.terminate()
}
}

@objc func terminateMenuBarApp(_ sender: NSMenuItem?) {
self.terminateRemainingJobs()
NSApplication.shared.terminate(self)
}

func handler(sig: Int32) -> Void {
self.terminateMenuBarApp(nil)
}

}

33 changes: 23 additions & 10 deletions shbar2/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,71 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"idiom" : "mac",
"filename" : "icon_16x16.png",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"idiom" : "mac",
"filename" : "icon_16x16@2x.png",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"idiom" : "mac",
"filename" : "icon_32x32.png",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"idiom" : "mac",
"filename" : "icon_32x32@2x.png",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"idiom" : "mac",
"filename" : "icon_128x128.png",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"idiom" : "mac",
"filename" : "icon_128x128@2x.png",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"idiom" : "mac",
"filename" : "icon_256x256.png",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"idiom" : "mac",
"filename" : "icon_256x256@2x.png",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"idiom" : "mac",
"filename" : "icon_512x512.png",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"idiom" : "mac",
"filename" : "icon_512x512@2x.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"pre-rendered" : true
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions shbar2/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.0.1</string>
<string>0.0.2</string>
<key>CFBundleVersion</key>
<string>8</string>
<string>10</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down

0 comments on commit 2218c79

Please sign in to comment.