status-desktop/ui/shared/IconButton.qml
Pascal Precht f4d16d7661 refactor(IconButton): turn IconButton into proper button control
So far our `IconButton` hasn't been a proper button control which comes
with many downsides, such as:

- Some default button behaviours need to be simulated (e.g. `onClicked`)
- Any support built-in features for all controls in QML like `ToolTip` aren't can't be used
- There are probably accessibility aspects to it as well

We use the `IconButton` in many different places. Sometimes it doesn't
even act as a button, but just as an icon. I suggest we introduce a separate
`StatusIcon` component for that in future changes.

This commit turns the `IconButton` into a proper `RoundButton`, restoring
the control behaviour and features we get from QML.

This also required to expose the `icon` property as a `iconImg`,
because a `RoundButton` already comes with an `icon`.
On the other hand, we could remove the `onClick` simulation and can now
take advantage of components like `ToolTip`.
2020-08-25 10:26:34 +02:00

93 lines
2.3 KiB
QML

import QtQuick 2.3
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import Qt.labs.platform 1.1
import "../imports"
RoundButton {
property int iconWidth: 14
property int iconHeight: 14
property alias iconImg: imgIcon
property bool clickable: true
property string iconName: "plusSign"
property color color: Style.current.blue
icon.width: iconWidth
icon.height: iconHeight
id: btnAddContainer
width: 36
height: 36
radius: width / 2
background: Rectangle {
color: parent.color
radius: parent.radius
}
Image {
id: imgIcon
fillMode: Image.PreserveAspectFit
source: "../app/img/" + parent.iconName + ".svg"
width: btnAddContainer.iconWidth
height: btnAddContainer.iconHeight
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
state: "default"
rotation: 0
states: [
State {
name: "default"
PropertyChanges {
target: imgIcon
rotation: 0
}
},
State {
name: "rotated"
PropertyChanges {
target: imgIcon
rotation: 45
}
}
]
transitions: [
Transition {
from: "default"
to: "rotated"
RotationAnimation {
duration: 150
direction: RotationAnimation.Clockwise
easing.type: Easing.InCubic
}
},
Transition {
from: "rotated"
to: "default"
RotationAnimation {
duration: 150
direction: RotationAnimation.Counterclockwise
easing.type: Easing.OutCubic
}
}
]
}
onClicked: {
if (btnAddContainer.clickable) {
imgIcon.state = "rotated"
}
}
MouseArea {
id: mouseArea
visible: btnAddContainer.clickable
anchors.fill: parent
onPressed: mouse.accepted = false
cursorShape: Qt.PointingHandCursor
}
}