/*SPDX-FileCopyrightText: 2014-2017 Weng Xuetian <wengxt@gmail.com>SPDX-License-Identifier: GPL-2.0-or-later*/import QtQuickimport QtQuick.Layoutsimport org.kde.plasma.core as PlasmaCoreimport org.kde.plasma.components as PlasmaComponents3import plasma.applet.org.kde.plasma.kimpanel as Kimpanelimport org.kde.kirigami as Kirigamiimport org.kde.plasma.plasmoidPlasmaCore.Dialog {id: inputpaneltype: PlasmaCore.Dialog.PopupMenuflags: {var flag = Qt.WindowTransparentForInput | Qt.WindowStaysOnTopHint | Qt.WindowDoesNotAcceptFocus;if (Qt.platform.pluginName.includes("wayland")) {flag |= Qt.ToolTip;} else {flag |= Qt.Popup;}return flag;}location: PlasmaCore.Types.Floatingvisible: helper.auxVisible || helper.preeditVisible || helper.lookupTableVisiblerequired property Kimpanel.Kimpanel helperreadonly property bool verticalLayout: (helper.lookupTableLayout === 1) || (helper.lookupTableLayout === 0 && Plasmoid.configuration.vertical_lookup_table);property int highlightCandidate: helper.lookupTableCursorproperty int hoveredCandidate: -1property font preferredFont: Plasmoid.configuration.use_default_font ? Kirigami.Theme.defaultFont : Plasmoid.configuration.fontreadonly property alias textOffset: fontMetrics.ascentreadonly property alias labelHeight: fontMetrics.heightproperty rect position: helper.spotRectonPositionChanged : updatePosition();onWidthChanged : updatePosition();onHeightChanged : updatePosition();mainItem: Item {Layout.minimumWidth: childrenRect.widthLayout.minimumHeight: childrenRect.heightLayout.maximumWidth: childrenRect.widthLayout.maximumHeight: childrenRect.heightFontMetrics {id: fontMetricsfont: inputpanel.preferredFont}Column {spacing: Kirigami.Units.smallSpacingRow {id: textLabelwidth: auxLabel.width + preedit.widthheight: inputpanel.labelHeightvisible: inputpanel.helper.auxVisible || inputpanel.helper.preeditVisiblebaselineOffset: inputpanel.textOffsetPlasmaComponents3.Label {id: auxLabelanchors.baseline: parent.baselinefont: inputpanel.preferredFonttext: inputpanel.helper.auxTexttextFormat: Text.PlainTextvisible: inputpanel.helper.auxVisible}Item {id: preeditwidth: preeditLabel1.width + preeditLabel2.width + 2height: parent.heightclip: truevisible: inputpanel.helper.preeditVisiblebaselineOffset: inputpanel.textOffsetPlasmaComponents3.Label {id: preeditLabel1anchors.baseline: parent.baselineanchors.left: parent.leftfont: inputpanel.preferredFonttextFormat: Text.PlainText}Rectangle {color: Kirigami.Theme.textColorheight: parent.heightwidth: 1opacity: 0.8z: 1anchors.verticalCenter: parent.verticalCenteranchors.left: preeditLabel1.right}PlasmaComponents3.Label {id: preeditLabel2anchors.baseline: parent.baselineanchors.left: preeditLabel1.rightfont: inputpanel.preferredFonttextFormat: Text.PlainText}}}GridLayout {flow: inputpanel.verticalLayout ? GridLayout.TopToBottom : GridLayout.LeftToRightcolumns: inputpanel.verticalLayout ? 1 : tableList.count + 1rows: inputpanel.verticalLayout ? tableList.count + 1 : 1Repeater {model: ListModel {id: tableListdynamicRoles: true}delegate: Item {id: candidateDelegatewidth: candidate.width + highlight.marginHints.left + highlight.marginHints.rightheight: inputpanel.labelHeight + highlight.marginHints.top + highlight.marginHints.bottomLayout.minimumWidth: widthLayout.minimumHeight: heightLayout.maximumWidth: widthLayout.maximumHeight: heightreadonly property bool selected: inputpanel.highlightCandidate === model.index || candidateMouseArea.pressedRow {id: candidatewidth: childrenRect.widthheight: childrenRect.heightx: highlight.marginHints.lefty: highlight.marginHints.topbaselineOffset: inputpanel.textOffsetspacing: Kirigami.Units.smallSpacingPlasmaComponents3.Label {id: tableLabeltext: model.labeltextFormat: Text.PlainTextfont: inputpanel.preferredFontopacity: 0.8color: Kirigami.Theme.textColoranchors.baseline: parent.baseline}PlasmaComponents3.Label {text: model.textid: candidateTextLabeltextFormat: Text.PlainTextfont: inputpanel.preferredFontcolor: Kirigami.Theme.textColoranchors.baseline: parent.baseline}}MouseArea {id: candidateMouseAreaanchors.fill: parenthoverEnabled: trueonReleased: helper.selectCandidate(model.index)onContainsMouseChanged: {inputpanel.hoveredCandidate = containsMouse ? model.index : -1;}}CandidateHighlight {id: highlightz: -1visible: inputpanel.highlightCandidate === model.index || inputpanel.hoveredCandidate === model.indexselected: candidateDelegate.selectedanchors {fill: parent}}}}Row {id: buttonwidth: inputpanel.labelHeight * 2height: inputpanel.labelHeightLayout.minimumWidth: widthLayout.minimumHeight: heightLayout.maximumWidth: widthLayout.maximumHeight: heightspacing: Kirigami.Units.smallSpacingKirigami.Icon {id: prevButtonsource: inputpanel.verticalLayout ? "arrow-up" : "arrow-left"width: inputpanel.labelHeightheight: widthscale: prevButtonMouseArea.pressed ? 0.9 : 1active: prevButtonMouseArea.containsMouseMouseArea {id: prevButtonMouseAreaanchors.fill: parenthoverEnabled: trueonReleased: inputpanel.helper.lookupTablePageUp()}}Kirigami.Icon {id: nextButtonsource: inputpanel.verticalLayout ? "arrow-down" : "arrow-right"width: inputpanel.labelHeightheight: widthscale: nextButtonMouseArea.pressed ? 0.9 : 1active: nextButtonMouseArea.containsMouseMouseArea {id: nextButtonMouseAreaanchors.fill: parenthoverEnabled: trueonReleased: inputpanel.helper.lookupTablePageDown()}}}}}Kimpanel.Screen {id: screen}// Kimpanel's update may come in with in several DBus message. Use// timer to delegate the update so we get less flicker.Timer {id: timerinterval: 1onTriggered: inputpanel.updateLookupTable()}Connections {target: inputpanel.helperfunction onPreeditTextChanged() {var charArray = [...inputpanel.helper.preeditText];preeditLabel1.text = charArray.slice(0, inputpanel.helper.caretPos).join('');preeditLabel2.text = charArray.slice(inputpanel.helper.caretPos).join('');}function onLookupTableChanged() {timer.restart();}}}function updateLookupTable() {inputpanel.hoveredCandidate = -1;button.visible = helper.lookupTableVisible && (helper.hasPrev || helper.hasNext);var labels = helper.labels;var texts = helper.texts;var length = Math.min(labels.length, texts.length);if (helper.lookupTableVisible) {if (length< tableList.count) {tableList.remove(length, tableList.count - length);}for (var i = 0; i < length; i ++) {if (i >= tableList.count) {tableList.append({'label' : labels[i], 'text': texts[i], 'index': i});} else {tableList.set(i, {'label' : labels[i], 'text': texts[i], 'index': i});}}} else {tableList.clear();}}function updatePosition() {var rect = screen.geometryForPoint(position.x, position.y);var devicePerPixelRatio = screen.devicePixelRatioForPoint(position.x, position.y);var x, y;var width = inputpanel.width * devicePerPixelRatio;var height = inputpanel.height * devicePerPixelRatio;if (position.x < rect.x) {x = rect.x;} else {x = position.x;}if (position.y < rect.y) {y = rect.y;} else {y = position.y + position.height;}if (x + width > rect.x + rect.width) {x = rect.x + rect.width - width;}if (y + height > rect.y + rect.height) {if (y > rect.y + rect.height) {y = rect.y + rect.height - height - 40;} else {y = y - height - (position.height === 0 ? 40 : position.height);}}var newRect = screen.geometryForPoint(x, y);devicePerPixelRatio = screen.devicePixelRatioForPoint(x, y);inputpanel.x = newRect.x + (x - newRect.x) / devicePerPixelRatio;inputpanel.y = newRect.y + (y - newRect.y) / devicePerPixelRatio;}}