Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FloatingTextField added #261

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/imports/controls/FloatingTextField.qml
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the import version compatible with Qt 5.10 ?
2.10 and 2.3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't think about that, I'm using latest qt. I'm not sure it's necessary to support 5.10


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using font.pointSize makes the animation choppy here, maybe use scale instead ?

Copy link
Contributor Author

@luntik2012 luntik2012 Jul 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works on my linux and windows systems

NumberAnimation {
    properties: "anchors.topMargin,font.pointSize"
    duration: Units.shortDuration
    easing.type: Easing.InOutQuad
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line needed ? Shouldn't we keep the font size the same as the textfield font size ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to guideline it's needed

}
}
]

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)
}
}
1 change: 1 addition & 0 deletions src/imports/controls/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -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