Give RenderObject a useful toString().

This makes debugging the render tree a lot easier. Just print the node
you care about, and you get an indented tree view of its subtree,
including settings. New subclasses should implement the new virtual
method debugDescribeSettings() to expose new settings.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/1157993005
This commit is contained in:
Hixie 2015-06-04 14:10:07 -07:00
parent 0fcbeef342
commit 8ee5aa0bc6
5 changed files with 53 additions and 0 deletions

View File

@ -130,6 +130,9 @@ class RenderScaffold extends RenderBox {
}
}
String debugDescribeChildren(String prefix) {
return _slots.keys.map((slot) => '${prefix}${slot}: ${_slots[slot].toString(prefix)}').join('\n');
}
}
class Scaffold extends RenderObjectWrapper {

View File

@ -109,6 +109,7 @@ class BoxConstraints {
class BoxParentData extends ParentData {
sky.Point position = new sky.Point(0.0, 0.0);
String toString() => 'position=$position';
}
abstract class RenderBox extends RenderObject {
@ -213,6 +214,8 @@ class RenderSizedBox extends RenderProxyBox {
if (child != null)
child.layout(new BoxConstraints.tight(size));
}
String debugDescribeSettings(String prefix) => '${prefix}desiredSize: ${desiredSize}';
}
class RenderClip extends RenderProxyBox {
@ -285,6 +288,7 @@ class RenderPadding extends RenderBox with RenderObjectWithChildMixin<RenderBox>
}
}
String debugDescribeSettings(String prefix) => '${prefix}padding: ${padding}';
}
class RenderImage extends RenderBox {
@ -359,6 +363,8 @@ class RenderImage extends RenderBox {
if (needsScale)
canvas.restore();
}
String debugDescribeSettings(String prefix) => '${prefix}url: ${src}\n${prefix}dimensions: ${requestedSize}';
}
class BorderSide {
@ -417,6 +423,17 @@ class BoxDecoration {
final sky.Color backgroundColor;
final Border border;
String toString([String prefix = '']) {
List<String> result = [];
if (backgroundColor != null)
result.add('${prefix}backgroundColor: $backgroundColor');
if (border != null)
result.add('${prefix}border: $border');
if (result.isEmpty)
return '${prefix}<no decorations specified>';
return result.join('\n');
}
}
class RenderDecoratedBox extends RenderProxyBox {
@ -495,6 +512,8 @@ class RenderDecoratedBox extends RenderProxyBox {
super.paint(canvas);
}
String debugDescribeSettings(String prefix) => '${prefix}decoration:\n${decoration.toString(prefix + " ")}';
}
class RenderTransform extends RenderProxyBox {

View File

@ -13,6 +13,7 @@ class FlexBoxParentData extends BoxParentData with ContainerParentDataMixin<Rend
flex = other.flex;
super.merge(other);
}
String toString() => '${super.toString()}; flex=$flex';
}
enum FlexDirection { horizontal, vertical }

View File

@ -16,6 +16,7 @@ class ParentData {
// override this in subclasses to merge in data from other into this
assert(other.runtimeType == this.runtimeType);
}
String toString() => '<none>';
}
const kLayoutDirections = 4;
@ -219,6 +220,18 @@ abstract class RenderObject extends AbstractNode {
// }
// You must not add yourself to /result/ if you return false.
String toString([String prefix = '']) {
String header = '${runtimeType}\n';
prefix += ' ';
String settings = '${debugDescribeSettings(prefix)}';
if (settings != '')
settings += '\n';
return '${header}${settings}${debugDescribeChildren(prefix)}';
}
String debugDescribeSettings(String prefix) => '${prefix}parentData: ${parentData}';
String debugDescribeChildren(String prefix) => '';
}
class HitTestResult {
@ -252,6 +265,7 @@ abstract class RenderObjectWithChildMixin<ChildType extends RenderObject> {
if (_child != null)
_child.detach();
}
String debugDescribeChildren(String prefix) => '${prefix}child: ${child.toString(prefix)}';
}
@ -401,4 +415,18 @@ abstract class ContainerRenderObjectMixin<ChildType extends RenderObject, Parent
assert(child.parentData is ParentDataType);
return child.parentData.nextSibling;
}
String debugDescribeChildren(String prefix) {
String result = '';
int count = 1;
ChildType child = _firstChild;
while (child != null) {
if (result != '')
result += '\n';
result += '${prefix}child ${count}: ${child.toString(prefix)}';
count += 1;
child = child.parentData.nextSibling;
}
return result;
}
}

View File

@ -61,4 +61,6 @@ class RenderParagraph extends RenderBox {
}
// we should probably expose a way to do precise (inter-glpyh) hit testing
String debugDescribeChildren(String prefix) => '${prefix}TEXT (${color}): ${text}';
}