Skip to content

Commit

Permalink
improve add and launch features
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbeardman committed Aug 12, 2024
1 parent f35b629 commit 1951b49
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Stapler/StaplerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,31 @@ class StaplerViewModel: ObservableObject {
objectWillChange.send()
}

func addAliasesViaFileSelector() {
func addAliasesViaFileSelector() -> Bool {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = true
panel.canChooseDirectories = false
panel.canChooseFiles = true

if panel.runModal() == .OK {
var addedAliases = false
for url in panel.urls {
do {
let newAlias = try AliasItem(url: url)
addAlias(newAlias)
addedAliases = true
} catch {
handleError(error)
}
}

if addedAliases {
hasUnsavedChanges = true
objectWillChange.send()
return true
}
}
return false
}

func handleError(_ error: Error) {
Expand Down Expand Up @@ -341,7 +350,6 @@ struct ContentView: View {
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in
if event.keyCode == 36 { // Return key
launchSelected()
return nil
} else if event.keyCode == 49 { // Space key
showQuickLook()
return nil
Expand Down Expand Up @@ -425,8 +433,9 @@ struct ContentView: View {

private func setupNotificationObservers() {
NotificationCenter.default.addObserver(forName: .addAlias, object: nil, queue: .main) { _ in
viewModel.addAliasesViaFileSelector()
updateDocument()
if viewModel.addAliasesViaFileSelector() {
updateDocument()
}
}
NotificationCenter.default.addObserver(forName: .removeAlias, object: nil, queue: .main) { _ in
removeSelectedAliases()
Expand Down Expand Up @@ -455,6 +464,8 @@ struct ContentView: View {
}

private func launchSelected() {
guard !NSEvent.modifierFlags.contains(.command) else { return }

if selection.isEmpty {
// If no items are selected, launch all items
viewModel.launchAliases(at: IndexSet(integersIn: 0..<viewModel.document.aliases.count))
Expand Down

0 comments on commit 1951b49

Please sign in to comment.