10 #include "flutter/common/task_runners.h"
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/synchronization/waitable_event.h"
13 #include "flutter/fml/trace_event.h"
14 #include "flutter/shell/common/shell_io_manager.h"
15 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
16 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
25 const std::shared_ptr<IOSContext>& context,
27 const flutter::TaskRunners& task_runners)
28 : PlatformView(delegate, task_runners),
29 ios_context_(context),
30 platform_views_controller_(platform_views_controller),
31 platform_message_handler_(
35 PlatformView::Delegate& delegate,
37 const flutter::TaskRunners& task_runners,
38 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch)
41 IOSContext::Create(is_gpu_disabled_sync_switch, delegate.OnPlatformViewGetSettings()),
42 platform_views_controller,
49 platform_message_handler_->HandlePlatformMessage(std::move(message));
53 return owner_controller_;
57 FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
58 std::lock_guard<std::mutex> guard(ios_surface_mutex_);
60 if (ios_surface_ || !owner_controller) {
64 owner_controller_ = owner_controller;
65 ApplyLocaleToOwnerController();
68 dealloc_view_controller_observer_.reset([[NSNotificationCenter defaultCenter]
70 object:owner_controller_
71 queue:[NSOperationQueue mainQueue]
72 usingBlock:^(NSNotification* note) {
74 if (accessibility_bridge_) {
78 static_cast<FlutterView*
>(deallocatingController.viewIfLoaded);
79 accessibility_bridge_->SetViewController(nil, deallocatingView);
81 owner_controller_ = nil;
84 if (owner_controller_ && owner_controller_.isViewLoaded) {
87 EnsureAccessibilityBridge();
88 if (accessibility_bridge_) {
89 accessibility_bridge_->SetViewController(owner_controller_, previousView);
99 FML_DCHECK(owner_controller_);
100 FML_DCHECK(owner_controller_.isViewLoaded) <<
"FlutterViewController's view should be loaded "
101 "before attaching to PlatformViewIOS.";
103 CALayer* ca_layer = flutter_view.layer;
105 FML_DCHECK(ios_surface_ !=
nullptr);
107 EnsureAccessibilityBridge();
108 if (accessibility_bridge_) {
109 accessibility_bridge_->SetViewController(owner_controller_, previousView);
110 if (accessibility_bridge_->HasSemantics()) {
111 PostSemanticsUpdateNotification();
117 NSObject<FlutterTexture>* texture) {
118 RegisterTexture(ios_context_->CreateExternalTexture(
texture_id, texture));
123 FML_DCHECK(task_runners_.GetRasterTaskRunner()->RunsTasksOnCurrentThread());
124 std::lock_guard<std::mutex> guard(ios_surface_mutex_);
126 FML_DLOG(INFO) <<
"Could not CreateRenderingSurface, this PlatformViewIOS "
127 "has no ViewController.";
130 return ios_surface_->CreateGPUSurface();
135 return std::make_shared<IOSExternalViewEmbedder>(platform_views_controller_, ios_context_);
140 return ios_context_->GetImpellerContext();
145 flutter::SemanticsNodeUpdates update,
146 flutter::CustomAccessibilityActionUpdates actions) {
147 EnsureAccessibilityBridge();
148 if (!accessibility_bridge_) {
151 accessibility_bridge_.get()->UpdateSemantics(std::move(update), actions);
152 if (owner_controller_ && owner_controller_.isViewLoaded) {
158 PostSemanticsUpdateNotification();
164 application_locale_ = std::move(locale);
165 ApplyLocaleToOwnerController();
170 semantics_tree_enabled_ = enabled;
172 EnsureAccessibilityBridge();
174 accessibility_bridge_.reset();
180 return std::make_unique<VsyncWaiterIOS>(task_runners_, FlutterDisplayLinkManager.shared);
185 if (accessibility_bridge_) {
186 accessibility_bridge_.get()->clearState();
188 if (!owner_controller_) {
191 [owner_controller_.platformViewsController reset];
192 [owner_controller_.restorationPlugin reset];
193 [owner_controller_.textInputPlugin reset];
198 const std::vector<std::string>& supported_locale_data) {
199 size_t localeDataLength = 3;
200 NSMutableArray<NSString*>* supported_locale_identifiers =
201 [NSMutableArray arrayWithCapacity:supported_locale_data.size() / localeDataLength];
202 for (
size_t i = 0; i < supported_locale_data.size(); i += localeDataLength) {
203 NSDictionary<NSString*, NSString*>* dict = @{
204 NSLocaleLanguageCode : [NSString stringWithUTF8String:supported_locale_data[i].c_str()]
206 NSLocaleCountryCode : [NSString stringWithUTF8String:supported_locale_data[i + 1].c_str()]
208 NSLocaleScriptCode : [NSString stringWithUTF8String:supported_locale_data[i + 2].c_str()]
211 [supported_locale_identifiers addObject:[NSLocale localeIdentifierFromComponents:dict]];
213 NSArray<NSString*>* result =
214 [NSBundle preferredLocalizationsFromArray:supported_locale_identifiers];
217 std::unique_ptr<std::vector<std::string>> out = std::make_unique<std::vector<std::string>>();
219 if (result !=
nullptr && [result count] > 0) {
220 NSLocale* locale = [NSLocale localeWithLocaleIdentifier:[result firstObject]];
221 NSString* languageCode = [locale languageCode];
222 out->emplace_back(languageCode ==
nullptr ?
"" : languageCode.UTF8String);
223 NSString* countryCode = [locale countryCode];
224 out->emplace_back(countryCode ==
nullptr ?
"" : countryCode.UTF8String);
225 NSString* scriptCode = [locale scriptCode];
226 out->emplace_back(scriptCode ==
nullptr ?
"" : scriptCode.UTF8String);
231 void PlatformViewIOS::PostSemanticsUpdateNotification() {
233 object:owner_controller_];
236 void PlatformViewIOS::ApplyLocaleToOwnerController() {
237 if (owner_controller_) {
238 owner_controller_.applicationLocale =
239 application_locale_.empty() ? nil : @(application_locale_.data());
243 void PlatformViewIOS::EnsureAccessibilityBridge() {
244 if (semantics_tree_enabled_ && !accessibility_bridge_ && owner_controller_) {
245 accessibility_bridge_ =
246 std::make_unique<AccessibilityBridge>(owner_controller_,
this, platform_views_controller_);
250 PlatformViewIOS::ScopedObserver::ScopedObserver() {}
252 PlatformViewIOS::ScopedObserver::~ScopedObserver() {
254 [[NSNotificationCenter defaultCenter] removeObserver:observer_];
258 void PlatformViewIOS::ScopedObserver::reset(id<NSObject> observer) {
259 if (observer != observer_) {
261 [[NSNotificationCenter defaultCenter] removeObserver:observer_];
263 observer_ = observer;
FLUTTER_DARWIN_EXPORT NSNotificationName const FlutterSemanticsUpdateNotification
NSNotificationName const FlutterViewControllerWillDealloc
Manages the lifetime of the on-screen and off-screen rendering contexts on iOS. On-screen contexts ar...
static std::unique_ptr< IOSSurface > Create(const std::shared_ptr< IOSContext > &context, CALayer *layer)