mirror of
https://github.com/swiftbar/SwiftBar.git
synced 2024-09-11 07:55:23 +03:00
Add folder support in Shortcuts selection screen #344
This commit is contained in:
parent
f6b5995325
commit
471082583e
@ -27,6 +27,7 @@ class PreferencesStore: ObservableObject {
|
||||
static let shared = PreferencesStore()
|
||||
enum PreferencesKeys: String {
|
||||
case PluginDirectory
|
||||
case ShortcutsFolder
|
||||
case DisabledPlugins
|
||||
case Terminal
|
||||
case Shell
|
||||
@ -52,6 +53,12 @@ class PreferencesStore: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
@Published var shortcutsFolder: String {
|
||||
didSet {
|
||||
PreferencesStore.setValue(value: shortcutsFolder, key: .ShortcutsFolder)
|
||||
}
|
||||
}
|
||||
|
||||
var pluginDirectoryResolvedURL: URL? {
|
||||
guard let path = pluginDirectoryPath as NSString? else { return nil }
|
||||
return URL(fileURLWithPath: path.expandingTildeInPath).resolvingSymlinksInPath()
|
||||
@ -144,6 +151,7 @@ class PreferencesStore: ObservableObject {
|
||||
|
||||
init() {
|
||||
pluginDirectoryPath = PreferencesStore.getValue(key: .PluginDirectory) as? String
|
||||
shortcutsFolder = PreferencesStore.getValue(key: .ShortcutsFolder) as? String ?? ""
|
||||
disabledPlugins = PreferencesStore.getValue(key: .DisabledPlugins) as? [PluginID] ?? []
|
||||
terminal = .Terminal
|
||||
shell = .Bash
|
||||
|
@ -73,7 +73,7 @@ struct ShortcutPluginsPreferencesView: View {
|
||||
showingSheet.toggle()
|
||||
}
|
||||
.sheet(isPresented: $showingSheet) {
|
||||
AddShortcutPluginView(pluginManager: pluginManager, isPresented: $showingSheet, shortcutsManager: ShortcutsManager.shared)
|
||||
AddShortcutPluginView(pluginManager: pluginManager, isPresented: $showingSheet)
|
||||
}
|
||||
}.padding([.trailing, .leading], 20)
|
||||
.padding(.bottom, 10)
|
||||
@ -90,42 +90,52 @@ struct AddShortcutPluginView: View {
|
||||
@State var refreshValue: String = "1"
|
||||
|
||||
@State var refreshUnit: String = "s"
|
||||
var shortcutsManager: ShortcutsManager
|
||||
@ObservedObject var shortcutsManager = ShortcutsManager.shared
|
||||
@ObservedObject var prefs = PreferencesStore.shared
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Add Plugin")
|
||||
.font(.headline)
|
||||
Group {
|
||||
HStack {
|
||||
Text("Name:")
|
||||
TextField("Unique Plugin Name...", text: $name)
|
||||
.frame(width: 150)
|
||||
Picker("Shortcut:", selection: $selectedShortcut, content: {
|
||||
ForEach(shortcutsManager.shortcuts, id: \.self) { shortcut in
|
||||
Text(shortcut)
|
||||
}
|
||||
})
|
||||
HStack(spacing: 0) {
|
||||
Button(action: {
|
||||
shortcutsManager.getShortcuts()
|
||||
}, label: {
|
||||
Image(systemName: "arrow.triangle.2.circlepath")
|
||||
}).help("Refresh Shortcuts List")
|
||||
|
||||
if !selectedShortcut.isEmpty {
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Name:")
|
||||
TextField("Unique Plugin Name...", text: $name)
|
||||
Spacer()
|
||||
}
|
||||
HStack {
|
||||
Picker("Folder:", selection: $prefs.shortcutsFolder, content: {
|
||||
ForEach(shortcutsManager.folders, id: \.self) { shortcut in
|
||||
Text(shortcut)
|
||||
}
|
||||
})
|
||||
Picker("Shortcut:", selection: $selectedShortcut, content: {
|
||||
ForEach(shortcutsManager.shortcuts, id: \.self) { shortcut in
|
||||
Text(shortcut)
|
||||
}
|
||||
})
|
||||
HStack(spacing: 0) {
|
||||
Button(action: {
|
||||
shortcutsManager.viewCurrentShortcut(shortcut: selectedShortcut)
|
||||
shortcutsManager.refresh()
|
||||
}, label: {
|
||||
Image(systemName: "slider.horizontal.3")
|
||||
}).help("Open in Shortcuts.app")
|
||||
}
|
||||
Image(systemName: "arrow.triangle.2.circlepath")
|
||||
}).help("Refresh Shortcuts List")
|
||||
|
||||
Button(action: {
|
||||
shortcutsManager.createShortcut()
|
||||
}, label: {
|
||||
Image(systemName: "plus")
|
||||
}).help("Create New Shortcut")
|
||||
if !selectedShortcut.isEmpty {
|
||||
Button(action: {
|
||||
shortcutsManager.viewCurrentShortcut(shortcut: selectedShortcut)
|
||||
}, label: {
|
||||
Image(systemName: "slider.horizontal.3")
|
||||
}).help("Open in Shortcuts.app")
|
||||
}
|
||||
|
||||
Button(action: {
|
||||
shortcutsManager.createShortcut()
|
||||
}, label: {
|
||||
Image(systemName: "plus")
|
||||
}).help("Create New Shortcut")
|
||||
}
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
@ -169,7 +179,7 @@ struct ShortcutPluginsPreferencesView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
ShortcutPluginsPreferencesView(pluginManager: PluginManager.shared)
|
||||
AddShortcutPluginView(pluginManager: PluginManager.shared, isPresented: $isPresented, shortcutsManager: ShortcutsManager.shared)
|
||||
AddShortcutPluginView(pluginManager: PluginManager.shared, isPresented: $isPresented)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import AppKit
|
||||
import Combine
|
||||
import Foundation
|
||||
import ScriptingBridge
|
||||
|
||||
@ -31,8 +32,11 @@ public class ShortcutsManager: ObservableObject {
|
||||
var task: Process?
|
||||
var shortcutsURL = URL(fileURLWithPath: "/usr/bin/shortcuts")
|
||||
var shellURL = URL(fileURLWithPath: "/bin/zsh")
|
||||
var prefs = PreferencesStore.shared
|
||||
var cancellable: AnyCancellable?
|
||||
|
||||
@Published public var shortcuts: [String] = []
|
||||
@Published public var folders: [String] = []
|
||||
|
||||
lazy var shortcutInputPath: URL = {
|
||||
let directory = NSTemporaryDirectory()
|
||||
@ -41,14 +45,18 @@ public class ShortcutsManager: ObservableObject {
|
||||
|
||||
public init() {
|
||||
if #available(macOS 12, *) {
|
||||
getShortcutsFolders()
|
||||
getShortcuts()
|
||||
cancellable = prefs.$shortcutsFolder.receive(on: RunLoop.main).sink { [weak self] folder in
|
||||
self?.getShortcuts(folder: folder)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func getShortcuts() {
|
||||
public func getShortcuts(folder: String? = nil) {
|
||||
task = Process()
|
||||
task?.executableURL = shortcutsURL
|
||||
task?.arguments = ["list"]
|
||||
task?.arguments = ["list", "-f", "\(folder ?? prefs.shortcutsFolder)"]
|
||||
|
||||
let pipe = Pipe()
|
||||
task?.standardOutput = pipe
|
||||
@ -60,6 +68,20 @@ public class ShortcutsManager: ObservableObject {
|
||||
shortcuts = output.components(separatedBy: .newlines).sorted()
|
||||
}
|
||||
|
||||
public func getShortcutsFolders() {
|
||||
task = Process()
|
||||
task?.executableURL = shortcutsURL
|
||||
task?.arguments = ["list", "--folders"]
|
||||
|
||||
let pipe = Pipe()
|
||||
task?.standardOutput = pipe
|
||||
task?.launch()
|
||||
task?.waitUntilExit()
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output = String(data: data, encoding: .utf8) ?? ""
|
||||
folders = output.components(separatedBy: .newlines).sorted()
|
||||
}
|
||||
|
||||
public func runShortcut(shortcut: String, input: Any? = nil) throws -> String {
|
||||
guard let app: ShortcutsEvents? = SBApplication(bundleIdentifier: "com.apple.shortcuts.events") else {
|
||||
throw RunShortcutError(errorReason: .NoPermissions, message: "Can't access Shortcuts.app, please verify the permissions")
|
||||
@ -91,4 +113,9 @@ public class ShortcutsManager: ObservableObject {
|
||||
public func createShortcut() {
|
||||
NSWorkspace.shared.open(URL(string: "shortcuts://create-shortcut")!)
|
||||
}
|
||||
|
||||
public func refresh() {
|
||||
getShortcutsFolders()
|
||||
getShortcuts()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user