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

[macOS] Handle reparenting platform views #52152

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions shell/platform/darwin/macos/framework/Source/FlutterMutatorView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,18 @@ - (void)updatePlatformViewWithBounds:(CGRect)untransformedBounds
[containerSuperview addSubview:_platformViewContainer];
_platformViewContainer.frame = self.bounds;

// Nest the platform view in the PlatformViewContainer.
[_platformViewContainer addSubview:_platformView];
_platformView.frame = untransformedBounds;
// Nest the platform view in the PlatformViewContainer, but only if the view doesn't have a
// superview yet. Sometimes the platform view reparents itself (WKWebView entering FullScreen)
// in which case do not forcefully move it back.
if (_platformView.superview == nil) {
[_platformViewContainer addSubview:_platformView];
}

// Originally first subview would be the _platformView. However during WKWebView full screen
// the platform view gets replaced with a placeholder. Given that _platformViewContainer does
// not contain any other views it is safe to assume that any subview found can be treated
// as the platform view.
_platformViewContainer.subviews.firstObject.frame = untransformedBounds;

// Transform for the platform view is finalTransform adjusted for bounding rect origin.
CATransform3D translation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,28 @@ void ExpectTransform3DEqual(const CATransform3D& t, const CATransform3D& u) {
EXPECT_EQ([mutatorView hitTest:NSMakePoint(10, 50)], platformView);
}

TEST(FlutterMutatorViewTest, ReparentingPlatformView) {
NSView* platformView = [[NSView alloc] init];
FlutterMutatorView* mutatorView = [[FlutterMutatorView alloc] initWithPlatformView:platformView];
ApplyFlutterLayer(mutatorView, FlutterSize{100, 100}, {});
EXPECT_TRUE(platformView.superview == mutatorView.platformViewContainer);
EXPECT_TRUE(CGRectEqualToRect(platformView.frame, CGRectMake(0, 0, 100, 100)));

// Reparent platform view and replace it with placeholder (mimicking WKWebKit going full screen)
NSView* newParent = [[NSView alloc] init];
[newParent addSubview:platformView];
platformView.frame = CGRectMake(10, 10, 200, 200);

NSView* placeholderView = [[NSView alloc] init];
[mutatorView.platformViewContainer addSubview:placeholderView];
ApplyFlutterLayer(mutatorView, FlutterSize{100, 100}, {});

// Platform view should not be touched but the replacement view should be properly positioned.
EXPECT_TRUE(platformView.superview == newParent);
EXPECT_TRUE(CGRectEqualToRect(platformView.frame, CGRectMake(10, 10, 200, 200)));
EXPECT_TRUE(CGRectEqualToRect(placeholderView.frame, CGRectMake(0, 0, 100, 100)));
}

@interface FlutterCursorCoordinatorTest : NSObject

@end
Expand Down