From 31efe25d14bdcf42ad15b64731314ffdad431714 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Fri, 12 Feb 2016 10:38:44 +0100 Subject: Fix doubled characters when using IME on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Windows, when exiting the IME by pressing a cursor key or clicking into the edit field, we do not receive an QInputMethodEvent with a commitText. Instead, we get an empty QInputMethodEvent and a key{Press|Release}Event pair with a key of zero. The committed text is in the event's text property. Do not call ImeConfirmComposition for the empty QInputMethodEvent but for the later keyReleaseEvent. Do not forward those key events. Change-Id: I844aa05493aca4c388a8b1de835baf2a819558f5 Task-number: QTBUG-50252 Reviewed-by: Michael BrĂ¼ning --- src/core/render_widget_host_view_qt.cpp | 24 ++++++++++++++++++++++-- src/core/render_widget_host_view_qt.h | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) (limited to 'src/core') diff --git a/src/core/render_widget_host_view_qt.cpp b/src/core/render_widget_host_view_qt.cpp index 4a773bc19..b15aa94ef 100644 --- a/src/core/render_widget_host_view_qt.cpp +++ b/src/core/render_widget_host_view_qt.cpp @@ -253,6 +253,7 @@ RenderWidgetHostViewQt::RenderWidgetHostViewQt(content::RenderWidgetHost* widget , m_needsDelegatedFrameAck(false) , m_didFirstVisuallyNonEmptyLayout(false) , m_adapterClient(0) + , m_imeInProgress(false) , m_anchorPositionWithinSelection(0) , m_cursorPositionWithinSelection(0) , m_initPending(false) @@ -903,6 +904,23 @@ void RenderWidgetHostViewQt::handleKeyEvent(QKeyEvent *ev) if (IsMouseLocked() && ev->key() == Qt::Key_Escape && ev->type() == QEvent::KeyRelease) UnlockMouse(); + if (m_imeInProgress) { + // IME composition was not finished with a valid commit string. + // We're getting the composition result in a key event. + if (ev->key() != 0) { + // The key event is not a result of an IME composition. Cancel IME. + m_host->ImeCancelComposition(); + m_imeInProgress = false; + } else { + if (ev->type() == QEvent::KeyRelease) { + m_host->ImeConfirmComposition(toString16(ev->text()), gfx::Range::InvalidRange(), + false); + m_imeInProgress = false; + } + return; + } + } + content::NativeWebKeyboardEvent webEvent = WebEventFactory::toWebKeyboardEvent(ev); if (webEvent.type == blink::WebInputEvent::RawKeyDown && !ev->text().isEmpty()) { // Blink won't consume the RawKeyDown, but rather the Char event in this case. @@ -959,11 +977,12 @@ void RenderWidgetHostViewQt::handleInputMethodEvent(QInputMethodEvent *ev) } } - if (preeditString.isEmpty()) { + if (!commitString.isEmpty()) { gfx::Range replacementRange = (replacementLength > 0) ? gfx::Range(replacementStart, replacementStart + replacementLength) : gfx::Range::InvalidRange(); m_host->ImeConfirmComposition(toString16(commitString), replacementRange, false); - } else { + m_imeInProgress = false; + } else if (!preeditString.isEmpty()) { if (!selectionRange.IsValid()) { // We did not receive a valid selection range, hence the range is going to mark the cursor position. int newCursorPosition = (cursorPositionInPreeditString < 0) ? preeditString.length() : cursorPositionInPreeditString; @@ -971,6 +990,7 @@ void RenderWidgetHostViewQt::handleInputMethodEvent(QInputMethodEvent *ev) selectionRange.set_end(newCursorPosition); } m_host->ImeSetComposition(toString16(preeditString), underlines, selectionRange.start(), selectionRange.end()); + m_imeInProgress = true; } } diff --git a/src/core/render_widget_host_view_qt.h b/src/core/render_widget_host_view_qt.h index 274138dcf..7c4723f0e 100644 --- a/src/core/render_widget_host_view_qt.h +++ b/src/core/render_widget_host_view_qt.h @@ -227,6 +227,7 @@ private: MultipleMouseClickHelper m_clickHelper; ui::TextInputType m_currentInputType; + bool m_imeInProgress; QRect m_cursorRect; size_t m_anchorPositionWithinSelection; size_t m_cursorPositionWithinSelection; -- cgit v1.2.3