Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

Renzo-Olivares
Copy link
Contributor

@Renzo-Olivares Renzo-Olivares commented Jun 8, 2023

Fixes a few issues with the Linux text editing delta implementation that was causing some issues when composing CJK text.

im_preedit_changed_cb dispatches a delta with the delta range set as the current composing region causing a crash when composing because the new composing region range might not yet exist in the oldText that the delta is applied to. We should instead send the composing region before the change to the text as that will be the correct range that is being modified in the oldText.

im_preedit_end_cb dispatches a delta with an empty oldText, a not empty deltaText, and a delta range of (-1,-1) causing a crash when composing ends because the delta range will never be within the range of the text. We should instead send the current text value as the oldText and not send a deltaRange or deltaText since a (-1,-1) range essentially means nothing in the text changed.

im_commit_cb does not account for the case when we were previously composing when the text was committed. This causes the dispatched delta to have a delta range that always inserts at the current the selection, when it should instead replace the previous composing range. We should account for the case when we are previously composing before committing the text, and dispatch a delta with the previous composing region.

Fixes flutter/flutter#113909

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] and the [C++, Objective-C, Java style guides].
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the [CLA].
  • All existing and new tests are passing.

Renzo Olivares added 7 commits June 7, 2023 16:49
@Renzo-Olivares Renzo-Olivares marked this pull request as ready for review June 8, 2023 02:08
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Renzo-Olivares Renzo-Olivares changed the title Linux composing delta fixes [Linux] composing delta fixes Jun 8, 2023
Comment on lines 306 to 313
flutter::TextEditingDelta* delta;
if (wasComposing) {
delta = new flutter::TextEditingDelta(text_before_change,
composing_before_change, text);
} else {
delta = new flutter::TextEditingDelta(text_before_change,
selection_before_change, text);
}
Copy link
Member

@cbracken cbracken Jun 8, 2023

Choose a reason for hiding this comment

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

Possibly more succinct:

flutter::TextRange replace_range = was_composing ? composing_before_change : selection_before_change;
std::unique_ptr<flutter::TextEditingDelta> delta =
    std::make_unique<flutter::TextEditingDelta>(text_before_change, replacement_range, text);
  

