From df64376478e586f8afaf1f4162a1190338e713ab Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Thu, 8 Nov 2018 11:21:55 -0800 Subject: [PATCH] Make the FlutterPlatformViewFactory create FlutterPlatformViews. (flutter/engine#6782) Handing a UIView refererence directly to the engine makes it challenging for plugin authors to retain a controller for that UIView (e.g the controller that talks over the platform channel) for as long as the embedded view is needed. We instead make the factory return a FlutterPlatformView which is a wrapper around the UIView that the engine retains as long as the platform view instance is needed. This allows plugin authors to keep their control logic in the FlutterPlatformView and know that the engine is responsible for retaining the reference. --- engine/src/flutter/shell/common/rasterizer.cc | 7 ++--- .../framework/Headers/FlutterPlatformViews.h | 16 ++++++++-- .../framework/Source/FlutterPlatformViews.mm | 29 +++++++++++-------- .../Source/FlutterPlatformViews_Internal.h | 3 +- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index 9684b4dcd9..b5be5e49e5 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -164,10 +164,9 @@ bool Rasterizer::DrawToSurface(flow::LayerTree& layer_tree) { auto external_view_embedder = surface_->GetExternalViewEmbedder(); - // TODO(amirh): uncomment this once external_view_embedder is populated. - // if (external_view_embedder != nullptr) { - // external_view_embedder->SetFrameSize(layer_tree.frame_size()); - // } + if (external_view_embedder != nullptr) { + external_view_embedder->SetFrameSize(layer_tree.frame_size()); + } auto compositor_frame = compositor_context_->AcquireFrame( surface_->GetContext(), canvas, external_view_embedder, diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h index a8145fa628..72086dec13 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h @@ -12,6 +12,16 @@ NS_ASSUME_NONNULL_BEGIN +/** + * Wraps a `UIView` for embedding in the Flutter hierarchy + */ +@protocol FlutterPlatformView +/** + * Returns a reference to the `UIView` that is wrapped by this `FlutterPlatformView`. + */ +- (UIView*)view; +@end + FLUTTER_EXPORT @protocol FlutterPlatformViewFactory /** @@ -28,9 +38,9 @@ FLUTTER_EXPORT * 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; +- (NSObject*)createWithFrame:(CGRect)frame + viewIdentifier:(int64_t)viewId + arguments:(id _Nullable)args; /** * Returns the `FlutterMessageCodec` for decoding the args parameter of `createWithFrame`. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index 20f089d6d9..f0b800737a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -72,13 +72,17 @@ void FlutterPlatformViewsController::OnCreate(FlutterMethodCall* call, FlutterRe } } - UIView* embedded_view = [factory createWithFrame:CGRectZero - viewIdentifier:viewId - arguments:params]; - FlutterTouchInterceptingView* view = - [[[FlutterTouchInterceptingView alloc] initWithEmbeddedView:embedded_view + NSObject* embedded_view = [factory createWithFrame:CGRectZero + viewIdentifier:viewId + arguments:params]; + views_[viewId] = fml::scoped_nsobject>([embedded_view retain]); + + FlutterTouchInterceptingView* touch_interceptor = + [[[FlutterTouchInterceptingView alloc] initWithEmbeddedView:embedded_view.view flutterView:flutter_view_] autorelease]; - views_[viewId] = fml::scoped_nsobject([view retain]); + + touch_interceptors_[viewId] = + fml::scoped_nsobject([touch_interceptor retain]); result(nil); } @@ -94,9 +98,10 @@ void FlutterPlatformViewsController::OnDispose(FlutterMethodCall* call, FlutterR return; } - UIView* view = views_[viewId].get(); - [view removeFromSuperview]; + UIView* touch_interceptor = touch_interceptors_[viewId].get(); + [touch_interceptor removeFromSuperview]; views_.erase(viewId); + touch_interceptors_.erase(viewId); overlays_.erase(viewId); result(nil); } @@ -113,7 +118,7 @@ void FlutterPlatformViewsController::OnAcceptGesture(FlutterMethodCall* call, return; } - FlutterTouchInterceptingView* view = views_[viewId].get(); + FlutterTouchInterceptingView* view = touch_interceptors_[viewId].get(); [view releaseGesture]; result(nil); @@ -159,8 +164,8 @@ SkCanvas* FlutterPlatformViewsController::CompositeEmbeddedView( CGRectMake(params.offsetPixels.x() / screenScale, params.offsetPixels.y() / screenScale, params.sizePoints.width(), params.sizePoints.height()); - UIView* view = views_[view_id].get(); - [view setFrame:rect]; + UIView* touch_interceptor = touch_interceptors_[view_id].get(); + [touch_interceptor setFrame:rect]; SkCanvas* canvas = composition_frames_[view_id]->SkiaCanvas(); canvas->clear(SK_ColorTRANSPARENT); @@ -204,7 +209,7 @@ bool FlutterPlatformViewsController::Present() { active_composition_order_.clear(); for (size_t i = 0; i < composition_order_.size(); i++) { int view_id = composition_order_[i]; - [flutter_view addSubview:views_[view_id].get()]; + [flutter_view addSubview:touch_interceptors_[view_id].get()]; [flutter_view addSubview:overlays_[view_id]->overlay_view.get()]; active_composition_order_.push_back(view_id); } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h index e3cfa4141d..2e40d9c861 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h @@ -70,7 +70,8 @@ class FlutterPlatformViewsController { fml::scoped_nsobject channel_; fml::scoped_nsobject flutter_view_; std::map>> factories_; - std::map> views_; + std::map>> views_; + std::map> touch_interceptors_; std::map> overlays_; SkISize frame_size_;