Skip to content

Include cursor in textfield intrinsic width measurement#25055

Merged
justinmc merged 3 commits intoflutter:masterfrom
justinmc:intrinsic-width-cursor
Dec 26, 2018
Merged

Include cursor in textfield intrinsic width measurement#25055
justinmc merged 3 commits intoflutter:masterfrom
justinmc:intrinsic-width-cursor

Conversation

@justinmc
Copy link
Contributor

@justinmc justinmc commented Dec 6, 2018

Closes #24612

When using TextField with InstrinsicWidth, the width of the cursor was not included in the measurement of the width of the text content. This caused content to scroll slightly instead of triggering a bump up to the next width step size. Here the string reads "123456", but it has been scrolled so that the 1 is not visible:

This PR includes the cursor width in the calculation, so the width will increase before scrolling happens. Here I've used a large step size to illustrate the bump up in size with the same string:

@justinmc justinmc self-assigned this Dec 6, 2018
@justinmc justinmc requested a review from HansMuller December 6, 2018 22:50
@justinmc
Copy link
Contributor Author

justinmc commented Dec 6, 2018

@HansMuller What do you think of this solution, any implications that I'm not thinking of? There was only 1 test that failed when I made this change (see the 1 pixel that I changed in a test).

@zoechi zoechi added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels Dec 7, 2018
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. Just needs a little more test coverage

double computeMaxIntrinsicWidth(double height) {
_layoutText(double.infinity);
return _textPainter.maxIntrinsicWidth;
return _textPainter.maxIntrinsicWidth + cursorWidth;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably assert(cursorWidth != null && cursorWidth >= 0.0) in the RenderEditable constructor. There are many other places in the code that appear to make this assumption. Not sure if cursorWidth == 0.0 really works; if not assert that it's > 0.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like cursorWidth 0.0 works and results in not showing the cursor at all, so I'm using >= in the assertion.

expect(tapCount, 0);
});

testWidgets('Includes cursor for TextField', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also verify that if the the TextField is created within an IntrinsicWidth with no stepWidth specified, that the result is a textfield that's just wide enough to drive to the cursor to the end without scrolling.

Tests use the fixed-width Ahem font by default, so you can easily predict the textfield's exact width.

Should check this with a textfield that's textAlign: TextAlign.right and TextAlign.left

);
expect(editable.getMinIntrinsicWidth(double.infinity), 50.0);
expect(editable.getMaxIntrinsicWidth(double.infinity), 50.0);
expect(editable.getMaxIntrinsicWidth(double.infinity), 51.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add a comment here about including the the 1.0 cursorWidth

@justinmc
Copy link
Contributor Author

@HansMuller Done and ready for re-review

Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@justinmc justinmc merged commit 9161ec4 into flutter:master Dec 26, 2018
@justinmc justinmc deleted the intrinsic-width-cursor branch December 26, 2018 16:56
kangwang1988 added a commit to XianyuTech/flutter that referenced this pull request Dec 27, 2018
* flt_master:
  Actively reject UiKitView gestures. (flutter#25792)
  Roll engine e75fad7..9daf5c8 (3 commits) (flutter#25786)
  Roll engine d8c5ec0..e75fad7 (5 commits) (flutter#25757)
  Include cursor in textfield intrinsic width measurement (flutter#25055)
@Hixie
Copy link
Contributor

Hixie commented Jan 7, 2019

This had some unfortunate impact on some of the performance metrics.

@justinmc
Copy link
Contributor Author

justinmc commented Jan 9, 2019

Hmm, I'll try to dig into that.

@justinmc
Copy link
Contributor Author

justinmc commented Jan 9, 2019

For the record this code change seems to be absolved of causing performance problems now. Performance is back to normal after something was changed with the bots.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextField's IntrinsicWidth should take cursor into account

5 participants