| 1 |
// SPDX-FileName: LineEdit.qml |
| 2 |
// SPDX-FileComment: LineEdit widget used in the launcher |
| 3 |
// SPDX-FileCopyrightText: Copyright (C) 2025 James Turner |
| 4 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
import QtQuick 2.4 |
| 7 |
import FlightGear 1.0 |
| 8 |
|
| 9 |
FocusScope { |
| 10 |
id: root |
| 11 |
property string label |
| 12 |
property string placeholder: "" |
| 13 |
property alias validator: edit.validator |
| 14 |
property alias text: edit.text |
| 15 |
property bool enabled: true |
| 16 |
property bool commitOnReturn: true |
| 17 |
|
| 18 |
property alias suggestedWidthString: metrics.text |
| 19 |
readonly property int suggestedWidth: useFullWidth ? root.width |
| 20 |
: ((metrics.width == 0) ? Style.strutSize * 4 |
| 21 |
: metrics.width) |
| 22 |
|
| 23 |
property bool useFullWidth: false |
| 24 |
|
| 25 |
readonly property int xOffsetForEditFrame: editFrame.x |
| 26 |
|
| 27 |
implicitHeight: editFrame.height |
| 28 |
implicitWidth: (metrics.width == 0) ? label.implicitWidth + Style.strutSize * 4 |
| 29 |
: metrics.width + label.implicitWidth + (Style.margin * 3) |
| 30 |
|
| 31 |
Keys.onReturnPressed: { |
| 32 |
if (activeFocus && commitOnReturn) { |
| 33 |
focus = false; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
TextMetrics { |
| 38 |
id: metrics |
| 39 |
} |
| 40 |
|
| 41 |
StyledText { |
| 42 |
id: label |
| 43 |
text: root.label |
| 44 |
anchors.verticalCenter: editFrame.verticalCenter |
| 45 |
hover: editFrame.activeFocus |
| 46 |
enabled: root.enabled |
| 47 |
} |
| 48 |
|
| 49 |
Rectangle { |
| 50 |
id: editFrame |
| 51 |
|
| 52 |
anchors.left: label.right |
| 53 |
anchors.margins: Style.margin |
| 54 |
|
| 55 |
height: edit.implicitHeight + Style.margin |
| 56 |
width: Math.min(root.width - (label.width + Style.margin), Math.max(suggestedWidth, edit.implicitWidth) + Style.margin * 2); |
| 57 |
|
| 58 |
radius: Style.roundRadius |
| 59 |
border.color: root.enabled ? (edit.activeFocus ? Style.frameColor : Style.minorFrameColor) : Style.disabledMinorFrameColor |
| 60 |
border.width: 1 |
| 61 |
clip: true |
| 62 |
color: Style.backgroundColor |
| 63 |
|
| 64 |
TextInput { |
| 65 |
id: edit |
| 66 |
enabled: root.enabled |
| 67 |
anchors.verticalCenter: parent.verticalCenter |
| 68 |
anchors.left: parent.left |
| 69 |
anchors.right: parent.right |
| 70 |
anchors.margins: Style.margin |
| 71 |
selectByMouse: true |
| 72 |
focus: true |
| 73 |
color: enabled ? (activeFocus ? Style.themeColor : Style.baseTextColor) : Style.disabledTextColor |
| 74 |
font.pixelSize: Style.baseFontPixelSize |
| 75 |
StyledText { |
| 76 |
id: placeholder |
| 77 |
visible: (edit.text.length == 0) && !edit.activeFocus |
| 78 |
text: root.placeholder |
| 79 |
enabled: root.enabled |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
} |