[engine, web] return switch expressions in many places (#162336)

This commit is contained in:
Kevin Moore 2025-01-28 20:07:45 -06:00 committed by GitHub
parent 60182c5e21
commit 1ba0561963
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 128 additions and 264 deletions

View File

@ -269,14 +269,10 @@ const List<String> kAllBrowserNames = <String>[kChrome, kEdge, kFirefox, kSafari
/// ///
/// The [browserName] matches the browser name passed as the `--browser` option. /// The [browserName] matches the browser name passed as the `--browser` option.
BrowserEnvironment getBrowserEnvironment(BrowserName browserName, {required bool useDwarf}) { BrowserEnvironment getBrowserEnvironment(BrowserName browserName, {required bool useDwarf}) {
switch (browserName) { return switch (browserName) {
case BrowserName.chrome: BrowserName.chrome => ChromeEnvironment(useDwarf: useDwarf),
return ChromeEnvironment(useDwarf: useDwarf); BrowserName.edge => EdgeEnvironment(),
case BrowserName.edge: BrowserName.firefox => FirefoxEnvironment(),
return EdgeEnvironment(); BrowserName.safari => SafariMacOsEnvironment(),
case BrowserName.firefox: };
return FirefoxEnvironment();
case BrowserName.safari:
return SafariMacOsEnvironment();
}
} }

View File

@ -64,22 +64,20 @@ class CompileBundleStep implements PipelineStep {
} }
TestCompiler _createCompiler(CompileConfiguration config) { TestCompiler _createCompiler(CompileConfiguration config) {
switch (config.compiler) { return switch (config.compiler) {
case Compiler.dart2js: Compiler.dart2js => Dart2JSCompiler(
return Dart2JSCompiler(
testSetDirectory, testSetDirectory,
outputBundleDirectory, outputBundleDirectory,
renderer: config.renderer, renderer: config.renderer,
isVerbose: isVerbose, isVerbose: isVerbose,
); ),
case Compiler.dart2wasm: Compiler.dart2wasm => Dart2WasmCompiler(
return Dart2WasmCompiler(
testSetDirectory, testSetDirectory,
outputBundleDirectory, outputBundleDirectory,
renderer: config.renderer, renderer: config.renderer,
isVerbose: isVerbose, isVerbose: isVerbose,
); ),
} };
} }
@override @override

View File

@ -477,14 +477,11 @@ class BrowserPlatform extends PlatformPlugin {
} }
String getCanvasKitVariant() { String getCanvasKitVariant() {
switch (suite.runConfig.variant) { return switch (suite.runConfig.variant) {
case CanvasKitVariant.full: CanvasKitVariant.full => 'full',
return 'full'; CanvasKitVariant.chromium => 'chromium',
case CanvasKitVariant.chromium: null => 'auto',
return 'chromium'; };
case null:
return 'auto';
}
} }
String _makeBuildConfigString(String scriptBase, CompileConfiguration config) { String _makeBuildConfigString(String scriptBase, CompileConfiguration config) {

View File

@ -133,55 +133,33 @@ class KeyData {
// JavaScript only support 32-bit bitwise operations and needs to use // JavaScript only support 32-bit bitwise operations and needs to use
// division instead. // division instead.
final int planeNum = (logical / 0x100000000).floor(); final int planeNum = (logical / 0x100000000).floor();
final String planeDescription = final String planeDescription = switch (planeNum) {
(() { 0x000 => ' (Unicode)',
switch (planeNum) { 0x001 => ' (Unprintable)',
case 0x000: 0x002 => ' (Flutter)',
return ' (Unicode)'; 0x011 => ' (Android)',
case 0x001: 0x012 => ' (Fuchsia)',
return ' (Unprintable)'; 0x013 => ' (iOS)',
case 0x002: 0x014 => ' (macOS)',
return ' (Flutter)'; 0x015 => ' (GTK)',
case 0x011: 0x016 => ' (Windows)',
return ' (Android)'; 0x017 => ' (Web)',
case 0x012: 0x018 => ' (GLFW)',
return ' (Fuchsia)'; _ => '',
case 0x013: };
return ' (iOS)';
case 0x014:
return ' (macOS)';
case 0x015:
return ' (GTK)';
case 0x016:
return ' (Windows)';
case 0x017:
return ' (Web)';
case 0x018:
return ' (GLFW)';
}
return '';
})();
return '$result$planeDescription'; return '$result$planeDescription';
} }
String? _escapeCharacter() { String? _escapeCharacter() {
if (character == null) { return switch (character) {
return '<none>'; null => '<none>',
} '\n' => r'"\n"',
switch (character!) { '\t' => r'"\t"',
case '\n': '\r' => r'"\r"',
return r'"\n"'; '\b' => r'"\b"',
case '\t': '\f' => r'"\f"',
return r'"\t"'; _ => '"$character"',
case '\r': };
return r'"\r"';
case '\b':
return r'"\b"';
case '\f':
return r'"\f"';
default:
return '"$character"';
}
} }
String? _quotedCharCode() { String? _quotedCharCode() {

View File

@ -705,15 +705,12 @@ Future<void> _decodeImageFromListAsync(Uint8List list, ImageDecoderCallback call
// to right, then from top to down. The order of the 4 bytes of pixels is // to right, then from top to down. The order of the 4 bytes of pixels is
// decided by `format`. // decided by `format`.
Future<Codec> createBmp(Uint8List pixels, int width, int height, int rowBytes, PixelFormat format) { Future<Codec> createBmp(Uint8List pixels, int width, int height, int rowBytes, PixelFormat format) {
late bool swapRedBlue; final bool swapRedBlue = switch (format) {
switch (format) { PixelFormat.bgra8888 => true,
case PixelFormat.bgra8888: PixelFormat.rgba8888 => false,
swapRedBlue = true; PixelFormat.rgbaFloat32 =>
case PixelFormat.rgba8888: throw UnimplementedError('RGB conversion from rgbaFloat32 data is not implemented'),
swapRedBlue = false; };
case PixelFormat.rgbaFloat32:
throw UnimplementedError('RGB conversion from rgbaFloat32 data is not implemented');
}
// See https://en.wikipedia.org/wiki/BMP_file_format for format examples. // See https://en.wikipedia.org/wiki/BMP_file_format for format examples.
// The header is in the 108-byte BITMAPV4HEADER format, or as called by // The header is in the 108-byte BITMAPV4HEADER format, or as called by

View File

@ -911,7 +911,7 @@ class ContextStateHandle {
strokeCap ??= ui.StrokeCap.butt; strokeCap ??= ui.StrokeCap.butt;
if (strokeCap != _currentStrokeCap) { if (strokeCap != _currentStrokeCap) {
_currentStrokeCap = strokeCap; _currentStrokeCap = strokeCap;
context.lineCap = stringForStrokeCap(strokeCap)!; context.lineCap = strokeCap.name;
} }
} }
@ -928,7 +928,7 @@ class ContextStateHandle {
strokeJoin ??= ui.StrokeJoin.miter; strokeJoin ??= ui.StrokeJoin.miter;
if (strokeJoin != _currentStrokeJoin) { if (strokeJoin != _currentStrokeJoin) {
_currentStrokeJoin = strokeJoin; _currentStrokeJoin = strokeJoin;
context.lineJoin = stringForStrokeJoin(strokeJoin); context.lineJoin = strokeJoin.name;
} }
} }

View File

@ -3431,17 +3431,14 @@ String get _canvasKitBaseUrl => configuration.canvasKitBaseUrl;
@visibleForTesting @visibleForTesting
List<String> getCanvasKitJsFileNames(CanvasKitVariant variant) { List<String> getCanvasKitJsFileNames(CanvasKitVariant variant) {
switch (variant) { return switch (variant) {
case CanvasKitVariant.auto: CanvasKitVariant.auto => <String>[
return <String>[
if (_enableCanvasKitChromiumInAutoMode) _kChromiumCanvasKitJsFileName, if (_enableCanvasKitChromiumInAutoMode) _kChromiumCanvasKitJsFileName,
_kFullCanvasKitJsFileName, _kFullCanvasKitJsFileName,
]; ],
case CanvasKitVariant.full: CanvasKitVariant.full => <String>[_kFullCanvasKitJsFileName],
return <String>[_kFullCanvasKitJsFileName]; CanvasKitVariant.chromium => <String>[_kChromiumCanvasKitJsFileName],
case CanvasKitVariant.chromium: };
return <String>[_kChromiumCanvasKitJsFileName];
}
} }
Iterable<String> get _canvasKitJsUrls { Iterable<String> get _canvasKitJsUrls {

View File

@ -614,12 +614,10 @@ class HtmlViewEmbedder {
} }
DomElement _getElement(RenderingEntity entity) { DomElement _getElement(RenderingEntity entity) {
switch (entity) { return switch (entity) {
case RenderingRenderCanvas(): RenderingRenderCanvas() => entity.displayCanvas!.hostElement,
return entity.displayCanvas!.hostElement; RenderingPlatformView() => _viewClipChains[entity.viewId]!.root,
case RenderingPlatformView(): };
return _viewClipChains[entity.viewId]!.root;
}
} }
/// Returns a [List] of ints mapping elements from the [next] rendering to /// Returns a [List] of ints mapping elements from the [next] rendering to
@ -796,18 +794,13 @@ class Mutator {
return false; return false;
} }
switch (type) { return switch (type) {
case MutatorType.clipRect: MutatorType.clipRect => rect == typedOther.rect,
return rect == typedOther.rect; MutatorType.clipRRect => rrect == typedOther.rrect,
case MutatorType.clipRRect: MutatorType.clipPath => path == typedOther.path,
return rrect == typedOther.rrect; MutatorType.transform => matrix == typedOther.matrix,
case MutatorType.clipPath: MutatorType.opacity => alpha == typedOther.alpha,
return path == typedOther.path; };
case MutatorType.transform:
return matrix == typedOther.matrix;
case MutatorType.opacity:
return alpha == typedOther.alpha;
}
} }
@override @override

View File

@ -899,13 +899,10 @@ class CkParagraph implements ui.Paragraph {
@override @override
ui.TextRange getWordBoundary(ui.TextPosition position) { ui.TextRange getWordBoundary(ui.TextPosition position) {
assert(!_disposed, 'Paragraph has been disposed.'); assert(!_disposed, 'Paragraph has been disposed.');
final int characterPosition; final int characterPosition = switch (position.affinity) {
switch (position.affinity) { ui.TextAffinity.upstream => position.offset - 1,
case ui.TextAffinity.upstream: ui.TextAffinity.downstream => position.offset,
characterPosition = position.offset - 1; };
case ui.TextAffinity.downstream:
characterPosition = position.offset;
}
final SkTextRange skRange = skiaObject.getWordBoundary(characterPosition.toDouble()); final SkTextRange skRange = skiaObject.getWordBoundary(characterPosition.toDouble());
return ui.TextRange(start: skRange.start.toInt(), end: skRange.end.toInt()); return ui.TextRange(start: skRange.start.toInt(), end: skRange.end.toInt());
} }

View File

@ -1323,31 +1323,6 @@ SvgBlendMode? blendModeToSvgEnum(ui.BlendMode? blendMode) {
} }
} }
String? stringForStrokeCap(ui.StrokeCap? strokeCap) {
if (strokeCap == null) {
return null;
}
switch (strokeCap) {
case ui.StrokeCap.butt:
return 'butt';
case ui.StrokeCap.round:
return 'round';
case ui.StrokeCap.square:
return 'square';
}
}
String stringForStrokeJoin(ui.StrokeJoin strokeJoin) {
switch (strokeJoin) {
case ui.StrokeJoin.round:
return 'round';
case ui.StrokeJoin.bevel:
return 'bevel';
case ui.StrokeJoin.miter:
return 'miter';
}
}
/// Clips the content element against a stack of clip operations and returns /// Clips the content element against a stack of clip operations and returns
/// root of a tree that contains content node. /// root of a tree that contains content node.
/// ///

