From bf6f6cb73995d04f6d2ed3a67e69ee9bbde99c5b Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Wed, 13 Jun 2018 13:57:33 +0300 Subject: [PATCH] FloatingTextField added --- src/imports/controls/FloatingTextField.qml | 99 ++++++++++++++++++++++ src/imports/controls/qmldir | 1 + 2 files changed, 100 insertions(+) create mode 100644 src/imports/controls/FloatingTextField.qml diff --git a/src/imports/controls/FloatingTextField.qml b/src/imports/controls/FloatingTextField.qml new file mode 100644 index 00000000..5f4e44d9 --- /dev/null +++ b/src/imports/controls/FloatingTextField.qml @@ -0,0 +1,99 @@ +import QtQuick 2.11 +import QtQuick.Controls 2.4 +import QtQuick.Controls.Material 2.4 +import QtQuick.Templates 2.4 as T + +import Fluid.Controls 1.0 + +T.TextField { + id: control + + implicitWidth: Math.max((background ? background.implicitWidth : 0), + placeholderText ? + placeholder.implicitWidth + + leftPadding + rightPadding : 0) || + (contentWidth + leftPadding + rightPadding) + implicitHeight: Math.max(contentHeight + topPadding + bottomPadding, + (background ? background.implicitHeight : 0), + placeholder.implicitHeight + topPadding + bottomPadding) + + topPadding: contentHeight + bottomPadding: Units.smallSpacing * 2 + + selectByMouse: true + + Label { + id: placeholder + + anchors.top: parent.top + x: control.leftPadding + + font: control.font + text: control.placeholderText + elide: Label.AlignRight + width: control.width - (control.leftPadding + control.rightPadding) + + readonly property int offset: Qt.application.font.pixelSize + + + states: [ + State { + name: "focused" + when: control.focus + PropertyChanges { + target: placeholder + + color: enabled ? Material.accent : Material.hintTextColor + font.pointSize: Qt.application.font.pointSize + anchors.topMargin: 0 + } + }, + State { + name: "notFocusedNotEmpty" + when: !control.focus && control.text != "" + PropertyChanges { + target: placeholder + + font.pointSize: Qt.application.font.pointSize + anchors.topMargin: 0 + color: Material.hintTextColor + } + }, + State { + name: "notFocusedEmpty" + when: !control.focus && control.text == "" + PropertyChanges { + target: placeholder + + color: Material.hintTextColor + anchors.topMargin: offset * 2 + font.pointSize: Qt.application.font.pointSize + 2 + } + } + ] + + transitions: [ + Transition { + NumberAnimation { + properties: "anchors.topMargin,font.pointSize" + duration: Units.shortDuration + easing.type: Easing.InOutQuad + } + ColorAnimation { + duration: Units.shortDuration + } + } + ] + } + + background: Rectangle { + y: control.height - height - control.bottomPadding + 8 + implicitWidth: 120 + height: control.activeFocus || control.hovered ? 2 : 1 + color: control.activeFocus ? + control.Material.accentColor : + (control.hovered ? + control.Material.primaryTextColor : + control.Material.hintTextColor) + } +} diff --git a/src/imports/controls/qmldir b/src/imports/controls/qmldir index df323f5f..18a41e45 100644 --- a/src/imports/controls/qmldir +++ b/src/imports/controls/qmldir @@ -62,6 +62,7 @@ ApplicationWindow 1.1 ApplicationWindow11.qml AppToolBar 1.1 AppToolBar11.qml AppBar 1.1 AppBar11.qml Chip 1.1 Chip.qml +FloatingTextField 1.1 FloatingTextField.qml Page 1.1 Page11.qml PageSidebar 1.1 PageSidebar.qml SearchBar 1.1 SearchBar11.qml