Fix Caret Height On Empty Lines#120834
Conversation
…rCaret` now reports the real rect that will be painted. move caret x-coordinate clamping to RenderEditable since TextPainter doesn't know about clipping.
ebea8cf to
2f4610b
Compare
|
Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change). If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review. For more guidance, visit Writing a golden file test for Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
| @@ -1166,8 +1196,8 @@ class TextPainter { | |||
| /// | |||
| /// Valid only after [layout] has been called. | |||
| double? getFullHeightForCaret(TextPosition position, Rect caretPrototype) { | |||
There was a problem hiding this comment.
Do we still need caretPrototype here? Though removing it would probably be a breaking change since this method is public.
There was a problem hiding this comment.
It's no longer used. But as you pointed out it's a public API so I didn't change the signature.
There was a problem hiding this comment.
Should we deprecate the parameter?
There was a problem hiding this comment.
Yeah FullHeight and Offset are pretty limited. Maybe we'll have different caret painter classes and let them report the bounding box.
| class _CaretMetrics { | ||
| const _CaretMetrics({required this.offset, this.fullHeight}); | ||
| /// | ||
| // This should be a sealed class: A _CaretMetrics is either a _LineCaretMetrics |
There was a problem hiding this comment.
nit: should this also be ///? or Should the entire comment be // since this is a private class.
There was a problem hiding this comment.
The // part is not meant for API users just a maintenance note.
| class _EmptyLineCaretMetrics implements _CaretMetrics { | ||
| const _EmptyLineCaretMetrics({ required this.lineVerticalOffset }); | ||
|
|
||
| /// The y offset of the unoccupied line. |
There was a problem hiding this comment.
nit: Should be consistent with // vs ///
| // non-empty line are associated with a glyph within the same line. | ||
| class _LineCaretMetrics implements _CaretMetrics { | ||
| const _LineCaretMetrics({required this.offset, required this.writingDirection, required this.fullHeight}); | ||
| /// The offset of the top left corner of the caret from the top left |
There was a problem hiding this comment.
nit: class should be consistent with // vs /// for documentation.
| final Rect caretPrototype = _caretPrototype; | ||
| final Offset caretOffset = _textPainter.getOffsetForCaret(caretPosition, caretPrototype); | ||
| Rect caretRect = caretPrototype.shift(caretOffset + cursorOffset); | ||
| final double scrollableWidth = math.max(_textPainter.width + _caretMargin, size.width); |
There was a problem hiding this comment.
Not sure I understand why we are taking into account scrollableWidth, could explain for my sanity?
There was a problem hiding this comment.
when there're trailing spaces the caret x offset may be greater than the width of the text painter.
Renzo-Olivares
left a comment
There was a problem hiding this comment.
LGTM, the broken tests seem unrelated probably just needs a sync up.
| @@ -1166,8 +1196,8 @@ class TextPainter { | |||
| /// | |||
| /// Valid only after [layout] has been called. | |||
| double? getFullHeightForCaret(TextPosition position, Rect caretPrototype) { | |||
There was a problem hiding this comment.
Should we deprecate the parameter?
| // Cache the input parameters to prevent repeat work later. | ||
| _previousCaretPosition = position; | ||
| _previousCaretPrototype = caretPrototype; | ||
| return _caretMetrics = metrics ?? const _EmptyLineCaretMetrics(lineVerticalOffset: 0); |
There was a problem hiding this comment.
I'm a fan of returning this change to return the value instead of setting it to an instance variable 👍
| // superior (subjectively and in terms of fidelity) in _paintCaret. We | ||
| // should rework this properly to once again match the platform. The constant | ||
| // _kCaretHeightOffset scales poorly for small font sizes. | ||
| // TODO(LongCatIsLooong): https://github.com/flutter/flutter/issues/120836 |
There was a problem hiding this comment.
Thanks for creating an issue for this!
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| void main() { | ||
| test('WidgetSpan codeUnitAt', () { |
There was a problem hiding this comment.
Thanks for adding a test for this method!
|
auto label is removed for flutter/flutter, pr: 120834, due to - The status or check suite Google testing has failed. Please fix the issues identified (or deflake) before re-applying this label. |
|
The google testing failures are expected. Merging manually. |
Fixes #92080 (related: #114950)
getLocalRectForCaretnow reports the real rect that will be painted.WidgetSpan.codeUnitAtVisitorimplementationIncorporated some changes from #113268 and #118128
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.