Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 212aa26

Browse files
committed
migrate svg
1 parent b433678 commit 212aa26

File tree

10 files changed

+148
-55
lines changed

10 files changed

+148
-55
lines changed

lib/web_ui/lib/src/engine/canvaskit/image_web_codecs.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
import 'dart:async';
1212
import 'dart:convert' show base64;
13+
import 'dart:js_interop';
1314
import 'dart:math' as math;
1415
import 'dart:typed_data';
1516

@@ -147,18 +148,18 @@ class CkBrowserImageDecoder implements ui.Codec {
147148
_cacheExpirationClock.callback = null;
148149
try {
149150
final ImageDecoder webDecoder = ImageDecoder(ImageDecoderOptions(
150-
type: contentType,
151-
data: data,
151+
type: contentType.toJS,
152+
data: data.toJS,
152153

153154
// Flutter always uses premultiplied alpha when decoding.
154-
premultiplyAlpha: 'premultiply',
155+
premultiplyAlpha: 'premultiply'.toJS,
155156
// "default" gives the browser the liberty to convert to display-appropriate
156157
// color space, typically SRGB, which is what we want.
157-
colorSpaceConversion: 'default',
158+
colorSpaceConversion: 'default'.toJS,
158159

159160
// Flutter doesn't give the developer a way to customize this, so if this
160161
// is an animated image we should prefer the animated track.
161-
preferAnimation: true,
162+
preferAnimation: true.toJS,
162163
));
163164

164165
await promiseToFuture<void>(webDecoder.tracks.ready);
@@ -217,7 +218,7 @@ class CkBrowserImageDecoder implements ui.Codec {
217218
_debugCheckNotDisposed();
218219
final ImageDecoder webDecoder = await _getOrCreateWebDecoder();
219220
final DecodeResult result = await promiseToFuture<DecodeResult>(
220-
webDecoder.decode(DecodeOptions(frameIndex: _nextFrameIndex.toDouble())),
221+
webDecoder.decode(DecodeOptions(frameIndex: _nextFrameIndex.toJS)),
221222
);
222223
final VideoFrame frame = result.image;
223224
_nextFrameIndex = (_nextFrameIndex + 1) % frameCount;

lib/web_ui/lib/src/engine/configuration.dart

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
@JS()
4545
library configuration;
4646

47+
import 'dart:js_interop';
4748
import 'package:js/js.dart';
4849
import 'package:meta/meta.dart';
4950
import 'canvaskit/renderer.dart';
@@ -266,16 +267,35 @@ external JsFlutterConfiguration? get _jsConfiguration;
266267
class JsFlutterConfiguration {}
267268

268269
extension JsFlutterConfigurationExtension on JsFlutterConfiguration {
269-
external String? get canvasKitBaseUrl;
270-
external String? get canvasKitVariant;
271-
external bool? get canvasKitForceCpuOnly;
272-
external double? get canvasKitMaximumSurfaces;
273-
external bool? get debugShowSemanticsNodes;
270+
@JS('canvasKitBaseUrl')
271+
external JSString? get _canvasKitBaseUrl;
272+
String? get canvasKitBaseUrl => _canvasKitBaseUrl?.toDart;
273+
274+
@JS('canvasKitVariant')
275+
external JSString? get _canvasKitVariant;
276+
String? get canvasKitVariant => _canvasKitVariant?.toDart;
277+
278+
@JS('canvasKitForceCpuOnly')
279+
external JSBoolean? get _canvasKitForceCpuOnly;
280+
bool? get canvasKitForceCpuOnly => _canvasKitForceCpuOnly?.toDart;
281+
282+
@JS('canvasKitMaximumSurfaces')
283+
external JSNumber? get _canvasKitMaximumSurfaces;
284+
double? get canvasKitMaximumSurfaces => _canvasKitMaximumSurfaces?.toDart;
285+
286+
@JS('debugShowSemanticsNodes')
287+
external JSBoolean? get _debugShowSemanticsNodes;
288+
bool? get debugShowSemanticsNodes => _debugShowSemanticsNodes?.toDart;
289+
274290
external DomElement? get hostElement;
275-
external String? get renderer;
291+
292+
@JS('renderer')
293+
external JSString? get _renderer;
294+
String? get renderer => _renderer?.toDart;
276295
}
277296

278297
/// A JavaScript entrypoint that allows developer to set rendering backend
279298
/// at runtime before launching the application.
280299
@JS('window.flutterWebRenderer')
281-
external String? get _requestedRendererType;
300+
external JSString? get __requestedRendererType;
301+
String? get _requestedRendererType => __requestedRendererType?.toDart;

lib/web_ui/lib/src/engine/profiler.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:js_interop';
67

78
import 'package:js/js.dart';
89
import 'package:ui/ui.dart' as ui;
@@ -12,7 +13,8 @@ import 'platform_dispatcher.dart';
1213
import 'safe_browser_api.dart';
1314

1415
@JS('window._flutter_internal_on_benchmark')
15-
external Object? get onBenchmark;
16+
external JSAny? get _onBenchmark;
17+
Object? get onBenchmark => _onBenchmark?.toObjectShallow;
1618

1719
/// A function that computes a value of type [R].
1820
///

lib/web_ui/lib/src/engine/safe_browser_api.dart

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
library browser_api;
1313

1414
import 'dart:async';
15+
import 'dart:js_interop';
1516
import 'dart:js_util' as js_util;
1617
import 'dart:math' as math;
1718
import 'dart:typed_data';
@@ -201,7 +202,9 @@ DomCanvasElement? tryCreateCanvasElement(int width, int height) {
201202
}
202203

203204
@JS('window.ImageDecoder')
204-
external Object? get _imageDecoderConstructor;
205+
external JSAny? get __imageDecoderConstructor;
206+
Object? get _imageDecoderConstructor =>
207+
__imageDecoderConstructor?.toObjectShallow;
205208

206209
/// Environment variable that allows the developer to opt out of using browser's
207210
/// `ImageDecoder` API, and use the WASM codecs bundled with CanvasKit.
@@ -265,9 +268,13 @@ class ImageDecoder {
265268

266269
extension ImageDecoderExtension on ImageDecoder {
267270
external ImageTrackList get tracks;
268-
external bool get complete;
271+
272+
@JS('complete')
273+
external JSBoolean get _complete;
274+
bool get complete => _complete.toDart;
275+
269276
external JsPromise decode(DecodeOptions options);
270-
external void close();
277+
external JSVoid close();
271278
}
272279

273280
/// Options passed to the `ImageDecoder` constructor.
@@ -280,13 +287,13 @@ extension ImageDecoderExtension on ImageDecoder {
280287
@staticInterop
281288
class ImageDecoderOptions {
282289
external factory ImageDecoderOptions({
283-
required String type,
284-
required Uint8List data,
285-
required String premultiplyAlpha,
286-
int? desiredWidth,
287-
int? desiredHeight,
288-
required String colorSpaceConversion,
289-
required bool preferAnimation,
290+
required JSString type,
291+
required JSUint8Array data,
292+
required JSString premultiplyAlpha,
293+
JSNumber? desiredWidth,
294+
JSNumber? desiredHeight,
295+
required JSString colorSpaceConversion,
296+
required JSBoolean preferAnimation,
290297
});
291298
}
292299

@@ -302,7 +309,10 @@ class DecodeResult {}
302309

303310
extension DecodeResultExtension on DecodeResult {
304311
external VideoFrame get image;
305-
external bool get complete;
312+
313+
@JS('complete')
314+
external JSBoolean get _complete;
315+
bool get complete => _complete.toDart;
306316
}
307317

308318
/// Options passed to [ImageDecoder.decode].
@@ -315,7 +325,7 @@ extension DecodeResultExtension on DecodeResult {
315325
@staticInterop
316326
class DecodeOptions {
317327
external factory DecodeOptions({
318-
required double frameIndex,
328+
required JSNumber frameIndex,
319329
});
320330
}
321331

@@ -332,16 +342,40 @@ class DecodeOptions {
332342
class VideoFrame implements DomCanvasImageSource {}
333343

334344
extension VideoFrameExtension on VideoFrame {
335-
external double allocationSize();
336-
external JsPromise copyTo(Object destination);
337-
external String? get format;
338-
external double get codedWidth;
339-
external double get codedHeight;
340-
external double get displayWidth;
341-
external double get displayHeight;
342-
external double? get duration;
345+
@JS('allocationSize')
346+
external JSNumber _allocationSize();
347+
double allocationSize() => _allocationSize().toDart;
348+
349+
@JS('copyTo')
350+
external JsPromise _copyTo(JSAny destination);
351+
JsPromise copyTo(Object destination) => _copyTo(destination.toJSAnyShallow);
352+
353+
@JS('format')
354+
external JSString? get _format;
355+
String? get format => _format?.toDart;
356+
357+
@JS('codedWidth')
358+
external JSNumber get _codedWidth;
359+
double get codedWidth => _codedWidth.toDart;
360+
361+
@JS('codedHeight')
362+
external JSNumber get _codedHeight;
363+
double get codedHeight => _codedHeight.toDart;
364+
365+
@JS('displayWidth')
366+
external JSNumber get _displayWidth;
367+
double get displayWidth => _displayWidth.toDart;
368+
369+
@JS('displayHeight')
370+
external JSNumber get _displayHeight;
371+
double get displayHeight => _displayHeight.toDart;
372+
373+
@JS('duration')
374+
external JSNumber? get _duration;
375+
double? get duration => _duration?.toDart;
376+
343377
external VideoFrame clone();
344-
external void close();
378+
external JSVoid close();
345379
}
346380

347381
/// Corresponds to the browser's `ImageTrackList` type.
@@ -370,8 +404,13 @@ extension ImageTrackListExtension on ImageTrackList {
370404
class ImageTrack {}
371405

372406
extension ImageTrackExtension on ImageTrack {
373-
external double get repetitionCount;
374-
external double get frameCount;
407+
@JS('repetitionCount')
408+
external JSNumber get _repetitionCount;
409+
double get repetitionCount => _repetitionCount.toDart;
410+
411+
@JS('frameCount')
412+
external JSNumber get _frameCount;
413+
double get frameCount => _frameCount.toDart;
375414
}
376415

377416
void scaleCanvas2D(Object context2d, num x, num y) {

lib/web_ui/lib/src/engine/svg.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:js_interop';
56
import 'package:js/js.dart';
67

78
import 'dom.dart';
@@ -91,8 +92,14 @@ extension SVGAnimatedLengthExtension on SVGAnimatedLength {
9192
class SVGLength {}
9293

9394
extension SVGLengthExtension on SVGLength {
94-
external set valueAsString(String? value);
95-
external void newValueSpecifiedUnits(int unitType, num valueInSpecifiedUnits);
95+
@JS('valueAsString')
96+
external set _valueAsString(JSString? value);
97+
set valueAsString(String? value) => _valueAsString = value?.toJS;
98+
99+
@JS('newValueSpecifiedUnits')
100+
external JSVoid _newValueSpecifiedUnits(JSNumber unitType, JSNumber valueInSpecifiedUnits);
101+
void newValueSpecifiedUnits(int unitType, num valueInSpecifiedUnits) =>
102+
_newValueSpecifiedUnits(unitType.toJS, valueInSpecifiedUnits.toJS);
96103
}
97104

98105
const int svgLengthTypeNumber = 1;
@@ -102,7 +109,9 @@ const int svgLengthTypeNumber = 1;
102109
class SVGAnimatedEnumeration {}
103110

104111
extension SVGAnimatedEnumerationExtenson on SVGAnimatedEnumeration {
105-
external set baseVal(int? value);
112+
@JS('baseVal')
113+
external set _baseVal(JSNumber? value);
114+
set baseVal(int? value) => _baseVal = value?.toJS;
106115
}
107116

108117
@JS()
@@ -186,15 +195,19 @@ extension SVGFEBlendCompositeExtension on SVGFECompositeElement {
186195
class SVGAnimatedString {}
187196

188197
extension SVGAnimatedStringExtension on SVGAnimatedString {
189-
external set baseVal(String? value);
198+
@JS('baseVal')
199+
external set _baseVal(JSString? value);
200+
set baseVal(String? value) => _baseVal = value?.toJS;
190201
}
191202

192203
@JS()
193204
@staticInterop
194205
class SVGAnimatedNumber {}
195206

196207
extension SVGAnimatedNumberExtension on SVGAnimatedNumber {
197-
external set baseVal(num? value);
208+
@JS('baseVal')
209+
external set _baseVal(JSNumber? value);
210+
set baseVal(num? value) => _baseVal = value?.toJS;
198211
}
199212

200213
@JS()
@@ -218,5 +231,7 @@ extension SVGNumberListExtension on SVGNumberList {
218231
class SVGNumber {}
219232

220233
extension SVGNumberExtension on SVGNumber {
221-
external set value(num? value);
234+
@JS('value')
235+
external set _value(JSNumber? value);
236+
set value(num? v) => _value = v?.toJS;
222237
}

lib/web_ui/lib/src/engine/view_embedder/hot_restart_cache_handler.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:js_interop';
6+
57
import 'package:js/js.dart';
68
import 'package:ui/src/engine.dart';
79

@@ -11,10 +13,14 @@ import '../dom.dart';
1113
/// to clear. Delay removal of old visible state to make the
1214
/// transition appear smooth.
1315
@JS('window.__flutterState')
14-
external List<Object?>? get hotRestartStore;
16+
external JSArray? get _hotRestartStore;
17+
List<Object?>? get hotRestartStore =>
18+
_hotRestartStore?.toObjectShallow as List<Object>?;
1519

1620
@JS('window.__flutterState')
17-
external set hotRestartStore(List<Object?>? nodes);
21+
external set _hotRestartStore(JSArray? nodes);
22+
set hotRestartStore(List<Object?>? nodes) =>
23+
_hotRestartStore = nodes?.toJSAnyShallow as JSArray?;
1824

1925
/// Handles [DomElement]s that need to be removed after a hot-restart.
2026
///

lib/web_ui/test/canvaskit/skia_objects_cache_test.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:js_interop';
6+
57
import 'package:js/js.dart';
68

79
import 'package:test/bootstrap/browser.dart';
@@ -363,7 +365,8 @@ class TestSkDeletableMock {
363365
deleteCount++;
364366
}
365367

366-
JsConstructor get constructor => TestJsConstructor(name: 'TestSkDeletable');
368+
JsConstructor get constructor => TestJsConstructor(name:
369+
'TestSkDeletable'.toJS);
367370
}
368371

369372
@JS()
@@ -373,22 +376,22 @@ class TestSkDeletable implements SkDeletable {
373376
factory TestSkDeletable() {
374377
final TestSkDeletableMock mock = TestSkDeletableMock();
375378
return TestSkDeletable._(
376-
isDeleted: allowInterop(() => mock.isDeleted()),
377-
delete: allowInterop(() => mock.delete()),
379+
isDeleted: () { return mock.isDeleted(); }.toJS,
380+
delete: () { return mock.delete(); }.toJS,
378381
constructor: mock.constructor);
379382
}
380383

381384
external factory TestSkDeletable._({
382-
bool Function() isDeleted,
383-
void Function() delete,
385+
JSFunction isDeleted,
386+
JSFunction delete,
384387
JsConstructor constructor});
385388
}
386389

387390
@JS()
388391
@anonymous
389392
@staticInterop
390393
class TestJsConstructor implements JsConstructor {
391-
external factory TestJsConstructor({String name});
394+
external factory TestJsConstructor({JSString name});
392395
}
393396

394397
class TestSkiaObject extends ManagedSkiaObject<SkPaint> {

0 commit comments

Comments
 (0)