From 4e6e24f72951afd9abf87eb53971cfec4fb40002 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 13 Feb 2025 15:53:36 -0500 Subject: [PATCH] [web] Cleanup everything HTML from the framework (#162837) Things being removed: - Custom image filter logic in `cupertino/dialog.dart`. - Network image support for the HTML renderer. - Shader warm up guard for the HTML renderer. - Custom caret metrics logic in `text_painter.dart`. --- .../flutter/lib/src/cupertino/dialog.dart | 2 +- .../lib/src/painting/_network_image_web.dart | 33 ---------------- .../lib/src/painting/shader_warm_up.dart | 23 +++++------ .../lib/src/painting/text_painter.dart | 39 ++++++------------- 4 files changed, 22 insertions(+), 75 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index 336453d122..7d870fa5d4 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -684,7 +684,7 @@ class CupertinoPopupSurface extends StatelessWidget { isVibrancePainted = debugIsVibrancePainted; return true; }()); - if ((kIsWeb && !isSkiaWeb) || !isVibrancePainted) { + if (!isVibrancePainted) { if (blurSigma == 0) { return null; } diff --git a/packages/flutter/lib/src/painting/_network_image_web.dart b/packages/flutter/lib/src/painting/_network_image_web.dart index c02726e47d..3cf73c31b6 100644 --- a/packages/flutter/lib/src/painting/_network_image_web.dart +++ b/packages/flutter/lib/src/painting/_network_image_web.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:js_interop'; import 'dart:ui' as ui; -import 'dart:ui_web' as ui_web; import 'package:flutter/foundation.dart'; @@ -134,9 +133,6 @@ class NetworkImage extends image_provider.ImageProvider _loadAsync( NetworkImage key, _SimpleDecoderCallback decode, @@ -176,9 +172,6 @@ class NetworkImage extends image_provider.ImageProvider element and // pass the headers with the request to fetch the image. Since the user has // provided headers, this function should assume the headers are required to @@ -188,32 +181,6 @@ class NetworkImage extends image_provider.ImageProvider tag - // in this case. - - // Resolve the Codec before passing it to - // [MultiFrameImageStreamCompleter] so any errors aren't reported - // twice (once from the MultiFrameImageStreamCompleter) and again - // from the wrapping [ForwardingImageStreamCompleter]. - final Uri resolved = Uri.base.resolve(key.url); - final ui.Codec codec = await ui_web.createImageCodecFromUrl( - resolved, - chunkCallback: (int bytes, int total) { - chunkEvents.add(ImageChunkEvent(cumulativeBytesLoaded: bytes, expectedTotalBytes: total)); - }, - ); - return MultiFrameImageStreamCompleter( - chunkEvents: chunkEvents.stream, - codec: Future.value(codec), - scale: key.scale, - debugLabel: key.url, - informationCollector: _imageStreamInformationCollector(key), - ); - } - switch (webHtmlElementStrategy) { case image_provider.WebHtmlElementStrategy.never: return loadViaDecode(); diff --git a/packages/flutter/lib/src/painting/shader_warm_up.dart b/packages/flutter/lib/src/painting/shader_warm_up.dart index 228ed5a4c4..75a5170f7a 100644 --- a/packages/flutter/lib/src/painting/shader_warm_up.dart +++ b/packages/flutter/lib/src/painting/shader_warm_up.dart @@ -90,20 +90,17 @@ abstract class ShaderWarmUp { await warmUpOnCanvas(canvas); final ui.Picture picture = recorder.endRecording(); assert(debugCaptureShaderWarmUpPicture(picture)); - if (!kIsWeb || isSkiaWeb) { - // Picture.toImage is not implemented on the html renderer. - TimelineTask? debugShaderWarmUpTask; + TimelineTask? debugShaderWarmUpTask; + if (!kReleaseMode) { + debugShaderWarmUpTask = TimelineTask()..start('Warm-up shader'); + } + try { + final ui.Image image = await picture.toImage(size.width.ceil(), size.height.ceil()); + assert(debugCaptureShaderWarmUpImage(image)); + image.dispose(); + } finally { if (!kReleaseMode) { - debugShaderWarmUpTask = TimelineTask()..start('Warm-up shader'); - } - try { - final ui.Image image = await picture.toImage(size.width.ceil(), size.height.ceil()); - assert(debugCaptureShaderWarmUpImage(image)); - image.dispose(); - } finally { - if (!kReleaseMode) { - debugShaderWarmUpTask!.finish(); - } + debugShaderWarmUpTask!.finish(); } } picture.dispose(); diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index bcca79bfd8..58176bca97 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -1528,9 +1528,7 @@ class TextPainter { final _TextPainterLayoutCacheWithOffset cachedLayout = _layoutCache!; // If nothing is laid out, top start is the only reasonable place to place // the cursor. - // The HTML renderer reports numberOfLines == 1 when the text is empty: - // https://github.com/flutter/flutter/issues/143331 - if (cachedLayout.paragraph.numberOfLines < 1 || plainText.isEmpty) { + if (cachedLayout.paragraph.numberOfLines < 1) { // TODO(LongCatIsLooong): assert when an invalid position is given. return null; } @@ -1588,31 +1586,16 @@ class TextPainter { boxHeightStyle: ui.BoxHeightStyle.strut, ); - if (boxes.isNotEmpty) { - final bool anchorToLeft = switch (glyphInfo.writingDirection) { - TextDirection.ltr => anchorToLeadingEdge, - TextDirection.rtl => !anchorToLeadingEdge, - }; - final TextBox box = anchorToLeft ? boxes.first : boxes.last; - metrics = _LineCaretMetrics( - offset: Offset(anchorToLeft ? box.left : box.right, box.top), - writingDirection: box.direction, - height: box.bottom - box.top, - ); - } else { - // Fall back to glyphInfo. This should only happen when using the HTML renderer. - assert(kIsWeb && !isSkiaWeb); - final Rect graphemeBounds = glyphInfo.graphemeClusterLayoutBounds; - final double dx = switch (glyphInfo.writingDirection) { - TextDirection.ltr => anchorToLeadingEdge ? graphemeBounds.left : graphemeBounds.right, - TextDirection.rtl => anchorToLeadingEdge ? graphemeBounds.right : graphemeBounds.left, - }; - metrics = _LineCaretMetrics( - offset: Offset(dx, graphemeBounds.top), - writingDirection: glyphInfo.writingDirection, - height: graphemeBounds.height, - ); - } + final bool anchorToLeft = switch (glyphInfo.writingDirection) { + TextDirection.ltr => anchorToLeadingEdge, + TextDirection.rtl => !anchorToLeadingEdge, + }; + final TextBox box = anchorToLeft ? boxes.first : boxes.last; + metrics = _LineCaretMetrics( + offset: Offset(anchorToLeft ? box.left : box.right, box.top), + writingDirection: box.direction, + height: box.bottom - box.top, + ); cachedLayout._previousCaretPositionKey = caretPositionCacheKey; return _caretMetrics = metrics;