Debian Codegraph @

src/imports/Components/Themes/Ambiance/2.0/ButtonStyle.qml

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Florian Boucault <[email protected]>
17 */
18
19 import QtQuick 2.4
20 import Lomiri.Components
21
22 Item {
23 id: buttonStyle
24
25 property var button: styledItem
26 property real minimumWidth: units.gu(10)
27 property real horizontalPadding: units.gu(1)
28 property color defaultColor: LomiriColors.graphite
29 property font defaultFont: Qt.font({family: "Ubuntu", pixelSize: FontUtils.sizeToPixels("medium")})
30 property Gradient defaultGradient
31 property real buttonFaceOffset: 0
32 property bool stroke: button.hasOwnProperty("strokeColor") && button.strokeColor !== Qt.rgba(0.0, 0.0, 0.0, 0.0)
33 /*!
34 The property overrides the button's default background with an item. This
35 item can be used by derived styles to reuse the ButtonStyle and override
36 the default coloured background with an image or any other drawing.
37 The default value is null.
38 */
39 property Item backgroundSource: null
40
41 width: button.width
42 height: button.height
43 implicitWidth: Math.max(minimumWidth, foreground.implicitWidth + 2*horizontalPadding)
44 implicitHeight: units.gu(4)
45
46 LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
47 LayoutMirroring.childrenInherit: true
48
49 /*! \internal */
50 // Color properties in a JS ternary operator don't work as expected in
51 // QML because it overwrites alpha values with 1. A workaround is to use
52 // Qt.rgba(). For more information, see
53 // https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1197802 and
54 // https://bugreports.qt-project.org/browse/QTBUG-32238.
55 function __colorHack(color) { return Qt.rgba(color.r, color.g, color.b, color.a); }
56
57
58 /* The proxy is necessary because Gradient.stops and GradientStop.color are
59 non-NOTIFYable properties. They cannot be written to so it is fine but
60 the proxy avoids the warnings.
61 */
62 property QtObject gradientProxy: gradientProxyObject
63 QtObject {
64 id: gradientProxyObject
65 property color topColor
66 property color bottomColor
67
68 function updateGradient() {
69 if (button.gradient) {
70 topColor = button.gradient.stops[0].color;
71 bottomColor = button.gradient.stops[1].color;
72 }
73 }
74
75 Component.onCompleted: {
76 updateGradient();
77 button.gradientChanged.connect(updateGradient);
78 }
79 }
80
81 // Use the gradient if it is defined and the color has not been set manually
82 // or the gradient has been set manually
83 property bool isGradient: button.gradient && (button.color == defaultColor ||
84 button.gradient != defaultGradient)
85
86 FocusShape {
87 }
88
89 Image {
90 id: strokeBorder
91 anchors.fill: parent
92 anchors.margins: -units.gu(0.5)
93 // FIXME: this PNG is way too big (462x108) and do not scale properly
94 // ie. the corners are visually incorrect at most sizes
95 source: stroke ? Qt.resolvedUrl("../artwork/[email protected]") : ""
96 scale: units.gridUnit / 27
97 visible: false
98 cache: false
99 asynchronous: true
100 }
101
102 ShaderEffect {
103 id: colorizedImage
104
105 anchors.fill: parent
106 visible: stroke && strokeBorder.status == Image.Ready
107
108 property Item source: visible ? strokeBorder : null
109 property color keyColorOut: stroke ? button.strokeColor : Qt.rgba(0.0, 0.0, 0.0, 0.0)
110 property color keyColorIn: Qt.rgba(1.0, 1.0, 1.0, 1.0)
111 property real threshold: 1.0
112
113 fragmentShader: "qrc:/2.0/shaders/colorizeimage.frag.qsb"
114 }
115
116 LomiriShape {
117 id: background
118 anchors.fill: parent
119 borderSource: "radius_idle.sci" // Deprecated, use a dedicated shape.
120 visible: stroke ? false : ((backgroundColor.a != 0.0) || backgroundSource)
121 source: backgroundSource
122
123 backgroundColor: backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.topColor) : __colorHack(button.color))
124 secondaryBackgroundColor: backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.bottomColor) : __colorHack(button.color))
125 backgroundMode: isGradient ? LomiriShape.VerticalGradient : LomiriShape.SolidColor
126 opacity: styledItem.enabled ? 1.0 : 0.6
127 }
128
129 LomiriShape {
130 id: backgroundPressed
131 anchors.fill: parent
132 backgroundColor: stroke ? button.strokeColor : background.backgroundColor
133 secondaryBackgroundColor: background.secondaryBackgroundColor
134 backgroundMode: stroke ? LomiriShape.SolidColor : LomiriShape.VerticalGradient
135 borderSource: "radius_pressed.sci" // Deprecated, use a dedicated shape.
136 opacity: button.pressed ? 1.0 : 0.0
137 Behavior on opacity {
138 NumberAnimation {
139 duration: LomiriAnimation.SnapDuration
140 easing.type: Easing.Linear
141 }
142 }
143 visible: stroke || background.visible
144 }
145
146 ButtonForeground {
147 id: foreground
148 width: parent.width - 2*horizontalPadding
149 anchors {
150 centerIn: parent
151 horizontalCenterOffset: buttonFaceOffset
152 }
153 text: button.text
154 /* Pick either a clear or dark text color depending on the luminance of the
155 background color to maintain best contrast (works in most cases),
156 when strokeColor is not specified.
157 */
158 textColor: stroke
159 ? button.strokeColor
160 : ColorUtils.contrastRatio("#FFFFFF", button.color) >= 4.1 && !(stroke && !button.pressed)
161 ? "#FFFFFF"
162 : "#111111"
163 iconSource: button.iconSource
164 iconPosition: button.iconPosition
165 iconSize: units.gu(3)
166 font: button.font
167 spacing: horizontalPadding
168 transformOrigin: Item.Top
169 scale: button.pressed ? 0.98 : 1.0
170 Behavior on scale {
171 NumberAnimation {
172 duration: LomiriAnimation.SnapDuration
173 easing.type: Easing.Linear
174 }
175 }
176 }
177 }