Skip to content

Commit dd85edf

Browse files
committed
add TextStyle.debugLabel
1 parent 039a61f commit dd85edf

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

packages/flutter/lib/src/painting/text_style.dart

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:flutter/foundation.dart';
88

99
import 'basic_types.dart';
1010

11+
const String _kDefaultDebugLabel = 'unknown';
12+
1113
/// An immutable style in which paint text.
1214
///
1315
/// ## Sample code
@@ -230,10 +232,13 @@ class TextStyle extends Diagnosticable {
230232
this.decoration,
231233
this.decorationColor,
232234
this.decorationStyle,
235+
String debugLabel,
233236
String fontFamily,
234237
String package,
235238
}) : fontFamily = package == null ? fontFamily : 'packages/$package/$fontFamily',
236-
assert(inherit != null);
239+
debugLabel = debugLabel == null ? _kDefaultDebugLabel : debugLabel,
240+
assert(inherit != null),
241+
assert(debugLabel == null || debugLabel.length <= 100);
237242

238243

239244
/// Whether null values are replaced with their value in an ancestor text
@@ -302,6 +307,12 @@ class TextStyle extends Diagnosticable {
302307
/// The style in which to paint the text decorations (e.g., dashed).
303308
final TextDecorationStyle decorationStyle;
304309

310+
/// A human-readable description of this text style.
311+
///
312+
/// This property is not considered when comparing text styles using `==` or
313+
/// [compareTo], and it does not affect [hashCode].
314+
final String debugLabel;
315+
305316
/// Creates a copy of this text style but with the given fields replaced with
306317
/// the new values.
307318
TextStyle copyWith({
@@ -317,7 +328,14 @@ class TextStyle extends Diagnosticable {
317328
TextDecoration decoration,
318329
Color decorationColor,
319330
TextDecorationStyle decorationStyle,
331+
String debugLabel,
320332
}) {
333+
String newDebugLabel;
334+
assert(() {
335+
if (this.debugLabel != _kDefaultDebugLabel)
336+
newDebugLabel = _capDebugLabelLength(debugLabel ?? 'copy of ${this.debugLabel}');
337+
return true;
338+
}());
321339
return new TextStyle(
322340
inherit: inherit,
323341
color: color ?? this.color,
@@ -332,9 +350,18 @@ class TextStyle extends Diagnosticable {
332350
decoration: decoration ?? this.decoration,
333351
decorationColor: decorationColor ?? this.decorationColor,
334352
decorationStyle: decorationStyle ?? this.decorationStyle,
353+
debugLabel: newDebugLabel,
335354
);
336355
}
337356

357+
static String _capDebugLabelLength(String label) {
358+
const int maxLabelLength = 100;
359+
if (label.length > maxLabelLength) {
360+
return '${label.substring(0, maxLabelLength - 3)}...';
361+
}
362+
return label;
363+
}
364+
338365
/// Creates a copy of this text style replacing or altering the specified
339366
/// properties.
340367
///
@@ -386,6 +413,14 @@ class TextStyle extends Diagnosticable {
386413
assert(heightFactor != null);
387414
assert(heightDelta != null);
388415
assert(heightFactor != null || (heightFactor == 1.0 && heightDelta == 0.0));
416+
417+
String modifiedDebugLabel;
418+
assert(() {
419+
if (debugLabel != _kDefaultDebugLabel)
420+
modifiedDebugLabel = _capDebugLabelLength('modified $debugLabel');
421+
return true;
422+
}());
423+
389424
return new TextStyle(
390425
inherit: inherit,
391426
color: color ?? this.color,
@@ -400,6 +435,7 @@ class TextStyle extends Diagnosticable {
400435
decoration: decoration ?? this.decoration,
401436
decorationColor: decorationColor ?? this.decorationColor,
402437
decorationStyle: decorationStyle ?? this.decorationStyle,
438+
debugLabel: modifiedDebugLabel,
403439
);
404440
}
405441

@@ -422,6 +458,13 @@ class TextStyle extends Diagnosticable {
422458
return this;
423459
if (!other.inherit)
424460
return other;
461+
462+
String mergedDebugLabel;
463+
assert(() {
464+
mergedDebugLabel = _capDebugLabelLength('${other.debugLabel} < $debugLabel');
465+
return true;
466+
}());
467+
425468
return copyWith(
426469
color: other.color,
427470
fontFamily: other.fontFamily,
@@ -434,7 +477,8 @@ class TextStyle extends Diagnosticable {
434477
height: other.height,
435478
decoration: other.decoration,
436479
decorationColor: other.decorationColor,
437-
decorationStyle: other.decorationStyle
480+
decorationStyle: other.decorationStyle,
481+
debugLabel: mergedDebugLabel,
438482
);
439483
}
440484

@@ -443,6 +487,13 @@ class TextStyle extends Diagnosticable {
443487
/// This will not work well if the styles don't set the same fields.
444488
static TextStyle lerp(TextStyle begin, TextStyle end, double t) {
445489
assert(begin.inherit == end.inherit);
490+
491+
String lerpDebugLabel;
492+
assert(() {
493+
lerpDebugLabel = _capDebugLabelLength('lerp(${begin.debugLabel}, ${end.debugLabel})');
494+
return true;
495+
}());
496+
446497
return new TextStyle(
447498
inherit: end.inherit,
448499
color: Color.lerp(begin.color, end.color, t),
@@ -456,7 +507,8 @@ class TextStyle extends Diagnosticable {
456507
height: ui.lerpDouble(begin.height ?? end.height, end.height ?? begin.height, t),
457508
decoration: t < 0.5 ? begin.decoration : end.decoration,
458509
decorationColor: Color.lerp(begin.decorationColor, end.decorationColor, t),
459-
decorationStyle: t < 0.5 ? begin.decorationStyle : end.decorationStyle
510+
decorationStyle: t < 0.5 ? begin.decorationStyle : end.decorationStyle,
511+
debugLabel: lerpDebugLabel,
460512
);
461513
}
462514

@@ -583,6 +635,8 @@ class TextStyle extends Diagnosticable {
583635
@override
584636
void debugFillProperties(DiagnosticPropertiesBuilder properties, { String prefix: '' }) {
585637
super.debugFillProperties(properties);
638+
if (debugLabel != _kDefaultDebugLabel)
639+
properties.add(new StringProperty('${prefix}debugLabel', debugLabel, defaultValue: null));
586640
final List<DiagnosticsNode> styles = <DiagnosticsNode>[];
587641
styles.add(new DiagnosticsProperty<Color>('${prefix}color', color, defaultValue: null));
588642
styles.add(new StringProperty('${prefix}family', fontFamily, defaultValue: null, quoted: false));

packages/flutter/test/painting/text_style_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,42 @@ void main() {
142142
expect(s7.fontFamily, 'packages/p/test');
143143
expect(s7.getTextStyle().toString(), 'TextStyle(color: unspecified, decoration: unspecified, decorationColor: unspecified, decorationStyle: unspecified, fontWeight: unspecified, fontStyle: unspecified, textBaseline: unspecified, fontFamily: packages/p/test, fontSize: unspecified, letterSpacing: unspecified, wordSpacing: unspecified, height: unspecified)');
144144
});
145+
146+
test('TextStyle.debugLabel', () {
147+
const TextStyle unknown = const TextStyle();
148+
const TextStyle foo = const TextStyle(debugLabel: 'foo', fontSize: 1.0);
149+
const TextStyle bar = const TextStyle(debugLabel: 'bar', fontSize: 2.0);
150+
const TextStyle baz = const TextStyle(debugLabel: 'baz', fontSize: 3.0);
151+
152+
expect(unknown.debugLabel, 'unknown');
153+
expect(unknown.toString(), 'TextStyle(<all styles inherited>)');
154+
expect(unknown.copyWith().debugLabel, 'unknown');
155+
expect(unknown.apply().debugLabel, 'unknown');
156+
157+
expect(foo.debugLabel, 'foo');
158+
expect(foo.toString(), 'TextStyle(debugLabel: "foo", inherit: true, size: 1.0)');
159+
expect(foo.merge(bar).debugLabel, 'bar < foo');
160+
expect(foo.merge(bar).merge(baz).debugLabel, 'baz < bar < foo');
161+
expect(foo.copyWith().debugLabel, 'copy of foo');
162+
expect(foo.apply().debugLabel, 'modified foo');
163+
expect(TextStyle.lerp(foo, bar, 0.5).debugLabel, 'lerp(foo, bar)');
164+
expect(TextStyle.lerp(foo.merge(bar), baz, 0.5).copyWith().debugLabel, 'copy of lerp(bar < foo, baz)');
165+
166+
expect(() => new TextStyle(debugLabel: 'a' * 300), throwsA(const isInstanceOf<AssertionError>()));
167+
168+
TextStyle cappedMerge = foo;
169+
TextStyle cappedCopy = foo;
170+
TextStyle cappedApply = foo;
171+
TextStyle cappedLerp = foo;
172+
for (int i = 0; i < 50; i++) {
173+
cappedMerge = cappedMerge.merge(bar);
174+
cappedCopy = cappedCopy.copyWith();
175+
cappedApply = cappedApply.apply();
176+
cappedLerp = TextStyle.lerp(cappedLerp, bar, 0.5);
177+
}
178+
expect(cappedMerge.debugLabel, hasLength(100));
179+
expect(cappedCopy.debugLabel, hasLength(100));
180+
expect(cappedApply.debugLabel, hasLength(100));
181+
expect(cappedLerp.debugLabel, hasLength(100));
182+
});
145183
}

0 commit comments

Comments
 (0)