diff --git a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index 3dfe4591c1233..291340b3764ac 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm @@ -19,6 +19,8 @@ #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h" #import "flutter/shell/platform/embedder/embedder.h" +#pragma mark - Static types and data. + namespace { using flutter::KeyboardLayoutNotifier; using flutter::LayoutClue; @@ -131,27 +133,6 @@ void Reset() { } }; -/** - * Returns the current Unicode layout data (kTISPropertyUnicodeKeyLayoutData). - * - * To use the returned data, convert it to CFDataRef first, finds its bytes - * with CFDataGetBytePtr, then reinterpret it into const UCKeyboardLayout*. - * It's returned in NSData* to enable auto reference count. - */ -NSData* currentKeyboardLayoutData() { - TISInputSourceRef source = TISCopyCurrentKeyboardInputSource(); - CFTypeRef layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData); - if (layout_data == nil) { - CFRelease(source); - // TISGetInputSourceProperty returns null with Japanese keyboard layout. - // Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return. - // https://github.com/microsoft/node-native-keymap/blob/5f0699ded00179410a14c0e1b0e089fe4df8e130/src/keyboard_mac.mm#L91 - source = TISCopyCurrentKeyboardLayoutInputSource(); - layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData); - } - return (__bridge_transfer NSData*)CFRetain(layout_data); -} - } // namespace #pragma mark - Private interface declaration. @@ -249,22 +230,21 @@ - (void)onKeyboardLayoutChanged; @end -#pragma mark - Private dependant functions +#pragma mark - FlutterViewWrapper implementation. -namespace { -void OnKeyboardLayoutChanged(CFNotificationCenterRef center, - void* observer, - CFStringRef name, - const void* object, - CFDictionaryRef userInfo) { +/** + * NotificationCenter callback invoked on kTISNotifySelectedKeyboardInputSourceChanged events. + */ +static void OnKeyboardLayoutChanged(CFNotificationCenterRef center, + void* observer, + CFStringRef name, + const void* object, + CFDictionaryRef userInfo) { FlutterViewController* controller = (__bridge FlutterViewController*)observer; if (controller != nil) { [controller onKeyboardLayoutChanged]; } } -} // namespace - -#pragma mark - FlutterViewWrapper implementation. @implementation FlutterViewWrapper { FlutterView* _flutterView; @@ -902,6 +882,27 @@ - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey { #pragma mark - FlutterKeyboardViewDelegate +/** + * Returns the current Unicode layout data (kTISPropertyUnicodeKeyLayoutData). + * + * To use the returned data, convert it to CFDataRef first, finds its bytes + * with CFDataGetBytePtr, then reinterpret it into const UCKeyboardLayout*. + * It's returned in NSData* to enable auto reference count. + */ +static NSData* CurrentKeyboardLayoutData() { + TISInputSourceRef source = TISCopyCurrentKeyboardInputSource(); + CFTypeRef layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData); + if (layout_data == nil) { + CFRelease(source); + // TISGetInputSourceProperty returns null with Japanese keyboard layout. + // Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return. + // https://github.com/microsoft/node-native-keymap/blob/5f0699ded00179410a14c0e1b0e089fe4df8e130/src/keyboard_mac.mm#L91 + source = TISCopyCurrentKeyboardLayoutInputSource(); + layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData); + } + return (__bridge_transfer NSData*)CFRetain(layout_data); +} + - (void)sendKeyEvent:(const FlutterKeyEvent&)event callback:(nullable FlutterKeyEventCallback)callback userData:(nullable void*)userData { @@ -922,7 +923,7 @@ - (void)subscribeToKeyboardLayoutChange:(nullable KeyboardLayoutNotifier)callbac - (LayoutClue)lookUpLayoutForKeyCode:(uint16_t)keyCode shift:(BOOL)shift { if (_keyboardLayoutData == nil) { - _keyboardLayoutData = currentKeyboardLayoutData(); + _keyboardLayoutData = CurrentKeyboardLayoutData(); } const UCKeyboardLayout* layout = reinterpret_cast( CFDataGetBytePtr((__bridge CFDataRef)_keyboardLayoutData));