-
Notifications
You must be signed in to change notification settings - Fork 6k
[Linux] composing delta fixes #42648
Changes from 11 commits
cfe03b1
81e7972
a4311b6
5fe5f77
290a5c4
4f4f4ce
bed42da
2acbad0
82d49a6
dfd89c4
2bade18
bedd07c
3d2f51c
1317a3a
73fd1ed
bd1e88e
5c3f14a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,6 +263,8 @@ static void im_preedit_changed_cb(FlTextInputPlugin* self) { | |
| FlTextInputPluginPrivate* priv = static_cast<FlTextInputPluginPrivate*>( | ||
| fl_text_input_plugin_get_instance_private(self)); | ||
| std::string text_before_change = priv->text_model->GetText(); | ||
| flutter::TextRange composing_before_change = | ||
| priv->text_model->composing_range(); | ||
| g_autofree gchar* buf = nullptr; | ||
| gint cursor_offset = 0; | ||
| gtk_im_context_get_preedit_string(priv->im_context, &buf, nullptr, | ||
|
|
@@ -278,7 +280,7 @@ static void im_preedit_changed_cb(FlTextInputPlugin* self) { | |
| if (priv->enable_delta_model) { | ||
| std::string text(buf); | ||
| flutter::TextEditingDelta delta = flutter::TextEditingDelta( | ||
| text_before_change, priv->text_model->composing_range(), text); | ||
| text_before_change, composing_before_change, text); | ||
| update_editing_state_with_delta(self, &delta); | ||
| } else { | ||
| update_editing_state(self); | ||
|
|
@@ -290,17 +292,27 @@ static void im_commit_cb(FlTextInputPlugin* self, const gchar* text) { | |
| FlTextInputPluginPrivate* priv = static_cast<FlTextInputPluginPrivate*>( | ||
| fl_text_input_plugin_get_instance_private(self)); | ||
| std::string text_before_change = priv->text_model->GetText(); | ||
| 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(); | ||
|
|
||
| priv->text_model->AddText(text); | ||
| if (priv->text_model->composing()) { | ||
| priv->text_model->CommitComposing(); | ||
| } | ||
|
|
||
| if (priv->enable_delta_model) { | ||
| flutter::TextEditingDelta delta = flutter::TextEditingDelta( | ||
| text_before_change, selection_before_change, text); | ||
| update_editing_state_with_delta(self, &delta); | ||
| flutter::TextEditingDelta* delta; | ||
| if (wasComposing) { | ||
| delta = new flutter::TextEditingDelta(text_before_change, | ||
| composing_before_change, text); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid new/delete in C++ code. Use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbracken What's the reasoning behind this? (for my own education)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. |
||
| } else { | ||
| delta = new flutter::TextEditingDelta(text_before_change, | ||
| selection_before_change, text); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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);
|
||
| update_editing_state_with_delta(self, delta); | ||
| delete delta; | ||
| } else { | ||
| update_editing_state(self); | ||
| } | ||
|
|
@@ -310,10 +322,12 @@ static void im_commit_cb(FlTextInputPlugin* self, const gchar* text) { | |
| static void im_preedit_end_cb(FlTextInputPlugin* self) { | ||
| FlTextInputPluginPrivate* priv = static_cast<FlTextInputPluginPrivate*>( | ||
| fl_text_input_plugin_get_instance_private(self)); | ||
| std::string text_before_change = priv->text_model->GetText(); | ||
| priv->text_model->EndComposing(); | ||
| 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), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correction, a delta range of |
||
| priv->text_model->GetText()); | ||
| update_editing_state_with_delta(self, &delta); | ||
| } else { | ||
| update_editing_state(self); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.