From 64475cc6780515172a5f38428854b14f69da070d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 17 Mar 2015 15:47:30 +0100 Subject: iOS: Leave it up to QIOSTextInputResponder to decide when to reconfigure We need to store the IM state as part of the QIOSTextInputResponder, so that the text responder can decide at a later point if it can still be the text responder for the current (possibly changed) focus object. Change-Id: I4ec861c5479238edf6a0fc101fa8241958af2d32 Reviewed-by: Richard Moe Gustavsen --- src/plugins/platforms/ios/qiosinputcontext.mm | 36 +++++++-------- src/plugins/platforms/ios/qiostextresponder.h | 4 ++ src/plugins/platforms/ios/qiostextresponder.mm | 62 ++++++++++++++++++++------ 3 files changed, 70 insertions(+), 32 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/ios/qiosinputcontext.mm b/src/plugins/platforms/ios/qiosinputcontext.mm index a558ebe1f7..2ebd87f29c 100644 --- a/src/plugins/platforms/ios/qiosinputcontext.mm +++ b/src/plugins/platforms/ios/qiosinputcontext.mm @@ -540,6 +540,9 @@ void QIOSInputContext::setFocusObject(QObject *focusObject) qImDebug() << "clearing focus object" << focusObject << "as hide-keyboard gesture is active"; clearCurrentFocusObject(); return; + } else if (focusObject == m_imeState.focusObject) { + qImDebug() << "same focus object as last update, skipping reset"; + return; } reset(); @@ -575,37 +578,30 @@ void QIOSInputContext::update(Qt::InputMethodQueries updatedProperties) // Mask for properties that we are interested in and see if any of them changed updatedProperties &= (Qt::ImEnabled | Qt::ImHints | Qt::ImQueryInput | Qt::ImPlatformData); - if (updatedProperties & Qt::ImEnabled) { - // Switching on and off input-methods needs a re-fresh of hints and platform - // data when we turn them on again, as the IM state we have may have been - // invalidated when IM was switched off. We could defer this until we know - // if IM was turned on, to limit the extra query parameters, but for simplicity - // we always do the update. - updatedProperties |= (Qt::ImHints | Qt::ImPlatformData); - } - qImDebug() << "fw =" << qApp->focusWindow() << "fo =" << qApp->focusObject(); + // Perform update first, so we can trust the value of inputMethodAccepted() Qt::InputMethodQueries changedProperties = m_imeState.update(updatedProperties); - if (m_textResponder && changedProperties & (Qt::ImHints | Qt::ImPlatformData)) { - qImDebug() << "current IM state requires new text responder"; - [m_textResponder autorelease]; - m_textResponder = 0; - } if (inputMethodAccepted()) { - if (!m_textResponder) { + if (!m_textResponder || [m_textResponder needsKeyboardReconfigure:changedProperties]) { qImDebug() << "creating new text responder"; + [m_textResponder autorelease]; m_textResponder = [[QIOSTextInputResponder alloc] initWithInputContext:this]; + } else { + qImDebug() << "no need to reconfigure keyboard, just notifying input delegate"; + [m_textResponder notifyInputDelegate:changedProperties]; } - if (![m_textResponder isFirstResponder]) - [m_textResponder becomeFirstResponder]; - [m_textResponder notifyInputDelegate:changedProperties]; + if (![m_textResponder isFirstResponder]) { + qImDebug() << "IM enabled, making text responder first responder"; + [m_textResponder becomeFirstResponder]; + } if (changedProperties & Qt::ImCursorRectangle) scrollToCursor(); } else if ([m_textResponder isFirstResponder]) { + qImDebug() << "IM not enabled, resigning text responder as first responder"; [m_textResponder resignFirstResponder]; } } @@ -635,6 +631,8 @@ bool QIOSInputContext::inputMethodAccepted() const */ void QIOSInputContext::reset() { + qImDebug() << "updating Qt::ImQueryAll and unmarking text"; + update(Qt::ImQueryAll); [m_textResponder setMarkedText:@"" selectedRange:NSMakeRange(0, 0)]; @@ -651,6 +649,8 @@ void QIOSInputContext::reset() */ void QIOSInputContext::commit() { + qImDebug() << "unmarking text"; + [m_textResponder unmarkText]; [m_textResponder notifyInputDelegate:Qt::ImSurroundingText]; } diff --git a/src/plugins/platforms/ios/qiostextresponder.h b/src/plugins/platforms/ios/qiostextresponder.h index 4cb8a9c815..96d30ea89a 100644 --- a/src/plugins/platforms/ios/qiostextresponder.h +++ b/src/plugins/platforms/ios/qiostextresponder.h @@ -34,6 +34,7 @@ #import #include +#include class QIOSInputContext; @@ -41,12 +42,15 @@ class QIOSInputContext; { @private QIOSInputContext *m_inputContext; + QInputMethodQueryEvent *m_configuredImeState; QString m_markedText; BOOL m_inSendEventToFocusObject; BOOL m_inSelectionChange; } - (id)initWithInputContext:(QIOSInputContext *)context; +- (BOOL)needsKeyboardReconfigure:(Qt::InputMethodQueries)updatedProperties; + - (void)notifyInputDelegate:(Qt::InputMethodQueries)updatedProperties; @property(readwrite, retain) UIView *inputView; diff --git a/src/plugins/platforms/ios/qiostextresponder.mm b/src/plugins/platforms/ios/qiostextresponder.mm index f0cb021da0..5d2a675f90 100644 --- a/src/plugins/platforms/ios/qiostextresponder.mm +++ b/src/plugins/platforms/ios/qiostextresponder.mm @@ -166,8 +166,9 @@ m_inSelectionChange = NO; m_inputContext = inputContext; - QVariantMap platformData = [self imValue:Qt::ImPlatformData].toMap(); - Qt::InputMethodHints hints = Qt::InputMethodHints([self imValue:Qt::ImHints].toUInt()); + m_configuredImeState = new QInputMethodQueryEvent(m_inputContext->imeState().currentState); + QVariantMap platformData = m_configuredImeState->value(Qt::ImPlatformData).toMap(); + Qt::InputMethodHints hints = Qt::InputMethodHints(m_configuredImeState->value(Qt::ImHints).toUInt()); self.returnKeyType = platformData.value(kImePlatformDataReturnKeyType).isValid() ? UIReturnKeyType(platformData.value(kImePlatformDataReturnKeyType).toInt()) : @@ -211,9 +212,42 @@ { self.inputView = 0; self.inputAccessoryView = 0; + delete m_configuredImeState; + [super dealloc]; } +- (BOOL)needsKeyboardReconfigure:(Qt::InputMethodQueries)updatedProperties +{ + if ((updatedProperties & Qt::ImEnabled)) { + Q_ASSERT([self currentImeState:Qt::ImEnabled].toBool()); + + // When switching on input-methods we need to consider hints and platform data + // as well, as the IM state that we were based on may have been invalidated when + // IM was switched off. + + qImDebug() << "IM was turned on, we need to check hints and platform data as well"; + updatedProperties |= (Qt::ImHints | Qt::ImPlatformData); + } + + // Based on what we set up in initWithInputContext above + updatedProperties &= (Qt::ImHints | Qt::ImPlatformData); + + if (!updatedProperties) + return NO; + + for (uint i = 0; i < (sizeof(Qt::ImQueryAll) * CHAR_BIT); ++i) { + if (Qt::InputMethodQuery property = Qt::InputMethodQuery(int(updatedProperties & (1 << i)))) { + if ([self currentImeState:property] != m_configuredImeState->value(property)) { + qImDebug() << property << "has changed since text responder was configured, need reconfigure"; + return YES; + } + } + } + + return NO; +} + - (BOOL)canBecomeFirstResponder { return YES; @@ -258,7 +292,7 @@ if ([UIResponder currentFirstResponder] == [self nextResponder]) { // We have resigned the keyboard, and transferred first responder back to the parent view Q_ASSERT(!FirstResponderCandidate::currentCandidate()); - if ([self imValue:Qt::ImEnabled].toBool()) { + if ([self currentImeState:Qt::ImEnabled].toBool()) { // The current focus object expects text input, but there // is no keyboard to get input from. So we clear focus. qImDebug() << "no keyboard available, clearing focus object"; @@ -320,7 +354,7 @@ QCoreApplication::sendEvent(focusObject, &e); } -- (QVariant)imValue:(Qt::InputMethodQuery)query +- (QVariant)currentImeState:(Qt::InputMethodQuery)query { return m_inputContext->imeState().currentState.value(query); } @@ -337,7 +371,7 @@ -(UITextPosition *)endOfDocument { - int endPosition = [self imValue:Qt::ImSurroundingText].toString().length(); + int endPosition = [self currentImeState:Qt::ImSurroundingText].toString().length(); return [QUITextPosition positionWithIndex:endPosition]; } @@ -361,8 +395,8 @@ - (UITextRange *)selectedTextRange { - int cursorPos = [self imValue:Qt::ImCursorPosition].toInt(); - int anchorPos = [self imValue:Qt::ImAnchorPosition].toInt(); + int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt(); + int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt(); return [QUITextRange rangeWithNSRange:NSMakeRange(qMin(cursorPos, anchorPos), qAbs(anchorPos - cursorPos))]; } @@ -370,7 +404,7 @@ { int s = static_cast([range start]).index; int e = static_cast([range end]).index; - return [self imValue:Qt::ImSurroundingText].toString().mid(s, e - s).toNSString(); + return [self currentImeState:Qt::ImSurroundingText].toString().mid(s, e - s).toNSString(); } - (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange @@ -482,8 +516,8 @@ if (!m_markedText.isEmpty()) return CGRectZero; - int cursorPos = [self imValue:Qt::ImCursorPosition].toInt(); - int anchorPos = [self imValue:Qt::ImAnchorPosition].toInt(); + int cursorPos = [self currentImeState:Qt::ImCursorPosition].toInt(); + int anchorPos = [self currentImeState:Qt::ImAnchorPosition].toInt(); NSRange r = static_cast(range).range; QList attrs; @@ -547,7 +581,7 @@ int p = static_cast(position).index; if (direction == UITextLayoutDirectionLeft) return [QUITextRange rangeWithNSRange:NSMakeRange(0, p)]; - int l = [self imValue:Qt::ImSurroundingText].toString().length(); + int l = [self currentImeState:Qt::ImSurroundingText].toString().length(); return [QUITextRange rangeWithNSRange:NSMakeRange(p, l - p)]; } @@ -555,7 +589,7 @@ { // No API in Qt for determining this. Use sensible default instead: Q_UNUSED(point); - return [QUITextPosition positionWithIndex:[self imValue:Qt::ImCursorPosition].toInt()]; + return [QUITextPosition positionWithIndex:[self currentImeState:Qt::ImCursorPosition].toInt()]; } - (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range @@ -563,14 +597,14 @@ // No API in Qt for determining this. Use sensible default instead: Q_UNUSED(point); Q_UNUSED(range); - return [QUITextPosition positionWithIndex:[self imValue:Qt::ImCursorPosition].toInt()]; + return [QUITextPosition positionWithIndex:[self currentImeState:Qt::ImCursorPosition].toInt()]; } - (UITextRange *)characterRangeAtPoint:(CGPoint)point { // No API in Qt for determining this. Use sensible default instead: Q_UNUSED(point); - return [QUITextRange rangeWithNSRange:NSMakeRange([self imValue:Qt::ImCursorPosition].toInt(), 0)]; + return [QUITextRange rangeWithNSRange:NSMakeRange([self currentImeState:Qt::ImCursorPosition].toInt(), 0)]; } - (void)setMarkedTextStyle:(NSDictionary *)style -- cgit v1.2.3