2222namespace flutter {
2323namespace {
2424
25- constexpr int kImplicitViewId = 0 ;
25+ static constexpr int64_t kFlutterImplicitViewId = 0ll ;
2626
2727Dart_Handle ToByteData (const fml::Mapping& buffer) {
2828 return tonic::DartByteData::Create (buffer.GetMapping (), buffer.GetSize ());
@@ -34,7 +34,11 @@ PlatformConfigurationClient::~PlatformConfigurationClient() {}
3434
3535PlatformConfiguration::PlatformConfiguration (
3636 PlatformConfigurationClient* client)
37- : client_(client) {}
37+ : client_(client) {
38+ if (client_->ImplicitViewEnabled ()) {
39+ DoAddView (kFlutterImplicitViewId );
40+ }
41+ }
3842
3943PlatformConfiguration::~PlatformConfiguration () {}
4044
@@ -43,6 +47,10 @@ void PlatformConfiguration::DidCreateIsolate() {
4347
4448 on_error_.Set (tonic::DartState::Current (),
4549 Dart_GetField (library, tonic::ToDart (" _onError" )));
50+ add_view_.Set (tonic::DartState::Current (),
51+ Dart_GetField (library, tonic::ToDart (" _addView" )));
52+ remove_view_.Set (tonic::DartState::Current (),
53+ Dart_GetField (library, tonic::ToDart (" _removeView" )));
4654 update_displays_.Set (
4755 tonic::DartState::Current (),
4856 Dart_GetField (library, tonic::ToDart (" _updateDisplays" )));
@@ -76,12 +84,46 @@ void PlatformConfiguration::DidCreateIsolate() {
7684 report_timings_.Set (tonic::DartState::Current (),
7785 Dart_GetField (library, tonic::ToDart (" _reportTimings" )));
7886
79- // TODO(loicsharma): This should only be created if the embedder enables the
80- // implicit view.
81- // See: https://github.com/flutter/flutter/issues/120306
82- windows_.emplace (kImplicitViewId ,
83- std::make_unique<Window>(
84- kImplicitViewId , ViewportMetrics{1.0 , 0.0 , 0.0 , -1 , 0 }));
87+ library_.Set (tonic::DartState::Current (),
88+ Dart_LookupLibrary (tonic::ToDart (" dart:ui" )));
89+ }
90+
91+ void PlatformConfiguration::AddView (int64_t view_id) {
92+ FML_DCHECK (view_id != kFlutterImplicitViewId );
93+ DoAddView (view_id);
94+ }
95+
96+ void PlatformConfiguration::DoAddView (int64_t view_id) {
97+ windows_.emplace (
98+ view_id, std::make_unique<Window>(library_, view_id,
99+ ViewportMetrics{1.0 , 0.0 , 0.0 , -1 , 0 }));
100+ if (view_id == kFlutterImplicitViewId ) {
101+ return ;
102+ }
103+ std::shared_ptr<tonic::DartState> dart_state = add_view_.dart_state ().lock ();
104+ if (!dart_state) {
105+ return ;
106+ }
107+ tonic::DartState::Scope scope (dart_state);
108+ tonic::CheckAndHandleError (
109+ tonic::DartInvoke (add_view_.Get (), {
110+ tonic::ToDart (view_id),
111+ }));
112+ }
113+
114+ void PlatformConfiguration::RemoveView (int64_t view_id) {
115+ FML_DCHECK (view_id != kFlutterImplicitViewId );
116+ windows_.erase (view_id);
117+ std::shared_ptr<tonic::DartState> dart_state =
118+ remove_view_.dart_state ().lock ();
119+ if (!dart_state) {
120+ return ;
121+ }
122+ tonic::DartState::Scope scope (dart_state);
123+ tonic::CheckAndHandleError (
124+ tonic::DartInvoke (remove_view_.Get (), {
125+ tonic::ToDart (view_id),
126+ }));
85127}
86128
87129void PlatformConfiguration::UpdateDisplays (
@@ -492,16 +534,6 @@ Dart_Handle PlatformConfigurationNativeApi::ComputePlatformResolvedLocale(
492534 return tonic::DartConverter<std::vector<std::string>>::ToDart (results);
493535}
494536
495- Dart_Handle PlatformConfigurationNativeApi::ImplicitViewEnabled () {
496- UIDartState::ThrowIfUIOperationsProhibited ();
497- bool enabled = UIDartState::Current ()
498- ->platform_configuration ()
499- ->client ()
500- ->ImplicitViewEnabled ();
501-
502- return Dart_NewBoolean (enabled);
503- }
504-
505537std::string PlatformConfigurationNativeApi::DefaultRouteName () {
506538 UIDartState::ThrowIfUIOperationsProhibited ();
507539 return UIDartState::Current ()
0 commit comments