Skip to content

Dragging an unfocused field#93333

Closed
justinmc wants to merge 10 commits intoflutter:masterfrom
justinmc:drag-after-unfocused-field
Closed

Dragging an unfocused field#93333
justinmc wants to merge 10 commits intoflutter:masterfrom
justinmc:drag-after-unfocused-field

Conversation

@justinmc
Copy link
Contributor

@justinmc justinmc commented Nov 9, 2021

Previously, it wasn't really possible to unfocus a field during a drag gesture; the field would be immediately re-focused at the next dragupdate and the selection changes would continue. This PR stops listening to drag events when the field is not focused.

Fixes #93143

@justinmc justinmc self-assigned this Nov 9, 2021
@flutter-dashboard flutter-dashboard bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels Nov 9, 2021
@google-cla google-cla bot added the cla: yes label Nov 9, 2021
@LongCatIsLooong
Copy link
Contributor

Does this prevent edge scrolling if the text field yields focus once the touch point goes outside of its bounds?

@justinmc
Copy link
Contributor Author

I've just merged in master so we get the edge scrolling PR. The answer is yes, but I think the behavior is desired for that case, even if it is a little weird.

Without the MouseRegion that unfocuses on exit, the edge scrolling behavior is the same with/without this PR.

Here is a video of me testing edge scrolling on this PR using the the example app from the issue #93143.

Screen.Recording.2021-11-18.at.4.04.23.PM.mov

@skia-gold
Copy link

Gold has detected about 1 new digest(s) on patchset 3.
View them at https://flutter-gold.skia.org/cl/github/93333

@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 #93333 at sha aa113f9

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Nov 19, 2021
void onDragSelectionUpdate(DragStartDetails startDetails, DragUpdateDetails updateDetails) {
if (!delegate.selectionEnabled)
return;
if (!editableText.widget.focusNode.hasFocus)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer remove these lines:

// This will show the keyboard for all selection changes on the
// EditableWidget, not just changes triggered by user gestures.
requestKeyboard();

instead of explicitly preventing drag update events from getting through when the text field is unfocused. Even if the text field is unfocused you may still want edge scroll?

Unfortunately some tests that use EditableText are using tap to focus the input field and removing the said lines breaks those tests but that should be easy to fix? Not sure how many tests are doing that though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh another thing is current the text selection highlight is only visible if the text field has the focus. Maybe we should change that so when the selection is being changed the highlight is still visible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vs-krasnov Do you have any opinion on this as the original author of the issue? What if the field unfocused but the selection continued to be visible and to change with the drag?

Choose a reason for hiding this comment

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

@justinmc thanks for taking care of this issue so quickly ❤️
IMHO, once a text field becomes unfocused, we shouldn't change the state of this text field anymore based on future drag events.
I wouldn't expect the selection to continue for unfocused text fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like Chrome does continue to allow edge scrolling but doesn't show the highlight:

web

Does it work for everyone if we go with that behavior in Flutter? I've updated the PR to do that. Assuming tests go green.

HTML example code
<!doctype html>
<html class="no-js" lang="">

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta property="og:title" content="">
  <meta property="og:type" content="">
  <meta property="og:url" content="">
  <meta property="og:image" content="">

  <link rel="manifest" href="site.webmanifest">
  <link rel="apple-touch-icon" href="icon.png">
  <!-- Place favicon.ico in the root directory -->

  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/main.css">

  <meta name="theme-color" content="#fafafa">

  <style>
    .box {
      width:  100px;
      height: 100px;
      background-color: dodgerblue;
    }
  </style>
</head>

<body>

  <!-- Add your site or application content here -->
  <p>Hello world! This is HTML5 Boilerplate.</p>
  <input type="number" />
  <div class="box">Hover over this to unfocus</div>
  <script src="js/vendor/modernizr-3.11.2.min.js"></script>
  <script src="js/plugins.js"></script>
  <script src="js/main.js"></script>

  <!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
  <script>
    window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
    ga('create', 'UA-XXXXX-Y', 'auto'); ga('set', 'anonymizeIp', true); ga('set', 'transport', 'beacon'); ga('send', 'pageview')
  </script>
  <script src="https://www.google-analytics.com/analytics.js" async></script>
  <script>
    document.querySelector('.box').addEventListener('mouseenter', () => {
      document.activeElement.blur();
    });
  </script>
</body>

</html>

Choose a reason for hiding this comment

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

It makes sense to follow the existing platform's behavior 👍

@skia-gold
Copy link

Gold has detected about 7 new digest(s) on patchset 4.
View them at https://flutter-gold.skia.org/cl/github/93333

@justinmc justinmc force-pushed the drag-after-unfocused-field branch from a99a244 to f135d55 Compare November 23, 2021 23:11
@justinmc
Copy link
Contributor Author

justinmc commented Dec 7, 2021

@LongCatIsLooong Ready for re-review.

// This will show the keyboard for all selection changes on the
// EditableWidget, not just changes triggered by user gestures.
requestKeyboard();
if (!_hasFocus) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think requestFocus does nothing if the node is already the primary focus so the if check is not needed?

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 not sure I understand the change. This part seems to say that when the text selection changes the field will keep requesting focus? Then if the user keeps dragging the selection the text field will regain focus?

…esture handler instead. Maybe should have more focuses in other gestures too?
@skia-gold
Copy link

Gold has detected about 7 new digest(s) on patchset 9.
View them at https://flutter-gold.skia.org/cl/github/93333

@justinmc
Copy link
Contributor Author

Talking to @LongCatIsLooong, it seems like this PR needs a bit of refactoring to be able to land with the correct behavior. The tap gesture detector in RenderEditable prevents us from being able to set focus in the gesture detectors. We need to move that out of RenderEditable before this solution can move forward.

The goal is still the behavior agreed upon above.

I'm going to put this PR back into draft mode.

@justinmc justinmc marked this pull request as draft December 10, 2021 21:57
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

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.

@goderbauer
Copy link
Member

(triage): Are there still plans for this PR?

@justinmc
Copy link
Contributor Author

Sorry I abandoned this PR for awhile. I'll get it up to date and then either finish it or close it for now depending on how relevant it still looks.

@justinmc
Copy link
Contributor Author

I'm going to close this as I investigate refactoring TextSelectionControls as a part of my context menu work. Once that is done, I should reinvestigate if it makes this refactor easier.

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. will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextField cannot be unfocused when mouse primary button is pressed

5 participants