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

[SuperTextField] Fix new line duplication (Resolves #1468) #1469

Merged

Conversation

angelosilvestre
Copy link
Collaborator

[SuperTextField] Fix new line duplication. Resolves #1468

On macOS, pressing ENTER or SHIFT + ENTER is inserting two lines. This is caused because pressing ENTER generates both an onPerformAction call and a \n insertion.

On Windows this issue doesn't happen because we are using key event handlers to insert new lines. If we remove the key handler that inserts new lines, the issue happens on Windows as well. This issue was introduced when the selector handlers were implemented.

This PR changes _onPerformAction to avoid inserting new lines.

@@ -738,3 +739,21 @@ class FakeSuperEditorScroller implements DocumentScroller {
@override
void detach() => throw UnimplementedError();
}

/// The platform to be used when simulating a keyboard event with `sendKeyEvent`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would this platform be different than the defaultTargetPlatform? Also, where is this being used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's used in _pressEnterOnSuperTextField. I extracted this from pressEnter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you extract it instead of use it? And also I'd still like to know why it doesn't use defaultTargetPlatform. I don't remember what we did for pressEnter originally, or why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't use pressEnter because I had to check whether or not the key was handled in order to generate the input action and the insertion delta.

@@ -1288,7 +1288,13 @@ class _SuperTextFieldImeInteractorState extends State<SuperTextFieldImeInteracto
void _onPerformAction(TextInputAction action) {
switch (action) {
case TextInputAction.newline:
widget.textController.insertNewline();
// On mac desktop, we don't insert new lines via keyboard events.
Copy link
Contributor

Choose a reason for hiding this comment

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

This says "On mac desktop" but I don't see any platform inspection nearby....

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not inspecting the platform because for other desktop platforms, the new line is inserted via keyboard handlers. Because of that, the IME doesn't reports input actions nor insertion deltas.

If, at some point, we remove the ENTER keyboard handler, we would be in the same situation as on mac...

Should I just update the comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the purpose of this code comment to explain why we're not doing anything when we get a newline?

If so, does the following comment better capture the situation?

// Do nothing for IME newline actions
//
// Mac: Key presses flow, unhandled, to the OS and turn into IME selectors. We handle newlines there.
// Windows/Linux: Newlines are inserted by Super Editor key handlers, instead of IME actions, because Flutter doesn't dispatch newline IME actions for these platforms.
// Android/iOS: This text field implementation is only for desktop, mobile is handled elsewhere.

Also, can you remind me why we're using key handlers for Windows/Linux and not using the IME actions? Is that because Flutter doesn't send us IME actions for those?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previously, we used key handlers to insert new line on all desktop platforms. After the performSelection implementation, we stopped doing that on mac.

We could probably could do the same on the other desktop platforms. I tested on Windows and we also receive the input actions and insertion deltas if we don't handle the ENTER key.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Let's try removing the ENTER handler from the IME handler list for Windows and Linux.

@@ -683,6 +621,38 @@ void main() {
});
}

Future<void> _pressEnterOnSuperTextField(WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually specific to SuperTextField? Why doesn't this apply to SuperEditor, too? Also, does this belong in the testing robots package for general use?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could add this to the test robots. Should I open a PR for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah you can open a PR for that. What about the first two questions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will apply for SuperEditor also. I was looking into implementing it on testing robots pressEnter, but then we will need to add an optional imeClientGetter parameter.

To generate deltas, we need to access DeltaTextInputClient in order to obtain the current editing value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we introduce a pressEnterOnIme() to the robots package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you release a new version of flutter_test_robots? We'll need a pub version with the new methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just released v0.0.22


// The ENTER key event wasn't handled by the textfield.
// The OS generates both a "\n" insertion and a new line action.
await tester.ime.typeText('\n', getter: imeClientGetter);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be checking for an open connection instead of assuming that there is one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

@matthew-carroll
Copy link
Contributor

@angelosilvestre is this ready for review? Or still doing work?

@matthew-carroll
Copy link
Contributor

Also, looks like tests are failing, FYI.

@angelosilvestre
Copy link
Collaborator Author

@matthew-carroll This is ready for another review now.

@matthew-carroll
Copy link
Contributor

Looks like this PR has a merge conflict.

@angelosilvestre angelosilvestre force-pushed the 1468_newline_duplication branch from bc580eb to 77754b0 Compare October 16, 2023 01:01
@angelosilvestre
Copy link
Collaborator Author

@matthew-carroll Rebased.

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

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

LGTM with removal of one superfluous import

@@ -1,6 +1,7 @@
import 'dart:math';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need this import?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope. Removed.

@matthew-carroll matthew-carroll merged commit c851667 into superlistapp:main Oct 17, 2023
10 of 11 checks passed
@matthew-carroll
Copy link
Contributor

FYI @angelosilvestre CI is going to automatically create a PR when we merge, but you'll have to explicitly close and re-open the PR to get other CI checks to run.

@angelosilvestre angelosilvestre deleted the 1468_newline_duplication branch October 17, 2023 22:38
@angelosilvestre
Copy link
Collaborator Author

@matthew-carroll I didn't find any PR created for after the merge

@matthew-carroll
Copy link
Contributor

Ugh - CI is failing for some reason....complaining that GitHub bot doesn't have permission, which doesn't make sense given that this has worked previously.

https://github.com/superlistapp/super_editor/actions/runs/6552954869/job/17799413806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SuperTextField] SHIFT+ENTER adds 2 new lines
2 participants