mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-15 08:16:13 +03:00

This replaces the use of our custom `Ghostty.KeyEquivalent` with the SwiftUI `KeyboardShortcut` type. This is a more standard way to represent keyboard shortcuts and lets us more tightly integrate with SwiftUI/AppKit when necessary over our custom type. Note that not all Ghostty triggers can be represented as KeyboardShortcut values because macOS itself does not support binding keys such as function keys (e.g. F1-F12) to KeyboardShortcuts. This isn't an issue since all input also passes through a lower level libghostty API which can handle all key events, we just can't show these keyboard shortcuts on things like the menu bar. This was already true before this commit.
46 lines
1.3 KiB
Swift
46 lines
1.3 KiB
Swift
import SwiftUI
|
|
|
|
extension KeyboardShortcut: @retroactive CustomStringConvertible {
|
|
public var description: String {
|
|
var result = ""
|
|
|
|
if modifiers.contains(.command) {
|
|
result.append("⌘")
|
|
}
|
|
if modifiers.contains(.control) {
|
|
result.append("⌃")
|
|
}
|
|
if modifiers.contains(.option) {
|
|
result.append("⌥")
|
|
}
|
|
if modifiers.contains(.shift) {
|
|
result.append("⇧")
|
|
}
|
|
|
|
let keyString: String
|
|
switch key {
|
|
case .return: keyString = "⏎"
|
|
case .escape: keyString = "⎋"
|
|
case .delete: keyString = "⌫"
|
|
case .space: keyString = "␣"
|
|
case .tab: keyString = "⇥"
|
|
case .upArrow: keyString = "↑"
|
|
case .downArrow: keyString = "↓"
|
|
case .leftArrow: keyString = "←"
|
|
case .rightArrow: keyString = "→"
|
|
default:
|
|
keyString = String(key.character)
|
|
}
|
|
|
|
result.append(keyString)
|
|
return result
|
|
}
|
|
}
|
|
|
|
// This is available in macOS 14 so this only applies to early macOS versions.
|
|
extension KeyEquivalent: @retroactive Equatable {
|
|
public static func == (lhs: KeyEquivalent, rhs: KeyEquivalent) -> Bool {
|
|
lhs.character == rhs.character
|
|
}
|
|
}
|