Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as SwiftUI .keyboardShortcut() helper #69

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions Sources/KeyboardShortcuts/Shortcut.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Cocoa
import Carbon.HIToolbox

import SwiftUI

extension KeyboardShortcuts {
/**
A keyboard shortcut.
Expand Down Expand Up @@ -292,3 +294,59 @@ extension KeyboardShortcuts.Shortcut: CustomStringConvertible {
modifiers.description + (keyToCharacter()?.uppercased() ?? "�")
}
}

// MARK: - SwiftUI Helpers

extension KeyboardShortcuts.Shortcut {

@available(iOS 14.0, macOS 11.0, *)
var swiftUI: SwiftUI.KeyboardShortcut {
.init(.init(keyEquivalent.first!), modifiers: modifiers.swiftUI)
}

}

@available(macOS 10.15, *)
extension NSEvent.ModifierFlags {
var swiftUI: SwiftUI.EventModifiers {
var modifiers: SwiftUI.EventModifiers = []
if contains(.shift) {
modifiers.insert(.shift)
}
if contains(.command) {
modifiers.insert(.command)
}
if contains(.capsLock) {
modifiers.insert(.capsLock)
}
if contains(.function) {
modifiers.insert(.function)
}
if contains(.option) {
modifiers.insert(.option)
}
if contains(.control) {
modifiers.insert(.control)
}
return modifiers
}
}

@available(iOS 14.0, macOS 11.0, *)
extension View {

@ViewBuilder
/// Assigns the global keyboard shortcut to the modified control.
///
/// Only assigns a keyboard shortcut, if one was defined (or it has a default shortcut).
///
/// - Parameter shortcut: Strongly-typed name of the shortcut
public func keyboardShortcut(_ shortcut: KeyboardShortcuts.Name) -> some View {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the shortcut attached to KeyboardShortcuts.Name is changed by the user, it will not be changed here. You need to use a ViewModifier and listen to updates to the keyboard shortcut, like the NSMenuItem extension does.

Copy link
Author

@nighthawk nighthawk Feb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected so, too, but that's not necessary in my testing. As soon as I assign a new keyboard shortcut using the Recorder, the .commands is re-evaluated as this method will be called again.

I'm not sure how that works though, but empirically it works.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that only applies if they're in the same view. But that's not the case if they are in different windows (preferences vs main app window).

if let shortcut = (shortcut.shortcut ?? shortcut.defaultShortcut)?.swiftUI {
self.keyboardShortcut(shortcut)
} else {
self
}
}
nighthawk marked this conversation as resolved.
Show resolved Hide resolved

}