@@ -274,14 +274,13 @@ class _UntilTextBoundary extends TextBoundary {
274
274
/// caret's size and position. This is preferred due to the expensive
275
275
/// nature of the calculation.
276
276
///
277
- // This should be a sealed class: A _CaretMetrics is either a _LineCaretMetrics
278
- // or an _EmptyLineCaretMetrics.
277
+ // A _CaretMetrics is either a _LineCaretMetrics or an _EmptyLineCaretMetrics.
279
278
@immutable
280
- abstract class _CaretMetrics { }
279
+ sealed class _CaretMetrics { }
281
280
282
281
/// The _CaretMetrics for carets located in a non-empty line. Carets located in a
283
282
/// non-empty line are associated with a glyph within the same line.
284
- class _LineCaretMetrics implements _CaretMetrics {
283
+ final class _LineCaretMetrics implements _CaretMetrics {
285
284
const _LineCaretMetrics ({required this .offset, required this .writingDirection, required this .fullHeight});
286
285
/// The offset of the top left corner of the caret from the top left
287
286
/// corner of the paragraph.
@@ -294,7 +293,7 @@ class _LineCaretMetrics implements _CaretMetrics {
294
293
295
294
/// The _CaretMetrics for carets located in an empty line (when the text is
296
295
/// empty, or the caret is between two a newline characters).
297
- class _EmptyLineCaretMetrics implements _CaretMetrics {
296
+ final class _EmptyLineCaretMetrics implements _CaretMetrics {
298
297
const _EmptyLineCaretMetrics ({ required this .lineVerticalOffset });
299
298
300
299
/// The y offset of the unoccupied line.
@@ -856,12 +855,10 @@ class TextPainter {
856
855
/// Valid only after [layout] has been called.
857
856
double computeDistanceToActualBaseline (TextBaseline baseline) {
858
857
assert (_debugAssertTextLayoutIsValid);
859
- switch (baseline) {
860
- case TextBaseline .alphabetic:
861
- return _paragraph! .alphabeticBaseline;
862
- case TextBaseline .ideographic:
863
- return _paragraph! .ideographicBaseline;
864
- }
858
+ return switch (baseline) {
859
+ TextBaseline .alphabetic => _paragraph! .alphabeticBaseline,
860
+ TextBaseline .ideographic => _paragraph! .ideographicBaseline,
861
+ };
865
862
}
866
863
867
864
/// Whether any text was truncated or ellipsized.
@@ -1144,29 +1141,17 @@ class TextPainter {
1144
1141
}
1145
1142
1146
1143
static double _computePaintOffsetFraction (TextAlign textAlign, TextDirection textDirection) {
1147
- switch (textAlign) {
1148
- case TextAlign .left:
1149
- return 0.0 ;
1150
- case TextAlign .right:
1151
- return 1.0 ;
1152
- case TextAlign .center:
1153
- return 0.5 ;
1154
- case TextAlign .start:
1155
- case TextAlign .justify:
1156
- switch (textDirection) {
1157
- case TextDirection .rtl:
1158
- return 1.0 ;
1159
- case TextDirection .ltr:
1160
- return 0.0 ;
1161
- }
1162
- case TextAlign .end:
1163
- switch (textDirection) {
1164
- case TextDirection .rtl:
1165
- return 0.0 ;
1166
- case TextDirection .ltr:
1167
- return 1.0 ;
1168
- }
1169
- }
1144
+ return switch ((textAlign, textDirection)) {
1145
+ (TextAlign .left, _) => 0.0 ,
1146
+ (TextAlign .right, _) => 1.0 ,
1147
+ (TextAlign .center, _) => 0.5 ,
1148
+ (TextAlign .start, TextDirection .ltr) => 0.0 ,
1149
+ (TextAlign .start, TextDirection .rtl) => 1.0 ,
1150
+ (TextAlign .justify, TextDirection .ltr) => 0.0 ,
1151
+ (TextAlign .justify, TextDirection .rtl) => 1.0 ,
1152
+ (TextAlign .end, TextDirection .ltr) => 1.0 ,
1153
+ (TextAlign .end, TextDirection .rtl) => 0.0 ,
1154
+ };
1170
1155
}
1171
1156
1172
1157
/// Returns the offset at which to paint the caret.
@@ -1181,29 +1166,27 @@ class TextPainter {
1181
1166
caretMetrics = _computeCaretMetrics (position);
1182
1167
}
1183
1168
1184
- if (caretMetrics is _EmptyLineCaretMetrics ) {
1185
- final double paintOffsetAlignment = _computePaintOffsetFraction (textAlign, textDirection! );
1186
- // The full width is not (width - caretPrototype.width)
1187
- // because RenderEditable reserves cursor width on the right. Ideally this
1188
- // should be handled by RenderEditable instead.
1189
- final double dx = paintOffsetAlignment == 0 ? 0 : paintOffsetAlignment * width;
1190
- return Offset (dx, caretMetrics.lineVerticalOffset);
1191
- }
1192
-
1193
- final Offset offset;
1194
- switch ((caretMetrics as _LineCaretMetrics ).writingDirection) {
1195
- case TextDirection .rtl:
1196
- offset = Offset (caretMetrics.offset.dx - caretPrototype.width, caretMetrics.offset.dy);
1197
- case TextDirection .ltr:
1198
- offset = caretMetrics.offset;
1169
+ final Offset rawOffset;
1170
+ switch (caretMetrics) {
1171
+ case _EmptyLineCaretMetrics (: final double lineVerticalOffset):
1172
+ final double paintOffsetAlignment = _computePaintOffsetFraction (textAlign, textDirection! );
1173
+ // The full width is not (width - caretPrototype.width)
1174
+ // because RenderEditable reserves cursor width on the right. Ideally this
1175
+ // should be handled by RenderEditable instead.
1176
+ final double dx = paintOffsetAlignment == 0 ? 0 : paintOffsetAlignment * width;
1177
+ return Offset (dx, lineVerticalOffset);
1178
+ case _LineCaretMetrics (writingDirection: TextDirection .ltr, : final Offset offset):
1179
+ rawOffset = offset;
1180
+ case _LineCaretMetrics (writingDirection: TextDirection .rtl, : final Offset offset):
1181
+ rawOffset = Offset (offset.dx - caretPrototype.width, offset.dy);
1199
1182
}
1200
1183
// If offset.dx is outside of the advertised content area, then the associated
1201
1184
// glyph cluster belongs to a trailing newline character. Ideally the behavior
1202
1185
// should be handled by higher-level implementations (for instance,
1203
1186
// RenderEditable reserves width for showing the caret, it's best to handle
1204
1187
// the clamping there).
1205
- final double adjustedDx = clampDouble (offset .dx, 0 , width);
1206
- return Offset (adjustedDx, offset .dy);
1188
+ final double adjustedDx = clampDouble (rawOffset .dx, 0 , width);
1189
+ return Offset (adjustedDx, rawOffset .dy);
1207
1190
}
1208
1191
1209
1192
/// {@template flutter.painting.textPainter.getFullHeightForCaret}
@@ -1216,8 +1199,10 @@ class TextPainter {
1216
1199
// TODO(LongCatIsLooong): make this case impossible; see https://github.com/flutter/flutter/issues/79495
1217
1200
return null ;
1218
1201
}
1219
- final _CaretMetrics caretMetrics = _computeCaretMetrics (position);
1220
- return caretMetrics is _LineCaretMetrics ? caretMetrics.fullHeight : null ;
1202
+ return switch (_computeCaretMetrics (position)) {
1203
+ _LineCaretMetrics (: final double fullHeight) => fullHeight,
1204
+ _EmptyLineCaretMetrics () => null ,
1205
+ };
1221
1206
}
1222
1207
1223
1208
// Cached caret metrics. This allows multiple invokes of [getOffsetForCaret] and
@@ -1238,17 +1223,10 @@ class TextPainter {
1238
1223
return _caretMetrics;
1239
1224
}
1240
1225
final int offset = position.offset;
1241
- final _CaretMetrics ? metrics;
1242
- switch (position.affinity) {
1243
- case TextAffinity .upstream: {
1244
- metrics = _getMetricsFromUpstream (offset) ?? _getMetricsFromDownstream (offset);
1245
- break ;
1246
- }
1247
- case TextAffinity .downstream: {
1248
- metrics = _getMetricsFromDownstream (offset) ?? _getMetricsFromUpstream (offset);
1249
- break ;
1250
- }
1251
- }
1226
+ final _CaretMetrics ? metrics = switch (position.affinity) {
1227
+ TextAffinity .upstream => _getMetricsFromUpstream (offset) ?? _getMetricsFromDownstream (offset),
1228
+ TextAffinity .downstream => _getMetricsFromDownstream (offset) ?? _getMetricsFromUpstream (offset),
1229
+ };
1252
1230
// Cache the input parameters to prevent repeat work later.
1253
1231
_previousCaretPosition = position;
1254
1232
return _caretMetrics = metrics ?? const _EmptyLineCaretMetrics (lineVerticalOffset: 0 );
0 commit comments