Skip to content

Commit 59e21ff

Browse files
authored
add support for JS types (flutter#40310)
1 parent 476985a commit 59e21ff

File tree

10 files changed

+1803
-685
lines changed

10 files changed

+1803
-685
lines changed

lib/web_ui/lib/src/engine/dom.dart

Lines changed: 1760 additions & 647 deletions
Large diffs are not rendered by default.

lib/web_ui/lib/src/engine/font_change_util.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:typed_data';
77

88
import 'dom.dart';
99
import 'platform_dispatcher.dart';
10-
import 'safe_browser_api.dart';
1110
import 'services.dart';
1211

1312
final ByteData? _fontChangeMessage =
@@ -23,13 +22,13 @@ FutureOr<void> sendFontChangeMessage() async {
2322
if (!_fontChangeScheduled) {
2423
_fontChangeScheduled = true;
2524
// Batch updates into next animationframe.
26-
domWindow.requestAnimationFrame(allowInterop((num _) {
25+
domWindow.requestAnimationFrame((_) {
2726
_fontChangeScheduled = false;
2827
EnginePlatformDispatcher.instance.invokeOnPlatformMessage(
2928
'flutter/system',
3029
_fontChangeMessage,
3130
(_) {},
3231
);
33-
}));
32+
});
3433
}
3534
}

lib/web_ui/lib/src/engine/initialization.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:developer' as developer;
7+
import 'dart:js_interop';
78

89
import 'package:ui/src/engine/assets.dart';
910
import 'package:ui/src/engine/browser_detection.dart';
@@ -174,7 +175,7 @@ Future<void> initializeEngineServices({
174175
// fires.
175176
if (!waitingForAnimation) {
176177
waitingForAnimation = true;
177-
domWindow.requestAnimationFrame(allowInterop((num highResTime) {
178+
domWindow.requestAnimationFrame((JSNumber highResTime) {
178179
frameTimingsOnVsync();
179180

180181
// Reset immediately, because `frameHandler` can schedule more frames.
@@ -185,7 +186,7 @@ Future<void> initializeEngineServices({
185186
// milliseconds as a double value, with sub-millisecond information
186187
// hidden in the fraction. So we first multiply it by 1000 to uncover
187188
// microsecond precision, and only then convert to `int`.
188-
final int highResTimeMicroseconds = (1000 * highResTime).toInt();
189+
final int highResTimeMicroseconds = (1000 * highResTime.toDart).toInt();
189190

190191
// In Flutter terminology "building a frame" consists of "beginning
191192
// frame" and "drawing frame".
@@ -206,7 +207,7 @@ Future<void> initializeEngineServices({
206207
// implement it properly.
207208
EnginePlatformDispatcher.instance.invokeOnDrawFrame();
208209
}
209-
}));
210+
});
210211
}
211212
};
212213

lib/web_ui/lib/src/engine/platform_dispatcher.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:js_interop';
78
import 'dart:typed_data';
89

910
import 'package:ui/src/engine/canvaskit/renderer.dart';
@@ -934,17 +935,17 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
934935
void _addFontSizeObserver() {
935936
const String styleAttribute = 'style';
936937

937-
_fontSizeObserver = createDomMutationObserver(allowInterop(
938-
(List<dynamic> mutations, DomMutationObserver _) {
939-
for (final dynamic mutation in mutations) {
940-
final DomMutationRecord record = mutation as DomMutationRecord;
938+
_fontSizeObserver = createDomMutationObserver(
939+
(JSArray mutations, DomMutationObserver _) {
940+
for (final JSAny? mutation in mutations.toDart) {
941+
final DomMutationRecord record = mutation! as DomMutationRecord;
941942
if (record.type == 'attributes' &&
942943
record.attributeName == styleAttribute) {
943944
final double newTextScaleFactor = findBrowserTextScaleFactor();
944945
_updateTextScaleFactor(newTextScaleFactor);
945946
}
946947
}
947-
}));
948+
});
948949
_fontSizeObserver!.observe(
949950
domDocument.documentElement!,
950951
attributes: true,

lib/web_ui/test/engine/surface/scene_builder_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@TestOn('chrome || firefox')
66

77
import 'dart:async';
8+
import 'dart:js_interop';
89
import 'dart:js_util' as js_util;
910

1011
import 'package:test/bootstrap/browser.dart';
@@ -500,12 +501,12 @@ void testMain() {
500501

501502
// Watches DOM mutations and counts deletions and additions to the child
502503
// list of the `<flt-scene>` element.
503-
final DomMutationObserver observer = createDomMutationObserver(allowInterop((List<dynamic> mutations, _) {
504-
for (final DomMutationRecord record in mutations.cast<DomMutationRecord>()) {
504+
final DomMutationObserver observer = createDomMutationObserver((JSArray mutations, _) {
505+
for (final DomMutationRecord record in mutations.toDart.cast<DomMutationRecord>()) {
505506
actualDeletions.addAll(record.removedNodes!);
506507
actualAdditions.addAll(record.addedNodes!);
507508
}
508-
}));
509+
});
509510
observer.observe(
510511
SurfaceSceneBuilder.debugLastFrameScene!.rootElement!, childList: true);
511512

lib/web_ui/test/text/font_collection_test.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ void testMain() {
3434
await fontManager.downloadAllFonts();
3535
fontManager.registerDownloadedFonts();
3636
domDocument.fonts!
37-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
37+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
3838
fontFamilyList.add(f.family!);
39-
}));
39+
});
4040

4141
expect(fontFamilyList.length, equals(1));
4242
expect(fontFamilyList.first, 'Ahem');
@@ -51,9 +51,9 @@ void testMain() {
5151
await fontManager.downloadAllFonts();
5252
fontManager.registerDownloadedFonts();
5353
domDocument.fonts!
54-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
54+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
5555
fontFamilyList.add(f.family!);
56-
}));
56+
});
5757

5858
expect(fontFamilyList.length, equals(1));
5959
expect(fontFamilyList.first, 'Ahem ahem ahem');
@@ -70,9 +70,9 @@ void testMain() {
7070
await fontManager.downloadAllFonts();
7171
fontManager.registerDownloadedFonts();
7272
domDocument.fonts!
73-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
73+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
7474
fontFamilyList.add(f.family!);
75-
}));
75+
});
7676

