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

[macOS] Fix Ctrl+Tab is broken #40706

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
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 @@ -6,6 +6,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"

#include <Carbon/Carbon.h>
#import <objc/message.h>

#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
Expand Down Expand Up @@ -163,6 +164,8 @@ @interface FlutterViewWrapper : NSView

- (void)setBackgroundColor:(NSColor*)color;

- (BOOL)performKeyEquivalent:(NSEvent*)event;

@end

/**
Expand Down Expand Up @@ -239,6 +242,37 @@ - (void)onKeyboardLayoutChanged;

@end

#pragma mark - NSEvent (KeyEquivalentMarker) protocol

@interface NSEvent (KeyEquivalentMarker)

// Internally marks that the event was received through performKeyEquivalent:.
// When text editing is active, keyboard events that have modifier keys pressed
// are received through performKeyEquivalent: instead of keyDown:. If such event
// is passed to TextInputContext but doesn't result in a text editing action it
// needs to be forwarded by FlutterKeyboardManager to the next responder.
- (void)markAsKeyEquivalent;

// Returns YES if the event is marked as a key equivalent.
- (BOOL)isKeyEquivalent;

@end

@implementation NSEvent (KeyEquivalentMarker)

// This field doesn't need a value because only its address is used as a unique identifier.
static char markerKey;

- (void)markAsKeyEquivalent {
objc_setAssociatedObject(self, &markerKey, @true, OBJC_ASSOCIATION_RETAIN);
}

- (BOOL)isKeyEquivalent {
return [objc_getAssociatedObject(self, &markerKey) boolValue] == YES;
}

@end

#pragma mark - Private dependant functions

namespace {
Expand All @@ -258,12 +292,15 @@ void OnKeyboardLayoutChanged(CFNotificationCenterRef center,

@implementation FlutterViewWrapper {
FlutterView* _flutterView;
FlutterViewController* _controller;
}

- (instancetype)initWithFlutterView:(FlutterView*)view {
- (instancetype)initWithFlutterView:(FlutterView*)view
controller:(FlutterViewController*)controller {
self = [super initWithFrame:NSZeroRect];
if (self) {
_flutterView = view;
_controller = controller;
view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self addSubview:view];
}
Expand All @@ -274,6 +311,24 @@ - (void)setBackgroundColor:(NSColor*)color {
[_flutterView setBackgroundColor:color];
}

- (BOOL)performKeyEquivalent:(NSEvent*)event {
if ([_controller isDispatchingKeyEvent:event]) {
// When NSWindow is nextResponder, keyboard manager will send to it
// unhandled events (through [NSWindow keyDown:]). If event has both
Comment on lines +316 to +317
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "will send to it unhandled events" => "will send unhandled events to it"

// control and cmd modifiers set (i.e. cmd+control+space - emoji picker)
// NSWindow will then send this event as performKeyEquivalent: to first
// responder, which might be FlutterTextInputPlugin. If that's the case, the
// plugin must not handle the event, otherwise the emoji picker would not
// work (due to first responder returning YES from performKeyEquivalent:)
// and there would be an infinite loop, because FlutterViewController will
// send the event back to [keyboardManager handleEvent:].
return NO;
}
[event markAsKeyEquivalent];
[_flutterView keyDown:event];
return YES;
}

- (NSArray*)accessibilityChildren {
return @[ _flutterView ];
}
Expand Down Expand Up @@ -405,7 +460,8 @@ - (void)loadView {
if (_backgroundColor != nil) {
[flutterView setBackgroundColor:_backgroundColor];
}
FlutterViewWrapper* wrapperView = [[FlutterViewWrapper alloc] initWithFlutterView:flutterView];
FlutterViewWrapper* wrapperView = [[FlutterViewWrapper alloc] initWithFlutterView:flutterView
controller:self];
self.view = wrapperView;
_flutterView = flutterView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ @interface FlutterViewControllerTestObjC : NSObject
- (bool)testKeyEventsAreSentToFramework;
- (bool)testKeyEventsArePropagatedIfNotHandled;
- (bool)testKeyEventsAreNotPropagatedIfHandled;
- (bool)testCtrlTabKeyEventIsPropagated;
- (bool)testFlagsChangedEventsArePropagatedIfNotHandled;
- (bool)testKeyboardIsRestartedOnEngineRestart;
- (bool)testTrackpadGesturesAreSentToFramework;
Expand Down Expand Up @@ -207,6 +208,10 @@ id MockGestureEvent(NSEventType type, NSEventPhase phase, double magnification,
ASSERT_TRUE([[FlutterViewControllerTestObjC alloc] testKeyEventsAreNotPropagatedIfHandled]);
}

TEST(FlutterViewControllerTest, TestCtrlTabKeyEventIsPropagated) {
ASSERT_TRUE([[FlutterViewControllerTestObjC alloc] testCtrlTabKeyEventIsPropagated]);
}

TEST(FlutterViewControllerTest, TestFlagsChangedEventsArePropagatedIfNotHandled) {
ASSERT_TRUE(
[[FlutterViewControllerTestObjC alloc] testFlagsChangedEventsArePropagatedIfNotHandled]);
Expand Down Expand Up @@ -280,6 +285,45 @@ - (bool)testKeyEventsAreSentToFramework {
return true;
}

// Regression test for https://github.com/flutter/flutter/issues/122084.
- (bool)testCtrlTabKeyEventIsPropagated {
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
__block bool called = false;
__block FlutterKeyEvent last_event;
OCMStub([[engineMock ignoringNonObjectArgs] sendKeyEvent:FlutterKeyEvent {}
callback:nil
userData:nil])
.andDo((^(NSInvocation* invocation) {
FlutterKeyEvent* event;
[invocation getArgument:&event atIndex:2];
called = true;
last_event = *event;
}));
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
nibName:@""
bundle:nil];
// Ctrl+tab
NSEvent* event = [NSEvent keyEventWithType:NSEventTypeKeyDown
location:NSZeroPoint
modifierFlags:0x40101
timestamp:0
windowNumber:0
context:nil
characters:@""
charactersIgnoringModifiers:@""
isARepeat:NO
keyCode:48];
const uint64_t kPhysicalKeyTab = 0x7002b;

[viewController viewWillAppear]; // Initializes the event channel.
[viewController.view performKeyEquivalent:event];

EXPECT_TRUE(called);
EXPECT_EQ(last_event.type, kFlutterKeyEventTypeDown);
EXPECT_EQ(last_event.physical, kPhysicalKeyTab);
return true;
}

- (bool)testKeyEventsArePropagatedIfNotHandled {
id engineMock = flutter::testing::CreateMockFlutterEngine(@"");
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
Expand Down