From 6feedcc6a4862f2f0bca896567740ef1a06c2a42 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 30 May 2019 08:57:10 -0700 Subject: [PATCH] use toFixedAsString and DoubleProperty in diagnosticProperties (#33488) --- .../flutter/lib/src/foundation/debug.dart | 21 ++++++++++++++++++- .../lib/src/foundation/diagnostics.dart | 3 ++- .../lib/src/material/bottom_sheet_theme.dart | 2 +- .../lib/src/material/dialog_theme.dart | 2 +- .../floating_action_button_theme.dart | 10 ++++----- .../lib/src/material/snack_bar_theme.dart | 2 +- .../lib/src/painting/flutter_logo.dart | 2 +- .../lib/src/painting/image_stream.dart | 2 +- .../lib/src/painting/matrix_utils.dart | 21 +++++++++++-------- packages/flutter/lib/src/rendering/stack.dart | 12 +++++------ packages/flutter/lib/src/rendering/table.dart | 8 +++---- .../foundation/double_precision_test.dart | 20 ++++++++++++++++++ 12 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 packages/flutter/test/foundation/double_precision_test.dart diff --git a/packages/flutter/lib/src/foundation/debug.dart b/packages/flutter/lib/src/foundation/debug.dart index c445e977f3..a83c1d9883 100644 --- a/packages/flutter/lib/src/foundation/debug.dart +++ b/packages/flutter/lib/src/foundation/debug.dart @@ -24,7 +24,8 @@ import 'print.dart'; bool debugAssertAllFoundationVarsUnset(String reason, { DebugPrintCallback debugPrintOverride = debugPrintThrottled }) { assert(() { if (debugPrint != debugPrintOverride || - debugDefaultTargetPlatformOverride != null) + debugDefaultTargetPlatformOverride != null || + debugDoublePrecision != null) throw FlutterError(reason); return true; }()); @@ -73,3 +74,21 @@ Future debugInstrumentAction(String description, Future action()) { const Map timelineWhitelistArguments = { 'mode': 'basic', }; + +/// Configure [debugFormatDouble] using [num.toStringAsPrecision]. +/// +/// Defaults to null, which uses the default logic of [debugFormatDouble]. +int debugDoublePrecision; + +/// Formats a double to have standard formatting. +/// +/// This behavior can be overriden by [debugDoublePrecision]. +String debugFormatDouble(double value) { + if (value == null) { + return 'null'; + } + if (debugDoublePrecision != null) { + return value.toStringAsPrecision(debugDoublePrecision); + } + return value.toStringAsFixed(1); +} diff --git a/packages/flutter/lib/src/foundation/diagnostics.dart b/packages/flutter/lib/src/foundation/diagnostics.dart index 294192a8ea..9a07e20f34 100644 --- a/packages/flutter/lib/src/foundation/diagnostics.dart +++ b/packages/flutter/lib/src/foundation/diagnostics.dart @@ -7,6 +7,7 @@ import 'dart:math' as math; import 'package:meta/meta.dart'; import 'assertions.dart'; +import 'debug.dart'; // Examples can assume: // int rows, columns; @@ -1911,7 +1912,7 @@ class DoubleProperty extends _NumProperty { ); @override - String numberToString() => value?.toStringAsFixed(1); + String numberToString() => debugFormatDouble(value); } /// An int valued property with an optional unit the value is measured in. diff --git a/packages/flutter/lib/src/material/bottom_sheet_theme.dart b/packages/flutter/lib/src/material/bottom_sheet_theme.dart index 0ba47eabc4..6fd046a276 100644 --- a/packages/flutter/lib/src/material/bottom_sheet_theme.dart +++ b/packages/flutter/lib/src/material/bottom_sheet_theme.dart @@ -105,7 +105,7 @@ class BottomSheetThemeData extends Diagnosticable { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('backgroundColor', backgroundColor, defaultValue: null)); - properties.add(DiagnosticsProperty('elevation', elevation, defaultValue: null)); + properties.add(DoubleProperty('elevation', elevation, defaultValue: null)); properties.add(DiagnosticsProperty('shape', shape, defaultValue: null)); } } diff --git a/packages/flutter/lib/src/material/dialog_theme.dart b/packages/flutter/lib/src/material/dialog_theme.dart index 3fd0b79ef3..8b9ca5fccc 100644 --- a/packages/flutter/lib/src/material/dialog_theme.dart +++ b/packages/flutter/lib/src/material/dialog_theme.dart @@ -122,7 +122,7 @@ class DialogTheme extends Diagnosticable { super.debugFillProperties(properties); properties.add(DiagnosticsProperty('backgroundColor', backgroundColor)); properties.add(DiagnosticsProperty('shape', shape, defaultValue: null)); - properties.add(DiagnosticsProperty('elevation', elevation)); + properties.add(DoubleProperty('elevation', elevation)); properties.add(DiagnosticsProperty('titleTextStyle', titleTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty('contentTextStyle', contentTextStyle, defaultValue: null)); } diff --git a/packages/flutter/lib/src/material/floating_action_button_theme.dart b/packages/flutter/lib/src/material/floating_action_button_theme.dart index 606aa2d0e0..919207d06e 100644 --- a/packages/flutter/lib/src/material/floating_action_button_theme.dart +++ b/packages/flutter/lib/src/material/floating_action_button_theme.dart @@ -179,11 +179,11 @@ class FloatingActionButtonThemeData extends Diagnosticable { properties.add(DiagnosticsProperty('backgroundColor', backgroundColor, defaultValue: defaultData.backgroundColor)); properties.add(DiagnosticsProperty('focusColor', focusColor, defaultValue: defaultData.focusColor)); properties.add(DiagnosticsProperty('hoverColor', hoverColor, defaultValue: defaultData.hoverColor)); - properties.add(DiagnosticsProperty('elevation', elevation, defaultValue: defaultData.elevation)); - properties.add(DiagnosticsProperty('focusElevation', focusElevation, defaultValue: defaultData.focusElevation)); - properties.add(DiagnosticsProperty('hoverElevation', hoverElevation, defaultValue: defaultData.hoverElevation)); - properties.add(DiagnosticsProperty('disabledElevation', disabledElevation, defaultValue: defaultData.disabledElevation)); - properties.add(DiagnosticsProperty('highlightElevation', highlightElevation, defaultValue: defaultData.highlightElevation)); + properties.add(DoubleProperty('elevation', elevation, defaultValue: defaultData.elevation)); + properties.add(DoubleProperty('focusElevation', focusElevation, defaultValue: defaultData.focusElevation)); + properties.add(DoubleProperty('hoverElevation', hoverElevation, defaultValue: defaultData.hoverElevation)); + properties.add(DoubleProperty('disabledElevation', disabledElevation, defaultValue: defaultData.disabledElevation)); + properties.add(DoubleProperty('highlightElevation', highlightElevation, defaultValue: defaultData.highlightElevation)); properties.add(DiagnosticsProperty('shape', shape, defaultValue: defaultData.shape)); } } diff --git a/packages/flutter/lib/src/material/snack_bar_theme.dart b/packages/flutter/lib/src/material/snack_bar_theme.dart index 01e3fce31d..39aeb1e7d7 100644 --- a/packages/flutter/lib/src/material/snack_bar_theme.dart +++ b/packages/flutter/lib/src/material/snack_bar_theme.dart @@ -166,7 +166,7 @@ class SnackBarThemeData extends Diagnosticable { properties.add(DiagnosticsProperty('backgroundColor', backgroundColor, defaultValue: null)); properties.add(DiagnosticsProperty('actionTextColor', actionTextColor, defaultValue: null)); properties.add(DiagnosticsProperty('disabledActionTextColor', disabledActionTextColor, defaultValue: null)); - properties.add(DiagnosticsProperty('elevation', elevation, defaultValue: null)); + properties.add(DoubleProperty('elevation', elevation, defaultValue: null)); properties.add(DiagnosticsProperty('shape', shape, defaultValue: null)); properties.add(DiagnosticsProperty('behavior', behavior, defaultValue: null)); } diff --git a/packages/flutter/lib/src/painting/flutter_logo.dart b/packages/flutter/lib/src/painting/flutter_logo.dart index 7c9e20885a..b4de45f429 100644 --- a/packages/flutter/lib/src/painting/flutter_logo.dart +++ b/packages/flutter/lib/src/painting/flutter_logo.dart @@ -238,7 +238,7 @@ class FlutterLogoDecoration extends Decoration { properties.add(DiagnosticsNode.message('$lightColor/$darkColor on $textColor')); properties.add(EnumProperty('style', style)); if (_inTransition) - properties.add(DiagnosticsNode.message('transition $_position:$_opacity')); + properties.add(DiagnosticsNode.message('transition ${debugFormatDouble(_position)}:${debugFormatDouble(_opacity)}')); } } diff --git a/packages/flutter/lib/src/painting/image_stream.dart b/packages/flutter/lib/src/painting/image_stream.dart index 6280c01a63..a6af101c53 100644 --- a/packages/flutter/lib/src/painting/image_stream.dart +++ b/packages/flutter/lib/src/painting/image_stream.dart @@ -41,7 +41,7 @@ class ImageInfo { final double scale; @override - String toString() => '$image @ ${scale}x'; + String toString() => '$image @ ${debugFormatDouble(scale)}x'; @override int get hashCode => hashValues(image, scale); diff --git a/packages/flutter/lib/src/painting/matrix_utils.dart b/packages/flutter/lib/src/painting/matrix_utils.dart index c1f478cafc..4d0b5fb326 100644 --- a/packages/flutter/lib/src/painting/matrix_utils.dart +++ b/packages/flutter/lib/src/painting/matrix_utils.dart @@ -265,9 +265,12 @@ class MatrixUtils { List debugDescribeTransform(Matrix4 transform) { if (transform == null) return const ['null']; - final List matrix = transform.toString().split('\n').toList(); - matrix.removeLast(); - return matrix; + return [ + '[0] ${debugFormatDouble(transform.entry(0, 0))},${debugFormatDouble(transform.entry(0, 1))},${debugFormatDouble(transform.entry(0, 2))},${debugFormatDouble(transform.entry(0, 3))}', + '[1] ${debugFormatDouble(transform.entry(1, 0))},${debugFormatDouble(transform.entry(1, 1))},${debugFormatDouble(transform.entry(1, 2))},${debugFormatDouble(transform.entry(1, 3))}', + '[2] ${debugFormatDouble(transform.entry(2, 0))},${debugFormatDouble(transform.entry(2, 1))},${debugFormatDouble(transform.entry(2, 2))},${debugFormatDouble(transform.entry(2, 3))}', + '[3] ${debugFormatDouble(transform.entry(3, 0))},${debugFormatDouble(transform.entry(3, 1))},${debugFormatDouble(transform.entry(3, 2))},${debugFormatDouble(transform.entry(3, 3))}', + ]; } /// Property which handles [Matrix4] that represent transforms. @@ -296,13 +299,13 @@ class TransformProperty extends DiagnosticsProperty { if (parentConfiguration != null && !parentConfiguration.lineBreakProperties) { // Format the value on a single line to be compatible with the parent's // style. - final List rows = [ - value.getRow(0), - value.getRow(1), - value.getRow(2), - value.getRow(3), + final List values = [ + '${debugFormatDouble(value.entry(0, 0))},${debugFormatDouble(value.entry(0, 1))},${debugFormatDouble(value.entry(0, 2))},${debugFormatDouble(value.entry(0, 3))}', + '${debugFormatDouble(value.entry(1, 0))},${debugFormatDouble(value.entry(1, 1))},${debugFormatDouble(value.entry(1, 2))},${debugFormatDouble(value.entry(1, 3))}', + '${debugFormatDouble(value.entry(2, 0))},${debugFormatDouble(value.entry(2, 1))},${debugFormatDouble(value.entry(2, 2))},${debugFormatDouble(value.entry(2, 3))}', + '${debugFormatDouble(value.entry(3, 0))},${debugFormatDouble(value.entry(3, 1))},${debugFormatDouble(value.entry(3, 2))},${debugFormatDouble(value.entry(3, 3))}', ]; - return '[${rows.join("; ")}]'; + return '[${values.join('; ')}]'; } return debugDescribeTransform(value).join('\n'); } diff --git a/packages/flutter/lib/src/rendering/stack.dart b/packages/flutter/lib/src/rendering/stack.dart index ff69afbfe6..5404395931 100644 --- a/packages/flutter/lib/src/rendering/stack.dart +++ b/packages/flutter/lib/src/rendering/stack.dart @@ -215,17 +215,17 @@ class StackParentData extends ContainerBoxParentData { String toString() { final List values = []; if (top != null) - values.add('top=$top'); + values.add('top=${debugFormatDouble(top)}'); if (right != null) - values.add('right=$right'); + values.add('right=${debugFormatDouble(right)}'); if (bottom != null) - values.add('bottom=$bottom'); + values.add('bottom=${debugFormatDouble(bottom)}'); if (left != null) - values.add('left=$left'); + values.add('left=${debugFormatDouble(left)}'); if (width != null) - values.add('width=$width'); + values.add('width=${debugFormatDouble(width)}'); if (height != null) - values.add('height=$height'); + values.add('height=${debugFormatDouble(height)}'); if (values.isEmpty) values.add('not positioned'); values.add(super.toString()); diff --git a/packages/flutter/lib/src/rendering/table.dart b/packages/flutter/lib/src/rendering/table.dart index 1e15b26682..49e19aa3ad 100644 --- a/packages/flutter/lib/src/rendering/table.dart +++ b/packages/flutter/lib/src/rendering/table.dart @@ -141,7 +141,7 @@ class FixedColumnWidth extends TableColumnWidth { } @override - String toString() => '$runtimeType($value)'; + String toString() => '$runtimeType(${debugFormatDouble(value)})'; } /// Sizes the column to a fraction of the table's constraints' maxWidth. @@ -210,7 +210,7 @@ class FlexColumnWidth extends TableColumnWidth { } @override - String toString() => '$runtimeType($value)'; + String toString() => '$runtimeType(${debugFormatDouble(value)})'; } /// Sizes the column such that it is the size that is the maximum of @@ -1178,8 +1178,8 @@ class RenderTable extends RenderBox { properties.add(DiagnosticsProperty>('specified column widths', _columnWidths, level: _columnWidths.isEmpty ? DiagnosticLevel.hidden : DiagnosticLevel.info)); properties.add(DiagnosticsProperty('default column width', defaultColumnWidth)); properties.add(MessageProperty('table size', '$columns\u00D7$rows')); - properties.add(IterableProperty('column offsets', _columnLefts, ifNull: 'unknown')); - properties.add(IterableProperty('row offsets', _rowTops, ifNull: 'unknown')); + properties.add(IterableProperty('column offsets', _columnLefts?.map(debugFormatDouble), ifNull: 'unknown')); + properties.add(IterableProperty('row offsets', _rowTops?.map(debugFormatDouble), ifNull: 'unknown')); } @override diff --git a/packages/flutter/test/foundation/double_precision_test.dart b/packages/flutter/test/foundation/double_precision_test.dart new file mode 100644 index 0000000000..4af84df6c3 --- /dev/null +++ b/packages/flutter/test/foundation/double_precision_test.dart @@ -0,0 +1,20 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/foundation.dart'; +import '../flutter_test_alternative.dart'; + +void main() { + test('debugFormatDouble formats doubles', () { + expect(debugFormatDouble(1), '1.0'); + expect(debugFormatDouble(1.1), '1.1'); + expect(debugFormatDouble(null), 'null'); + }); + + test('debugDoublePrecision can control double precision', () { + debugDoublePrecision = 3; + expect(debugFormatDouble(1), '1.00'); + debugDoublePrecision = null; + }); +}