-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
Conversation
/// 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
83af2c0
to
0dcedd5
Compare
@nighthawk Still interested in working or this or should I close? |
Excuse the slow reply. I didn't yet have time to look into how to do it using a |
Closing for lack of activity. #101 |
This adds a
.keyboardShortcut(_ shortcut: KeyboardShortcuts.Name)
helper function to assign a shortcut defined through thisKeyboardShortcuts
to a SwiftUI button. This provides a similar functionality to the existingNSMenuItem
extension, but when using SwiftUI'sCommands
to define your menu items.This can be used as follows:
In my testing on macOS 12 this works well without having to disable the global keyboard shortcut while the menu is visible, but I'm not really sure if it's safe to do so.