View File

@ -14,7 +14,6 @@ import '../svg.dart';
import '../text/canvas_paragraph.dart'; import '../text/canvas_paragraph.dart';
import '../util.dart'; import '../util.dart';
import '../vector_math.dart'; import '../vector_math.dart';
import 'bitmap_canvas.dart';
import 'painting.dart'; import 'painting.dart';
import 'path/path.dart'; import 'path/path.dart';
import 'path/path_to_svg.dart'; import 'path/path_to_svg.dart';
@ -351,8 +350,8 @@ SVGSVGElement pathToSvgElement(SurfacePath path, SurfacePaintData paint) {
paint.strokeWidth != null)) { paint.strokeWidth != null)) {
svgPath.setAttribute('stroke', colorValueToCssString(paint.color)); svgPath.setAttribute('stroke', colorValueToCssString(paint.color));
svgPath.setAttribute('stroke-width', '${paint.strokeWidth ?? 1.0}'); svgPath.setAttribute('stroke-width', '${paint.strokeWidth ?? 1.0}');
if (paint.strokeCap != null) { if (paint.strokeCap case final value?) {
svgPath.setAttribute('stroke-linecap', '${stringForStrokeCap(paint.strokeCap)}'); svgPath.setAttribute('stroke-linecap', value.name);
} }
svgPath.setAttribute('fill', 'none'); svgPath.setAttribute('fill', 'none');
} else { } else {

View File

@ -453,14 +453,11 @@ class StandardMessageCodec implements MessageCodec<dynamic> {
/// [readValueOfType]. /// [readValueOfType].
int readSize(ReadBuffer buffer) { int readSize(ReadBuffer buffer) {
final int value = buffer.getUint8(); final int value = buffer.getUint8();
switch (value) { return switch (value) {
case 254: 254 => buffer.getUint16(),
return buffer.getUint16(); 255 => buffer.getUint32(),
case 255: _ => value,
return buffer.getUint32(); };
default:
return value;
}
} }
} }

