-
Notifications
You must be signed in to change notification settings - Fork 6k
[MacOS] Add support for creating and presenting platform views. #22905
Changes from 1 commit
c1fbeac
f049b8b
4b85acf
4dbbff9
b8f5837
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // 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. | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
|
|
||
| #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h" | ||
|
|
||
| @implementation FlutterPlatformViewController | ||
|
|
||
| - (instancetype)init { | ||
| self = [super init]; | ||
|
|
||
| self->_platformViewFactories = [[NSMutableDictionary alloc] init]; | ||
RichardJCai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return self; | ||
| } | ||
|
|
||
| - (void)onCreateWithViewId:(int64_t)viewId | ||
| viewType:(nonnull NSString*)viewType | ||
| result:(nonnull FlutterResult)result { | ||
| if (_platformViews.count(viewId) != 0) { | ||
| result([FlutterError errorWithCode:@"recreating_view" | ||
| message:@"trying to create an already created view" | ||
| details:[NSString stringWithFormat:@"view id: '%lld'", viewId]]); | ||
| } | ||
|
|
||
| NSObject<FlutterPlatformViewFactory>* factory = _platformViewFactories[viewType]; | ||
| if (factory == nil) { | ||
| result([FlutterError | ||
| errorWithCode:@"unregistered_view_type" | ||
| message:@"trying to create a view with an unregistered type" | ||
| details:[NSString stringWithFormat:@"unregistered view type: '%@'", viewType]]); | ||
| return; | ||
| } | ||
|
|
||
| NSObject<FlutterPlatformView>* platform_view = [factory createWithFrame:CGRectZero | ||
| viewIdentifier:viewId | ||
| arguments:nil]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this always nil? |
||
|
|
||
| _platformViews[viewId] = [platform_view view]; | ||
| result(nil); | ||
| } | ||
|
|
||
| - (void)onDisposeWithViewId:(int64_t)viewId result:(nonnull FlutterResult)result { | ||
| if (_platformViews.count(viewId) == 0) { | ||
| result([FlutterError errorWithCode:@"unknown_view" | ||
| message:@"trying to dispose an unknown" | ||
| details:[NSString stringWithFormat:@"view id: '%lld'", viewId]]); | ||
| return; | ||
| } | ||
|
|
||
| // The following disposePlatformViews call will dispose the views. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what "the following [...] call" is referring to here. Do you mean "The next call to"? |
||
| _platformViewsToDispose.insert(viewId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that you're already doing a lookup to see if it's present anyway, why not insert the view itself here to avoid a second lookup later (and to make the structure match its name; it sounds like it contains views, but currently doesn't). |
||
| result(nil); | ||
| } | ||
|
|
||
| - (void)registerViewFactory:(nonnull NSObject<FlutterPlatformViewFactory>*)factory | ||
| withId:(nonnull NSString*)factoryId { | ||
| _platformViewFactories[factoryId] = factory; | ||
| } | ||
|
|
||
| - (void)handleMethodCall:(nonnull FlutterMethodCall*)call result:(nonnull FlutterResult)result { | ||
| if ([[call method] isEqualToString:@"create"]) { | ||
| NSMutableDictionary<NSString*, id>* args = [call arguments]; | ||
| int64_t viewId = [args[@"id"] longValue]; | ||
| NSString* viewType = [NSString stringWithUTF8String:([args[@"viewType"] UTF8String])]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you round-tripping through a C string here? |
||
| [self onCreateWithViewId:viewId viewType:viewType result:result]; | ||
| } else if ([[call method] isEqualToString:@"dispose"]) { | ||
| NSNumber* arg = [call arguments]; | ||
| int64_t viewId = [arg longLongValue]; | ||
| [self onDisposeWithViewId:viewId result:result]; | ||
| } else { | ||
| result(FlutterMethodNotImplemented); | ||
| } | ||
| } | ||
|
|
||
| - (void)disposePlatformViews { | ||
| if (_platformViewsToDispose.empty()) { | ||
| return; | ||
| } | ||
|
|
||
| FML_DCHECK([[NSThread currentThread] isMainThread]) | ||
| << "Must be on the main thread to handle disposing platform views"; | ||
| for (int64_t viewId : _platformViewsToDispose) { | ||
| NSView* view = _platformViews[viewId]; | ||
| [view removeFromSuperview]; | ||
| _platformViews.erase(viewId); | ||
| } | ||
| _platformViewsToDispose.clear(); | ||
| } | ||
|
|
||
| @end | ||
Uh oh!
There was an error while loading. Please reload this page.