Flutter Engine
The Flutter Engine
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
7#pragma mark - UndoManager channel method names.
8static NSString* const kSetUndoStateMethod = @"UndoManager.setUndoState";
9
10#pragma mark - Undo State field names
11static NSString* const kCanUndo = @"canUndo";
12static NSString* const kCanRedo = @"canRedo";
13
14@interface FlutterUndoManagerPlugin ()
15@property(nonatomic, weak, readonly) id<FlutterUndoManagerDelegate> undoManagerDelegate;
16@end
17
18@implementation FlutterUndoManagerPlugin
19
20- (instancetype)initWithDelegate:(id<FlutterUndoManagerDelegate>)undoManagerDelegate {
21 self = [super init];
22
23 if (self) {
24 _undoManagerDelegate = undoManagerDelegate;
25 }
26
27 return self;
28}
29
30- (void)dealloc {
31 [_undoManagerDelegate.undoManager removeAllActionsWithTarget:self];
32}
33
34- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
35 NSString* method = call.method;
36 id args = call.arguments;
37 if ([method isEqualToString:kSetUndoStateMethod]) {
38 [self setUndoState:args];
39 result(nil);
40 } else {
42 }
43}
44
45- (void)resetUndoManager {
46 [self.undoManagerDelegate.undoManager removeAllActionsWithTarget:self];
47}
48
49- (void)registerUndoWithDirection:(FlutterUndoRedoDirection)direction {
50 NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
51 [undoManager beginUndoGrouping];
52 [undoManager registerUndoWithTarget:self
53 handler:^(FlutterUndoManagerPlugin* target) {
54 // Register undo with opposite direction.
55 FlutterUndoRedoDirection newDirection =
56 (direction == FlutterUndoRedoDirectionRedo)
57 ? FlutterUndoRedoDirectionUndo
58 : FlutterUndoRedoDirectionRedo;
59 [target registerUndoWithDirection:newDirection];
60 // Invoke method on delegate.
61 [target.undoManagerDelegate handleUndoWithDirection:direction];
62 }];
63 [undoManager endUndoGrouping];
64}
65
66- (void)registerRedo {
67 NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
68 [undoManager beginUndoGrouping];
69 [undoManager registerUndoWithTarget:self
70 handler:^(id target) {
71 // Register undo with opposite direction.
72 [target registerUndoWithDirection:FlutterUndoRedoDirectionRedo];
73 }];
74 [undoManager endUndoGrouping];
75 [undoManager undo];
76}
77
78- (void)setUndoState:(NSDictionary*)dictionary {
79 NSUndoManager* undoManager = self.undoManagerDelegate.undoManager;
80 BOOL groupsByEvent = undoManager.groupsByEvent;
81 undoManager.groupsByEvent = NO;
82 BOOL canUndo = [dictionary[kCanUndo] boolValue];
83 BOOL canRedo = [dictionary[kCanRedo] boolValue];
84
85 [self resetUndoManager];
86
87 if (canUndo) {
88 [self registerUndoWithDirection:FlutterUndoRedoDirectionUndo];
89 }
90 if (canRedo) {
91 [self registerRedo];
92 }
93 UIView<UITextInput>* textInputView = self.undoManagerDelegate.activeTextInputView;
94 if (textInputView != nil) {
95 // This is needed to notify the iPadOS keyboard that it needs to update the
96 // state of the UIBarButtons. Otherwise, the state changes to NSUndoManager
97 // will not show up until the next keystroke (or other trigger).
98 UITextInputAssistantItem* assistantItem = textInputView.inputAssistantItem;
99 assistantItem.leadingBarButtonGroups = assistantItem.leadingBarButtonGroups;
100 }
101 undoManager.groupsByEvent = groupsByEvent;
102}
103
104@end
void(^ FlutterResult)(id _Nullable result)
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
static NSString *const kSetUndoStateMethod
static NSString *const kCanUndo
static NSString *const kCanRedo
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
id< FlutterUndoManagerDelegate > undoManagerDelegate
def call(args)
Definition: dom.py:159
int BOOL
Definition: windows_types.h:37