Skip to content

Fix Caret Height On Empty Lines - #120834

Merged
LongCatIsLooong merged 5 commits into
flutter:masterfrom
LongCatIsLooong:empty-text-caret
Mar 13, 2023
Merged

LongCatIsLooong merged 5 commits into
flutter:masterfrom
LongCatIsLooong:empty-text-caret

Conversation

@LongCatIsLooong

@LongCatIsLooong LongCatIsLooong commented Feb 15, 2023 •

Copy link
Copy Markdown
Contributor

Fixes #92080 (related: #114950)

  • getLocalRectForCaret now reports the real rect that will be painted.
  • Move caret x-coordinate clamping to RenderEditable since TextPainter doesn't know about clipping.
  • Caret Caching no longer depends on "caret prototype".
  • Fix WidgetSpan.codeUnitAtVisitor implementation

Incorporated some changes from #113268 and #118128

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

…rCaret` now reports the real rect that will be painted.

move caret x-coordinate clamping to RenderEditable since TextPainter doesn't know about clipping.
@flutter-dashboard flutter-dashboard Bot added a: text input Entering text in a text field or keyboard related problems p: cupertino_ui cupertino_ui package in flutter/packages p: material_ui material_ui package in flutter/packages framework flutter/packages/flutter repository. See also f: labels. labels Feb 15, 2023
@LongCatIsLooong LongCatIsLooong mentioned this pull request Feb 15, 2023
3 tasks
@flutter-dashboard

Copy link
Copy Markdown

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 package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #120834 at sha 2f4610b

@flutter-dashboard flutter-dashboard Bot added the will affect goldens Changes to golden files label Feb 16, 2023
@@ -1166,8 +1196,8 @@ class TextPainter {
///
/// Valid only after [layout] has been called.
double? getFullHeightForCaret(TextPosition position, Rect caretPrototype) {

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

class _EmptyLineCaretMetrics implements _CaretMetrics {
const _EmptyLineCaretMetrics({ required this.lineVerticalOffset });

/// The y offset of the unoccupied line.

Copy link
Copy Markdown
Contributor

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 ///

// 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

Copy link
Copy Markdown
Contributor

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);

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

@Renzo-Olivares Renzo-Olivares left a comment

Copy link
Copy Markdown
Contributor

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.

@justinmc justinmc left a comment •

Copy link
Copy Markdown
Contributor

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) {

Copy link
Copy Markdown
Contributor

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);

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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', () {

Copy link
Copy Markdown
Contributor

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!

@LongCatIsLooong LongCatIsLooong added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 13, 2023
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 13, 2023
@auto-submit

auto-submit Bot commented Mar 13, 2023

Copy link
Copy Markdown
Contributor

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.

@LongCatIsLooong

Copy link
Copy Markdown
Contributor Author

The google testing failures are expected. Merging manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. p: cupertino_ui cupertino_ui package in flutter/packages p: material_ui material_ui package in flutter/packages will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The cursor height becomes shorter when I delete all characters from TextField

3 participants