Skip to content

Fix line breaks being lost when copying after selection gesture in SelectableRegion#184421

Merged
auto-submit[bot] merged 7 commits into
flutter:masterfrom
Renzo-Olivares:selectableregion-newlines-copy-fix
Apr 2, 2026
Merged

Fix line breaks being lost when copying after selection gesture in SelectableRegion#184421
auto-submit[bot] merged 7 commits into
flutter:masterfrom
Renzo-Olivares:selectableregion-newlines-copy-fix

Conversation

@Renzo-Olivares

Copy link
Copy Markdown
Contributor

Original discussion: #184043

Fixes #154253

Before this change the _rect used by _SelectableFragment was calculated using getBoxesForSelection which defaults to a BoxHeightStyle of BoxHeightStyle.tight. This style ensures the boxes returned from getBoxesForSelection tightly wrap each character. This was causing an issue when text ended or was split on new lines because the SelectableFragment._rect did not include the region that actually had the new lines. So when _SelectableFragment internally used SelectionUtils.adjustDragOffset to clamp the position to the selectable rect, the selectable rect is inaccurate and always results in a position that is above the new line and not on it.

After this change the _rect used by _SelectableFragment is calculated using getBoxesForSelection with a BoxHeightStyle of BoxHeightStyle.max. This style ensures the boxes returned from getBoxesForSelection cover the entire height of the line, and as a result including the new line character region.

Other notes:

TextPainter.getBoxesForSelection explicitly notes that leading or trailing new line characters will be represented by zero-width boxes.

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • 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].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos labels Mar 31, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request modifies RenderParagraph to use ui.BoxHeightStyle.max when retrieving selection boxes, ensuring trailing newlines are correctly captured during selection. It also adds a regression test and updates existing tests with a coordinate calculation helper. Reviewer feedback suggests renaming the helper function for clarity and using dynamic line height for the caret rectangle to improve test robustness.

@Renzo-Olivares

Copy link
Copy Markdown
Contributor Author

I created another PR because I somehow managed to break my old one (and a few others). Apologies for any confusion.

@loic-sharma loic-sharma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@chunhtai chunhtai 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

@Renzo-Olivares Renzo-Olivares added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Apr 2, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 2, 2026
@auto-submit

auto-submit Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/184421, because - The status or check suite ci.yaml validation has failed. Please fix the issues identified (or deflake) before re-applying this label.

@Renzo-Olivares Renzo-Olivares force-pushed the selectableregion-newlines-copy-fix branch from 69142bf to f877722 Compare April 2, 2026 18:40
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 2, 2026
@Renzo-Olivares Renzo-Olivares added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Apr 2, 2026
@auto-submit auto-submit Bot added this pull request to the merge queue Apr 2, 2026
Merged via the queue into flutter:master with commit 5fda718 Apr 2, 2026
80 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 2, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 3, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Apr 3, 2026
flutter/flutter@0f401ee...7245c3f

2026-04-03 [email protected] Roll Skia from c07c67045b6d to 5d847ba5c4aa (1 revision) (flutter/flutter#184570)
2026-04-03 [email protected] Roll Dart SDK from 3c7a79045b8b to 46f49142acd9 (1 revision) (flutter/flutter#184567)
2026-04-03 [email protected] Roll ICU from ee5f27adc28b to ff7995a708a1 (5 revisions) (flutter/flutter#184566)
2026-04-03 [email protected] Roll Skia from 9ae8231be181 to c07c67045b6d (4 revisions) (flutter/flutter#184562)
2026-04-03 [email protected] Roll Fuchsia Linux SDK from BFLjk6Uwd0gs_Hkdk... to PpL3Bn2YMb2h9LbdK... (flutter/flutter#184556)
2026-04-03 [email protected] Roll Skia from 0566b2f5f0d1 to 9ae8231be181 (1 revision) (flutter/flutter#184547)
2026-04-03 [email protected] Roll Dart SDK from 6008eaddd589 to 3c7a79045b8b (3 revisions) (flutter/flutter#184551)
2026-04-03 [email protected] Fix wide gamut macos integration test (flutter/flutter#184427)
2026-04-02 [email protected] forward an application name to DDS (flutter/flutter#184459)
2026-04-02 [email protected] Roll Skia from 973117cfa875 to 0566b2f5f0d1 (8 revisions) (flutter/flutter#184534)
2026-04-02 [email protected] Support different joins for stroked rects in uber_sdf, fix incorrect aa (flutter/flutter#184395)
2026-04-02 [email protected] [ Widget Preview ] Handle collections and records in custom preview annotations (flutter/flutter#184518)
2026-04-02 [email protected] Moves android_semantics_integration_test out of staging (flutter/flutter#184079)
2026-04-02 [email protected] Roll Packages from b3fcf14 to 66bf7ec (4 revisions) (flutter/flutter#184514)
2026-04-02 [email protected] Fix line breaks being lost when copying after selection gesture in SelectableRegion (flutter/flutter#184421)
2026-04-02 [email protected] Add plugin version to SwiftPM package symlink directory (flutter/flutter#183668)
2026-04-02 [email protected] Add our own wrapper for `CommonExtension` due to change in signature from 8.x->9.0 (flutter/flutter#184433)
2026-04-02 [email protected] [Android] Use EdgeToEdge.enable/WindowCompat for edge-to-edge mode instead of deprecated View flags (flutter/flutter#183072)
2026-04-02 [email protected] [data_assets] Cleanup tests (flutter/flutter#184209)
2026-04-02 [email protected] Enable SPM by default on Stable (flutter/flutter#184495)
2026-04-02 [email protected] Roll Dart SDK from d84bdfeb45eb to 6008eaddd589 (2 revisions) (flutter/flutter#184513)
2026-04-02 [email protected] Reland "Even more awaits" (flutter/flutter#184467)
2026-04-02 [email protected] Roll Skia from bb9fd8653739 to 973117cfa875 (2 revisions) (flutter/flutter#184498)
2026-04-02 [email protected] [ Widget Preview ] Use analysis server for widget preview detection (flutter/flutter#184473)
2026-04-02 [email protected] [web_ui] Fix avoid_type_to_string lint violation (flutter/flutter#184342)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
mbcorona pushed a commit to mbcorona/flutter that referenced this pull request Apr 15, 2026
…lectableRegion (flutter#184421)

Original discussion: flutter#184043

Fixes flutter#154253 

Before this change the `_rect` used by `_SelectableFragment` was
calculated using `getBoxesForSelection` which defaults to a
`BoxHeightStyle` of `BoxHeightStyle.tight`. This style ensures the boxes
returned from `getBoxesForSelection` tightly wrap each character. This
was causing an issue when text ended or was split on new lines because
the `SelectableFragment._rect` did not include the region that actually
had the new lines. So when `_SelectableFragment` internally used
`SelectionUtils.adjustDragOffset` to clamp the position to the
selectable rect, the selectable rect is inaccurate and always results in
a position that is above the new line and not on it.

After this change the `_rect` used by `_SelectableFragment` is
calculated using `getBoxesForSelection` with a `BoxHeightStyle` of
`BoxHeightStyle.max`. This style ensures the boxes returned from
`getBoxesForSelection` cover the entire height of the line, and as a
result including the new line character region.

Other notes:

`TextPainter.getBoxesForSelection` explicitly notes that leading or
trailing new line characters will be represented by zero-width boxes.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

---------

Co-authored-by: Renzo Olivares <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line breaks are lost when selecting text in SelectionArea/SelectableRegion

3 participants