Skip to content

Update default selectionHeightStyle and selectionWidthStyle for EditableText #167762

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

Merged
merged 30 commits into from
Jun 16, 2025

Conversation

Renzo-Olivares
Copy link
Contributor

@Renzo-Olivares Renzo-Olivares commented Apr 24, 2025

This change updates the default selectionHeightStyle and selectionWidthStyle for EditableText to more closely match the behavior on native platforms.

/// The default value for [selectionHeightStyle].
///
/// On web platforms, this defaults to [ui.BoxHeightStyle.max].
///
/// On native platforms, this defaults to [ui.BoxHeightStyle.includeLineSpacingMiddle] for all
/// platforms.
static ui.BoxHeightStyle get defaultSelectionHeightStyle {
  if (kIsWeb) {
    return ui.BoxHeightStyle.max;
  }
  switch (defaultTargetPlatform) {
    case TargetPlatform.android:
    case TargetPlatform.fuchsia:
    case TargetPlatform.iOS:
    case TargetPlatform.linux:
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
      return ui.BoxHeightStyle.includeLineSpacingMiddle;
  }
}

/// The default value for [selectionWidthStyle].
///
/// On web platforms, this defaults to [ui.BoxWidthStyle.max] for Apple platforms and
/// [ui.BoxWidthStyle.tight] for all others.
///
/// On non-web platforms, this defaults to [ui.BoxWidthStyle.max] for all
/// platforms.
static ui.BoxWidthStyle get defaultSelectionWidthStyle {
  if (kIsWeb) {
    if (defaultTargetPlatform == TargetPlatform.iOS || WebBrowserDetection.browserIsSafari) {
      return ui.BoxWidthStyle.max;
    }
    return ui.BoxWidthStyle.tight;
  }
  switch (defaultTargetPlatform) {
    case TargetPlatform.android:
    case TargetPlatform.fuchsia:
    case TargetPlatform.iOS:
    case TargetPlatform.linux:
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
      return ui.BoxWidthStyle.max;
  }
}

Native Selection Style Behavior

Android Native Behavior

Component Type UI Framework / Component Image
Static Text Android TextView native_android_static_textview
Static Text Jetpack Compose Text native_android_static_compose
Static Text Chrome (Web) "p element" android_chrome_web
Text Field Android EditText native_android_textfield_edittext
Text Field Jetpack Compose TextField native_android_textfield_compose

iOS Native Behavior

Component Type UI Framework / Component Image
Text Field Native SwiftUI TextEditor native_ios_texteditor
Text Field Native SwiftUI TextField native_ios_textfield
Static Text Safari (Web) "p element" mobile_safari_web

macOS Native Behavior

Component Type UI Framework / Component Image
Static Text Native SwiftUI Text native_macos_static_text
Static Text Chrome (Web) "p element" chrome_web
Static Text Firefox (Web) "p element" firefox_web
Static Text Safari (Web) "p element" safari_web
Text Field Native SwiftUI TextEditor native_macos_texteditor
Text Field Native SwiftUI TextField native_macos_textfield

Flutter Selection Style Behavior

Flutter Before Behavior

This was the behavior on all platforms before this change.

Component Type Behavior Image
Text Field Flutter TextField flutter_before_text_field
Static Text Flutter SelectableText flutter_before_selectabletext

Flutter Chrome After Behavior

This is the behavior on all chrome web after this change.

Component Type Behavior Image
Static Text Flutter SelectableText in Chrome flutter_after_chrome_selectabletext
Text Field Flutter TextField in Chrome flutter_after_chrome_textfield

Flutter Safari After Behavior

This is the behavior on Safari after this change. (all iOS browsers, and Safari on macOS).

Component Type Behavior Image
Text Field Flutter TextField in Safari flutter_after_safari_textfield
Static Text Flutter SelectableText in Safari flutter_after_safari_selectabletext

Flutter Native After Behavior

This is the behavior on native platforms after this change.

Component Type Behavior Image
Static Text Flutter SelectableText on Native platforms flutter_after_native_selectabletext
Text Field Flutter TextField on Native platforms flutter_after_native_textfield

Fixes #68675

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].
  • 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 a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. f: cupertino flutter/packages/flutter/cupertino repository labels Apr 24, 2025
@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch 2 times, most recently from 673b3cc to 2d72f21 Compare April 25, 2025 18:27
@flutter-dashboard
Copy link

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 #167762 at sha 0437620

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Apr 25, 2025
@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch from 0b3f5ca to b0a86f0 Compare April 29, 2025 17:23
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha b0a86f0

@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch 3 times, most recently from f1379ae to 21b08e8 Compare May 1, 2025 18:29
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha 21b08e8

