This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add an iOS PlatformViewsController for creating/disposing UIViews. #6569
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96388d6
Add an iOS PlatformViewsController for creating/disposing UIViews.
amirh c8535bb
make PlatformViewsController a C++ class
amirh e9551d7
review comments followup and expose viewId to the plugin
amirh f62c939
review comments followup
amirh 01bce7b
review comments followup
amirh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2018 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_FLUTTERPLATFORMVIEWS_H_ | ||
| #define FLUTTER_FLUTTERPLATFORMVIEWS_H_ | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| #import "FlutterCodecs.h" | ||
| #import "FlutterMacros.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| FLUTTER_EXPORT | ||
| @protocol FlutterPlatformViewFactory <NSObject> | ||
| /** | ||
| * Create a `FlutterPlatformView`. | ||
| * | ||
| * Implemented by iOS code that expose a `UIView` for embedding in a Flutter app. | ||
| * | ||
| * The implementation of this method should create a new `UIView` and return it. | ||
| * | ||
| * @param frame The rectangle for the newly created `UIView` measued in points. | ||
| * @param viewIdentifier A unique identifier for this `UIView`. | ||
| * @param arguments Parameters for creating the `UIView` sent from the Dart side of the Flutter app. | ||
| * If `createArgsCodec` is not implemented, or if no creation arguments were sent from the Dart | ||
| * code, this will be null. Otherwise this will be the value sent from the Dart code as decoded by | ||
| * `createArgsCodec`. | ||
| */ | ||
| - (UIView*)createWithFrame:(CGRect)frame | ||
| viewIdentifier:(int64_t)viewId | ||
| arguments:(id _Nullable)args; | ||
|
|
||
| /** | ||
| * Returns the `FlutterMessageCodec` for decoding the args parameter of `createWithFrame`. | ||
| * | ||
| * Only needs to be implemented if `createWithFrame` needs an arguments parameter. | ||
| */ | ||
| @optional | ||
| - (NSObject<FlutterMessageCodec>*)createArgsCodec; | ||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
|
|
||
| #endif // FLUTTER_FLUTTERPLATFORMVIEWS_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2018 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "FlutterPlatformViews_Internal.h" | ||
| #include "flutter/fml/platform/darwin/scoped_nsobject.h" | ||
| #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterChannels.h" | ||
|
|
||
| namespace shell { | ||
|
|
||
| FlutterPlatformViewsController::FlutterPlatformViewsController( | ||
| NSObject<FlutterBinaryMessenger>* messenger) { | ||
| channel_.reset([[FlutterMethodChannel alloc] | ||
| initWithName:@"flutter/platform_views" | ||
| binaryMessenger:messenger | ||
| codec:[FlutterStandardMethodCodec sharedInstance]]); | ||
| [channel_.get() setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { | ||
| OnMethodCall(call, result); | ||
| }]; | ||
| } | ||
|
|
||
| void FlutterPlatformViewsController::OnMethodCall(FlutterMethodCall* call, FlutterResult& result) { | ||
| if ([[call method] isEqualToString:@"create"]) { | ||
| OnCreate(call, result); | ||
| } else if ([[call method] isEqualToString:@"dispose"]) { | ||
| OnDispose(call, result); | ||
| } else { | ||
| result(FlutterMethodNotImplemented); | ||
| } | ||
| } | ||
|
|
||
| void FlutterPlatformViewsController::OnCreate(FlutterMethodCall* call, FlutterResult& result) { | ||
| NSDictionary<NSString*, id>* args = [call arguments]; | ||
|
|
||
| long viewId = [args[@"id"] longValue]; | ||
| std::string viewType([args[@"viewType"] UTF8String]); | ||
|
|
||
| if (views_[viewId] != nil) { | ||
| result([FlutterError errorWithCode:@"recreating_view" | ||
| message:@"trying to create an already created view" | ||
| details:[NSString stringWithFormat:@"view id: '%ld'", viewId]]); | ||
| } | ||
|
|
||
| NSObject<FlutterPlatformViewFactory>* factory = factories_[viewType].get(); | ||
| if (factory == nil) { | ||
| result([FlutterError errorWithCode:@"unregistered_view_type" | ||
| message:@"trying to create a view with an unregistered type" | ||
| details:[NSString stringWithFormat:@"unregistered view type: '%@'", | ||
| args[@"viewType"]]]); | ||
| return; | ||
| } | ||
|
|
||
| // TODO(amirh): decode and pass the creation args. | ||
| views_[viewId] = fml::scoped_nsobject<UIView>([[factory createWithFrame:CGRectZero | ||
| viewIdentifier:viewId | ||
| arguments:nil] retain]); | ||
| result(nil); | ||
| } | ||
|
|
||
| void FlutterPlatformViewsController::OnDispose(FlutterMethodCall* call, FlutterResult& result) { | ||
| NSDictionary<NSString*, id>* args = [call arguments]; | ||
| int64_t viewId = [args[@"id"] longLongValue]; | ||
|
|
||
| if (views_[viewId] == nil) { | ||
| result([FlutterError errorWithCode:@"unknown_view" | ||
| message:@"trying to dispose an unknown" | ||
| details:[NSString stringWithFormat:@"view id: '%lld'", viewId]]); | ||
| return; | ||
| } | ||
|
|
||
| views_.erase(viewId); | ||
| result(nil); | ||
| } | ||
|
|
||
| void FlutterPlatformViewsController::RegisterViewFactory( | ||
| NSObject<FlutterPlatformViewFactory>* factory, | ||
| NSString* factoryId) { | ||
| std::string idString([factoryId UTF8String]); | ||
| FML_CHECK(factories_.count(idString) == 0); | ||
| factories_[idString] = | ||
| fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>([factory retain]); | ||
| } | ||
|
|
||
| } // namespace shell |
34 changes: 34 additions & 0 deletions
34
shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2018 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ | ||
|
|
||
| #include "flutter/fml/platform/darwin/scoped_nsobject.h" | ||
| #include "flutter/shell/common/shell.h" | ||
| #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterBinaryMessenger.h" | ||
| #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterChannels.h" | ||
| #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h" | ||
|
|
||
| namespace shell { | ||
|
|
||
| class FlutterPlatformViewsController { | ||
| public: | ||
| FlutterPlatformViewsController(NSObject<FlutterBinaryMessenger>* messenger); | ||
|
|
||
| void RegisterViewFactory(NSObject<FlutterPlatformViewFactory>* factory, NSString* factoryId); | ||
|
|
||
| private: | ||
| fml::scoped_nsobject<FlutterMethodChannel> channel_; | ||
| std::map<std::string, fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>> factories_; | ||
| std::map<int64_t, fml::scoped_nsobject<UIView>> views_; | ||
|
|
||
| void OnMethodCall(FlutterMethodCall* call, FlutterResult& result); | ||
| void OnCreate(FlutterMethodCall* call, FlutterResult& result); | ||
| void OnDispose(FlutterMethodCall* call, FlutterResult& result); | ||
| }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disallow copy and assign here. See the section about this in the style guide https://google.github.io/styleguide/cppguide.html#Copyable_Movable_Types.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| } // namespace shell | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.