Libplasma

RoundButton.qml
1/*
2 SPDX-FileCopyrightText: 2018 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Templates as T
10import org.kde.ksvg as KSvg
11//NOTE: importing PlasmaCore is necessary in order to make KSvg load the current Plasma Theme
12import org.kde.plasma.core as PlasmaCore
13import org.kde.kirigami as Kirigami
14import "private" as Private
15
16T.RoundButton {
17 id: control
18
19 Accessible.role: Accessible.Button
20
21 implicitWidth: Math.max(Kirigami.Units.gridUnit, contentItem.implicitWidth)
22 + leftPadding + rightPadding
23 implicitHeight: Math.max(Kirigami.Units.gridUnit, contentItem.implicitHeight)
24 + topPadding + bottomPadding
25
26 leftPadding: text.length > 0 ? surfaceNormal.margins.left : contentItem.extraSpace
27 topPadding: text.length > 0 ? surfaceNormal.margins.top : contentItem.extraSpace
28 rightPadding: text.length > 0 ? surfaceNormal.margins.right : contentItem.extraSpace
29 bottomPadding: text.length > 0 ? surfaceNormal.margins.bottom : contentItem.extraSpace
30
31 hoverEnabled: !Kirigami.Settings.tabletMode
32
33 Kirigami.Theme.colorSet: Kirigami.Theme.Button
34 Kirigami.Theme.inherit: false
35
36 contentItem: RowLayout {
37 // This is the spacing which will make the icon a square inscribed in the circle with an extra smallspacing of margins
38 readonly property int extraSpace: implicitWidth / 2 - implicitWidth / 2 * Math.sqrt(2) / 2 + Kirigami.Units.smallSpacing
39 Kirigami.Icon {
40 Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
41 Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
42 Layout.fillWidth: true
43 Layout.fillHeight: true
44 visible: source.length > 0
45 source: control.icon ? (control.icon.name || control.icon.source) : ""
46 }
47 Label {
48 visible: text.length > 0
49 text: control.text
50 font: control.font
51 opacity: enabled || control.highlighted || control.checked ? 1 : 0.4
52 color: Kirigami.Theme.textColor
53 horizontalAlignment: Text.AlignHCenter
54 verticalAlignment: Text.AlignVCenter
55 elide: Text.ElideRight
56 }
57 }
58
59 background: Item {
60 id: backgroundItem
61
62 opacity: control.enabled ? 1 : 0.6
63
64 // Round Button
65
66 KSvg.Svg {
67 id: buttonSvg
68 imagePath: "widgets/actionbutton"
69 colorSet: KSvg.Svg.Button
70 }
71
72 Private.RoundShadow {
73 id: roundShadow
74 visible: !control.flat || control.activeFocus || control.highlighted
75 anchors.fill: parent
76 state: {
77 if (control.down) {
78 return "hidden"
79 } else if (control.hovered) {
80 return "hover"
81 } else if (control.activeFocus || control.highlighted) {
82 return "focus"
83 } else {
84 return "shadow"
85 }
86 }
87 }
88
90 id: buttonItem
91 svg: buttonSvg
92 elementId: (control.down || control.checked) ? "pressed" : "normal"
93 anchors.fill: parent
94 //internal: if there is no hover status, don't paint on mouse over in touchscreens
95 opacity: (control.down || control.checked || !control.flat || (roundShadow.hasOverState && control.hovered)) ? 1 : 0
96 Behavior on opacity {
97 enabled: Kirigami.Units.longDuration > 0
98 PropertyAnimation { duration: Kirigami.Units.longDuration }
99 }
100 }
101
102 // Normal Button
103 // TODO: Make round button always round?
104
105 readonly property bool useNormalButton: control.text.length > 0
106
107 Private.ButtonShadow {
108 anchors.fill: parent
109 showShadow: backgroundItem.useNormalButton && !control.flat && (!control.down || !control.checked)
110 }
111
113 id: surfaceNormal
114 anchors.fill: parent
115 imagePath: "widgets/button"
116 prefix: "normal"
117 opacity: backgroundItem.useNormalButton && (!control.flat || control.hovered) && (!control.down || !control.checked) ? 1 : 0
118 Behavior on opacity {
119 enabled: Kirigami.Units.longDuration > 0
120 OpacityAnimator {
121 duration: Kirigami.Units.longDuration
122 easing.type: Easing.InOutQuad
123 }
124 }
125 }
126
127 Private.ButtonFocus {
128 anchors.fill: parent
129 showFocus: backgroundItem.useNormalButton && control.activeFocus && !control.down
130 }
131
132 Private.ButtonHover {
133 anchors.fill: parent
134 showHover: backgroundItem.useNormalButton && control.hovered && !control.down
135 }
136
138 anchors.fill: parent
139 imagePath: "widgets/button"
140 prefix: "pressed"
141 visible: backgroundItem.useNormalButton
142 opacity: control.checked || control.down ? 1 : 0
143 Behavior on opacity {
144 enabled: Kirigami.Units.longDuration > 0
145 OpacityAnimator {
146 duration: Kirigami.Units.longDuration
147 easing.type: Easing.InOutQuad
148 }
149 }
150 }
151 }
152}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.