plasma-framework/src/declarativeimports/plasmacomponents3/private/FlatButtonBackground.qml
Nate Graham 5447cef2d8 Port to singleton Units
The context property version is slower to access and won't be supported
in Qt6. Let's port away from it and use the singleton version instead.

Here was my full process for making this change:

1. Made the change with `find . -name '*.qml' | xargs perl -pi -e 's/units\./PlasmaCore\.Units\./g'`
2. Verified no more occurrences with `grep -r " units."`
3. Made sure this didn't change any comments in a silly way by inspecting the output of `git diff | grep "+   " | grep "//"`
4. Manually inspected the full git diff to make sure there were no other unintentional or silly changes (there were none)
5. verified that all changed files have the PlasmaCore import with the correct name with `for FILE in `git status | grep modified | cut -d ":" -f 3`; do grep -q "as PlasmaCore" $FILE || echo "$FILE needs the PlasmaCore import"; done` (one needed the import)
2021-03-07 13:34:47 +00:00

199 lines
6.3 KiB
QML

/*
* SPDX-FileCopyrightText: 2020 Noah Davis <noahadvs@gmail.com>
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
import QtQuick 2.6
import org.kde.plasma.core 2.0 as PlasmaCore
Item {
id: root
// TODO: Mark these required when our minimum Qt version is new enough.
property bool hovered
property bool pressed
property bool checked
property bool focused
property real leftMargin: surfaceHover.margins.left
property real topMargin: surfaceHover.margins.top
property real rightMargin: surfaceHover.margins.right
property real bottomMargin: surfaceHover.margins.bottom
property string usedPrefix: surfaceHover.usedPrefix
ButtonShadow {
anchors.fill: parent
showShadow: !(root.checked || root.pressed) && root.usedPrefix === "normal"
}
ButtonFocus {
anchors.fill: parent
showFocus: root.focused && !root.pressed
flat: true
}
// TODO: Maybe add a way to customize the look of normal state flat buttons with "toolbutton-normal"?
// TODO: Maybe add a way to customize the background of focused flat buttons with "toolbutton-focus-background"?
// TODO KF6: "flat" would be a more logical name than "toolbutton" since toolbuttons can be non-flat.
PlasmaCore.FrameSvgItem {
id: surfaceHover
anchors.fill: parent
imagePath: "widgets/button"
/* TODO KF6: making "toolbutton-hover" margins work like "hover"
* and using "hover" as a fallback would make more sense.
* If that is done, make ButtonHover handle flat button hover effects.
*/
// The fallback is "normal" to match PC2 behavior. Some 3rd party themes depend on this.
prefix: ["toolbutton-hover", "normal"]
}
PlasmaCore.FrameSvgItem {
id: surfacePressed
anchors.fill: parent
imagePath: "widgets/button"
prefix: ["toolbutton-pressed", "pressed"]
opacity: 0
}
state: {
if (root.checked || root.pressed) {
return "pressed";
} else if (root.hovered) {
return "hovered";
} else {
return "normal";
}
}
states: [
State {
name: "normal"
PropertyChanges {
target: root
leftMargin: surfaceHover.margins.left
topMargin: surfaceHover.margins.top
rightMargin: surfaceHover.margins.right
bottomMargin: surfaceHover.margins.bottom
usedPrefix: surfaceHover.usedPrefix
}
PropertyChanges {
target: surfaceHover
visible: false
}
PropertyChanges {
target: surfacePressed
opacity: 0
}
},
State {
name: "hovered"
PropertyChanges {
target: root
leftMargin: surfaceHover.margins.left
topMargin: surfaceHover.margins.top
rightMargin: surfaceHover.margins.right
bottomMargin: surfaceHover.margins.bottom
usedPrefix: surfaceHover.usedPrefix
}
PropertyChanges {
target: surfaceHover
visible: true
}
PropertyChanges {
target: surfacePressed
opacity: 0
}
},
State {
name: "pressed"
PropertyChanges {
target: root
leftMargin: surfacePressed.margins.left
topMargin: surfacePressed.margins.top
rightMargin: surfacePressed.margins.right
bottomMargin: surfacePressed.margins.bottom
usedPrefix: surfacePressed.usedPrefix
}
PropertyChanges {
target: surfaceHover
visible: false
}
PropertyChanges {
target: surfacePressed
opacity: 1
}
}
]
transitions: [
/* FIXME: For some reason, one of the surfaces will stop working when
* OpacityAnimator is used.
* OpacityAnimator would be more efficient.
*/
Transition {
from: "*"
to: "normal"
SequentialAnimation {
PropertyAction {
targets: [surfaceHover]
property: "visible"
value: false
}
NumberAnimation {
property: "opacity"
duration: PlasmaCore.Units.shortDuration
easing.type: Easing.OutQuad
}
PropertyAction {
targets: [surfacePressed]
property: "visible"
value: false
}
}
},
Transition {
from: "*"
to: "hovered"
SequentialAnimation {
PropertyAction {
target: surfaceHover
property: "visible"
value: true
}
NumberAnimation {
property: "opacity"
// Using a shorter duration here makes things feel more responsive.
duration: PlasmaCore.Units.shortDuration/2
easing.type: Easing.OutQuad
}
PropertyAction {
target: surfacePressed
property: "visible"
value: false
}
}
},
Transition {
from: "*"
to: "pressed"
SequentialAnimation {
PropertyAction {
target: surfacePressed
property: "visible"
value: true
}
NumberAnimation {
property: "opacity"
// Using a shorter duration here makes things feel more responsive.
duration: PlasmaCore.Units.shortDuration/2
easing.type: Easing.OutQuad
}
PropertyAction {
target: surfaceHover
property: "visible"
value: false
}
}
}
]
}