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

Commit 252ba98

Browse files
committed
Do not expose KeyboardManager from controller
1 parent 96859b3 commit 252ba98

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
- (void)handleEvent:(nonnull NSEvent*)event;
4141

4242
/**
43-
* The event currently being redispatched.
43+
* Returns yes if event currently being redispatched.
4444
*
4545
* In some instances (i.e. emoji shortcut) the event may be redelivered by cocoa
4646
* as key equivalent to FlutterTextInput, in which case it shouldn't be
4747
* processed again.
4848
*/
49-
@property(nonatomic, nullable) NSEvent* eventBeingDispatched;
49+
- (BOOL)isDispatchingKeyEvent:(nonnull NSEvent*)event;
5050

5151
@end

shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ @interface FlutterKeyboardManager ()
7070

7171
@property(nonatomic) NSMutableDictionary<NSNumber*, NSNumber*>* layoutMap;
7272

73+
@property(nonatomic, nullable) NSEvent* eventBeingDispatched;
74+
7375
/**
7476
* Add a primary responder, which asynchronously decides whether to handle an
7577
* event.
@@ -168,6 +170,10 @@ - (void)handleEvent:(nonnull NSEvent*)event {
168170
[self processNextEvent];
169171
}
170172

173+
- (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
174+
return _eventBeingDispatched == event;
175+
}
176+
171177
#pragma mark - Private
172178

173179
- (void)processNextEvent {

shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ - (void)keyUp:(NSEvent*)event {
490490
}
491491

492492
- (BOOL)performKeyEquivalent:(NSEvent*)event {
493-
if (_flutterViewController.keyboardManager.eventBeingDispatched == event) {
493+
if ([_flutterViewController isDispatchingKeyEvent:event]) {
494494
// When NSWindow is nextResponder, keyboard manager will send to it
495495
// unhandled events (through [NSWindow keyDown:]). If event has has both
496496
// control and cmd modifiers set (i.e. cmd+control+space - emoji picker)

shell/platform/darwin/macos/framework/Source/FlutterTextInputPluginTest.mm

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -865,12 +865,14 @@ - (bool)testComposingWithDeltasWhenSelectionIsActive {
865865

866866
- (bool)testPerformKeyEquivalent {
867867
__block NSEvent* eventBeingDispatchedByKeyboardManager = nil;
868-
id keyboardManagerMock = OCMClassMock([FlutterKeyboardManager class]);
869-
OCMStub([keyboardManagerMock eventBeingDispatched]).andDo(^(NSInvocation* invocation) {
870-
[invocation setReturnValue:&eventBeingDispatchedByKeyboardManager];
871-
});
872868
FlutterViewController* viewControllerMock = OCMClassMock([FlutterViewController class]);
873-
OCMStub([viewControllerMock keyboardManager]).andReturn(keyboardManagerMock);
869+
OCMStub([viewControllerMock isDispatchingKeyEvent:[OCMArg any]])
870+
.andDo(^(NSInvocation* invocation) {
871+
NSEvent* event;
872+
[invocation getArgument:(void*)&event atIndex:2];
873+
BOOL result = event == eventBeingDispatchedByKeyboardManager;
874+
[invocation setReturnValue:&result];
875+
});
874876

875877
NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
876878
location:NSZeroPoint

shell/platform/darwin/macos/framework/Source/FlutterViewController.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ @interface FlutterViewController () <FlutterViewReshapeListener>
176176
*/
177177
@property(nonatomic) id keyUpMonitor;
178178

179+
/**
180+
* Pointer to a keyboard manager, a hub that manages how key events are
181+
* dispatched to various Flutter key responders, and whether the event is
182+
* propagated to the next NSResponder.
183+
*/
184+
@property(nonatomic, readonly, nonnull) FlutterKeyboardManager* keyboardManager;
185+
179186
@property(nonatomic) KeyboardLayoutNotifier keyboardLayoutNotifier;
180187

181188
@property(nonatomic) NSData* keyboardLayoutData;
@@ -334,6 +341,10 @@ - (instancetype)initWithEngine:(nonnull FlutterEngine*)engine
334341
return self;
335342
}
336343

344+
- (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
345+
return [_keyboardManager isDispatchingKeyEvent:event];
346+
}
347+
337348
- (void)loadView {
338349
FlutterView* flutterView;
339350
if ([FlutterRenderingBackend renderUsingMetal]) {

shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
*/
2020
@property(nonatomic, readonly, nonnull) FlutterTextInputPlugin* textInputPlugin;
2121

22-
/**
23-
* Pointer to a keyboard manager, a hub that manages how key events are
24-
* dispatched to various Flutter key responders, and whether the event is
25-
* propagated to the next NSResponder.
26-
*/
27-
@property(nonatomic, readonly, nonnull) FlutterKeyboardManager* keyboardManager;
28-
2922
/**
3023
* Initializes this FlutterViewController with the specified `FlutterEngine`.
3124
*
@@ -39,6 +32,11 @@
3932
nibName:(nullable NSString*)nibName
4033
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
4134

35+
/**
36+
* Returns YES if provided event is being currently redispatched by keyboard manager.
37+
*/
38+
- (BOOL)isDispatchingKeyEvent:(nonnull NSEvent*)event;
39+
4240
@end
4341

4442
// Private methods made visible for testing

0 commit comments

Comments
 (0)