Bug 1593683 - Part 1. Restore caret position after committing composition. r=geckoview-reviewers,esawin

When calling `InputConnection.finishComposingText` commits composition string,
but caret position isn't changed. But
`RemoveComposition(COMMIT_IME_COMPOSITION)` sets caret position to end of
string.

So we have to restore caret position after calling
`RemoveComposition(COMMIT_IME_COMPOSITION)` via `finishComposingText`.

Differential Revision: https://phabricator.services.mozilla.com/D76657
This commit is contained in:
Makoto Kato 2020-05-28 15:38:03 +00:00
parent d52843b372
commit 605c87a3ce

View File

@ -1138,12 +1138,56 @@ void GeckoEditableSupport::OnImeRequestCursorUpdates(int aRequestMode) {
mIMEMonitorCursor = (aRequestMode == EditableClient::START_MONITOR); mIMEMonitorCursor = (aRequestMode == EditableClient::START_MONITOR);
} }
class MOZ_STACK_CLASS AutoSelectionRestore final {
public:
explicit AutoSelectionRestore(nsIWidget* widget) : mWidget(widget) {
MOZ_ASSERT(widget);
WidgetQueryContentEvent selection(true, eQuerySelectedText, widget);
nsEventStatus status = nsEventStatus_eIgnore;
widget->DispatchEvent(&selection, status);
if (!selection.mSucceeded) {
mOffset = UINT32_MAX;
mLength = UINT32_MAX;
return;
}
mOffset = selection.mReply.mOffset;
mLength = selection.mReply.mString.Length();
}
~AutoSelectionRestore() {
if (mWidget->Destroyed() || mOffset == UINT32_MAX) {
return;
}
WidgetSelectionEvent selection(true, eSetSelection, mWidget);
selection.mOffset = mOffset;
selection.mLength = mLength;
selection.mExpandToClusterBoundary = false;
selection.mReason = nsISelectionListener::IME_REASON;
nsEventStatus status = nsEventStatus_eIgnore;
mWidget->DispatchEvent(&selection, status);
}
private:
nsCOMPtr<nsIWidget> mWidget;
uint32_t mOffset;
uint32_t mLength;
};
void GeckoEditableSupport::OnImeRequestCommit() { void GeckoEditableSupport::OnImeRequestCommit() {
if (mIMEMaskEventsCount > 0) { if (mIMEMaskEventsCount > 0) {
// Not focused. // Not focused.
return; return;
} }
nsCOMPtr<nsIWidget> widget = GetWidget();
if (NS_WARN_IF(!widget)) {
return;
}
AutoSelectionRestore restore(widget);
RemoveComposition(COMMIT_IME_COMPOSITION); RemoveComposition(COMMIT_IME_COMPOSITION);
} }