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

Commit fff8d8d

Browse files
committed
Apply review
1 parent ac86e44 commit fff8d8d

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
viewIdentifier:viewId
159159
arguments:params];
160160
UIView* platform_view = [embedded_view view];
161-
FML_DCHECK(platform_view == [embedded_view view]) << "The [view] method of FlutterPlatformView should not return a new instance of UIView during the same frame.";
162161
// Set a unique view identifier, so the platform view can be identified in unit tests.
163162
platform_view.accessibilityIdentifier = [NSString stringWithFormat:@"platform_view[%ld]", viewId];
164163
views_[viewId] = fml::scoped_nsobject<NSObject<FlutterPlatformView>>([embedded_view retain]);
@@ -312,11 +311,11 @@
312311
views_to_recomposite_.insert(view_id);
313312
}
314313

315-
NSObject<FlutterPlatformView>* FlutterPlatformViewsController::GetPlatformViewByID(int view_id) {
314+
UIView* FlutterPlatformViewsController::GetPlatformViewByID(int view_id) {
316315
if (views_.empty()) {
317316
return nil;
318317
}
319-
return views_[view_id].get();
318+
return [touch_interceptors_[view_id].get() embeddedView];
320319
}
321320

322321
std::vector<SkCanvas*> FlutterPlatformViewsController::GetCurrentCanvases() {
@@ -449,7 +448,7 @@
449448
}
450449

451450
SkRect FlutterPlatformViewsController::GetPlatformViewRect(int view_id) {
452-
UIView* platform_view = [views_[view_id].get() view];
451+
UIView* platform_view = GetPlatformViewByID(view_id);
453452
UIScreen* screen = [UIScreen mainScreen];
454453
CGRect platform_view_cgrect = [platform_view convertRect:platform_view.bounds
455454
toView:flutter_view_];
@@ -732,6 +731,7 @@ - (instancetype)initWithTarget:(id)target
732731
@implementation FlutterTouchInterceptingView {
733732
fml::scoped_nsobject<DelayingGestureRecognizer> _delayingRecognizer;
734733
FlutterPlatformViewGestureRecognizersBlockingPolicy _blockingPolicy;
734+
UIView* _embeddedView;
735735
}
736736
- (instancetype)initWithEmbeddedView:(UIView*)embeddedView
737737
platformViewsController:
@@ -741,6 +741,7 @@ - (instancetype)initWithEmbeddedView:(UIView*)embeddedView
741741
self = [super initWithFrame:embeddedView.frame];
742742
if (self) {
743743
self.multipleTouchEnabled = YES;
744+
_embeddedView = embeddedView;
744745
embeddedView.autoresizingMask =
745746
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
746747

@@ -762,6 +763,10 @@ - (instancetype)initWithEmbeddedView:(UIView*)embeddedView
762763
return self;
763764
}
764765

766+
- (UIView*)embeddedView {
767+
return [[_embeddedView retain] autorelease];
768+
}
769+
765770
- (void)releaseGesture {
766771
_delayingRecognizer.get().state = UIGestureRecognizerStateFailed;
767772
}

shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@class FlutterPlatformViewsTestMockPlatformView;
1818
static FlutterPlatformViewsTestMockPlatformView* gMockPlatformView = nil;
1919
const float kFloatCompareEpsilon = 0.001;
20+
BOOL enableViewCreateOnceCheck = NO;
2021

2122
@interface FlutterPlatformViewsTestMockPlatformView : UIView
2223
@end
@@ -50,6 +51,21 @@ - (instancetype)init {
5051
return self;
5152
}
5253

54+
- (UIView*)view {
55+
[self checkViewCreatedOnce];
56+
return _view;
57+
}
58+
59+
- (void)checkViewCreatedOnce {
60+
if (enableViewCreateOnceCheck) {
61+
static bool created = false;
62+
if (created) {
63+
abort();
64+
}
65+
created = true;
66+
}
67+
}
68+
5369
- (void)dealloc {
5470
[_view release];
5571
_view = nil;
@@ -113,6 +129,62 @@ @interface FlutterPlatformViewsTest : XCTestCase
113129

114130
@implementation FlutterPlatformViewsTest
115131

132+
- (void)testFlutterViewOnlyCreateOnceInOneFrame {
133+
enableViewCreateOnceCheck = YES;
134+
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
135+
auto thread_task_runner = CreateNewThread("FlutterPlatformViewsTest");
136+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
137+
/*platform=*/thread_task_runner,
138+
/*raster=*/thread_task_runner,
139+
/*ui=*/thread_task_runner,
140+
/*io=*/thread_task_runner);
141+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
142+
/*delegate=*/mock_delegate,
143+
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
144+
/*task_runners=*/runners);
145+
146+
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
147+
148+
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
149+
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
150+
flutterPlatformViewsController->RegisterViewFactory(
151+
factory, @"MockFlutterPlatformView",
152+
FlutterPlatformViewGestureRecognizersBlockingPolicyEager);
153+
FlutterResult result = ^(id result) {
154+
};
155+
flutterPlatformViewsController->OnMethodCall(
156+
[FlutterMethodCall
157+
methodCallWithMethodName:@"create"
158+
arguments:@{@"id" : @2, @"viewType" : @"MockFlutterPlatformView"}],
159+
result);
160+
UIView* mockFlutterView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)] autorelease];
161+
flutterPlatformViewsController->SetFlutterView(mockFlutterView);
162+
// Create embedded view params
163+
flutter::MutatorsStack stack;
164+
// Layer tree always pushes a screen scale factor to the stack
165+
SkMatrix screenScaleMatrix =
166+
SkMatrix::MakeScale([UIScreen mainScreen].scale, [UIScreen mainScreen].scale);
167+
stack.PushTransform(screenScaleMatrix);
168+
// Push a translate matrix
169+
SkMatrix translateMatrix = SkMatrix::MakeTrans(100, 100);
170+
stack.PushTransform(translateMatrix);
171+
SkMatrix finalMatrix;
172+
finalMatrix.setConcat(screenScaleMatrix, translateMatrix);
173+
174+
auto embeddedViewParams =
175+
std::make_unique<flutter::EmbeddedViewParams>(finalMatrix, SkSize::Make(300, 300), stack);
176+
177+
flutterPlatformViewsController->PrerollCompositeEmbeddedView(2, std::move(embeddedViewParams));
178+
flutterPlatformViewsController->CompositeEmbeddedView(2);
179+
180+
flutterPlatformViewsController->GetPlatformViewRect(2);
181+
182+
XCTAssertNotNil(gMockPlatformView);
183+
184+
flutterPlatformViewsController->Reset();
185+
enableViewCreateOnceCheck = NO;
186+
}
187+
116188
- (void)testCanCreatePlatformViewWithoutFlutterView {
117189
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
118190
auto thread_task_runner = CreateNewThread("FlutterPlatformViewsTest");

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ class FlutterPlatformViewsController {
149149
void PrerollCompositeEmbeddedView(int view_id,
150150
std::unique_ptr<flutter::EmbeddedViewParams> params);
151151

152-
// Returns the `FlutterPlatformView` object associated with the view_id.
152+
// Returns the `FlutterPlatformView`'s `view` object associated with the view_id.
153153
//
154154
// If the `FlutterPlatformViewsController` does not contain any `FlutterPlatformView` object or
155155
// a `FlutterPlatformView` object asscociated with the view_id cannot be found, the method
156156
// returns nil.
157-
NSObject<FlutterPlatformView>* GetPlatformViewByID(int view_id);
157+
UIView* GetPlatformViewByID(int view_id);
158158

159159
PostPrerollResult PostPrerollAction(fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger);
160160

@@ -325,6 +325,9 @@ class FlutterPlatformViewsController {
325325

326326
// Prevent the touch sequence from ever arriving to the embedded view.
327327
- (void)blockGesture;
328+
329+
// Get embedded view
330+
- (UIView*)embeddedView;
328331
@end
329332

330333
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_

shell/platform/darwin/ios/framework/Source/SemanticsObject.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ - (instancetype)initWithSemanticsObject:(SemanticsObject*)object {
560560
flutter::FlutterPlatformViewsController* controller =
561561
object.bridge->GetPlatformViewsController();
562562
if (controller) {
563-
_platformView = [[controller->GetPlatformViewByID(object.node.platformViewId) view] retain];
563+
_platformView = [controller->GetPlatformViewByID(object.node.platformViewId) retain];
564564
}
565565
}
566566
return self;

0 commit comments

Comments
 (0)