Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterConstants.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The name of the Info.plist flag to enable the embedded MacOS views preview.
const char* const kEmbeddedViewsPreview = "io.flutter_embedded_views_preview";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need the constant in a separate file, can move it to FlutterPlatformViewController.mm

Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,17 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
},
.identifier = ++sTaskRunnerIdentifiers,
};

bool embedded_views_preview_enabled = [FlutterPlatformViewController embeddedViewsEnabled];

const FlutterCustomTaskRunners custom_task_runners = {
.struct_size = sizeof(FlutterCustomTaskRunners),
.platform_task_runner = &cocoa_task_runner_description,
};
// If platform views are enabled, set the render thread to the platform thread.
// Otherwise the render thread is created separately in embedder_thread_host.cc.
.render_task_runner =
embedded_views_preview_enabled ? &cocoa_task_runner_description : nullptr};

flutterArguments.custom_task_runners = &custom_task_runners;

[self loadAOTData:_project.assetsPath];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@

#include "flutter/fml/logging.h"

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterConstants.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h"

@implementation FlutterPlatformViewController
@implementation FlutterPlatformViewController {
bool _embeddedViewsEnabled;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this instance method.

}

- (instancetype)init {
self = [super init];

self->_platformViewFactories = [[NSMutableDictionary alloc] init];
_platformViewFactories = [[NSMutableDictionary alloc] init];
[self setEmbeddedViewsEnabled:[FlutterPlatformViewController embeddedViewsEnabled]];
return self;
}

- (void)onCreateWithViewId:(int64_t)viewId
viewType:(nonnull NSString*)viewType
result:(nonnull FlutterResult)result {
if (!_embeddedViewsEnabled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this, the if (factory == nil) already handles this.
Same in onDispose

NSLog(@"Must set `io.flutter_embedded_views_preview` to true in Info.plist to enable platform "
@"views");
return;
}
if (_platformViews.count(viewId) != 0) {
result([FlutterError errorWithCode:@"recreating_view"
message:@"trying to create an already created view"
Expand All @@ -42,6 +51,12 @@ - (void)onCreateWithViewId:(int64_t)viewId
}

- (void)onDisposeWithViewId:(int64_t)viewId result:(nonnull FlutterResult)result {
if (!_embeddedViewsEnabled) {
NSLog(@"Must set `io.flutter_embedded_views_preview` to true in Info.plist to enable platform "
@"views");
return;
}

if (_platformViews.count(viewId) == 0) {
result([FlutterError errorWithCode:@"unknown_view"
message:@"trying to dispose an unknown"
Expand All @@ -56,6 +71,11 @@ - (void)onDisposeWithViewId:(int64_t)viewId result:(nonnull FlutterResult)result

- (void)registerViewFactory:(nonnull NSObject<FlutterPlatformViewFactory>*)factory
withId:(nonnull NSString*)factoryId {
if (!_embeddedViewsEnabled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just be if (![self embeddedViewsEnabled])

NSLog(@"Must set `io.flutter_embedded_views_preview` to true in Info.plist to enable platform "
@"views");
return;
}
_platformViewFactories[factoryId] = factory;
}

Expand Down Expand Up @@ -89,4 +109,12 @@ - (void)disposePlatformViews {
_platformViewsToDispose.clear();
}

- (void)setEmbeddedViewsEnabled:(bool)embeddedViewsEnabled {
_embeddedViewsEnabled = embeddedViewsEnabled;
}

+ (bool)embeddedViewsEnabled {
return [[[NSBundle mainBundle] objectForInfoDictionaryKey:@(kEmbeddedViewsPreview)] boolValue];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,33 @@

namespace flutter::testing {

TEST(FlutterPlatformViewController, EmbeddedViewsDisabled) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
[platformViewController setEmbeddedViewsEnabled:false];

FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"create"
arguments:@{
@"id" : @2,
@"viewType" : @"FlutterPlatformViewMock"
}];

__block bool called = false;
FlutterResult result = ^(id result) {
called = true;
};

[platformViewController handleMethodCall:methodCall result:result];

// The call should not have happened if embeddedViewsEnabled is false.
EXPECT_FALSE(called);
}

TEST(FlutterPlatformViewController, TestCreatePlatformViewNoMatchingViewType) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
[platformViewController setEmbeddedViewsEnabled:true];

FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"create"
Expand All @@ -38,6 +62,7 @@
TEST(FlutterPlatformViewController, TestRegisterPlatformViewFactoryAndCreate) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
[platformViewController setEmbeddedViewsEnabled:true];

FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc];

Expand Down Expand Up @@ -65,6 +90,7 @@
TEST(FlutterPlatformViewController, TestCreateAndDispose) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
[platformViewController setEmbeddedViewsEnabled:true];

FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc];

Expand Down Expand Up @@ -108,6 +134,7 @@
TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
[platformViewController setEmbeddedViewsEnabled:true];

FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"dispose"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@
*/
- (void)disposePlatformViews;

/**
* Set _embedded_views_preview_enabled value.
*/
- (void)setEmbeddedViewsEnabled:(bool)embeddedViewsEnabled;

/**
* Return whether or not platform views are enabled by looking for the
* io.flutter_embedded_views_preview flag in Info.plist.
*/
+ (bool)embeddedViewsEnabled;

@end
30 changes: 25 additions & 5 deletions shell/platform/darwin/macos/framework/Source/FlutterView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
Expand All @@ -15,6 +16,7 @@ @interface FlutterView () <FlutterResizeSynchronizerDelegate> {
__weak id<FlutterViewReshapeListener> _reshapeListener;
FlutterResizeSynchronizer* _resizeSynchronizer;
FlutterSurfaceManager* _surfaceManager;
bool _embedded_views_preview_enabled;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need this ivar, can just use [FlutterPlatformViewController embeddedViewsEnabled] when necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ivars should be camelCase.

}

@end
Expand Down Expand Up @@ -42,6 +44,9 @@ - (instancetype)initWithFrame:(NSRect)frame

_reshapeListener = reshapeListener;
}

_embedded_views_preview_enabled = [FlutterPlatformViewController embeddedViewsEnabled];

return self;
}

Expand All @@ -67,15 +72,30 @@ - (int)frameBufferIDForSize:(CGSize)size {
}

- (void)present {
[_resizeSynchronizer requestCommit];
// If _embedded_views_preview_enabled is true, the main and raster
// threads are merged. Thus we cannot call resizeSynchronizer::requestCommit
// as it blocks on the raster thread.
if (_embedded_views_preview_enabled) {
[self resizeSynchronizerFlush:_resizeSynchronizer];
[self resizeSynchronizerCommit:_resizeSynchronizer];
} else {
[_resizeSynchronizer requestCommit];
}
}

- (void)reshaped {
CGSize scaledSize = [self convertSizeToBacking:self.bounds.size];
[_resizeSynchronizer beginResize:scaledSize
notify:^{
[_reshapeListener viewDidReshape:self];
}];
// If _embedded_views_preview_enabled is true, the main and raster
// threads are merged. Thus we cannot call resizeSynchronizer::beginResize
// as it blocks on the main thread.
if (_embedded_views_preview_enabled) {
[_reshapeListener viewDidReshape:self];
} else {
[_resizeSynchronizer beginResize:scaledSize
notify:^{
[_reshapeListener viewDidReshape:self];
}];
}
}

#pragma mark - NSView overrides
Expand Down