Skip to content

Commit 251f4a8

Browse files
Add a viewController property to the ios/macOS FlutterPluginRegistrar protocol (flutter#174168)
flutter#104117 Adds a `viewController` (per flutter#104117 (comment), presumably because iOS plugins want to presenting new view controllers) getter that makes the single-view assumption. ~This is a method instead of a weak + readonly property like the macOS registrar protocol because existing getters are also methods, e.g.,~ ```objc - (NSObject<FlutterBinaryMessenger>*)messenger; - (NSObject<FlutterTextureRegistry>*)textures; ``` ~Unfortunately swift callers will have to use a preprocess macro and add different code paths for macOS and iOS.~ #### Single view assumption I've considered adding a `- (nullable UIViewController*)viewControllerForViewID: ViewIDType;` method but that will force plugins to make breaking changes in order to migrate off of the deprecated `keyWindow` property. Take the text input plugin as an example, it has to add a `viewID` argument to the `setTextInputClient` call when establishing a new text input connection, so the text input plugin knows which view hierarchy to add the text input view to. It is unclear to me whether more `FlutterPluginRegistrar` changes are needed to completely get rid of the single-view assumption in the protocol (the views `FlutterPlatformViewFactory` creates are always added as subviews to `flutterView` so I guess that interface may have to change in the future? According to according to [Flutter’s multi-window status](https://docs.google.com/document/d/13E27tD8_9f6lDgwg3MpGNTV8XIRCZH3ByI-t9kI9IUM/edit?tab=t.0#heading=h.8lxwn1tiky70), only a `viewForIdentifier` method will be added to registrar), so IMO it would be a better experience to not introduce the `viewID` concept right now and force plugins to make break changes, since they may have to make more breaking changes in the future. Alternatively, we could also ignore the `viewID` args for now and always return the view controller of the implicit view in the implementation, so that plugin authors can use made-up view IDs to get the implicit view controller. But that's going to make future multiview migrations difficult since the compiler won't be able to catch unmigrated code if the plugin is using a bogus view ID. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent fb4c6ab commit 251f4a8

8 files changed

Lines changed: 67 additions & 1 deletion

File tree

engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ FLUTTER_DARWIN_EXPORT
3434
* @param frame The rectangle for the newly created `UIView` measured in points.
3535
* @param viewId A unique identifier for this `UIView`.
3636
* @param args Parameters for creating the `UIView` sent from the Dart side of the Flutter app.
37-
* If `createArgsCodec` is not implemented, or if no creation arguments were sent from the Dart
37+
* If `createArgsCodec` is not implemented, or if no creation arguments were sent from the Dart
3838
* code, this will be null. Otherwise this will be the value sent from the Dart code as decoded by
3939
* `createArgsCodec`.
4040
*/

engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,20 @@ typedef enum {
297297
*/
298298
- (NSObject<FlutterTextureRegistry>*)textures;
299299

300+
/**
301+
* The `UIViewController` whose view is displaying Flutter content.
302+
*
303+
* The plugin typically should not store a strong reference to this view
304+
* controller.
305+
*
306+
* This property is provided for backwards compatibility for apps that assume
307+
* a single view, and will eventually be replaced by the multi-view API variant.
308+
*
309+
* This property may be |nil|, for instance in a headless environment, or when
310+
* the underlying Flutter engine is deallocated.
311+
*/
312+
@property(nullable, readonly) UIViewController* viewController;
313+
300314
/**
301315
* Registers a `FlutterPlatformViewFactory` for creation of platform views.
302316
*

engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <UIKit/UIKit.h>
56
#include "common/settings.h"
67
#define FML_USED_ON_EMBEDDER
78

@@ -1521,6 +1522,10 @@ - (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine
15211522
return _flutterEngine.textureRegistry;
15221523
}
15231524

1525+
- (nullable UIViewController*)viewController {
1526+
return _flutterEngine.viewController;
1527+
}
1528+
15241529
- (void)publish:(NSObject*)value {
15251530
_flutterEngine.pluginPublications[_pluginKey] = value;
15261531
}

engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ - (void)testNotifyPluginOfDealloc {
189189
OCMVerify([plugin detachFromEngineForRegistrar:[OCMArg any]]);
190190
}
191191

192+
- (void)testGetViewControllerFromRegistrar {
193+
FlutterDartProject* project = [[FlutterDartProject alloc] init];
194+
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"engine" project:project];
195+
id mockEngine = OCMPartialMock(engine);
196+
NSObject<FlutterPluginRegistrar>* registrar = [mockEngine registrarForPlugin:@"plugin"];
197+
198+
// Verify accessing the viewController getter calls FlutterEngine.viewController.
199+
(void)[registrar viewController];
200+
OCMVerify(times(1), [mockEngine viewController]);
201+
}
202+
192203
- (void)testSetBinaryMessengerToSameBinaryMessenger {
193204
FakeBinaryMessengerRelay* fakeBinaryMessenger = [[FakeBinaryMessengerRelay alloc] init];
194205

engine/src/flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ FLUTTER_DARWIN_EXPORT
4949
*/
5050
@property(nullable, readonly) NSView* view;
5151

52+
/**
53+
* The `NSViewController` that hosts |view|.
54+
*
55+
* The plugin typically should not store a strong reference to this view
56+
* controller.
57+
*
58+
* This property is provided for backwards compatibility for apps that assume
59+
* a single view, and will eventually be replaced by the multi-view API variant.
60+
*
61+
* This property is |nil| when |view| is |nil|.
62+
*/
63+
@property(nullable, readonly) NSViewController* viewController;
64+
5265
/**
5366
* Registers |delegate| to receive handleMethodCall:result: callbacks for the given |channel|.
5467
*/

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ - (NSView*)viewForIdentifier:(FlutterViewIdentifier)viewIdentifier {
366366
return controller.flutterView;
367367
}
368368

369+
- (NSViewController*)viewController {
370+
return [_flutterEngine viewControllerForIdentifier:kFlutterImplicitViewId];
371+
}
372+
369373
- (void)addMethodCallDelegate:(nonnull id<FlutterPlugin>)delegate
370374
channel:(nonnull FlutterMethodChannel*)channel {
371375
[channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,21 @@ @implementation MockableFlutterEngine
744744
EXPECT_EQ([engine valuePublishedByPlugin:pluginName], secondValue);
745745
}
746746

747+
TEST_F(FlutterEngineTest, RegistrarForwardViewControllerLookUpToEngine) {
748+
NSString* fixtures = @(flutter::testing::GetFixturesPath());
749+
FlutterDartProject* project = [[FlutterDartProject alloc]
750+
initWithAssetsPath:fixtures
751+
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
752+
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:project];
753+
754+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
755+
nibName:nil
756+
bundle:nil];
757+
id<FlutterPluginRegistrar> registrar = [engine registrarForPlugin:@"MyPlugin"];
758+
759+
EXPECT_EQ([registrar viewController], viewController);
760+
}
761+
747762
// If a channel overrides a previous channel with the same name, cleaning
748763
// the previous channel should not affect the new channel.
749764
//

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPluginTest.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ - (nonnull NSString*)lookupKeyForAsset:(nonnull NSString*)asset
5858
fromPackage:(nonnull NSString*)package {
5959
return @"";
6060
}
61+
62+
- (NSViewController*)viewController {
63+
return nil;
64+
}
6165
@end
6266

6367
namespace flutter::testing {

0 commit comments

Comments
 (0)