[web] Migrate Flutter Web DOM usage to JS static interop - 28. (flutter/engine#33356)
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -46,17 +45,17 @@ import 'window.dart';
|
||||
/// can be reused, [CanvasPool] will move canvas(s) from pool to reusablePool
|
||||
/// to prevent reallocation.
|
||||
class CanvasPool extends _SaveStackTracking {
|
||||
html.CanvasRenderingContext2D? _context;
|
||||
DomCanvasRenderingContext2D? _context;
|
||||
ContextStateHandle? _contextHandle;
|
||||
final int _widthInBitmapPixels, _heightInBitmapPixels;
|
||||
// List of canvases that have been allocated and used in this paint cycle.
|
||||
List<html.CanvasElement>? _activeCanvasList;
|
||||
List<DomCanvasElement>? _activeCanvasList;
|
||||
// List of canvases available to reuse from prior paint cycle.
|
||||
List<html.CanvasElement>? _reusablePool;
|
||||
List<DomCanvasElement>? _reusablePool;
|
||||
// Current canvas element or null if marked for lazy allocation.
|
||||
html.CanvasElement? _canvas;
|
||||
DomCanvasElement? _canvas;
|
||||
|
||||
html.HtmlElement? _rootElement;
|
||||
DomHTMLElement? _rootElement;
|
||||
int _saveContextCount = 0;
|
||||
final double _density;
|
||||
|
||||
@@ -65,7 +64,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
this._density);
|
||||
|
||||
/// Initializes canvas pool to be hosted on a surface.
|
||||
void mount(html.HtmlElement rootElement) {
|
||||
void mount(DomHTMLElement rootElement) {
|
||||
_rootElement = rootElement;
|
||||
}
|
||||
|
||||
@@ -78,8 +77,8 @@ class CanvasPool extends _SaveStackTracking {
|
||||
}
|
||||
|
||||
/// Returns [CanvasRenderingContext2D] api to draw into this canvas.
|
||||
html.CanvasRenderingContext2D get context {
|
||||
html.CanvasRenderingContext2D? ctx = _context;
|
||||
DomCanvasRenderingContext2D get context {
|
||||
DomCanvasRenderingContext2D? ctx = _context;
|
||||
if (ctx == null) {
|
||||
_createCanvas();
|
||||
ctx = _context!;
|
||||
@@ -128,7 +127,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
if (_canvas != null) {
|
||||
_restoreContextSave();
|
||||
_contextHandle!.reset();
|
||||
_activeCanvasList ??= <html.CanvasElement>[];
|
||||
_activeCanvasList ??= <DomCanvasElement>[];
|
||||
_activeCanvasList!.add(_canvas!);
|
||||
_canvas = null;
|
||||
_context = null;
|
||||
@@ -139,7 +138,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
void _createCanvas() {
|
||||
bool requiresClearRect = false;
|
||||
bool reused = false;
|
||||
html.CanvasElement? canvas;
|
||||
DomCanvasElement? canvas;
|
||||
if (_canvas != null) {
|
||||
_canvas!.width = 0;
|
||||
_canvas!.height = 0;
|
||||
@@ -214,13 +213,13 @@ class CanvasPool extends _SaveStackTracking {
|
||||
_replayClipStack();
|
||||
}
|
||||
|
||||
html.CanvasElement? _allocCanvas(int width, int height) {
|
||||
DomCanvasElement? _allocCanvas(int width, int height) {
|
||||
// The dartdocs for `tryCreateCanvasElement` on why we don't use the
|
||||
// `html.CanvasElement` constructor.
|
||||
// `DomCanvasElement` constructor.
|
||||
return tryCreateCanvasElement(
|
||||
(width * _density).ceil(),
|
||||
(height * _density).ceil(),
|
||||
);
|
||||
) as DomCanvasElement?;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -229,7 +228,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
if (_canvas != null) {
|
||||
// Restore to the state where we have only applied the scaling.
|
||||
final html.CanvasRenderingContext2D? ctx = _context;
|
||||
final DomCanvasRenderingContext2D? ctx = _context;
|
||||
if (ctx != null) {
|
||||
try {
|
||||
ctx.font = '';
|
||||
@@ -247,7 +246,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
int _replaySingleSaveEntry(int clipDepth, Matrix4 prevTransform,
|
||||
Matrix4 transform, List<SaveClipEntry>? clipStack) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
if (clipStack != null) {
|
||||
for (final int clipCount = clipStack.length;
|
||||
clipDepth < clipCount;
|
||||
@@ -306,7 +305,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
void _replayClipStack() {
|
||||
// Replay save/clip stack on this canvas now.
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
int clipDepth = 0;
|
||||
Matrix4 prevTransform = Matrix4.identity();
|
||||
final int len = _saveStack.length;
|
||||
@@ -327,7 +326,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
if (_canvas != null) {
|
||||
_restoreContextSave();
|
||||
_contextHandle!.reset();
|
||||
_activeCanvasList ??= <html.CanvasElement>[];
|
||||
_activeCanvasList ??= <DomCanvasElement>[];
|
||||
_activeCanvasList!.add(_canvas!);
|
||||
_context = null;
|
||||
_contextHandle = null;
|
||||
@@ -344,7 +343,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
/// that are reused from last instance can be cleanup.
|
||||
void endOfPaint() {
|
||||
if (_reusablePool != null) {
|
||||
for (final html.CanvasElement e in _reusablePool!) {
|
||||
for (final DomCanvasElement e in _reusablePool!) {
|
||||
if (browserEngine == BrowserEngine.webkit) {
|
||||
e.width = e.height = 0;
|
||||
}
|
||||
@@ -366,7 +365,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
/// coordinate system, and the pixel ratio is applied such that CSS pixels are
|
||||
/// translated to bitmap pixels.
|
||||
void _initializeViewport(bool clearCanvas) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
// Save the canvas state with top-level transforms so we can undo
|
||||
// any clips later when we reuse the canvas.
|
||||
ctx.save();
|
||||
@@ -390,7 +389,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
EnginePlatformDispatcher.browserDevicePixelRatio * _density;
|
||||
|
||||
void _resetTransform() {
|
||||
final html.CanvasElement? canvas = _canvas;
|
||||
final DomCanvasElement? canvas = _canvas;
|
||||
if (canvas != null) {
|
||||
canvas.style.transformOrigin = '';
|
||||
canvas.style.transform = '';
|
||||
@@ -399,7 +398,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
/// Returns a "data://" URI containing a representation of the image in this
|
||||
/// canvas in PNG format.
|
||||
String toDataUrl() => _canvas?.toDataUrl() ?? '';
|
||||
String toDataUrl() => _canvas?.toDataURL('image/png') ?? '';
|
||||
|
||||
@override
|
||||
void save() {
|
||||
@@ -498,7 +497,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
}
|
||||
}
|
||||
|
||||
void _clipRect(html.CanvasRenderingContext2D ctx, ui.Rect rect) {
|
||||
void _clipRect(DomCanvasRenderingContext2D ctx, ui.Rect rect) {
|
||||
ctx.beginPath();
|
||||
ctx.rect(rect.left, rect.top, rect.width, rect.height);
|
||||
ctx.clip();
|
||||
@@ -512,7 +511,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
}
|
||||
}
|
||||
|
||||
void _clipRRect(html.CanvasRenderingContext2D ctx, ui.RRect rrect) {
|
||||
void _clipRRect(DomCanvasRenderingContext2D ctx, ui.RRect rrect) {
|
||||
final ui.Path path = ui.Path()..addRRect(rrect);
|
||||
_runPath(ctx, path as SurfacePath);
|
||||
ctx.clip();
|
||||
@@ -522,7 +521,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
void clipPath(ui.Path path) {
|
||||
super.clipPath(path);
|
||||
if (_canvas != null) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
_runPath(ctx, path as SurfacePath);
|
||||
if (path.fillType == ui.PathFillType.nonZero) {
|
||||
ctx.clip();
|
||||
@@ -534,7 +533,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
/// Fill a virtually infinite rect with a color and optional blendMode.
|
||||
void drawColor(ui.Color color, ui.BlendMode blendMode) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
contextHandle.blendMode = blendMode;
|
||||
contextHandle.fillStyle = colorToCssString(color);
|
||||
contextHandle.strokeStyle = '';
|
||||
@@ -548,7 +547,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
/// Fill a virtually infinite rect with the color.
|
||||
void fill() {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
ctx.beginPath();
|
||||
// We can't use (0, 0, width, height) because the current transform can
|
||||
// cause it to not fill the entire clip.
|
||||
@@ -557,7 +556,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
/// Draws a line from [p1] to [p2].
|
||||
void strokeLine(ui.Offset p1, ui.Offset p2) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
ctx.beginPath();
|
||||
final ui.Rect? shaderBounds = contextHandle._shaderBounds;
|
||||
if (shaderBounds == null) {
|
||||
@@ -573,7 +572,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
/// Draws a set of points with given radius, lines between points or
|
||||
/// a polygon.
|
||||
void drawPoints(ui.PointMode pointMode, Float32List points, double radius) {
|
||||
final html.CanvasRenderingContext2D ctx = context;
|
||||
final DomCanvasRenderingContext2D ctx = context;
|
||||
final int len = points.length;
|
||||
final ui.Rect? shaderBounds = contextHandle._shaderBounds;
|
||||
final double offsetX = shaderBounds == null ? 0 : -shaderBounds.left;
|
||||
@@ -611,7 +610,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
static Float32List _runBuffer = Float32List(PathRefIterator.kMaxBufferSize);
|
||||
|
||||
/// 'Runs' the given [path] by applying all of its commands to the canvas.
|
||||
void _runPath(html.CanvasRenderingContext2D ctx, SurfacePath path) {
|
||||
void _runPath(DomCanvasRenderingContext2D ctx, SurfacePath path) {
|
||||
ctx.beginPath();
|
||||
final Float32List p = _runBuffer;
|
||||
final PathRefIterator iter = PathRefIterator(path.pathRef);
|
||||
@@ -668,7 +667,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
/// Applies path to drawing context, preparing for fill and other operations.
|
||||
///
|
||||
/// WARNING: Don't refactor _runPath/_runPathWithOffset. Latency sensitive
|
||||
void _runPathWithOffset(html.CanvasRenderingContext2D ctx, SurfacePath path,
|
||||
void _runPathWithOffset(DomCanvasRenderingContext2D ctx, SurfacePath path,
|
||||
double offsetX, double offsetY) {
|
||||
ctx.beginPath();
|
||||
final Float32List p = _runBuffer;
|
||||
@@ -857,7 +856,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
|
||||
void _clearActiveCanvasList() {
|
||||
if (_activeCanvasList != null) {
|
||||
for (final html.CanvasElement c in _activeCanvasList!) {
|
||||
for (final DomCanvasElement c in _activeCanvasList!) {
|
||||
if (browserEngine == BrowserEngine.webkit) {
|
||||
c.width = c.height = 0;
|
||||
}
|
||||
@@ -874,7 +873,7 @@ class CanvasPool extends _SaveStackTracking {
|
||||
/// to initialize current values.
|
||||
class ContextStateHandle {
|
||||
/// Associated canvas element context tracked by this context state.
|
||||
final html.CanvasRenderingContext2D context;
|
||||
final DomCanvasRenderingContext2D context;
|
||||
final CanvasPool _canvasPool;
|
||||
/// Dpi of context.
|
||||
final double density;
|
||||
@@ -890,7 +889,7 @@ class ContextStateHandle {
|
||||
Object? _currentStrokeStyle;
|
||||
double _currentLineWidth = 1.0;
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set blendMode(ui.BlendMode? blendMode) {
|
||||
if (blendMode != _currentBlendMode) {
|
||||
_currentBlendMode = blendMode;
|
||||
@@ -899,7 +898,7 @@ class ContextStateHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set strokeCap(ui.StrokeCap? strokeCap) {
|
||||
strokeCap ??= ui.StrokeCap.butt;
|
||||
if (strokeCap != _currentStrokeCap) {
|
||||
@@ -908,7 +907,7 @@ class ContextStateHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set lineWidth(double lineWidth) {
|
||||
if (lineWidth != _currentLineWidth) {
|
||||
_currentLineWidth = lineWidth;
|
||||
@@ -916,7 +915,7 @@ class ContextStateHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set strokeJoin(ui.StrokeJoin? strokeJoin) {
|
||||
strokeJoin ??= ui.StrokeJoin.miter;
|
||||
if (strokeJoin != _currentStrokeJoin) {
|
||||
@@ -925,7 +924,7 @@ class ContextStateHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set fillStyle(Object? colorOrGradient) {
|
||||
if (!identical(colorOrGradient, _currentFillStyle)) {
|
||||
_currentFillStyle = colorOrGradient;
|
||||
@@ -933,7 +932,7 @@ class ContextStateHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// See [html.CanvasRenderingContext2D].
|
||||
/// See [DomCanvasRenderingContext2D].
|
||||
set strokeStyle(Object? colorOrGradient) {
|
||||
if (!identical(colorOrGradient, _currentStrokeStyle)) {
|
||||
_currentStrokeStyle = colorOrGradient;
|
||||
@@ -984,9 +983,7 @@ class ContextStateHandle {
|
||||
if (paint.shader is EngineGradient) {
|
||||
final EngineGradient engineShader = paint.shader! as EngineGradient;
|
||||
final Object paintStyle =
|
||||
engineShader.createPaintStyle(_canvasPool.context as
|
||||
DomCanvasRenderingContext2D, shaderBounds,
|
||||
density);
|
||||
engineShader.createPaintStyle(_canvasPool.context, shaderBounds, density);
|
||||
fillStyle = paintStyle;
|
||||
strokeStyle = paintStyle;
|
||||
_shaderBounds = shaderBounds;
|
||||
@@ -995,9 +992,7 @@ class ContextStateHandle {
|
||||
} else if (paint.shader is EngineImageShader) {
|
||||
final EngineImageShader imageShader = paint.shader! as EngineImageShader;
|
||||
final Object paintStyle =
|
||||
imageShader.createPaintStyle(_canvasPool.context as
|
||||
DomCanvasRenderingContext2D, shaderBounds,
|
||||
density);
|
||||
imageShader.createPaintStyle(_canvasPool.context, shaderBounds, density);
|
||||
fillStyle = paintStyle;
|
||||
strokeStyle = paintStyle;
|
||||
if (imageShader.requiresTileOffset) {
|
||||
@@ -1117,7 +1112,7 @@ class ContextStateHandle {
|
||||
}
|
||||
|
||||
/// Resets drawing context state to defaults for
|
||||
/// [html.CanvasRenderingContext2D].
|
||||
/// [DomCanvasRenderingContext2D].
|
||||
void reset() {
|
||||
context.fillStyle = '';
|
||||
// Read back fillStyle/strokeStyle values from context so that input such
|
||||
|
||||
@@ -38,6 +38,8 @@ extension DomWindowExtension on DomWindow {
|
||||
js_util.promiseToFuture(js_util.callMethod(this, 'fetch', <String>[url]));
|
||||
// ignore: non_constant_identifier_names
|
||||
external DomURL get URL;
|
||||
external bool dispatchEvent(DomEvent event);
|
||||
external DomMediaQueryList matchMedia(String? query);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -62,6 +64,8 @@ extension DomNavigatorExtension on DomNavigator {
|
||||
external String get language;
|
||||
external String? get platform;
|
||||
external String get userAgent;
|
||||
List<String>? get languages =>
|
||||
js_util.getProperty<List<Object?>?>(this, 'languages')?.cast<String>();
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -82,6 +86,7 @@ extension DomDocumentExtension on DomDocument {
|
||||
external DomElement createElementNS(
|
||||
String namespaceURI, String qualifiedName);
|
||||
external DomText createTextNode(String data);
|
||||
external DomEvent createEvent(String eventType);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -92,6 +97,7 @@ extension DomHTMLDocumentExtension on DomHTMLDocument {
|
||||
external DomFontFaceSet? get fonts;
|
||||
external DomHTMLHeadElement? get head;
|
||||
external DomHTMLBodyElement? get body;
|
||||
external set title(String? value);
|
||||
}
|
||||
|
||||
@JS('document')
|
||||
@@ -131,6 +137,18 @@ extension DomEventExtension on DomEvent {
|
||||
external String get type;
|
||||
external void preventDefault();
|
||||
external void stopPropagation();
|
||||
void initEvent(String type, [bool? bubbles, bool? cancelable]) =>
|
||||
js_util.callMethod(this, 'initEvent', <Object>[
|
||||
type,
|
||||
if (bubbles != null) bubbles,
|
||||
if (cancelable != null) cancelable
|
||||
]);
|
||||
}
|
||||
|
||||
DomEvent createDomEvent(String type, String name) {
|
||||
final DomEvent event = domDocument.createEvent(type);
|
||||
event.initEvent(name, true, true);
|
||||
return event;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -497,6 +515,7 @@ extension DomCanvasRenderingContext2DExtension on DomCanvasRenderingContext2D {
|
||||
external set font(String value);
|
||||
external set lineWidth(num? value);
|
||||
external set strokeStyle(Object? value);
|
||||
external Object? get strokeStyle;
|
||||
external void beginPath();
|
||||
external void closePath();
|
||||
external DomCanvasGradient createLinearGradient(
|
||||
@@ -505,7 +524,8 @@ extension DomCanvasRenderingContext2DExtension on DomCanvasRenderingContext2D {
|
||||
external DomCanvasGradient createRadialGradient(
|
||||
num x0, num y0, num r0, num x1, num y1, num r1);
|
||||
external void drawImage(DomCanvasImageSource source, num destX, num destY);
|
||||
external void fill();
|
||||
void fill([Object? pathOrWinding]) => js_util.callMethod(
|
||||
this, 'fill', <Object?>[if (pathOrWinding != null) pathOrWinding]);
|
||||
external void fillRect(num x, num y, num width, num height);
|
||||
void fillText(String text, num x, num y, [num? maxWidth]) =>
|
||||
js_util.callMethod(this, 'fillText',
|
||||
@@ -519,6 +539,33 @@ extension DomCanvasRenderingContext2DExtension on DomCanvasRenderingContext2D {
|
||||
external void rect(num x, num y, num width, num height);
|
||||
external void resetTransform();
|
||||
external void restore();
|
||||
external void setTransform(num a, num b, num c, num d, num e, num f);
|
||||
external void transform(num a, num b, num c, num d, num e, num f);
|
||||
void clip([Object? pathOrWinding]) => js_util.callMethod(
|
||||
this, 'clip', <Object?>[if (pathOrWinding != null) pathOrWinding]);
|
||||
external void scale(num x, num y);
|
||||
external void clearRect(num x, num y, num width, num height);
|
||||
external void translate(num x, num y);
|
||||
external void rotate(num angle);
|
||||
external void bezierCurveTo(
|
||||
num cp1x, num cp1y, num numcp2x, num cp2y, num x, num y);
|
||||
external void quadraticCurveTo(num cpx, num cpy, num x, num y);
|
||||
external set globalCompositeOperation(String value);
|
||||
external set lineCap(String value);
|
||||
external set lineJoin(String value);
|
||||
external set shadowBlur(num value);
|
||||
void arc(num x, num y, num radius, num startAngle, num endAngle,
|
||||
[bool antiClockwise = false]) =>
|
||||
js_util.callMethod(this, 'arc',
|
||||
<Object>[x, y, radius, startAngle, endAngle, antiClockwise]);
|
||||
external set filter(String? value);
|
||||
external set shadowOffsetX(num? x);
|
||||
external set shadowOffsetY(num? y);
|
||||
external set shadowColor(String? value);
|
||||
external void ellipse(num x, num y, num radiusX, num radiusY, num rotation,
|
||||
num startAngle, num endAngle, bool? antiClockwise);
|
||||
external void strokeText(String text, num x, num y);
|
||||
external set globalAlpha(num? value);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -808,9 +855,12 @@ DomMutationObserver createDomMutationObserver(DomMutationCallback callback) =>
|
||||
|
||||
extension DomMutationObserverExtension on DomMutationObserver {
|
||||
external void disconnect();
|
||||
void observe(DomNode target, {bool? childList}) {
|
||||
final Map<String, bool> options = <String, bool>{
|
||||
void observe(DomNode target,
|
||||
{bool? childList, bool? attributes, List<String>? attributeFilter}) {
|
||||
final Map<String, dynamic> options = <String, dynamic>{
|
||||
if (childList != null) 'childList': childList,
|
||||
if (attributes != null) 'attributes': attributes,
|
||||
if (attributeFilter != null) 'attributeFilter': attributeFilter
|
||||
};
|
||||
return js_util
|
||||
.callMethod(this, 'observe', <Object>[target, js_util.jsify(options)]);
|
||||
@@ -837,8 +887,37 @@ extension DomMutationRecordExtension on DomMutationRecord {
|
||||
}
|
||||
return createDomListWrapper<DomNode>(list);
|
||||
}
|
||||
|
||||
external String? get attributeName;
|
||||
external String? get type;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMediaQueryList extends DomEventTarget {}
|
||||
|
||||
extension DomMediaQueryListExtension on DomMediaQueryList {
|
||||
external bool get matches;
|
||||
external void addListener(DomEventListener? listener);
|
||||
external void removeListener(DomEventListener? listener);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMediaQueryListEvent extends DomEvent {}
|
||||
|
||||
extension DomMediaQueryListEventExtension on DomMediaQueryListEvent {
|
||||
external bool? get matches;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomPath2D {}
|
||||
|
||||
DomPath2D createDomPath2D([Object? path]) =>
|
||||
domCallConstructorString('Path2D', <Object>[if (path != null) path])!
|
||||
as DomPath2D;
|
||||
|
||||
Object? domGetConstructor(String constructorName) =>
|
||||
js_util.getProperty(domWindow, constructorName);
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -159,7 +158,7 @@ class BitmapCanvas extends EngineCanvas {
|
||||
_canvasPositionX = _bounds.left.floor() - kPaddingPixels;
|
||||
_canvasPositionY = _bounds.top.floor() - kPaddingPixels;
|
||||
_updateRootElementTransform();
|
||||
_canvasPool.mount(rootElement as html.HtmlElement);
|
||||
_canvasPool.mount(rootElement as DomHTMLElement);
|
||||
_setupInitialTransform();
|
||||
}
|
||||
|
||||
@@ -892,18 +891,18 @@ class BitmapCanvas extends EngineCanvas {
|
||||
|
||||
void setCssFont(String cssFont) {
|
||||
if (cssFont != _cachedLastCssFont) {
|
||||
final html.CanvasRenderingContext2D ctx = _canvasPool.context;
|
||||
final DomCanvasRenderingContext2D ctx = _canvasPool.context;
|
||||
ctx.font = cssFont;
|
||||
_cachedLastCssFont = cssFont;
|
||||
}
|
||||
}
|
||||
|
||||
/// Measures the given [text] and returns a [html.TextMetrics] object that
|
||||
/// Measures the given [text] and returns a [DomTextMetrics] object that
|
||||
/// contains information about the measurement.
|
||||
///
|
||||
/// The text is measured using the font set by the most recent call to
|
||||
/// [setCssFont].
|
||||
html.TextMetrics measureText(String text) {
|
||||
DomTextMetrics measureText(String text) {
|
||||
return _canvasPool.context.measureText(text);
|
||||
}
|
||||
|
||||
@@ -912,7 +911,7 @@ class BitmapCanvas extends EngineCanvas {
|
||||
/// The text is drawn starting at coordinates ([x], [y]). It uses the current
|
||||
/// font set by the most recent call to [setCssFont].
|
||||
void drawText(String text, double x, double y, {ui.PaintingStyle? style, List<ui.Shadow>? shadows}) {
|
||||
final html.CanvasRenderingContext2D ctx = _canvasPool.context;
|
||||
final DomCanvasRenderingContext2D ctx = _canvasPool.context;
|
||||
if (shadows != null) {
|
||||
ctx.save();
|
||||
for (final ui.Shadow shadow in shadows) {
|
||||
@@ -1013,8 +1012,7 @@ class BitmapCanvas extends EngineCanvas {
|
||||
'Linear/Radial/SweepGradient not supported yet');
|
||||
final Int32List? colors = vertices.colors;
|
||||
final ui.VertexMode mode = vertices.mode;
|
||||
final DomCanvasRenderingContext2D ctx = _canvasPool.context as
|
||||
DomCanvasRenderingContext2D;
|
||||
final DomCanvasRenderingContext2D ctx = _canvasPool.context;
|
||||
if (colors == null &&
|
||||
paint.style != ui.PaintingStyle.fill &&
|
||||
paint.shader == null) {
|
||||
|
||||
@@ -130,7 +130,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
|
||||
/// Returns device pixel ratio returned by browser.
|
||||
static double get browserDevicePixelRatio {
|
||||
final double? ratio = html.window.devicePixelRatio as double?;
|
||||
final double? ratio = domWindow.devicePixelRatio as double?;
|
||||
// Guard against WebOS returning 0 and other browsers returning null.
|
||||
return (ratio == null || ratio == 0.0) ? 1.0 : ratio;
|
||||
}
|
||||
@@ -429,7 +429,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
// TODO(ferhat): Find more appropriate defaults? Or noop when values are null?
|
||||
final String label = arguments['label'] as String? ?? '';
|
||||
final int primaryColor = arguments['primaryColor'] as int? ?? 0xFF000000;
|
||||
html.document.title = label;
|
||||
domDocument.title = label;
|
||||
setThemeColor(ui.Color(primaryColor));
|
||||
replyToPlatformMessage(callback, codec.encodeSuccessEnvelope(true));
|
||||
return;
|
||||
@@ -455,7 +455,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
|
||||
// Dispatched by the bindings to delay service worker initialization.
|
||||
case 'flutter/service_worker':
|
||||
html.window.dispatchEvent(html.Event('flutter-first-frame'));
|
||||
domWindow.dispatchEvent(createDomEvent('Event', 'flutter-first-frame'));
|
||||
return;
|
||||
|
||||
case 'flutter/textinput':
|
||||
@@ -736,7 +736,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
|
||||
static List<ui.Locale> parseBrowserLanguages() {
|
||||
// TODO(yjbanov): find a solution for IE
|
||||
final List<String>? languages = html.window.navigator.languages;
|
||||
final List<String>? languages = domWindow.navigator.languages;
|
||||
if (languages == null || languages.isEmpty) {
|
||||
// To make it easier for the app code, let's not leave the locales list
|
||||
// empty. This way there's fewer corner cases for apps to handle.
|
||||
@@ -799,26 +799,26 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
/// recalculate [textScaleFactor].
|
||||
///
|
||||
/// Updates [textScaleFactor] with the new value.
|
||||
html.MutationObserver? _fontSizeObserver;
|
||||
DomMutationObserver? _fontSizeObserver;
|
||||
|
||||
/// Set the callback function for updating [textScaleFactor] based on
|
||||
/// font-size changes in the browser's <html> element.
|
||||
void _addFontSizeObserver() {
|
||||
const String styleAttribute = 'style';
|
||||
|
||||
_fontSizeObserver = html.MutationObserver(
|
||||
(List<dynamic> mutations, html.MutationObserver _) {
|
||||
_fontSizeObserver = createDomMutationObserver(allowInterop(
|
||||
(List<dynamic> mutations, DomMutationObserver _) {
|
||||
for (final dynamic mutation in mutations) {
|
||||
final html.MutationRecord record = mutation as html.MutationRecord;
|
||||
final DomMutationRecord record = mutation as DomMutationRecord;
|
||||
if (record.type == 'attributes' &&
|
||||
record.attributeName == styleAttribute) {
|
||||
final double newTextScaleFactor = findBrowserTextScaleFactor();
|
||||
_updateTextScaleFactor(newTextScaleFactor);
|
||||
}
|
||||
}
|
||||
});
|
||||
}));
|
||||
_fontSizeObserver!.observe(
|
||||
html.document.documentElement!,
|
||||
domDocument.documentElement!,
|
||||
attributes: true,
|
||||
attributeFilter: <String>[styleAttribute],
|
||||
);
|
||||
@@ -887,13 +887,13 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
String? get systemFontFamily => configuration.systemFontFamily;
|
||||
|
||||
/// Reference to css media query that indicates the user theme preference on the web.
|
||||
final html.MediaQueryList _brightnessMediaQuery =
|
||||
html.window.matchMedia('(prefers-color-scheme: dark)');
|
||||
final DomMediaQueryList _brightnessMediaQuery =
|
||||
domWindow.matchMedia('(prefers-color-scheme: dark)');
|
||||
|
||||
/// A callback that is invoked whenever [_brightnessMediaQuery] changes value.
|
||||
///
|
||||
/// Updates the [_platformBrightness] with the new user preference.
|
||||
html.EventListener? _brightnessMediaQueryListener;
|
||||
DomEventListener? _brightnessMediaQueryListener;
|
||||
|
||||
/// Set the callback function for listening changes in [_brightnessMediaQuery] value.
|
||||
void _addBrightnessMediaQueryListener() {
|
||||
@@ -901,12 +901,12 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
? ui.Brightness.dark
|
||||
: ui.Brightness.light);
|
||||
|
||||
_brightnessMediaQueryListener = (html.Event event) {
|
||||
final html.MediaQueryListEvent mqEvent =
|
||||
event as html.MediaQueryListEvent;
|
||||
_brightnessMediaQueryListener = allowInterop((DomEvent event) {
|
||||
final DomMediaQueryListEvent mqEvent =
|
||||
event as DomMediaQueryListEvent;
|
||||
_updatePlatformBrightness(
|
||||
mqEvent.matches! ? ui.Brightness.dark : ui.Brightness.light);
|
||||
};
|
||||
});
|
||||
_brightnessMediaQuery.addListener(_brightnessMediaQueryListener);
|
||||
registerHotRestartListener(() {
|
||||
_removeBrightnessMediaQueryListener();
|
||||
@@ -1184,6 +1184,7 @@ const double _defaultRootFontSize = 16.0;
|
||||
/// Finds the text scale factor of the browser by looking at the computed style
|
||||
/// of the browser's <html> element.
|
||||
double findBrowserTextScaleFactor() {
|
||||
final num fontSize = parseFontSize(html.document.documentElement!) ?? _defaultRootFontSize;
|
||||
final num fontSize = parseFontSize(domDocument.documentElement! as
|
||||
html.Element) ?? _defaultRootFontSize;
|
||||
return fontSize / _defaultRootFontSize;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
// 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 'package:ui/ui.dart' as ui;
|
||||
|
||||
import 'dom.dart';
|
||||
import 'util.dart';
|
||||
|
||||
/// Renders an RRect using path primitives.
|
||||
@@ -183,7 +183,7 @@ abstract class RRectRenderer {
|
||||
|
||||
/// Renders RRect to a 2d canvas.
|
||||
class RRectToCanvasRenderer extends RRectRenderer {
|
||||
final html.CanvasRenderingContext2D context;
|
||||
final DomCanvasRenderingContext2D context;
|
||||
RRectToCanvasRenderer(this.context);
|
||||
@override
|
||||
void beginPath() {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../dom.dart';
|
||||
import '../html/bitmap_canvas.dart';
|
||||
import '../html/painting.dart';
|
||||
import 'canvas_paragraph.dart';
|
||||
|
||||
@@ -695,7 +695,7 @@ bool? _ellipseFeatureDetected;
|
||||
|
||||
/// Draws CanvasElement ellipse with fallback.
|
||||
void drawEllipse(
|
||||
html.CanvasRenderingContext2D context,
|
||||
DomCanvasRenderingContext2D context,
|
||||
double centerX,
|
||||
double centerY,
|
||||
double radiusX,
|
||||
|
||||
Reference in New Issue
Block a user