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-06-01 08:36:26 +00:00
parent ef2a3ed5b1
commit 83fbd04e63

View File

@ -1138,12 +1138,64 @@ void GeckoEditableSupport::OnImeRequestCursorUpdates(int aRequestMode) {
mIMEMonitorCursor = (aRequestMode == EditableClient::START_MONITOR);
}
class MOZ_STACK_CLASS AutoSelectionRestore final {
public:
explicit AutoSelectionRestore(nsIWidget* widget,
TextEventDispatcher* dispatcher)
: mWidget(widget), mDispatcher(dispatcher) {
MOZ_ASSERT(widget);
if (!dispatcher || !dispatcher->IsComposing()) {
mOffset = UINT32_MAX;
mLength = UINT32_MAX;
return;
}
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;
RefPtr<TextEventDispatcher> mDispatcher;
uint32_t mOffset;
uint32_t mLength;
};
void GeckoEditableSupport::OnImeRequestCommit() {
if (mIMEMaskEventsCount > 0) {
// Not focused.
return;
}
nsCOMPtr<nsIWidget> widget = GetWidget();
if (NS_WARN_IF(!widget)) {
return;
}
AutoSelectionRestore restore(widget, mDispatcher);
RemoveComposition(COMMIT_IME_COMPOSITION);
}