From 7b19e9157d2522489cd3681ad1c826667ab8205a Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 31 Oct 2017 10:39:44 -0700 Subject: [PATCH] Add TextPosition.== and hashCode, and some text API docs (flutter/engine#4283) --- engine/src/flutter/lib/ui/natives.dart | 10 ++-- engine/src/flutter/lib/ui/text.dart | 64 +++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/lib/ui/natives.dart b/engine/src/flutter/lib/ui/natives.dart index 9a9a4d4d45..99d1394005 100644 --- a/engine/src/flutter/lib/ui/natives.dart +++ b/engine/src/flutter/lib/ui/natives.dart @@ -5,7 +5,7 @@ part of dart.ui; // Corelib 'print' implementation. -void _print(arg) { +void _print(dynamic arg) { _Logger._printString(arg.toString()); } @@ -43,13 +43,13 @@ Uri _getBaseURL() => Uri.parse(_baseURL); // Required for gen_snapshot to work correctly. int _isolateId; -_getPrintClosure() => _print; -_getScheduleMicrotaskClosure() => _scheduleMicrotask; -_getGetBaseURLClosure() => _getBaseURL; +Function _getPrintClosure() => _print; +Function _getScheduleMicrotaskClosure() => _scheduleMicrotask; +Function _getGetBaseURLClosure() => _getBaseURL; // Though the "main" symbol is not included in any of the libraries imported // above, the builtin library will be included manually during VM setup. This // symbol is only necessary for precompilation. It is marked as a stanalone // entry point into the VM. This prevents the precompiler from tree shaking // away "main" -_getMainClosure() => main; +Function _getMainClosure() => main; diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index 57ee8cad74..71cd5ed6cf 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -154,17 +154,18 @@ class TextDecoration { /// Draw a line through each line of text static const TextDecoration lineThrough = const TextDecoration._(0x4); + @override bool operator ==(dynamic other) { - if (identical(this, other)) - return true; if (other is! TextDecoration) return false; final TextDecoration typedOther = other; return _mask == typedOther._mask; } + @override int get hashCode => _mask.hashCode; + @override String toString() { if (_mask == 0) return 'TextDecoration.none'; @@ -652,7 +653,10 @@ enum TextDirection { } /// A rectangle enclosing a run of text. +/// +/// This is similar to [Rect] but includes an inherent [TextDirection]. class TextBox { + /// Creates an object that describes a box containing text. const TextBox.fromLTRBD( this.left, this.top, @@ -670,12 +674,16 @@ class TextBox { ) : direction = TextDirection.values[directionIndex]; /// The left edge of the text box, irrespective of direction. + /// + /// To get the leading edge (which may depend on the [direction]), consider [start]. final double left; /// The top edge of the text box. final double top; /// The right edge of the text box, irrespective of direction. + /// + /// To get the trailing edge (which may depend on the [direction]), consider [end]. final double right; /// The bottom edge of the text box. @@ -687,16 +695,25 @@ class TextBox { /// Returns a rect of the same size as this box. Rect toRect() => new Rect.fromLTRB(left, top, right, bottom); - /// The left edge of the box for ltr text; the right edge of the box for rtl text. + /// The [left] edge of the box for left-to-right text; the [right] edge of the box for right-to-left text. + /// + /// See also: + /// + /// * [direction], which specifies the text direction. double get start { return (direction == TextDirection.ltr) ? left : right; } - /// The right edge of the box for ltr text; the left edge of the box for rtl text. + /// The [right] edge of the box for left-to-right text; the [left] edge of the box for right-to-left text. + /// + /// See also: + /// + /// * [direction], which specifies the text direction. double get end { return (direction == TextDirection.ltr) ? right : left; } + @override bool operator ==(dynamic other) { if (identical(this, other)) return true; @@ -710,8 +727,10 @@ class TextBox { && typedOther.direction == direction; } + @override int get hashCode => hashValues(left, top, right, bottom, direction); + @override String toString() => 'TextBox.fromLTRBD(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)}, $direction)'; } @@ -738,15 +757,45 @@ enum TextAffinity { /// A visual position in a string of text. class TextPosition { - const TextPosition({ this.offset, this.affinity: TextAffinity.downstream }); + /// Creates an object representing a particular position in a string. + /// + /// The arguments must not be null (so the [offset] argument is required). + const TextPosition({ + this.offset, + this.affinity: TextAffinity.downstream, + }) : assert(offset != null), + assert(affinity != null); - /// The index of the character just prior to the position. + /// The index of the character that immediately follows the position. + /// + /// For example, given the string `'Hello'`, offset 0 represents the cursor + /// being before the `H`, while offset 5 represents the cursor being just + /// after the `o`. final int offset; /// If the offset has more than one visual location (e.g., occurs at a line /// break), which of the two locations is represented by this position. + /// + /// For example, if the text `'AB'` had a forced line break between the `A` + /// and the `B`, then the downstream affinity at offset 1 represents the + /// cursor being just after the `A` on the first line, while the upstream + /// affinity at offset 1 represents the cursor being just before the `B` on + /// the first line. final TextAffinity affinity; + @override + bool operator ==(dynamic other) { + if (other.runtimeType != runtimeType) + return false; + final TextPosition typedOther = other; + return typedOther.offset == offset + && typedOther.affinity == affinity; + } + + @override + int get hashCode => hashValues(offset, affinity); + + @override String toString() { return '$runtimeType(offset: $offset, affinity: $affinity)'; } @@ -785,6 +834,7 @@ class ParagraphConstraints { /// [Paragraph] with a [ParagraphBuilder]. final double width; + @override bool operator ==(dynamic other) { if (other.runtimeType != runtimeType) return false; @@ -792,8 +842,10 @@ class ParagraphConstraints { return typedOther.width == width; } + @override int get hashCode => width.hashCode; + @override String toString() => '$runtimeType(width: $width)'; }