From 445787a0dec63e4422b18c07a2b720f4cf2bbd13 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 18 Mar 2024 15:46:41 +0900 Subject: [PATCH 1/2] [macOS] Consolidate static functions in one block No semantic changes, just consolidates static functions in FlutterViewController.mm local to the translation unit into a single contiguous block at the top. Also includes two minor (non-semantic) changes: * Corrects static function naming to UpperCamelCase * Adds a mark to help highlight the static data/function block at the top of the file in Xcode. --- .../framework/Source/FlutterViewController.mm | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index 3dfe4591c1233..cd98930d5400a 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 data and functions. + namespace { using flutter::KeyboardLayoutNotifier; using flutter::LayoutClue; @@ -131,6 +133,20 @@ void Reset() { } }; +/** + * NotificationCenter callback invoked on kTISNotifySelectedKeyboardInputSourceChanged events. + */ +void OnKeyboardLayoutChanged(CFNotificationCenterRef center, + void* observer, + CFStringRef name, + const void* object, + CFDictionaryRef userInfo) { + FlutterViewController* controller = (__bridge FlutterViewController*)observer; + if (controller != nil) { + [controller onKeyboardLayoutChanged]; + } +} + /** * Returns the current Unicode layout data (kTISPropertyUnicodeKeyLayoutData). * @@ -138,7 +154,7 @@ void Reset() { * with CFDataGetBytePtr, then reinterpret it into const UCKeyboardLayout*. * It's returned in NSData* to enable auto reference count. */ -NSData* currentKeyboardLayoutData() { +NSData* CurrentKeyboardLayoutData() { TISInputSourceRef source = TISCopyCurrentKeyboardInputSource(); CFTypeRef layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData); if (layout_data == nil) { @@ -249,21 +265,6 @@ - (void)onKeyboardLayoutChanged; @end -#pragma mark - Private dependant functions - -namespace { -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 { @@ -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)); From 54de06f4055dc274e4ed7b9817fce2bff776491b Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 26 Mar 2024 13:17:41 -0700 Subject: [PATCH 2/2] Update static func locs --- .../framework/Source/FlutterViewController.mm | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index cd98930d5400a..291340b3764ac 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm @@ -19,7 +19,7 @@ #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h" #import "flutter/shell/platform/embedder/embedder.h" -#pragma mark - Static data and functions. +#pragma mark - Static types and data. namespace { using flutter::KeyboardLayoutNotifier; @@ -133,41 +133,6 @@ void Reset() { } }; -/** - * NotificationCenter callback invoked on kTISNotifySelectedKeyboardInputSourceChanged events. - */ -void OnKeyboardLayoutChanged(CFNotificationCenterRef center, - void* observer, - CFStringRef name, - const void* object, - CFDictionaryRef userInfo) { - FlutterViewController* controller = (__bridge FlutterViewController*)observer; - if (controller != nil) { - [controller onKeyboardLayoutChanged]; - } -} - -/** - * 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. @@ -267,6 +232,20 @@ - (void)onKeyboardLayoutChanged; #pragma mark - FlutterViewWrapper implementation. +/** + * 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]; + } +} + @implementation FlutterViewWrapper { FlutterView* _flutterView; __weak FlutterViewController* _controller; @@ -903,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 {