[web] Add views proxy and getInitialData. (flutter/engine#49320)
Adds a Dart API so Application/Plugin programmers can retrieve the `initialData` configuration value that may be passed when adding a view from JS in a multiViewEnabled app.
When adding a view to an app like this:
```js
flutterApp.addView({
hostElement: someElement,
initialData: {
randomUUID: globalThis.crypto.randomUUID(),
}
});
```
`initialData` can be accessed from Dart by defining a JS-interop class like:
```dart
import 'dart:js_interop';
// The JS-interop definition of the `initialData` object passed to the views of this app.
@JS()
@staticInterop
class InitialData {}
/// The attributes of the [InitialData] object.
extension InitialDataExtension on InitialData {
external String? get randomUUID;
}
```
And then, from the code of the application:
```dart
...
Widget build(BuildContext context) {
final int viewId = View.of(context).viewId;
final InitialData? data = ui_web.views.getInitialData(viewId) as InitialData?;
return Text('${data?.randomUUID}');
}
...
```
## Testing
Will add unit tests once naming is sorted out :)
[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -6143,6 +6143,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/ui.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/asset_manager.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/benchmarks.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/flutter_views_proxy.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/images.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/initialization.dart + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/platform_location.dart + ../../../flutter/LICENSE
|
||||
@@ -8997,6 +8998,7 @@ FILE: ../../../flutter/lib/web_ui/lib/ui.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/asset_manager.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/benchmarks.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/flutter_views_proxy.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/images.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/initialization.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/platform_location.dart
|
||||
|
||||
@@ -25,9 +25,7 @@ extension JsFlutterViewOptionsExtension on JsFlutterViewOptions {
|
||||
return _hostElement!;
|
||||
}
|
||||
|
||||
@JS('initialData')
|
||||
external JSObject? get _initialData;
|
||||
Object? get initialData => _initialData?.toObjectDeep;
|
||||
external JSAny? get initialData;
|
||||
}
|
||||
|
||||
/// The public JS API of a running Flutter Web App.
|
||||
|
||||
@@ -10,6 +10,7 @@ library ui_web;
|
||||
|
||||
export 'ui_web/asset_manager.dart';
|
||||
export 'ui_web/benchmarks.dart';
|
||||
export 'ui_web/flutter_views_proxy.dart';
|
||||
export 'ui_web/images.dart';
|
||||
export 'ui_web/initialization.dart';
|
||||
export 'ui_web/navigation/platform_location.dart';
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
import 'dart:js_interop';
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
/// Exposes web-only functionality for this app's `FlutterView`s objects.
|
||||
final FlutterViewManagerProxy views = FlutterViewManagerProxy(
|
||||
viewManager: EnginePlatformDispatcher.instance.viewManager,
|
||||
);
|
||||
|
||||
/// This class exposes web-only attributes for the registered views in a multi-view app.
|
||||
class FlutterViewManagerProxy {
|
||||
FlutterViewManagerProxy({
|
||||
required FlutterViewManager viewManager,
|
||||
}) : _viewManager = viewManager;
|
||||
// The proxied viewManager instance.
|
||||
final FlutterViewManager _viewManager;
|
||||
|
||||
/// Returns the `hostElement` configuration value passed from JS when `viewId` was added.
|
||||
///
|
||||
/// This is useful for plugins and apps to have a safe DOM Element where they
|
||||
/// can add their own custom HTML elements (for example: file inputs for the
|
||||
/// file_selector plugin).
|
||||
JSAny? getHostElement(int viewId) {
|
||||
return _viewManager.getOptions(viewId)?.hostElement as JSAny?;
|
||||
}
|
||||
|
||||
/// Returns the `initialData` configuration value passed from JS when `viewId` was added.
|
||||
///
|
||||
/// Developers can access the initial data from Dart in two ways:
|
||||
/// * Defining their own `staticInterop` class that describes what you're
|
||||
/// passing to your views, to retain type safety on Dart (preferred).
|
||||
/// * Calling [NullableUndefineableJSAnyExtension.dartify] and accessing the
|
||||
/// returned object as if it were a [Map] (not recommended).
|
||||
JSAny? getInitialData(int viewId) {
|
||||
return _viewManager.getOptions(viewId)?.initialData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:js_interop';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui_web/src/ui_web.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() => doTests);
|
||||
}
|
||||
|
||||
Future<void> doTests() async {
|
||||
group('FlutterViewManagerProxy', () {
|
||||
final EnginePlatformDispatcher platformDispatcher =
|
||||
EnginePlatformDispatcher.instance;
|
||||
final FlutterViewManager viewManager =
|
||||
FlutterViewManager(platformDispatcher);
|
||||
final FlutterViewManagerProxy views =
|
||||
FlutterViewManagerProxy(viewManager: viewManager);
|
||||
|
||||
late EngineFlutterView view;
|
||||
late int viewId;
|
||||
late DomElement hostElement;
|
||||
|
||||
int registerViewWithOptions(Map<String, Object?> options) {
|
||||
final JsFlutterViewOptions jsOptions =
|
||||
options.toJSAnyDeep as JsFlutterViewOptions;
|
||||
viewManager.registerView(view, jsViewOptions: jsOptions);
|
||||
return viewId;
|
||||
}
|
||||
|
||||
setUp(() {
|
||||
view = EngineFlutterView(platformDispatcher, createDomElement('div'));
|
||||
viewId = view.viewId;
|
||||
hostElement = createDomElement('div');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
viewManager.unregisterView(viewId);
|
||||
});
|
||||
|
||||
group('getHostElement', () {
|
||||
test('null when viewId is unknown', () {
|
||||
final JSAny? element = views.getHostElement(33930);
|
||||
expect(element, isNull);
|
||||
});
|
||||
|
||||
test('can retrieve hostElement for a known view', () {
|
||||
final int viewId = registerViewWithOptions(<String, Object?>{
|
||||
'hostElement': hostElement,
|
||||
});
|
||||
|
||||
final JSAny? element = views.getHostElement(viewId);
|
||||
|
||||
expect(element, hostElement);
|
||||
});
|
||||
});
|
||||
|
||||
group('getInitialData', () {
|
||||
test('null when viewId is unknown', () {
|
||||
final JSAny? element = views.getInitialData(33930);
|
||||
expect(element, isNull);
|
||||
});
|
||||
|
||||
test('can retrieve initialData for a known view', () {
|
||||
final int viewId = registerViewWithOptions(<String, Object?>{
|
||||
'hostElement': hostElement,
|
||||
'initialData': <String, Object?>{
|
||||
'someInt': 42,
|
||||
'someString': 'A String',
|
||||
'decimals': <double>[math.pi, math.e],
|
||||
},
|
||||
});
|
||||
|
||||
final InitialData? element =
|
||||
views.getInitialData(viewId) as InitialData?;
|
||||
|
||||
expect(element, isNotNull);
|
||||
expect(element!.someInt, 42);
|
||||
expect(element.someString, 'A String');
|
||||
expect(element.decimals, <double>[math.pi, math.e]);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// The JS-interop definition of the `initialData` object passed to the views of this app.
|
||||
@JS()
|
||||
@staticInterop
|
||||
class InitialData {}
|
||||
|
||||
/// The attributes of the [InitialData] object.
|
||||
extension InitialDataExtension on InitialData {
|
||||
external int get someInt;
|
||||
external String? get someString;
|
||||
|
||||
@JS('decimals')
|
||||
external JSArray<JSNumber> get _decimals;
|
||||
List<double> get decimals =>
|
||||
_decimals.toDart.map((JSNumber e) => e.toDartDouble).toList();
|
||||
}
|
||||
Reference in New Issue
Block a user