mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-10-01 23:48:35 +00:00
macOS: use KeyboardShortcut rather than homegrown KeyEquivalent
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.
This commit is contained in:
27
macos/Sources/Helpers/EventModifiers+Extension.swift
Normal file
27
macos/Sources/Helpers/EventModifiers+Extension.swift
Normal file
@@ -0,0 +1,27 @@
|
||||
import SwiftUI
|
||||
|
||||
// MARK: EventModifiers to NSEvent and Back
|
||||
|
||||
extension EventModifiers {
|
||||
init(nsFlags: NSEvent.ModifierFlags) {
|
||||
var result: SwiftUI.EventModifiers = []
|
||||
if nsFlags.contains(.shift) { result.insert(.shift) }
|
||||
if nsFlags.contains(.control) { result.insert(.control) }
|
||||
if nsFlags.contains(.option) { result.insert(.option) }
|
||||
if nsFlags.contains(.command) { result.insert(.command) }
|
||||
if nsFlags.contains(.capsLock) { result.insert(.capsLock) }
|
||||
self = result
|
||||
}
|
||||
}
|
||||
|
||||
extension NSEvent.ModifierFlags {
|
||||
init(swiftUIFlags: SwiftUI.EventModifiers) {
|
||||
var result: NSEvent.ModifierFlags = []
|
||||
if swiftUIFlags.contains(.shift) { result.insert(.shift) }
|
||||
if swiftUIFlags.contains(.control) { result.insert(.control) }
|
||||
if swiftUIFlags.contains(.option) { result.insert(.option) }
|
||||
if swiftUIFlags.contains(.command) { result.insert(.command) }
|
||||
if swiftUIFlags.contains(.capsLock) { result.insert(.capsLock) }
|
||||
self = result
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user