Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Sources/Pesty/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,28 @@ final class AppController: NSObject, NSApplicationDelegate {
win.makeKeyAndOrderFront(nil)
}

/// Handles the small set of commands that are only meaningful while the
/// Paste Bar is active. Keeping this separate lets both the local event
/// monitor and NSPanel key-equivalent path consume the same shortcuts.
func handleBarCommandShortcut(_ event: NSEvent) -> Bool {
let flags = event.modifierFlags
guard flags.contains(.command),
flags.contains(.shift),
!flags.contains(.option),
!flags.contains(.control) else { return false }

switch Int(event.keyCode) {
case kVK_ANSI_S:
showSettings()
return true
case kVK_ANSI_P:
togglePestyPause()
return true
default:
return false
}
}

private func startKeyMonitor() {
stopKeyMonitor()
keyMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
Expand All @@ -455,6 +477,8 @@ final class AppController: NSObject, NSApplicationDelegate {
}

private func handleKey(_ event: NSEvent) -> NSEvent? {
if handleBarCommandShortcut(event) { return nil }

let code = Int(event.keyCode)
let flags = event.modifierFlags
let cmd = flags.contains(.command)
Expand Down
8 changes: 6 additions & 2 deletions Sources/Pesty/UI/BarWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ final class BarPanel: NSPanel {

override func performKeyEquivalent(with event: NSEvent) -> Bool {
// AppKit routes Command-key combinations through key equivalents before
// SwiftUI receives a keyDown event. Handle Copy at the panel level as a
// reliable counterpart to the navigation monitor in AppController.
// SwiftUI receives a keyDown event. Handle the bar-local commands at
// the panel level as a reliable counterpart to the navigation monitor
// in AppController.
if AppController.shared.handleBarCommandShortcut(event) {
return true
}
if event.keyCode == kVK_ANSI_C, event.modifierFlags.contains(.command) {
AppController.shared.commandCopy()
return true
Expand Down