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

Commit a63841d

Browse files
authored
iOS: Clean up @synthesize directives / ivars (#56665)
In modern Objective-C, `@property` directives automatically generate a backing ivar (property name prefixed with an underscore), the getter, and (for readwrite properties) the setter. `@synthesize` directives are generally only required if the backing ivar has a different name from the property. Also updates the FlutterMetalLayer API to match CAMetalLayer: * Adds API_AVAILABLE declaration to match that on CAMetalLayer. * Eliminates wantsExtendedDynamicRangeContent property as it's also part of CALayer's interface and unused in our implementation. Also eliminates unnecessary ivars where they're being synthesized by `@property` declarations. Previously, we were overriding the behaviour of UIViewController.prefersStatusBarHidden by synthesizing _flutterPrefersStatusBarHidden as a backing ivar. Since we're explicitly overriding the behaviour of a superclass, it's more idiomatic to synthesize a private property or explicitly declare an ivar then explicitly override the getter instead. Further, this adds documentation to cases where `@synthesize` directives are required, such as: * Creating a backing ivar for a readonly property. * Creating a backing ivar for a property with a custom getter/setter. * Synthesising the ivar, getter, and setter for a property declared in a protocol being implemented. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent e45787f commit a63841d

12 files changed

+36
-32
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99

1010
/// Drop-in replacement (as far as Flutter is concerned) for CAMetalLayer
1111
/// that can present with transaction from a background thread.
12+
///
13+
/// Properties and method declarations must exactly match those in the
14+
/// CAMetalLayer interface declaration.
1215
@interface FlutterMetalLayer : CALayer
1316

1417
@property(nullable, retain) id<MTLDevice> device;
15-
@property(nullable, readonly) id<MTLDevice> preferredDevice;
18+
@property(nullable, readonly)
19+
id<MTLDevice> preferredDevice API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0))
20+
API_UNAVAILABLE(watchos);
1621
@property MTLPixelFormat pixelFormat;
1722
@property BOOL framebufferOnly;
1823
@property CGSize drawableSize;
1924
@property BOOL presentsWithTransaction;
2025
@property(nullable) CGColorSpaceRef colorspace;
21-
@property BOOL wantsExtendedDynamicRangeContent;
2226

2327
- (nullable id<CAMetalDrawable>)nextDrawable;
2428

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ - (void)returnTexture:(FlutterTexture*)texture;
5757

5858
@end
5959

60-
@interface FlutterTexture : NSObject {
61-
id<MTLTexture> _texture;
62-
IOSurface* _surface;
63-
CFTimeInterval _presentedTime;
64-
}
60+
@interface FlutterTexture : NSObject
6561