7777
expect(fontFamilyList.length, equals(1));
7878
expect(fontFamilyList.first, 'AhEm');
@@ -89,9 +89,9 @@ void testMain() {
8989
await fontManager.downloadAllFonts();
9090
fontManager.registerDownloadedFonts();
9191
domDocument.fonts!
92-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
92+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
9393
fontFamilyList.add(f.family!);
94-
}));
94+
});
9595

9696
if (browserEngine != BrowserEngine.firefox) {
9797
expect(fontFamilyList.length, equals(2));
@@ -114,9 +114,9 @@ void testMain() {
114114
await fontManager.downloadAllFonts();
115115
fontManager.registerDownloadedFonts();
116116
domDocument.fonts!
117-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
117+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
118118
fontFamilyList.add(f.family!);
119-
}));
119+
});
120120

121121
if (browserEngine != BrowserEngine.firefox) {
122122
expect(fontFamilyList.length, equals(2));
@@ -139,9 +139,9 @@ void testMain() {
139139
await fontManager.downloadAllFonts();
140140
fontManager.registerDownloadedFonts();
141141
domDocument.fonts!
142-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
142+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
143143
fontFamilyList.add(f.family!);
144-
}));
144+
});
145145

146146
if (browserEngine != BrowserEngine.firefox) {
147147
expect(fontFamilyList.length, equals(2));
@@ -165,9 +165,9 @@ void testMain() {
165165
await fontManager.downloadAllFonts();
166166
fontManager.registerDownloadedFonts();
167167
domDocument.fonts!
168-
.forEach(allowInterop((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
168+
.forEach((DomFontFace f, DomFontFace f2, DomFontFaceSet s) {
169169
fontFamilyList.add(f.family!);
170-
}));
170+
});
171171

172172
if (browserEngine != BrowserEngine.firefox) {
173173
expect(fontFamilyList.length, equals(2));

lib/web_ui/test/text/font_loading_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Future<void> testMain() async {
8181
final ByteBuffer response = await httpFetchByteBuffer(testFontUrl);
8282
await ui.loadFontFromList(response.asUint8List(), fontFamily: 'Blehm');
8383
final Completer<void> completer = Completer<void>();
84-
domWindow.requestAnimationFrame(allowInterop((_) { completer.complete();}) );
84+
domWindow.requestAnimationFrame((_) { completer.complete();});
8585
await completer.future;
8686
window.onPlatformMessage = oldHandler;
8787
expect(actualName, 'flutter/system');
@@ -94,11 +94,11 @@ Future<void> testMain() async {
9494

9595
bool _containsFontFamily(String family) {
9696
bool found = false;
97-
domDocument.fonts!.forEach(allowInterop((DomFontFace fontFace,
97+
domDocument.fonts!.forEach((DomFontFace fontFace,
9898
DomFontFace fontFaceAgain, DomFontFaceSet fontFaceSet) {
9999
if (fontFace.family == family) {
100100
found = true;
101101
}
102-
}));
102+
});
103103
return found;
104104
}

web_sdk/sdk_rewriter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import 'dart:convert' hide Codec;
4646
import 'dart:developer' as developer;
4747
import 'dart:js_util' as js_util;
4848
import 'dart:_js_annotations';
49+
import 'dart:js_interop' hide JS;
4950
import 'dart:math' as math;
5051
import 'dart:typed_data';
5152
import 'dart:ui' as ui;

web_sdk/test/sdk_rewriter_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import 'dart:convert' hide Codec;
3232
import 'dart:developer' as developer;
3333
import 'dart:js_util' as js_util;
3434
import 'dart:_js_annotations';
35+
import 'dart:js_interop' hide JS;
3536
import 'dart:math' as math;
3637
import 'dart:typed_data';
3738
import 'dart:ui' as ui;

web_sdk/web_engine_tester/lib/static/host.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ library test.host;
99

1010
import 'dart:async';
1111
import 'dart:convert';
12+
import 'dart:js_interop';
1213

1314
import 'package:js/js.dart';
1415
import 'package:stack_trace/stack_trace.dart';
@@ -22,7 +23,7 @@ import 'package:ui/src/engine/dom.dart';
2223
class _TestRunner {}
2324

2425
extension _TestRunnerExtension on _TestRunner {
25-
external void waitUntilDone();
26+
external JSVoid waitUntilDone();
2627
}
2728

2829
/// Returns the current content shell runner, or `null` if none exists.
@@ -37,19 +38,19 @@ external _TestRunner? get testRunner; // ignore: library_private_types_in_public
3738
@anonymous
3839
@staticInterop
3940
class _JSApi {
40-
external factory _JSApi({void Function() resume, void Function() restartCurrent});
41+
external factory _JSApi({JSFunction resume, JSFunction restartCurrent});
4142
}
4243

4344
extension _JSApiExtension on _JSApi {
4445
/// Causes the test runner to resume running, as though the user had clicked
4546
/// the "play" button.
4647
// ignore: unused_element
47-
external Function get resume;
48+
external JSFunction get resume;
4849

4950
/// Causes the test runner to restart the current test once it finishes
5051
/// running.
5152
// ignore: unused_element
52-
external Function get restartCurrent;
53+
external JSFunction get restartCurrent;
5354
}
5455

5556
/// Sets the top-level `dartTest` object so that it's visible to JS.
@@ -159,15 +160,15 @@ void main() {
159160
Timer.periodic(const Duration(seconds: 1),
160161
(_) => serverChannel.sink.add(<String, String>{'command': 'ping'}));
161162

162-
_jsApi = _JSApi(resume: allowInterop(() {
163+
_jsApi = _JSApi(resume: () {
163164
if (!domDocument.body!.classList.contains('paused')) {
164165
return;
165166
}
166167
domDocument.body!.classList.remove('paused');
167168
serverChannel.sink.add(<String, String>{'command': 'resume'});
168-
}), restartCurrent: allowInterop(() {
169+
}.toJS, restartCurrent: () {
169170
serverChannel.sink.add(<String, String>{'command': 'restart'});
170-
}));
171+
}.toJS);
171172
},
172173
(dynamic error, StackTrace stackTrace) {
173174
print('$error\n${Trace.from(stackTrace).terse}'); // ignore: avoid_print

0 commit comments

Comments
 (0)