flutter::TextEditingDelta* delta;
if (wasComposing) {
delta = new flutter::TextEditingDelta(text_before_change,
composing_before_change, text);
Copy link
Member

Choose a reason for hiding this comment

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

Avoid new/delete in C++ code. Use std::unique_ptr<Foo>, which you can create with std::make_unique<Foo>(constructor, args, go, here);

Copy link
Contributor

Choose a reason for hiding this comment

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

@cbracken What's the reasoning behind this? (for my own education)

Copy link
Member

Choose a reason for hiding this comment

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

Generally always use RAII-based smart pointers (primarily unique_ptr) unless it's impossible, they're guaranteed to clean themselves up at end of scope whether it's a return or throw, and they're effectively zero overhead.

In this case there's only one way out of the scope, but there's a function call in there that could do something crazy like throw (but almost certainly doesn't 😉). The more ways out of a scope the more deletes you'll need to make sure not to forget. Smart pointers guarantee cleanup so why even risk it?

In modern c++ new/delete are generally considered bad practice unless absolutely necessary. In our codebase there are maybe two spots I've found where we couldn't use either unique_ptr or shared_ptr, and we still have a new/delete, but we should avoid adding any more.

More reading in the C++ FAQ and C++ Core Guidelines.

flutter::TextRange composing_before_change =
priv->text_model->composing_range();
flutter::TextRange selection_before_change = priv->text_model->selection();
gboolean wasComposing = priv->text_model->composing();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
gboolean wasComposing = priv->text_model->composing();
gboolean was_composing = priv->text_model->composing();

if (priv->enable_delta_model) {
flutter::TextEditingDelta delta = flutter::TextEditingDelta(
"", flutter::TextRange(-1, -1), priv->text_model->GetText());
text_before_change, flutter::TextRange(-1, -1),
Copy link
Member

@cbracken cbracken Jun 8, 2023

Choose a reason for hiding this comment

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

I'm surprised this works. The type of the base/extent positions are size_t, which is unsigned. I'm going to take a quick look at what options we have for modelling this properly.

Copy link
Member

@cbracken cbracken Jun 8, 2023

Choose a reason for hiding this comment

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

Here's an incomplete prototype of something we could use to do this safely:
https://github.com/flutter/engine/compare/main...cbracken:flutter_engine:add-invalid-range?expand=1

If this seems useful, I could commit it, but when I added TextRange originally my thought was to see how far along we could make it without modelling things using invalid positions like -1 since some platforms (macOS, iOS) don't model any positions with negative numbers, so we're going to need to do translation between models somewhere regardless and TextRange was cleaner without the concept of negative values.

If we think this is something that we absolutely need to model in the embedders, I could clean this up and update the existing TextRange-using embedders to use it.

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 think in this case it may be correct to use composing_before_change. So this may not be needed. Thanks for checking that out!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correction, a delta range of (-1,-1), is needed but we should just use TextEditingDelta(const std::u16string & text) for this case.

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM, but I'm interested if @cbracken has a better way to represent the (-1,-1) TextRange.

Thanks for fixing these, I probably introduced these bugs when I wrote this 😁 .

flutter::TextEditingDelta* delta;
if (wasComposing) {
delta = new flutter::TextEditingDelta(text_before_change,
composing_before_change, text);
Copy link
Contributor

Choose a reason for hiding this comment

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

@cbracken What's the reasoning behind this? (for my own education)

Copy link
Member

@cbracken cbracken 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 patching! Added a few comments/suggestions.

Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

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

LGTM stamp from a Japanese personal seal

@Renzo-Olivares Renzo-Olivares added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 8, 2023
@auto-submit auto-submit bot merged commit 8485c38 into flutter:main Jun 8, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 8, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 8, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 9, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 9, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 9, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 9, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jun 9, 2023
…sions) (#128554)

Manual roll Flutter Engine from a5f7d5d75ff2 to 8f9e608d39ab (31 revisions)

Manual roll requested by [email protected]

flutter/engine@a5f7d5d...8f9e608

2023-06-09 [email protected] Revert Dart to 3.1.0-180.0.dev (flutter/engine#42688)
2023-06-09 [email protected] [fuchsia] Bump the target API level to 12, and pass it to fidlc (flutter/engine#42667)
2023-06-09 [email protected] [Impeller] Add tests for foreground blends with mask blurs (flutter/engine#42687)
2023-06-09 [email protected] Document the use of realm in archives. (flutter/engine#42682)
2023-06-09 [email protected] Roll ANGLE from 76b0e7f38b44 to d8339e78db54 (1 revision) (flutter/engine#42684)
2023-06-08 [email protected] Prevent double upload of benchmarks. (flutter/engine#42683)
2023-06-08 [email protected] Roll ANGLE from 5215293366f0 to 76b0e7f38b44 (2 revisions) (flutter/engine#42680)
2023-06-08 [email protected] Remove all the uses of master branch in the .ci.yaml file. (flutter/engine#42679)
2023-06-08 [email protected] Replace benchmarks with its v2 version. (flutter/engine#42677)
2023-06-08 [email protected] [Impeller] Reorder blend filter checks to avoid unnecessary destination snapshot (flutter/engine#42678)
2023-06-08 [email protected] [Impeller] Specify blend mode on blend filter commands (flutter/engine#42676)
2023-06-08 [email protected] Roll ANGLE from c49674d1565c to 5215293366f0 (1 revision) (flutter/engine#42673)
2023-06-08 [email protected] [Linux] composing delta fixes (flutter/engine#42648)
2023-06-08 [email protected] Fix: invalid time-point comparison between each from different clock source (flutter/engine#42409)
2023-06-08 [email protected] Fix license hash (flutter/engine#42675)
2023-06-08 [email protected] Revert Dart to 3.1.0-184.0.dev (flutter/engine#42671)
2023-06-08 [email protected] Roll ANGLE from b5d261ac5c5b to c49674d1565c (2 revisions) (flutter/engine#42670)
2023-06-08 [email protected] Roll Dart SDK from b7fe6e0c274c to 3a9145a57432 (1 revision) (flutter/engine#42668)
2023-06-08 [email protected] [Impeller] sort all vertex inputs by location. (flutter/engine#42664)
2023-06-08 [email protected] Roll ANGLE from 05e087658b10 to b5d261ac5c5b (1 revision) (flutter/engine#42662)
2023-06-08 [email protected] Revert "[Impeller] Added a switch to turn on vulkan" (flutter/engine#42660)
2023-06-08 [email protected] Roll ANGLE from 15a29438b099 to 05e087658b10 (6 revisions) (flutter/engine#42659)
2023-06-08 [email protected] Roll Fuchsia Linux SDK from aMTaMP0DdKdJnxSbc... to lPCv1NshK-tvjtLgC... (flutter/engine#42658)
2023-06-08 [email protected] Roll Fuchsia Mac SDK from DL1QQ5eZRVNARqLx-... to ukXXOXtI7uRIukzF5... (flutter/engine#42655)
2023-06-08 [email protected] Roll Dart SDK from f0ae96d202ca to b7fe6e0c274c (1 revision) (flutter/engine#42657)
2023-06-08 [email protected] Roll Dart SDK from 9e633e463902 to f0ae96d202ca (1 revision) (flutter/engine#42651)
2023-06-08 [email protected] Benchmarks configurations for engine v2. (flutter/engine#42622)
2023-06-08 [email protected] Roll Skia from 1a3adf848e61 to 8fdbbca7d35d (1 revision) (flutter/engine#42645)
2023-06-08 [email protected] Roll Dart SDK from bbce07ad3944 to 9e633e463902 (3 revisions) (flutter/engine#42646)
2023-06-07 [email protected] Roll dart to 3.1.0-180.0.dev (flutter/engine#42638)
2023-06-07 [email protected] Roll Skia from 156542f8bf13 to 1a3adf848e61 (1 revision) (flutter/engine#42644)

Also rolling transitive DEPS:
  fuchsia/sdk/core/linux-amd64 from aMTaMP0DdKdJ to lPCv1NshK-tv
  fuchsia/sdk/core/mac-amd64 from DL1QQ5eZRVNA to ukXXOXtI7uRI

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App needs tests platform-linux
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeltaTextInputClient has different behavior between macOS and Linux
3 participants