Flutter iOS Embedder
FlutterKeyboardManager Class Reference

#import <FlutterKeyboardManager.h>

Inheritance diagram for FlutterKeyboardManager:

Instance Methods

(void) - addPrimaryResponder:
 
(void) - addSecondaryResponder:
 
(void) - handlePress:nextAction:
 

Detailed Description

A hub that manages how key events are dispatched to various Flutter key responders, and propagates it to the superclass if the Flutter key responders do not handle it.

This class manages one or more primary responders, as well as zero or more secondary responders.

An event that is received by |handlePresses| is first dispatched to all primary responders. Each primary responder responds asynchronously with a boolean, indicating whether it handles the event.

An event that is not handled by any primary responders is then passed to to the first secondary responder (in the chronological order of addition), which responds synchronously with a boolean, indicating whether it handles the event. If not, the event is passed to the next secondary responder, and so on.

The event is then handed back to the |completeCallback| from the original call to |handlePresses| so that it can respond synchronously to the OS if the event was not handled by the responders. The |completeCallback| is called on the platform thread because the platform thread is blocked by a nested event loop while the response from the framework is being collected, and it needs to be called on the platform thread to unblock the thread by exiting the nested event loop.

Preventing primary responders from receiving events is not supported, because in reality this class only supports two hardcoded responders (FlutterChannelKeyResponder and FlutterEmbedderKeyResponder), where the only purpose of supporting two is to maintain the legacy channel API during the deprecation window, after which the channel responder should be removed, and only one primary responder will exist.

Definition at line 53 of file FlutterKeyboardManager.h.

Method Documentation

◆ addPrimaryResponder:

- (void) addPrimaryResponder: (nonnull id<FlutterKeyPrimaryResponder>)  responder

Add a primary responder, which asynchronously decides whether to handle an event.

Definition at line 45 of file FlutterKeyboardManager.mm.

45  :(nonnull id<FlutterKeyPrimaryResponder>)responder {
46  [_primaryResponders addObject:responder];
47 }

◆ addSecondaryResponder:

- (void) addSecondaryResponder: (nonnull id<FlutterKeySecondaryResponder>)  responder

Add a secondary responder, which synchronously decides whether to handle an event in order if no earlier responders handle.

Definition at line 49 of file FlutterKeyboardManager.mm.

49  :(nonnull id<FlutterKeySecondaryResponder>)responder {
50  [_secondaryResponders addObject:responder];
51 }

◆ handlePress:nextAction:

- (void) handlePress: (nonnull FlutterUIPressProxy*)  press
nextAction: (ios(13.4))  API_AVAILABLE 

Dispatches a key press event to all responders, gathering their responses, and then calls the |nextAction| if the event was not handled.

Definition at line 53 of file FlutterKeyboardManager.mm.

53  :(nonnull FlutterUIPressProxy*)press
54  nextAction:(nonnull void (^)())next API_AVAILABLE(ios(13.4)) {
55  if (@available(iOS 13.4, *)) {
56  // no-op
57  } else {
58  return;
59  }
60 
61  bool __block wasHandled = false;
62  KeyEventCompleteCallback completeCallback = ^void(bool handled, FlutterUIPressProxy* press) {
63  wasHandled = handled;
64  CFRunLoopStop(CFRunLoopGetCurrent());
65  };
66  switch (press.phase) {
67  case UIPressPhaseBegan:
68  case UIPressPhaseEnded: {
69  // Having no primary responders requires extra logic, but Flutter hard-codes
70  // all primary responders, so this is a situation that Flutter will never
71  // encounter.
72  NSAssert([_primaryResponders count] >= 0, @"At least one primary responder must be added.");
73 
74  __block __weak __typeof(self) weakSelf = self;
75  __block NSUInteger unreplied = [self.primaryResponders count];
76  __block BOOL anyHandled = false;
77  FlutterAsyncKeyCallback replyCallback = ^(BOOL handled) {
78  unreplied--;
79  NSAssert(unreplied >= 0, @"More primary responders replied than expected.");
80  anyHandled = anyHandled || handled;
81  if (unreplied == 0) {
82  if (!anyHandled && weakSelf) {
83  [weakSelf dispatchToSecondaryResponders:press complete:completeCallback];
84  } else {
85  completeCallback(true, press);
86  }
87  }
88  };
89  for (id<FlutterKeyPrimaryResponder> responder in _primaryResponders) {
90  [responder handlePress:press callback:replyCallback];
91  }
92  // Create a nested run loop while we wait for a response from the
93  // framework. Once the completeCallback is called, this run loop will exit
94  // and the main one will resume. The completeCallback MUST be called, or
95  // the app will get stuck in this run loop indefinitely.
96  //
97  // We need to run in this mode so that UIKit doesn't give us new
98  // events until we are done processing this one.
99  CFRunLoopRunInMode(fml::MessageLoopDarwin::kMessageLoopCFRunLoopMode, kDistantFuture, NO);
100  break;
101  }
102  case UIPressPhaseChanged:
103  case UIPressPhaseCancelled:
104  case UIPressPhaseStationary:
105  break;
106  }
107  if (!wasHandled) {
108  next();
109  }
110 }

References kDistantFuture.


The documentation for this class was generated from the following files:
self
return self
Definition: FlutterTextureRegistryRelay.mm:19
API_AVAILABLE
UITextSmartQuotesType smartQuotesType API_AVAILABLE(ios(11.0))
kDistantFuture
static constexpr FLUTTER_ASSERT_ARC CFTimeInterval kDistantFuture
Definition: FlutterKeyboardManager.mm:12
FlutterAsyncKeyCallback
void(^ FlutterAsyncKeyCallback)(BOOL handled)
Definition: FlutterKeyPrimaryResponder.h:10
KeyEventCompleteCallback
void(^ KeyEventCompleteCallback)(bool, FlutterUIPressProxy *_Nonnull) API_AVAILABLE(ios(13.4))
Definition: FlutterKeyboardManager.h:17
FlutterUIPressProxy
Definition: FlutterUIPressProxy.h:17