Skip to content

Commit c8d8016

Browse files
authored
Revert "Fix text.rich to merge widget span (flutter#113461)" (flutter#121562)
This reverts commit 660992a.
1 parent 22cbe6e commit c8d8016

File tree

4 files changed

+21
-312
lines changed

4 files changed

+21
-312
lines changed

packages/flutter/lib/src/rendering/object.dart

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'package:flutter/semantics.dart';
1515

1616
import 'debug.dart';
1717
import 'layer.dart';
18-
import 'proxy_box.dart';
1918

2019
export 'package:flutter/foundation.dart' show
2120
DiagnosticPropertiesBuilder,
@@ -3228,15 +3227,14 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
32283227
final SemanticsConfiguration config = _semanticsConfiguration;
32293228
bool dropSemanticsOfPreviousSiblings = config.isBlockingSemanticsOfPreviouslyPaintedNodes;
32303229

3231-
bool producesForkingFragment = !config.hasBeenAnnotated && !config.isSemanticBoundary;
3230+
final bool producesForkingFragment = !config.hasBeenAnnotated && !config.isSemanticBoundary;
32323231
final bool childrenMergeIntoParent = mergeIntoParent || config.isMergingSemanticsOfDescendants;
32333232
final List<SemanticsConfiguration> childConfigurations = <SemanticsConfiguration>[];
32343233
final bool explicitChildNode = config.explicitChildNodes || parent is! RenderObject;
32353234
final bool hasChildConfigurationsDelegate = config.childConfigurationsDelegate != null;
32363235
final Map<SemanticsConfiguration, _InterestingSemanticsFragment> configToFragment = <SemanticsConfiguration, _InterestingSemanticsFragment>{};
32373236
final List<_InterestingSemanticsFragment> mergeUpFragments = <_InterestingSemanticsFragment>[];
32383237
final List<List<_InterestingSemanticsFragment>> siblingMergeFragmentGroups = <List<_InterestingSemanticsFragment>>[];
3239-
final bool hasTags = config.tagsForChildren?.isNotEmpty ?? false;
32403238
visitChildrenForSemantics((RenderObject renderChild) {
32413239
assert(!_needsLayout);
32423240
final _SemanticsFragment parentFragment = renderChild._getSemanticsForParent(
@@ -3252,9 +3250,7 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
32523250
}
32533251
for (final _InterestingSemanticsFragment fragment in parentFragment.mergeUpFragments) {
32543252
fragment.addAncestor(this);
3255-
if (hasTags) {
3256-
fragment.addTags(config.tagsForChildren!);
3257-
}
3253+
fragment.addTags(config.tagsForChildren);
32583254
if (hasChildConfigurationsDelegate && fragment.config != null) {
32593255
// This fragment need to go through delegate to determine whether it
32603256
// merge up or not.
@@ -3270,9 +3266,7 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
32703266
for (final List<_InterestingSemanticsFragment> siblingMergeGroup in parentFragment.siblingMergeGroups) {
32713267
for (final _InterestingSemanticsFragment siblingMergingFragment in siblingMergeGroup) {
32723268
siblingMergingFragment.addAncestor(this);
3273-
if (hasTags) {
3274-
siblingMergingFragment.addTags(config.tagsForChildren!);
3275-
}
3269+
siblingMergingFragment.addTags(config.tagsForChildren);
32763270
}
32773271
siblingMergeFragmentGroups.add(siblingMergeGroup);
32783272
}
@@ -3285,25 +3279,14 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
32853279
for (final _InterestingSemanticsFragment fragment in mergeUpFragments) {
32863280
fragment.markAsExplicit();
32873281
}
3288-
} else if (hasChildConfigurationsDelegate) {
3282+
} else if (hasChildConfigurationsDelegate && childConfigurations.isNotEmpty) {
32893283
final ChildSemanticsConfigurationsResult result = config.childConfigurationsDelegate!(childConfigurations);
32903284
mergeUpFragments.addAll(
3291-
result.mergeUp.map<_InterestingSemanticsFragment>((SemanticsConfiguration config) {
3292-
final _InterestingSemanticsFragment? fragment = configToFragment[config];
3293-
if (fragment == null) {
3294-
// Parent fragment of Incomplete fragments can't be a forking
3295-
// fragment since they need to be merged.
3296-
producesForkingFragment = false;
3297-
return _IncompleteSemanticsFragment(config: config, owner: this);
3298-
}
3299-
return fragment;
3300-
}),
3285+
result.mergeUp.map<_InterestingSemanticsFragment>((SemanticsConfiguration config) => configToFragment[config]!),
33013286
);
33023287
for (final Iterable<SemanticsConfiguration> group in result.siblingMergeGroups) {
33033288
siblingMergeFragmentGroups.add(
3304-
group.map<_InterestingSemanticsFragment>((SemanticsConfiguration config) {
3305-
return configToFragment[config] ?? _IncompleteSemanticsFragment(config: config, owner: this);
3306-
}).toList(),
3289+
group.map<_InterestingSemanticsFragment>((SemanticsConfiguration config) => configToFragment[config]!).toList()
33073290
);
33083291
}
33093292
}
@@ -4184,10 +4167,10 @@ abstract class _InterestingSemanticsFragment extends _SemanticsFragment {
41844167
Set<SemanticsTag>? _tagsForChildren;
41854168

41864169
/// Tag all children produced by [compileChildren] with `tags`.
4187-
///
4188-
/// `tags` must not be empty.
4189-
void addTags(Iterable<SemanticsTag> tags) {
4190-
assert(tags.isNotEmpty);
4170+
void addTags(Iterable<SemanticsTag>? tags) {
4171+
if (tags == null || tags.isEmpty) {
4172+
return;
4173+
}
41914174
_tagsForChildren ??= <SemanticsTag>{};
41924175
_tagsForChildren!.addAll(tags);
41934176
}
@@ -4281,48 +4264,6 @@ class _RootSemanticsFragment extends _InterestingSemanticsFragment {
42814264
}
42824265
}
42834266

4284-
/// A fragment with partial information that must not form an explicit
4285-
/// semantics node without merging into another _SwitchableSemanticsFragment.
4286-
///
4287-
/// This fragment is generated from synthetic SemanticsConfiguration returned from
4288-
/// [SemanticsConfiguration.childConfigurationsDelegate].
4289-
class _IncompleteSemanticsFragment extends _InterestingSemanticsFragment {
4290-
_IncompleteSemanticsFragment({
4291-
required this.config,
4292-
required super.owner,
4293-
}) : super(dropsSemanticsOfPreviousSiblings: false);
4294-
4295-
@override
4296-
void addAll(Iterable<_InterestingSemanticsFragment> fragments) {
4297-
assert(false, 'This fragment must be a leaf node');
4298-
}
4299-
4300-
@override
4301-
void compileChildren({
4302-
required Rect? parentSemanticsClipRect,
4303-
required Rect? parentPaintClipRect,
4304-
required double elevationAdjustment,
4305-
required List<SemanticsNode> result,
4306-
required List<SemanticsNode> siblingNodes,
4307-
}) {
4308-
// There is nothing to do because this fragment must be a leaf node and
4309-
// must not be explicit.
4310-
}
4311-
4312-
@override
4313-
final SemanticsConfiguration config;
4314-
4315-
@override
4316-
void markAsExplicit() {
4317-
assert(
4318-
false,
4319-
'SemanticsConfiguration created in '
4320-
'SemanticsConfiguration.childConfigurationsDelegate must not produce '
4321-
'its own semantics node'
4322-
);
4323-
}
4324-
}
4325-
43264267
/// An [_InterestingSemanticsFragment] that can be told to only add explicit
43274268
/// [SemanticsNode]s to the parent.
43284269
///
@@ -4601,14 +4542,6 @@ class _SwitchableSemanticsFragment extends _InterestingSemanticsFragment {
46014542
}
46024543
}
46034544

4604-
@override
4605-
void addTags(Iterable<SemanticsTag> tags) {
4606-
super.addTags(tags);
4607-
// _ContainerSemanticsFragments add their tags to child fragments through
4608-
// this method. This fragment must make sure its _config is in sync.
4609-
tags.forEach(_config.addTagForChildren);
4610-
}
4611-
46124545
void _ensureConfigIsWritable() {
46134546
if (!_isConfigWritable) {
46144547
_config = _config.copy();

packages/flutter/lib/src/rendering/paragraph.dart

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ class RenderParagraph extends RenderBox
119119

120120
static final String _placeholderCharacter = String.fromCharCode(PlaceholderSpan.placeholderCodeUnit);
121121
final TextPainter _textPainter;
122-
123-
List<AttributedString>? _cachedAttributedLabels;
124-
122+
AttributedString? _cachedAttributedLabel;
125123
List<InlineSpanSemanticsInformation>? _cachedCombinedSemanticsInfos;
126124

127125
/// The text to display.
@@ -137,7 +135,7 @@ class RenderParagraph extends RenderBox
137135
break;
138136
case RenderComparison.paint:
139137
_textPainter.text = value;
140-
_cachedAttributedLabels = null;
138+
_cachedAttributedLabel = null;
141139
_cachedCombinedSemanticsInfos = null;
142140
_extractPlaceholderSpans(value);
143141
markNeedsPaint();
@@ -146,7 +144,7 @@ class RenderParagraph extends RenderBox
146144
case RenderComparison.layout:
147145
_textPainter.text = value;
148146
_overflowShader = null;
149-
_cachedAttributedLabels = null;
147+
_cachedAttributedLabel = null;
150148
_cachedCombinedSemanticsInfos = null;
151149
_extractPlaceholderSpans(value);
152150
markNeedsLayout();
@@ -1037,23 +1035,12 @@ class RenderParagraph extends RenderBox
10371035
void describeSemanticsConfiguration(SemanticsConfiguration config) {
10381036
super.describeSemanticsConfiguration(config);
10391037
_semanticsInfo = text.getSemanticsInformation();
1040-
bool needsAssembleSemanticsNode = false;
1041-
bool needsChildConfigrationsDelegate = false;
1042-
for (final InlineSpanSemanticsInformation info in _semanticsInfo!) {
1043-
if (info.recognizer != null) {
1044-
needsAssembleSemanticsNode = true;
1045-
break;
1046-
}
1047-
needsChildConfigrationsDelegate = needsChildConfigrationsDelegate || info.isPlaceholder;
1048-
}
10491038

1050-
if (needsAssembleSemanticsNode) {
1039+
if (_semanticsInfo!.any((InlineSpanSemanticsInformation info) => info.recognizer != null)) {
10511040
config.explicitChildNodes = true;
10521041
config.isSemanticBoundary = true;
1053-
} else if (needsChildConfigrationsDelegate) {
1054-
config.childConfigurationsDelegate = _childSemanticsConfigurationsDelegate;
10551042
} else {
1056-
if (_cachedAttributedLabels == null) {
1043+
if (_cachedAttributedLabel == null) {
10571044
final StringBuffer buffer = StringBuffer();
10581045
int offset = 0;
10591046
final List<StringAttribute> attributes = <StringAttribute>[];
@@ -1063,77 +1050,21 @@ class RenderParagraph extends RenderBox
10631050
final TextRange originalRange = infoAttribute.range;
10641051
attributes.add(
10651052
infoAttribute.copy(
1066-
range: TextRange(
1067-
start: offset + originalRange.start,
1068-
end: offset + originalRange.end,
1069-
),
1053+
range: TextRange(start: offset + originalRange.start,
1054+
end: offset + originalRange.end)
10701055
),
10711056
);
10721057
}
10731058
buffer.write(label);
10741059
offset += label.length;
10751060
}
1076-
_cachedAttributedLabels = <AttributedString>[AttributedString(buffer.toString(), attributes: attributes)];
1061+
_cachedAttributedLabel = AttributedString(buffer.toString(), attributes: attributes);
10771062
}
1078-
config.attributedLabel = _cachedAttributedLabels![0];
1063+
config.attributedLabel = _cachedAttributedLabel!;
10791064
config.textDirection = textDirection;
10801065
}
10811066
}
10821067

1083-
ChildSemanticsConfigurationsResult _childSemanticsConfigurationsDelegate(List<SemanticsConfiguration> childConfigs) {
1084-
final ChildSemanticsConfigurationsResultBuilder builder = ChildSemanticsConfigurationsResultBuilder();
1085-
int placeholderIndex = 0;
1086-
int childConfigsIndex = 0;
1087-
int attributedLabelCacheIndex = 0;
1088-
InlineSpanSemanticsInformation? seenTextInfo;
1089-
_cachedCombinedSemanticsInfos ??= combineSemanticsInfo(_semanticsInfo!);
1090-
for (final InlineSpanSemanticsInformation info in _cachedCombinedSemanticsInfos!) {
1091-
if (info.isPlaceholder) {
1092-
if (seenTextInfo != null) {
1093-
builder.markAsMergeUp(_createSemanticsConfigForTextInfo(seenTextInfo, attributedLabelCacheIndex));
1094-
attributedLabelCacheIndex += 1;
1095-
}
1096-
// Mark every childConfig belongs to this placeholder to merge up group.
1097-
while (childConfigsIndex < childConfigs.length &&
1098-
childConfigs[childConfigsIndex].tagsChildrenWith(PlaceholderSpanIndexSemanticsTag(placeholderIndex))) {
1099-
builder.markAsMergeUp(childConfigs[childConfigsIndex]);
1100-
childConfigsIndex += 1;
1101-
}
1102-
placeholderIndex += 1;
1103-
} else {
1104-
seenTextInfo = info;
1105-
}
1106-
}
1107-
1108-
// Handle plain text info at the end.
1109-
if (seenTextInfo != null) {
1110-
builder.markAsMergeUp(_createSemanticsConfigForTextInfo(seenTextInfo, attributedLabelCacheIndex));
1111-
}
1112-
return builder.build();
1113-
}
1114-
1115-
SemanticsConfiguration _createSemanticsConfigForTextInfo(InlineSpanSemanticsInformation textInfo, int cacheIndex) {
1116-
assert(!textInfo.requiresOwnNode);
1117-
final List<AttributedString> cachedStrings = _cachedAttributedLabels ??= <AttributedString>[];
1118-
assert(cacheIndex <= cachedStrings.length);
1119-
final bool hasCache = cacheIndex < cachedStrings.length;
1120-
1121-
late AttributedString attributedLabel;
1122-
if (hasCache) {
1123-
attributedLabel = cachedStrings[cacheIndex];
1124-
} else {
1125-
assert(cachedStrings.length == cacheIndex);
1126-
attributedLabel = AttributedString(
1127-
textInfo.semanticsLabel ?? textInfo.text,
1128-
attributes: textInfo.stringAttributes,
1129-
);
1130-
cachedStrings.add(attributedLabel);
1131-
}
1132-
return SemanticsConfiguration()
1133-
..textDirection = textDirection
1134-
..attributedLabel = attributedLabel;
1135-
}
1136-
11371068
// Caches [SemanticsNode]s created during [assembleSemanticsNode] so they
11381069
// can be re-used when [assembleSemanticsNode] is called again. This ensures
11391070
// stable ids for the [SemanticsNode]s of [TextSpan]s across

packages/flutter/lib/src/semantics/semantics.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class AttributedString {
346346
}
347347

348348
@override
349-
int get hashCode => Object.hash(string, attributes);
349+
int get hashCode => Object.hash(string, attributes,);
350350

351351
@override
352352
String toString() {
@@ -3805,8 +3805,7 @@ class SemanticsConfiguration {
38053805
/// which of them should be merged upwards into the parent SemanticsNode.
38063806
///
38073807
/// The input list of [SemanticsConfiguration]s can be empty if the rendering
3808-
/// object of this semantics configuration is a leaf node or child rendering
3809-
/// objects do not contribute to the semantics.
3808+
/// object of this semantics configuration is a leaf node.
38103809
ChildSemanticsConfigurationsDelegate? get childConfigurationsDelegate => _childConfigurationsDelegate;
38113810
ChildSemanticsConfigurationsDelegate? _childConfigurationsDelegate;
38123811
set childConfigurationsDelegate(ChildSemanticsConfigurationsDelegate? value) {

0 commit comments

Comments
 (0)