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

Commit 09140c5

Browse files
committed
[macOS] Add platformview create parameter support
Previously, when creating native platform views on macOS, we ignored any parameters passed via the framework side "params" argument in the "create" method call, and instead always passed a nil value to the FlutterPlatformViewFactory. This made it impossible for users of macOS platform views to pass constructor arguments to the NSView subclass implementing the platform view. We now decode the arguments data using the codec specified by the `FlutterPlatformViewFactory` and pass them through to the `[FlutterPlatformViewFactory createWithIdentifier:arguments:]` method where the platform view author can make use of them. Fixes: flutter/flutter#124723 This is a part of the broader macOS platform view support effort: flutter/flutter#41722
1 parent 59d5444 commit 09140c5

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
@interface FlutterPlatformViewController ()
1818

1919
/**
20-
* Creates a platform view of viewType with viewId.
20+
* Creates a platform view of viewType with viewId and arguments passed from
21+
* the framework's creationParams constructor parameter.
2122
* FlutterResult is updated to contain nil for success or to contain
2223
* a FlutterError if there is an error.
2324
*/
2425
- (void)onCreateWithViewID:(int64_t)viewId
2526
viewType:(nonnull NSString*)viewType
27+
arguments:(id _Nullable)args
2628
result:(nonnull FlutterResult)result;
2729

2830
/**

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ - (instancetype)init {
2727

2828
- (void)onCreateWithViewID:(int64_t)viewId
2929
viewType:(nonnull NSString*)viewType
30+
arguments:(id _Nullable)args
3031
result:(nonnull FlutterResult)result {
3132
if (_platformViews.count(viewId) != 0) {
3233
result([FlutterError errorWithCode:@"recreating_view"
@@ -52,7 +53,7 @@ - (void)onCreateWithViewID:(int64_t)viewId
5253
return;
5354
}
5455

55-
NSView* platform_view = [factory createWithViewIdentifier:viewId arguments:nil];
56+
NSView* platform_view = [factory createWithViewIdentifier:viewId arguments:args];
5657
// Flutter compositing requires CALayer-backed platform views.
5758
// Force the platform view to be backed by a CALayer.
5859
[platform_view setWantsLayer:YES];
@@ -92,7 +93,17 @@ - (void)handleMethodCall:(nonnull FlutterMethodCall*)call result:(nonnull Flutte
9293
if ([args objectForKey:@"id"]) {
9394
int64_t viewId = [args[@"id"] longLongValue];
9495
NSString* viewType = [NSString stringWithUTF8String:([args[@"viewType"] UTF8String])];
95-
[self onCreateWithViewID:viewId viewType:viewType result:result];
96+
97+
id params = nil;
98+
NSObject<FlutterPlatformViewFactory>* factory = _platformViewFactories[viewType];
99+
if ([factory respondsToSelector:@selector(createArgsCodec)]) {
100+
NSObject<FlutterMessageCodec>* codec = [factory createArgsCodec];
101+
if (codec != nil && args[@"params"] != nil) {
102+
FlutterStandardTypedData* paramsData = args[@"params"];
103+
params = [codec decode:paramsData.data];
104+
}
105+
}
106+
[self onCreateWithViewID:viewId viewType:viewType arguments:params result:result];
96107
} else {
97108
result([FlutterError errorWithCode:@"unknown_view"
98109
message:@"'id' argument must be passed to create a platform view."

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@
4343

4444
[platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
4545

46+
NSDictionary* params = @{
47+
@"album" : @"スコットとリバース",
48+
@"releaseYear" : @2013,
49+
@"artists" : @[ @"Scott Murphy", @"Rivers Cuomo" ],
50+
@"playlist" : @[ @"おかしいやつ", @"ほどけていたんだ" ],
51+
};
52+
FlutterStandardMessageCodec* codec = [FlutterStandardMessageCodec sharedInstance];
53+
FlutterStandardTypedData* paramData =
54+
[FlutterStandardTypedData typedDataWithBytes:[codec encode:params]];
55+
4656
FlutterMethodCall* methodCall =
4757
[FlutterMethodCall methodCallWithMethodName:@"create"
4858
arguments:@{
4959
@"id" : @2,
50-
@"viewType" : @"MockPlatformView"
60+
@"viewType" : @"MockPlatformView",
61+
@"params" : paramData,
5162
}];
5263

5364
__block bool success = false;
@@ -58,8 +69,33 @@
5869
}
5970
};
6071
[platformViewController handleMethodCall:methodCall result:result];
61-
6272
EXPECT_TRUE(success);
73+
74+
// Verify PlatformView parameters are decoded correctly.
75+
TestFlutterPlatformView* view =
76+
(TestFlutterPlatformView*)[platformViewController platformViewWithID:2];
77+
ASSERT_TRUE(view != nil);
78+
ASSERT_TRUE(view.args != nil);
79+
80+
// Verify string type.
81+
NSString* album = [view.args objectForKey:@"album"];
82+
EXPECT_TRUE([album isEqualToString:@"スコットとリバース"]);
83+
84+
// Verify int type.
85+
NSNumber* releaseYear = [view.args objectForKey:@"releaseYear"];
86+
EXPECT_EQ(releaseYear.intValue, 2013);
87+
88+
// Verify list/array types.
89+
NSArray* artists = [view.args objectForKey:@"artists"];
90+
ASSERT_TRUE(artists != nil);
91+
ASSERT_EQ(artists.count, 2ul);
92+
EXPECT_TRUE([artists[0] isEqualToString:@"Scott Murphy"]);
93+
EXPECT_TRUE([artists[1] isEqualToString:@"Rivers Cuomo"]);
94+
95+
NSArray* playlist = [view.args objectForKey:@"playlist"];
96+
ASSERT_EQ(playlist.count, 2ul);
97+
EXPECT_TRUE([playlist[0] isEqualToString:@"おかしいやつ"]);
98+
EXPECT_TRUE([playlist[1] isEqualToString:@"ほどけていたんだ"]);
6399
}
64100

65101
TEST(FlutterPlatformViewController, TestCreateAndDispose) {

shell/platform/darwin/macos/framework/Source/TestFlutterPlatformView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
88

99
@interface TestFlutterPlatformView : NSView
10+
11+
/// Arguments passed via the params value in the create method call.
12+
@property(nonatomic, copy) id args;
13+
1014
@end
1115

1216
@interface TestFlutterPlatformViewFactory : NSObject <FlutterPlatformViewFactory>

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

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

1010
@implementation TestFlutterPlatformView
1111

12-
- (instancetype)initWithFrame:(CGRect)frame {
12+
- (instancetype)initWithFrame:(CGRect)frame arguments:(nullable NSDictionary*)args {
1313
self = [super initWithFrame:frame];
14+
_args = args;
1415
return self;
1516
}
1617

1718
@end
1819

1920
@implementation TestFlutterPlatformViewFactory
2021
- (NSView*)createWithViewIdentifier:(int64_t)viewId arguments:(nullable id)args {
21-
return [[TestFlutterPlatformView alloc] initWithFrame:CGRectZero];
22+
return [[TestFlutterPlatformView alloc] initWithFrame:CGRectZero arguments:args];
2223
}
2324

2425
- (NSObject<FlutterMessageCodec>*)createArgsCodec {

0 commit comments

Comments
 (0)