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

Conversation

nighthawk
Copy link

This adds a .keyboardShortcut(_ shortcut: KeyboardShortcuts.Name) helper function to assign a shortcut defined through this KeyboardShortcuts to a SwiftUI button. This provides a similar functionality to the existing NSMenuItem extension, but when using SwiftUI's Commands to define your menu items.

This can be used as follows:

@main
struct MyApp: App {
  
  var body: some Scene {
    WindowGroup {
      ...
    }
    .commands {
      CommandMenu("Unicorns") {
        Button("Toggle Unicorn Mode") { ... }
        .keyboardShortcut(.toggleUnicornMode)
      }
    }
  }

}

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.

/// 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).

Sources/KeyboardShortcuts/Shortcut.swift Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

@nighthawk Still interested in working or this or should I close?

@nighthawk
Copy link
Author

Excuse the slow reply. I didn't yet have time to look into how to do it using a ViewModifier and haven't yet had any urgency for it as it works as is for how I use it even though I use it across different views, i.e., the app's settings view (to set it) and the app's main window's menu item (to display and trigger it).

@sindresorhus
Copy link
Owner

Closing for lack of activity. #101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants