Refactor and migrate FlutterUndoManagerPlugin to ARC (flutter/engine#52234)
Smart pointers support ARC as of https://github.com/flutter/engine/pull/47612, and the unit tests were migrated in https://github.com/flutter/engine/pull/48162. Migrate `FlutterUndoManagerPlugin` from MRC to ARC. 1. Refactor so the plugin and its tests don't need to understand the details of `FlutterViewController` or `FlutterEngine` (its delegate). This decouples the plugin, and means it doesn't depend on any MRC classes. 2. Change the delegate so conforming only requires the objects the undo plugin actually needs. Part of https://github.com/flutter/flutter/issues/137801.
This commit is contained in:
@@ -84,6 +84,9 @@ source_set("flutter_framework_source_arc") {
|
||||
"framework/Source/FlutterTextureRegistryRelay.mm",
|
||||
"framework/Source/FlutterUIPressProxy.h",
|
||||
"framework/Source/FlutterUIPressProxy.mm",
|
||||
"framework/Source/FlutterUndoManagerDelegate.h",
|
||||
"framework/Source/FlutterUndoManagerPlugin.h",
|
||||
"framework/Source/FlutterUndoManagerPlugin.mm",
|
||||
"framework/Source/KeyCodeMap.g.mm",
|
||||
"framework/Source/KeyCodeMap_Internal.h",
|
||||
"framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h",
|
||||
@@ -157,9 +160,6 @@ source_set("flutter_framework_source") {
|
||||
"framework/Source/FlutterPluginAppLifeCycleDelegate.mm",
|
||||
"framework/Source/FlutterSemanticsScrollView.h",
|
||||
"framework/Source/FlutterSemanticsScrollView.mm",
|
||||
"framework/Source/FlutterUndoManagerDelegate.h",
|
||||
"framework/Source/FlutterUndoManagerPlugin.h",
|
||||
"framework/Source/FlutterUndoManagerPlugin.mm",
|
||||
"framework/Source/FlutterView.h",
|
||||
"framework/Source/FlutterView.mm",
|
||||
"framework/Source/FlutterViewController.mm",
|
||||
|
||||
@@ -430,7 +430,6 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
|
||||
[self maybeSetupPlatformViewChannels];
|
||||
[self updateDisplays];
|
||||
_textInputPlugin.get().viewController = viewController;
|
||||
_undoManagerPlugin.get().viewController = viewController;
|
||||
|
||||
if (viewController) {
|
||||
__block FlutterEngine* blockSelf = self;
|
||||
@@ -465,7 +464,6 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
|
||||
- (void)notifyViewControllerDeallocated {
|
||||
[[self lifecycleChannel] sendMessage:@"AppLifecycleState.detached"];
|
||||
_textInputPlugin.get().viewController = nil;
|
||||
_undoManagerPlugin.get().viewController = nil;
|
||||
if (!_allowHeadlessExecution) {
|
||||
[self destroyContext];
|
||||
} else if (_shell) {
|
||||
@@ -1189,12 +1187,19 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
|
||||
|
||||
#pragma mark - Undo Manager Delegate
|
||||
|
||||
- (void)flutterUndoManagerPlugin:(FlutterUndoManagerPlugin*)undoManagerPlugin
|
||||
handleUndoWithDirection:(FlutterUndoRedoDirection)direction {
|
||||
- (void)handleUndoWithDirection:(FlutterUndoRedoDirection)direction {
|
||||
NSString* action = (direction == FlutterUndoRedoDirectionUndo) ? @"undo" : @"redo";
|
||||
[_undoManagerChannel.get() invokeMethod:@"UndoManagerClient.handleUndo" arguments:@[ action ]];
|
||||
}
|
||||
|
||||
- (UIView<UITextInput>*)activeTextInputView {
|
||||
return [[self textInputPlugin] textInputView];
|
||||
}
|
||||
|
||||
- (NSUndoManager*)undoManager {
|
||||
return self.viewController.undoManager;
|
||||
}
|
||||
|
||||
#pragma mark - Screenshot Delegate
|
||||
|
||||
- (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type
|
||||
|
||||
@@ -16,11 +16,29 @@ typedef NS_ENUM(NSInteger, FlutterUndoRedoDirection) {
|
||||
// NOLINTEND(readability-identifier-naming)
|
||||
};
|
||||
|
||||
@class FlutterUndoManagerPlugin;
|
||||
|
||||
/**
|
||||
* Protocol for undo manager changes from the `FlutterUndoManagerPlugin`, typically a
|
||||
* `FlutterEngine`.
|
||||
*/
|
||||
@protocol FlutterUndoManagerDelegate <NSObject>
|
||||
- (void)flutterUndoManagerPlugin:(FlutterUndoManagerPlugin*)undoManagerPlugin
|
||||
handleUndoWithDirection:(FlutterUndoRedoDirection)direction;
|
||||
|
||||
/**
|
||||
* The `NSUndoManager` that should be managed by the `FlutterUndoManagerPlugin`.
|
||||
* When the delegate is `FlutterEngine` this will be the `FlutterViewController`'s undo manager.
|
||||
*/
|
||||
@property(nonatomic, readonly, nullable) NSUndoManager* undoManager;
|
||||
|
||||
/**
|
||||
* Used to notify the active view when undo manager state (can redo/can undo)
|
||||
* changes, in order to force keyboards to update undo/redo buttons.
|
||||
*/
|
||||
@property(nonatomic, readonly, nullable) UIView<UITextInput>* activeTextInputView;
|
||||
|
||||
/**
|
||||
* Pass changes to the framework through the undo manager channel.
|
||||
*/
|
||||
- (void)handleUndoWithDirection:(FlutterUndoRedoDirection)direction;
|
||||
|
||||
@end
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
|
||||
@@ -7,15 +7,11 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#import "flutter/fml/memory/weak_ptr.h"
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h"
|
||||
|
||||
@interface FlutterUndoManagerPlugin : NSObject
|
||||
|
||||
@property(nonatomic, assign) FlutterViewController* viewController;
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
+ (instancetype)new NS_UNAVAILABLE;
|
||||
|
||||
|
||||
@@ -3,12 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
|
||||
#pragma mark - UndoManager channel method names.
|
||||
static NSString* const kSetUndoStateMethod = @"UndoManager.setUndoState";
|
||||
@@ -17,15 +11,16 @@ static NSString* const kSetUndoStateMethod = @"UndoManager.setUndoState";
|
||||
static NSString* const kCanUndo = @"canUndo";
|
||||
static NSString* const kCanRedo = @"canRedo";
|
||||
|
||||
@implementation FlutterUndoManagerPlugin {
|
||||
id<FlutterUndoManagerDelegate> _undoManagerDelegate;
|
||||
}
|
||||
@interface FlutterUndoManagerPlugin ()
|
||||
@property(nonatomic, weak, readonly) id<FlutterUndoManagerDelegate> undoManagerDelegate;
|
||||
@end
|
||||
|
||||
@implementation FlutterUndoManagerPlugin
|
||||
|
||||
- (instancetype)initWithDelegate:(id<FlutterUndoManagerDelegate>)undoManagerDelegate {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
// `_undoManagerDelegate` is a weak reference because it should retain FlutterUndoManagerPlugin.
|
||||
_undoManagerDelegate = undoManagerDelegate;
|
||||
}
|
||||
|
||||
@@ -33,8 +28,7 @@ static NSString* const kCanRedo = @"canRedo";
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self resetUndoManager];
|
||||
[super dealloc];
|
||||
[_undoManagerDelegate.undoManager removeAllActionsWithTarget:self];
|
||||
}
|
||||
|
||||
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
|
||||
@@ -48,46 +42,43 @@ static NSString* const kCanRedo = @"canRedo";
|
||||
}
|
||||
}
|
||||
|
||||
- (NSUndoManager*)undoManager {
|
||||
return _viewController.undoManager;
|
||||
- (void)resetUndoManager {
|
||||
[self.undoManagerDelegate.undoManager removeAllActionsWithTarget:self];
|
||||
}
|
||||
|
||||
- (void)resetUndoManager API_AVAILABLE(ios(9.0)) {
|
||||
[[self undoManager] removeAllActionsWithTarget:self];
|
||||
- (void)registerUndoWithDirection:(FlutterUndoRedoDirection)direction {
|
||||
NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
|
||||
[undoManager beginUndoGrouping];
|
||||
[undoManager registerUndoWithTarget:self
|
||||
handler:^(FlutterUndoManagerPlugin* target) {
|
||||
// Register undo with opposite direction.
|
||||
FlutterUndoRedoDirection newDirection =
|
||||
(direction == FlutterUndoRedoDirectionRedo)
|
||||
? FlutterUndoRedoDirectionUndo
|
||||
: FlutterUndoRedoDirectionRedo;
|
||||
[target registerUndoWithDirection:newDirection];
|
||||
// Invoke method on delegate.
|
||||
[target.undoManagerDelegate handleUndoWithDirection:direction];
|
||||
}];
|
||||
[undoManager endUndoGrouping];
|
||||
}
|
||||
|
||||
- (void)registerUndoWithDirection:(FlutterUndoRedoDirection)direction API_AVAILABLE(ios(9.0)) {
|
||||
[[self undoManager] beginUndoGrouping];
|
||||
[[self undoManager] registerUndoWithTarget:self
|
||||
handler:^(id target) {
|
||||
// Register undo with opposite direction.
|
||||
FlutterUndoRedoDirection newDirection =
|
||||
(direction == FlutterUndoRedoDirectionRedo)
|
||||
? FlutterUndoRedoDirectionUndo
|
||||
: FlutterUndoRedoDirectionRedo;
|
||||
[target registerUndoWithDirection:newDirection];
|
||||
// Invoke method on delegate.
|
||||
[_undoManagerDelegate flutterUndoManagerPlugin:self
|
||||
handleUndoWithDirection:direction];
|
||||
}];
|
||||
[[self undoManager] endUndoGrouping];
|
||||
- (void)registerRedo {
|
||||
NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
|
||||
[undoManager beginUndoGrouping];
|
||||
[undoManager registerUndoWithTarget:self
|
||||
handler:^(id target) {
|
||||
// Register undo with opposite direction.
|
||||
[target registerUndoWithDirection:FlutterUndoRedoDirectionRedo];
|
||||
}];
|
||||
[undoManager endUndoGrouping];
|
||||
[undoManager undo];
|
||||
}
|
||||
|
||||
- (void)registerRedo API_AVAILABLE(ios(9.0)) {
|
||||
[[self undoManager] beginUndoGrouping];
|
||||
[[self undoManager]
|
||||
registerUndoWithTarget:self
|
||||
handler:^(id target) {
|
||||
// Register undo with opposite direction.
|
||||
[target registerUndoWithDirection:FlutterUndoRedoDirectionRedo];
|
||||
}];
|
||||
[[self undoManager] endUndoGrouping];
|
||||
[[self undoManager] undo];
|
||||
}
|
||||
|
||||
- (void)setUndoState:(NSDictionary*)dictionary API_AVAILABLE(ios(9.0)) {
|
||||
BOOL groupsByEvent = [self undoManager].groupsByEvent;
|
||||
[self undoManager].groupsByEvent = NO;
|
||||
- (void)setUndoState:(NSDictionary*)dictionary {
|
||||
NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
|
||||
BOOL groupsByEvent = undoManager.groupsByEvent;
|
||||
undoManager.groupsByEvent = NO;
|
||||
BOOL canUndo = [dictionary[kCanUndo] boolValue];
|
||||
BOOL canRedo = [dictionary[kCanRedo] boolValue];
|
||||
|
||||
@@ -99,16 +90,15 @@ static NSString* const kCanRedo = @"canRedo";
|
||||
if (canRedo) {
|
||||
[self registerRedo];
|
||||
}
|
||||
|
||||
if (_viewController.engine.textInputPlugin.textInputView != nil) {
|
||||
UIView<UITextInput>* textInputView = self.undoManagerDelegate.activeTextInputView;
|
||||
if (textInputView != nil) {
|
||||
// This is needed to notify the iPadOS keyboard that it needs to update the
|
||||
// state of the UIBarButtons. Otherwise, the state changes to NSUndoManager
|
||||
// will not show up until the next keystroke (or other trigger).
|
||||
UITextInputAssistantItem* assistantItem =
|
||||
_viewController.engine.textInputPlugin.textInputView.inputAssistantItem;
|
||||
UITextInputAssistantItem* assistantItem = textInputView.inputAssistantItem;
|
||||
assistantItem.leadingBarButtonGroups = assistantItem.leadingBarButtonGroups;
|
||||
}
|
||||
[self undoManager].groupsByEvent = groupsByEvent;
|
||||
undoManager.groupsByEvent = groupsByEvent;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -8,54 +8,77 @@
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
|
||||
|
||||
FLUTTER_ASSERT_ARC
|
||||
|
||||
@interface FlutterEngine ()
|
||||
- (nonnull FlutterUndoManagerPlugin*)undoManagerPlugin;
|
||||
- (nonnull FlutterTextInputPlugin*)textInputPlugin;
|
||||
/// OCMock does not allow mocking both class and protocol. Use this to mock the methods used on
|
||||
/// `UIView<UITextInput>*` in the plugin.
|
||||
@interface TextInputViewTest : NSObject
|
||||
|
||||
@property(nonatomic, weak) id<UITextInputDelegate> inputDelegate;
|
||||
@property(nonatomic, readonly) UITextInputAssistantItem* inputAssistantItem;
|
||||
|
||||
@end
|
||||
|
||||
@interface FlutterUndoManagerPluginForTest : FlutterUndoManagerPlugin
|
||||
@property(nonatomic, assign) NSUndoManager* undoManager;
|
||||
@implementation TextInputViewTest
|
||||
@end
|
||||
|
||||
@implementation FlutterUndoManagerPluginForTest {
|
||||
@interface FakeFlutterUndoManagerDelegate : NSObject <FlutterUndoManagerDelegate>
|
||||
|
||||
@property(readonly) NSUInteger undoCount;
|
||||
@property(readonly) NSUInteger redoCount;
|
||||
|
||||
- (instancetype)initWithUndoManager:(NSUndoManager*)undoManager
|
||||
activeTextInputView:(TextInputViewTest*)activeTextInputView;
|
||||
|
||||
@end
|
||||
|
||||
@implementation FakeFlutterUndoManagerDelegate
|
||||
|
||||
@synthesize undoManager = _undoManager;
|
||||
@synthesize activeTextInputView = _activeTextInputView;
|
||||
|
||||
- (instancetype)initWithUndoManager:(NSUndoManager*)undoManager
|
||||
activeTextInputView:(UIView<UITextInput>*)activeTextInputView {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_undoManager = undoManager;
|
||||
_activeTextInputView = activeTextInputView;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)handleUndoWithDirection:(FlutterUndoRedoDirection)direction {
|
||||
if (direction == FlutterUndoRedoDirectionUndo) {
|
||||
_undoCount++;
|
||||
} else {
|
||||
_redoCount++;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface FlutterUndoManagerPluginTest : XCTestCase
|
||||
@property(nonatomic, strong) id engine;
|
||||
@property(nonatomic, strong) FlutterUndoManagerPluginForTest* undoManagerPlugin;
|
||||
@property(nonatomic, strong) FlutterViewController* viewController;
|
||||
@property(nonatomic, strong) NSUndoManager* undoManager;
|
||||
@property(nonatomic) FakeFlutterUndoManagerDelegate* undoManagerDelegate;
|
||||
@property(nonatomic) FlutterUndoManagerPlugin* undoManagerPlugin;
|
||||
@property(nonatomic) TextInputViewTest* activeTextInputView;
|
||||
@property(nonatomic) NSUndoManager* undoManager;
|
||||
@end
|
||||
|
||||
@implementation FlutterUndoManagerPluginTest {
|
||||
}
|
||||
@implementation FlutterUndoManagerPluginTest
|
||||
|
||||
- (void)setUp {
|
||||
[super setUp];
|
||||
self.engine = OCMClassMock([FlutterEngine class]);
|
||||
|
||||
self.undoManagerPlugin = [[FlutterUndoManagerPluginForTest alloc] initWithDelegate:self.engine];
|
||||
|
||||
self.viewController = [[FlutterViewController alloc] init];
|
||||
self.undoManagerPlugin.viewController = self.viewController;
|
||||
|
||||
self.undoManager = OCMClassMock([NSUndoManager class]);
|
||||
self.undoManagerPlugin.undoManager = self.undoManager;
|
||||
}
|
||||
self.activeTextInputView = OCMClassMock([TextInputViewTest class]);
|
||||
|
||||
- (void)tearDown {
|
||||
[self.undoManager removeAllActionsWithTarget:self.undoManagerPlugin];
|
||||
self.engine = nil;
|
||||
self.viewController = nil;
|
||||
self.undoManager = nil;
|
||||
[super tearDown];
|
||||
self.undoManagerDelegate =
|
||||
[[FakeFlutterUndoManagerDelegate alloc] initWithUndoManager:self.undoManager
|
||||
activeTextInputView:self.activeTextInputView];
|
||||
|
||||
self.undoManagerPlugin =
|
||||
[[FlutterUndoManagerPlugin alloc] initWithDelegate:self.undoManagerDelegate];
|
||||
}
|
||||
|
||||
- (void)testSetUndoState {
|
||||
@@ -74,18 +97,6 @@ FLUTTER_ASSERT_ARC
|
||||
.andDo(^(NSInvocation* invocation) {
|
||||
removeAllActionsCount++;
|
||||
});
|
||||
__block int delegateUndoCount = 0;
|
||||
OCMStub([self.engine flutterUndoManagerPlugin:[OCMArg any]
|
||||
handleUndoWithDirection:FlutterUndoRedoDirectionUndo])
|
||||
.andDo(^(NSInvocation* invocation) {
|
||||
delegateUndoCount++;
|
||||
});
|
||||
__block int delegateRedoCount = 0;
|
||||
OCMStub([self.engine flutterUndoManagerPlugin:[OCMArg any]
|
||||
handleUndoWithDirection:FlutterUndoRedoDirectionRedo])
|
||||
.andDo(^(NSInvocation* invocation) {
|
||||
delegateRedoCount++;
|
||||
});
|
||||
__block int undoCount = 0;
|
||||
OCMStub([self.undoManager undo]).andDo(^(NSInvocation* invocation) {
|
||||
undoCount++;
|
||||
@@ -114,14 +125,14 @@ FLUTTER_ASSERT_ARC
|
||||
|
||||
// Invoking the undo handler will invoke the handleUndo delegate method with "undo".
|
||||
undoHandler(self.undoManagerPlugin);
|
||||
XCTAssertEqual(1, delegateUndoCount);
|
||||
XCTAssertEqual(0, delegateRedoCount);
|
||||
XCTAssertEqual(1UL, self.undoManagerDelegate.undoCount);
|
||||
XCTAssertEqual(0UL, self.undoManagerDelegate.redoCount);
|
||||
XCTAssertEqual(2, registerUndoCount);
|
||||
|
||||
// Invoking the redo handler will invoke the handleUndo delegate method with "redo".
|
||||
undoHandler(self.undoManagerPlugin);
|
||||
XCTAssertEqual(1, delegateUndoCount);
|
||||
XCTAssertEqual(1, delegateRedoCount);
|
||||
XCTAssertEqual(1UL, self.undoManagerDelegate.undoCount);
|
||||
XCTAssertEqual(1UL, self.undoManagerDelegate.redoCount);
|
||||
XCTAssertEqual(3, registerUndoCount);
|
||||
|
||||
// If canRedo is true, an undo will be registered and undo will be called.
|
||||
@@ -137,22 +148,12 @@ FLUTTER_ASSERT_ARC
|
||||
|
||||
// Invoking the redo handler will invoke the handleUndo delegate method with "redo".
|
||||
undoHandler(self.undoManagerPlugin);
|
||||
XCTAssertEqual(1, delegateUndoCount);
|
||||
XCTAssertEqual(2, delegateRedoCount);
|
||||
XCTAssertEqual(1UL, self.undoManagerDelegate.undoCount);
|
||||
XCTAssertEqual(2UL, self.undoManagerDelegate.redoCount);
|
||||
}
|
||||
|
||||
- (void)testSetUndoStateDoesInteractWithInputDelegate {
|
||||
// Regression test for https://github.com/flutter/flutter/issues/133424
|
||||
FlutterViewController* viewController = OCMPartialMock(self.viewController);
|
||||
self.undoManagerPlugin.viewController = self.viewController;
|
||||
|
||||
FlutterTextInputPlugin* textInputPlugin = OCMClassMock([FlutterTextInputPlugin class]);
|
||||
FlutterTextInputView* textInputView = OCMClassMock([FlutterTextInputView class]);
|
||||
|
||||
OCMStub([viewController engine]).andReturn(self.engine);
|
||||
OCMStub([self.engine textInputPlugin]).andReturn(textInputPlugin);
|
||||
OCMStub([textInputPlugin textInputView]).andReturn(textInputView);
|
||||
|
||||
FlutterMethodCall* setUndoStateCall =
|
||||
[FlutterMethodCall methodCallWithMethodName:@"UndoManager.setUndoState"
|
||||
arguments:@{@"canUndo" : @NO, @"canRedo" : @NO}];
|
||||
@@ -160,7 +161,7 @@ FLUTTER_ASSERT_ARC
|
||||
result:^(id _Nullable result){
|
||||
}];
|
||||
|
||||
OCMVerify(never(), [textInputView inputDelegate]);
|
||||
OCMVerify(never(), [self.activeTextInputView inputDelegate]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user