[web] Make PlatformViewManager a clear singleton (flutter/engine#46044)
Make it clear that `PlatformViewManager` is a singleton and follow the patterns that we use for other singleton classes. Part of https://github.com/flutter/flutter/issues/134443
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../../engine.dart' show platformViewManager;
|
||||
import '../../engine.dart' show PlatformViewManager;
|
||||
import '../configuration.dart';
|
||||
import '../dom.dart';
|
||||
import '../html/path_to_svg_clip.dart';
|
||||
@@ -136,7 +136,7 @@ class HtmlViewEmbedder {
|
||||
}
|
||||
// We need an overlay for each visible platform view. Invisible platform
|
||||
// views will be grouped with (at most) one visible platform view later.
|
||||
final bool needNewOverlay = platformViewManager.isVisible(viewId);
|
||||
final bool needNewOverlay = PlatformViewManager.instance.isVisible(viewId);
|
||||
if (needNewOverlay && hasAvailableOverlay) {
|
||||
final CkPictureRecorder pictureRecorder = CkPictureRecorder();
|
||||
pictureRecorder.beginRecording(ui.Offset.zero & _frameSize);
|
||||
@@ -164,11 +164,11 @@ class HtmlViewEmbedder {
|
||||
final int overlayIndex = _context.visibleViewCount;
|
||||
_compositionOrder.add(viewId);
|
||||
// Keep track of the number of visible platform views.
|
||||
if (platformViewManager.isVisible(viewId)) {
|
||||
if (PlatformViewManager.instance.isVisible(viewId)) {
|
||||
_context.visibleViewCount++;
|
||||
}
|
||||
// We need a new overlay if this is a visible view.
|
||||
final bool needNewOverlay = platformViewManager.isVisible(viewId);
|
||||
final bool needNewOverlay = PlatformViewManager.instance.isVisible(viewId);
|
||||
CkPictureRecorder? recorderToUseForRendering;
|
||||
if (needNewOverlay) {
|
||||
if (overlayIndex < _context.pictureRecordersCreatedDuringPreroll.length) {
|
||||
@@ -467,7 +467,7 @@ class HtmlViewEmbedder {
|
||||
for (final int viewId in diffResult.viewsToAdd) {
|
||||
bool isViewInvalid = false;
|
||||
assert(() {
|
||||
isViewInvalid = !platformViewManager.knowsViewId(viewId);
|
||||
isViewInvalid = !PlatformViewManager.instance.knowsViewId(viewId);
|
||||
if (isViewInvalid) {
|
||||
debugInvalidViewIds ??= <int>[];
|
||||
debugInvalidViewIds!.add(viewId);
|
||||
@@ -520,7 +520,7 @@ class HtmlViewEmbedder {
|
||||
|
||||
bool isViewInvalid = false;
|
||||
assert(() {
|
||||
isViewInvalid = !platformViewManager.knowsViewId(viewId);
|
||||
isViewInvalid = !PlatformViewManager.instance.knowsViewId(viewId);
|
||||
if (isViewInvalid) {
|
||||
debugInvalidViewIds ??= <int>[];
|
||||
debugInvalidViewIds!.add(viewId);
|
||||
@@ -655,7 +655,7 @@ class HtmlViewEmbedder {
|
||||
|
||||
for (int i = 0; i < views.length; i++) {
|
||||
final int view = views[i];
|
||||
if (platformViewManager.isInvisible(view)) {
|
||||
if (PlatformViewManager.instance.isInvisible(view)) {
|
||||
// We add as many invisible views as we find to the current group.
|
||||
currentGroup.add(view);
|
||||
} else {
|
||||
@@ -720,7 +720,7 @@ class HtmlViewEmbedder {
|
||||
|
||||
/// Clears the state of this view embedder. Used in tests.
|
||||
void debugClear() {
|
||||
final Set<int> allViews = platformViewManager.debugClear();
|
||||
final Set<int> allViews = PlatformViewManager.instance.debugClear();
|
||||
disposeViews(allViews);
|
||||
_context = EmbedderFrameContext();
|
||||
_currentCompositionParams.clear();
|
||||
|
||||
@@ -270,8 +270,3 @@ set debugDisableFontFallbacks(bool value) {
|
||||
_debugDisableFontFallbacks = value;
|
||||
}
|
||||
bool _debugDisableFontFallbacks = false;
|
||||
|
||||
/// The shared instance of PlatformViewManager shared across the engine to handle
|
||||
/// rendering of PlatformViews into the web app.
|
||||
// TODO(dit): How to make this overridable from tests?
|
||||
final PlatformViewManager platformViewManager = PlatformViewManager();
|
||||
|
||||
@@ -596,7 +596,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
|
||||
case 'flutter/platform_views':
|
||||
_platformViewMessageHandler ??= PlatformViewMessageHandler(
|
||||
contentManager: platformViewManager,
|
||||
contentManager: PlatformViewManager.instance,
|
||||
contentHandler: (DomElement content) {
|
||||
flutterViewEmbedder.glassPaneElement.append(content);
|
||||
},
|
||||
|
||||
@@ -38,6 +38,11 @@ class PlatformViewManager {
|
||||
);
|
||||
}
|
||||
|
||||
/// The shared instance of PlatformViewManager shared across the engine to handle
|
||||
/// rendering of PlatformViews into the web app.
|
||||
// TODO(dit): How to make this overridable from tests?
|
||||
static final PlatformViewManager instance = PlatformViewManager();
|
||||
|
||||
// The factory functions, indexed by the viewType
|
||||
final Map<String, Function> _factories = <String, Function>{};
|
||||
|
||||
@@ -49,7 +54,7 @@ class PlatformViewManager {
|
||||
|
||||
/// Returns `true` if the passed in `viewType` has been registered before.
|
||||
///
|
||||
/// See [registerViewFactory] to understand how factories are registered.
|
||||
/// See [registerFactory] to understand how factories are registered.
|
||||
bool knowsViewType(String viewType) {
|
||||
return _factories.containsKey(viewType);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ class PlatformViewRegistry {
|
||||
Function viewFactory, {
|
||||
bool isVisible = true,
|
||||
}) {
|
||||
return platformViewManager.registerFactory(
|
||||
return PlatformViewManager.instance.registerFactory(
|
||||
viewType,
|
||||
viewFactory,
|
||||
isVisible: isVisible,
|
||||
@@ -53,6 +53,6 @@ class PlatformViewRegistry {
|
||||
///
|
||||
/// Throws if no view has been created for [viewId].
|
||||
Object getViewById(int viewId) {
|
||||
return platformViewManager.getViewById(viewId);
|
||||
return PlatformViewManager.instance.getViewById(viewId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -917,8 +917,8 @@ void testMain() {
|
||||
await createPlatformView(5, 'test-invisible-view');
|
||||
await createPlatformView(6, 'test-invisible-view');
|
||||
|
||||
expect(platformViewManager.isInvisible(0), isFalse);
|
||||
expect(platformViewManager.isInvisible(1), isTrue);
|
||||
expect(PlatformViewManager.instance.isInvisible(0), isFalse);
|
||||
expect(PlatformViewManager.instance.isInvisible(1), isTrue);
|
||||
|
||||
LayerSceneBuilder sb = LayerSceneBuilder();
|
||||
sb.pushOffset(0, 0);
|
||||
|
||||
@@ -38,7 +38,7 @@ Future<void> testMain() async {
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
platformViewManager.debugClear();
|
||||
PlatformViewManager.instance.debugClear();
|
||||
});
|
||||
|
||||
test('picture + overlapping platformView', () async {
|
||||
|
||||
Reference in New Issue
Block a user