From 38194c973de785c943a57467a484e7570cd5ce55 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 28 Jul 2022 17:30:05 -0700 Subject: [PATCH] API documentation cleanup (mainly code snippet fixes) (flutter/engine#34974) --- engine/src/flutter/lib/ui/annotations.dart | 5 +- .../src/flutter/lib/ui/channel_buffers.dart | 16 +- engine/src/flutter/lib/ui/hash_codes.dart | 37 +++- engine/src/flutter/lib/ui/natives.dart | 4 +- engine/src/flutter/lib/ui/painting.dart | 177 ++++++++++-------- 5 files changed, 146 insertions(+), 93 deletions(-) diff --git a/engine/src/flutter/lib/ui/annotations.dart b/engine/src/flutter/lib/ui/annotations.dart index 358c277a50..6722ba4007 100644 --- a/engine/src/flutter/lib/ui/annotations.dart +++ b/engine/src/flutter/lib/ui/annotations.dart @@ -22,14 +22,15 @@ part of dart.ui; /// For example, in the following class the `toString` method will remain as /// `return _buffer.toString();`, even if the `--delete-tostring-package-uri` /// option would otherwise apply and replace it with `return super.toString()`. +/// (By convention, `dart:ui` is usually imported `as ui`, hence the prefix.) /// /// ```dart /// class MyStringBuffer { -/// StringBuffer _buffer = StringBuffer(); +/// final StringBuffer _buffer = StringBuffer(); /// /// // ... /// -/// @keepToString +/// @ui.keepToString /// @override /// String toString() { /// return _buffer.toString(); diff --git a/engine/src/flutter/lib/ui/channel_buffers.dart b/engine/src/flutter/lib/ui/channel_buffers.dart index 96224fbf99..511adf8f5f 100644 --- a/engine/src/flutter/lib/ui/channel_buffers.dart +++ b/engine/src/flutter/lib/ui/channel_buffers.dart @@ -246,15 +246,13 @@ class _Channel { /// message overflows, in debug mode, a message is printed to the /// console. The message looks like the following: /// -/// ``` -/// A message on the com.example channel was discarded before it could be -/// handled. -/// This happens when a plugin sends messages to the framework side before the -/// framework has had an opportunity to register a listener. See the -/// ChannelBuffers API documentation for details on how to configure the channel -/// to expect more messages, or to expect messages to get discarded: -/// https://api.flutter.dev/flutter/dart-ui/ChannelBuffers-class.html -/// ``` +/// > A message on the com.example channel was discarded before it could be +/// > handled. +/// > This happens when a plugin sends messages to the framework side before the +/// > framework has had an opportunity to register a listener. See the +/// > ChannelBuffers API documentation for details on how to configure the channel +/// > to expect more messages, or to expect messages to get discarded: +/// > https://api.flutter.dev/flutter/dart-ui/ChannelBuffers-class.html /// /// There are tradeoffs associated with any size. The correct size /// should be chosen for the semantics of the channel. To change the diff --git a/engine/src/flutter/lib/ui/hash_codes.dart b/engine/src/flutter/lib/ui/hash_codes.dart index 6bf51dfa45..77cf0fd740 100644 --- a/engine/src/flutter/lib/ui/hash_codes.dart +++ b/engine/src/flutter/lib/ui/hash_codes.dart @@ -3,6 +3,12 @@ // found in the LICENSE file. part of dart.ui; +// Examples can assume: +// int foo = 0; +// int bar = 0; +// List quux = []; +// int baz = 0; + class _HashEnd { const _HashEnd(); } const _HashEnd _hashEnd = _HashEnd(); @@ -37,7 +43,26 @@ class _Jenkins { /// For example: /// /// ```dart -/// int hashCode => hashValues(foo, bar, hashList(quux), baz); +/// int get hashCode => hashValues(foo, bar, hashList(quux), baz); // ignore: deprecated_member_use +/// ``` +/// +/// ## Deprecation +/// +/// This function has been replaced by [Object.hash], so that it can be used +/// outside of Flutter as well. The new function is a drop-in replacement. +/// +/// The [hashList] function has also been replaced, [Object.hashAll] is the new +/// function. The example above therefore is better written as: +/// +/// ```dart +/// int get hashCode => Object.hash(foo, bar, Object.hashAll(quux), baz); +/// ``` +/// +/// If `quux` in this example was nullable, then it would need special handling, +/// because [Object.hashAll]'s argument is not nullable: +/// +/// ```dart +/// int get hashCode => Object.hash(foo, bar, quux == null ? null : Object.hashAll(quux), baz); /// ``` @Deprecated( 'Use Object.hash() instead. ' @@ -115,6 +140,16 @@ int hashValues( /// Combine the [Object.hashCode] values of an arbitrary number of objects from /// an [Iterable] into one value. This function will return the same value if /// given null as if given an empty list. +/// +/// ## Deprecation +/// +/// This function has been replaced by [Object.hashAll], so that it can be used +/// outside of Flutter as well. The new function is a drop-in replacement, except +/// that the argument must not be null. +/// +/// There is also a new function, [Object.hashAllUnordered], which is similar +/// but returns the same hash code regardless of the order of the elements in +/// the provided iterable. @Deprecated( 'Use Object.hashAll() or Object.hashAllUnordered() instead. ' 'This feature was deprecated in v3.1.0-0.0.pre.897' diff --git a/engine/src/flutter/lib/ui/natives.dart b/engine/src/flutter/lib/ui/natives.dart index 5c0ca3a7aa..4d924b1fbf 100644 --- a/engine/src/flutter/lib/ui/natives.dart +++ b/engine/src/flutter/lib/ui/natives.dart @@ -100,7 +100,7 @@ void _setupHooks() { /// /// Here are some examples: /// -/// ``` +/// ```csv /// dart:core,Duration,get:inMilliseconds /// package:flutter/src/widgets/binding.dart,::,runApp /// file:///.../my_app.dart,::,main @@ -135,6 +135,6 @@ typedef _ScheduleImmediateClosure = void Function(void Function()); _ScheduleImmediateClosure _getScheduleMicrotaskClosure() => _scheduleMicrotask; // Used internally to indicate whether the Engine is using Impeller for -// rendering +// rendering. @pragma('vm:entry-point') bool _impellerEnabled = false; diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 58de15195e..9d0876ea25 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -3,6 +3,13 @@ // found in the LICENSE file. part of dart.ui; +// Examples can assume: +// // (for the example in Color) +// // ignore_for_file: use_full_hex_values_for_flutter_colors +// late ui.Image _image; +// dynamic _cacheImage(dynamic _, [dynamic __]) { } +// dynamic _drawImage(dynamic _, [dynamic __]) { } + // Some methods in this file assert that their arguments are not null. These // asserts are just to improve the error messages; they should only cover // arguments that are either dereferenced _in Dart_, before being passed to the @@ -60,10 +67,10 @@ Color _scaleAlpha(Color a, double factor) { /// Here are some ways it could be constructed: /// /// ```dart -/// Color c = const Color(0xFF42A5F5); -/// Color c = const Color.fromARGB(0xFF, 0x42, 0xA5, 0xF5); -/// Color c = const Color.fromARGB(255, 66, 165, 245); -/// Color c = const Color.fromRGBO(66, 165, 245, 1.0); +/// Color c1 = const Color(0xFF42A5F5); +/// Color c2 = const Color.fromARGB(0xFF, 0x42, 0xA5, 0xF5); +/// Color c3 = const Color.fromARGB(255, 66, 165, 245); +/// Color c4 = const Color.fromRGBO(66, 165, 245, 1.0); /// ``` /// /// If you are having a problem with `Color` wherein it seems your color is just @@ -1418,15 +1425,11 @@ class Paint { /// [ImageFilter.blur]: /// /// ```dart - /// import 'dart:ui' as ui; - /// - /// ui.Image image; - /// /// void paint(Canvas canvas, Size size) { /// canvas.drawImage( - /// image, - /// Offset.zero, - /// Paint()..imageFilter = ui.ImageFilter.blur(sigmaX: .5, sigmaY: .5), + /// _image, + /// ui.Offset.zero, + /// Paint()..imageFilter = ui.ImageFilter.blur(sigmaX: 0.5, sigmaY: 0.5), /// ); /// } /// ``` @@ -1738,6 +1741,8 @@ class Image { /// /// ```dart /// import 'dart:async'; + /// import 'dart:typed_data'; + /// import 'dart:ui'; /// /// Future _loadImage(int width, int height) { /// final Completer completer = Completer(); @@ -1775,7 +1780,7 @@ class Image { /// } /// /// class MyImageHolder { - /// MyImageLoader(this.image); + /// MyImageHolder(this.image); /// /// final Image image; /// @@ -1890,8 +1895,8 @@ typedef ImageDecoderCallback = void Function(Image result); /// /// ```dart /// /// BAD -/// Future nextFrameRoutine(Codec codec) async { -/// final FrameInfo frameInfo = await codec.getNextFrame(); +/// Future nextFrameRoutine(ui.Codec codec) async { +/// final ui.FrameInfo frameInfo = await codec.getNextFrame(); /// _cacheImage(frameInfo); /// // ERROR - _cacheImage is now responsible for disposing the image, and /// // the image may not be available any more for this drawing routine. @@ -1906,8 +1911,8 @@ typedef ImageDecoderCallback = void Function(Image result); /// /// ```dart /// /// GOOD -/// Future nextFrameRoutine(Codec codec) async { -/// final FrameInfo frameInfo = await codec.getNextFrame(); +/// Future nextFrameRoutine(ui.Codec codec) async { +/// final ui.FrameInfo frameInfo = await codec.getNextFrame(); /// _cacheImage(frameInfo.image.clone(), frameInfo.duration); /// _drawImage(frameInfo.image.clone(), frameInfo.duration); /// // This method is done with its handle, and has passed handles to its @@ -3112,18 +3117,16 @@ class ColorFilter implements ImageFilter { /// Every pixel's color value, repsented as an `[R, G, B, A]`, is matrix /// multiplied to create a new color: /// - /// ```text - /// | R' | | a00 a01 a02 a03 a04 | | R | - /// | G' | | a10 a11 a22 a33 a44 | | G | - /// | B' | = | a20 a21 a22 a33 a44 | * | B | - /// | A' | | a30 a31 a22 a33 a44 | | A | - /// | 1 | | 0 0 0 0 1 | | 1 | - /// ``` + /// | R' | | a00 a01 a02 a03 a04 | | R | + /// | G' | | a10 a11 a22 a33 a44 | | G | + /// | B' | = | a20 a21 a22 a33 a44 | * | B | + /// | A' | | a30 a31 a22 a33 a44 | | A | + /// | 1 | | 0 0 0 0 1 | | 1 | /// /// The matrix is in row-major order and the translation column is specified /// in unnormalized, 0...255, space. For example, the identity matrix is: /// - /// ``` + /// ```dart /// const ColorFilter identity = ColorFilter.matrix([ /// 1, 0, 0, 0, 0, /// 0, 1, 0, 0, 0, @@ -3136,7 +3139,7 @@ class ColorFilter implements ImageFilter { /// /// An inversion color matrix: /// - /// ``` + /// ```dart /// const ColorFilter invert = ColorFilter.matrix([ /// -1, 0, 0, 0, 255, /// 0, -1, 0, 0, 255, @@ -3147,7 +3150,7 @@ class ColorFilter implements ImageFilter { /// /// A sepia-toned color matrix (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#sepiaEquivalent)): /// - /// ``` + /// ```dart /// const ColorFilter sepia = ColorFilter.matrix([ /// 0.393, 0.769, 0.189, 0, 0, /// 0.349, 0.686, 0.168, 0, 0, @@ -3158,7 +3161,7 @@ class ColorFilter implements ImageFilter { /// /// A greyscale color filter (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent)): /// - /// ``` + /// ```dart /// const ColorFilter greyscale = ColorFilter.matrix([ /// 0.2126, 0.7152, 0.0722, 0, 0, /// 0.2126, 0.7152, 0.0722, 0, 0, @@ -4123,7 +4126,7 @@ class FragmentProgram extends NativeFieldWrapperClass1 { /// /// Consider the following snippit of GLSL code. /// - /// ``` + /// ```glsl /// layout (location = 0) uniform float a; /// layout (location = 1) uniform vec2 b; /// layout (location = 2) uniform vec3 c; @@ -4147,7 +4150,7 @@ class FragmentProgram extends NativeFieldWrapperClass1 { /// /// Consider the following snippit of GLSL code. /// - /// ``` + /// ```glsl /// layout (location = 0) uniform sampler2D a; /// layout (location = 1) uniform sampler2D b; /// ``` @@ -4725,12 +4728,15 @@ class Canvas extends NativeFieldWrapperClass1 { /// following two clip method calls: /// /// ```dart - /// clipPath(Path() - /// ..addRect(const Rect.fromLTRB(10, 10, 20, 20)) - /// ..addRect(const Rect.fromLTRB(80, 80, 100, 100))); - /// clipPath(Path() - /// ..addRect(const Rect.fromLTRB(80, 10, 100, 20)) - /// ..addRect(const Rect.fromLTRB(10, 80, 20, 100))); + /// void draw(Canvas canvas) { + /// canvas.clipPath(Path() + /// ..addRect(const Rect.fromLTRB(10, 10, 20, 20)) + /// ..addRect(const Rect.fromLTRB(80, 80, 100, 100))); + /// canvas.clipPath(Path() + /// ..addRect(const Rect.fromLTRB(80, 10, 100, 20)) + /// ..addRect(const Rect.fromLTRB(10, 80, 20, 100))); + /// ... + /// } /// ``` /// /// After executing both of those calls there is no area left in which to draw @@ -5178,12 +5184,14 @@ class Canvas extends NativeFieldWrapperClass1 { /// /// ```dart /// class Sprite { + /// Sprite(this.index, this.center); /// int index; - /// double centerX; - /// double centerY; + /// Offset center; /// } /// /// class MyPainter extends CustomPainter { + /// MyPainter(this.spriteAtlas, this.allSprites); + /// /// // assume spriteAtlas contains N 10x10 sprites side by side in a (N*10)x10 image /// ui.Image spriteAtlas; /// List allSprites; @@ -5200,8 +5208,8 @@ class Canvas extends NativeFieldWrapperClass1 { /// anchorX: 5.0, /// anchorY: 5.0, /// // Location at which to draw the center of the sprite - /// translateX: sprite.centerX, - /// translateY: sprite.centerY, + /// translateX: sprite.center.dx, + /// translateY: sprite.center.dy, /// ), /// ], [ /// for (Sprite sprite in allSprites) @@ -5217,14 +5225,16 @@ class Canvas extends NativeFieldWrapperClass1 { /// /// ```dart /// class Sprite { + /// Sprite(this.index, this.center, this.alpha, this.rotation); /// int index; - /// double centerX; - /// double centerY; + /// Offset center; /// int alpha; /// double rotation; /// } /// /// class MyPainter extends CustomPainter { + /// MyPainter(this.spriteAtlas, this.allSprites); + /// /// // assume spriteAtlas contains N 10x10 sprites side by side in a (N*10)x10 image /// ui.Image spriteAtlas; /// List allSprites; @@ -5241,8 +5251,8 @@ class Canvas extends NativeFieldWrapperClass1 { /// anchorX: 5.0, /// anchorY: 5.0, /// // Location at which to draw the center of the sprite - /// translateX: sprite.centerX, - /// translateY: sprite.centerY, + /// translateX: sprite.center.dx, + /// translateY: sprite.center.dy, /// ), /// ], [ /// for (Sprite sprite in allSprites) @@ -5348,12 +5358,14 @@ class Canvas extends NativeFieldWrapperClass1 { /// /// ```dart /// class Sprite { + /// Sprite(this.index, this.center); /// int index; - /// double centerX; - /// double centerY; + /// Offset center; /// } /// /// class MyPainter extends CustomPainter { + /// MyPainter(this.spriteAtlas, this.allSprites); + /// /// // assume spriteAtlas contains N 10x10 sprites side by side in a (N*10)x10 image /// ui.Image spriteAtlas; /// List allSprites; @@ -5367,7 +5379,8 @@ class Canvas extends NativeFieldWrapperClass1 { /// Float32List rectList = Float32List(allSprites.length * 4); /// Float32List transformList = Float32List(allSprites.length * 4); /// for (int i = 0; i < allSprites.length; i++) { - /// final double rectX = sprite.spriteIndex * 10.0; + /// Sprite sprite = allSprites[i]; + /// final double rectX = sprite.index * 10.0; /// rectList[i * 4 + 0] = rectX; /// rectList[i * 4 + 1] = 0.0; /// rectList[i * 4 + 2] = rectX + 10.0; @@ -5379,11 +5392,11 @@ class Canvas extends NativeFieldWrapperClass1 { /// // the necessary values or do the same math directly. /// transformList[i * 4 + 0] = 1.0; /// transformList[i * 4 + 1] = 0.0; - /// transformList[i * 4 + 2] = sprite.centerX - 5.0; - /// transformList[i * 4 + 3] = sprite.centerY - 5.0; + /// transformList[i * 4 + 2] = sprite.center.dx - 5.0; + /// transformList[i * 4 + 3] = sprite.center.dy - 5.0; /// } /// Paint paint = Paint(); - /// canvas.drawAtlas(spriteAtlas, transformList, rectList, null, null, null, paint); + /// canvas.drawRawAtlas(spriteAtlas, transformList, rectList, null, null, null, paint); /// } /// /// ... @@ -5394,14 +5407,16 @@ class Canvas extends NativeFieldWrapperClass1 { /// /// ```dart /// class Sprite { + /// Sprite(this.index, this.center, this.alpha, this.rotation); /// int index; - /// double centerX; - /// double centerY; + /// Offset center; /// int alpha; /// double rotation; /// } /// /// class MyPainter extends CustomPainter { + /// MyPainter(this.spriteAtlas, this.allSprites); + /// /// // assume spriteAtlas contains N 10x10 sprites side by side in a (N*10)x10 image /// ui.Image spriteAtlas; /// List allSprites; @@ -5416,7 +5431,8 @@ class Canvas extends NativeFieldWrapperClass1 { /// Float32List transformList = Float32List(allSprites.length * 4); /// Int32List colorList = Int32List(allSprites.length); /// for (int i = 0; i < allSprites.length; i++) { - /// final double rectX = sprite.spriteIndex * 10.0; + /// Sprite sprite = allSprites[i]; + /// final double rectX = sprite.index * 10.0; /// rectList[i * 4 + 0] = rectX; /// rectList[i * 4 + 1] = 0.0; /// rectList[i * 4 + 2] = rectX + 10.0; @@ -5434,8 +5450,8 @@ class Canvas extends NativeFieldWrapperClass1 { /// anchorX: 5.0, /// anchorY: 5.0, /// // Location at which to draw the center of the sprite - /// translateX: sprite.centerX, - /// translateY: sprite.centerY, + /// translateX: sprite.center.dx, + /// translateY: sprite.center.dy, /// ); /// transformList[i * 4 + 0] = transform.scos; /// transformList[i * 4 + 1] = transform.ssin; @@ -5449,7 +5465,7 @@ class Canvas extends NativeFieldWrapperClass1 { /// colorList[i] = sprite.alpha << 24; /// } /// Paint paint = Paint(); - /// canvas.drawAtlas(spriteAtlas, transformList, rectList, colorList, BlendMode.srcIn, null, paint); + /// canvas.drawRawAtlas(spriteAtlas, transformList, rectList, colorList, BlendMode.srcIn, null, paint); /// } /// /// ... @@ -6081,30 +6097,33 @@ typedef _Callback = void Function(T result); /// failure. typedef _Callbacker = String? Function(_Callback callback); -/// Converts a method that receives a value-returning callback to a method that -/// returns a Future. -/// -/// Return a [String] to cause an [Exception] to be synchronously thrown with -/// that string as a message. -/// -/// If the callback is called with null, the future completes with an error. -/// -/// Example usage: -/// -/// ```dart -/// typedef IntCallback = void Function(int result); -/// -/// String _doSomethingAndCallback(IntCallback callback) { -/// Timer(Duration(seconds: 1), () { callback(1); }); -/// } -/// -/// Future doSomething() { -/// return _futurize(_doSomethingAndCallback); -/// } -/// ``` -// Note: this function is not directly tested so that it remains private, instead an exact -// copy of it has been inlined into the test at lib/ui/fixtures/ui_test.dart. if you change -// this function, then you must update the test. +// Converts a method that receives a value-returning callback to a method that +// returns a Future. +// +// Return a [String] to cause an [Exception] to be synchronously thrown with +// that string as a message. +// +// If the callback is called with null, the future completes with an error. +// +// Example usage: +// +// ```dart +// typedef IntCallback = void Function(int result); +// +// String? _doSomethingAndCallback(IntCallback callback) { +// Timer(const Duration(seconds: 1), () { callback(1); }); +// } +// +// Future doSomething() { +// return _futurize(_doSomethingAndCallback); +// } +// ``` +// +// This function is private and so not directly tested. Instead, an exact copy of it +// has been inlined into the test at lib/ui/fixtures/ui_test.dart. if you change this +// function, then you must update the test. +// +// TODO(ianh): We should either automate the code duplication or just make it public. Future _futurize(_Callbacker callbacker) { final Completer completer = Completer.sync(); // If the callback synchronously throws an error, then synchronously