Files
Devin Lin c4fe01beaf Wrap keyboard layouts in KeyboardLayoutDefinition
Currently layouts are created with the KeyboardLayout component. When we
switch to alternate layouts (i.e. numpad, symbols), the Keyboard switches out the
KeyboardLayout completely to the alternate layout's KeyboardLayout.

We currently don't have a "holding" object for a keyboard layout,
preventing us from having a shared TextComposer session between all of
the "sublayouts" within a layout (ex. QWERTY <-> symbols <-> numpad)

This is problematic for CJK because the TextComposer gets reset when we
switch to other pages, like the symbols page.

---

This MR introduces a "KeyboardLayoutDefinition" holding component for
layouts, so that we can retain state within a keyboard layout. In this
case, we keep the TextComposer on the KeyboardLayoutDefinition rather
than the KeyboardLayout so it isn't reset when we switch to other
keyboard pages.

I also replaced the JSON-specified alternate layouts with it now being
specified on the KeyboardLayoutDefinition, which makes things a bit more
centralized.

---

Kind of annoying that I had to do this after porting the layouts, but
this was a big oversight I missed that came up during CJK testing :/
2026-10-25 14:00:43 -04:00

459 lines
11 KiB
C++

/*
SPDX-FileCopyrightText: 2026 Devin Lin <devin@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "inputengine.h"
#include "inputbackend.h"
#include "textcomposers/abstracttextcomposer.h"
#include <QLocale>
static bool isNonShiftModifierKey(int key)
{
switch (key) {
case Qt::Key_Control:
case Qt::Key_Alt:
case Qt::Key_Meta:
case Qt::Key_AltGr:
return true;
default:
return false;
}
}
InputEngine::InputEngine(InputBackend *backend, QObject *parent)
: QObject(parent)
, m_backend(backend)
, m_locale(QLocale().name())
{
Q_ASSERT(m_backend);
connectBackend();
connect(this, &InputEngine::uppercaseChanged, this, [this] {
if (!m_textComposer) {
return;
}
m_textComposer->setTextCase(uppercase() ? AbstractTextComposer::TextCase::Upper : AbstractTextComposer::TextCase::Lower);
});
connect(this, &InputEngine::textComposerChanged, this, [this] {
if (!m_textComposer) {
return;
}
m_textComposer->setTextCase(uppercase() ? AbstractTextComposer::TextCase::Upper : AbstractTextComposer::TextCase::Lower);
});
connect(this, &InputEngine::preeditTextChanged, this, &InputEngine::updateAutomaticShift);
}
AbstractTextComposer *InputEngine::textComposer() const
{
return m_textComposer;
}
void InputEngine::setTextComposer(AbstractTextComposer *textComposer)
{
if (m_textComposer == textComposer) {
return;
}
if (m_textComposer) {
disconnect(m_textComposer, nullptr, this, nullptr);
m_textComposer->reset();
}
m_textComposer = textComposer;
if (m_textComposer) {
m_textComposer->setInputEngine(this);
m_textComposer->setTextCase(uppercase() ? AbstractTextComposer::TextCase::Upper : AbstractTextComposer::TextCase::Lower);
connect(m_textComposer, &AbstractTextComposer::autoCapitalizationSupportedChanged, this, &InputEngine::resetShiftState);
connect(m_textComposer, &AbstractTextComposer::autoCapitalizationPolicyChanged, this, &InputEngine::updateAutomaticShift);
connect(m_textComposer, &QObject::destroyed, this, [this] {
clear();
Q_EMIT textComposerChanged();
});
}
Q_EMIT textComposerChanged();
if (!m_textComposer || !m_textComposer->autoCapitalizationSupported()) {
setCapsLockActive(false);
setShiftActive(false);
} else if (inputMethodHints().testAnyFlags(Qt::ImhPreferUppercase | Qt::ImhUppercaseOnly)) {
resetShiftState();
} else {
updateAutomaticShift();
}
}
bool InputEngine::shiftActive() const
{
return m_shiftActive;
}
void InputEngine::setShiftActive(bool active)
{
if (m_shiftActive == active) {
return;
}
m_shiftActive = active;
if (!m_shiftActive && m_backend->pressedKeys().contains(Qt::Key_Shift)) {
m_backend->sendKeyPressed(Qt::Key_Shift, false);
}
Q_EMIT shiftActiveChanged();
Q_EMIT uppercaseChanged();
}
bool InputEngine::capsLockActive() const
{
return m_capsLockActive;
}
void InputEngine::setCapsLockActive(bool active)
{
if (m_capsLockActive == active) {
return;
}
m_capsLockActive = active;
Q_EMIT capsLockActiveChanged();
Q_EMIT uppercaseChanged();
}
bool InputEngine::uppercase() const
{
if (m_capsLockActive) {
return true;
}
if (!m_shiftActive) {
return false;
}
for (const int key : m_backend->pressedKeys()) {
if (isNonShiftModifierKey(key)) {
return false;
}
}
return true;
}
QString InputEngine::preeditText() const
{
return m_preeditText;
}
void InputEngine::setPreeditText(const QString &text)
{
if (m_preeditText == text) {
return;
}
m_preeditText = text;
m_backend->setPreeditText(text);
Q_EMIT preeditTextChanged();
}
QString InputEngine::preeditPrefix() const
{
return m_preeditPrefix;
}
void InputEngine::setPreeditPrefix(const QString &text)
{
if (m_preeditPrefix == text) {
return;
}
m_preeditPrefix = text;
Q_EMIT preeditPrefixChanged();
}
QStringList InputEngine::candidates() const
{
return m_candidates;
}
void InputEngine::setCandidates(const QStringList &candidates)
{
if (m_candidates == candidates) {
return;
}
m_candidates = candidates;
Q_EMIT candidatesChanged();
Q_EMIT wordCandidateListVisibleHintChanged();
}
QString InputEngine::locale() const
{
return m_locale;
}
void InputEngine::setLocale(const QString &locale)
{
if (m_locale == locale) {
return;
}
m_locale = locale;
Q_EMIT localeChanged();
}
int InputEngine::anchorPosition() const
{
return utf16PositionFromUtf8Offset(m_backend->anchorPositionUtf8());
}
int InputEngine::cursorPosition() const
{
return utf16PositionFromUtf8Offset(m_backend->cursorPositionUtf8());
}
Qt::InputMethodHints InputEngine::inputMethodHints() const
{
return m_backend->inputMethodHints();
}
QString InputEngine::surroundingText() const
{
return m_backend->surroundingText();
}
QString InputEngine::selectedText() const
{
const QByteArray utf8 = surroundingText().toUtf8();
const int start = qMin(m_backend->cursorPositionUtf8(), m_backend->anchorPositionUtf8());
const int end = qMax(m_backend->cursorPositionUtf8(), m_backend->anchorPositionUtf8());
return QString::fromUtf8(utf8.mid(start, end - start));
}
QList<int> InputEngine::pressedKeys() const
{
return m_backend->pressedKeys();
}
bool InputEngine::preeditBubbleVisibleHint() const
{
return m_textComposer && m_textComposer->showsPreeditBubble();
}
WordCandidateListModel *InputEngine::wordCandidateListModel() const
{
if (!m_wordCandidateListModel) {
m_wordCandidateListModel = new WordCandidateListModel(const_cast<InputEngine *>(this), const_cast<InputEngine *>(this));
}
return m_wordCandidateListModel;
}
bool InputEngine::wordCandidateListVisibleHint() const
{
return !m_candidates.isEmpty();
}
bool InputEngine::sendTextComposerKey(int key, const QString &text)
{
if (!m_textComposer) {
return false;
}
if (!m_backend->pressedKeys().isEmpty()) {
return sendDirectKeyClick(key);
}
return m_textComposer->keyEvent(Qt::Key(key), text);
}
bool InputEngine::sendDirectKey(int key, bool pressed)
{
return m_backend->sendKeyPressed(key, pressed);
}
bool InputEngine::isKeyPressed(int key) const
{
return m_backend->pressedKeys().contains(key);
}
bool InputEngine::selectCandidate(int index)
{
if (!m_textComposer) {
return false;
}
return m_textComposer->selectCandidate(index);
}
void InputEngine::commit()
{
if (!m_preeditText.isEmpty()) {
m_backend->commitText(m_preeditText);
clearCompositionState(true);
}
}
void InputEngine::commit(const QString &text, int replaceFrom, int replaceLength)
{
if (replaceLength != 0) {
m_backend->deleteSurroundingText(replaceFrom, replaceLength);
}
if (!text.isEmpty()) {
m_backend->commitText(text);
}
clearCompositionState(true);
}
void InputEngine::clear()
{
clearCompositionState(true);
}
bool InputEngine::handleKeyCommit(int key, const QString &text)
{
switch (key) {
case Qt::Key_Backspace:
case Qt::Key_Delete:
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Tab:
case Qt::Key_Space:
m_backend->sendKeyClick(key);
return true;
default:
break;
}
if (text.isEmpty()) {
return false;
}
commit(text);
if (m_shiftActive && !m_capsLockActive) {
setShiftActive(false);
}
return true;
}
void InputEngine::connectBackend()
{
if (!m_backend->parent()) {
m_backend->setParent(this);
}
connect(m_backend, &InputBackend::surroundingTextChanged, this, [this] {
Q_EMIT surroundingTextChanged();
Q_EMIT selectedTextChanged();
Q_EMIT anchorPositionChanged();
Q_EMIT cursorPositionChanged();
updateAutomaticShift();
});
connect(m_backend, &InputBackend::inputMethodHintsChanged, this, [this] {
Q_EMIT inputMethodHintsChanged();
resetShiftState();
});
connect(m_backend, &InputBackend::pressedKeysChanged, this, [this] {
Q_EMIT pressedKeysChanged();
Q_EMIT uppercaseChanged();
});
connect(m_backend, &InputBackend::activeChanged, this, [this] {
Q_EMIT surroundingTextChanged();
Q_EMIT selectedTextChanged();
Q_EMIT anchorPositionChanged();
Q_EMIT cursorPositionChanged();
Q_EMIT inputMethodHintsChanged();
resetShiftState();
});
connect(m_backend, &InputBackend::resetRequested, this, [this] {
if (m_textComposer) {
m_textComposer->reset();
} else {
clear();
}
resetShiftState();
});
}
void InputEngine::resetShiftState()
{
if (!m_backend->isActive()) {
setCapsLockActive(false);
setShiftActive(false);
return;
}
const auto hints = inputMethodHints();
const bool preferUppercase =
m_textComposer && m_textComposer->autoCapitalizationSupported() && hints.testAnyFlags(Qt::ImhPreferUppercase | Qt::ImhUppercaseOnly);
setCapsLockActive(preferUppercase);
setShiftActive(false);
if (!preferUppercase) {
updateAutomaticShift();
}
}
void InputEngine::updateAutomaticShift()
{
if (m_capsLockActive) {
return;
}
if (!m_textComposer || !m_textComposer->autoCapitalizationSupported()) {
setShiftActive(false);
return;
}
const auto hints = inputMethodHints();
const auto disablingHints = Qt::ImhNoAutoUppercase | Qt::ImhUppercaseOnly | Qt::ImhLowercaseOnly | Qt::ImhEmailCharactersOnly | Qt::ImhUrlCharactersOnly
| Qt::ImhDialableCharactersOnly | Qt::ImhFormattedNumbersOnly | Qt::ImhDigitsOnly;
if (!m_backend->isActive() || hints.testAnyFlags(disablingHints) || hints.testFlag(Qt::ImhPreferLowercase) || !m_preeditText.isEmpty()) {
setShiftActive(false);
return;
}
const QString textBeforeCursor = surroundingText().left(cursorPosition());
setShiftActive(m_textComposer->shouldAutoCapitalize(textBeforeCursor));
}
void InputEngine::clearCompositionState(bool updateBackend)
{
const bool hadPreedit = !m_preeditText.isEmpty();
const bool hadCandidates = !m_candidates.isEmpty();
if (updateBackend && hadPreedit) {
m_backend->setPreeditText(QString());
}
if (hadPreedit) {
m_preeditText.clear();
Q_EMIT preeditTextChanged();
}
if (!m_preeditPrefix.isEmpty()) {
m_preeditPrefix.clear();
Q_EMIT preeditPrefixChanged();
}
if (hadCandidates) {
m_candidates.clear();
Q_EMIT candidatesChanged();
Q_EMIT wordCandidateListVisibleHintChanged();
}
}
bool InputEngine::sendDirectKeyClick(int key)
{
return m_backend->sendKeyClick(key);
}
int InputEngine::utf16PositionFromUtf8Offset(uint32_t offset) const
{
return QString::fromUtf8(surroundingText().toUtf8().first(offset)).size();
}