Skip to content

Improved text selection #81839

@jonahwilliams

Description

@jonahwilliams

Currently the flutter text selection is fairly limited. Historically this is based on the same code that enables text interaction in text edit environments. This has lead to two peculiarities:

  • It is text field specific, instead of being global. This prevents copying of text together that happens to be grouped into different SelectableText widgets.

  • It is opt-in. This makes sense for mobile where touch gestures are heavily overloaded, but on desktop and desktop web applications drag gestures universally perform text selection. In general, requiring an opt-in is fairly user hostile.

These two problems can be addressed separately, but together would make text selection behave as a user would expect.

In order to make text selection global, we'd need to hoist the selection logic into a new widget, lets call it a SelectionArea. This could be introduced by default under the WidgetsApp. This widget would introduce the correct drag gesture detector which wrapped the entire screen. It would also provide itself as an inherited widget, allowing text and other widgets types to register their text contents and screen location with. When a drag gesture is performed, it could determine whether the drag "crossed" (from a document perspective) the text selection and highlight it using a single overlay.

Some open questions on this:

  • Can we make the screen sized drag gesture detector always lose to more specific gesture detectors? How do we avoid accidentally competing with other contents.
  • How do we handle selection through an infinite scrollable / slivers? It seems like screen coordinates would not be sufficient, we'd also need to know some relative coordinates.
  • How do we handle non-text data types? Selection could be made generic to support images or other compatible clipboard types.
  • How do users opt-out of selection?

Once the selection logic is hoisted into the new widget, the SelectableText widget can be hollowed out a bit. The registration code can be moved into the text RenderObject, with the Text/SelectableText widgets responsible for retrieving the inherited widget. A single field on Text could configure this selection behavior, with it enabled by default on desktop and web and disabled by default elsewhere. SelectableText would be updated to always set this flag to true.

design doc: http://flutter.dev/go/global-selection

sub task:

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: qualityA truly polished experiencea: text inputEntering text in a text field or keyboard related problemsframeworkflutter/packages/flutter repository. See also f: labels.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions