Skip to content

Commit

Permalink
Allow requesting a reduced widget tree with getRootWidgetTree servi…
Browse files Browse the repository at this point in the history
…ce extension (flutter#157309)
  • Loading branch information
elliette authored and M97Chahboun committed Oct 30, 2024
1 parent 158298c commit 93e96ab
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 45 deletions.
142 changes: 114 additions & 28 deletions packages/flutter/lib/src/foundation/diagnostics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1605,12 +1605,29 @@ abstract class DiagnosticsNode {
/// by this method and interactive tree views in the Flutter IntelliJ
/// plugin.
@mustCallSuper
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
Map<String, Object?> result = <String, Object?>{};
assert(() {
final bool hasChildren = getChildren().isNotEmpty;
result = <String, Object?>{
final Map<String, Object?> essentialDetails = <String, Object?>{
'description': toDescription(),
'shouldIndent': style != DiagnosticsTreeStyle.flat &&
style != DiagnosticsTreeStyle.error,
...delegate.additionalNodeProperties(this, fullDetails: fullDetails),
if (delegate.subtreeDepth > 0)
'children': toJsonList(
delegate.filterChildren(getChildren(), this),
this,
delegate,
fullDetails: fullDetails,
),
};

result = !fullDetails ? essentialDetails : <String, Object?>{
...essentialDetails,
'type': runtimeType.toString(),
if (name != null)
'name': name,
Expand All @@ -1634,18 +1651,12 @@ abstract class DiagnosticsNode {
'allowWrap': allowWrap,
if (allowNameWrap)
'allowNameWrap': allowNameWrap,
...delegate.additionalNodeProperties(this),
if (delegate.includeProperties)
'properties': toJsonList(
delegate.filterProperties(getProperties(), this),
this,
delegate,
),
if (delegate.subtreeDepth > 0)
'children': toJsonList(
delegate.filterChildren(getChildren(), this),
this,
delegate,
fullDetails: fullDetails,
),
};
return true;
Expand All @@ -1661,8 +1672,9 @@ abstract class DiagnosticsNode {
static List<Map<String, Object?>> toJsonList(
List<DiagnosticsNode>? nodes,
DiagnosticsNode? parent,
DiagnosticsSerializationDelegate delegate,
) {
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
bool truncated = false;
if (nodes == null) {
return const <Map<String, Object?>>[];
Expand All @@ -1674,7 +1686,10 @@ abstract class DiagnosticsNode {
truncated = true;
}
final List<Map<String, Object?>> json = nodes.map<Map<String, Object?>>((DiagnosticsNode node) {
return node.toJsonMap(delegate.delegateForNode(node));
return node.toJsonMap(
delegate.delegateForNode(node),
fullDetails: fullDetails,
);
}).toList();
if (truncated) {
json.last['truncated'] = true;
Expand Down Expand Up @@ -1857,8 +1872,17 @@ class StringProperty extends DiagnosticsProperty<String> {
final bool quoted;

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
json['quoted'] = quoted;
return json;
}
Expand Down Expand Up @@ -1913,8 +1937,18 @@ abstract class _NumProperty<T extends num> extends DiagnosticsProperty<T> {
}) : super.lazy();

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}

if (unit != null) {
json['unit'] = unit;
}
Expand Down Expand Up @@ -2097,8 +2131,17 @@ class FlagProperty extends DiagnosticsProperty<bool> {
);

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (ifTrue != null) {
json['ifTrue'] = ifTrue;
}
Expand Down Expand Up @@ -2219,8 +2262,17 @@ class IterableProperty<T> extends DiagnosticsProperty<Iterable<T>> {
}

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (value != null) {
json['values'] = value!.map<String>((T value) => value.toString()).toList();
}
Expand Down Expand Up @@ -2357,8 +2409,17 @@ class ObjectFlagProperty<T> extends DiagnosticsProperty<T> {
}

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (ifPresent != null) {
json['ifPresent'] = ifPresent;
}
Expand Down Expand Up @@ -2435,8 +2496,17 @@ class FlagsSummary<T> extends DiagnosticsProperty<Map<String, T?>> {
}

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (value.isNotEmpty) {
json['values'] = _formattedValues().toList();
}
Expand Down Expand Up @@ -2555,7 +2625,10 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
final bool allowNameWrap;

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final T? v = value;
List<Map<String, Object?>>? properties;
if (delegate.expandPropertyValues && delegate.includeProperties && v is Diagnosticable && getProperties().isEmpty) {
Expand All @@ -2565,9 +2638,16 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
delegate.filterProperties(v.toDiagnosticsNode().getProperties(), this),
this,
delegate,
fullDetails: fullDetails,
);
}
final Map<String, Object?> json = super.toJsonMap(delegate);
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (properties != null) {
json['properties'] = properties;
}
Expand Down Expand Up @@ -3503,7 +3583,10 @@ abstract class DiagnosticsSerializationDelegate {
///
/// This method is called for every [DiagnosticsNode] that's included in
/// the serialization.
Map<String, Object?> additionalNodeProperties(DiagnosticsNode node);
Map<String, Object?> additionalNodeProperties(
DiagnosticsNode node, {
bool fullDetails = true,
});

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

@override
Map<String, Object?> additionalNodeProperties(DiagnosticsNode node) {
Map<String, Object?> additionalNodeProperties(
DiagnosticsNode node, {
bool fullDetails = true,
}) {
return const <String, Object?>{};
}

Expand Down
13 changes: 11 additions & 2 deletions packages/flutter/lib/src/painting/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,17 @@ class ColorProperty extends DiagnosticsProperty<Color> {
});

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (value != null) {
json['valueProperties'] = <String, Object>{
'red': value!.red,
Expand Down
11 changes: 8 additions & 3 deletions packages/flutter/lib/src/widgets/framework.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5379,13 +5379,18 @@ class _ElementDiagnosticableTreeNode extends DiagnosticableTreeNode {
final bool stateful;

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(delegate, fullDetails: fullDetails,);
final Element element = value as Element;
if (!element.debugIsDefunct) {
json['widgetRuntimeType'] = element.widget.runtimeType.toString();
}
json['stateful'] = stateful;
if (fullDetails) {
json['stateful'] = stateful;
}
return json;
}
}
Expand Down
13 changes: 11 additions & 2 deletions packages/flutter/lib/src/widgets/icon_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,17 @@ class IconDataProperty extends DiagnosticsProperty<IconData> {
});

@override
Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
final Map<String, Object?> json = super.toJsonMap(delegate);
Map<String, Object?> toJsonMap(
DiagnosticsSerializationDelegate delegate, {
bool fullDetails = true,
}) {
final Map<String, Object?> json = super.toJsonMap(
delegate,
fullDetails: fullDetails,
);
if (!fullDetails) {
return json;
}
if (value != null) {
json['valueProperties'] = <String, Object>{
'codePoint': value!.codePoint,
Expand Down
Loading

0 comments on commit 93e96ab

Please sign in to comment.