From 7f0de2e390dacd47de8d6984c9d3f65ebe334034 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Fri, 19 Oct 2018 10:51:59 -0700 Subject: [PATCH 1/5] Attach and position embedded UIVIews. --- flow/embedded_views.h | 4 ++++ flow/layers/platform_view_layer.cc | 6 ++++++ .../framework/Source/FlutterPlatformViews.mm | 17 +++++++++++++++-- .../Source/FlutterPlatformViews_Internal.h | 5 ++++- .../framework/Source/FlutterViewController.mm | 2 +- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/flow/embedded_views.h b/flow/embedded_views.h index 577922f42133f..ec259e3d39a0d 100644 --- a/flow/embedded_views.h +++ b/flow/embedded_views.h @@ -11,6 +11,10 @@ namespace flow { class EmbeddedViewParams { public: + double translateXPixels; + double translateYPixels; + double widthPoints; + double heightPoints; }; // This is only used on iOS when running in a non headless mode, diff --git a/flow/layers/platform_view_layer.cc b/flow/layers/platform_view_layer.cc index 42fdd91954b62..09b277c67fcc1 100644 --- a/flow/layers/platform_view_layer.cc +++ b/flow/layers/platform_view_layer.cc @@ -23,6 +23,12 @@ void PlatformViewLayer::Paint(PaintContext& context) const { return; } EmbeddedViewParams params; + SkMatrix transform = context.canvas.getTotalMatrix(); + params.translateXPixels = transform.getTranslateX(); + params.translateYPixels = transform.getTranslateY(); + params.widthPoints = size_.width(); + params.heightPoints = size_.height(); + context.view_embedder->CompositeEmbeddedView(view_id_, params); } } // namespace flow diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index 9b63070c6f9d9..1481ebdc75b4f 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -13,7 +13,9 @@ namespace shell { FlutterPlatformViewsController::FlutterPlatformViewsController( - NSObject* messenger) { + NSObject* messenger, + FlutterView* flutter_view) + : flutter_view_([flutter_view retain]) { channel_.reset([[FlutterMethodChannel alloc] initWithName:@"flutter/platform_views" binaryMessenger:messenger @@ -58,6 +60,9 @@ views_[viewId] = fml::scoped_nsobject([[factory createWithFrame:CGRectZero viewIdentifier:viewId arguments:nil] retain]); + + FlutterView* flutter_view = flutter_view_.get(); + [flutter_view addSubview:views_[viewId].get()]; result(nil); } @@ -72,6 +77,8 @@ return; } + UIView* view = views_[viewId].get(); + [view removeFromSuperview]; views_.erase(viewId); result(nil); } @@ -87,7 +94,13 @@ void FlutterPlatformViewsController::CompositeEmbeddedView(int view_id, const flow::EmbeddedViewParams& params) { - // TODO(amirh): implement this. + CGFloat screenScale = [[UIScreen mainScreen] scale]; + CGRect rect = + CGRectMake(params.translateXPixels / screenScale, params.translateYPixels / screenScale, + params.widthPoints, params.heightPoints); + + UIView* view = views_[view_id]; + [view setFrame:rect]; } } // namespace shell diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h index 9c9d80435606d..494d1fe5f1c16 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h @@ -5,6 +5,7 @@ #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ +#include "FlutterView.h" #include "flutter/flow/embedded_views.h" #include "flutter/fml/platform/darwin/scoped_nsobject.h" #include "flutter/shell/common/shell.h" @@ -16,7 +17,8 @@ namespace shell { class FlutterPlatformViewsController : public flow::ExternalViewEmbedder { public: - FlutterPlatformViewsController(NSObject* messenger); + FlutterPlatformViewsController(NSObject* messenger, + FlutterView* flutter_view); void RegisterViewFactory(NSObject* factory, NSString* factoryId); @@ -24,6 +26,7 @@ class FlutterPlatformViewsController : public flow::ExternalViewEmbedder { private: fml::scoped_nsobject channel_; + fml::scoped_nsobject flutter_view_; std::map>> factories_; std::map> views_; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 3636899fa4700..b70c903c93eb6 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -113,7 +113,7 @@ - (void)performCommonViewControllerInitialization { _statusBarStyle = UIStatusBarStyleDefault; [self setupNotificationCenterObservers]; - _platformViewsController.reset(new shell::FlutterPlatformViewsController(_engine.get())); + _platformViewsController.reset(new shell::FlutterPlatformViewsController(_engine.get(), _flutterView.get())); } - (fml::scoped_nsobject)engine { From 1baa2e23264d53490b7306cc4c66ba3bfbb96a27 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Wed, 24 Oct 2018 16:59:30 -0700 Subject: [PATCH 2/5] use SkPoint and SkSize in the embedded view params --- flow/embedded_views.h | 8 ++++---- flow/layers/platform_view_layer.cc | 6 ++---- .../darwin/ios/framework/Source/FlutterPlatformViews.mm | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/flow/embedded_views.h b/flow/embedded_views.h index ec259e3d39a0d..07a32b27197a6 100644 --- a/flow/embedded_views.h +++ b/flow/embedded_views.h @@ -6,15 +6,15 @@ #define FLUTTER_FLOW_EMBEDDED_VIEWS_H_ #include "flutter/fml/memory/ref_counted.h" +#include "third_party/skia/include/core/SkPoint.h" +#include "third_party/skia/include/core/SkSize.h" namespace flow { class EmbeddedViewParams { public: - double translateXPixels; - double translateYPixels; - double widthPoints; - double heightPoints; + SkPoint translatePixels; + SkSize sizePoints; }; // This is only used on iOS when running in a non headless mode, diff --git a/flow/layers/platform_view_layer.cc b/flow/layers/platform_view_layer.cc index 09b277c67fcc1..e6fec7e362b01 100644 --- a/flow/layers/platform_view_layer.cc +++ b/flow/layers/platform_view_layer.cc @@ -24,10 +24,8 @@ void PlatformViewLayer::Paint(PaintContext& context) const { } EmbeddedViewParams params; SkMatrix transform = context.canvas.getTotalMatrix(); - params.translateXPixels = transform.getTranslateX(); - params.translateYPixels = transform.getTranslateY(); - params.widthPoints = size_.width(); - params.heightPoints = size_.height(); + params.translatePixels = SkPoint::Make(transform.getTranslateX(), transform.getTranslateY()); + params.sizePoints = size_; context.view_embedder->CompositeEmbeddedView(view_id_, params); } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index 1481ebdc75b4f..f3857421a2557 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -96,8 +96,8 @@ const flow::EmbeddedViewParams& params) { CGFloat screenScale = [[UIScreen mainScreen] scale]; CGRect rect = - CGRectMake(params.translateXPixels / screenScale, params.translateYPixels / screenScale, - params.widthPoints, params.heightPoints); + CGRectMake(params.translatePixels.x() / screenScale, params.translatePixels.y() / screenScale, + params.sizePoints.width(), params.sizePoints.height()); UIView* view = views_[view_id]; [view setFrame:rect]; From 75d6ad450747f527ad26ae960b869d348cd15642 Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Wed, 24 Oct 2018 17:09:27 -0700 Subject: [PATCH 3/5] review comments followup --- flow/embedded_views.h | 2 +- flow/layers/platform_view_layer.cc | 2 +- .../darwin/ios/framework/Source/FlutterPlatformViews.mm | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/flow/embedded_views.h b/flow/embedded_views.h index 07a32b27197a6..10740f6d0e23d 100644 --- a/flow/embedded_views.h +++ b/flow/embedded_views.h @@ -13,7 +13,7 @@ namespace flow { class EmbeddedViewParams { public: - SkPoint translatePixels; + SkPoint offsetPixels; SkSize sizePoints; }; diff --git a/flow/layers/platform_view_layer.cc b/flow/layers/platform_view_layer.cc index e6fec7e362b01..a2de53199696f 100644 --- a/flow/layers/platform_view_layer.cc +++ b/flow/layers/platform_view_layer.cc @@ -24,7 +24,7 @@ void PlatformViewLayer::Paint(PaintContext& context) const { } EmbeddedViewParams params; SkMatrix transform = context.canvas.getTotalMatrix(); - params.translatePixels = SkPoint::Make(transform.getTranslateX(), transform.getTranslateY()); + params.offsetPixels = SkPoint::Make(transform.getTranslateX(), transform.getTranslateY()); params.sizePoints = size_; context.view_embedder->CompositeEmbeddedView(view_id_, params); diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index f3857421a2557..2eb5f8b95ba80 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -94,9 +94,11 @@ void FlutterPlatformViewsController::CompositeEmbeddedView(int view_id, const flow::EmbeddedViewParams& params) { + // TODO(amirh): assert that this is running on the platform thread once we support the iOS + // embedded views thread configuration. CGFloat screenScale = [[UIScreen mainScreen] scale]; CGRect rect = - CGRectMake(params.translatePixels.x() / screenScale, params.translatePixels.y() / screenScale, + CGRectMake(params.offsetPixels.x() / screenScale, params.offsetPixels.y() / screenScale, params.sizePoints.width(), params.sizePoints.height()); UIView* view = views_[view_id]; From 9291956921274ead3c26c19d8e298bd173bb951a Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Fri, 26 Oct 2018 14:35:13 -0700 Subject: [PATCH 4/5] review comment followup --- .../platform/darwin/ios/framework/Source/FlutterPlatformViews.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index 2eb5f8b95ba80..48e175ef04e80 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -96,6 +96,7 @@ const flow::EmbeddedViewParams& params) { // TODO(amirh): assert that this is running on the platform thread once we support the iOS // embedded views thread configuration. + // TODO(amirh): do nothing if the params didn't change. CGFloat screenScale = [[UIScreen mainScreen] scale]; CGRect rect = CGRectMake(params.offsetPixels.x() / screenScale, params.offsetPixels.y() / screenScale, From e46e69374c23845c938c408f6b1eaac89ac438fb Mon Sep 17 00:00:00 2001 From: Amir Hardon Date: Fri, 26 Oct 2018 14:38:30 -0700 Subject: [PATCH 5/5] format --- flow/layers/platform_view_layer.cc | 3 ++- .../darwin/ios/framework/Source/FlutterViewController.mm | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/flow/layers/platform_view_layer.cc b/flow/layers/platform_view_layer.cc index a2de53199696f..19057fbfd4b9c 100644 --- a/flow/layers/platform_view_layer.cc +++ b/flow/layers/platform_view_layer.cc @@ -24,7 +24,8 @@ void PlatformViewLayer::Paint(PaintContext& context) const { } EmbeddedViewParams params; SkMatrix transform = context.canvas.getTotalMatrix(); - params.offsetPixels = SkPoint::Make(transform.getTranslateX(), transform.getTranslateY()); + params.offsetPixels = + SkPoint::Make(transform.getTranslateX(), transform.getTranslateY()); params.sizePoints = size_; context.view_embedder->CompositeEmbeddedView(view_id_, params); diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index b70c903c93eb6..31c91871f88f0 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -113,7 +113,8 @@ - (void)performCommonViewControllerInitialization { _statusBarStyle = UIStatusBarStyleDefault; [self setupNotificationCenterObservers]; - _platformViewsController.reset(new shell::FlutterPlatformViewsController(_engine.get(), _flutterView.get())); + _platformViewsController.reset( + new shell::FlutterPlatformViewsController(_engine.get(), _flutterView.get())); } - (fml::scoped_nsobject)engine {