Unverified Commit 0438bdfe authored by Hans Muller's avatar Hans Muller Committed by GitHub

TextStyle.apply,copyWith,merge should support a package parameter (#90986)

parent 5a85c0d8
...@@ -773,6 +773,17 @@ class TextStyle with Diagnosticable { ...@@ -773,6 +773,17 @@ class TextStyle with Diagnosticable {
/// How visual text overflow should be handled. /// How visual text overflow should be handled.
final TextOverflow? overflow; final TextOverflow? overflow;
// Return the original value of fontFamily, without the additional
// "packages/$_package/" prefix.
String? get _fontFamily {
if (_package != null && fontFamily != null) {
final String fontFamilyPrefix = 'packages/$_package/';
assert(fontFamily!.startsWith(fontFamilyPrefix));
return fontFamily!.substring(fontFamilyPrefix.length);
}
return fontFamily;
}
/// Creates a copy of this text style but with the given fields replaced with /// Creates a copy of this text style but with the given fields replaced with
/// the new values. /// the new values.
/// ///
...@@ -786,8 +797,6 @@ class TextStyle with Diagnosticable { ...@@ -786,8 +797,6 @@ class TextStyle with Diagnosticable {
bool? inherit, bool? inherit,
Color? color, Color? color,
Color? backgroundColor, Color? backgroundColor,
String? fontFamily,
List<String>? fontFamilyFallback,
double? fontSize, double? fontSize,
FontWeight? fontWeight, FontWeight? fontWeight,
FontStyle? fontStyle, FontStyle? fontStyle,
...@@ -806,6 +815,9 @@ class TextStyle with Diagnosticable { ...@@ -806,6 +815,9 @@ class TextStyle with Diagnosticable {
TextDecorationStyle? decorationStyle, TextDecorationStyle? decorationStyle,
double? decorationThickness, double? decorationThickness,
String? debugLabel, String? debugLabel,
String? fontFamily,
List<String>? fontFamilyFallback,
String? package,
TextOverflow? overflow, TextOverflow? overflow,
}) { }) {
assert(color == null || foreground == null, _kColorForegroundWarning); assert(color == null || foreground == null, _kColorForegroundWarning);
...@@ -816,12 +828,11 @@ class TextStyle with Diagnosticable { ...@@ -816,12 +828,11 @@ class TextStyle with Diagnosticable {
newDebugLabel = debugLabel ?? '(${this.debugLabel}).copyWith'; newDebugLabel = debugLabel ?? '(${this.debugLabel}).copyWith';
return true; return true;
}()); }());
return TextStyle( return TextStyle(
inherit: inherit ?? this.inherit, inherit: inherit ?? this.inherit,
color: this.foreground == null && foreground == null ? color ?? this.color : null, color: this.foreground == null && foreground == null ? color ?? this.color : null,
backgroundColor: this.background == null && background == null ? backgroundColor ?? this.backgroundColor : null, backgroundColor: this.background == null && background == null ? backgroundColor ?? this.backgroundColor : null,
fontFamily: fontFamily ?? this.fontFamily,
fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback,
fontSize: fontSize ?? this.fontSize, fontSize: fontSize ?? this.fontSize,
fontWeight: fontWeight ?? this.fontWeight, fontWeight: fontWeight ?? this.fontWeight,
fontStyle: fontStyle ?? this.fontStyle, fontStyle: fontStyle ?? this.fontStyle,
...@@ -840,6 +851,9 @@ class TextStyle with Diagnosticable { ...@@ -840,6 +851,9 @@ class TextStyle with Diagnosticable {
decorationStyle: decorationStyle ?? this.decorationStyle, decorationStyle: decorationStyle ?? this.decorationStyle,
decorationThickness: decorationThickness ?? this.decorationThickness, decorationThickness: decorationThickness ?? this.decorationThickness,
debugLabel: newDebugLabel, debugLabel: newDebugLabel,
fontFamily: fontFamily ?? _fontFamily,
fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback,
package: package ?? _package,
overflow: overflow ?? this.overflow, overflow: overflow ?? this.overflow,
); );
} }
...@@ -898,6 +912,7 @@ class TextStyle with Diagnosticable { ...@@ -898,6 +912,7 @@ class TextStyle with Diagnosticable {
Locale? locale, Locale? locale,
List<ui.Shadow>? shadows, List<ui.Shadow>? shadows,
List<ui.FontFeature>? fontFeatures, List<ui.FontFeature>? fontFeatures,
String? package,
TextOverflow? overflow, TextOverflow? overflow,
}) { }) {
assert(fontSizeFactor != null); assert(fontSizeFactor != null);
...@@ -928,7 +943,7 @@ class TextStyle with Diagnosticable { ...@@ -928,7 +943,7 @@ class TextStyle with Diagnosticable {
inherit: inherit, inherit: inherit,
color: foreground == null ? color ?? this.color : null, color: foreground == null ? color ?? this.color : null,
backgroundColor: background == null ? backgroundColor ?? this.backgroundColor : null, backgroundColor: background == null ? backgroundColor ?? this.backgroundColor : null,
fontFamily: fontFamily ?? this.fontFamily, fontFamily: fontFamily ?? _fontFamily,
fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback, fontFamilyFallback: fontFamilyFallback ?? this.fontFamilyFallback,
fontSize: fontSize == null ? null : fontSize! * fontSizeFactor + fontSizeDelta, fontSize: fontSize == null ? null : fontSize! * fontSizeFactor + fontSizeDelta,
fontWeight: fontWeight == null ? null : FontWeight.values[(fontWeight!.index + fontWeightDelta).clamp(0, FontWeight.values.length - 1)], fontWeight: fontWeight == null ? null : FontWeight.values[(fontWeight!.index + fontWeightDelta).clamp(0, FontWeight.values.length - 1)],
...@@ -948,6 +963,7 @@ class TextStyle with Diagnosticable { ...@@ -948,6 +963,7 @@ class TextStyle with Diagnosticable {
decorationStyle: decorationStyle ?? this.decorationStyle, decorationStyle: decorationStyle ?? this.decorationStyle,
decorationThickness: decorationThickness == null ? null : decorationThickness! * decorationThicknessFactor + decorationThicknessDelta, decorationThickness: decorationThickness == null ? null : decorationThickness! * decorationThicknessFactor + decorationThicknessDelta,
overflow: overflow ?? this.overflow, overflow: overflow ?? this.overflow,
package: package ?? _package,
debugLabel: modifiedDebugLabel, debugLabel: modifiedDebugLabel,
); );
} }
...@@ -989,8 +1005,6 @@ class TextStyle with Diagnosticable { ...@@ -989,8 +1005,6 @@ class TextStyle with Diagnosticable {
return copyWith( return copyWith(
color: other.color, color: other.color,
backgroundColor: other.backgroundColor, backgroundColor: other.backgroundColor,
fontFamily: other.fontFamily,
fontFamilyFallback: other.fontFamilyFallback,
fontSize: other.fontSize, fontSize: other.fontSize,
fontWeight: other.fontWeight, fontWeight: other.fontWeight,
fontStyle: other.fontStyle, fontStyle: other.fontStyle,
...@@ -1008,8 +1022,11 @@ class TextStyle with Diagnosticable { ...@@ -1008,8 +1022,11 @@ class TextStyle with Diagnosticable {
decorationColor: other.decorationColor, decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle, decorationStyle: other.decorationStyle,
decorationThickness: other.decorationThickness, decorationThickness: other.decorationThickness,
overflow: other.overflow,
debugLabel: mergedDebugLabel, debugLabel: mergedDebugLabel,
fontFamily: other._fontFamily,
fontFamilyFallback: other.fontFamilyFallback,
package: other._package,
overflow: other.overflow,
); );
} }
...@@ -1044,8 +1061,6 @@ class TextStyle with Diagnosticable { ...@@ -1044,8 +1061,6 @@ class TextStyle with Diagnosticable {
inherit: b!.inherit, inherit: b!.inherit,
color: Color.lerp(null, b.color, t), color: Color.lerp(null, b.color, t),
backgroundColor: Color.lerp(null, b.backgroundColor, t), backgroundColor: Color.lerp(null, b.backgroundColor, t),
fontFamily: t < 0.5 ? null : b.fontFamily,
fontFamilyFallback: t < 0.5 ? null : b.fontFamilyFallback,
fontSize: t < 0.5 ? null : b.fontSize, fontSize: t < 0.5 ? null : b.fontSize,
fontWeight: FontWeight.lerp(null, b.fontWeight, t), fontWeight: FontWeight.lerp(null, b.fontWeight, t),
fontStyle: t < 0.5 ? null : b.fontStyle, fontStyle: t < 0.5 ? null : b.fontStyle,
...@@ -1057,14 +1072,17 @@ class TextStyle with Diagnosticable { ...@@ -1057,14 +1072,17 @@ class TextStyle with Diagnosticable {
locale: t < 0.5 ? null : b.locale, locale: t < 0.5 ? null : b.locale,
foreground: t < 0.5 ? null : b.foreground, foreground: t < 0.5 ? null : b.foreground,
background: t < 0.5 ? null : b.background, background: t < 0.5 ? null : b.background,
decoration: t < 0.5 ? null : b.decoration,
shadows: t < 0.5 ? null : b.shadows, shadows: t < 0.5 ? null : b.shadows,
fontFeatures: t < 0.5 ? null : b.fontFeatures, fontFeatures: t < 0.5 ? null : b.fontFeatures,
decoration: t < 0.5 ? null : b.decoration,
decorationColor: Color.lerp(null, b.decorationColor, t), decorationColor: Color.lerp(null, b.decorationColor, t),
decorationStyle: t < 0.5 ? null : b.decorationStyle, decorationStyle: t < 0.5 ? null : b.decorationStyle,
decorationThickness: t < 0.5 ? null : b.decorationThickness, decorationThickness: t < 0.5 ? null : b.decorationThickness,
overflow: t < 0.5 ? null : b.overflow,
debugLabel: lerpDebugLabel, debugLabel: lerpDebugLabel,
fontFamily: t < 0.5 ? null : b._fontFamily,
fontFamilyFallback: t < 0.5 ? null : b.fontFamilyFallback,
package: t < 0.5 ? null : b._package,
overflow: t < 0.5 ? null : b.overflow,
); );
} }
...@@ -1073,8 +1091,6 @@ class TextStyle with Diagnosticable { ...@@ -1073,8 +1091,6 @@ class TextStyle with Diagnosticable {
inherit: a.inherit, inherit: a.inherit,
color: Color.lerp(a.color, null, t), color: Color.lerp(a.color, null, t),
backgroundColor: Color.lerp(null, a.backgroundColor, t), backgroundColor: Color.lerp(null, a.backgroundColor, t),
fontFamily: t < 0.5 ? a.fontFamily : null,
fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : null,
fontSize: t < 0.5 ? a.fontSize : null, fontSize: t < 0.5 ? a.fontSize : null,
fontWeight: FontWeight.lerp(a.fontWeight, null, t), fontWeight: FontWeight.lerp(a.fontWeight, null, t),
fontStyle: t < 0.5 ? a.fontStyle : null, fontStyle: t < 0.5 ? a.fontStyle : null,
...@@ -1092,8 +1108,11 @@ class TextStyle with Diagnosticable { ...@@ -1092,8 +1108,11 @@ class TextStyle with Diagnosticable {
decorationColor: Color.lerp(a.decorationColor, null, t), decorationColor: Color.lerp(a.decorationColor, null, t),
decorationStyle: t < 0.5 ? a.decorationStyle : null, decorationStyle: t < 0.5 ? a.decorationStyle : null,
decorationThickness: t < 0.5 ? a.decorationThickness : null, decorationThickness: t < 0.5 ? a.decorationThickness : null,
overflow: t < 0.5 ? a.overflow : null,
debugLabel: lerpDebugLabel, debugLabel: lerpDebugLabel,
fontFamily: t < 0.5 ? a._fontFamily : null,
fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : null,
package: t < 0.5 ? a._package : null,
overflow: t < 0.5 ? a.overflow : null,
); );
} }
...@@ -1101,8 +1120,6 @@ class TextStyle with Diagnosticable { ...@@ -1101,8 +1120,6 @@ class TextStyle with Diagnosticable {
inherit: b.inherit, inherit: b.inherit,
color: a.foreground == null && b.foreground == null ? Color.lerp(a.color, b.color, t) : null, color: a.foreground == null && b.foreground == null ? Color.lerp(a.color, b.color, t) : null,
backgroundColor: a.background == null && b.background == null ? Color.lerp(a.backgroundColor, b.backgroundColor, t) : null, backgroundColor: a.background == null && b.background == null ? Color.lerp(a.backgroundColor, b.backgroundColor, t) : null,
fontFamily: t < 0.5 ? a.fontFamily : b.fontFamily,
fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : b.fontFamilyFallback,
fontSize: ui.lerpDouble(a.fontSize ?? b.fontSize, b.fontSize ?? a.fontSize, t), fontSize: ui.lerpDouble(a.fontSize ?? b.fontSize, b.fontSize ?? a.fontSize, t),
fontWeight: FontWeight.lerp(a.fontWeight, b.fontWeight, t), fontWeight: FontWeight.lerp(a.fontWeight, b.fontWeight, t),
fontStyle: t < 0.5 ? a.fontStyle : b.fontStyle, fontStyle: t < 0.5 ? a.fontStyle : b.fontStyle,
...@@ -1128,8 +1145,11 @@ class TextStyle with Diagnosticable { ...@@ -1128,8 +1145,11 @@ class TextStyle with Diagnosticable {
decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t), decorationColor: Color.lerp(a.decorationColor, b.decorationColor, t),
decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle, decorationStyle: t < 0.5 ? a.decorationStyle : b.decorationStyle,
decorationThickness: ui.lerpDouble(a.decorationThickness ?? b.decorationThickness, b.decorationThickness ?? a.decorationThickness, t), decorationThickness: ui.lerpDouble(a.decorationThickness ?? b.decorationThickness, b.decorationThickness ?? a.decorationThickness, t),
overflow: t < 0.5 ? a.overflow : b.overflow,
debugLabel: lerpDebugLabel, debugLabel: lerpDebugLabel,
fontFamily: t < 0.5 ? a._fontFamily : b._fontFamily,
fontFamilyFallback: t < 0.5 ? a.fontFamilyFallback : b.fontFamilyFallback,
package: t < 0.5 ? a._package : b._package,
overflow: t < 0.5 ? a.overflow : b.overflow,
); );
} }
...@@ -1264,7 +1284,6 @@ class TextStyle with Diagnosticable { ...@@ -1264,7 +1284,6 @@ class TextStyle with Diagnosticable {
&& other.inherit == inherit && other.inherit == inherit
&& other.color == color && other.color == color
&& other.backgroundColor == backgroundColor && other.backgroundColor == backgroundColor
&& other.fontFamily == fontFamily
&& other.fontSize == fontSize && other.fontSize == fontSize
&& other.fontWeight == fontWeight && other.fontWeight == fontWeight
&& other.fontStyle == fontStyle && other.fontStyle == fontStyle
...@@ -1276,13 +1295,15 @@ class TextStyle with Diagnosticable { ...@@ -1276,13 +1295,15 @@ class TextStyle with Diagnosticable {
&& other.locale == locale && other.locale == locale
&& other.foreground == foreground && other.foreground == foreground
&& other.background == background && other.background == background
&& listEquals(other.shadows, shadows)
&& listEquals(other.fontFeatures, fontFeatures)
&& other.decoration == decoration && other.decoration == decoration
&& other.decorationColor == decorationColor && other.decorationColor == decorationColor
&& other.decorationStyle == decorationStyle && other.decorationStyle == decorationStyle
&& other.decorationThickness == decorationThickness && other.decorationThickness == decorationThickness
&& listEquals(other.shadows, shadows) && other.fontFamily == fontFamily
&& listEquals(other.fontFeatures, fontFeatures)
&& listEquals(other.fontFamilyFallback, fontFamilyFallback) && listEquals(other.fontFamilyFallback, fontFamilyFallback)
&& other._package == _package
&& other.overflow == overflow; && other.overflow == overflow;
} }
...@@ -1292,7 +1313,6 @@ class TextStyle with Diagnosticable { ...@@ -1292,7 +1313,6 @@ class TextStyle with Diagnosticable {
inherit, inherit,
color, color,
backgroundColor, backgroundColor,
fontFamily,
fontSize, fontSize,
fontWeight, fontWeight,
fontStyle, fontStyle,
...@@ -1304,12 +1324,15 @@ class TextStyle with Diagnosticable { ...@@ -1304,12 +1324,15 @@ class TextStyle with Diagnosticable {
locale, locale,
foreground, foreground,
background, background,
hashList(shadows),
hashList(fontFeatures),
decoration, decoration,
decorationColor, decorationColor,
decorationStyle, decorationStyle,
hashList(shadows), decorationThickness,
hashList(fontFeatures), fontFamily,
hashList(fontFamilyFallback), hashList(fontFamilyFallback),
_package,
overflow, overflow,
]); ]);
} }
......
...@@ -790,6 +790,7 @@ class _TextStyleProxy implements TextStyle { ...@@ -790,6 +790,7 @@ class _TextStyleProxy implements TextStyle {
List<ui.Shadow>? shadows, List<ui.Shadow>? shadows,
List<ui.FontFeature>? fontFeatures, List<ui.FontFeature>? fontFeatures,
TextOverflow? overflow, TextOverflow? overflow,
String? package,
}) { }) {
throw UnimplementedError(); throw UnimplementedError();
} }
...@@ -825,6 +826,7 @@ class _TextStyleProxy implements TextStyle { ...@@ -825,6 +826,7 @@ class _TextStyleProxy implements TextStyle {
double? decorationThickness, double? decorationThickness,
String? debugLabel, String? debugLabel,
TextOverflow? overflow, TextOverflow? overflow,
String? package,
}) { }) {
throw UnimplementedError(); throw UnimplementedError();
} }
......
...@@ -495,4 +495,17 @@ void main() { ...@@ -495,4 +495,17 @@ void main() {
TextLeadingDistribution.proportional, TextLeadingDistribution.proportional,
); );
}); });
test('TextStyle fontFamily and package', () {
expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo') != const TextStyle(fontFamily: 'fontFamily', package: 'bar'), true);
expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').hashCode != const TextStyle(package: 'bar', fontFamily: 'fontFamily').hashCode, true);
expect(const TextStyle(fontFamily: 'fontFamily').fontFamily, 'fontFamily');
expect(const TextStyle(fontFamily: 'fontFamily').fontFamily, 'fontFamily');
expect(const TextStyle(fontFamily: 'fontFamily').copyWith(package: 'bar').fontFamily, 'packages/bar/fontFamily');
expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').fontFamily, 'packages/foo/fontFamily');
expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').copyWith(package: 'bar').fontFamily, 'packages/bar/fontFamily');
expect(const TextStyle().merge(const TextStyle(fontFamily: 'fontFamily', package: 'bar')).fontFamily, 'packages/bar/fontFamily');
expect(const TextStyle().apply(fontFamily: 'fontFamily', package: 'foo').fontFamily, 'packages/foo/fontFamily');
expect(const TextStyle(fontFamily: 'fontFamily', package: 'foo').apply(fontFamily: 'fontFamily', package: 'bar').fontFamily, 'packages/bar/fontFamily');
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment