macOS: Report preedit cursor position and cursor visibility correctly

Unfortunately QInputMethodEvent::Cursor defines length == 0 as the
cursor being hidden, and length > 0 as being visible. This is the
opposite of what native macOS application do.

A future improvement here would be to base the QInputMethodEvent
logic on QStyle::SH_BlinkCursorWhenTextSelected, which we already
respect for normal selections. That would also allow us to use
QInputMethodEvent::Cursor to set the preedit selection, which
we currently have to fake via QInputMethodEvent::TextFormat.

Pick-to: 6.2
Change-Id: I75b5d8c5403283a0988355e440a98b4df35ec995
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2021-08-27 19:18:36 +02:00
parent d7c6d69a8c
commit 9877d55a50

View File

@ -185,9 +185,16 @@
QString preeditString = QString::fromNSString(isAttributedString ? [text string] : text);
QList<QInputMethodEvent::Attribute> preeditAttributes;
preeditAttributes << QInputMethodEvent::Attribute(
QInputMethodEvent::Cursor, selectedRange.location + selectedRange.length, true);
// The QInputMethodEvent::Cursor specifies that the length
// determines whether the cursor is visible or not, but uses
// logic opposite of that of native AppKit application, where
// the cursor is visible if there's no selection, and hidden
// if there's a selection. Instead of passing on the length
// directly we need to inverse the logic.
const bool showCursor = !selectedRange.length;
preeditAttributes << QInputMethodEvent::Attribute(
QInputMethodEvent::Cursor, selectedRange.location, showCursor);
// QInputMethodEvent::Selection unfortunately doesn't apply to the
// preedit text, and QInputMethodEvent::Cursor which does, doesn't