summaryrefslogtreecommitdiff
path: root/src/extras/Private/CircularButtonStyleHelper.qml
blob: 1b69363f3411b7b0e47183e35d8f2ad8b5fc2718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Extras module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

import QtQuick 2.2
import QtQuick.Extras 1.4
import QtQuick.Extras.Private 1.0

QtObject {
    id: circularButtonStyleHelper

    property Item control

    property color buttonColorUpTop: "#e3e3e3"
    property color buttonColorUpBottom: "#b3b3b3"
    property color buttonColorDownTop: "#d3d3d3"
    property color buttonColorDownBottom: "#939393"
    property color outerArcColorTop: "#9c9c9c"
    property color outerArcColorBottom: Qt.rgba(0.941, 0.941, 0.941, 0.29)
    property color innerArcColorTop: "#e3e3e3"
    property color innerArcColorBottom: "#acacac"
    property real innerArcColorBottomStop: 0.4
    property color shineColor: Qt.rgba(1, 1, 1, 0.29)
    property real smallestAxis: control ? Math.min(control.width, control.height) : 0
    property real outerArcLineWidth: smallestAxis * 0.04
    property real innerArcLineWidth: Math.max(1, outerArcLineWidth * 0.1)
    property real shineArcLineWidth: Math.max(1, outerArcLineWidth * 0.1)
    property real implicitWidth: Math.round(TextSingleton.implicitHeight * 8)
    property real implicitHeight: Math.round(TextSingleton.implicitHeight * 8)

    property color textColorUp: "#4e4e4e"
    property color textColorDown: "#303030"
    property color textRaisedColorUp: "#ffffff"
    property color textRaisedColorDown: "#e3e3e3"

    property real radius: (smallestAxis * 0.5) - outerArcLineWidth - innerArcLineWidth
    property real halfRadius: radius / 2
    property real outerArcRadius: innerArcRadius + outerArcLineWidth / 2
    property real innerArcRadius: radius + innerArcLineWidth / 2
    property real shineArcRadius: outerArcRadius + outerArcLineWidth / 2 - shineArcLineWidth / 2
    property real zeroAngle: Math.PI * 0.5

    property color buttonColorTop: control && control.pressed ? buttonColorDownTop : buttonColorUpTop
    property color buttonColorBottom: control && control.pressed ? buttonColorDownBottom : buttonColorUpBottom

    function toPixels(percentageOfSmallestAxis) {
        return percentageOfSmallestAxis * smallestAxis;
    }

    function paintBackground(ctx) {
        ctx.reset();

        if (outerArcRadius < 0 || radius < 0)
            return;

        var xCenter = ctx.canvas.width / 2;
        var yCenter = ctx.canvas.height / 2;

        /* Draw outer arc */
        ctx.beginPath();
        ctx.lineWidth = outerArcLineWidth;
        ctx.arc(xCenter, yCenter, outerArcRadius, 0, Math.PI * 2, false);
        var gradient = ctx.createRadialGradient(xCenter, yCenter - halfRadius,
            0, xCenter, yCenter - halfRadius, radius * 1.5);
        gradient.addColorStop(0, outerArcColorTop);
        gradient.addColorStop(1, outerArcColorBottom);
        ctx.strokeStyle = gradient;
        ctx.stroke();

        /* Draw the shine along the bottom */
        ctx.beginPath();
        ctx.lineWidth = shineArcLineWidth;
        ctx.arc(xCenter, yCenter, shineArcRadius, 0, Math.PI, false);
        gradient = ctx.createLinearGradient(xCenter, yCenter + radius, xCenter, yCenter);
        gradient.addColorStop(0, shineColor);
        gradient.addColorStop(0.5, "rgba(255, 255, 255, 0)");
        ctx.strokeStyle = gradient;
        ctx.stroke();

        /* Draw inner arc */
        ctx.beginPath();
        ctx.lineWidth = innerArcLineWidth + 1;
        ctx.arc(xCenter, yCenter, innerArcRadius, 0, Math.PI * 2, false);
        gradient = ctx.createLinearGradient(xCenter, yCenter - halfRadius,
            xCenter, yCenter + halfRadius);
        gradient.addColorStop(0, innerArcColorTop);
        gradient.addColorStop(innerArcColorBottomStop, innerArcColorBottom);
        ctx.strokeStyle = gradient;
        ctx.stroke();

        /* Draw the button's body */
        ctx.beginPath();
        ctx.ellipse(xCenter - radius, yCenter - radius, radius * 2, radius * 2);
        gradient = ctx.createRadialGradient(xCenter, yCenter + radius * 0.85, 0,
            xCenter, yCenter + radius * 0.85, radius * (0.85 * 2));
        gradient.addColorStop(1, buttonColorTop);
        gradient.addColorStop(0, buttonColorBottom);
        ctx.fillStyle = gradient;
        ctx.fill();
    }
}