Skip to content

Commit 802bae0

Browse files
authored
Allow requesting a reduced widget tree with getRootWidgetTree service extension (#157309)
1 parent 99ddbc3 commit 802bae0

File tree

7 files changed

+380
-45
lines changed

7 files changed

+380
-45
lines changed

packages/flutter/lib/src/foundation/diagnostics.dart

Lines changed: 114 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,12 +1605,29 @@ abstract class DiagnosticsNode {
16051605
/// by this method and interactive tree views in the Flutter IntelliJ
16061606
/// plugin.
16071607
@mustCallSuper
1608-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
1608+
Map<String, Object?> toJsonMap(
1609+
DiagnosticsSerializationDelegate delegate, {
1610+
bool fullDetails = true,
1611+
}) {
16091612
Map<String, Object?> result = <String, Object?>{};
16101613
assert(() {
16111614
final bool hasChildren = getChildren().isNotEmpty;
1612-
result = <String, Object?>{
1615+
final Map<String, Object?> essentialDetails = <String, Object?>{
16131616
'description': toDescription(),
1617+
'shouldIndent': style != DiagnosticsTreeStyle.flat &&
1618+
style != DiagnosticsTreeStyle.error,
1619+
...delegate.additionalNodeProperties(this, fullDetails: fullDetails),
1620+
if (delegate.subtreeDepth > 0)
1621+
'children': toJsonList(
1622+
delegate.filterChildren(getChildren(), this),
1623+
this,
1624+
delegate,
1625+
fullDetails: fullDetails,
1626+
),
1627+
};
1628+
1629+
result = !fullDetails ? essentialDetails : <String, Object?>{
1630+
...essentialDetails,
16141631
'type': runtimeType.toString(),
16151632
if (name != null)
16161633
'name': name,
@@ -1634,18 +1651,12 @@ abstract class DiagnosticsNode {
16341651
'allowWrap': allowWrap,
16351652
if (allowNameWrap)
16361653
'allowNameWrap': allowNameWrap,
1637-
...delegate.additionalNodeProperties(this),
16381654
if (delegate.includeProperties)
16391655
'properties': toJsonList(
16401656
delegate.filterProperties(getProperties(), this),
16411657
this,
16421658
delegate,
1643-
),
1644-
if (delegate.subtreeDepth > 0)
1645-
'children': toJsonList(
1646-
delegate.filterChildren(getChildren(), this),
1647-
this,
1648-
delegate,
1659+
fullDetails: fullDetails,
16491660
),
16501661
};
16511662
return true;
@@ -1661,8 +1672,9 @@ abstract class DiagnosticsNode {
16611672
static List<Map<String, Object?>> toJsonList(
16621673
List<DiagnosticsNode>? nodes,
16631674
DiagnosticsNode? parent,
1664-
DiagnosticsSerializationDelegate delegate,
1665-
) {
1675+
DiagnosticsSerializationDelegate delegate, {
1676+
bool fullDetails = true,
1677+
}) {
16661678
bool truncated = false;
16671679
if (nodes == null) {
16681680
return const <Map<String, Object?>>[];
@@ -1674,7 +1686,10 @@ abstract class DiagnosticsNode {
16741686
truncated = true;
16751687
}
16761688
final List<Map<String, Object?>> json = nodes.map<Map<String, Object?>>((DiagnosticsNode node) {
1677-
return node.toJsonMap(delegate.delegateForNode(node));
1689+
return node.toJsonMap(
1690+
delegate.delegateForNode(node),
1691+
fullDetails: fullDetails,
1692+
);
16781693
}).toList();
16791694
if (truncated) {
16801695
json.last['truncated'] = true;
@@ -1857,8 +1872,17 @@ class StringProperty extends DiagnosticsProperty<String> {
18571872
final bool quoted;
18581873

18591874
@override
1860-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
1861-
final Map<String, Object?> json = super.toJsonMap(delegate);
1875+
Map<String, Object?> toJsonMap(
1876+
DiagnosticsSerializationDelegate delegate, {
1877+
bool fullDetails = true,
1878+
}) {
1879+
final Map<String, Object?> json = super.toJsonMap(
1880+
delegate,
1881+
fullDetails: fullDetails,
1882+
);
1883+
if (!fullDetails) {
1884+
return json;
1885+
}
18621886
json['quoted'] = quoted;
18631887
return json;
18641888
}
@@ -1913,8 +1937,18 @@ abstract class _NumProperty<T extends num> extends DiagnosticsProperty<T> {
19131937
}) : super.lazy();
19141938

19151939
@override
1916-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
1917-
final Map<String, Object?> json = super.toJsonMap(delegate);
1940+
Map<String, Object?> toJsonMap(
1941+
DiagnosticsSerializationDelegate delegate, {
1942+
bool fullDetails = true,
1943+
}) {
1944+
final Map<String, Object?> json = super.toJsonMap(
1945+
delegate,
1946+
fullDetails: fullDetails,
1947+
);
1948+
if (!fullDetails) {
1949+
return json;
1950+
}
1951+
19181952
if (unit != null) {
19191953
json['unit'] = unit;
19201954
}
@@ -2097,8 +2131,17 @@ class FlagProperty extends DiagnosticsProperty<bool> {
20972131
);
20982132

20992133
@override
2100-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
2101-
final Map<String, Object?> json = super.toJsonMap(delegate);
2134+
Map<String, Object?> toJsonMap(
2135+
DiagnosticsSerializationDelegate delegate, {
2136+
bool fullDetails = true,
2137+
}) {
2138+
final Map<String, Object?> json = super.toJsonMap(
2139+
delegate,
2140+
fullDetails: fullDetails,
2141+
);
2142+
if (!fullDetails) {
2143+
return json;
2144+
}
21022145
if (ifTrue != null) {
21032146
json['ifTrue'] = ifTrue;
21042147
}
@@ -2219,8 +2262,17 @@ class IterableProperty<T> extends DiagnosticsProperty<Iterable<T>> {
22192262
}
22202263

22212264
@override
2222-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
2223-
final Map<String, Object?> json = super.toJsonMap(delegate);
2265+
Map<String, Object?> toJsonMap(
2266+
DiagnosticsSerializationDelegate delegate, {
2267+
bool fullDetails = true,
2268+
}) {
2269+
final Map<String, Object?> json = super.toJsonMap(
2270+
delegate,
2271+
fullDetails: fullDetails,
2272+
);
2273+
if (!fullDetails) {
2274+
return json;
2275+
}
22242276
if (value != null) {
22252277
json['values'] = value!.map<String>((T value) => value.toString()).toList();
22262278
}
@@ -2357,8 +2409,17 @@ class ObjectFlagProperty<T> extends DiagnosticsProperty<T> {
23572409
}
23582410

23592411
@override
2360-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
2361-
final Map<String, Object?> json = super.toJsonMap(delegate);
2412+
Map<String, Object?> toJsonMap(
2413+
DiagnosticsSerializationDelegate delegate, {
2414+
bool fullDetails = true,
2415+
}) {
2416+
final Map<String, Object?> json = super.toJsonMap(
2417+
delegate,
2418+
fullDetails: fullDetails,
2419+
);
2420+
if (!fullDetails) {
2421+
return json;
2422+
}
23622423
if (ifPresent != null) {
23632424
json['ifPresent'] = ifPresent;
23642425
}
@@ -2435,8 +2496,17 @@ class FlagsSummary<T> extends DiagnosticsProperty<Map<String, T?>> {
24352496
}
24362497

24372498
@override
2438-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
2439-
final Map<String, Object?> json = super.toJsonMap(delegate);
2499+
Map<String, Object?> toJsonMap(
2500+
DiagnosticsSerializationDelegate delegate, {
2501+
bool fullDetails = true,
2502+
}) {
2503+
final Map<String, Object?> json = super.toJsonMap(
2504+
delegate,
2505+
fullDetails: fullDetails,
2506+
);
2507+
if (!fullDetails) {
2508+
return json;
2509+
}
24402510
if (value.isNotEmpty) {
24412511
json['values'] = _formattedValues().toList();
24422512
}
@@ -2555,7 +2625,10 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
25552625
final bool allowNameWrap;
25562626

25572627
@override
2558-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
2628+
Map<String, Object?> toJsonMap(
2629+
DiagnosticsSerializationDelegate delegate, {
2630+
bool fullDetails = true,
2631+
}) {
25592632
final T? v = value;
25602633
List<Map<String, Object?>>? properties;
25612634
if (delegate.expandPropertyValues && delegate.includeProperties && v is Diagnosticable && getProperties().isEmpty) {
@@ -2565,9 +2638,16 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
25652638
delegate.filterProperties(v.toDiagnosticsNode().getProperties(), this),
25662639
this,
25672640
delegate,
2641+
fullDetails: fullDetails,
25682642
);
25692643
}
2570-
final Map<String, Object?> json = super.toJsonMap(delegate);
2644+
final Map<String, Object?> json = super.toJsonMap(
2645+
delegate,
2646+
fullDetails: fullDetails,
2647+
);
2648+
if (!fullDetails) {
2649+
return json;
2650+
}
25712651
if (properties != null) {
25722652
json['properties'] = properties;
25732653
}
@@ -3503,7 +3583,10 @@ abstract class DiagnosticsSerializationDelegate {
35033583
///
35043584
/// This method is called for every [DiagnosticsNode] that's included in
35053585
/// the serialization.
3506-
Map<String, Object?> additionalNodeProperties(DiagnosticsNode node);
3586+
Map<String, Object?> additionalNodeProperties(
3587+
DiagnosticsNode node, {
3588+
bool fullDetails = true,
3589+
});
35073590

35083591
/// Filters the list of [DiagnosticsNode]s that will be included as children
35093592
/// for the given `owner` node.
@@ -3595,7 +3678,10 @@ class _DefaultDiagnosticsSerializationDelegate implements DiagnosticsSerializati
35953678
});
35963679

35973680
@override
3598-
Map<String, Object?> additionalNodeProperties(DiagnosticsNode node) {
3681+
Map<String, Object?> additionalNodeProperties(
3682+
DiagnosticsNode node, {
3683+
bool fullDetails = true,
3684+
}) {
35993685
return const <String, Object?>{};
36003686
}
36013687

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,17 @@ class ColorProperty extends DiagnosticsProperty<Color> {
495495
});
496496

497497
@override
498-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
499-
final Map<String, Object?> json = super.toJsonMap(delegate);
498+
Map<String, Object?> toJsonMap(
499+
DiagnosticsSerializationDelegate delegate, {
500+
bool fullDetails = true,
501+
}) {
502+
final Map<String, Object?> json = super.toJsonMap(
503+
delegate,
504+
fullDetails: fullDetails,
505+
);
506+
if (!fullDetails) {
507+
return json;
508+
}
500509
if (value != null) {
501510
json['valueProperties'] = <String, Object>{
502511
'red': value!.red,

packages/flutter/lib/src/widgets/framework.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5379,13 +5379,18 @@ class _ElementDiagnosticableTreeNode extends DiagnosticableTreeNode {
53795379
final bool stateful;
53805380

53815381
@override
5382-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
5383-
final Map<String, Object?> json = super.toJsonMap(delegate);
5382+
Map<String, Object?> toJsonMap(
5383+
DiagnosticsSerializationDelegate delegate, {
5384+
bool fullDetails = true,
5385+
}) {
5386+
final Map<String, Object?> json = super.toJsonMap(delegate, fullDetails: fullDetails,);
53845387
final Element element = value as Element;
53855388
if (!element.debugIsDefunct) {
53865389
json['widgetRuntimeType'] = element.widget.runtimeType.toString();
53875390
}
5388-
json['stateful'] = stateful;
5391+
if (fullDetails) {
5392+
json['stateful'] = stateful;
5393+
}
53895394
return json;
53905395
}
53915396
}

packages/flutter/lib/src/widgets/icon_data.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,17 @@ class IconDataProperty extends DiagnosticsProperty<IconData> {
121121
});
122122

123123
@override
124-
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
125-
final Map<String, Object?> json = super.toJsonMap(delegate);
124+
Map<String, Object?> toJsonMap(
125+
DiagnosticsSerializationDelegate delegate, {
126+
bool fullDetails = true,
127+
}) {
128+
final Map<String, Object?> json = super.toJsonMap(
129+
delegate,
130+
fullDetails: fullDetails,
131+
);
132+
if (!fullDetails) {
133+
return json;
134+
}
126135
if (value != null) {
127136
json['valueProperties'] = <String, Object>{
128137
'codePoint': value!.codePoint,

0 commit comments

Comments
 (0)