fix(macos): route keyboard through NSTextInputContext when a text input has focus#226
Open
paravozz wants to merge 1 commit intoRustAudio:masterfrom
Open
Conversation
2126dc3 to
2ebd0f7
Compare
…ut has focus Adopts NSTextInputClient on the custom NSView and routes `keyDown:` through `[[self inputContext] handleEvent:]` when the application declares a text input has focus (new `WindowHandler::has_text_focus` trait method, default `false`). Adds a matching `performKeyEquivalent:` override, and activates / deactivates the input context on `becomeFirstResponder` / `resignFirstResponder`. Before this, plugins hosting text inputs on macOS produced a system beep per keypress, couldn't use IME or dead-key composition, and lost keys to the host's accelerator table even when the host was configured to forward them to the plugin. Apps that do not override `has_text_focus()` get the old behaviour. The opt-in avoids changing key semantics for non-text-input views (game controls etc.) that rely on the raw-keycode path. Companion to vizia/vizia#630 and #631 on the consumer side. Addresses vizia/vizia-plug#7.
2ebd0f7 to
421bbfa
Compare
This file contains hidden or 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
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.
Summary
baseview on macOS delivers keyboard input via
keyDown:on its custom NSView subclass, translating the NSEvent's keyCode andcharactersinto akeyboard-types::KeyboardEvent. This works for Latin letters but has several gaps for plugin hosting on macOS:Standard Cocoa text views don't exhibit any of this because AppKit routes their keys through
NSTextInputContext, triggeringinsertText:replacementRange:/doCommandBySelector:callbacks. JUCE plugin views adopt the same pattern. This patch brings baseview's custom NSView into that pipeline when the app declares a text input is focused.Change
New
WindowHandler::has_text_focus()trait method, defaultfalse. Apps override it when a focused widget expects text input.src/macos/view.rs:NSTextInputClientprotocol conformance + the method suite.insertText:replacementRange:anddoCommandBySelector:dispatch aKeyboardEventby re-processing the stored NSEvent; the rest return sentinels.keyDown:that routes through[[self inputContext] handleEvent:]whenhas_text_focus()is true; falls back to the existing raw-keycode path when false.performKeyEquivalent:override routing through the same input-context path — AppKit calls this beforekeyDown:for potential menu shortcuts.[inputContext activate]/deactivateinbecomeFirstResponder/resignFirstResponder. Marks our input context as globally active while the view has focus;handleEvent:requires this.src/macos/window.rs:WindowState.current_key_eventholds the in-flight NSEvent so NSTextInputClient callbacks can access the original keyCode / modifiers.WindowState::has_text_focus()helper that queries theWindowHandler.Why this shape
has_text_focus()is an app-level opt-in because baseview doesn't know what a "text input" is in the consumer's model. The consumer knows; baseview asks at dispatch time.process_native_eventalready does the rightkeyCode → Codemapping; we just dispatch throughtrigger_eventat the moment AppKit finishes decoding.Test plan
vizia::Textboxin a nih-plug + vizia-plug plugin works end-to-end with no system beep.keyDown:path is preserved — host shortcuts like space = transport continue to reach the DAW.has_text_focus()see identical behaviour to pre-patch.Context