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

171 lines
6.4 KiB
QML

/*
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
*/
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.keyboard
import org.kde.plasma.keyboard.virtualkeyboard
import org.kde.plasma.keyboard.virtualkeyboard.components
Item {
id: root
property var languagePopup: null
property var virtualKeyboardContext: null
property real padding: 0
property real leftPadding: padding
property real rightPadding: padding
property real topPadding: padding
property real bottomPadding: padding
readonly property var inputEngine: virtualKeyboardContext ? virtualKeyboardContext.inputEngine : VirtualKeyboard.inputEngine
readonly property var keyboardController: virtualKeyboardContext ? virtualKeyboardContext.keyboardController : VirtualKeyboard.keyboardController
readonly property var keyboardPackageResolver: virtualKeyboardContext ? virtualKeyboardContext.keyboardPackageResolver : VirtualKeyboard.keyboardPackageResolver
readonly property bool navigationAvailable: keyboardNavigation.navigationAvailable
property alias navigationModeActive: keyboardNavigation.navigationModeActive
readonly property string layoutId: keyboardController ? keyboardController.layoutId : ""
readonly property string packageId: keyboardPackageResolver ? keyboardPackageResolver.packageId(layoutId) : ""
readonly property string layoutType: keyboardController ? keyboardController.layoutType : "main"
readonly property var layoutDefinition: layoutDefinitionLoader.item
readonly property var textComposer: (layoutLoader.item && layoutLoader.item.textComposer) || (layoutDefinition ? layoutDefinition.textComposer : null)
implicitHeight: __keyboardHeight
implicitWidth: __keyboardWidth
// Always have the keyboard panel be 30% of the screen height, or 150px, whichever is larger
readonly property real __baseKeyboardInputAreaHeight: Math.max(Screen.height * 0.3, 150)
readonly property real __keyboardInputAreaHeight: __baseKeyboardInputAreaHeight * (layoutLoader.item?.panelHeightFactor ?? 1)
readonly property real __contentHeight: __keyboardInputAreaHeight + (candidateStrip.visible ? candidateStrip.implicitHeight : 0)
readonly property real __horizontalPadding: leftPadding + rightPadding
readonly property real __verticalPadding: topPadding + bottomPadding
readonly property real __keyboardHeight: __contentHeight + __verticalPadding
readonly property real __keyboardWidth: Math.round(__contentHeight * aspectRatio) + __horizontalPadding
// The value to multiply the height by to get the width
readonly property real aspectRatio: {
// Ratio to just fill the screen width
const availableWidth = Math.max(0, Screen.width - __horizontalPadding);
const fillScreenWidthRatio = availableWidth / __contentHeight;
if (PlasmaKeyboardSettings.panelFillScreenWidth) {
return fillScreenWidthRatio;
}
const targetAspectRatio = 3.0; // Target width ratio: 3 * height
return Math.min(fillScreenWidthRatio, targetAspectRatio);
}
// The design ratios for the keyboard height
readonly property real __keyboardDesignHeight: {
if (Screen.width < 500) {
// Phone mode
return 800;
}
if (Screen.width < 1200) {
// Wider
return 600;
}
// Widest
return 700;
}
readonly property real panelScaleHint: Math.max(0.3, __keyboardInputAreaHeight / __keyboardDesignHeight)
function resetNavigation() {
keyboardNavigation.resetNavigation();
}
function handleNavigationPressed(key) {
keyboardNavigation.handleNavigationPressed(key);
}
function handleNavigationReleased(key) {
keyboardNavigation.handleNavigationReleased(key);
}
function updateScaleHint() {
if (width <= 0) {
return;
}
BreezeConstants.scaleHint = panelScaleHint;
}
Binding {
target: root.inputEngine
property: "textComposer"
value: root.textComposer
when: root.inputEngine !== null && root.textComposer !== null
}
onPanelScaleHintChanged: updateScaleHint()
Component.onCompleted: updateScaleHint()
Loader {
id: layoutDefinitionLoader
source: root.keyboardPackageResolver ? root.keyboardPackageResolver.layoutUrl(root.layoutId) : ""
onStatusChanged: {
if (status === Loader.Error) {
console.warn("Failed to load keyboard layout definition", root.layoutId, source);
}
}
}
KeyboardNavigation {
id: keyboardNavigation
layoutLoader: layoutLoader
keyboardStrip: candidateStrip
languagePopup: root.languagePopup
}
ColumnLayout {
anchors {
fill: parent
leftMargin: root.leftPadding
rightMargin: root.rightPadding
topMargin: root.topPadding
bottomMargin: root.bottomPadding
}
spacing: 0
KeyboardStrip {
id: candidateStrip
Layout.fillWidth: true
inputEngine: root.inputEngine
}
Loader {
id: layoutLoader
Layout.preferredWidth: parent.width
Layout.maximumWidth: (item && item.maxWidthToHeightRatio !== -1) ? (item.maxWidthToHeightRatio * height) : parent.width
Layout.preferredHeight: root.__keyboardInputAreaHeight
Layout.alignment: Qt.AlignHCenter
sourceComponent: root.layoutDefinition ? (root.layoutDefinition[root.layoutType] ?? root.layoutDefinition.main) : null
onLoaded: {
resetNavigation();
if (item && item.virtualKeyboardContext !== undefined) {
item.virtualKeyboardContext = root.virtualKeyboardContext;
}
if (item && item.layoutId !== undefined) {
item.layoutId = root.layoutId;
}
if (item && item.packageId !== undefined) {
item.packageId = root.packageId;
}
}
onStatusChanged: {
if (status === Loader.Error) {
console.warn("Failed to load keyboard layout", root.layoutId, root.layoutType);
}
}
}
}
}