Skip to content

Commit e7baef0

Browse files
authored
Use DiagnosticableMixin instead of Diagnosticable for some conditionals. (#49726)
This PR fixes #49647 by changing DiagnosticableMixin.toDiagnosticsNode and other things to accept DiagnosticableMixin instead of requiring Diagnosticable.
1 parent 6ee8b65 commit e7baef0

8 files changed

Lines changed: 49 additions & 27 deletions

File tree

‎packages/flutter/lib/src/cupertino/icon_theme_data.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'colors.dart';
88

99
/// An [IconThemeData] subclass that automatically resolves its [color] when retrieved
1010
/// using [IconTheme.of].
11-
class CupertinoIconThemeData extends IconThemeData with DiagnosticableMixin {
11+
class CupertinoIconThemeData extends IconThemeData with DiagnosticableMixin implements Diagnosticable {
1212
/// Creates a [CupertinoIconThemeData].
1313
///
1414
/// The opacity applies to both explicit and default icon colors. The value

‎packages/flutter/lib/src/foundation/assertions.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class FlutterErrorDetails extends Diagnosticable {
351351
return longMessage;
352352
}
353353

354-
Diagnosticable _exceptionToDiagnosticable() {
354+
DiagnosticableMixin _exceptionToDiagnosticable() {
355355
if (exception is FlutterError) {
356356
return exception as FlutterError;
357357
}
@@ -374,7 +374,7 @@ class FlutterErrorDetails extends Diagnosticable {
374374
if (kReleaseMode) {
375375
return DiagnosticsNode.message(formatException());
376376
}
377-
final Diagnosticable diagnosticable = _exceptionToDiagnosticable();
377+
final DiagnosticableMixin diagnosticable = _exceptionToDiagnosticable();
378378
DiagnosticsNode summary;
379379
if (diagnosticable != null) {
380380
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
@@ -388,7 +388,7 @@ class FlutterErrorDetails extends Diagnosticable {
388388
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
389389
super.debugFillProperties(properties);
390390
final DiagnosticsNode verb = ErrorDescription('thrown${ context != null ? ErrorDescription(" $context") : ""}');
391-
final Diagnosticable diagnosticable = _exceptionToDiagnosticable();
391+
final DiagnosticableMixin diagnosticable = _exceptionToDiagnosticable();
392392
if (exception is NullThrownError) {
393393
properties.add(ErrorDescription('The null value was $verb.'));
394394
} else if (exception is num) {

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
26402640
Map<String, Object> toJsonMap(DiagnosticsSerializationDelegate delegate) {
26412641
final T v = value;
26422642
List<Map<String, Object>> properties;
2643-
if (delegate.expandPropertyValues && delegate.includeProperties && v is Diagnosticable && getProperties().isEmpty) {
2643+
if (delegate.expandPropertyValues && delegate.includeProperties && v is DiagnosticableMixin && getProperties().isEmpty) {
26442644
// Exclude children for expanded nodes to avoid cycles.
26452645
delegate = delegate.copyWith(subtreeDepth: 0, includeProperties: false);
26462646
properties = DiagnosticsNode.toJsonList(
@@ -2666,7 +2666,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
26662666
json['exception'] = exception.toString();
26672667
json['propertyType'] = propertyType.toString();
26682668
json['defaultLevel'] = describeEnum(_defaultLevel);
2669-
if (value is Diagnosticable || value is DiagnosticsNode)
2669+
if (value is DiagnosticableMixin || value is DiagnosticsNode)
26702670
json['isDiagnosticableValue'] = true;
26712671
if (v is num)
26722672
// Workaround for https://github.com/flutter/flutter/issues/39937#issuecomment-529558033.
@@ -2846,7 +2846,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
28462846
if (object is DiagnosticsNode) {
28472847
return object.getProperties();
28482848
}
2849-
if (object is Diagnosticable) {
2849+
if (object is DiagnosticableMixin) {
28502850
return object.toDiagnosticsNode(style: style).getProperties();
28512851
}
28522852
}
@@ -2860,7 +2860,7 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
28602860
if (object is DiagnosticsNode) {
28612861
return object.getChildren();
28622862
}
2863-
if (object is Diagnosticable) {
2863+
if (object is DiagnosticableMixin) {
28642864
return object.toDiagnosticsNode(style: style).getChildren();
28652865
}
28662866
}
@@ -2870,8 +2870,8 @@ class DiagnosticsProperty<T> extends DiagnosticsNode {
28702870

28712871
/// [DiagnosticsNode] that lazily calls the associated [Diagnosticable] [value]
28722872
/// to implement [getChildren] and [getProperties].
2873-
class DiagnosticableNode<T extends Diagnosticable> extends DiagnosticsNode {
2874-
/// Create a diagnostics describing a [Diagnosticable] value.
2873+
class DiagnosticableNode<T extends DiagnosticableMixin> extends DiagnosticsNode {
2874+
/// Create a diagnostics describing a [DiagnosticableMixin] value.
28752875
///
28762876
/// The [value] argument must not be null.
28772877
DiagnosticableNode({
@@ -2991,7 +2991,7 @@ String describeEnum(Object enumEntry) {
29912991
}
29922992

29932993
/// Builder to accumulate properties and configuration used to assemble a
2994-
/// [DiagnosticsNode] from a [Diagnosticable] object.
2994+
/// [DiagnosticsNode] from a [DiagnosticableMixin] object.
29952995
class DiagnosticPropertiesBuilder {
29962996
/// Creates a [DiagnosticPropertiesBuilder] with [properties] initialize to
29972997
/// an empty array.
@@ -3053,10 +3053,8 @@ abstract class Diagnosticable with DiagnosticableMixin {
30533053
const Diagnosticable();
30543054
}
30553055

3056-
/// A mixin class that provides the implementation for [Diagnosticable].
3057-
///
3058-
/// This mixin can be used to add diagnostics to a class which already has an
3059-
/// base class.
3056+
/// A mixin class for providing string and [DiagnosticsNode] debug
3057+
/// representations describing the properties of an object.
30603058
///
30613059
/// The string debug representation is generated from the intermediate
30623060
/// [DiagnosticsNode] representation. The [DiagnosticsNode] representation is
@@ -3108,9 +3106,9 @@ mixin DiagnosticableMixin {
31083106
/// relationship between the parent and the node. For example, pass
31093107
/// [DiagnosticsTreeStyle.offstage] to indicate that a node is offstage.
31103108
DiagnosticsNode toDiagnosticsNode({ String name, DiagnosticsTreeStyle style }) {
3111-
return DiagnosticableNode<Diagnosticable>(
3109+
return DiagnosticableNode<DiagnosticableMixin>(
31123110
name: name,
3113-
value: this as Diagnosticable,
3111+
value: this,
31143112
style: style,
31153113
);
31163114
}
@@ -3338,8 +3336,8 @@ mixin DiagnosticableMixin {
33383336
/// See also:
33393337
///
33403338
/// * [DiagnosticableTreeMixin], a mixin that implements this class.
3341-
/// * [Diagnosticable], which should be used instead of this class to provide
3342-
/// diagnostics for objects without children.
3339+
/// * [DiagnosticableMixin], which should be used instead of this class to
3340+
/// provide diagnostics for objects without children.
33433341
abstract class DiagnosticableTree extends Diagnosticable {
33443342
/// Abstract const constructor. This constructor enables subclasses to provide
33453343
/// const constructors so that they can be used in const expressions.
@@ -3643,7 +3641,7 @@ abstract class DiagnosticsSerializationDelegate {
36433641
/// will be included.
36443642
bool get includeProperties;
36453643

3646-
/// Whether properties that have a [Diagnosticable] as value should be
3644+
/// Whether properties that have a [DiagnosticableMixin] as value should be
36473645
/// expanded.
36483646
bool get expandPropertyValues;
36493647

‎packages/flutter/lib/src/scheduler/ticker.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class TickerProvider {
3838
Ticker createTicker(TickerCallback onTick);
3939
}
4040

41-
// TODO(jacobr): make Ticker Diagnosticable to simplify reporting errors
41+
// TODO(jacobr): make Ticker use DiagnosticableMixin to simplify reporting errors
4242
// related to a ticker.
4343
/// Calls its callback once per animation frame.
4444
///

‎packages/flutter/lib/src/widgets/shortcuts.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class KeySet<T extends KeyboardKey> {
110110
/// This is a thin wrapper around a [Set], but changes the equality comparison
111111
/// from an identity comparison to a contents comparison so that non-identical
112112
/// sets with the same keys in them will compare as equal.
113-
class LogicalKeySet extends KeySet<LogicalKeyboardKey> with DiagnosticableMixin {
113+
class LogicalKeySet extends KeySet<LogicalKeyboardKey> with DiagnosticableMixin implements Diagnosticable {
114114
/// A constructor for making a [LogicalKeySet] of up to four keys.
115115
///
116116
/// If you need a set of more than four keys, use [LogicalKeySet.fromSet].
@@ -201,7 +201,7 @@ class ShortcutMapProperty extends DiagnosticsProperty<Map<LogicalKeySet, Intent>
201201
///
202202
/// A [ShortcutManager] is obtained by calling [Shortcuts.of] on the context of
203203
/// the widget that you want to find a manager for.
204-
class ShortcutManager extends ChangeNotifier with DiagnosticableMixin {
204+
class ShortcutManager extends ChangeNotifier with DiagnosticableMixin implements Diagnosticable {
205205
/// Constructs a [ShortcutManager].
206206
///
207207
/// The [shortcuts] argument must not be null.

‎packages/flutter/lib/src/widgets/widget_inspector.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ class _DiagnosticsPathNode {
635635
}
636636

637637
List<_DiagnosticsPathNode> _followDiagnosticableChain(
638-
List<Diagnosticable> chain, {
638+
List<DiagnosticableMixin> chain, {
639639
String name,
640640
DiagnosticsTreeStyle style,
641641
}) {
@@ -644,7 +644,7 @@ List<_DiagnosticsPathNode> _followDiagnosticableChain(
644644
return path;
645645
DiagnosticsNode diagnostic = chain.first.toDiagnosticsNode(name: name, style: style);
646646
for (int i = 1; i < chain.length; i += 1) {
647-
final Diagnosticable target = chain[i];
647+
final DiagnosticableMixin target = chain[i];
648648
bool foundMatch = false;
649649
final List<DiagnosticsNode> children = diagnostic.getChildren();
650650
for (int j = 0; j < children.length; j += 1) {
@@ -1550,7 +1550,7 @@ mixin WidgetInspectorService {
15501550
return true;
15511551
}
15521552
final Object value = node.value;
1553-
if (value is! Diagnosticable) {
1553+
if (value is! DiagnosticableMixin) {
15541554
return true;
15551555
}
15561556
if (value is! Element || !isWidgetCreationTracked()) {

‎packages/flutter/test/foundation/diagnostics_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void validatePropertyJsonSerializationHelper(final Map<String, Object> json, Dia
180180
}
181181
expect(json['propertyType'], equals(property.propertyType.toString()));
182182
expect(json.containsKey('defaultLevel'), isTrue);
183-
if (property.value is Diagnosticable) {
183+
if (property.value is DiagnosticableMixin) {
184184
expect(json['isDiagnosticableValue'], isTrue);
185185
} else {
186186
expect(json.containsKey('isDiagnosticableValue'), isFalse);

‎packages/flutter/test/widgets/shortcuts_test.dart‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,29 @@ void main() {
322322
expect(description.length, equals(1));
323323
expect(description[0], equals('shortcuts: <Debug Label>'));
324324
});
325+
test('Shortcuts diagnostics work when manager specified.', () {
326+
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
327+
328+
Shortcuts(
329+
manager: ShortcutManager(),
330+
shortcuts: <LogicalKeySet, Intent>{
331+
LogicalKeySet(
332+
LogicalKeyboardKey.keyA,
333+
LogicalKeyboardKey.keyB,
334+
): const Intent(ActivateAction.key)
335+
},
336+
).debugFillProperties(builder);
337+
338+
final List<String> description = builder.properties
339+
.where((DiagnosticsNode node) {
340+
return !node.isFiltered(DiagnosticLevel.info);
341+
})
342+
.map((DiagnosticsNode node) => node.toString())
343+
.toList();
344+
345+
expect(description.length, equals(2));
346+
expect(description[0], equalsIgnoringHashCodes('manager: ShortcutManager#00000(shortcuts: {})'));
347+
expect(description[1], equalsIgnoringHashCodes('shortcuts: {{Key A + Key B}: Intent#00000(key: [<ActivateAction>])}'));
348+
});
325349
});
326350
}

0 commit comments

Comments
 (0)