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

On macOS, pass key event to menu on key press #760

Merged
merged 1 commit into from Nov 21, 2022
Merged

On macOS, pass key event to menu on key press #760

merged 1 commit into from Nov 21, 2022

Conversation

wusyong
Copy link
Member

@wusyong wusyong commented Nov 18, 2022

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Docs
  • New Binding issue #___
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change?

  • Yes, and the changes were approved in issue #___
  • No

Checklist

  • When resolving issues, they are referenced in the PR's title (e.g fix: remove a typo, closes #___, #___)
  • A change file is added if any packages will require a version bump due to this PR per the instructions in the readme.
  • I have added a convincing reason for adding this feature, if necessary

Other information

fix tauri-apps/tauri#2626

@wusyong wusyong requested a review from a team as a code owner November 18, 2022 11:03
Comment on lines +298 to +311

// Key event chain is consumed by window and cannot pass to menu.
// So we pass the event to menu if we have one
extern "C" fn key_down(_: &mut Object, _: Sel, event: id) {
unsafe {
let app = cocoa::appkit::NSApp();
let menu: id = msg_send![app, mainMenu];
if !menu.is_null() {
let () = msg_send![menu, performKeyEquivalent: event];
}
}
}
// We need to declare this method to get Command+ key equivalent.
extern "C" fn do_command_by_selector(_: &Object, _: Sel, _: Sel) {}
Copy link
Member

Choose a reason for hiding this comment

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

can this fix be upstream to winit? seems like a valid use-case for any winit app.

Copy link
Member Author

@wusyong wusyong Nov 19, 2022

Choose a reason for hiding this comment

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

winit can call performKeyEquivalent in WinitWindow's sendEvent but I'm not sure if winit team willing to take this workaround. Key event chain in macOS is a mess tbh.

As for workaround for users of winit, I think we can add a NSView as subview and add similar code like above. Or just replace winitWindow with default NSView if they don't care any window events sent from winit like we did before (we can't do this in wry anymore since event like resize has moved to winitView).

Copy link
Member

Choose a reason for hiding this comment

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

winit ships with a default menu so this workaround is valid for their use-case too but we won't know for sure until we open a PR.

@wusyong wusyong merged commit 2e5e138 into dev Nov 21, 2022
@wusyong wusyong deleted the key-eq branch November 21, 2022 08:12
@github-actions github-actions bot mentioned this pull request Nov 21, 2022
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.

MacOS: Keypress on non-input element triggers unsupported key feedback sound.
2 participants