[macOS] Consolidate FlutterViewController static types/data (flutter/engine#51486)

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 three minor (non-semantic) changes:
* Corrects static function naming to UpperCamelCase
* Adds doc comments for `OnKeyboardLayoutChanged`
* Adds a mark to help highlight the static data/function block at the top of the file in Xcode.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Bracken 2024-03-26 14:46:53 -07:00 committed by GitHub
parent 9945d244bb
commit 70867ee4d9

View File

@ -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 @@ struct MouseState {
}
};
/**
* 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 @@ NSData* currentKeyboardLayoutData() {
@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 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
#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 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
- (LayoutClue)lookUpLayoutForKeyCode:(uint16_t)keyCode shift:(BOOL)shift {
if (_keyboardLayoutData == nil) {
_keyboardLayoutData = currentKeyboardLayoutData();
_keyboardLayoutData = CurrentKeyboardLayoutData();
}
const UCKeyboardLayout* layout = reinterpret_cast<const UCKeyboardLayout*>(
CFDataGetBytePtr((__bridge CFDataRef)_keyboardLayoutData));