Unity 8
FadingLabel.qml
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 import QtGraphicalEffects 1.0
19 import Ubuntu.Components 1.3
20 
21 Label {
22  property real fadeDistance: units.gu(8)
23 
24  layer.enabled: contentWidth > width
25  layer.effect: ShaderEffect {
26  property real fadeThreshold: Math.min(1, Math.max(0, 1 - fadeDistance / width))
27 
28  fragmentShader: "
29  varying highp vec2 qt_TexCoord0;
30  uniform lowp float qt_Opacity;
31  uniform sampler2D source;
32  uniform lowp float fadeThreshold;
33  void main(void)
34  {
35  highp vec4 sourceColor = texture2D(source, qt_TexCoord0);
36  lowp float alpha = 1.0;
37  if (qt_TexCoord0.x > fadeThreshold && fadeThreshold < 1.0)
38  alpha = (1.0 - qt_TexCoord0.x) / (1.0 - fadeThreshold);
39  gl_FragColor = sourceColor * alpha * qt_Opacity;
40  }"
41  }
42 }