[ios_edit_menu]add native edit menu (flutter/engine#50095)

Support native edit menu on the engine side. 

Design doc: https://docs.google.com/document/d/16-8kn58h_oD902e7vPSh6W20aHRBJKyNOdSe5rbAe_g/edit?resourcekey=0-gVdJ3fbOybV70ZKeHU7fkQ&tab=t.0

*List which issues are fixed by this PR. You must list at least one issue.*

https://github.com/flutter/flutter/issues/103163

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
hellohuanlin
2024-04-22 16:36:18 -07:00
committed by GitHub
parent 627238cc93
commit cabd4fcacd
14 changed files with 262 additions and 3 deletions

View File

@@ -1075,6 +1075,14 @@ class PlatformDispatcher {
bool get nativeSpellCheckServiceDefined => _nativeSpellCheckServiceDefined;
bool _nativeSpellCheckServiceDefined = false;
/// Whether showing system context menu is supported on the current platform.
///
/// This option is used by [AdaptiveTextSelectionToolbar] to decide whether
/// to show system context menu, or to fallback to the default Flutter context
/// menu.
bool get supportsShowingSystemContextMenu => _supportsShowingSystemContextMenu;
bool _supportsShowingSystemContextMenu = false;
/// Whether briefly displaying the characters as you type in obscured text
/// fields is enabled in system settings.
///
@@ -1142,6 +1150,14 @@ class PlatformDispatcher {
} else {
_nativeSpellCheckServiceDefined = false;
}
final bool? supportsShowingSystemContextMenu = data['supportsShowingSystemContextMenu'] as bool?;
if (supportsShowingSystemContextMenu != null) {
_supportsShowingSystemContextMenu = supportsShowingSystemContextMenu;
} else {
_supportsShowingSystemContextMenu = false;
}
// This field is optional.
final bool? brieflyShowPassword = data['brieflyShowPassword'] as bool?;
if (brieflyShowPassword != null) {

View File

@@ -561,6 +561,13 @@ class SingletonFlutterWindow extends FlutterView {
/// service is specified.
bool get nativeSpellCheckServiceDefined => platformDispatcher.nativeSpellCheckServiceDefined;
/// Whether the spell check service is supported on the current platform.
///
/// This option is used by [EditableTextState] to define its
/// [SpellCheckConfiguration] when a default spell check service
/// is requested.
bool get supportsShowingSystemContextMenu => platformDispatcher.supportsShowingSystemContextMenu;
/// Whether briefly displaying the characters as you type in obscured text
/// fields is enabled in system settings.
///

View File

@@ -124,6 +124,8 @@ abstract class PlatformDispatcher {
bool get nativeSpellCheckServiceDefined => false;
bool get supportsShowingSystemContextMenu => false;
bool get brieflyShowPassword => true;
VoidCallback? get onTextScaleFactorChanged;

View File

@@ -390,6 +390,9 @@ final class EngineFlutterWindow extends EngineFlutterView implements ui.Singleto
@override
bool get nativeSpellCheckServiceDefined => platformDispatcher.nativeSpellCheckServiceDefined;
@override
bool get supportsShowingSystemContextMenu => platformDispatcher.supportsShowingSystemContextMenu;
@override
bool get brieflyShowPassword => platformDispatcher.brieflyShowPassword;

View File

@@ -46,6 +46,8 @@ abstract class SingletonFlutterWindow extends FlutterView {
bool get nativeSpellCheckServiceDefined;
bool get supportsShowingSystemContextMenu;
bool get brieflyShowPassword;
bool get alwaysUse24HourFormat;

View File

@@ -1080,6 +1080,12 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
arguments:@[ @(client), @(start), @(end) ]];
}
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
willDismissEditMenuWithTextInputClient:(int)client {
[_platformChannel.get() invokeMethod:@"ContextMenu.onDismissSystemContextMenu"
arguments:@[ @(client) ]];
}
#pragma mark - FlutterViewEngineDelegate
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView showToolbar:(int)client {

View File

@@ -149,11 +149,36 @@ static void SetStatusBarStyleForSharedApplication(UIStatusBarStyle style) {
} else if ([method isEqualToString:@"Share.invoke"]) {
[self showShareViewController:args];
result(nil);
} else if ([method isEqualToString:@"ContextMenu.showSystemContextMenu"]) {
[self showSystemContextMenu:args];
result(nil);
} else if ([method isEqualToString:@"ContextMenu.hideSystemContextMenu"]) {
[self hideSystemContextMenu];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)showSystemContextMenu:(NSDictionary*)args {
if (@available(iOS 16.0, *)) {
FlutterTextInputPlugin* textInputPlugin = [_engine.get() textInputPlugin];
BOOL shownEditMenu = [textInputPlugin showEditMenu:args];
if (!shownEditMenu) {
FML_LOG(ERROR) << "Only text input supports system context menu for now. Ensure the system "
"context menu is shown with an active text input connection. See "
"https://github.com/flutter/flutter/issues/143033.";
}
}
}
- (void)hideSystemContextMenu {
if (@available(iOS 16.0, *)) {
FlutterTextInputPlugin* textInputPlugin = [_engine.get() textInputPlugin];
[textInputPlugin hideEditMenu];
}
}
- (void)showShareViewController:(NSString*)content {
UIViewController* engineViewController = [_engine.get() viewController];

View File

@@ -65,6 +65,8 @@ typedef NS_ENUM(NSInteger, FlutterFloatingCursorDragState) {
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView removeTextPlaceholder:(int)client;
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
didResignFirstResponderWithTextInputClient:(int)client;
- (void)flutterTextInputView:(FlutterTextInputView*)textInputView
willDismissEditMenuWithTextInputClient:(int)client;
@end
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTINPUTDELEGATE_H_

View File

@@ -60,6 +60,8 @@ typedef NS_ENUM(NSInteger, FlutterScribbleInteractionStatus) {
*/
- (void)setUpIndirectScribbleInteraction:(id<FlutterViewResponder>)viewResponder;
- (void)resetViewResponder;
- (BOOL)showEditMenu:(NSDictionary*)args API_AVAILABLE(ios(16.0));
- (void)hideEditMenu API_AVAILABLE(ios(16.0));
@end
@@ -128,7 +130,8 @@ API_AVAILABLE(ios(13.0)) @interface FlutterTextPlaceholder : UITextPlaceholder
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
FLUTTER_DARWIN_EXPORT
#endif
@interface FlutterTextInputView : UIView <UITextInput, UIScribbleInteractionDelegate>
@interface FlutterTextInputView
: UIView <UITextInput, UIScribbleInteractionDelegate, UIEditMenuInteractionDelegate>
// UITextInput
@property(nonatomic, readonly) NSMutableString* text;
@@ -158,6 +161,8 @@ FLUTTER_DARWIN_EXPORT
@property(nonatomic, weak) id<FlutterViewResponder> viewResponder;
@property(nonatomic) FlutterScribbleFocusStatus scribbleFocusStatus;
@property(nonatomic, strong) NSArray<FlutterTextSelectionRect*>* selectionRects;
@property(nonatomic, strong) UIEditMenuInteraction* editMenuInteraction API_AVAILABLE(ios(16.0));
- (void)resetScribbleInteractionStatusIfEnding;
- (BOOL)isScribbleAvailable;

View File

@@ -794,6 +794,7 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
// This is cleared at the start of each keyboard interaction. (Enter a character, delete a character
// etc)
@property(nonatomic, copy) NSString* temporarilyDeletedComposedCharacter;
@property(nonatomic, assign) CGRect editMenuTargetRect;
- (void)setEditableTransform:(NSArray*)matrix;
@end
@@ -859,9 +860,44 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
}
}
if (@available(iOS 16.0, *)) {
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
[self addInteraction:_editMenuInteraction];
}
return self;
}
- (UIMenu*)editMenuInteraction:(UIEditMenuInteraction*)interaction
menuForConfiguration:(UIEditMenuConfiguration*)configuration
suggestedActions:(NSArray<UIMenuElement*>*)suggestedActions API_AVAILABLE(ios(16.0)) {
return [UIMenu menuWithChildren:suggestedActions];
}
- (void)editMenuInteraction:(UIEditMenuInteraction*)interaction
willDismissMenuForConfiguration:(UIEditMenuConfiguration*)configuration
animator:(id<UIEditMenuInteractionAnimating>)animator
API_AVAILABLE(ios(16.0)) {
[self.textInputDelegate flutterTextInputView:self
willDismissEditMenuWithTextInputClient:_textInputClient];
}
- (CGRect)editMenuInteraction:(UIEditMenuInteraction*)interaction
targetRectForConfiguration:(UIEditMenuConfiguration*)configuration API_AVAILABLE(ios(16.0)) {
return _editMenuTargetRect;
}
- (void)showEditMenuWithTargetRect:(CGRect)targetRect API_AVAILABLE(ios(16.0)) {
_editMenuTargetRect = targetRect;
UIEditMenuConfiguration* config =
[UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:CGPointZero];
[self.editMenuInteraction presentEditMenuWithConfiguration:config];
}
- (void)hideEditMenu API_AVAILABLE(ios(16.0)) {
[self.editMenuInteraction dismissMenu];
}
- (void)configureWithDictionary:(NSDictionary*)configuration {
NSDictionary* inputType = configuration[kKeyboardType];
NSString* keyboardAppearance = configuration[kKeyboardAppearance];
@@ -1148,8 +1184,10 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
if (action == @selector(paste:)) {
// Forbid pasting images, memojis, or other non-string content.
return [UIPasteboard generalPasteboard].hasStrings;
} else if (action == @selector(copy:) || action == @selector(cut:) ||
action == @selector(delete:)) {
return [self textInRange:_selectedTextRange].length > 0;
}
return [super canPerformAction:action withSender:sender];
}
@@ -2511,6 +2549,23 @@ static BOOL IsSelectionRectBoundaryCloserToPoint(CGPoint point,
_keyboardViewContainer.frame = _keyboardRect;
}
- (BOOL)showEditMenu:(NSDictionary*)args API_AVAILABLE(ios(16.0)) {
if (!self.activeView.isFirstResponder) {
return NO;
}
NSDictionary<NSString*, NSNumber*>* encodedTargetRect = args[@"targetRect"];
CGRect globalTargetRect = CGRectMake(
[encodedTargetRect[@"x"] doubleValue], [encodedTargetRect[@"y"] doubleValue],
[encodedTargetRect[@"width"] doubleValue], [encodedTargetRect[@"height"] doubleValue]);
CGRect localTargetRect = [self.hostView convertRect:globalTargetRect toView:self.activeView];
[self.activeView showEditMenuWithTargetRect:localTargetRect];
return YES;
}
- (void)hideEditMenu {
[self.activeView hideEditMenu];
}
- (void)setEditableSizeAndTransform:(NSDictionary*)dictionary {
NSArray* transform = dictionary[@"transform"];
[_activeView setEditableTransform:transform];

View File

@@ -2847,6 +2847,117 @@ FLUTTER_ASSERT_ARC
XCTAssertNil(activeView.superview, @"activeView must be removed from view hierarchy.");
}
- (void)testEditMenu_shouldSetupEditMenuDelegateCorrectly {
if (@available(iOS 16.0, *)) {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[UIApplication.sharedApplication.keyWindow addSubview:inputView];
XCTAssertEqual(inputView.editMenuInteraction.delegate, inputView,
@"editMenuInteraction setup delegate correctly");
}
}
- (void)testEditMenu_shouldNotPresentEditMenuIfNotFirstResponder {
if (@available(iOS 16.0, *)) {
FlutterTextInputPlugin* myInputPlugin =
[[FlutterTextInputPlugin alloc] initWithDelegate:OCMClassMock([FlutterEngine class])];
BOOL shownEditMenu = [myInputPlugin showEditMenu:@{}];
XCTAssertFalse(shownEditMenu, @"Should not show edit menu if not first responder.");
}
}
- (void)testEditMenu_shouldPresentEditMenuWithCorrectConfiguration {
if (@available(iOS 16.0, *)) {
FlutterTextInputPlugin* myInputPlugin =
[[FlutterTextInputPlugin alloc] initWithDelegate:OCMClassMock([FlutterEngine class])];
FlutterViewController* myViewController = [[FlutterViewController alloc] init];
myInputPlugin.viewController = myViewController;
[myViewController loadView];
FlutterMethodCall* setClientCall =
[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
arguments:@[ @(123), self.mutableTemplateCopy ]];
[myInputPlugin handleMethodCall:setClientCall
result:^(id _Nullable result){
}];
FlutterTextInputView* myInputView = myInputPlugin.activeView;
FlutterTextInputView* mockInputView = OCMPartialMock(myInputView);
OCMStub([mockInputView isFirstResponder]).andReturn(YES);
XCTestExpectation* expectation = [[XCTestExpectation alloc]
initWithDescription:@"presentEditMenuWithConfiguration must be called."];
id mockInteraction = OCMClassMock([UIEditMenuInteraction class]);
OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
.andDo(^(NSInvocation* invocation) {
// arguments are released once invocation is released.
[invocation retainArguments];
UIEditMenuConfiguration* config;
[invocation getArgument:&config atIndex:2];
XCTAssertEqual(config.preferredArrowDirection, UIEditMenuArrowDirectionAutomatic,
@"UIEditMenuConfiguration must use automatic arrow direction.");
XCTAssert(CGPointEqualToPoint(config.sourcePoint, CGPointZero),
@"UIEditMenuConfiguration must have the correct point.");
[expectation fulfill];
});
NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
@{@"x" : @(0), @"y" : @(0), @"width" : @(0), @"height" : @(0)};
BOOL shownEditMenu = [myInputPlugin showEditMenu:@{@"targetRect" : encodedTargetRect}];
XCTAssertTrue(shownEditMenu, @"Should show edit menu with correct configuration.");
[self waitForExpectations:@[ expectation ] timeout:1.0];
}
}
- (void)testEditMenu_shouldPresentEditMenuWithCorectTargetRect {
if (@available(iOS 16.0, *)) {
FlutterTextInputPlugin* myInputPlugin =
[[FlutterTextInputPlugin alloc] initWithDelegate:OCMClassMock([FlutterEngine class])];
FlutterViewController* myViewController = [[FlutterViewController alloc] init];
myInputPlugin.viewController = myViewController;
[myViewController loadView];
FlutterMethodCall* setClientCall =
[FlutterMethodCall methodCallWithMethodName:@"TextInput.setClient"
arguments:@[ @(123), self.mutableTemplateCopy ]];
[myInputPlugin handleMethodCall:setClientCall
result:^(id _Nullable result){
}];
FlutterTextInputView* myInputView = myInputPlugin.activeView;
FlutterTextInputView* mockInputView = OCMPartialMock(myInputView);
OCMStub([mockInputView isFirstResponder]).andReturn(YES);
XCTestExpectation* expectation = [[XCTestExpectation alloc]
initWithDescription:@"presentEditMenuWithConfiguration must be called."];
id mockInteraction = OCMClassMock([UIEditMenuInteraction class]);
OCMStub([mockInputView editMenuInteraction]).andReturn(mockInteraction);
OCMStub([mockInteraction presentEditMenuWithConfiguration:[OCMArg any]])
.andDo(^(NSInvocation* invocation) {
[expectation fulfill];
});
myInputView.frame = CGRectMake(10, 20, 30, 40);
NSDictionary<NSString*, NSNumber*>* encodedTargetRect =
@{@"x" : @(100), @"y" : @(200), @"width" : @(300), @"height" : @(400)};
BOOL shownEditMenu = [myInputPlugin showEditMenu:@{@"targetRect" : encodedTargetRect}];
XCTAssertTrue(shownEditMenu, @"Should show edit menu with correct configuration.");
[self waitForExpectations:@[ expectation ] timeout:1.0];
CGRect targetRect =
[myInputView editMenuInteraction:mockInteraction
targetRectForConfiguration:OCMClassMock([UIEditMenuConfiguration class])];
// the encoded target rect is in global coordinate space.
XCTAssert(CGRectEqualToRect(targetRect, CGRectMake(90, 180, 300, 400)),
@"targetRectForConfiguration must return the correct target rect.");
}
}
- (void)testInteractiveKeyboardAfterUserScrollWillResignFirstResponder {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[UIApplication.sharedApplication.keyWindow addSubview:inputView];

View File

@@ -2134,7 +2134,8 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
@"alwaysUse24HourFormat" : @([self isAlwaysUse24HourFormat]),
@"platformBrightness" : [self brightnessMode],
@"platformContrast" : [self contrastMode],
@"nativeSpellCheckServiceDefined" : @true
@"nativeSpellCheckServiceDefined" : @true,
@"supportsShowingSystemContextMenu" : @([self supportsShowingSystemContextMenu])
}];
}
@@ -2196,6 +2197,14 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
#endif
}
- (BOOL)supportsShowingSystemContextMenu {
if (@available(iOS 16.0, *)) {
return YES;
} else {
return NO;
}
}
- (BOOL)isAlwaysUse24HourFormat {
// iOS does not report its "24-Hour Time" user setting in the API. Instead, it applies
// it automatically to NSDateFormatter when used with [NSLocale currentLocale]. It is

View File

@@ -2204,4 +2204,18 @@ extern NSNotificationName const FlutterViewControllerWillDealloc;
XCTAssertNil(viewController.keyboardAnimationVSyncClient);
}
- (void)testSupportsShowingSystemContextMenuForIOS16AndAbove {
FlutterEngine* engine = [[FlutterEngine alloc] init];
[engine runWithEntrypoint:nil];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
BOOL supportsShowingSystemContextMenu = [viewController supportsShowingSystemContextMenu];
if (@available(iOS 16.0, *)) {
XCTAssertTrue(supportsShowingSystemContextMenu);
} else {
XCTAssertFalse(supportsShowingSystemContextMenu);
}
}
@end

View File

@@ -69,6 +69,8 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
- (void)deregisterNotifications;
- (int32_t)accessibilityFlags;
- (BOOL)supportsShowingSystemContextMenu;
@end
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEWCONTROLLER_INTERNAL_H_