33// found in the LICENSE file.
44
55import 'dart:html' as html;
6- import 'dart:typed_data' ;
76
8- import 'package:ui/src/engine.dart' show window, NullTreeSanitizer;
7+ import 'package:ui/src/engine.dart' show window, NullTreeSanitizer, platformViewManager, createPlatformViewSlot ;
98import 'package:ui/ui.dart' as ui;
109
1110import '../html/path_to_svg_clip.dart' ;
12- import '../services.dart' ;
1311import '../util.dart' ;
1412import '../vector_math.dart' ;
1513import 'canvas.dart' ;
@@ -37,11 +35,13 @@ class HtmlViewEmbedder {
3735 final Map <int , EmbeddedViewParams > _currentCompositionParams =
3836 < int , EmbeddedViewParams > {};
3937
40- /// The HTML element associated with the given view id.
41- final Map <int ?, html.Element > _views = < int ? , html.Element > {};
42-
43- /// The root view in the stack of mutator elements for the view id.
44- final Map <int ?, html.Element ?> _rootViews = < int ? , html.Element ? > {};
38+ /// The clip chain for a view Id.
39+ ///
40+ /// This contains:
41+ /// * The root view in the stack of mutator elements for the view id.
42+ /// * The slot view in the stack (what shows the actual platform view contents).
43+ /// * The number of clipping elements used last time the view was composited.
44+ final Map <int , ViewClipChain > _viewClipChains = < int , ViewClipChain > {};
4545
4646 /// Surfaces used to draw on top of platform views, keyed by platform view ID.
4747 ///
@@ -51,18 +51,12 @@ class HtmlViewEmbedder {
5151 /// The views that need to be recomposited into the scene on the next frame.
5252 final Set <int > _viewsToRecomposite = < int > {};
5353
54- /// The views that need to be disposed of on the next frame.
55- final Set <int > _viewsToDispose = < int > {};
56-
5754 /// The list of view ids that should be composited, in order.
5855 List <int > _compositionOrder = < int > [];
5956
6057 /// The most recent composition order.
6158 List <int > _activeCompositionOrder = < int > [];
6259
63- /// The number of clipping elements used last time the view was composited.
64- Map <int , int > _clipCount = < int , int > {};
65-
6660 /// The size of the frame, in physical pixels.
6761 ui.Size _frameSize = ui.window.physicalSize;
6862
@@ -74,76 +68,6 @@ class HtmlViewEmbedder {
7468 _frameSize = size;
7569 }
7670
77- void handlePlatformViewCall (
78- ByteData ? data,
79- ui.PlatformMessageResponseCallback ? callback,
80- ) {
81- const MethodCodec codec = StandardMethodCodec ();
82- final MethodCall decoded = codec.decodeMethodCall (data);
83-
84- switch (decoded.method) {
85- case 'create' :
86- _create (decoded, callback);
87- return ;
88- case 'dispose' :
89- _dispose (decoded, callback! );
90- return ;
91- }
92- callback !(null );
93- }
94-
95- void _create (
96- MethodCall methodCall, ui.PlatformMessageResponseCallback ? callback) {
97- final Map <dynamic , dynamic > args = methodCall.arguments;
98- final int ? viewId = args['id' ];
99- final String ? viewType = args['viewType' ];
100- const MethodCodec codec = StandardMethodCodec ();
101-
102- if (_views[viewId] != null ) {
103- callback !(codec.encodeErrorEnvelope (
104- code: 'recreating_view' ,
105- message: 'trying to create an already created view' ,
106- details: 'view id: $viewId ' ,
107- ));
108- return ;
109- }
110-
111- final ui.PlatformViewFactory ? factory =
112- ui.platformViewRegistry.registeredFactories[viewType];
113- if (factory == null ) {
114- callback !(codec.encodeErrorEnvelope (
115- code: 'unregistered_view_type' ,
116- message: 'trying to create a view with an unregistered type' ,
117- details: 'unregistered view type: $viewType ' ,
118- ));
119- return ;
120- }
121-
122- // TODO(het): Support creation parameters.
123- html.Element embeddedView = factory (viewId! );
124- _views[viewId] = embeddedView;
125-
126- _rootViews[viewId] = embeddedView;
127-
128- callback !(codec.encodeSuccessEnvelope (null ));
129- }
130-
131- void _dispose (
132- MethodCall methodCall, ui.PlatformMessageResponseCallback callback) {
133- final int ? viewId = methodCall.arguments;
134- const MethodCodec codec = StandardMethodCodec ();
135- if (viewId == null || ! _views.containsKey (viewId)) {
136- callback (codec.encodeErrorEnvelope (
137- code: 'unknown_view' ,
138- message: 'trying to dispose an unknown view' ,
139- details: 'view id: $viewId ' ,
140- ));
141- return ;
142- }
143- _viewsToDispose.add (viewId);
144- callback (codec.encodeSuccessEnvelope (null ));
145- }
146-
14771 List <CkCanvas > getCurrentCanvases () {
14872 final List <CkCanvas > canvases = < CkCanvas > [];
14973 for (int i = 0 ; i < _compositionOrder.length; i++ ) {
@@ -179,28 +103,39 @@ class HtmlViewEmbedder {
179103 }
180104
181105 void _compositeWithParams (int viewId, EmbeddedViewParams params) {
182- final html. Element platformView = _views[ viewId] ! ;
183- platformView.style.width = '${ params . size . width }px' ;
184- platformView.style.height = '${ params . size . height }px' ;
185- platformView.style.position = 'absolute' ;
106+ // If we haven't seen this viewId yet, cache it for clips/transforms.
107+ ViewClipChain clipChain = _viewClipChains. putIfAbsent (viewId, () {
108+ return ViewClipChain (view : createPlatformViewSlot (viewId)) ;
109+ }) ;
186110
187- // <flt-scene-host> disables pointer events. Reenable them here because the
188- // underlying platform view would want to handle the pointer events.
189- platformView.style.pointerEvents = 'auto' ;
111+ html.Element slot = clipChain.slot;
190112
113+ // See `apply()` in the PersistedPlatformView class for the HTML version
114+ // of this code.
115+ slot.style
116+ ..width = '${params .size .width }px'
117+ ..height = '${params .size .height }px'
118+ ..position = 'absolute' ;
119+
120+ // Recompute the position in the DOM of the `slot` element...
191121 final int currentClippingCount = _countClips (params.mutators);
192- final int ? previousClippingCount = _clipCount[viewId] ;
122+ final int previousClippingCount = clipChain.clipCount ;
193123 if (currentClippingCount != previousClippingCount) {
194- _clipCount[viewId] = currentClippingCount;
195- html.Element oldPlatformViewRoot = _rootViews[viewId]! ;
196- html.Element ? newPlatformViewRoot = _reconstructClipViewsChain (
124+ html.Element oldPlatformViewRoot = clipChain.root;
125+ html.Element newPlatformViewRoot = _reconstructClipViewsChain (
197126 currentClippingCount,
198- platformView ,
127+ slot ,
199128 oldPlatformViewRoot,
200129 );
201- _rootViews[viewId] = newPlatformViewRoot;
130+ // Store the updated root element, and clip count
131+ clipChain.updateClipChain (
132+ root: newPlatformViewRoot,
133+ clipCount: currentClippingCount,
134+ );
202135 }
203- _applyMutators (params.mutators, platformView, viewId);
136+
137+ // Apply mutators to the slot
138+ _applyMutators (params.mutators, slot, viewId);
204139 }
205140
206141 int _countClips (MutatorsStack mutators) {
@@ -213,7 +148,7 @@ class HtmlViewEmbedder {
213148 return clipCount;
214149 }
215150
216- html.Element ? _reconstructClipViewsChain (
151+ html.Element _reconstructClipViewsChain (
217152 int numClips,
218153 html.Element platformView,
219154 html.Element headClipView,
@@ -386,8 +321,6 @@ class HtmlViewEmbedder {
386321 }
387322
388323 void submitFrame () {
389- disposeViews ();
390-
391324 for (int i = 0 ; i < _compositionOrder.length; i++ ) {
392325 int viewId = _compositionOrder[i];
393326 _ensureOverlayInitialized (viewId);
@@ -412,28 +345,26 @@ class HtmlViewEmbedder {
412345 int viewId = _compositionOrder[i];
413346
414347 if (assertionsEnabled) {
415- if (! _views. containsKey (viewId)) {
348+ if (! platformViewManager. knowsViewId (viewId)) {
416349 debugInvalidViewIds ?? = < int > [];
417350 debugInvalidViewIds.add (viewId);
418351 continue ;
419352 }
420353 }
421354
422355 unusedViews.remove (viewId);
423- html.Element platformViewRoot = _rootViews [viewId]! ;
356+ html.Element platformViewRoot = _viewClipChains [viewId]! .root ;
424357 html.Element overlay = _overlays[viewId]! .htmlElement;
425358 platformViewRoot.remove ();
426359 skiaSceneHost! .append (platformViewRoot);
427360 overlay.remove ();
428361 skiaSceneHost! .append (overlay);
429362 _activeCompositionOrder.add (viewId);
430363 }
364+
431365 _compositionOrder.clear ();
432366
433- for (final int unusedViewId in unusedViews) {
434- _releaseOverlay (unusedViewId);
435- _rootViews[unusedViewId]? .remove ();
436- }
367+ disposeViews (unusedViews);
437368
438369 if (assertionsEnabled) {
439370 if (debugInvalidViewIds != null && debugInvalidViewIds.isNotEmpty) {
@@ -445,24 +376,18 @@ class HtmlViewEmbedder {
445376 }
446377 }
447378
448- void disposeViews () {
449- if (_viewsToDispose.isEmpty) {
450- return ;
451- }
452-
453- for (final int viewId in _viewsToDispose) {
454- final html.Element rootView = _rootViews[viewId]! ;
455- rootView.remove ();
456- _views.remove (viewId);
457- _rootViews.remove (viewId);
379+ void disposeViews (Set <int > viewsToDispose) {
380+ for (final int viewId in viewsToDispose) {
381+ // Remove viewId from the _viewClipChains Map, and then from the DOM.
382+ ViewClipChain clipChain = _viewClipChains.remove (viewId)! ;
383+ clipChain.root.remove ();
384+ // More cleanup
458385 _releaseOverlay (viewId);
459386 _currentCompositionParams.remove (viewId);
460- _clipCount.remove (viewId);
461387 _viewsToRecomposite.remove (viewId);
462388 _cleanUpClipDefs (viewId);
463389 _svgClipDefs.remove (viewId);
464390 }
465- _viewsToDispose.clear ();
466391 }
467392
468393 void _releaseOverlay (int viewId) {
@@ -499,6 +424,29 @@ class HtmlViewEmbedder {
499424 }
500425}
501426
427+ /// Represents a Clip Chain (for a view).
428+ ///
429+ /// Objects of this class contain:
430+ /// * The root view in the stack of mutator elements for the view id.
431+ /// * The slot view in the stack (the actual contents of the platform view).
432+ /// * The number of clipping elements used last time the view was composited.
433+ class ViewClipChain {
434+ html.Element _root;
435+ html.Element _slot;
436+ int _clipCount = - 1 ;
437+
438+ ViewClipChain ({required html.Element view}) : this ._root = view, this ._slot = view;
439+
440+ html.Element get root => _root;
441+ html.Element get slot => _slot;
442+ int get clipCount => _clipCount;
443+
444+ void updateClipChain ({required html.Element root, required int clipCount}) {
445+ _root = root;
446+ _clipCount = clipCount;
447+ }
448+ }
449+
502450/// Caches surfaces used to overlay platform views.
503451class OverlayCache {
504452 static const int kDefaultCacheSize = 5 ;
0 commit comments