Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cfbd14e

Browse files
committed
Migrate FlutterEmbedderKeyResponder to ARC
1 parent 76a270f commit cfbd14e

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

shell/platform/darwin/ios/BUILD.gn

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ source_set("flutter_framework_source_arc") {
5959
public_configs = [ "//flutter:config" ]
6060

6161
sources = [
62+
"framework/Source/FlutterEmbedderKeyResponder.h",
63+
"framework/Source/FlutterEmbedderKeyResponder.mm",
64+
"framework/Source/FlutterKeyPrimaryResponder.h",
65+
"framework/Source/FlutterKeySecondaryResponder.h",
6266
"framework/Source/FlutterMetalLayer.h",
6367
"framework/Source/FlutterMetalLayer.mm",
6468
"framework/Source/FlutterRestorationPlugin.h",
@@ -78,6 +82,8 @@ source_set("flutter_framework_source_arc") {
7882
"UIKit.framework",
7983
"IOSurface.framework",
8084
]
85+
86+
deps += [ "//flutter/shell/platform/embedder:embedder_as_internal_library" ]
8187
}
8288

8389
source_set("flutter_framework_source") {
@@ -100,14 +106,10 @@ source_set("flutter_framework_source") {
100106
"framework/Source/FlutterDartProject_Internal.h",
101107
"framework/Source/FlutterDartVMServicePublisher.h",
102108
"framework/Source/FlutterDartVMServicePublisher.mm",
103-
"framework/Source/FlutterEmbedderKeyResponder.h",
104-
"framework/Source/FlutterEmbedderKeyResponder.mm",
105109
"framework/Source/FlutterEngine.mm",
106110
"framework/Source/FlutterEngineGroup.mm",
107111
"framework/Source/FlutterEngine_Internal.h",
108112
"framework/Source/FlutterHeadlessDartRunner.mm",
109-
"framework/Source/FlutterKeyPrimaryResponder.h",
110-
"framework/Source/FlutterKeySecondaryResponder.h",
111113
"framework/Source/FlutterKeyboardManager.h",
112114
"framework/Source/FlutterKeyboardManager.mm",
113115
"framework/Source/FlutterOverlayView.h",

shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponder.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTEREMBEDDERKEYRESPONDER_H_
66
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTEREMBEDDERKEYRESPONDER_H_
77

8-
#import <Foundation/NSObject.h>
9-
#import <UIKit/UIKit.h>
8+
#import <Foundation/Foundation.h>
109

11-
#include "fml/memory/weak_ptr.h"
12-
13-
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
1410
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterKeyPrimaryResponder.h"
1511
#import "flutter/shell/platform/embedder/embedder.h"
1612

shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponder.mm

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
#import "KeyCodeMap_Internal.h"
1313
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
14+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
15+
16+
FLUTTER_ASSERT_ARC
1417

1518
namespace {
1619

@@ -165,7 +168,7 @@ static uint64_t GetLogicalKeyForEvent(FlutterUIPressProxy* press, NSNumber* mayb
165168
const char* characters =
166169
getEventCharacters(press.key.charactersIgnoringModifiers, press.key.keyCode);
167170
NSString* keyLabel =
168-
characters == nullptr ? nil : [[[NSString alloc] initWithUTF8String:characters] autorelease];
171+
characters == nullptr ? nil : [[NSString alloc] initWithUTF8String:characters];
169172
NSUInteger keyLabelLength = [keyLabel length];
170173
// If this key is printable, generate the logical key from its Unicode
171174
// value. Control keys such as ESC, CTRL, and SHIFT are not printable. HOME,
@@ -252,7 +255,7 @@ static bool isKeyDown(FlutterUIPressProxy* press) API_AVAILABLE(ios(13.4)) {
252255
*/
253256
@interface FlutterKeyPendingResponse : NSObject
254257

255-
@property(readonly) FlutterEmbedderKeyResponder* responder;
258+
@property(readonly, weak) FlutterEmbedderKeyResponder* responder;
256259

257260
@property(nonatomic) uint64_t responseId;
258261

@@ -302,7 +305,7 @@ - (void)resolveTo:(BOOL)handled;
302305
* Only set in debug mode. Nil in release mode, or if the callback has not been
303306
* handled.
304307
*/
305-
@property(readonly) NSString* debugHandleSource;
308+
@property(readonly, copy) NSString* debugHandleSource;
306309
@end
307310

308311
@implementation FlutterKeyCallbackGuard {
@@ -313,17 +316,12 @@ @implementation FlutterKeyCallbackGuard {
313316
- (nonnull instancetype)initWithCallback:(FlutterAsyncKeyCallback)callback {
314317
self = [super init];
315318
if (self != nil) {
316-
_callback = [callback copy];
319+
_callback = callback;
317320
_handled = FALSE;
318321
}
319322
return self;
320323
}
321324

322-
- (void)dealloc {
323-
[_callback release];
324-
[super dealloc];
325-
}
326-
327325
- (void)pendTo:(nonnull NSMutableDictionary<NSNumber*, FlutterAsyncKeyCallback>*)pendingResponses
328326
withId:(uint64_t)responseId {
329327
NSAssert(!_handled, @"This callback has been handled by %@.", _debugHandleSource);
@@ -356,15 +354,15 @@ @interface FlutterEmbedderKeyResponder ()
356354
*
357355
* Set by the initializer.
358356
*/
359-
@property(nonatomic, copy, readonly) FlutterSendKeyEvent sendEvent;
357+
@property(nonatomic, readonly) FlutterSendKeyEvent sendEvent;
360358

361359
/**
362360
* A map of pressed keys.
363361
*
364362
* The keys of the dictionary are physical keys, while the values are the logical keys
365363
* of the key down event.
366364
*/
367-
@property(nonatomic, retain, readonly) NSMutableDictionary<NSNumber*, NSNumber*>* pressingRecords;
365+
@property(nonatomic, readonly) NSMutableDictionary<NSNumber*, NSNumber*>* pressingRecords;
368366

369367
/**
370368
* A constant mask for NSEvent.modifierFlags that Flutter synchronizes with.
@@ -403,7 +401,7 @@ @interface FlutterEmbedderKeyResponder ()
403401
* Its values are |responseId|s, and keys are the callback that was received
404402
* along with the event.
405403
*/
406-
@property(nonatomic, retain, readonly)
404+
@property(nonatomic, readonly)
407405
NSMutableDictionary<NSNumber*, FlutterAsyncKeyCallback>* pendingResponses;
408406

409407
/**
@@ -492,7 +490,7 @@ @implementation FlutterEmbedderKeyResponder
492490
- (nonnull instancetype)initWithSendEvent:(FlutterSendKeyEvent)sendEvent {
493491
self = [super init];
494492
if (self != nil) {
495-
_sendEvent = [sendEvent copy];
493+
_sendEvent = sendEvent;
496494
_pressingRecords = [[NSMutableDictionary alloc] init];
497495
_pendingResponses = [[NSMutableDictionary alloc] init];
498496
_responseId = 1;
@@ -502,13 +500,6 @@ - (nonnull instancetype)initWithSendEvent:(FlutterSendKeyEvent)sendEvent {
502500
return self;
503501
}
504502

505-
- (void)dealloc {
506-
[_sendEvent release];
507-
[_pressingRecords release];
508-
[_pendingResponses release];
509-
[super dealloc];
510-
}
511-
512503
- (void)handlePress:(nonnull FlutterUIPressProxy*)press
513504
callback:(FlutterAsyncKeyCallback)callback API_AVAILABLE(ios(13.4)) {
514505
if (@available(iOS 13.4, *)) {
@@ -522,11 +513,11 @@ - (void)handlePress:(nonnull FlutterUIPressProxy*)press
522513
FlutterKeyCallbackGuard* guardedCallback = nil;
523514
switch (press.phase) {
524515
case UIPressPhaseBegan:
525-
guardedCallback = [[[FlutterKeyCallbackGuard alloc] initWithCallback:callback] autorelease];
516+
guardedCallback = [[FlutterKeyCallbackGuard alloc] initWithCallback:callback];
526517
[self handlePressBegin:press callback:guardedCallback];
527518
break;
528519
case UIPressPhaseEnded:
529-
guardedCallback = [[[FlutterKeyCallbackGuard alloc] initWithCallback:callback] autorelease];
520+
guardedCallback = [[FlutterKeyCallbackGuard alloc] initWithCallback:callback];
530521
[self handlePressEnd:press callback:guardedCallback];
531522
break;
532523
case UIPressPhaseChanged:
@@ -632,9 +623,9 @@ - (void)sendPrimaryFlutterEvent:(const FlutterKeyEvent&)event
632623
_responseId += 1;
633624
uint64_t responseId = _responseId;
634625
FlutterKeyPendingResponse* pending =
635-
[[[FlutterKeyPendingResponse alloc] initWithHandler:self responseId:responseId] autorelease];
626+
[[FlutterKeyPendingResponse alloc] initWithHandler:self responseId:responseId];
636627
[callback pendTo:_pendingResponses withId:responseId];
637-
_sendEvent(event, HandleResponse, pending);
628+
_sendEvent(event, HandleResponse, (__bridge_retained void* _Nullable)pending);
638629
}
639630

640631
- (void)sendEmptyEvent {
@@ -883,7 +874,7 @@ - (UInt32)adjustModifiers:(nonnull FlutterUIPressProxy*)press API_AVAILABLE(ios(
883874

884875
namespace {
885876
void HandleResponse(bool handled, void* user_data) {
886-
FlutterKeyPendingResponse* pending = reinterpret_cast<FlutterKeyPendingResponse*>(user_data);
877+
FlutterKeyPendingResponse* pending = (__bridge_transfer FlutterKeyPendingResponse*)user_data;
887878
[pending.responder handleResponse:handled forId:pending.responseId];
888879
}
889880
} // namespace

shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponderTest.mm

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,13 @@
2727
@interface TestKeyEvent : NSObject
2828
@property(nonatomic) FlutterKeyEvent* data;
2929
@property(nonatomic) FlutterKeyEventCallback callback;
30-
@property(nonatomic) void* _Nullable userData;
31-
- (nonnull instancetype)initWithEvent:(const FlutterKeyEvent*)event
32-
callback:(nullable FlutterKeyEventCallback)callback
33-
userData:(void* _Nullable)userData;
34-
- (BOOL)hasCallback;
35-
- (void)respond:(BOOL)handled;
30+
@property(nonatomic, nullable) void* userData;
3631
@end
3732

3833
@implementation TestKeyEvent
3934
- (instancetype)initWithEvent:(const FlutterKeyEvent*)event
4035
callback:(nullable FlutterKeyEventCallback)callback
41-
userData:(void* _Nullable)userData {
36+
userData:(nullable void*)userData {
4237
self = [super init];
4338
_data = new FlutterKeyEvent(*event);
4439
if (event->character != nullptr) {

0 commit comments

Comments
 (0)