Skip to content

Commit

Permalink
Do not expose KeyboardManager from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Jun 13, 2022
1 parent 96859b3 commit 252ba98
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
- (void)handleEvent:(nonnull NSEvent*)event;

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

@end
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ @interface FlutterKeyboardManager ()

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

@property(nonatomic, nullable) NSEvent* eventBeingDispatched;

/**
* Add a primary responder, which asynchronously decides whether to handle an
* event.
Expand Down Expand Up @@ -168,6 +170,10 @@ - (void)handleEvent:(nonnull NSEvent*)event {
[self processNextEvent];
}

- (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
return _eventBeingDispatched == event;
}

#pragma mark - Private

- (void)processNextEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ - (void)keyUp:(NSEvent*)event {
}

- (BOOL)performKeyEquivalent:(NSEvent*)event {
if (_flutterViewController.keyboardManager.eventBeingDispatched == event) {
if ([_flutterViewController isDispatchingKeyEvent:event]) {
// When NSWindow is nextResponder, keyboard manager will send to it
// unhandled events (through [NSWindow keyDown:]). If event has has both
// control and cmd modifiers set (i.e. cmd+control+space - emoji picker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,14 @@ - (bool)testComposingWithDeltasWhenSelectionIsActive {

- (bool)testPerformKeyEquivalent {
__block NSEvent* eventBeingDispatchedByKeyboardManager = nil;
id keyboardManagerMock = OCMClassMock([FlutterKeyboardManager class]);
OCMStub([keyboardManagerMock eventBeingDispatched]).andDo(^(NSInvocation* invocation) {
[invocation setReturnValue:&eventBeingDispatchedByKeyboardManager];
});
FlutterViewController* viewControllerMock = OCMClassMock([FlutterViewController class]);
OCMStub([viewControllerMock keyboardManager]).andReturn(keyboardManagerMock);
OCMStub([viewControllerMock isDispatchingKeyEvent:[OCMArg any]])
.andDo(^(NSInvocation* invocation) {
NSEvent* event;
[invocation getArgument:(void*)&event atIndex:2];
BOOL result = event == eventBeingDispatchedByKeyboardManager;
[invocation setReturnValue:&result];
});

NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ @interface FlutterViewController () <FlutterViewReshapeListener>
*/
@property(nonatomic) id keyUpMonitor;

/**
* Pointer to a keyboard manager, a hub that manages how key events are
* dispatched to various Flutter key responders, and whether the event is
* propagated to the next NSResponder.
*/
@property(nonatomic, readonly, nonnull) FlutterKeyboardManager* keyboardManager;

@property(nonatomic) KeyboardLayoutNotifier keyboardLayoutNotifier;

@property(nonatomic) NSData* keyboardLayoutData;
Expand Down Expand Up @@ -334,6 +341,10 @@ - (instancetype)initWithEngine:(nonnull FlutterEngine*)engine
return self;
}

- (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
return [_keyboardManager isDispatchingKeyEvent:event];
}

- (void)loadView {
FlutterView* flutterView;
if ([FlutterRenderingBackend renderUsingMetal]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
*/
@property(nonatomic, readonly, nonnull) FlutterTextInputPlugin* textInputPlugin;

/**
* Pointer to a keyboard manager, a hub that manages how key events are
* dispatched to various Flutter key responders, and whether the event is
* propagated to the next NSResponder.
*/
@property(nonatomic, readonly, nonnull) FlutterKeyboardManager* keyboardManager;

/**
* Initializes this FlutterViewController with the specified `FlutterEngine`.
*
Expand All @@ -39,6 +32,11 @@
nibName:(nullable NSString*)nibName
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;

/**
* Returns YES if provided event is being currently redispatched by keyboard manager.
*/
- (BOOL)isDispatchingKeyEvent:(nonnull NSEvent*)event;

@end

// Private methods made visible for testing
Expand Down

0 comments on commit 252ba98

Please sign in to comment.