Warning, /plasma-mobile/qmlkonsole/src/contents/ui/TerminalKeyToolBar.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2019-2020 Jonah Brüchert <jbb@kaidan.im>
0002 // SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 import QtQuick
0007 import QtQuick.Layouts
0008 import QtQuick.Controls
0009 
0010 import org.kde.konsoleqml
0011 import org.kde.kirigami as Kirigami
0012 
0013 import org.kde.qmlkonsole
0014 
0015 ToolBar {
0016     id: root
0017     
0018     property var terminal
0019     
0020     height: visible ? implicitHeight : 0
0021     
0022     function pressKeyWithModifier(key, modifier) {
0023         terminal.pressKey(key, modifier, true);
0024         modifierHolder.forceActiveFocus();
0025     }
0026     
0027     function pressModifierButton(modifier) {
0028         modifierHolder.text = "";
0029         modifierHolder.forceActiveFocus();
0030     }
0031     
0032     function pressKey(key, keyString) {
0033         if (ctrlButton.checked) {
0034             pressKeyWithModifier(key, Qt.ControlModifier);
0035         } else if (altButton.checked) {
0036             pressKeyWithModifier(key, Qt.AltModifier);
0037         } else {
0038             terminal.pressKey(key, 0, true, 0, keyString);
0039         }
0040     }
0041     
0042     contentItem: RowLayout {
0043         spacing: Kirigami.Units.smallSpacing
0044         
0045         // HACK: in order to add ctrl and alt button functionality, we have the keyboard enter the character into this invisible
0046         //       textfield, and then once a character is entered, send it to the terminal as a keypress.
0047         TextField {
0048             id: modifierHolder
0049             visible: false
0050             inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
0051             
0052             onTextChanged: {
0053                 if (text.length == 1) {
0054                     root.pressKey(Util.getKeyFromString(text), text);
0055                 }
0056                 text = "";
0057             }
0058         }
0059         
0060         // modifier buttons row
0061         ScrollView {
0062             Layout.fillWidth: true
0063             clip: true
0064             
0065             RowLayout {
0066                 property real delegateWidth: root.width 
0067                 
0068                 TerminalKeyButton {
0069                     id: ctrlButton
0070                     Layout.preferredWidth: Math.max(implicitWidth + Kirigami.Units.smallSpacing * 2, Math.round(Kirigami.Units.gridUnit * 2))
0071                     Layout.preferredHeight: Kirigami.Units.gridUnit * 2
0072                     text: i18nc("Control Key (should match the key on the keyboard of the locale)", "Ctrl")
0073                     checkable: true
0074                     
0075                     onClicked: {
0076                         if (checked) {
0077                             altButton.checked = false;
0078                             root.pressModifierButton(Qt.ControlModifier);
0079                         } else {
0080                             terminal.forceActiveFocus();
0081                         }
0082                         Qt.inputMethod.show();
0083                     }
0084                 }
0085                 
0086                 TerminalKeyButton {
0087                     id: altButton
0088                     Layout.preferredWidth: Math.max(implicitWidth + Kirigami.Units.smallSpacing * 2, Math.round(Kirigami.Units.gridUnit * 1.75))
0089                     Layout.preferredHeight: Kirigami.Units.gridUnit * 2
0090                     text: i18nc("Alt Key (should match the key on the keyboard of the locale)", "Alt")
0091                     checkable: true
0092                     
0093                     onClicked: {
0094                         if (checked) {
0095                             ctrlButton.checked = false;
0096                             root.pressModifierButton(Qt.AltModifier);
0097                         } else {
0098                             terminal.forceActiveFocus();
0099                         }
0100                         Qt.inputMethod.show();
0101                     }
0102                 }
0103 
0104                 TerminalKeyButton {
0105                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0106                     text: i18nc("Escape key (should match the key on the keyboard of the locale)", "Esc")
0107                     onClicked: {
0108                         terminal.pressKey(Qt.Key_Escape, 0, true)
0109                     }
0110                 }
0111                 TerminalKeyButton {
0112                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0113                     text: i18nc("Tab character key (should match the key on the keyboard of the locale)", "Tab")
0114                     onClicked: terminal.pressKey(Qt.Key_Tab, 0, true, 0, "")
0115                 }
0116 
0117                 TerminalKeyButton {
0118                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0119                     text: "↑"
0120                     onClicked: root.pressKey(Qt.Key_Up, "")
0121                     autoRepeat: true
0122                 }
0123                 TerminalKeyButton {
0124                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0125                     text: "↓"
0126                     onClicked: root.pressKey(Qt.Key_Down, "")
0127                     autoRepeat: true
0128                 }
0129                 TerminalKeyButton {
0130                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0131                     text: "←"
0132                     onClicked: root.pressKey(Qt.Key_Left, "")
0133                     autoRepeat: true
0134                 }
0135                 TerminalKeyButton {
0136                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0137                     text: "→"
0138                     onClicked: root.pressKey(Qt.Key_Right, "")
0139                     autoRepeat: true
0140                 }
0141                 
0142                 TerminalKeyButton {
0143                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0144                     text: "|"
0145                     onClicked: root.pressKey(Qt.Key_Bar, "|")
0146                 }
0147                 TerminalKeyButton {
0148                     Layout.preferredWidth: Kirigami.Units.gridUnit * 2
0149                     text: "~"
0150                     onClicked: root.pressKey(Qt.Key_AsciiTilde, "~")
0151                 }
0152             }
0153         }
0154         
0155         Kirigami.Separator { Layout.fillHeight: true }
0156         
0157         // control vkbd visibility
0158         ToolButton {
0159             Layout.preferredWidth: height
0160             Layout.preferredHeight: Kirigami.Units.gridUnit * 2
0161             icon.name: "input-keyboard-virtual"
0162             text: i18n("Toggle Virtual Keyboard")
0163             display: AbstractButton.IconOnly
0164             onClicked: {
0165                 if (Qt.inputMethod.visible) {
0166                     Qt.inputMethod.hide();
0167                 } else {
0168                     terminal.forceActiveFocus();
0169                     Qt.inputMethod.show();
0170                 }
0171             }
0172         }
0173     }
0174 }