33// found in the LICENSE file.
44
55#import " flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h"
6- #import " flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
76
8- #import < Foundation/Foundation.h>
9- #import < UIKit/UIKit.h>
10-
11- #include " flutter/fml/logging.h"
7+ #import " flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
128
139#pragma mark - UndoManager channel method names.
1410static NSString * const kSetUndoStateMethod = @" UndoManager.setUndoState" ;
1713static NSString * const kCanUndo = @" canUndo" ;
1814static NSString * const kCanRedo = @" canRedo" ;
1915
20- @implementation FlutterUndoManagerPlugin {
21- id <FlutterUndoManagerDelegate> _undoManagerDelegate;
22- }
16+ @interface FlutterUndoManagerPlugin ()
17+ @property (nonatomic , weak , readonly ) id <FlutterUndoManagerDelegate> undoManagerDelegate;
18+ @end
19+
20+ @implementation FlutterUndoManagerPlugin
2321
2422- (instancetype )initWithDelegate : (id <FlutterUndoManagerDelegate>)undoManagerDelegate {
2523 self = [super init ];
2624
2725 if (self) {
28- // `_undoManagerDelegate` is a weak reference because it should retain FlutterUndoManagerPlugin.
2926 _undoManagerDelegate = undoManagerDelegate;
3027 }
3128
@@ -34,7 +31,6 @@ - (instancetype)initWithDelegate:(id<FlutterUndoManagerDelegate>)undoManagerDele
3431
3532- (void )dealloc {
3633 [self resetUndoManager ];
37- [super dealloc ];
3834}
3935
4036- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
@@ -49,45 +45,47 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
4945}
5046
5147- (NSUndoManager *)undoManager {
52- return _viewController .undoManager ;
48+ return self. undoManagerDelegate . viewController .undoManager ;
5349}
5450
5551- (void )resetUndoManager API_AVAILABLE(ios(9.0 )) {
5652 [[self undoManager ] removeAllActionsWithTarget: self ];
5753}
5854
5955- (void )registerUndoWithDirection : (FlutterUndoRedoDirection)direction API_AVAILABLE(ios(9.0 )) {
60- [[self undoManager ] beginUndoGrouping ];
61- [[self undoManager ] registerUndoWithTarget: self
62- handler: ^(id target) {
63- // Register undo with opposite direction.
64- FlutterUndoRedoDirection newDirection =
65- (direction == FlutterUndoRedoDirectionRedo)
66- ? FlutterUndoRedoDirectionUndo
67- : FlutterUndoRedoDirectionRedo;
68- [target registerUndoWithDirection: newDirection];
69- // Invoke method on delegate.
70- [_undoManagerDelegate flutterUndoManagerPlugin: self
71- handleUndoWithDirection: direction];
72- }];
73- [[self undoManager ] endUndoGrouping ];
56+ NSUndoManager * undoManager = [self undoManager ];
57+ [undoManager beginUndoGrouping ];
58+ [undoManager registerUndoWithTarget: self
59+ handler: ^(FlutterUndoManagerPlugin* target) {
60+ // Register undo with opposite direction.
61+ FlutterUndoRedoDirection newDirection =
62+ (direction == FlutterUndoRedoDirectionRedo)
63+ ? FlutterUndoRedoDirectionUndo
64+ : FlutterUndoRedoDirectionRedo;
65+ [target registerUndoWithDirection: newDirection];
66+ // Invoke method on delegate.
67+ [target.undoManagerDelegate flutterUndoManagerPlugin: target
68+ handleUndoWithDirection: direction];
69+ }];
70+ [undoManager endUndoGrouping ];
7471}
7572
7673- (void )registerRedo API_AVAILABLE(ios(9.0 )) {
77- [[ self undoManager ] beginUndoGrouping ];
78- [[ self undoManager ]
79- registerUndoWithTarget: self
80- handler: ^(id target) {
81- // Register undo with opposite direction.
82- [target registerUndoWithDirection: FlutterUndoRedoDirectionRedo];
83- }];
84- [[ self undoManager ] endUndoGrouping ];
85- [[ self undoManager ] undo ];
74+ NSUndoManager * undoManager = [ self undoManager ];
75+ [undoManager beginUndoGrouping ];
76+ [undoManager registerUndoWithTarget: self
77+ handler: ^(id target) {
78+ // Register undo with opposite direction.
79+ [target registerUndoWithDirection: FlutterUndoRedoDirectionRedo];
80+ }];
81+ [undoManager endUndoGrouping ];
82+ [undoManager undo ];
8683}
8784
8885- (void )setUndoState : (NSDictionary *)dictionary API_AVAILABLE(ios(9.0 )) {
89- BOOL groupsByEvent = [self undoManager ].groupsByEvent ;
90- [self undoManager ].groupsByEvent = NO ;
86+ NSUndoManager * undoManager = [self undoManager ];
87+ BOOL groupsByEvent = undoManager.groupsByEvent ;
88+ undoManager.groupsByEvent = NO ;
9189 BOOL canUndo = [dictionary[kCanUndo ] boolValue ];
9290 BOOL canRedo = [dictionary[kCanRedo ] boolValue ];
9391
@@ -99,16 +97,15 @@ - (void)setUndoState:(NSDictionary*)dictionary API_AVAILABLE(ios(9.0)) {
9997 if (canRedo) {
10098 [self registerRedo ];
10199 }
102-
103- if (_viewController. engine . textInputPlugin . textInputView != nil ) {
100+ UIView<UITextInput>* textInputView = [ self .undoManagerDelegate.textInputPlugin textInputView ];
101+ if (textInputView != nil ) {
104102 // This is needed to notify the iPadOS keyboard that it needs to update the
105103 // state of the UIBarButtons. Otherwise, the state changes to NSUndoManager
106104 // will not show up until the next keystroke (or other trigger).
107- UITextInputAssistantItem* assistantItem =
108- _viewController.engine .textInputPlugin .textInputView .inputAssistantItem ;
105+ UITextInputAssistantItem* assistantItem = textInputView.inputAssistantItem ;
109106 assistantItem.leadingBarButtonGroups = assistantItem.leadingBarButtonGroups ;
110107 }
111- [ self undoManager ] .groupsByEvent = groupsByEvent;
108+ undoManager.groupsByEvent = groupsByEvent;
112109}
113110
114111@end
0 commit comments