1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2024 David Edmundson <davidedmundson@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "internalinputmethodcontext.h"
#include <QGuiApplication>
#include <QInputMethodEvent>
#include <QObject>
#include <QRect>
#include <QTextCharFormat>
#include <QWindow>
namespace KWin {
InternalInputMethodContext::InternalInputMethodContext(QObject *parent)
: QPlatformInputContext()
{
setParent(parent);
}
InternalInputMethodContext::~InternalInputMethodContext()
{
}
bool InternalInputMethodContext::isEnabled() const
{
return QGuiApplication::focusObject() != nullptr;
}
QString InternalInputMethodContext::surroundingText() const
{
return m_surroundingText;
}
int InternalInputMethodContext::cursorPosition() const
{
return m_cursorPos;
}
int InternalInputMethodContext::anchorPosition() const
{
return m_anchorPos;
}
bool InternalInputMethodContext::isValid() const
{
return true;
}
void InternalInputMethodContext::update(Qt::InputMethodQueries queries)
{
if (!QGuiApplication::focusObject()) {
return;
}
QInputMethodQueryEvent event(queries);
QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
m_surroundingText = event.value(Qt::ImSurroundingText).toString();
m_cursorPos = event.value(Qt::ImCursorPosition).toInt();
m_anchorPos = event.value(Qt::ImAnchorPosition).toInt();
Q_EMIT surroundingTextChanged();
}
if (queries & Qt::ImHints) {
// When kwin gets some text input with numbers and passwords this needs pouplating
}
if (queries & Qt::ImEnabled) {
Q_EMIT enabledChanged();
}
}
void InternalInputMethodContext::showInputPanel()
{
Q_EMIT showInputPanelRequested();
}
void InternalInputMethodContext::hideInputPanel()
{
Q_EMIT hideInputPanelRequested();
}
bool InternalInputMethodContext::isInputPanelVisible() const
{
return true;
}
QRectF InternalInputMethodContext::keyboardRect() const
{
return QRectF();
}
QLocale InternalInputMethodContext::locale() const
{
return QLocale();
}
Qt::LayoutDirection InternalInputMethodContext::inputDirection() const
{
return Qt::LayoutDirection();
}
void InternalInputMethodContext::setFocusObject(QObject *object)
{
if (inputMethodAccepted()) {
update(Qt::ImQueryAll);
}
// In QWindow::destory(), the focus window change may not be notified,
// Try to refresh potential missing enable change.
QWindow *window = QGuiApplication::focusWindow();
if (m_focusWindow != window) {
if (m_focusWindow) {
disconnect(m_focusWindow, &QObject::destroyed, this, &InternalInputMethodContext::enabledChanged);
}
m_focusWindow = window;
if (m_focusWindow) {
connect(m_focusWindow, &QObject::destroyed, this, &InternalInputMethodContext::enabledChanged);
}
}
Q_EMIT enabledChanged();
}
// From the InputMethod to our internal window
void InternalInputMethodContext::handlePreeditText(const QString &text, int cursorBegin, int cursorEnd)
{
QObject *focusObject = QGuiApplication::focusObject();
if (!focusObject) {
return;
}
QList<QInputMethodEvent::Attribute> attributes;
if (cursorBegin != -1 || cursorEnd != -1) {
// Current supported cursor shape is just line.
// It means, cursorEnd and cursorBegin are the same.
QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
text.length(),
1);
attributes.append(attribute1);
}
// only use single underline style for now
QTextCharFormat format;
format.setFontUnderline(true);
format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
0,
text.length(), format);
attributes.append(attribute2);
QInputMethodEvent event(text, attributes);
QCoreApplication::sendEvent(focusObject, &event);
}
void InternalInputMethodContext::handleCommitString(const QString &text)
{
QObject *focusObject = QGuiApplication::focusObject();
if (!focusObject) {
return;
}
QInputMethodEvent event(QString(), {});
event.setCommitString(text);
QCoreApplication::sendEvent(focusObject, &event);
}
void InternalInputMethodContext::handleDeleteSurroundingText(int deleteBefore, uint deleteAfter)
{
QObject *focusObject = QGuiApplication::focusObject();
if (!focusObject) {
return;
}
// insprired by QtWayland client code
const QString &surrounding = QInputMethod::queryFocusObject(Qt::ImSurroundingText, QVariant()).toString();
const int cursor = QInputMethod::queryFocusObject(Qt::ImCursorPosition, QVariant()).toInt();
const int anchor = QInputMethod::queryFocusObject(Qt::ImAnchorPosition, QVariant()).toInt();
const int selectionStart = qMin(cursor, anchor);
const int selectionEnd = qMax(cursor, anchor);
const int deleteBeforeTrasnslated = selectionStart - indexFromWayland(surrounding, -deleteBefore, selectionStart);
const int deleteAfterTranslated = indexFromWayland(surrounding, deleteAfter, selectionEnd) - selectionEnd;
QInputMethodEvent event(QString(), {});
event.setCommitString(QString(), deleteBeforeTrasnslated, deleteAfterTranslated);
QCoreApplication::sendEvent(focusObject, &event);
}
int InternalInputMethodContext::indexFromWayland(const QString &text, int length, int base)
{
if (length == 0) {
return base;
}
if (length < 0) {
const QByteArray &utf8 = QStringView{text}.left(base).toUtf8();
return QString::fromUtf8(utf8.first(qMax(utf8.size() + length, 0))).size();
} else {
const QByteArray &utf8 = QStringView{text}.mid(base).toUtf8();
return QString::fromUtf8(utf8.first(qMin(length, utf8.size()))).size() + base;
}
}
} //namespace
|