[web] Do not wipe the PlatformViewManager when disposing of a view. (flutter/engine#49991)
When a view gets disposed of, its rasterizer completely clears up the singleton `PlatformViewManager`. Particularly, it removes all registered platform view factories. This is wrong because the remaining PlatformViews on the page cannot be re-rendered, and the default Platform View factories (used by `pointer_interceptor`, for example), disappear. This PR attempts to preserve the `dispose` logic of the canvaskit rasterizer, without using the `debugClear` method of the `PlatformViewManager` (which is supposedly test-only). ## Issues * Fixes https://github.com/flutter/flutter/issues/142094 ## Tests * Added unit-test * Deployed demo app: https://dit-maps-tests.web.app [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -410,8 +410,7 @@ class HtmlViewEmbedder {
|
||||
// are going to be added back. Moving rather than removing and re-adding
|
||||
// the view helps it maintain state.
|
||||
disposeViews(diffResult.viewsToRemove
|
||||
.where((int view) => !diffResult.viewsToAdd.contains(view))
|
||||
.toSet());
|
||||
.where((int view) => !diffResult.viewsToAdd.contains(view)));
|
||||
_activeCompositionOrder.addAll(_compositionOrder);
|
||||
unusedViews.removeAll(_compositionOrder);
|
||||
|
||||
@@ -510,7 +509,7 @@ class HtmlViewEmbedder {
|
||||
);
|
||||
}
|
||||
|
||||
void disposeViews(Set<int> viewsToDispose) {
|
||||
void disposeViews(Iterable<int> viewsToDispose) {
|
||||
for (final int viewId in viewsToDispose) {
|
||||
// Remove viewId from the _viewClipChains Map, and then from the DOM.
|
||||
final ViewClipChain? clipChain = _viewClipChains.remove(viewId);
|
||||
@@ -659,8 +658,7 @@ class HtmlViewEmbedder {
|
||||
|
||||
/// Disposes the state of this view embedder.
|
||||
void dispose() {
|
||||
final Set<int> allViews = PlatformViewManager.instance.debugClear();
|
||||
disposeViews(allViews);
|
||||
disposeViews(_viewClipChains.keys);
|
||||
_context = EmbedderFrameContext();
|
||||
_currentCompositionParams.clear();
|
||||
debugCleanupSvgClipPaths();
|
||||
|
||||
@@ -213,16 +213,12 @@ class PlatformViewManager {
|
||||
bool isVisible(int viewId) => !isInvisible(viewId);
|
||||
|
||||
/// Clears the state. Used in tests.
|
||||
///
|
||||
/// Returns the set of know view ids, so they can be cleaned up.
|
||||
Set<int> debugClear() {
|
||||
final Set<int> result = _contents.keys.toSet();
|
||||
result.forEach(clearPlatformView);
|
||||
void debugClear() {
|
||||
_contents.keys.toList().forEach(clearPlatformView);
|
||||
_factories.clear();
|
||||
_contents.clear();
|
||||
_invisibleViews.clear();
|
||||
_viewIdToType.clear();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
|
||||
|
||||
import '../common/matchers.dart';
|
||||
import 'common.dart';
|
||||
@@ -68,5 +69,28 @@ void testMain() {
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
// Issue https://github.com/flutter/flutter/issues/142094
|
||||
test('does not reset platform view factories when disposing a view', () async {
|
||||
expect(PlatformViewManager.instance.knowsViewType('self-test'), isFalse);
|
||||
|
||||
final EngineFlutterView view = EngineFlutterView(
|
||||
EnginePlatformDispatcher.instance, createDomElement('multi-view'));
|
||||
EnginePlatformDispatcher.instance.viewManager.registerView(view);
|
||||
expect(
|
||||
CanvasKitRenderer.instance.debugGetRasterizerForView(view),
|
||||
isNotNull,
|
||||
);
|
||||
|
||||
EnginePlatformDispatcher.instance.viewManager
|
||||
.disposeAndUnregisterView(view.viewId);
|
||||
expect(
|
||||
CanvasKitRenderer.instance.debugGetRasterizerForView(view),
|
||||
isNull,
|
||||
);
|
||||
|
||||
expect(PlatformViewManager.instance.knowsViewType(ui_web.PlatformViewRegistry.defaultVisibleViewType), isTrue);
|
||||
expect(PlatformViewManager.instance.knowsViewType(ui_web.PlatformViewRegistry.defaultInvisibleViewType), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user