Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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<const UCKeyboardLayout*>(
CFDataGetBytePtr((__bridge CFDataRef)_keyboardLayoutData));
Expand Down