WIP: add panel command shortcuts

This commit is contained in:
Akshay Kolli
2026-06-30 01:38:00 -07:00
parent 800caebb83
commit 3a2078c599
2 changed files with 49 additions and 0 deletions

View File

@@ -12,6 +12,12 @@ struct ClipboardPanelReflowPlan {
let bottomSafeInset: CGFloat
}
enum ClipboardPanelShortcutAction: Equatable {
case copy
case open
case reveal
}
final class ClipboardPanelController: NSObject, NSWindowDelegate {
private enum Animation {
static let showDuration: TimeInterval = 0.16
@@ -317,6 +323,11 @@ final class ClipboardPanelController: NSObject, NSWindowDelegate {
self.viewModel.sortMode = mode
return nil
}
if self.shouldHandlePanelKeyEvent(event, allowSearchFieldEditing: true),
let action = Self.commandShortcutAction(forKeyCode: event.keyCode, modifiers: event.modifierFlags) {
self.performShortcutAction(action)
return nil
}
guard self.shouldHandlePanelKeyEvent(event) else { return event }
switch event.keyCode {
case 53:
@@ -343,6 +354,17 @@ final class ClipboardPanelController: NSObject, NSWindowDelegate {
}
}
private func performShortcutAction(_ action: ClipboardPanelShortcutAction) {
switch action {
case .copy:
viewModel.copySelected()
case .open:
viewModel.openSelected()
case .reveal:
viewModel.revealSelected()
}
}
private func shouldHandlePanelKeyEvent(_ event: NSEvent) -> Bool {
shouldHandlePanelKeyEvent(event, allowSearchFieldEditing: false)
}
@@ -369,6 +391,21 @@ final class ClipboardPanelController: NSObject, NSWindowDelegate {
return collectionShortcuts[keyCode]
}
static func commandShortcutAction(forKeyCode keyCode: UInt16, modifiers: NSEvent.ModifierFlags) -> ClipboardPanelShortcutAction? {
let relevantModifiers = modifiers.intersection(.deviceIndependentFlagsMask)
guard relevantModifiers == .command else { return nil }
switch keyCode {
case 8:
return .copy
case 31:
return .open
case 15:
return .reveal
default:
return nil
}
}
#if DEBUG
var debugPanelFrame: NSRect {
panel.frame

View File

@@ -144,4 +144,16 @@ final class ClipboardPanelControllerTests: XCTestCase {
XCTAssertNil(ClipboardPanelController.collectionShortcutMode(forKeyCode: 18, modifiers: [.command, .shift]))
XCTAssertNil(ClipboardPanelController.collectionShortcutMode(forKeyCode: 29, modifiers: .command))
}
func testCommandActionShortcutsMapToSelectedClipActions() {
XCTAssertEqual(ClipboardPanelController.commandShortcutAction(forKeyCode: 8, modifiers: .command), .copy)
XCTAssertEqual(ClipboardPanelController.commandShortcutAction(forKeyCode: 31, modifiers: .command), .open)
XCTAssertEqual(ClipboardPanelController.commandShortcutAction(forKeyCode: 15, modifiers: .command), .reveal)
}
func testCommandActionShortcutsRequireCommandOnlySoSearchTypingIsUntouched() {
XCTAssertNil(ClipboardPanelController.commandShortcutAction(forKeyCode: 8, modifiers: []))
XCTAssertNil(ClipboardPanelController.commandShortcutAction(forKeyCode: 8, modifiers: [.command, .shift]))
XCTAssertNil(ClipboardPanelController.commandShortcutAction(forKeyCode: 9, modifiers: .command))
}
}