Skip to content

Commit

Permalink
[web] ui.platformViewRegistry => ui_web.platformViewRegistry (#127493)
Browse files Browse the repository at this point in the history
Now that `platformViewRegistry` is [exposed](flutter/engine#41877) through `dart:ui_web`, we can do some cleanup here.

Part of #126831
  • Loading branch information
mdebbar committed May 24, 2023
1 parent ef54879 commit 2d142c5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 42 deletions.
3 changes: 1 addition & 2 deletions dev/benchmarks/macrobenchmarks/lib/src/draw_points.dart
Expand Up @@ -3,11 +3,10 @@
// found in the LICENSE file.

import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/material.dart';

import 'web/platform_views/web.dart';

class DrawPointsPage extends StatefulWidget {
const DrawPointsPage({super.key});

Expand Down
Expand Up @@ -4,19 +4,16 @@

import 'dart:async';
import 'dart:html' as html;
import 'dart:ui_web' as ui_web;

import 'package:flutter/material.dart';

// TODO(mdebbar): flutter/flutter#55000 Remove this conditional import once
// web-only dart:ui_web APIs are exposed from a dedicated place.
import 'platform_views/non_web.dart'
if (dart.library.html) 'platform_views/web.dart';
import 'recorder.dart';

const String benchmarkViewType = 'benchmark_element';

void _registerFactory() {
platformViewRegistry.registerViewFactory(benchmarkViewType, (int viewId) {
ui_web.platformViewRegistry.registerViewFactory(benchmarkViewType, (int viewId) {
final html.Element htmlElement = html.DivElement();
htmlElement.id = '${benchmarkViewType}_$viewId';
htmlElement.innerText = 'Google';
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -3,8 +3,7 @@
// found in the LICENSE file.

import 'dart:html' as html;
// platformViewRegistry is exposed in the web version
import 'dart:ui' as ui show platformViewRegistry; // ignore: undefined_shown_name
import 'dart:ui_web' as ui_web;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -37,8 +36,7 @@ void main() {
int viewInstanceCount = 0;

platformViewsRegistry.getNextPlatformViewId();
// ignore: undefined_prefixed_name, avoid_dynamic_calls
ui.platformViewRegistry.registerViewFactory('MyView', (int viewId) {
ui_web.platformViewRegistry.registerViewFactory('MyView', (int viewId) {
viewInstanceCount += 1;
return html.DivElement();
});
Expand Down
Expand Up @@ -9,6 +9,10 @@
import 'framework.dart';
import 'selection_container.dart';

/// Function signature for `ui_web.platformViewRegistry.registerViewFactory`.
@visibleForTesting
typedef RegisterViewFactory = void Function(String, Object Function(int viewId), {bool isVisible});

/// A widget that provides native selection context menu for its child subtree.
///
/// This widget currently only supports Flutter web. Using this widget in non-web
Expand Down Expand Up @@ -36,6 +40,12 @@ class PlatformSelectableRegionContextMenu extends StatelessWidget {
/// Detaches the `client` from the platform-appropriate selection context menus.
static void detach(SelectionContainerDelegate client) => throw UnimplementedError();

/// Override this to provide a custom implementation of `ui_web.platformViewRegistry.registerViewFactory`.
///
/// This should only be used for testing.
@visibleForTesting
static RegisterViewFactory? debugOverrideRegisterViewFactory;

@override
Widget build(BuildContext context) => throw UnimplementedError();
}
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui;
import 'dart:ui_web' as ui_web;

import 'package:flutter/rendering.dart';

Expand All @@ -29,7 +29,7 @@ const int _kRightClickButton = 2;

typedef _WebSelectionCallBack = void Function(DomHTMLElement, DomMouseEvent);

/// Function signature for `ui.platformViewRegistry.registerViewFactory`.
/// Function signature for `ui_web.platformViewRegistry.registerViewFactory`.
@visibleForTesting
typedef RegisterViewFactory = void Function(String, Object Function(int viewId), {bool isVisible});

Expand Down Expand Up @@ -67,10 +67,15 @@ class PlatformSelectableRegionContextMenu extends StatelessWidget {
// Keeps track if this widget has already registered its view factories or not.
static String? _registeredViewType;

/// See `_platform_selectable_region_context_menu_io.dart`.
static RegisterViewFactory get _registerViewFactory =>
debugOverrideRegisterViewFactory ?? ui_web.platformViewRegistry.registerViewFactory;

/// Override this to provide a custom implementation of [ui_web.platformViewRegistry.registerViewFactory].
///
/// This should only be used for testing.
// See `_platform_selectable_region_context_menu_io.dart`.
@visibleForTesting
// ignore: undefined_prefixed_name, invalid_assignment, avoid_dynamic_calls
static RegisterViewFactory registerViewFactory = ui.platformViewRegistry.registerViewFactory;
static RegisterViewFactory? debugOverrideRegisterViewFactory;

// Registers the view factories for the interceptor widgets.
static void _register() {
Expand Down Expand Up @@ -100,7 +105,7 @@ class PlatformSelectableRegionContextMenu extends StatelessWidget {
}

static String _registerWebSelectionCallback(_WebSelectionCallBack callback) {
registerViewFactory(_viewType, (int viewId) {
_registerViewFactory(_viewType, (int viewId) {
final DomHTMLElement htmlElement = createDomHTMLDivElement();
htmlElement
..style.width = '100%'
Expand Down
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: undefined_class, undefined_getter, undefined_setter

@TestOn('browser') // This file contains web-only library.
library;

Expand All @@ -16,16 +14,15 @@ import 'package:flutter_test/flutter_test.dart';

void main() {
html.Element? element;
final RegisterViewFactory originalFactory = PlatformSelectableRegionContextMenu.registerViewFactory;
PlatformSelectableRegionContextMenu.registerViewFactory = (String viewType, Object Function(int viewId) fn, {bool isVisible = true}) {
PlatformSelectableRegionContextMenu.debugOverrideRegisterViewFactory = (String viewType, Object Function(int viewId) fn, {bool isVisible = true}) {
element = fn(0) as html.Element;
// The element needs to be attached to the document body to receive mouse
// events.
html.document.body!.append(element!);
};
// This force register the dom element.
PlatformSelectableRegionContextMenu(child: const Placeholder());
PlatformSelectableRegionContextMenu.registerViewFactory = originalFactory;
PlatformSelectableRegionContextMenu.debugOverrideRegisterViewFactory = null;

test('DOM element is set up correctly', () async {
expect(element, isNotNull);
Expand Down

0 comments on commit 2d142c5

Please sign in to comment.