6662
@property(readonly, nonatomic) id<MTLTexture> texture;
6763
@property(readonly, nonatomic) IOSurface* surface;
@@ -72,11 +68,6 @@ @interface FlutterTexture : NSObject {
7268

7369
@implementation FlutterTexture
7470

75-
@synthesize texture = _texture;
76-
@synthesize surface = _surface;
77-
@synthesize presentedTime = _presentedTime;
78-
@synthesize waitingForCompletion;
79-
8071
- (instancetype)initWithTexture:(id<MTLTexture>)texture surface:(IOSurface*)surface {
8172
if (self = [super init]) {
8273
_texture = texture;
@@ -187,13 +178,6 @@ - (void)onDisplayLink:(CADisplayLink*)link {
187178

188179
@implementation FlutterMetalLayer
189180

190-
@synthesize preferredDevice = _preferredDevice;
191-
@synthesize device = _device;
192-
@synthesize pixelFormat = _pixelFormat;
193-
@synthesize framebufferOnly = _framebufferOnly;
194-
@synthesize colorspace = _colorspace;
195-
@synthesize wantsExtendedDynamicRangeContent = _wantsExtendedDynamicRangeContent;
196-
197181
- (instancetype)init {
198182
if (self = [super init]) {
199183
_preferredDevice = MTLCreateSystemDefaultDevice();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ - (UITextRange*)lineEnclosingPosition:(UITextPosition*)position
662662

663663
@implementation FlutterTextSelectionRect
664664

665+
// Synthesize properties declared readonly in UITextSelectionRect.
665666
@synthesize rect = _rect;
666667
@synthesize writingDirection = _writingDirection;
667668
@synthesize containsStart = _containsStart;

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ @interface FlutterViewController () <FlutterBinaryMessenger, UIScrollViewDelegat
7979
@property(nonatomic, assign) BOOL isHomeIndicatorHidden;
8080
@property(nonatomic, assign) BOOL isPresentingViewControllerAnimating;
8181

82+
// Internal state backing override of UIView.prefersStatusBarHidden.
83+
@property(nonatomic, assign) BOOL flutterPrefersStatusBarHidden;
84+
8285
@property(nonatomic, strong) NSMutableSet<NSNumber*>* ongoingTouches;
8386
// This scroll view is a workaround to accommodate iOS 13 and higher. There isn't a way to get
8487
// touches on the status bar to trigger scrolling to the top of a scroll view. We place a
@@ -157,9 +160,13 @@ @implementation FlutterViewController {
157160
MouseState _mouseState;
158161
}
159162

163+
// Synthesize properties with an overridden getter/setter.
160164
@synthesize viewOpaque = _viewOpaque;
161165
@synthesize displayingFlutterUI = _displayingFlutterUI;
162-
@synthesize prefersStatusBarHidden = _flutterPrefersStatusBarHidden;
166+
167+
// TODO(dkwingsmt): https://github.com/flutter/flutter/issues/138168
168+
// No backing ivar is currently required; when multiple views are supported, we'll need to
169+
// synthesize the ivar and store the view identifier.
163170
@dynamic viewIdentifier;
164171

165172
#pragma mark - Manage and override all designated initializers
@@ -2302,14 +2309,14 @@ - (void)onPreferredStatusBarStyleUpdated:(NSNotification*)notification {
23022309
}
23032310

23042311
- (void)setPrefersStatusBarHidden:(BOOL)hidden {
2305-
if (hidden != _flutterPrefersStatusBarHidden) {
2306-
_flutterPrefersStatusBarHidden = hidden;
2312+
if (hidden != self.flutterPrefersStatusBarHidden) {
2313+
self.flutterPrefersStatusBarHidden = hidden;
23072314
[self setNeedsStatusBarAppearanceUpdate];
23082315
}
23092316
}
23102317

23112318
- (BOOL)prefersStatusBarHidden {
2312-
return _flutterPrefersStatusBarHidden;
2319+
return self.flutterPrefersStatusBarHidden;
23132320
}
23142321

23152322
#pragma mark - Platform views

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,26 @@ - (void)sendKeyEvent:(const FlutterKeyEvent&)event
3838
/// Sometimes we have to use a custom mock to avoid retain cycles in OCMock.
3939
/// Used for testing low memory notification.
4040
@interface FlutterEnginePartialMock : FlutterEngine
41+
4142
@property(nonatomic, strong) FlutterBasicMessageChannel* lifecycleChannel;
4243
@property(nonatomic, strong) FlutterBasicMessageChannel* keyEventChannel;
4344
@property(nonatomic, weak) FlutterViewController* viewController;
4445
@property(nonatomic, strong) FlutterTextInputPlugin* textInputPlugin;
4546
@property(nonatomic, assign) BOOL didCallNotifyLowMemory;
47+
4648
- (FlutterTextInputPlugin*)textInputPlugin;
49+
4750
- (void)sendKeyEvent:(const FlutterKeyEvent&)event
4851
callback:(nullable FlutterKeyEventCallback)callback
4952
userData:(nullable void*)userData;
5053
@end
5154

5255
@implementation FlutterEnginePartialMock
53-
@synthesize viewController;
56+
57+
// Synthesize properties declared readonly in FlutterEngine.
5458
@synthesize lifecycleChannel;
5559
@synthesize keyEventChannel;
60+
@synthesize viewController;
5661
@synthesize textInputPlugin;
5762

5863
- (void)notifyLowMemory {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ @interface FlutterInactiveTextInput : UIView <UITextInput>
2222

2323
@implementation FlutterInactiveTextInput
2424

25+
// Synthesize properties declared in UITextInput protocol.
2526
@synthesize beginningOfDocument = _beginningOfDocument;
2627
@synthesize endOfDocument = _endOfDocument;
2728
@synthesize inputDelegate = _inputDelegate;

shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ @interface FlutterChannelKeyResponder ()
2929

3030
@implementation FlutterChannelKeyResponder
3131

32+
// Synthesize properties declared in FlutterKeyPrimaryResponder protocol.
3233
@synthesize layoutMap;
3334

3435
- (nonnull instancetype)initWithChannel:(nonnull FlutterBasicMessageChannel*)channel {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ - (void)handleResponse:(BOOL)handled forId:(uint64_t)responseId;
473473

474474
@implementation FlutterEmbedderKeyResponder
475475

476+
// Synthesize properties declared in FlutterKeyPrimaryResponder protocol.
476477
@synthesize layoutMap;
477478

478479
- (nonnull instancetype)initWithSendEvent:(FlutterSendEmbedderKeyEvent)sendEvent {

shell/platform/darwin/macos/framework/Source/FlutterMenuPluginTest.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ @interface FakePluginRegistrar : NSObject <FlutterPluginRegistrar>
2525
@end
2626

2727
@implementation FakePluginRegistrar
28+
29+
// Synthesize properties declared in FlutterPluginRegistrar protocol.
2830
@synthesize messenger;
2931
@synthesize textures;
3032
@synthesize view;

shell/platform/darwin/macos/framework/Source/FlutterThreadSynchronizerTest.mm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ @implementation FlutterThreadSynchronizerTestScaffold {
2828
FlutterThreadSynchronizer* _synchronizer;
2929
}
3030

31-
@synthesize synchronizer = _synchronizer;
32-
3331
- (nullable instancetype)init {
3432
self = [super init];
3533
if (self != nil) {

shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiterTest.mm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,27 @@
77

88
#import "flutter/testing/testing.h"
99

10-
@interface TestDisplayLink : FlutterDisplayLink {
11-
}
10+
@interface TestDisplayLink : FlutterDisplayLink
1211

1312
@property(nonatomic) CFTimeInterval nominalOutputRefreshPeriod;
1413

1514
@end
1615

1716
@implementation TestDisplayLink
1817

18+
// Synthesize properties declared readonly in FlutterDisplayLink.
1919
@synthesize nominalOutputRefreshPeriod = _nominalOutputRefreshPeriod;
20-
@synthesize delegate = _delegate;
21-
@synthesize paused = _paused;
2220

2321
- (instancetype)init {
2422
if (self = [super init]) {
25-
_paused = YES;
23+
self.paused = YES;
2624
}
2725
return self;
2826
}
2927

3028
- (void)tickWithTimestamp:(CFTimeInterval)timestamp
3129
targetTimestamp:(CFTimeInterval)targetTimestamp {
32-
[_delegate onDisplayLink:timestamp targetTimestamp:targetTimestamp];
30+
[self.delegate onDisplayLink:timestamp targetTimestamp:targetTimestamp];
3331
}
3432

3533
- (void)invalidate {

shell/platform/darwin/macos/framework/Source/FlutterViewController.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ @implementation FlutterViewController {
341341
FlutterThreadSynchronizer* _threadSynchronizer;
342342
}
343343

344+
// Synthesize properties declared readonly.
344345
@synthesize viewIdentifier = _viewIdentifier;
346+
345347
@dynamic accessibilityBridge;
346348

347349
/**

0 commit comments

Comments
 (0)