Skip to content

Commit

Permalink
Attach and position embedded UIVIews (flutter#6614)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirh authored Oct 26, 2018
1 parent df85722 commit f797004
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +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:
SkPoint offsetPixels;
SkSize sizePoints;
};

// This is only used on iOS when running in a non headless mode,
Expand Down
5 changes: 5 additions & 0 deletions flow/layers/platform_view_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void PlatformViewLayer::Paint(PaintContext& context) const {
return;
}
EmbeddedViewParams params;
SkMatrix transform = context.canvas.getTotalMatrix();
params.offsetPixels =
SkPoint::Make(transform.getTranslateX(), transform.getTranslateY());
params.sizePoints = size_;

context.view_embedder->CompositeEmbeddedView(view_id_, params);
}
} // namespace flow
20 changes: 18 additions & 2 deletions shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace shell {

FlutterPlatformViewsController::FlutterPlatformViewsController(
NSObject<FlutterBinaryMessenger>* messenger) {
NSObject<FlutterBinaryMessenger>* messenger,
FlutterView* flutter_view)
: flutter_view_([flutter_view retain]) {
channel_.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/platform_views"
binaryMessenger:messenger
Expand Down Expand Up @@ -58,6 +60,9 @@
views_[viewId] = fml::scoped_nsobject<UIView>([[factory createWithFrame:CGRectZero
viewIdentifier:viewId
arguments:nil] retain]);

FlutterView* flutter_view = flutter_view_.get();
[flutter_view addSubview:views_[viewId].get()];
result(nil);
}

Expand All @@ -72,6 +77,8 @@
return;
}

UIView* view = views_[viewId].get();
[view removeFromSuperview];
views_.erase(viewId);
result(nil);
}
Expand All @@ -87,7 +94,16 @@

void FlutterPlatformViewsController::CompositeEmbeddedView(int view_id,
const flow::EmbeddedViewParams& params) {
// TODO(amirh): implement this.
// 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,
params.sizePoints.width(), params.sizePoints.height());

UIView* view = views_[view_id];
[view setFrame:rect];
}

} // namespace shell
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,14 +17,16 @@ namespace shell {

class FlutterPlatformViewsController : public flow::ExternalViewEmbedder {
public:
FlutterPlatformViewsController(NSObject<FlutterBinaryMessenger>* messenger);
FlutterPlatformViewsController(NSObject<FlutterBinaryMessenger>* messenger,
FlutterView* flutter_view);

void RegisterViewFactory(NSObject<FlutterPlatformViewFactory>* factory, NSString* factoryId);

void CompositeEmbeddedView(int view_id, const flow::EmbeddedViewParams& params);

private:
fml::scoped_nsobject<FlutterMethodChannel> channel_;
fml::scoped_nsobject<FlutterView> flutter_view_;
std::map<std::string, fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>> factories_;
std::map<int64_t, fml::scoped_nsobject<UIView>> views_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ - (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<FlutterEngine>)engine {
Expand Down

0 comments on commit f797004

Please sign in to comment.