|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Portable catalog of macOS app menu shortcuts. The Cocoa host builds real |
| 4 | +/// `NSMenuItem`s from these specs so chords, selectors, and targets stay one |
| 5 | +/// source. The protocol suite rejects duplicate physical chords. |
| 6 | +public enum MenuShortcutTarget: String, Equatable, Sendable { |
| 7 | + case application |
| 8 | + case appDelegate |
| 9 | + case firstResponder |
| 10 | +} |
| 11 | + |
| 12 | +public struct MenuShortcutSpec: Equatable, Sendable { |
| 13 | + public let menu: String |
| 14 | + public let title: String |
| 15 | + public let key: String |
| 16 | + public let command: Bool |
| 17 | + public let shift: Bool |
| 18 | + public let option: Bool |
| 19 | + public let control: Bool |
| 20 | + public let selector: String |
| 21 | + public let target: MenuShortcutTarget |
| 22 | + public let separatorBefore: Bool |
| 23 | + |
| 24 | + public init( |
| 25 | + menu: String, |
| 26 | + title: String, |
| 27 | + key: String, |
| 28 | + command: Bool = true, |
| 29 | + shift: Bool = false, |
| 30 | + option: Bool = false, |
| 31 | + control: Bool = false, |
| 32 | + selector: String, |
| 33 | + target: MenuShortcutTarget = .firstResponder, |
| 34 | + separatorBefore: Bool = false |
| 35 | + ) { |
| 36 | + self.menu = menu |
| 37 | + self.title = title |
| 38 | + self.key = key |
| 39 | + self.command = command |
| 40 | + self.shift = shift |
| 41 | + self.option = option |
| 42 | + self.control = control |
| 43 | + self.selector = selector |
| 44 | + self.target = target |
| 45 | + self.separatorBefore = separatorBefore |
| 46 | + } |
| 47 | + |
| 48 | + public var chordIdentity: String { |
| 49 | + [ |
| 50 | + command ? "cmd" : nil, |
| 51 | + control ? "ctrl" : nil, |
| 52 | + option ? "opt" : nil, |
| 53 | + shift ? "shift" : nil, |
| 54 | + key.isEmpty ? nil : key.lowercased(), |
| 55 | + ].compactMap { $0 }.joined(separator: "+") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public let headlessMenuShortcuts: [MenuShortcutSpec] = [ |
| 60 | + .init( |
| 61 | + menu: "Headless", title: "Hide Headless", key: "h", selector: "hide:", |
| 62 | + target: .application |
| 63 | + ), |
| 64 | + .init( |
| 65 | + menu: "Headless", title: "Hide Others", key: "h", option: true, |
| 66 | + selector: "hideOtherApplications:", target: .application |
| 67 | + ), |
| 68 | + .init( |
| 69 | + menu: "Headless", title: "Quit Headless", key: "q", selector: "terminate:", |
| 70 | + target: .application |
| 71 | + ), |
| 72 | + .init( |
| 73 | + menu: "File", title: "New Window", key: "n", selector: "newWindow:", |
| 74 | + target: .appDelegate |
| 75 | + ), |
| 76 | + .init(menu: "File", title: "Open Location…", key: "l", selector: "openLocation:"), |
| 77 | + .init( |
| 78 | + menu: "File", title: "Save Snapshot to Desktop", key: "s", shift: true, |
| 79 | + selector: "saveSnapshot:", separatorBefore: true |
| 80 | + ), |
| 81 | + .init( |
| 82 | + menu: "File", title: "Close Window", key: "w", selector: "performClose:", |
| 83 | + separatorBefore: true |
| 84 | + ), |
| 85 | + .init(menu: "Edit", title: "Undo", key: "z", selector: "undo:"), |
| 86 | + .init(menu: "Edit", title: "Redo", key: "z", shift: true, selector: "redo:"), |
| 87 | + .init(menu: "Edit", title: "Cut", key: "x", selector: "cut:", separatorBefore: true), |
| 88 | + .init(menu: "Edit", title: "Copy", key: "c", selector: "copy:"), |
| 89 | + .init(menu: "Edit", title: "Paste", key: "v", selector: "paste:"), |
| 90 | + .init(menu: "Edit", title: "Select All", key: "a", selector: "selectAll:"), |
| 91 | + .init( |
| 92 | + menu: "Edit", title: "Copy Current URL", key: "c", shift: true, |
| 93 | + selector: "copyPageURL:", separatorBefore: true |
| 94 | + ), |
| 95 | + .init(menu: "View", title: "Reload Page", key: "r", selector: "reloadPage:"), |
| 96 | + .init( |
| 97 | + menu: "View", title: "Reload Ignoring Cache", key: "r", shift: true, |
| 98 | + selector: "hardReloadPage:" |
| 99 | + ), |
| 100 | + .init( |
| 101 | + menu: "View", title: "Zoom In", key: "=", selector: "zoomInPage:", |
| 102 | + separatorBefore: true |
| 103 | + ), |
| 104 | + .init(menu: "View", title: "Zoom Out", key: "-", selector: "zoomOutPage:"), |
| 105 | + .init(menu: "View", title: "Actual Size", key: "0", selector: "resetZoom:"), |
| 106 | + .init( |
| 107 | + menu: "View", title: "Enter Full Screen", key: "f", control: true, |
| 108 | + selector: "toggleFullScreen:", separatorBefore: true |
| 109 | + ), |
| 110 | + .init(menu: "History", title: "Back", key: "[", selector: "goBackAction:"), |
| 111 | + .init(menu: "History", title: "Forward", key: "]", selector: "goForwardAction:"), |
| 112 | + .init(menu: "Window", title: "Minimize", key: "m", selector: "performMiniaturize:"), |
| 113 | + .init( |
| 114 | + menu: "Window", title: "Pin on Top", key: "p", option: true, |
| 115 | + selector: "togglePin:", separatorBefore: true |
| 116 | + ), |
| 117 | + .init( |
| 118 | + menu: "Help", title: "Headless Help", key: "/", shift: true, |
| 119 | + selector: "showHelpPage:" |
| 120 | + ), |
| 121 | +] |
0 commit comments