Skip to content

Commit

Permalink
Add configure menu listing all workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-pennyworth committed Jul 11, 2024
1 parent 79062ef commit 13bd784
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 52 deletions.
4 changes: 4 additions & 0 deletions AlfredExtraPane.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
D08B6EE7266F9B520099EB36 /* PanePositionCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D08B6EE6266F9B520099EB36 /* PanePositionCodable.swift */; };
D0B3E0A42B8A1F3A008EA695 /* AlfredExtraPaneTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0B3E0A32B8A1F3A008EA695 /* AlfredExtraPaneTests.swift */; };
D0E764282C3C1E6D00E662F3 /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = D0E764272C3C1E6D00E662F3 /* Sparkle */; };
D0FCE9412C40049200F5006C /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0FCE9402C40049200F5006C /* Menu.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -63,6 +64,7 @@
D08B6EE6266F9B520099EB36 /* PanePositionCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanePositionCodable.swift; sourceTree = "<group>"; };
D0B3E0A12B8A1F39008EA695 /* AlfredExtraPaneTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AlfredExtraPaneTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D0B3E0A32B8A1F3A008EA695 /* AlfredExtraPaneTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlfredExtraPaneTests.swift; sourceTree = "<group>"; };
D0FCE9402C40049200F5006C /* Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -116,6 +118,7 @@
D01646B9255AF27A00646F0C /* utils.swift */,
D08B6EE4266F31790099EB36 /* Pane.swift */,
D08B6EE6266F9B520099EB36 /* PanePositionCodable.swift */,
D0FCE9402C40049200F5006C /* Menu.swift */,
);
path = AlfredExtraPane;
sourceTree = "<group>";
Expand Down Expand Up @@ -245,6 +248,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D0FCE9412C40049200F5006C /* Menu.swift in Sources */,
D03EA8D325551E0400D3656E /* main.swift in Sources */,
D08B6EE5266F31790099EB36 /* Pane.swift in Sources */,
D04F70D5255893B3008E17A4 /* NSColorExtension.swift in Sources */,
Expand Down
97 changes: 97 additions & 0 deletions AlfredExtraPane/Menu.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Alfred
import AppKit
import Foundation

extension AppDelegate {
func setupMenubarExtra() {
let appName =
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
statusItem =
NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem?.button {
let image = NSImage(
systemSymbolName: "sidebar.right",
accessibilityDescription: appName + " Menu"
)
image?.isTemplate = true
button.image = image
button.toolTip = appName
}

let menu = NSMenu()

// Menu for configuring panes
let configureMenu = NSMenu()
let globalConfigMenuItem = NSMenuItem(
title: "Global",
action: #selector(openFileAction(_:)),
keyEquivalent: ""
)
globalConfigMenuItem.representedObject = try? globalConfigFile()
configureMenu.addItem(globalConfigMenuItem)
configureMenu.addItem(NSMenuItem.separator())
for workflow in Alfred.workflows().sorted(by: {$0.name < $1.name}) {
let menuItem = NSMenuItem(
title: workflow.name,
action: #selector(openFileAction(_:)),
keyEquivalent: ""
)
menuItem.representedObject =
workflow.dir.appendingPathComponent(workflowConfigFilename)
configureMenu.addItem(menuItem)
}
let configureMenuItem = NSMenuItem(
title: "Configure",
action: nil,
keyEquivalent: ""
)
configureMenuItem.submenu = configureMenu
menu.addItem(configureMenuItem)

menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(
title: "Check for Updates",
action: #selector(checkForUpdates),
keyEquivalent: ""
))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(
title: "Restart " + appName,
action: #selector(restart),
keyEquivalent: ""
))
menu.addItem(NSMenuItem(
title: "Quit " + appName,
action: #selector(quit),
keyEquivalent: "q"
))

statusItem?.menu = menu
}

@objc func checkForUpdates() {
updaterController.checkForUpdates(nil)
}

@objc func restart() {
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [Bundle.main.bundlePath]
task.launch()
NSApplication.shared.terminate(nil)
}

@objc func quit() {
NSApplication.shared.terminate(nil)
}

@objc func openFileAction(_ sender: NSMenuItem) {
if let filePath = sender.representedObject as? URL {
if !fs.fileExists(atPath: filePath.path) {
let empty: [PaneConfig] = []
write(empty, to: filePath)
}
openFile(atPath: filePath.path)
}
}
}
52 changes: 0 additions & 52 deletions AlfredExtraPane/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,58 +87,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
}

func setupMenubarExtra() {
let appName =
Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
statusItem =
NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem?.button {
let image = NSImage(
systemSymbolName: "sidebar.right",
accessibilityDescription: appName + " Menu"
)
image?.isTemplate = true
button.image = image
button.toolTip = appName
}

let menu = NSMenu()
menu.addItem(NSMenuItem(
title: "Check for Updates",
action: #selector(checkForUpdates),
keyEquivalent: ""
))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(
title: "Restart " + appName,
action: #selector(restart),
keyEquivalent: ""
))
menu.addItem(NSMenuItem(
title: "Quit " + appName,
action: #selector(quit),
keyEquivalent: "q"
))

statusItem?.menu = menu
}

@objc func checkForUpdates() {
updaterController.checkForUpdates(nil)
}

@objc func restart() {
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [Bundle.main.bundlePath]
task.launch()
NSApplication.shared.terminate(nil)
}

@objc func quit() {
NSApplication.shared.terminate(nil)
}
}

autoreleasepool {
Expand Down
7 changes: 7 additions & 0 deletions AlfredExtraPane/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func readFile<T>(named: String, then: (String) -> T) -> T? {
}
}

func openFile(atPath path: String) {
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
}

// src: https://stackoverflow.com/a/26406426
extension URL {
var queryParameters: QueryParameters { return QueryParameters(url: self) }
Expand Down

0 comments on commit 13bd784

Please sign in to comment.