Files
plasma-keyboard/virtualkeyboard/keyboardpackageresolver.cpp
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

123 lines
3.6 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 "keyboardpackageresolver.h"
#include "logging.h"
#include <QFileInfo>
using namespace Qt::StringLiterals;
KeyboardPackageResolver::KeyboardPackageResolver(QObject *parent)
: QObject(parent)
{
rebuildIndex();
}
QStringList KeyboardPackageResolver::layoutIds() const
{
return m_layoutIds;
}
QString KeyboardPackageResolver::packageId(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
return layout ? layout->packageId : QString();
}
QString KeyboardPackageResolver::resolveLayoutId(const QString &id) const
{
const auto *layout = findLayout(id);
return layout ? layout->layoutId : QString();
}
QString KeyboardPackageResolver::layoutName(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
if (!layout) {
return {};
}
const QString name = KeyboardLayoutMetadata::keyboardLayoutName(layout->layoutDefinition);
return name.isEmpty() ? KeyboardLayoutMetadata::keyboardDisplayName(*layout) : name;
}
QString KeyboardPackageResolver::keyboardName(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
return layout ? KeyboardLayoutMetadata::keyboardDisplayName(*layout) : QString();
}
QString KeyboardPackageResolver::keyboardPrimaryLocale(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
return layout && !layout->locales.isEmpty() ? layout->locales.constFirst() : QString();
}
QStringList KeyboardPackageResolver::keyboardLocales(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
return layout ? layout->locales : QStringList();
}
QUrl KeyboardPackageResolver::layoutUrl(const QString &layoutId) const
{
const auto *layout = findLayout(layoutId);
if (!layout) {
return {};
}
const QString fileName = KeyboardLayoutMetadata::keyboardLayoutFile(layout->layoutDefinition);
const QString path = layout->packagePath + u"/contents/layouts/"_s + fileName;
if (fileName.isEmpty() || !QFileInfo::exists(path)) {
qCWarning(PlasmaKeyboard) << "KeyboardPackageResolver: no keyboard layout definition found for" << layoutId << path;
return {};
}
qCDebug(PlasmaKeyboard) << "KeyboardPackageResolver: loading keyboard layout definition" << layoutId << path;
return QUrl::fromLocalFile(path);
}
void KeyboardPackageResolver::rebuildIndex()
{
QList<KeyboardLayoutMetadata::Layout> layouts = KeyboardLayoutMetadata::keyboardLayouts();
for (const auto &layout : layouts) {
qCDebug(PlasmaKeyboard) << "KeyboardPackageResolver: discovered keyboard layout" << layout.layoutId << layout.packageId << layout.packagePath;
}
QStringList ids;
ids.reserve(layouts.size());
for (const auto &layout : layouts) {
ids.append(layout.layoutId);
}
if (m_layoutIds == ids && m_layouts.size() == layouts.size()) {
return;
}
m_layouts = layouts;
m_layoutIds = ids;
qCDebug(PlasmaKeyboard) << "KeyboardPackageResolver: active keyboard layouts" << m_layoutIds;
Q_EMIT layoutIdsChanged();
}
const KeyboardLayoutMetadata::Layout *KeyboardPackageResolver::findLayout(const QString &id) const
{
for (const auto &layout : m_layouts) {
if (layout.layoutId == id) {
return &layout;
}
}
for (const auto &layout : m_layouts) {
if (layout.packageId == id) {
return &layout;
}
}
return nullptr;
}