Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.3/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 1.3
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/stroke_button.png") : ""
96 visible: false
97 cache: false
98 asynchronous: true
99 }
100
101 ShaderEffect {
102 id: colorizedImage
103
104 anchors.fill: parent
105 visible: stroke && strokeBorder.status == Image.Ready
106
107 property Item source: visible ? strokeBorder : null
108 property color keyColorOut: stroke ? button.strokeColor : Qt.rgba(0.0, 0.0, 0.0, 0.0)
109 property color keyColorIn: Qt.rgba(1.0, 1.0, 1.0, 1.0)
110 property real threshold: 1.0
111
112 fragmentShader: "
113 varying highp vec2 qt_TexCoord0;
114 uniform sampler2D source;
115 uniform highp vec4 keyColorOut;
116 uniform highp vec4 keyColorIn;
117 uniform lowp float threshold;
118 uniform lowp float qt_Opacity;
119 void main() {
120 lowp vec4 sourceColor = texture2D(source, qt_TexCoord0);
121 gl_FragColor = mix(vec4(keyColorOut.rgb, 1.0) * sourceColor.a, sourceColor, step(threshold, distance(sourceColor.rgb / sourceColor.a, keyColorIn.rgb))) * qt_Opacity;
122 }"
123 }
124
125 LomiriShape {
126 id: background
127 anchors.fill: parent
128 borderSource: "radius_idle.sci" // Deprecated, use a dedicated shape.
129 visible: stroke ? false : ((backgroundColor.a != 0.0) || backgroundSource)
130 source: backgroundSource
131
132 backgroundColor: backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.topColor) : __colorHack(button.color))
133 secondaryBackgroundColor: backgroundSource ? "#00000000" : (isGradient ? __colorHack(gradientProxy.bottomColor) : __colorHack(button.color))
134 backgroundMode: isGradient ? LomiriShape.VerticalGradient : LomiriShape.SolidColor
135 opacity: styledItem.enabled ? 1.0 : 0.6
136 }
137
138 LomiriShape {
139 id: backgroundPressed
140 anchors.fill: parent
141 backgroundColor: stroke ? button.strokeColor : background.backgroundColor
142 secondaryBackgroundColor: background.secondaryBackgroundColor
143 backgroundMode: stroke ? LomiriShape.SolidColor : LomiriShape.VerticalGradient
144 borderSource: "radius_pressed.sci" // Deprecated, use a dedicated shape.
145 opacity: button.pressed ? 1.0 : 0.0
146 Behavior on opacity {
147 NumberAnimation {
148 duration: LomiriAnimation.SnapDuration
149 easing.type: Easing.Linear
150 }
151 }
152 visible: stroke || background.visible
153 }
154
155 ButtonForeground {
156 id: foreground
157 width: parent.width - 2*horizontalPadding
158 anchors {
159 centerIn: parent
160 horizontalCenterOffset: buttonFaceOffset
161 }
162 text: button.text
163 /* Pick either a clear or dark text color depending on the luminance of the
164 background color to maintain best contrast (works in most cases),
165 when strokeColor is not specified.
166 */
167 textColor: stroke
168 ? button.strokeColor
169 : ColorUtils.contrastRatio("#FFFFFF", button.color) >= 4.1 && !(stroke && !button.pressed)
170 ? "#FFFFFF"
171 : "#111111"
172 iconSource: button.iconSource
173 iconPosition: button.iconPosition
174 iconSize: units.gu(3)
175 font: button.font
176 spacing: horizontalPadding
177 transformOrigin: Item.Top
178 scale: button.pressed ? 0.98 : 1.0
179 Behavior on scale {
180 NumberAnimation {
181 duration: LomiriAnimation.SnapDuration
182 easing.type: Easing.Linear
183 }
184 }
185 }
186 }