-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
Fix logic for .globalKeyboardShortcut()
#193
Merged
sindresorhus
merged 4 commits into
sindresorhus:main
from
cweider:key-equivalent-mappings
Dec 7, 2024
Merged
Fix logic for .globalKeyboardShortcut()
#193
sindresorhus
merged 4 commits into
sindresorhus:main
from
cweider:key-equivalent-mappings
Dec 7, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For testing, this is a snippet that you can go and stick any old place:
|
Evaluating `globalKeyboardShortcut(_:)` revealed several issues with related to special keys: - Shortcuts using the `Key.space` would produce a `KeyEquivalent` that specified the "S" key. (cause: `first` being used on the `keyToCharacter()` return value, `"space"`) - Shortcuts using `Key.f*` would produce a `KeyEquivalent` that Specified the "F" key. (cause: same as above) - Shortcuts using `Key.keypad*` would produce a `KeyEquivalent` that was not limited to the number pad. (cause: neither SwiftUI nor AppKit make this distinction when producing menu items) Considering these issues, a reevaluation of special keys' handling is called for. Excepting the `Carbon` integration that drives the actual keyboard shortcuts, there are three distinct contexts where a `Shortcut` is used: - `Recoder`: where an `NSString` is produced to go into the `TextField` displaying the shortcut's current value - `NSMenuItem`: for specifying `keyEquivalent` and `keyEquivalentModifierMask` - `toSwiftUI`: for creating a `SwiftUI.KeyboardShortcut` Ideally, each of these three do their work in analogous ways. This branch accomplishes this with the following: - `SpecialKey` enumerates the cases that require extra attention. This is drawn directly from keys used in `keyToCharacterMapping` and used to ensure that each special case is handled. - Three mappings for `SpecialKey` are created for each of the three contexts where the handling is important: `presentableDescription`, `swiftUIKeyEquivalent`, and `appKitMenuItemKeyEquivalent`. - In these three contexts if there is a `SpecialKey`, the defined mapping is what rules. `keyToCharacter()` is used if and only if there is no `SpecialKey` available. An assertion to this effect is added. Fixes: sindresorhus#192
cweider
force-pushed
the
key-equivalent-mappings
branch
from
November 30, 2024 06:43
c6a0371
to
4bdbf5c
Compare
cweider
commented
Nov 30, 2024
I should add that cweider/KeyboardShortcuts@swiftui-key-equivalent is the minimal change for SwiftUI. That this, |
cweider
commented
Nov 30, 2024
sindresorhus
reviewed
Dec 3, 2024
sindresorhus
reviewed
Dec 3, 2024
This is looking very good. Thanks for working fixing this 🙏 |
Avoid repeating ourselves when creating these objects in a parameterized way (continue using literals where reasonable to get compile-time checks).
cweider
force-pushed
the
key-equivalent-mappings
branch
from
December 7, 2024 03:16
be009d9
to
6e9bed1
Compare
sindresorhus
changed the title
Refactor/fix handling of special keys in integrations
Fix logic for Dec 7, 2024
.globalKeyboardShortcut()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Evaluating
globalKeyboardShortcut(_:)
revealed several issues with related to special keys:Key.space
would produce aKeyEquivalent
that specified the "S" key. (cause:first
being used on thekeyToCharacter()
return value,"space"
)Key.f*
would produce aKeyEquivalent
that specified the "F" key. (cause: same as above)Key.keypad*
would produce aKeyEquivalent
that was not limited to the number pad. (cause: neither SwiftUI nor AppKit make this distinction when producing menu items)Considering these issues, a reevaluation of special keys' handling is called for. Excepting the
Carbon
integration that drives the actual keyboard shortcuts, there are three distinct contexts where aShortcut
is used:Recoder
: where anNSString
is produced to go into theTextField
displaying the shortcut's current valueNSMenuItem
: for specifyingkeyEquivalent
andkeyEquivalentModifierMask
toSwiftUI
: for creating aSwiftUI.KeyboardShortcut
Ideally, each of these three do their work in analogous ways. This branch accomplishes this with the following:
SpecialKey
enumerates the cases that require extra attention. This is drawn directly from keys used inkeyToCharacterMapping
and used to ensure that each special case is handled.SpecialKey
are created for each of the three contexts where the handling is important:presentableDescription
,swiftUIKeyEquivalent
, andappKitMenuItemKeyEquivalent
.SpecialKey
, the defined mapping is what rules.keyToCharacter()
is used if and only if there is noSpecialKey
available. An assertion to this effect is added.Fixes: #192