-
Notifications
You must be signed in to change notification settings - Fork 28.7k
Fix Caret Height On Empty Lines #120834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we deprecate the parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should this also be ///
? or Should the entire comment be //
since this is a private class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should be consistent with //
vs ///
// The _CaretMetrics for carets located in a non-empty line. Carets located in a | ||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand why we are taking into account scrollableWidth
, could explain for my sanity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when there're trailing spaces the caret x offset may be greater than the width of the text painter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, the broken tests seem unrelated probably just needs a sync up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
One comment about maybe adding a deprecation.
@@ -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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
getLocalRectForCaret
now reports the real rect that will be painted.WidgetSpan.codeUnitAtVisitor
implementationIncorporated some changes from #113268 and #118128
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.