Fix spinbox glitch on min/max numbers

This commit is contained in:
miruka 2021-02-28 16:28:56 -04:00
parent 41bc082044
commit 768a343412
2 changed files with 48 additions and 38 deletions

View File

@ -3,7 +3,6 @@
- sfx selection - sfx selection
- custom action - custom action
- fix spinbox buttons
- combo box custom item - combo box custom item
- fix flickable popups can't be flicked by keyboard - fix flickable popups can't be flicked by keyboard
- seen tooltips can't be shown on image hover - seen tooltips can't be shown on image hover

View File

@ -2,6 +2,7 @@
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Controls 2.12 import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
SpinBox { SpinBox {
id: box id: box
@ -11,53 +12,63 @@ SpinBox {
function reset() { value = Qt.binding(() => defaultValue || 0) } function reset() { value = Qt.binding(() => defaultValue || 0) }
// XXX TODO: default binding break
value: defaultValue || 0 value: defaultValue || 0
implicitHeight: theme.baseElementsHeight implicitHeight: theme.baseElementsHeight
padding: 0 padding: 0
editable: true editable: true
to: 2147483647
background: null background: null
contentItem: HTextField { contentItem: HRowLayout {
HButton {
text: qsTr("-")
font.pixelSize: theme.fontSize.biggest
autoRepeat: true
autoRepeatInterval: 50
// Don't set enabled to false or it glitches, use opacity instead
opacity: box.value > box.from ? 1 : theme.disabledElementsOpacity
onPressed: if (box.value > box.from) box.decrease()
Layout.fillHeight: true
Behavior on opacity { HNumberAnimation {} }
}
HTextField {
id: textField id: textField
height: parent.height height: parent.height
implicitWidth: 90 * theme.uiScale implicitWidth: 90 * theme.uiScale
radius: 0 radius: 0
horizontalAlignment: Qt.AlignHCenter horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter verticalAlignment: Qt.AlignVCenter
// FIXME text: box.value
text: box.textFromValue(box.value, box.locale)
readOnly: ! box.editable readOnly: ! box.editable
validator: box.validator validator: box.validator
inputMethodHints: Qt.ImhFormattedNumbersOnly inputMethodHints: Qt.ImhFormattedNumbersOnly
onTextChanged: if (text && text !== "-") box.value = text onTextEdited: {
if (! text || text === "-") return
const input = parseInt(text, 10)
box.value = Math.max(box.from, Math.min(box.to, input))
}
} }
down.indicator: HButton { HButton {
x: box.mirrored ? parent.width - width : 0
height: parent.height
font.pixelSize: theme.fontSize.biggest
text: qsTr("-")
autoRepeat: true
autoRepeatInterval: 50
onPressed: box.decrease()
}
up.indicator: HButton {
x: box.mirrored ? 0 : parent.width - width
height: parent.height
font.pixelSize: theme.fontSize.biggest
text: qsTr("+") text: qsTr("+")
font.pixelSize: theme.fontSize.biggest
autoRepeat: true autoRepeat: true
autoRepeatInterval: 50 autoRepeatInterval: 50
opacity: box.value < box.to ? 1 : theme.disabledElementsOpacity
onPressed: if (box.value < box.to) box.increase()
Layout.fillHeight: true
onPressed: box.increase() Behavior on opacity { HNumberAnimation {} }
} }
}
down.indicator: null
up.indicator: null
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
@ -65,7 +76,7 @@ SpinBox {
cursorShape: textField.hovered ? Qt.IBeamCursor : Qt.ArrowCursor cursorShape: textField.hovered ? Qt.IBeamCursor : Qt.ArrowCursor
onWheel: wheel => { onWheel: wheel => {
wheel.angleDelta.y < 0 ? box.decrease() : box.increase() wheel.angleDelta.y < 0 ? box.decrease() : box.increase()
wheel.accepted() wheel.accepted = true
} }
} }
} }