Skip to content
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

Disable arrow key focus navigation for text fields #42533

Merged
merged 3 commits into from Oct 15, 2019

Conversation

gspencergoog
Copy link
Contributor

Description

This disables the arrow key focus navigation for text fields. This was non-standard behavior for text fields, although it remains useful for other kinds of controls.

Related Issues

Fixes #42259

Tests

  • Added tests for Material and Cupertino text fields to make sure that arrow keys don't change focus.

Breaking Change

  • No, this is not a breaking change.

@fluttergithubbot fluttergithubbot added f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Oct 11, 2019
@gspencergoog
Copy link
Contributor Author

Actually, this exposed an issue in the focus subsystem. I have a fix here: #42554. Once that lands, this PR will pass tests.

@mdebbar
Copy link
Contributor

mdebbar commented Oct 14, 2019

Thanks for fixing this. The change looks good.

My only concern with this approach is that it disables arrow-key navigation in text fields with no way to re-enable it. Ideally, arrow-key navigation in text fields would be off by default. But if a developer decides that they want this behavior in their app, they should be able to do so.

@gspencergoog
Copy link
Contributor Author

Yes, I share that concern about not being able to re-define them, but I'm not sure how to implement that, other than adding an explicit flag to the text field to not re-map them. We already have too many flags, and this one seems oddly specific.

The other way I could block it would be to bind the DirectionalFocusAction to do nothing, but then the developer wouldn't be able to bind other keys to that action (e.g. maybe they want shift-left-arrow to navigate left).

Being able to re-bind the arrow keys to something is probably not as useful as it seems, however, since the _Editable will eat the left/right arrow keys most of the time before the shortcut gets activated, and when it's a multiline field, it'll eat the up/down arrow keys too. Not that it wouldn't be useful sometimes, but only rarely.

@gspencergoog
Copy link
Contributor Author

@mdebbar, can I take that as an "LGTM", or do you want someone else to review it?

Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

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

Thanks for the detailed reply.

LGTM!

@gspencergoog
Copy link
Contributor Author

gspencergoog commented Oct 15, 2019

Another way this could be implemented is to check the current focus inside the action when the action is invoked, and have the action do nothing if it's a text field. Then if someone really wanted to remap the keys, they could just replace the default action with another one that doesn't do that check.

The down side of that approach is that it would only work for text fields that wrapped EditableText, but that's a pretty good bet, since writing your own text widget is very hard. And if you're up for doing that, then you can easily define your own action that does what you want for your text fields.

Would that be a better implementation, do you think?

@mdebbar
Copy link
Contributor

mdebbar commented Oct 15, 2019

Yeah I think I prefer that approach. It also removes the duplication of code in material and cupertino text fields.

@gspencergoog gspencergoog merged commit 6b2cc85 into flutter:master Oct 15, 2019
@gspencergoog
Copy link
Contributor Author

Oh, crap, I didn't see your comment until after I submitted. I'll correct it tomorrow.

@technolion
Copy link

I was happy to see that focus traversal was implemented by using the arrow keys and very sad that it was removed again. I am developing a desktop app for macOS and Windows.
I there more info why you consider focus traversal with arrow keys as non-standard behaviour?

@gspencergoog
Copy link
Contributor Author

gspencergoog commented Oct 15, 2019

It's actually only removed for text fields. Other controls will continue to have arrow key focus traversal. We removed it for text fields because it is non-standard for both mobile and desktop, and so was surprising to users. If you open most desktop apps, focus a text field, and try and move it to another field with the arrow keys, it moves the cursor, and not the input focus. Spreadsheets are an exception, but most other apps (web browsers, preference panels, file dialogs, etc) don't allow arrow key traversal between fields. On macOS, even having arrow key traversal on non-list/non-grid primitives is non standard, although not as surprising.

If you still want focus traversal for text fields in your app, you will be able to add it back in again once I implement the change I alluded to in #42533 (comment) (which I'm going to attempt today). Once that exists, you will be able to bind the arrow keys to a directional focus action that doesn't check for text fields, it just won't be the default.

gspencergoog added a commit to gspencergoog/flutter that referenced this pull request Oct 16, 2019
gspencergoog added a commit that referenced this pull request Oct 17, 2019
…different way. (#42790)

In #42533, I disabled the up/down arrows for focus navigation in text fields, but we thought of a better way to do it, so this is that better way.

This change reverts the other change, and instead it tests the context of the node in the action to see if it's an EditableText node. If so, then it doesn't do the navigation action.
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Nov 26, 2019
This disables the arrow key focus navigation for text fields. This was non-standard behavior for text fields, although it remains useful for other kinds of controls.

Fixes flutter#42259
Inconnu08 pushed a commit to Inconnu08/flutter that referenced this pull request Nov 26, 2019
…different way. (flutter#42790)

In flutter#42533, I disabled the up/down arrows for focus navigation in text fields, but we thought of a better way to do it, so this is that better way.

This change reverts the other change, and instead it tests the context of the node in the action to see if it's an EditableText node. If so, then it doesn't do the navigation action.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Arrow keys cause unwanted focus traversal when a text field is active
5 participants