2020-09-24 09:57:54 +10:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-07-13 19:39:01 +10:00
|
|
|
import QtQuick 2.12
|
|
|
|
import QtQuick.Controls 2.12
|
2019-04-27 06:02:20 +10:00
|
|
|
|
|
|
|
TextField {
|
2019-07-18 15:53:28 +10:00
|
|
|
id: field
|
2020-07-12 14:25:57 +10:00
|
|
|
|
|
|
|
property string saveName: ""
|
|
|
|
property var saveId: "ALL"
|
|
|
|
property var saveProperties: ["text"]
|
|
|
|
|
|
|
|
property bool error: false
|
|
|
|
|
|
|
|
property alias radius: textFieldBackground.radius
|
|
|
|
property bool bordered: true
|
|
|
|
|
|
|
|
property color backgroundColor: theme.controls.textField.background
|
|
|
|
property color borderColor: theme.controls.textField.border
|
|
|
|
property color errorBorder: theme.controls.textField.errorBorder
|
|
|
|
|
|
|
|
property color focusedBackgroundColor:
|
|
|
|
theme.controls.textField.focusedBackground
|
|
|
|
property color focusedBorderColor: theme.controls.textField.focusedBorder
|
|
|
|
|
|
|
|
property var disabledText: null
|
|
|
|
property var defaultText: null
|
|
|
|
readonly property bool changed: text !== (defaultText || "")
|
|
|
|
|
|
|
|
property string previousDefaultText: "" // private
|
|
|
|
|
2020-09-04 08:27:31 +10:00
|
|
|
function reset() {
|
|
|
|
clear()
|
|
|
|
text = Qt.binding(() => defaultText || "")
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:30:36 +10:00
|
|
|
function loadState() {
|
|
|
|
if (! text) insertAtCursor(window.getState(this, "text", ""))
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:27:31 +10:00
|
|
|
function insertAtCursor(text) {
|
|
|
|
insert(cursorPosition, text)
|
|
|
|
}
|
2020-07-12 14:25:57 +10:00
|
|
|
|
|
|
|
|
2020-07-02 02:10:40 +10:00
|
|
|
text: defaultText || ""
|
2019-12-13 21:29:58 +11:00
|
|
|
opacity: enabled ? 1 : theme.disabledElementsOpacity
|
2019-12-11 06:32:42 +11:00
|
|
|
selectByMouse: true
|
2019-08-31 08:10:25 +10:00
|
|
|
leftPadding: theme.spacing
|
|
|
|
rightPadding: leftPadding
|
|
|
|
topPadding: theme.spacing / 1.5
|
|
|
|
bottomPadding: topPadding
|
|
|
|
|
2019-07-07 07:50:55 +10:00
|
|
|
font.family: theme.fontFamily.sans
|
|
|
|
font.pixelSize: theme.fontSize.normal
|
2019-08-31 07:04:42 +10:00
|
|
|
font.pointSize: -1
|
2019-04-29 05:13:18 +10:00
|
|
|
|
2019-09-08 07:02:09 +10:00
|
|
|
placeholderTextColor: theme.controls.textField.placeholderText
|
|
|
|
color: activeFocus ?
|
|
|
|
theme.controls.textField.focusedText :
|
|
|
|
theme.controls.textField.text
|
|
|
|
|
2019-04-29 05:13:18 +10:00
|
|
|
background: Rectangle {
|
|
|
|
id: textFieldBackground
|
2020-06-07 11:42:49 +10:00
|
|
|
radius: theme.radius
|
2019-07-18 15:53:28 +10:00
|
|
|
color: field.activeFocus ? focusedBackgroundColor : backgroundColor
|
2020-06-07 11:42:49 +10:00
|
|
|
|
2019-07-14 10:15:20 +10:00
|
|
|
border.width: bordered ? theme.controls.textField.borderWidth : 0
|
2020-06-07 11:42:49 +10:00
|
|
|
border.color: borderColor
|
|
|
|
|
2020-06-23 22:44:58 +10:00
|
|
|
HBottomFocusLine {
|
|
|
|
show: field.activeFocus
|
2020-06-07 11:42:49 +10:00
|
|
|
borderHeight: theme.controls.textField.borderWidth
|
|
|
|
color: error ? errorBorder : focusedBorderColor
|
|
|
|
}
|
2019-04-29 05:13:18 +10:00
|
|
|
}
|
2019-04-27 06:02:20 +10:00
|
|
|
|
2020-06-25 23:46:26 +10:00
|
|
|
Component.onCompleted: {
|
|
|
|
// Break binding
|
|
|
|
previousDefaultText = previousDefaultText
|
2020-09-04 08:30:36 +10:00
|
|
|
loadState()
|
2019-12-11 08:04:03 +11:00
|
|
|
}
|
2019-12-11 06:32:42 +11:00
|
|
|
|
2019-12-11 07:29:49 +11:00
|
|
|
onTextChanged: window.saveState(this)
|
2019-08-23 04:23:33 +10:00
|
|
|
|
2020-07-02 02:10:40 +10:00
|
|
|
onActiveFocusChanged:
|
|
|
|
if (defaultText !== null) text = text // Break binding
|
2020-06-25 23:46:26 +10:00
|
|
|
|
2020-07-02 02:10:40 +10:00
|
|
|
onDefaultTextChanged: if (defaultText !== null) {
|
2020-06-25 23:46:26 +10:00
|
|
|
if (text === previousDefaultText)
|
|
|
|
text = Qt.binding(() => defaultText)
|
|
|
|
|
|
|
|
previousDefaultText = defaultText
|
|
|
|
}
|
2020-06-03 17:41:30 +10:00
|
|
|
|
2020-08-24 05:41:20 +10:00
|
|
|
onPressed: ev => { if (ev.button === Qt.RightButton) contextMenu.spawn() }
|
|
|
|
onPressAndHold: ev => contextMenu.spawn()
|
|
|
|
|
2020-06-23 20:17:52 +10:00
|
|
|
// Prevent alt/super+any key from typing text
|
2019-08-23 04:23:33 +10:00
|
|
|
Keys.onPressed: if (
|
|
|
|
event.modifiers & Qt.AltModifier ||
|
|
|
|
event.modifiers & Qt.MetaModifier
|
2020-06-23 20:17:52 +10:00
|
|
|
) event.accepted = true
|
|
|
|
|
2020-07-10 14:02:43 +10:00
|
|
|
Keys.onMenuPressed: contextMenu.spawn(false)
|
|
|
|
|
2020-06-23 20:17:52 +10:00
|
|
|
// Prevent leaking arrow presses to parent elements when the carret is at
|
|
|
|
// the beginning or end of the text
|
2020-07-08 00:03:35 +10:00
|
|
|
Keys.onLeftPressed: event.accepted = cursorPosition === 0 && ! selectedText
|
|
|
|
Keys.onRightPressed:
|
|
|
|
event.accepted = cursorPosition === length && ! selectedText
|
2019-12-11 06:32:42 +11:00
|
|
|
|
|
|
|
|
2019-12-13 21:29:58 +11:00
|
|
|
Binding on color {
|
|
|
|
value: "transparent"
|
|
|
|
when: disabledText !== null && ! field.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
Binding on placeholderTextColor {
|
|
|
|
value: "transparent"
|
|
|
|
when: disabledText !== null && ! field.enabled
|
|
|
|
}
|
|
|
|
|
2020-03-18 08:41:15 +11:00
|
|
|
Binding on implicitHeight {
|
|
|
|
value: disabledTextLabel.implicitHeight
|
2020-05-30 04:43:30 +10:00
|
|
|
when: disabledText !== null && ! field.enabled
|
2020-03-18 08:41:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-16 19:42:41 +11:00
|
|
|
Behavior on opacity { HNumberAnimation {} }
|
2019-12-13 21:29:58 +11:00
|
|
|
Behavior on color { HColorAnimation {} }
|
|
|
|
Behavior on placeholderTextColor { HColorAnimation {} }
|
|
|
|
|
|
|
|
HLabel {
|
2020-03-18 08:41:15 +11:00
|
|
|
id: disabledTextLabel
|
2019-12-13 21:29:58 +11:00
|
|
|
anchors.fill: parent
|
|
|
|
visible: opacity > 0
|
|
|
|
opacity: disabledText !== null && parent.enabled ? 0 : 1
|
|
|
|
text: disabledText || ""
|
|
|
|
|
|
|
|
leftPadding: parent.leftPadding
|
|
|
|
rightPadding: parent.rightPadding
|
|
|
|
topPadding: parent.topPadding
|
|
|
|
bottomPadding: parent.bottomPadding
|
|
|
|
|
2020-03-18 08:41:15 +11:00
|
|
|
wrapMode:
|
2020-07-17 15:45:02 +10:00
|
|
|
parent.wrapMode === TextField.Wrap ? HLabel.Wrap :
|
|
|
|
parent.wrapMode === TextField.WordWrap ? HLabel.WordWrap :
|
|
|
|
parent.wrapMode === TextField.WrapAnywhere ? HLabel.WrapAnywhere :
|
2020-03-18 08:41:15 +11:00
|
|
|
Text.NoWrap
|
|
|
|
|
2019-12-13 21:29:58 +11:00
|
|
|
font.family: parent.font.family
|
|
|
|
font.pixelSize: parent.font.pixelSize
|
|
|
|
|
2019-12-16 19:42:41 +11:00
|
|
|
Behavior on opacity { HNumberAnimation {} }
|
2019-12-13 21:29:58 +11:00
|
|
|
}
|
2020-07-10 14:02:43 +10:00
|
|
|
|
|
|
|
HTextContextMenu { id: contextMenu }
|
2019-04-27 06:02:20 +10:00
|
|
|
}
|