[web] Migrate Flutter Web DOM usage to JS static interop - 25. (flutter/engine#33353)
This commit is contained in:
@@ -479,7 +479,7 @@ Future<Codec> instantiateImageCodec(
|
||||
if (engine.useCanvasKit) {
|
||||
return engine.skiaInstantiateImageCodec(list, targetWidth, targetHeight);
|
||||
} else {
|
||||
final html.Blob blob = html.Blob(<dynamic>[list.buffer]);
|
||||
final engine.DomBlob blob = engine.createDomBlob(<dynamic>[list.buffer]);
|
||||
return engine.HtmlBlobCodec(blob);
|
||||
}
|
||||
}
|
||||
@@ -493,7 +493,7 @@ Future<Codec> instantiateImageCodecFromBuffer(
|
||||
if (engine.useCanvasKit) {
|
||||
return engine.skiaInstantiateImageCodec(buffer._list!, targetWidth, targetHeight);
|
||||
} else {
|
||||
final html.Blob blob = html.Blob(<dynamic>[buffer._list!.buffer]);
|
||||
final engine.DomBlob blob = engine.createDomBlob(<dynamic>[buffer._list!.buffer]);
|
||||
return engine.HtmlBlobCodec(blob);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ extension DomWindowExtension on DomWindow {
|
||||
external DomPerformance get performance;
|
||||
Future<Object?> fetch(String url) =>
|
||||
js_util.promiseToFuture(js_util.callMethod(this, 'fetch', <String>[url]));
|
||||
// ignore: non_constant_identifier_names
|
||||
external DomURL get URL;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -69,8 +71,8 @@ extension DomDocumentExtension on DomDocument {
|
||||
external DomElement? get documentElement;
|
||||
external DomElement? querySelector(String selectors);
|
||||
Iterable<DomElement> querySelectorAll(String selectors) =>
|
||||
_DomElementListWrapper.create(js_util.callMethod<_DomElementList>(
|
||||
this, 'querySelectorAll', <Object>[selectors]));
|
||||
createDomListWrapper<DomElement>(js_util
|
||||
.callMethod<_DomList>(this, 'querySelectorAll', <Object>[selectors]));
|
||||
DomElement createElement(String name, [Object? options]) =>
|
||||
js_util.callMethod(this, 'createElement',
|
||||
<Object>[name, if (options != null) options]) as DomElement;
|
||||
@@ -175,8 +177,8 @@ class DomElement extends DomNode {}
|
||||
DomElement createDomElement(String tag) => domDocument.createElement(tag);
|
||||
|
||||
extension DomElementExtension on DomElement {
|
||||
Iterable<DomElement> get children => _DomElementListWrapper.create(
|
||||
js_util.getProperty<_DomElementList>(this, 'children'));
|
||||
Iterable<DomElement> get children => createDomListWrapper<DomElement>(
|
||||
js_util.getProperty<_DomList>(this, 'children'));
|
||||
external int get clientHeight;
|
||||
external int get clientWidth;
|
||||
external String get id;
|
||||
@@ -192,8 +194,8 @@ extension DomElementExtension on DomElement {
|
||||
external void prepend(DomNode node);
|
||||
external DomElement? querySelector(String selectors);
|
||||
Iterable<DomElement> querySelectorAll(String selectors) =>
|
||||
_DomElementListWrapper.create(js_util.callMethod<_DomElementList>(
|
||||
this, 'querySelectorAll', <Object>[selectors]));
|
||||
createDomListWrapper<DomElement>(js_util
|
||||
.callMethod<_DomList>(this, 'querySelectorAll', <Object>[selectors]));
|
||||
external void remove();
|
||||
external void setAttribute(String name, Object value);
|
||||
void appendText(String text) => append(createDomText(text));
|
||||
@@ -359,15 +361,23 @@ class DomHTMLBodyElement extends DomHTMLElement {}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomHTMLImageElement extends DomHTMLElement {}
|
||||
class DomHTMLImageElement extends DomHTMLElement
|
||||
implements DomCanvasImageSource {}
|
||||
|
||||
DomHTMLImageElement createDomHTMLImageElement() =>
|
||||
domDocument.createElement('img') as DomHTMLImageElement;
|
||||
|
||||
extension DomHTMLImageElemenExtension on DomHTMLImageElement {
|
||||
extension DomHTMLImageElementExtension on DomHTMLImageElement {
|
||||
external String? get alt;
|
||||
external set alt(String? value);
|
||||
external set src(String value);
|
||||
external String? get src;
|
||||
external set src(String? value);
|
||||
external int get naturalWidth;
|
||||
external int get naturalHeight;
|
||||
external set width(int? value);
|
||||
external set height(int? value);
|
||||
Future<dynamic> decode() =>
|
||||
js_util.promiseToFuture(js_util.callMethod(this, 'decode', <Object>[]));
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -741,6 +751,66 @@ extension DomPopStateEventExtension on DomPopStateEvent {
|
||||
dynamic get state => js_util.dartify(js_util.getProperty(this, 'state'));
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomURL {}
|
||||
|
||||
extension DomURLExtension on DomURL {
|
||||
external String createObjectURL(Object object);
|
||||
external void revokeObjectURL(String url);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomBlob {}
|
||||
|
||||
DomBlob createDomBlob(List<Object?> parts) =>
|
||||
domCallConstructorString('Blob', <Object>[parts])! as DomBlob;
|
||||
|
||||
typedef DomMutationCallback = void Function(
|
||||
List<dynamic> mutation, DomMutationObserver observer);
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMutationObserver {}
|
||||
|
||||
DomMutationObserver createDomMutationObserver(DomMutationCallback callback) =>
|
||||
domCallConstructorString('MutationObserver', <Object>[callback])!
|
||||
as DomMutationObserver;
|
||||
|
||||
extension DomMutationObserverExtension on DomMutationObserver {
|
||||
external void disconnect();
|
||||
void observe(DomNode target, {bool? childList}) {
|
||||
final Map<String, bool> options = <String, bool>{
|
||||
if (childList != null) 'childList': childList,
|
||||
};
|
||||
return js_util
|
||||
.callMethod(this, 'observe', <Object>[target, js_util.jsify(options)]);
|
||||
}
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMutationRecord {}
|
||||
|
||||
extension DomMutationRecordExtension on DomMutationRecord {
|
||||
Iterable<DomNode>? get addedNodes {
|
||||
final _DomList? list = js_util.getProperty<_DomList?>(this, 'addedNodes');
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
return createDomListWrapper<DomNode>(list);
|
||||
}
|
||||
|
||||
Iterable<DomNode>? get removedNodes {
|
||||
final _DomList? list = js_util.getProperty<_DomList?>(this, 'removedNodes');
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
return createDomListWrapper<DomNode>(list);
|
||||
}
|
||||
}
|
||||
|
||||
Object? domGetConstructor(String constructorName) =>
|
||||
js_util.getProperty(domWindow, constructorName);
|
||||
|
||||
@@ -761,47 +831,47 @@ bool domInstanceOfString(Object? element, String objectType) =>
|
||||
/// should only be returned as a wrapped object to Dart.
|
||||
@JS()
|
||||
@staticInterop
|
||||
class _DomElementList {}
|
||||
class _DomList {}
|
||||
|
||||
extension DomElementListExtension on _DomElementList {
|
||||
extension DomListExtension on _DomList {
|
||||
external int get length;
|
||||
DomElement item(int index) =>
|
||||
js_util.callMethod<DomElement>(this, 'item', <Object>[index]);
|
||||
DomNode item(int index) =>
|
||||
js_util.callMethod<DomNode>(this, 'item', <Object>[index]);
|
||||
}
|
||||
|
||||
class _DomElementListIterator extends Iterator<DomElement> {
|
||||
final _DomElementList elementList;
|
||||
class _DomListIterator<T> extends Iterator<T> {
|
||||
final _DomList list;
|
||||
int index = -1;
|
||||
|
||||
_DomElementListIterator(this.elementList);
|
||||
_DomListIterator(this.list);
|
||||
|
||||
@override
|
||||
bool moveNext() {
|
||||
index++;
|
||||
if (index > elementList.length) {
|
||||
if (index > list.length) {
|
||||
throw 'Iterator out of bounds';
|
||||
}
|
||||
return index < elementList.length;
|
||||
return index < list.length;
|
||||
}
|
||||
|
||||
@override
|
||||
DomElement get current => elementList.item(index);
|
||||
T get current => list.item(index) as T;
|
||||
}
|
||||
|
||||
class _DomElementListWrapper extends Iterable<DomElement> {
|
||||
final _DomElementList elementList;
|
||||
class _DomListWrapper<T> extends Iterable<T> {
|
||||
final _DomList list;
|
||||
|
||||
_DomElementListWrapper._(this.elementList);
|
||||
|
||||
/// This is a work around for a `TypeError` which can be triggered by calling
|
||||
/// `toList` on the `Iterable`.
|
||||
static Iterable<DomElement> create(_DomElementList elementList) =>
|
||||
_DomElementListWrapper._(elementList).cast<DomElement>();
|
||||
_DomListWrapper._(this.list);
|
||||
|
||||
@override
|
||||
Iterator<DomElement> get iterator => _DomElementListIterator(elementList);
|
||||
Iterator<T> get iterator => _DomListIterator<T>(list);
|
||||
|
||||
/// Override the length to avoid iterating through the whole collection.
|
||||
@override
|
||||
int get length => elementList.length;
|
||||
int get length => list.length;
|
||||
}
|
||||
|
||||
/// This is a work around for a `TypeError` which can be triggered by calling
|
||||
/// `toList` on the `Iterable`.
|
||||
Iterable<T> createDomListWrapper<T>(_DomList list) =>
|
||||
_DomListWrapper<T>._(list).cast<T>();
|
||||
|
||||
@@ -640,12 +640,11 @@ class BitmapCanvas extends EngineCanvas {
|
||||
}
|
||||
}
|
||||
// Can't reuse, create new instance.
|
||||
final html.ImageElement newImageElement = htmlImage.cloneImageElement();
|
||||
final DomHTMLImageElement newImageElement = htmlImage.cloneImageElement();
|
||||
if (_elementCache != null) {
|
||||
_elementCache!.cache(cacheKey, newImageElement as DomHTMLImageElement,
|
||||
_onEvictElement);
|
||||
_elementCache!.cache(cacheKey, newImageElement, _onEvictElement);
|
||||
}
|
||||
return newImageElement as DomHTMLImageElement;
|
||||
return newImageElement;
|
||||
}
|
||||
|
||||
static void _onEvictElement(DomHTMLElement element) {
|
||||
|
||||
@@ -3,18 +3,18 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import 'browser_detection.dart';
|
||||
import 'dom.dart';
|
||||
import 'safe_browser_api.dart';
|
||||
import 'util.dart';
|
||||
|
||||
Object? get _jsImageDecodeFunction => getJsProperty<Object?>(
|
||||
getJsProperty<Object>(
|
||||
getJsProperty<Object>(html.window, 'Image'),
|
||||
getJsProperty<Object>(domWindow, 'Image'),
|
||||
'prototype',
|
||||
),
|
||||
'decode',
|
||||
@@ -44,7 +44,7 @@ class HtmlCodec implements ui.Codec {
|
||||
// builders to create UI.
|
||||
chunkCallback?.call(0, 100);
|
||||
if (_supportsDecode) {
|
||||
final html.ImageElement imgElement = html.ImageElement();
|
||||
final DomHTMLImageElement imgElement = createDomHTMLImageElement();
|
||||
imgElement.src = src;
|
||||
setJsProperty<String>(imgElement, 'decoding', 'async');
|
||||
|
||||
@@ -82,24 +82,27 @@ class HtmlCodec implements ui.Codec {
|
||||
}
|
||||
|
||||
void _decodeUsingOnLoad(Completer<ui.FrameInfo> completer) {
|
||||
StreamSubscription<html.Event>? loadSubscription;
|
||||
late StreamSubscription<html.Event> errorSubscription;
|
||||
final html.ImageElement imgElement = html.ImageElement();
|
||||
final DomHTMLImageElement imgElement = createDomHTMLImageElement();
|
||||
// If the browser doesn't support asynchronous decoding of an image,
|
||||
// then use the `onload` event to decide when it's ready to paint to the
|
||||
// DOM. Unfortunately, this will cause the image to be decoded synchronously
|
||||
// on the main thread, and may cause dropped framed.
|
||||
errorSubscription = imgElement.onError.listen((html.Event event) {
|
||||
loadSubscription?.cancel();
|
||||
errorSubscription.cancel();
|
||||
late DomEventListener errorListener;
|
||||
DomEventListener? loadListener;
|
||||
errorListener = allowInterop((DomEvent event) {
|
||||
if (loadListener != null) {
|
||||
imgElement.removeEventListener('load', loadListener);
|
||||
}
|
||||
imgElement.removeEventListener('error', errorListener);
|
||||
completer.completeError(event);
|
||||
});
|
||||
loadSubscription = imgElement.onLoad.listen((html.Event event) {
|
||||
imgElement.addEventListener('error', errorListener);
|
||||
loadListener = allowInterop((DomEvent event) {
|
||||
if (chunkCallback != null) {
|
||||
chunkCallback!(100, 100);
|
||||
}
|
||||
loadSubscription!.cancel();
|
||||
errorSubscription.cancel();
|
||||
imgElement.removeEventListener('load', loadListener!);
|
||||
imgElement.removeEventListener('error', errorListener);
|
||||
final HtmlImage image = HtmlImage(
|
||||
imgElement,
|
||||
imgElement.naturalWidth,
|
||||
@@ -107,6 +110,7 @@ class HtmlCodec implements ui.Codec {
|
||||
);
|
||||
completer.complete(SingleFrameInfo(image));
|
||||
});
|
||||
imgElement.addEventListener('load', loadListener);
|
||||
imgElement.src = src;
|
||||
}
|
||||
|
||||
@@ -115,13 +119,13 @@ class HtmlCodec implements ui.Codec {
|
||||
}
|
||||
|
||||
class HtmlBlobCodec extends HtmlCodec {
|
||||
final html.Blob blob;
|
||||
final DomBlob blob;
|
||||
|
||||
HtmlBlobCodec(this.blob) : super(html.Url.createObjectUrlFromBlob(blob));
|
||||
HtmlBlobCodec(this.blob) : super(domWindow.URL.createObjectURL(blob));
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
html.Url.revokeObjectUrl(src);
|
||||
domWindow.URL.revokeObjectURL(src);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +140,7 @@ class SingleFrameInfo implements ui.FrameInfo {
|
||||
}
|
||||
|
||||
class HtmlImage implements ui.Image {
|
||||
final html.ImageElement imgElement;
|
||||
final DomHTMLImageElement imgElement;
|
||||
bool _requiresClone = false;
|
||||
HtmlImage(this.imgElement, this.width, this.height);
|
||||
|
||||
@@ -181,12 +185,12 @@ class HtmlImage implements ui.Image {
|
||||
// The format rawRgba always returns straight rather than premul currently.
|
||||
case ui.ImageByteFormat.rawRgba:
|
||||
case ui.ImageByteFormat.rawStraightRgba:
|
||||
final html.CanvasElement canvas = html.CanvasElement()
|
||||
final DomCanvasElement canvas = createDomCanvasElement()
|
||||
..width = width
|
||||
..height = height;
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.drawImage(imgElement, 0, 0);
|
||||
final html.ImageData imageData = ctx.getImageData(0, 0, width, height);
|
||||
final DomImageData imageData = ctx.getImageData(0, 0, width, height);
|
||||
return Future<ByteData?>.value(imageData.data.buffer.asByteData());
|
||||
default:
|
||||
if (imgElement.src?.startsWith('data:') == true) {
|
||||
@@ -200,9 +204,9 @@ class HtmlImage implements ui.Image {
|
||||
|
||||
// Returns absolutely positioned actual image element on first call and
|
||||
// clones on subsequent calls.
|
||||
html.ImageElement cloneImageElement() {
|
||||
DomHTMLImageElement cloneImageElement() {
|
||||
if (_requiresClone) {
|
||||
return imgElement.clone(true) as html.ImageElement;
|
||||
return imgElement.cloneNode(true) as DomHTMLImageElement;
|
||||
} else {
|
||||
_requiresClone = true;
|
||||
imgElement.style.position = 'absolute';
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import 'dom.dart';
|
||||
import 'html/bitmap_canvas.dart';
|
||||
import 'html/recording_canvas.dart';
|
||||
import 'html_image_codec.dart';
|
||||
import 'safe_browser_api.dart';
|
||||
import 'util.dart';
|
||||
|
||||
/// An implementation of [ui.PictureRecorder] backed by a [RecordingCanvas].
|
||||
@@ -57,7 +58,7 @@ class EnginePicture implements ui.Picture {
|
||||
final BitmapCanvas canvas = BitmapCanvas.imageData(imageRect);
|
||||
recordingCanvas!.apply(canvas, imageRect);
|
||||
final String imageDataUrl = canvas.toDataUrl();
|
||||
final html.ImageElement imageElement = html.ImageElement()
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement()
|
||||
..src = imageDataUrl
|
||||
..width = width
|
||||
..height = height;
|
||||
@@ -68,16 +69,22 @@ class EnginePicture implements ui.Picture {
|
||||
|
||||
// Ignoring the returned futures from onError and onLoad because we're
|
||||
// communicating through the `onImageLoaded` completer.
|
||||
// ignore: unawaited_futures
|
||||
imageElement.onError.first.then(onImageLoaded.completeError);
|
||||
// ignore: unawaited_futures
|
||||
imageElement.onLoad.first.then((_) {
|
||||
late final DomEventListener errorListener;
|
||||
errorListener = allowInterop((DomEvent event) {
|
||||
onImageLoaded.completeError(event);
|
||||
imageElement.removeEventListener('error', errorListener);
|
||||
});
|
||||
imageElement.addEventListener('error', errorListener);
|
||||
late final DomEventListener loadListener;
|
||||
loadListener = allowInterop((DomEvent event) {
|
||||
onImageLoaded.complete(HtmlImage(
|
||||
imageElement,
|
||||
width,
|
||||
height,
|
||||
));
|
||||
imageElement.removeEventListener('load', loadListener);
|
||||
});
|
||||
imageElement.addEventListener('load', loadListener);
|
||||
return onImageLoaded.future;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
@TestOn('chrome || firefox')
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -496,19 +495,19 @@ void testMain() {
|
||||
// Renders a `string` by breaking it up into individual characters and
|
||||
// rendering each character into its own layer.
|
||||
Future<void> testCase(String string, String description, { int deletions = 0, int additions = 0, int moves = 0 }) {
|
||||
final Set<html.Node> actualDeletions = <html.Node>{};
|
||||
final Set<html.Node> actualAdditions = <html.Node>{};
|
||||
final Set<DomNode> actualDeletions = <DomNode>{};
|
||||
final Set<DomNode> actualAdditions = <DomNode>{};
|
||||
|
||||
// Watches DOM mutations and counts deletions and additions to the child
|
||||
// list of the `<flt-scene>` element.
|
||||
final html.MutationObserver observer = html.MutationObserver((List<dynamic> mutations, _) {
|
||||
for (final html.MutationRecord record in mutations.cast<html.MutationRecord>()) {
|
||||
final DomMutationObserver observer = createDomMutationObserver(allowInterop((List<dynamic> mutations, _) {
|
||||
for (final DomMutationRecord record in mutations.cast<DomMutationRecord>()) {
|
||||
actualDeletions.addAll(record.removedNodes!);
|
||||
actualAdditions.addAll(record.addedNodes!);
|
||||
}
|
||||
});
|
||||
}));
|
||||
observer.observe(
|
||||
SurfaceSceneBuilder.debugLastFrameScene!.rootElement! as html.Node, childList: true);
|
||||
SurfaceSceneBuilder.debugLastFrameScene!.rootElement!, childList: true);
|
||||
|
||||
final SurfaceSceneBuilder builder = SurfaceSceneBuilder();
|
||||
for (int i = 0; i < string.length; i++) {
|
||||
@@ -912,9 +911,9 @@ EnginePicture _drawPathImagePath() {
|
||||
}
|
||||
|
||||
HtmlImage createTestImage({int width = 100, int height = 50}) {
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasElement canvas =
|
||||
createDomCanvasElement(width: width, height: height);
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, 33, 50);
|
||||
ctx.fill();
|
||||
@@ -924,7 +923,7 @@ HtmlImage createTestImage({int width = 100, int height = 50}) {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(66, 0, 33, 50);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -127,9 +126,9 @@ Future<void> testMain() async {
|
||||
}
|
||||
|
||||
engine.HtmlImage createTestImage({int width = 200, int height = 150}) {
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final engine.DomCanvasElement canvas =
|
||||
engine.createDomCanvasElement(width: width, height: height);
|
||||
final engine.DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, width / 3, height);
|
||||
ctx.fill();
|
||||
@@ -139,7 +138,7 @@ engine.HtmlImage createTestImage({int width = 200, int height = 150}) {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(2 * width / 3, 0, width / 3, height);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final engine.DomHTMLImageElement imageElement = engine.createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return engine.HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
@@ -109,7 +107,7 @@ const String _base64Encoded20x20TestImage = 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAI
|
||||
|
||||
HtmlImage _createRealTestImage() {
|
||||
return HtmlImage(
|
||||
html.ImageElement()
|
||||
createDomHTMLImageElement()
|
||||
..src = 'data:text/plain;base64,$_base64Encoded20x20TestImage',
|
||||
20,
|
||||
20,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -107,9 +106,9 @@ Future<void> testMain() async {
|
||||
HtmlImage createTestImage() {
|
||||
const int width = 100;
|
||||
const int height = 50;
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasElement canvas =
|
||||
createDomCanvasElement(width: width, height: height);
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, 33, 50);
|
||||
ctx.fill();
|
||||
@@ -119,7 +118,7 @@ HtmlImage createTestImage() {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(66, 0, 33, 50);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -192,5 +191,5 @@ HtmlImage createTestImage({int width = 200, int height = 150}) {
|
||||
ctx.fill();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement as html.ImageElement, width, height);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:math' as math;
|
||||
|
||||
@@ -726,16 +725,16 @@ const String base64ImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUh'
|
||||
|
||||
HtmlImage createNineSliceImage() {
|
||||
return HtmlImage(
|
||||
html.ImageElement()..src = base64ImageData,
|
||||
createDomHTMLImageElement()..src = base64ImageData,
|
||||
60,
|
||||
60,
|
||||
);
|
||||
}
|
||||
|
||||
HtmlImage createTestImage({int width = 100, int height = 50}) {
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasElement canvas =
|
||||
createDomCanvasElement(width: width, height: height);
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, 33, 50);
|
||||
ctx.fill();
|
||||
@@ -745,7 +744,7 @@ HtmlImage createTestImage({int width = 100, int height = 50}) {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(66, 0, 33, 50);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
@@ -144,7 +142,7 @@ const String _base64Encoded20x20TestImage =
|
||||
|
||||
HtmlImage _createRealTestImage() {
|
||||
return HtmlImage(
|
||||
html.ImageElement()
|
||||
createDomHTMLImageElement()
|
||||
..src = 'data:text/plain;base64,$_base64Encoded20x20TestImage',
|
||||
20,
|
||||
20,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -416,9 +415,9 @@ Future<void> testMain() async {
|
||||
}
|
||||
|
||||
Future<HtmlImage> createTestImage({int width = 50, int height = 40}) {
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasElement canvas =
|
||||
createDomCanvasElement(width: width, height: height);
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, width / 3, height);
|
||||
ctx.fill();
|
||||
@@ -428,11 +427,11 @@ Future<HtmlImage> createTestImage({int width = 50, int height = 40}) {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(2 * width / 3, 0, width / 3, height);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
final Completer<HtmlImage> completer = Completer<HtmlImage>();
|
||||
imageElement.onLoad.listen((html.Event event) {
|
||||
imageElement.addEventListener('load', allowInterop((DomEvent event) {
|
||||
completer.complete(HtmlImage(imageElement, width, height));
|
||||
});
|
||||
}));
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -156,14 +155,14 @@ Future<void> testMain() async {
|
||||
// if any pixels are left semi-transparent, which might be caused by
|
||||
// converting to and from pre-multiplied values. See
|
||||
// https://github.com/flutter/flutter/issues/92958 .
|
||||
final CanvasElement canvas = CanvasElement()
|
||||
final DomCanvasElement canvas = createDomCanvasElement()
|
||||
..width = 2
|
||||
..height = 2;
|
||||
final CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.drawImage((blueBackground as HtmlImage).imgElement, 0, 0);
|
||||
ctx.drawImage((sourceImage as HtmlImage).imgElement, 0, 0);
|
||||
|
||||
final ImageData imageData = ctx.getImageData(0, 0, 2, 2);
|
||||
final DomImageData imageData = ctx.getImageData(0, 0, 2, 2);
|
||||
final List<int> actualPixels = imageData.data;
|
||||
|
||||
final Uint8List benchmarkPixels = _pixelsToBytes(
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
@@ -29,7 +27,7 @@ void testMain() {
|
||||
final ui.Picture picture = recorder.endRecording();
|
||||
final HtmlImage image = await picture.toImage(200, 100) as HtmlImage;
|
||||
expect(image, isNotNull);
|
||||
html.document.body!
|
||||
domDocument.body!
|
||||
..style.margin = '0'
|
||||
..append(image.imgElement);
|
||||
try {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:math' as math;
|
||||
import 'dart:typed_data';
|
||||
|
||||
@@ -723,7 +722,7 @@ const String _base64Encoded20x20TestImage = 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUC'
|
||||
|
||||
HtmlImage _createRealTestImage() {
|
||||
return HtmlImage(
|
||||
html.ImageElement()
|
||||
createDomHTMLImageElement()
|
||||
..src = 'data:text/plain;base64,$_base64Encoded20x20TestImage',
|
||||
20,
|
||||
20,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js_util' as js_util;
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
@@ -131,9 +130,9 @@ HtmlImage createTestImage() {
|
||||
const int width = 16;
|
||||
const int width2 = width ~/ 2;
|
||||
const int height = 16;
|
||||
final html.CanvasElement canvas =
|
||||
html.CanvasElement(width: width, height: height);
|
||||
final html.CanvasRenderingContext2D ctx = canvas.context2D;
|
||||
final DomCanvasElement canvas =
|
||||
createDomCanvasElement(width: width, height: height);
|
||||
final DomCanvasRenderingContext2D ctx = canvas.context2D;
|
||||
ctx.fillStyle = '#E04040';
|
||||
ctx.fillRect(0, 0, width2, width2);
|
||||
ctx.fill();
|
||||
@@ -143,7 +142,7 @@ HtmlImage createTestImage() {
|
||||
ctx.fillStyle = '#2040E0';
|
||||
ctx.fillRect(width2, width2, width2, width2);
|
||||
ctx.fill();
|
||||
final html.ImageElement imageElement = html.ImageElement();
|
||||
final DomHTMLImageElement imageElement = createDomHTMLImageElement();
|
||||
imageElement.src = js_util.callMethod<String>(canvas, 'toDataURL', <dynamic>[]);
|
||||
return HtmlImage(imageElement, width, height);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
/// 50x50 pixel flutter logo image that contains alpha ramps and colors
|
||||
@@ -117,7 +115,7 @@ const String _flutterLogoBase64 =
|
||||
|
||||
HtmlImage createFlutterLogoTestImage() {
|
||||
return HtmlImage(
|
||||
html.ImageElement()
|
||||
createDomHTMLImageElement()
|
||||
..src = 'data:text/plain;base64,$_flutterLogoBase64',
|
||||
50,
|
||||
50,
|
||||
|
||||
Reference in New Issue
Block a user