View File

@ -118,33 +118,20 @@ enum UniformType {
} }
UniformType? uniformTypeFromJson(int value) { UniformType? uniformTypeFromJson(int value) {
switch (value) { return switch (value) {
case 0: 0 => UniformType.Boolean,
return UniformType.Boolean; 1 => UniformType.SByte,
case 1: 2 => UniformType.UByte,
return UniformType.SByte; 3 => UniformType.Short,
case 2: 4 => UniformType.UShort,
return UniformType.UByte; 5 => UniformType.Int,
case 3: 6 => UniformType.Uint,
return UniformType.Short; 7 => UniformType.Int64,
case 4: 8 => UniformType.Uint64,
return UniformType.UShort; 9 => UniformType.Half,
case 5: 10 => UniformType.Float,
return UniformType.Int; 11 => UniformType.Double,
case 6: 12 => UniformType.SampledImage,
return UniformType.Uint; _ => null,
case 7: };
return UniformType.Int64;
case 8:
return UniformType.Uint64;
case 9:
return UniformType.Half;
case 10:
return UniformType.Float;
case 11:
return UniformType.Double;
case 12:
return UniformType.SampledImage;
}
return null;
} }

View File

@ -225,13 +225,10 @@ class CanvasParagraph implements ui.Paragraph {
@override @override
ui.TextRange getWordBoundary(ui.TextPosition position) { ui.TextRange getWordBoundary(ui.TextPosition position) {
final int characterPosition; final int characterPosition = switch (position.affinity) {
switch (position.affinity) { ui.TextAffinity.upstream => position.offset - 1,
case ui.TextAffinity.upstream: ui.TextAffinity.downstream => position.offset,
characterPosition = position.offset - 1; };
case ui.TextAffinity.downstream:
characterPosition = position.offset;
}
final int start = WordBreaker.prevBreakIndex(plainText, characterPosition + 1); final int start = WordBreaker.prevBreakIndex(plainText, characterPosition + 1);
final int end = WordBreaker.nextBreakIndex(plainText, characterPosition); final int end = WordBreaker.nextBreakIndex(plainText, characterPosition);
return ui.TextRange(start: start, end: end); return ui.TextRange(start: start, end: end);

View File

@ -587,18 +587,13 @@ class LineBuilder {
final double emptySpace = maxWidth - width; final double emptySpace = maxWidth - width;
final ui.TextAlign textAlign = paragraph.paragraphStyle.effectiveTextAlign; final ui.TextAlign textAlign = paragraph.paragraphStyle.effectiveTextAlign;
switch (textAlign) { return switch (textAlign) {
case ui.TextAlign.center: ui.TextAlign.center => emptySpace / 2.0,
return emptySpace / 2.0; ui.TextAlign.right => emptySpace,
case ui.TextAlign.right: ui.TextAlign.start => _paragraphDirection == ui.TextDirection.rtl ? emptySpace : 0.0,
return emptySpace; ui.TextAlign.end => _paragraphDirection == ui.TextDirection.rtl ? 0.0 : emptySpace,
case ui.TextAlign.start: _ => 0.0,
return _paragraphDirection == ui.TextDirection.rtl ? emptySpace : 0.0; };
case ui.TextAlign.end:
return _paragraphDirection == ui.TextDirection.rtl ? 0.0 : emptySpace;
default:
return 0.0;
}
} }
bool get isOverflowing => width > maxWidth; bool get isOverflowing => width > maxWidth;

View File

@ -1108,26 +1108,11 @@ String? _textDecorationToCssString(
} }
} }
if (decorationStyle != null) { if (decorationStyle != null) {
decorations.write(_decorationStyleToCssString(decorationStyle)); decorations.write(decorationStyle.name);
} }
return decorations.isEmpty ? null : decorations.toString(); return decorations.isEmpty ? null : decorations.toString();
} }
String? _decorationStyleToCssString(ui.TextDecorationStyle decorationStyle) {
switch (decorationStyle) {
case ui.TextDecorationStyle.dashed:
return 'dashed';
case ui.TextDecorationStyle.dotted:
return 'dotted';
case ui.TextDecorationStyle.double:
return 'double';
case ui.TextDecorationStyle.solid:
return 'solid';
case ui.TextDecorationStyle.wavy:
return 'wavy';
}
}
/// Converts [align] to its corresponding CSS value. /// Converts [align] to its corresponding CSS value.
/// ///
/// This value is used as the "text-align" CSS property, e.g.: /// This value is used as the "text-align" CSS property, e.g.:

