From 30276cec3d47f4f4fa847fea90214ec5c28d54ed Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Mon, 11 Oct 2021 14:26:38 +0200 Subject: [PATCH] QIOSTextInputOverlay: listen for selection changes, also for Qt::ImReadOnly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The focus object can emit selection updates (e.g from mouse drag), and accept modifying it through IM when dragging on the handles, even if it doesn't accept text input and IM in general (and hence return false from inputMethodAccepted()). This is typically the case for read-only text fields. So we should listen for selection changes and enable handles also for this case (unless the IM hints tells us explicitly not to use handles/edit menu). Fixes: QTBUG-91545 Change-Id: I2855505fc229e954b2c43f5e11374e64bba7eb4e Reviewed-by: Tor Arne Vestbø --- .../platforms/ios/qiostextinputoverlay.mm | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/ios/qiostextinputoverlay.mm b/src/plugins/platforms/ios/qiostextinputoverlay.mm index 2ab732df7a0..c72d8dc596a 100644 --- a/src/plugins/platforms/ios/qiostextinputoverlay.mm +++ b/src/plugins/platforms/ios/qiostextinputoverlay.mm @@ -1048,15 +1048,39 @@ void QIOSTextInputOverlay::updateFocusObject() s_editMenu = nullptr; } - if (platformInputContext()->inputMethodAccepted()) { - s_editMenu = [QIOSEditMenu new]; - m_cursorRecognizer = [QIOSCursorRecognizer new]; + const QVariant hintsVariant = QGuiApplication::inputMethod()->queryFocusObject(Qt::ImHints, QVariant()); + const Qt::InputMethodHints hints = Qt::InputMethodHints(hintsVariant.toUInt()); + if (hints & Qt::ImhNoTextHandles) + return; + + // The focus object can emit selection updates (e.g from mouse drag), and + // accept modifying it through IM when dragging on the handles, even if it + // doesn't accept text input and IM in general (and hence return false from + // inputMethodAccepted()). This is the case for read-only text fields. + // Therefore, listen for selection changes also when the focus object + // reports that it's ImReadOnly (which we take as a hint that it's actually + // a text field, and that selections therefore might happen). But since + // we have no guarantee that the focus object can actually accept new selections + // through IM (and since we also need to respect if the input accepts selections + // in the first place), we only support selections started by the text field (e.g from + // mouse drag), even if we in theory could also start selections from a loupe. + + const bool inputAccepted = platformInputContext()->inputMethodAccepted(); + const bool readOnly = QGuiApplication::inputMethod()->queryFocusObject(Qt::ImReadOnly, QVariant()).toBool(); + + if (inputAccepted || readOnly) { + if (!(hints & Qt::ImhNoEditMenu)) + s_editMenu = [QIOSEditMenu new]; m_selectionRecognizer = [QIOSSelectionRecognizer new]; m_openMenuOnTapRecognizer = [QIOSTapRecognizer new]; - m_cursorRecognizer.enabled = YES; m_selectionRecognizer.enabled = YES; m_openMenuOnTapRecognizer.enabled = YES; } + + if (inputAccepted) { + m_cursorRecognizer = [QIOSCursorRecognizer new]; + m_cursorRecognizer.enabled = YES; + } } QT_END_NAMESPACE