@@ -66,14 +66,6 @@ typedef ErrorCallback = bool Function(Object exception, StackTrace stackTrace);
6666// A gesture setting value that indicates it has not been set by the engine.
6767const double _kUnsetGestureSetting = - 1.0 ;
6868
69- // The view ID of PlatformDispatcher.implicitView. This is an
70- // implementation detail that may change at any time. Apps
71- // should always use PlatformDispatcher.implicitView to determine
72- // the current implicit view, if any.
73- //
74- // Keep this in sync with kImplicitViewId in window/platform_configuration.cc.
75- const int _kImplicitViewId = 0 ;
76-
7769// A message channel to receive KeyData from the platform.
7870//
7971// See embedder.cc::kFlutterKeyDataChannel for more information.
@@ -218,10 +210,29 @@ class PlatformDispatcher {
218210 /// * [View.of] , for accessing the current view.
219211 /// * [PlatformDispatcher.views] for a list of all [FlutterView] s provided
220212 /// by the platform.
221- FlutterView ? get implicitView => _implicitViewEnabled () ? _views[_kImplicitViewId] : null ;
222-
223- @Native < Handle Function ()> (symbol: 'PlatformConfigurationNativeApi::ImplicitViewEnabled' )
224- external static bool _implicitViewEnabled ();
213+ FlutterView ? get implicitView {
214+ final FlutterView ? result = _views[_implicitViewId];
215+ // Make sure [implicitView] agrees with `_implicitViewId`.
216+ assert ((result != null ) == (_implicitViewId != null ),
217+ (_implicitViewId != null ) ?
218+ 'The implicit view ID is $_implicitViewId , but the implicit view does not exist.' :
219+ 'The implicit view ID is null, but the implicit view exists.' );
220+ // Make sure [implicitView] never chages.
221+ assert (() {
222+ if (_debugRecordedLastImplicitView) {
223+ assert (identical (_debugLastImplicitView, result),
224+ 'The implicitView has changed:\n '
225+ 'Last: $_debugLastImplicitView \n Current: $result ' );
226+ } else {
227+ _debugLastImplicitView = result;
228+ _debugRecordedLastImplicitView = true ;
229+ }
230+ return true ;
231+ }());
232+ return result;
233+ }
234+ FlutterView ? _debugLastImplicitView;
235+ bool _debugRecordedLastImplicitView = false ;
225236
226237 /// A callback that is invoked whenever the [ViewConfiguration] of any of the
227238 /// [views] changes.
@@ -249,6 +260,34 @@ class PlatformDispatcher {
249260 _onMetricsChangedZone = Zone .current;
250261 }
251262
263+ // Called from the engine, via hooks.dart
264+ //
265+ // Adds a new view with the specific view configuration.
266+ //
267+ // The implicit view must be added before [implicitView] is first called,
268+ // which is typically the main function.
269+ void _addView (int id, _ViewConfiguration viewConfiguration) {
270+ assert (! _views.containsKey (id), 'View ID $id already exists.' );
271+ _views[id] = FlutterView ._(id, this , viewConfiguration);
272+ _invoke (onMetricsChanged, _onMetricsChangedZone);
273+ }
274+
275+ // Called from the engine, via hooks.dart
276+ //
277+ // Removes the specific view.
278+ //
279+ // The target view must must exist. The implicit view must not be removed,
280+ // or an assertion will be triggered.
281+ void _removeView (int id) {
282+ assert (id != _implicitViewId, 'The implicit view #$id can not be removed.' );
283+ if (id == _implicitViewId) {
284+ return ;
285+ }
286+ assert (_views.containsKey (id), 'View ID $id does not exist.' );
287+ _views.remove (id);
288+ _invoke (onMetricsChanged, _onMetricsChangedZone);
289+ }
290+
252291 // Called from the engine, via hooks.dart.
253292 //
254293 // Updates the available displays.
@@ -264,15 +303,8 @@ class PlatformDispatcher {
264303 //
265304 // Updates the metrics of the window with the given id.
266305 void _updateWindowMetrics (int viewId, _ViewConfiguration viewConfiguration) {
267- final FlutterView ? view = _views[viewId];
268- if (viewId == _kImplicitViewId && view == null ) {
269- // TODO(goderbauer): Remove the implicit creation of the implicit view
270- // when we have an addView API and the implicit view is added via that.
271- _views[viewId] = FlutterView ._(viewId, this , viewConfiguration);
272- } else {
273- assert (view != null );
274- view! ._viewConfiguration = viewConfiguration;
275- }
306+ assert (_views.containsKey (viewId), 'View $viewId does not exist.' );
307+ _views[viewId]! ._viewConfiguration = viewConfiguration;
276308 _invoke (onMetricsChanged, _onMetricsChangedZone);
277309 }
278310
0 commit comments