Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0f52360

Browse files
authored
Hardware Keyboard: macOS (#23469)
The macOS changes for the Hardware Keyboard project.
1 parent 6a3d8c7 commit 0f52360

24 files changed

+3226
-230
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,15 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBacki
11051105
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.mm
11061106
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h
11071107
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm
1108+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.h
1109+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm
1110+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm
11081111
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
11091112
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h
11101113
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm
1114+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.h
1115+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm
1116+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponderUnittests.mm
11111117
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
11121118
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm
11131119
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h
@@ -1122,8 +1128,11 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCom
11221128
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositorUnittests.mm
11231129
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h
11241130
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.mm
1125-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIntermediateKeyResponder.h
1126-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIntermediateKeyResponder.mm
1131+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyPrimaryResponder.h
1132+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeySecondaryResponder.h
1133+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.h
1134+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm
1135+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerUnittests.mm
11271136
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMacOSExternalTexture.h
11281137
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h
11291138
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm
@@ -1158,6 +1167,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewC
11581167
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h
11591168
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.mm
11601169
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h
1170+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/KeyCodeMap.mm
1171+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/KeyCodeMap_internal.h
11611172
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h
11621173
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.mm
11631174
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/fixtures/flutter_desktop_test.dart

shell/platform/darwin/macos/BUILD.gn

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ source_set("flutter_framework_source") {
5959
"framework/Source/FlutterBackingStore.mm",
6060
"framework/Source/FlutterBackingStoreData.h",
6161
"framework/Source/FlutterBackingStoreData.mm",
62+
"framework/Source/FlutterChannelKeyResponder.h",
63+
"framework/Source/FlutterChannelKeyResponder.mm",
6264
"framework/Source/FlutterDartProject.mm",
6365
"framework/Source/FlutterDartProject_Internal.h",
66+
"framework/Source/FlutterEmbedderKeyResponder.h",
67+
"framework/Source/FlutterEmbedderKeyResponder.mm",
6468
"framework/Source/FlutterEngine.mm",
6569
"framework/Source/FlutterEngine_Internal.h",
6670
"framework/Source/FlutterExternalTextureGL.h",
@@ -73,8 +77,10 @@ source_set("flutter_framework_source") {
7377
"framework/Source/FlutterGLCompositor.mm",
7478
"framework/Source/FlutterIOSurfaceHolder.h",
7579
"framework/Source/FlutterIOSurfaceHolder.mm",
76-
"framework/Source/FlutterIntermediateKeyResponder.h",
77-
"framework/Source/FlutterIntermediateKeyResponder.mm",
80+
"framework/Source/FlutterKeyPrimaryResponder.h",
81+
"framework/Source/FlutterKeySecondaryResponder.h",
82+
"framework/Source/FlutterKeyboardManager.h",
83+
"framework/Source/FlutterKeyboardManager.mm",
7884
"framework/Source/FlutterMacOSExternalTexture.h",
7985
"framework/Source/FlutterMacOSExternalTexture.h",
8086
"framework/Source/FlutterMetalRenderer.h",
@@ -102,6 +108,7 @@ source_set("flutter_framework_source") {
102108
"framework/Source/FlutterView.mm",
103109
"framework/Source/FlutterViewController.mm",
104110
"framework/Source/FlutterViewController_Internal.h",
111+
"framework/Source/KeyCodeMap.mm",
105112
"framework/Source/MacOSGLContextSwitch.h",
106113
"framework/Source/MacOSGLContextSwitch.mm",
107114
]
@@ -158,9 +165,12 @@ executable("flutter_desktop_darwin_unittests") {
158165
testonly = true
159166

160167
sources = [
168+
"framework/Source/FlutterChannelKeyResponderUnittests.mm",
161169
"framework/Source/FlutterEmbedderExternalTextureUnittests.mm",
170+
"framework/Source/FlutterEmbedderKeyResponderUnittests.mm",
162171
"framework/Source/FlutterEngineTest.mm",
163172
"framework/Source/FlutterGLCompositorUnittests.mm",
173+
"framework/Source/FlutterKeyboardManagerUnittests.mm",
164174
"framework/Source/FlutterMetalRendererTest.mm",
165175
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
166176
"framework/Source/FlutterOpenGLRendererTest.mm",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyPrimaryResponder.h"
6+
7+
#import <Cocoa/Cocoa.h>
8+
9+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
10+
11+
/**
12+
* A primary responder of |FlutterKeyboardManager| that handles events by
13+
* sending the raw information through the method channel.
14+
*
15+
* This class corresponds to the RawKeyboard API in the framework.
16+
*/
17+
@interface FlutterChannelKeyResponder : NSObject <FlutterKeyPrimaryResponder>
18+
19+
/**
20+
* Create an instance by specifying the method channel to use.
21+
*/
22+
- (nonnull instancetype)initWithChannel:(nonnull FlutterBasicMessageChannel*)channel;
23+
24+
@end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <objc/message.h>
6+
7+
#import "FlutterChannelKeyResponder.h"
8+
#import "KeyCodeMap_internal.h"
9+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
10+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
11+
#import "flutter/shell/platform/embedder/embedder.h"
12+
13+
@interface FlutterChannelKeyResponder ()
14+
15+
/**
16+
* The channel used to communicate with Flutter.
17+
*/
18+
@property(nonatomic) FlutterBasicMessageChannel* channel;
19+
20+
/**
21+
* The |NSEvent.modifierFlags| of the last event received.
22+
*
23+
* Used to determine whether a FlagsChanged event should count as a keydown or
24+
* a keyup event.
25+
*/
26+
@property(nonatomic) uint64_t previouslyPressedFlags;
27+
28+
@end
29+
30+
@implementation FlutterChannelKeyResponder
31+
32+
- (nonnull instancetype)initWithChannel:(nonnull FlutterBasicMessageChannel*)channel {
33+
self = [super init];
34+
if (self != nil) {
35+
_channel = channel;
36+
_previouslyPressedFlags = 0;
37+
}
38+
return self;
39+
}
40+
41+
- (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
42+
NSString* type;
43+
switch (event.type) {
44+
case NSEventTypeKeyDown:
45+
type = @"keydown";
46+
break;
47+
case NSEventTypeKeyUp:
48+
type = @"keyup";
49+
break;
50+
case NSEventTypeFlagsChanged:
51+
if (event.modifierFlags < _previouslyPressedFlags) {
52+
type = @"keyup";
53+
} else {
54+
type = @"keydown";
55+
}
56+
break;
57+
default:
58+
NSAssert(false, @"Unexpected key event type (got %lu).", event.type);
59+
}
60+
_previouslyPressedFlags = event.modifierFlags;
61+
NSMutableDictionary* keyMessage = [@{
62+
@"keymap" : @"macos",
63+
@"type" : type,
64+
@"keyCode" : @(event.keyCode),
65+
@"modifiers" : @(event.modifierFlags),
66+
} mutableCopy];
67+
// Calling these methods on any other type of event
68+
// (e.g NSEventTypeFlagsChanged) will raise an exception.
69+
if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp) {
70+
keyMessage[@"characters"] = event.characters;
71+
keyMessage[@"charactersIgnoringModifiers"] = event.charactersIgnoringModifiers;
72+
}
73+
[_channel sendMessage:keyMessage
74+
reply:^(id reply) {
75+
if (!reply) {
76+
return callback(true);
77+
}
78+
// Only propagate the event to other responders if the framework didn't handle
79+
// it.
80+
callback([[reply valueForKey:@"handled"] boolValue]);
81+
}];
82+
}
83+
84+
#pragma mark - Private
85+
86+
@end

0 commit comments

Comments
 (0)