Warning, /frameworks/qqc2-desktop-style/org.kde.desktop/TextField.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2017 The Qt Company Ltd.
0004 
0005     SPDX-License-Identifier: LGPL-3.0-only OR GPL-2.0-or-later
0006 */
0007 
0008 
0009 import QtQuick 2.12
0010 import QtQuick.Window 2.1
0011 import QtQuick.Controls 2.15 as Controls
0012 import QtQuick.Templates 2.15 as T
0013 import org.kde.kirigami 2.4 as Kirigami
0014 import org.kde.qqc2desktopstyle.private 1.0 as StylePrivate
0015 import "private" as Private
0016 
0017 T.TextField {
0018     id: controlRoot
0019     palette: Kirigami.Theme.inherit ? Kirigami.Theme.palette : undefined
0020     Kirigami.Theme.colorSet: Kirigami.Theme.View
0021     Kirigami.Theme.inherit: false
0022 
0023     implicitWidth: Math.max(200,
0024                             placeholderText ? placeholder.implicitWidth + leftPadding + rightPadding : 0)
0025                             || contentWidth + leftPadding + rightPadding
0026     implicitHeight: Math.max(contentHeight + topPadding + bottomPadding,
0027                              background ? background.implicitHeight : 0,
0028                              placeholder.implicitHeight + topPadding + bottomPadding)
0029 
0030     padding: 6
0031 
0032     placeholderTextColor: Kirigami.Theme.disabledTextColor
0033     color: controlRoot.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
0034     selectionColor: Kirigami.Theme.highlightColor
0035     selectedTextColor: Kirigami.Theme.highlightedTextColor
0036     verticalAlignment: TextInput.AlignVCenter
0037     hoverEnabled: !Kirigami.Settings.tabletMode
0038 
0039     // Work around Qt bug where NativeRendering breaks for non-integer scale factors
0040     // https://bugreports.qt.io/browse/QTBUG-67007
0041     renderType: Screen.devicePixelRatio % 1 !== 0 ? Text.QtRendering : Text.NativeRendering
0042     selectByMouse: !Kirigami.Settings.tabletMode
0043 
0044     cursorDelegate: Kirigami.Settings.tabletMode ? mobileCursor : null
0045     Component {
0046         id: mobileCursor
0047         Private.MobileCursor {
0048             target: controlRoot
0049         }
0050     }
0051     onFocusChanged: {
0052         if (focus) {
0053             Private.MobileTextActionsToolBar.controlRoot = controlRoot;
0054         }
0055     }
0056 
0057     onTextChanged: Private.MobileTextActionsToolBar.shouldBeVisible = false;
0058 
0059     onPressed: Private.MobileTextActionsToolBar.shouldBeVisible = true;
0060 
0061     TapHandler {
0062         acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
0063         acceptedButtons: Qt.LeftButton | Qt.RightButton
0064 
0065         // unfortunately, taphandler's pressed event only triggers when the press is lifted
0066         // we need to use the longpress signal since it triggers when the button is first pressed
0067         longPressThreshold: 0
0068         onLongPressed: Private.TextFieldContextMenu.targetClick(point, controlRoot, null, null);
0069     }
0070 
0071     Keys.onPressed: {
0072         // trigger if context menu button is pressed
0073         Private.TextFieldContextMenu.targetKeyPressed(event, controlRoot)
0074 
0075         // Disable undo action for security reasons
0076         // See QTBUG-103934
0077         if ((echoMode === TextInput.PasswordEchoOnEdit || echoMode === TextInput.Password) && event.matches(StandardKey.Undo)) {
0078             event.accepted = true
0079         }
0080     }
0081 
0082     onPressAndHold: {
0083         if (!Kirigami.Settings.tabletMode) {
0084             return;
0085         }
0086         forceActiveFocus();
0087         cursorPosition = positionAt(event.x, event.y);
0088         selectWord();
0089     }
0090     Private.MobileCursor {
0091         target: controlRoot
0092         selectionStartHandle: true
0093         property var rect: controlRoot.positionToRectangle(controlRoot.selectionStart)
0094         x: rect.x + target.padding
0095         y: rect.y + target.padding
0096     }
0097 
0098     Text {
0099         id: placeholder
0100         x: controlRoot.leftPadding
0101         y: controlRoot.topPadding
0102         width: controlRoot.width - controlRoot.leftPadding - controlRoot.rightPadding
0103         height: controlRoot.height - controlRoot.topPadding - controlRoot.bottomPadding
0104 
0105         // Work around Qt bug where NativeRendering breaks for non-integer scale factors
0106         // https://bugreports.qt.io/browse/QTBUG-67007
0107         renderType: Screen.devicePixelRatio % 1 !== 0 ? Text.QtRendering : Text.NativeRendering
0108 
0109         text: controlRoot.placeholderText
0110         font: controlRoot.font
0111         color: controlRoot.placeholderTextColor
0112         LayoutMirroring.enabled: false
0113         horizontalAlignment: controlRoot.effectiveHorizontalAlignment
0114         verticalAlignment: controlRoot.verticalAlignment
0115         visible: !controlRoot.length && !controlRoot.preeditText && (!controlRoot.activeFocus || controlRoot.horizontalAlignment !== Qt.AlignHCenter)
0116         elide: Text.ElideRight
0117     }
0118 
0119     background: StylePrivate.StyleItem {
0120         control: controlRoot
0121         elementType: "edit"
0122 
0123         sunken: true
0124         hasFocus: controlRoot.activeFocus
0125         hover: controlRoot.hovered
0126     }
0127 }