View File

@ -166,16 +166,11 @@ FragmentFlow _getFragmentFlow(String text, int i) {
} }
final ui.TextDirection? textDirection = _textDirectionLookup.findForChar(codePoint); final ui.TextDirection? textDirection = _textDirectionLookup.findForChar(codePoint);
switch (textDirection) { return switch (textDirection) {
case ui.TextDirection.ltr: ui.TextDirection.ltr => FragmentFlow.ltr,
return FragmentFlow.ltr; ui.TextDirection.rtl => FragmentFlow.rtl,
null => FragmentFlow.sandwich,
case ui.TextDirection.rtl: };
return FragmentFlow.rtl;
case null:
return FragmentFlow.sandwich;
}
} }
bool _isDigit(int codePoint) { bool _isDigit(int codePoint) {

View File

@ -345,20 +345,14 @@ String colorValueToCssString(int value) {
if ((0xff000000 & value) == 0xff000000) { if ((0xff000000 & value) == 0xff000000) {
final String hexValue = (value & 0xFFFFFF).toRadixString(16); final String hexValue = (value & 0xFFFFFF).toRadixString(16);
final int hexValueLength = hexValue.length; final int hexValueLength = hexValue.length;
switch (hexValueLength) { return switch (hexValueLength) {
case 1: 1 => '#00000$hexValue',
return '#00000$hexValue'; 2 => '#0000$hexValue',
case 2: 3 => '#000$hexValue',
return '#0000$hexValue'; 4 => '#00$hexValue',
case 3: 5 => '#0$hexValue',
return '#000$hexValue'; _ => '#$hexValue',
case 4: };
return '#00$hexValue';
case 5:
return '#0$hexValue';
default:
return '#$hexValue';
}
} else { } else {
final double alpha = ((value >> 24) & 0xFF) / 255.0; final double alpha = ((value >> 24) & 0xFF) / 255.0;
final StringBuffer sb = StringBuffer(); final StringBuffer sb = StringBuffer();
@ -889,20 +883,7 @@ class LruCache<K extends Object, V extends Object> {
} }
/// Returns the VM-compatible string for the tile mode. /// Returns the VM-compatible string for the tile mode.
String tileModeString(ui.TileMode? tileMode) { String tileModeString(ui.TileMode? tileMode) => tileMode?.name ?? 'unspecified';
switch (tileMode) {
case ui.TileMode.clamp:
return 'clamp';
case ui.TileMode.mirror:
return 'mirror';
case ui.TileMode.repeated:
return 'repeated';
case ui.TileMode.decal:
return 'decal';
case null:
return 'unspecified';
}
}
/// A size where both the width and height are integers. /// A size where both the width and height are integers.
class BitmapSize { class BitmapSize {