@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch from 21b08e8 to 7fd1f42 Compare May 2, 2025 20:00
@@ -283,8 +283,8 @@ class CupertinoTextField extends StatefulWidget {
this.cursorRadius = const Radius.circular(2.0),
this.cursorOpacityAnimates = true,
this.cursorColor,
this.selectionHeightStyle = ui.BoxHeightStyle.tight,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see this a lot in the framework where higher level widgets set their default to the same as their basic counterpart, i.e. TextField sets the same defaults as EditableText. Is this a pattern we want to be strict about? Can we just keep the defaults in EditableText and have TextField fall back on that. This PR does this for selectionHeightStyle and selectionWidthStyle, but i'm curious what others think.

Copy link
Contributor

Choose a reason for hiding this comment

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

This has come up a few times and I have softly decided to support setting the default everywhere. I just sent you some notes I wrote about it.

Copy link
Member

@loic-sharma loic-sharma May 15, 2025

Choose a reason for hiding this comment

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

Hm it seems a drawback of omitting the default value here is that the property becomes nullable? It seems like a small usability win to have a non-nullable property if we set the default everywhere.

Also, not setting the default property also makes this particular change a breaking change. I'd lean towards avoid a breaking change if possible.

Copy link
Member

Choose a reason for hiding this comment

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

Ah but since EditableText.defaultSelectionHeightStyle and EditableText.defaultSelectionWidthStyle are non-const, we can't actually use them as default values in a const constructor? If that's the case, I'm OK with changing these to be null by default.

Copy link
Contributor Author

@Renzo-Olivares Renzo-Olivares May 20, 2025

Choose a reason for hiding this comment

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

Yeah it won't be possible to set the defaults because the selectionStyle getters are non-const. Even in the non-const constructors if the parameter is a named one its default value has to be const. Is this considered a breaking change because TextField().selectionHeightStyle/selectionWidthStyle now return null?

Copy link
Member

Choose a reason for hiding this comment

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

Is this considered a breaking change because TextField().selectionHeightStyle/selectionWidthStyle now return null?

Strictly speaking, yes.

One way to avoid this breaking change would be by making TextField().selectionHeightStyle/selectionWidthStyle getters that return EditableText.defaultSelectionWidthStyle/defaultSelectionWidthStyle instead of null. That feels a bit weird though, I suspect the breaking change is the better solution long-term.

@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha 7fd1f42

@@ -324,8 +324,8 @@ class RenderEditable extends RenderBox
bool paintCursorAboveText = false,
Offset cursorOffset = Offset.zero,
double devicePixelRatio = 1.0,
ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.tight,
ui.BoxWidthStyle selectionWidthStyle = ui.BoxWidthStyle.tight,
ui.BoxHeightStyle selectionHeightStyle = ui.BoxHeightStyle.max,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I set this to a reasonable default that wouldn't cause a line with runs of different heights to create "ledges".

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm on board with that. I've seen the "ledges" problem come up before and wondered why we didn't do this in the first place. We can always undo it if it breaks something we're not thinking of.

@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch 2 times, most recently from 2e52273 to 9a92460 Compare May 8, 2025 04:36
@@ -1659,9 +1659,9 @@ void main() {
composingRect = editable.getRectForComposingRange(const TextRange(start: 6, end: 7))!;
expect(composingRect, const Rect.fromLTRB(0.0, 14.0, 14.0, 28.0));
composingRect = editable.getRectForComposingRange(const TextRange(start: 7, end: 8))!; // H
expect(composingRect, const Rect.fromLTRB(14.0, 18.0, 24.0, 28.0));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test change makes sense since now that the default is BoxHeightStyle.max, these rects have a height of the largest span on the line which are the WidgetSpans in this test with a height of 14.0.

@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha 9a92460

@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch from 166f040 to 4ce5255 Compare May 8, 2025 18:36
@Renzo-Olivares Renzo-Olivares changed the title Test changing default selectionHeightStyle Update default selectionHeightStyle for EditableText May 8, 2025
@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch from a4c9c39 to 54c1043 Compare May 14, 2025 20:30
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha 54c1043

@Renzo-Olivares
Copy link
Contributor Author

One style that doesn't seem possible using the currently available BoxHeightStyle is the firefox default. I was able to modify SkParagraph to achieve the behavior Renzo-Olivares/skia@375a285 , but it still needs some work. I'll try to tackle that change at a later time.

selection_height_style-max_expected

@Renzo-Olivares Renzo-Olivares changed the title Update default selectionHeightStyle for EditableText Update default selectionHeightStyle and selectionWidthStyle for EditableText May 15, 2025
@Renzo-Olivares Renzo-Olivares force-pushed the default-text-height-style branch from f2787cb to ccfd64a Compare June 16, 2025 17:45
@flutter-dashboard
Copy link

Golden file changes are available for triage from new commit, Click here to view.

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 #167762 at sha ccfd64a

@Renzo-Olivares Renzo-Olivares added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 16, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Jun 16, 2025
Merged via the queue into flutter:master with commit fe6c969 Jun 16, 2025
70 of 71 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 17, 2025
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 f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. will affect goldens Changes to golden files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SelectableText] inconsistent selection background color between different character encoding
4 participants