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

122 lines
3.8 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 "keyboardlayoutmetadata.h"
#include <KPluginMetaData>
#include <QDir>
#include <QFileInfo>
#include <QLocale>
#include <QStandardPaths>
#include <QVariantList>
using namespace Qt::StringLiterals;
static QVariantMap keyboardSection(const KPluginMetaData &metadata)
{
return metadata.rawData().toVariantMap().value(u"X-Plasma-Keyboard"_s).toMap();
}
static QStringList locales(const KPluginMetaData &metadata)
{
return keyboardSection(metadata).value(u"Locales"_s).toStringList();
}
static QVariantList keyboardLayoutDefinitions(const KPluginMetaData &metadata)
{
return keyboardSection(metadata).value(u"KeyboardLayouts"_s).toList();
}
static QString packageId(const QString &directoryName, const KPluginMetaData &metadata)
{
return metadata.pluginId().isEmpty() ? directoryName : metadata.pluginId();
}
static QStringList rootPaths()
{
return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, u"plasma/keyboard/keyboardpackages"_s, QStandardPaths::LocateDirectory);
}
static QString localLayoutId(const QVariantMap &layout)
{
return layout.value(u"Id"_s).toString();
}
QString KeyboardLayoutMetadata::localeDisplayName(const QString &locale)
{
if (locale.isEmpty()) {
return {};
}
const QLocale qlocale(locale);
const QString language = qlocale.nativeLanguageName();
const QString territory = qlocale.nativeTerritoryName();
return territory.isEmpty() ? language : language + u" ("_s + territory + u")"_s;
}
QString KeyboardLayoutMetadata::keyboardLayoutName(const QVariantMap &layout)
{
return layout.value(u"Name"_s).toString();
}
QString KeyboardLayoutMetadata::keyboardLayoutFile(const QVariantMap &layout)
{
return layout.value(u"File"_s).toString();
}
QStringList KeyboardLayoutMetadata::keyboardLayoutFormFactors(const QVariantMap &layout)
{
return layout.value(u"FormFactors"_s).toStringList();
}
QString KeyboardLayoutMetadata::keyboardDisplayName(const Layout &layout)
{
return layout.keyboardName.isEmpty() ? layout.packageId : layout.keyboardName;
}
QList<KeyboardLayoutMetadata::Layout> KeyboardLayoutMetadata::keyboardLayouts()
{
QList<Layout> layouts;
for (const auto &root : rootPaths()) {
const QDir rootDir(root);
const QFileInfoList entries = rootDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const auto &entry : entries) {
const QString metadataPath = entry.absoluteFilePath() + u"/metadata.json"_s;
if (!QFileInfo::exists(metadataPath)) {
continue;
}
const KPluginMetaData metadata = KPluginMetaData::fromJsonFile(metadataPath);
if (!metadata.isValid()) {
continue;
}
const QString currentPackageId = packageId(entry.fileName(), metadata);
for (const auto &value : keyboardLayoutDefinitions(metadata)) {
const QVariantMap layoutDefinition = value.toMap();
const QString currentLocalLayoutId = localLayoutId(layoutDefinition);
if (currentLocalLayoutId.isEmpty()) {
continue;
}
layouts.append({
.packageId = currentPackageId,
.layoutId = currentPackageId + u"/"_s + currentLocalLayoutId,
.packagePath = entry.absoluteFilePath(),
.keyboardName = metadata.name(),
.locales = locales(metadata),
.formFactors = keyboardLayoutFormFactors(layoutDefinition),
.layoutDefinition = layoutDefinition,
});
}
}
}
return layouts;
}