| Flutter iOS Embedder |

FlutterUndoManagerPlugin.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h"
6 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
7
8 #import <Foundation/Foundation.h>
9 #import <UIKit/UIKit.h>
10
11 #include "flutter/fml/logging.h"
12
13 #pragma mark - UndoManager channel method names.
14 static NSString* const kSetUndoStateMethod = @"UndoManager.setUndoState";
15
16 #pragma mark - Undo State field names
17 static NSString* const kCanUndo = @"canUndo";
18 static NSString* const kCanRedo = @"canRedo";
19
20 @implementation FlutterUndoManagerPlugin {
21 id<FlutterUndoManagerDelegate> _undoManagerDelegate;
22 }
23
24 - (instancetype)initWithDelegate:(id<FlutterUndoManagerDelegate>)undoManagerDelegate {
25 self = [super init];
26
27 if (self) {
28 // `_undoManagerDelegate` is a weak reference because it should retain FlutterUndoManagerPlugin.
29 _undoManagerDelegate = undoManagerDelegate;
30 }
31
32 return self;
33 }
34
35 - (void)dealloc {
36 [self resetUndoManager];
37 [super dealloc];
38 }
39
40 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
41 NSString* method = call.method;
42 id args = call.arguments;
43 if ([method isEqualToString:kSetUndoStateMethod]) {
44 [self setUndoState:args];
45 result(nil);
46 } else {
47 result(FlutterMethodNotImplemented);
48 }
49 }
50
51 - (NSUndoManager*)undoManager {
52 return _viewController.undoManager;
53 }
54
55 - (void)resetUndoManager API_AVAILABLE(ios(9.0)) {
56 [[self undoManager] removeAllActionsWithTarget:self];
57 }
58
59 - (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];
74 }
75
76 - (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];
86 }
87
88 - (void)setUndoState:(NSDictionary*)dictionary API_AVAILABLE(ios(9.0)) {
89 BOOL groupsByEvent = [self undoManager].groupsByEvent;
90 [self undoManager].groupsByEvent = NO;
91 BOOL canUndo = [dictionary[kCanUndo] boolValue];
92 BOOL canRedo = [dictionary[kCanRedo] boolValue];
93
94 [self resetUndoManager];
95
96 if (canUndo) {
97 [self registerUndoWithDirection:FlutterUndoRedoDirectionUndo];
98 }
99 if (canRedo) {
100 [self registerRedo];
101 }
102
103 if (_viewController.engine.textInputPlugin.textInputView != nil) {
104 // This is needed to notify the iPadOS keyboard that it needs to update the
105 // state of the UIBarButtons. Otherwise, the state changes to NSUndoManager
106 // will not show up until the next keystroke (or other trigger).
107 UITextInputAssistantItem* assistantItem =
108 _viewController.engine.textInputPlugin.textInputView.inputAssistantItem;
109 assistantItem.leadingBarButtonGroups = assistantItem.leadingBarButtonGroups;
110 }
111 [self undoManager].groupsByEvent = groupsByEvent;
112 }
113
114 @end
_viewController
fml::WeakNSObject< FlutterViewController > _viewController
Definition: FlutterEngine.mm:122
FlutterMethodNotImplemented
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
kCanRedo
static NSString *const kCanRedo
Definition: FlutterUndoManagerPlugin.mm:18
FlutterUndoManagerPlugin.h
FlutterEngine_Internal.h
API_AVAILABLE
UITextSmartQuotesType smartQuotesType API_AVAILABLE(ios(11.0))
FlutterMethodCall::method
NSString * method
Definition: FlutterCodecs.h:233
FlutterMethodCall
Definition: FlutterCodecs.h:220
kSetUndoStateMethod
static NSString *const kSetUndoStateMethod
Definition: FlutterUndoManagerPlugin.mm:14
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
FlutterUndoManagerDelegate-p
Definition: FlutterUndoManagerDelegate.h:21
FlutterUndoManagerPlugin
Definition: FlutterUndoManagerPlugin.h:15
kCanUndo
static NSString *const kCanUndo
Definition: FlutterUndoManagerPlugin.mm:17
FlutterMethodCall::arguments
id arguments
Definition: FlutterCodecs.h:238