Commit 64a64535 authored by Adam Barth's avatar Adam Barth

Give loose contraints to text inside input widget

The input widget scrolls, so it should give its text loose constraints.  That
way the text ends up being its intrinsic size even if put in a context with
tight constraints.

Fixes #298
parent 97b1d21b
...@@ -167,7 +167,7 @@ class InputState extends ScrollableState<Input> { ...@@ -167,7 +167,7 @@ class InputState extends ScrollableState<Input> {
scrollTo(scrollBehavior.updateExtents( scrollTo(scrollBehavior.updateExtents(
contentExtent: _contentWidth, contentExtent: _contentWidth,
containerExtent: _containerWidth, containerExtent: _containerWidth,
scrollOffset: _contentWidth) scrollOffset: _contentWidth
); ));
} }
} }
...@@ -177,10 +177,18 @@ class TextPainter { ...@@ -177,10 +177,18 @@ class TextPainter {
return _applyFloatingPointHack(_paragraph.maxIntrinsicWidth); return _applyFloatingPointHack(_paragraph.maxIntrinsicWidth);
} }
double get width {
assert(!_needsLayout);
return _applyFloatingPointHack(_paragraph.width);
}
double get height {
assert(!_needsLayout);
return _applyFloatingPointHack(_paragraph.height);
}
Size get size { Size get size {
assert(!_needsLayout); assert(!_needsLayout);
double height = _applyFloatingPointHack(_paragraph.height);
double width = _applyFloatingPointHack(_paragraph.width);
return new Size(width, height); return new Size(width, height);
} }
......
...@@ -60,15 +60,21 @@ class RenderEditableParagraph extends RenderParagraph { ...@@ -60,15 +60,21 @@ class RenderEditableParagraph extends RenderParagraph {
markNeedsPaint(); markNeedsPaint();
} }
// Editable text does not support line wrap. BoxConstraints _getTextContraints(BoxConstraints constraints) {
bool get allowLineWrap => false; return new BoxConstraints(
minWidth: 0.0,
maxWidth: double.INFINITY,
minHeight: constraints.minHeight,
maxHeight: constraints.maxHeight
);
}
double _getIntrinsicWidth(BoxConstraints constraints) { double _getIntrinsicWidth(BoxConstraints constraints) {
// There should be no difference between the minimum and maximum width // There should be no difference between the minimum and maximum width
// because we only support single-line text. // because we only support single-line text.
layoutText(constraints); layoutText(_getTextContraints(constraints));
return constraints.constrainWidth( return constraints.constrainWidth(
textPainter.size.width + _kCursorGap + _kCursorWidth textPainter.width + _kCursorGap + _kCursorWidth
); );
} }
...@@ -81,23 +87,21 @@ class RenderEditableParagraph extends RenderParagraph { ...@@ -81,23 +87,21 @@ class RenderEditableParagraph extends RenderParagraph {
} }
void performLayout() { void performLayout() {
layoutText(constraints); layoutText(_getTextContraints(constraints));
Size contentSize = new Size(textPainter.width + _kCursorGap + _kCursorWidth, textPainter.height);
size = constraints.constrain(contentSize);
Offset cursorPadding = const Offset(_kCursorGap + _kCursorWidth, 0.0); if (_contentSize == null || _contentSize != contentSize) {
Size newContentSize = textPainter.size + cursorPadding; _contentSize = contentSize;
size = constraints.constrain(newContentSize);
if (_contentSize == null || _contentSize != newContentSize) {
_contentSize = newContentSize;
if (onContentSizeChanged != null) if (onContentSizeChanged != null)
onContentSizeChanged(newContentSize); onContentSizeChanged(_contentSize);
} }
} }
void paint(PaintingContext context, Offset offset) { void paint(PaintingContext context, Offset offset) {
layoutText(constraints); layoutText(_getTextContraints(constraints));
bool needsClipping = (_contentSize.width > size.width); final bool needsClipping = (_contentSize.width > size.width);
if (needsClipping) { if (needsClipping) {
context.canvas.save(); context.canvas.save();
context.canvas.clipRect(offset & size); context.canvas.clipRect(offset & size);
...@@ -107,15 +111,12 @@ class RenderEditableParagraph extends RenderParagraph { ...@@ -107,15 +111,12 @@ class RenderEditableParagraph extends RenderParagraph {
if (_showCursor) { if (_showCursor) {
Rect cursorRect = new Rect.fromLTWH( Rect cursorRect = new Rect.fromLTWH(
textPainter.size.width + _kCursorGap, offset.dx + _contentSize.width - _kCursorWidth - _scrollOffset.dx,
_kCursorHeightOffset, offset.dy + _kCursorHeightOffset - _scrollOffset.dy,
_kCursorWidth, _kCursorWidth,
size.height - 2.0 * _kCursorHeightOffset size.height - 2.0 * _kCursorHeightOffset
); );
context.canvas.drawRect( context.canvas.drawRect(cursorRect, new Paint()..color = _cursorColor);
cursorRect.shift(offset - _scrollOffset),
new Paint()..color = _cursorColor
);
} }
if (needsClipping) if (needsClipping)
......
...@@ -47,14 +47,11 @@ class RenderParagraph extends RenderBox { ...@@ -47,14 +47,11 @@ class RenderParagraph extends RenderBox {
markNeedsLayout(); markNeedsLayout();
} }
// Whether the text should be allowed to wrap to multiple lines.
bool get allowLineWrap => true;
void layoutText(BoxConstraints constraints) { void layoutText(BoxConstraints constraints) {
assert(constraints != null); assert(constraints != null);
if (_constraintsForCurrentLayout == constraints) if (_constraintsForCurrentLayout == constraints)
return; // already cached this layout return; // already cached this layout
textPainter.maxWidth = allowLineWrap ? constraints.maxWidth : double.INFINITY; textPainter.maxWidth = constraints.maxWidth;
textPainter.minWidth = constraints.minWidth; textPainter.minWidth = constraints.minWidth;
textPainter.minHeight = constraints.minHeight; textPainter.minHeight = constraints.minHeight;
textPainter.maxHeight = constraints.maxHeight; textPainter.maxHeight = constraints.maxHeight;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment