From 4a70cfed5fa8baf7ca05270c307eab9640b13804 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 6 Aug 2015 22:26:02 -0700 Subject: [PATCH 1/2] Rename various text-related classes - Inline -> Pargraph. This class is actually a box, not an inline. It's really a wrapper for RenderParagraph, so Paragraph is the normal name. - InlineBase -> RenderInline. The name we used in C++ for the base class of all inlines was RenderInline, which removes the ugly "Base" suffix. - InlineText -> RenderText. Aligns this name with C++. - InlineStyle -> RenderStyled. Matches the foregoing pattern. --- .../example/rendering/align_items.dart | 8 ++--- .../flutter/example/rendering/baseline.dart | 10 +++--- .../example/rendering/interactive_flex.dart | 4 +-- .../example/rendering/justify_content.dart | 2 +- .../example/rendering/render_paragraph.dart | 4 +-- .../flutter/example/rendering/touch_demo.dart | 2 +- packages/flutter/lib/rendering/paragraph.dart | 31 +++++++++---------- packages/flutter/lib/widgets/basic.dart | 24 +++++++------- 8 files changed, 42 insertions(+), 43 deletions(-) diff --git a/packages/flutter/example/rendering/align_items.dart b/packages/flutter/example/rendering/align_items.dart index 58d000468d0..c8f6b24d042 100644 --- a/packages/flutter/example/rendering/align_items.dart +++ b/packages/flutter/example/rendering/align_items.dart @@ -18,25 +18,25 @@ void main() { for(FlexAlignItems alignItems in FlexAlignItems.values) { TextStyle style = const TextStyle(color: const Color(0xFF000000)); - RenderParagraph paragraph = new RenderParagraph(new InlineStyle(style, [new InlineText("${alignItems}")])); + RenderParagraph paragraph = new RenderParagraph(new RenderStyled(style, [new RenderText("${alignItems}")])); table.add(new RenderPadding(child: paragraph, padding: new EdgeDims.only(top: 20.0))); var row = new RenderFlex(alignItems: alignItems, textBaseline: TextBaseline.alphabetic); style = new TextStyle(fontSize: 15.0, color: const Color(0xFF000000)); row.add(new RenderDecoratedBox( decoration: new BoxDecoration(backgroundColor: const Color(0x7FFFCCCC)), - child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo')])) + child: new RenderParagraph(new RenderStyled(style, [new RenderText('foo foo foo')])) )); style = new TextStyle(fontSize: 10.0, color: const Color(0xFF000000)); row.add(new RenderDecoratedBox( decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCFFCC)), - child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo')])) + child: new RenderParagraph(new RenderStyled(style, [new RenderText('foo foo foo')])) )); var subrow = new RenderFlex(alignItems: alignItems, textBaseline: TextBaseline.alphabetic); style = new TextStyle(fontSize: 25.0, color: const Color(0xFF000000)); subrow.add(new RenderDecoratedBox( decoration: new BoxDecoration(backgroundColor: const Color(0x7FCCCCFF)), - child: new RenderParagraph(new InlineStyle(style, [new InlineText('foo foo foo foo')])) + child: new RenderParagraph(new RenderStyled(style, [new RenderText('foo foo foo foo')])) )); subrow.add(new RenderSolidColorBox(const Color(0x7FCCFFFF), desiredSize: new Size(30.0, 40.0))); row.add(subrow); diff --git a/packages/flutter/example/rendering/baseline.dart b/packages/flutter/example/rendering/baseline.dart index daf1e83defe..527d9cd8b54 100644 --- a/packages/flutter/example/rendering/baseline.dart +++ b/packages/flutter/example/rendering/baseline.dart @@ -13,19 +13,19 @@ import 'package:sky/rendering/sky_binding.dart'; RenderBox getBox(double lh) { RenderParagraph paragraph = new RenderParagraph( - new InlineStyle( + new RenderStyled( new TextStyle( color: const Color(0xFF0000A0) ), [ - new InlineText('test'), - new InlineStyle( + new RenderText('test'), + new RenderStyled( new TextStyle( fontFamily: 'serif', fontSize: 50.0, height: lh ), - [new InlineText('مرحبا Hello')] + [new RenderText('مرحبا Hello')] ) ] ) @@ -57,7 +57,7 @@ RenderBox getBox(double lh) { paint.color = const Color(0xFFFF9000); paint.setStyle(sky.PaintingStyle.stroke); paint.strokeWidth = 3.0; - canvas.drawPath(path, paint); + canvas.drawPath(path, paint); } ) ) diff --git a/packages/flutter/example/rendering/interactive_flex.dart b/packages/flutter/example/rendering/interactive_flex.dart index 940aacfc7fc..2f1b97cef94 100644 --- a/packages/flutter/example/rendering/interactive_flex.dart +++ b/packages/flutter/example/rendering/interactive_flex.dart @@ -84,9 +84,9 @@ porchetta bacon kevin meatball meatloaf pig beef ribs chicken. Brisket ribeye andouille leberkas capicola meatloaf. Chicken pig ball tip pork picanha bresaola alcatra. Pork pork belly alcatra, flank chuck drumstick biltong doner jowl. Pancetta meatball tongue tenderloin rump tail jowl boudin."""; - var text = new InlineStyle( + var text = new RenderStyled( new TextStyle(color: const Color(0xFF009900)), - [new InlineText(meatyString)]); + [new RenderText(meatyString)]); padding = new RenderPadding( padding: const EdgeDims.all(10.0), child: new RenderParagraph(text)); diff --git a/packages/flutter/example/rendering/justify_content.dart b/packages/flutter/example/rendering/justify_content.dart index 2f069684068..13e01d0cdae 100644 --- a/packages/flutter/example/rendering/justify_content.dart +++ b/packages/flutter/example/rendering/justify_content.dart @@ -21,7 +21,7 @@ void main() { var table = new RenderFlex(direction: FlexDirection.vertical); void addRow(FlexJustifyContent justify) { - RenderParagraph paragraph = new RenderParagraph(new InlineStyle(style, [new InlineText("${justify}")])); + RenderParagraph paragraph = new RenderParagraph(new RenderStyled(style, [new RenderText("${justify}")])); table.add(new RenderPadding(child: paragraph, padding: new EdgeDims.only(top: 20.0))); var row = new RenderFlex(direction: FlexDirection.horizontal); row.add(new RenderSolidColorBox(const Color(0xFFFFCCCC), desiredSize: new Size(80.0, 60.0))); diff --git a/packages/flutter/example/rendering/render_paragraph.dart b/packages/flutter/example/rendering/render_paragraph.dart index 4d96887504c..3227e90d82e 100644 --- a/packages/flutter/example/rendering/render_paragraph.dart +++ b/packages/flutter/example/rendering/render_paragraph.dart @@ -32,9 +32,9 @@ andouille leberkas capicola meatloaf. Chicken pig ball tip pork picanha bresaola alcatra. Pork pork belly alcatra, flank chuck drumstick biltong doner jowl. Pancetta meatball tongue tenderloin rump tail jowl boudin."""; - var text = new InlineStyle( + var text = new RenderStyled( new TextStyle(color: const Color(0xFF009900)), - [new InlineText(meatyString)]); + [new RenderText(meatyString)]); child = new RenderDecoratedBox( decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF)), child: new RenderParagraph(text) diff --git a/packages/flutter/example/rendering/touch_demo.dart b/packages/flutter/example/rendering/touch_demo.dart index fcda20e5549..ea4d836a783 100644 --- a/packages/flutter/example/rendering/touch_demo.dart +++ b/packages/flutter/example/rendering/touch_demo.dart @@ -80,7 +80,7 @@ class RenderTouchDemo extends RenderBox { } void main() { - var paragraph = new RenderParagraph(new InlineText("Touch me!")); + var paragraph = new RenderParagraph(new RenderText("Touch me!")); var stack = new RenderStack(children: [ new RenderTouchDemo(), paragraph, diff --git a/packages/flutter/lib/rendering/paragraph.dart b/packages/flutter/lib/rendering/paragraph.dart index aa41b67fd14..5f86be6f879 100644 --- a/packages/flutter/lib/rendering/paragraph.dart +++ b/packages/flutter/lib/rendering/paragraph.dart @@ -8,7 +8,7 @@ import 'package:sky/painting/text_style.dart'; import 'package:sky/rendering/box.dart'; import 'package:sky/rendering/object.dart'; -abstract class InlineBase { +abstract class RenderInline { sky.Node _toDOM(sky.Document owner); String toString([String prefix = '']); @@ -16,8 +16,8 @@ abstract class InlineBase { } } -class InlineText extends InlineBase { - InlineText(this.text) { +class RenderText extends RenderInline { + RenderText(this.text) { assert(text != null); } @@ -27,25 +27,25 @@ class InlineText extends InlineBase { return owner.createText(text); } - bool operator ==(other) => other is InlineText && text == other.text; + bool operator ==(other) => other is RenderText && text == other.text; int get hashCode => text.hashCode; String toString([String prefix = '']) => '${prefix}InlineText: "${text}"'; } -class InlineStyle extends InlineBase { - InlineStyle(this.style, this.children) { +class RenderStyled extends RenderInline { + RenderStyled(this.style, this.children) { assert(style != null); assert(children != null); } final TextStyle style; - final List children; + final List children; sky.Node _toDOM(sky.Document owner) { sky.Element parent = owner.createElement('t'); style.applyToCSSStyle(parent.style); - for (InlineBase child in children) { + for (RenderInline child in children) { parent.appendChild(child._toDOM(owner)); } return parent; @@ -58,7 +58,7 @@ class InlineStyle extends InlineBase { bool operator ==(other) { if (identical(this, other)) return true; - if (other is! InlineStyle + if (other is! RenderStyled || style != other.style || children.length != other.children.length) return false; @@ -72,7 +72,7 @@ class InlineStyle extends InlineBase { int get hashCode { int value = 373; value = 37 * value + style.hashCode; - for (InlineBase child in children) + for (RenderInline child in children) value = 37 * value + child.hashCode; return value; } @@ -82,7 +82,7 @@ class InlineStyle extends InlineBase { result.add('${prefix}InlineStyle:'); var indent = '${prefix} '; result.add('${style.toString(indent)}'); - for (InlineBase child in children) { + for (RenderInline child in children) { result.add(child.toString(indent)); } return result.join('\n'); @@ -102,9 +102,8 @@ double _applyFloatingPointHack(double layoutValue) { class RenderParagraph extends RenderBox { - RenderParagraph(InlineBase inlineValue) { + RenderParagraph(this._inline) { _layoutRoot.rootElement = _document.createElement('p'); - inline = inlineValue; } final sky.Document _document = new sky.Document(); @@ -112,9 +111,9 @@ class RenderParagraph extends RenderBox { BoxConstraints _constraintsForCurrentLayout; // when null, we don't have a current layout - InlineBase _inline; - InlineBase get inline => _inline; - void set inline (InlineBase value) { + RenderInline _inline; + RenderInline get inline => _inline; + void set inline (RenderInline value) { if (_inline == value) return; _inline = value; diff --git a/packages/flutter/lib/widgets/basic.dart b/packages/flutter/lib/widgets/basic.dart index d97dca31171..d87e2140f4d 100644 --- a/packages/flutter/lib/widgets/basic.dart +++ b/packages/flutter/lib/widgets/basic.dart @@ -445,17 +445,17 @@ class Flexible extends ParentDataNode { : super(child, new FlexBoxParentData()..flex = flex, key: key); } -class Inline extends LeafRenderObjectWrapper { - Inline({ Key key, this.text }) : super(key: key); +class Paragraph extends LeafRenderObjectWrapper { + Paragraph({ Key key, this.inline }) : super(key: key); - final InlineBase text; + final RenderInline inline; - RenderParagraph createNode() => new RenderParagraph(text); + RenderParagraph createNode() => new RenderParagraph(inline); RenderParagraph get root => super.root; void syncRenderObject(Widget old) { super.syncRenderObject(old); - root.inline = text; + root.inline = inline; } } @@ -467,16 +467,16 @@ class StyledText extends Component { final dynamic elements; - InlineBase _toInline(dynamic element) { + RenderInline _toInline(dynamic element) { if (element is String) - return new InlineText(element); + return new RenderText(element); if (element is Iterable && element.first is TextStyle) - return new InlineStyle(element.first, element.skip(1).map(_toInline).toList()); + return new RenderStyled(element.first, element.skip(1).map(_toInline).toList()); throw new ArgumentError("invalid elements"); } Widget build() { - return new Inline(text: _toInline(elements)); + return new Paragraph(inline: _toInline(elements)); } } @@ -487,7 +487,7 @@ class Text extends Component { final TextStyle style; Widget build() { - InlineBase text = new InlineText(data); + RenderInline inline = new RenderText(data); TextStyle defaultStyle = DefaultTextStyle.of(this); TextStyle combinedStyle; if (defaultStyle != null) { @@ -499,8 +499,8 @@ class Text extends Component { combinedStyle = style; } if (combinedStyle != null) - text = new InlineStyle(combinedStyle, [text]); - return new Inline(text: text); + inline = new RenderStyled(combinedStyle, [inline]); + return new Paragraph(inline: inline); } } From 2f3af49b0aa11dd472def29db84a4942e313883b Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 6 Aug 2015 22:42:13 -0700 Subject: [PATCH 2/2] Improve error messages for StyledText Now they give you a hint as to what went wrong. Also, in checked mode, they now throw during construction instead of during building. Fixes #418 --- packages/flutter/lib/widgets/basic.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/widgets/basic.dart b/packages/flutter/lib/widgets/basic.dart index d87e2140f4d..8153a760b34 100644 --- a/packages/flutter/lib/widgets/basic.dart +++ b/packages/flutter/lib/widgets/basic.dart @@ -463,16 +463,22 @@ class StyledText extends Component { // elements ::= "string" | [ *] // Where "string" is text to display and text-style is an instance of // TextStyle. The text-style applies to all of the elements that follow. - StyledText({ this.elements, Key key }) : super(key: key); + StyledText({ this.elements, Key key }) : super(key: key) { + assert(_toInline(elements) != null); + } final dynamic elements; RenderInline _toInline(dynamic element) { if (element is String) return new RenderText(element); - if (element is Iterable && element.first is TextStyle) - return new RenderStyled(element.first, element.skip(1).map(_toInline).toList()); - throw new ArgumentError("invalid elements"); + if (element is Iterable) { + dynamic first = element.first; + if (first is! TextStyle) + throw new ArgumentError("First element of Iterable is a ${first.runtimeType} not a TextStyle"); + return new RenderStyled(first, element.skip(1).map(_toInline).toList()); + } + throw new ArgumentError("Element is ${element.runtimeType} not a String or an Iterable"); } Widget build() {