summaryrefslogtreecommitdiff
path: root/raveos-sddm-theme/sddm-rave-theme/Components/KeyboardLayoutIndicator.qml
blob: 0d95c06db1e0cd77f72b07f53d9ac07f275e4641 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

import QtQuick 2.15
import QtQml 2.15
import QtQuick.Controls 2.15

Item {
    id: layoutIndicator

    implicitWidth: layoutRow.implicitWidth
    implicitHeight: layoutRow.implicitHeight
    width:  implicitWidth
    height: implicitHeight

    property int layoutsCount: (typeof keyboard !== "undefined" && keyboard && keyboard.layouts) ? keyboard.layouts.length : 0

    visible: (config.HideKeyboardLayout !== "false") ? false : (currentShortName !== "")

    property string currentShortName: ""

    function updateCurrentShortName() {
        if (layoutsCount > 0) {
            var idx = keyboard.currentLayout
            if (idx >= 0 && idx < layoutsCount) {
                var layoutObj = keyboard.layouts[idx]
                if (layoutObj && layoutObj.shortName) {
                    currentShortName = layoutObj.shortName.toUpperCase()
                    return
                }
            }
        }
        
        var loc = config.Locale || Qt.locale().name
        if (loc) {
            var parts = loc.split("_")
            if (parts.length > 0 && parts[0]) {
                currentShortName = parts[0].toUpperCase()
                return
            }
        }
        currentShortName = ""
    }

    Component.onCompleted: updateCurrentShortName()

    property color textColor: config.UserIconColor || "#ffffff"
    property color hoverColor: config.HoverUserIconColor || "#b7cef1"

    Row {
        id: layoutRow
        height: root.font.pointSize * 1.1
        spacing: root.font.pointSize * 0.4

        Canvas {
            id: kbdCanvas
            width:  root.font.pointSize * 1.5
            height: root.font.pointSize * 1.1
            anchors.verticalCenter: parent.verticalCenter

            onPaint: {
                var ctx = getContext("2d")
                ctx.clearRect(0, 0, width, height)
                
                var canHover = layoutsCount >= 1
                var col = (layoutMouseArea.containsMouse && canHover) ? layoutIndicator.hoverColor : layoutIndicator.textColor
                
                ctx.strokeStyle = col
                ctx.fillStyle = col
                ctx.lineWidth = height * 0.08
                ctx.lineCap = "round"
                ctx.lineJoin = "round"
                ctx.globalAlpha = 0.85

                var r = height * 0.15
                var bx = ctx.lineWidth / 2
                var by = ctx.lineWidth / 2
                var bw = width - ctx.lineWidth
                var bh = height - ctx.lineWidth

                ctx.beginPath()
                ctx.moveTo(bx + r, by)
                ctx.arcTo(bx + bw, by,     bx + bw, by + bh, r)
                ctx.arcTo(bx + bw, by + bh, bx,     by + bh, r)
                ctx.arcTo(bx,     by + bh, bx,     by,     r)
                ctx.arcTo(bx,     by,     bx + bw, by,     r)
                ctx.closePath()
                ctx.stroke()

                ctx.lineWidth = height * 0.06
                var kw = bw / 6
                var kh = bh / 4
                for (var i = 0; i < 4; i++) {
                    ctx.fillRect(bx + kw * (i + 1), by + kh, kw * 0.6, kh * 0.6)
                    ctx.fillRect(bx + kw * (i + 0.5), by + kh * 2, kw * 0.6, kh * 0.6)
                }
                ctx.fillRect(bx + kw * 1.5, by + kh * 3, kw * 3, kh * 0.6)

                ctx.globalAlpha = 1.0
            }

            Connections {
                target: layoutMouseArea
                function onContainsMouseChanged() { kbdCanvas.requestPaint() }
            }
        }

        Text {
            id: layoutText
            anchors.verticalCenter: parent.verticalCenter
            text: layoutIndicator.currentShortName
            
            color: {
                var canHover = layoutsCount >= 1
                return (layoutMouseArea.containsMouse && canHover) ? layoutIndicator.hoverColor : layoutIndicator.textColor
            }
            
            font.pointSize: root.font.pointSize * 0.82
            font.family:      root.font.family
            font.bold:        true
            style: Text.Raised
            styleColor: Qt.rgba(0, 0, 0, 0.40)
        }
    }

    MouseArea {
        id: layoutMouseArea
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: (layoutsCount >= 1) ? Qt.PointingHandCursor : Qt.ArrowCursor
        
        onClicked: {
            if (layoutsCount >= 1) {
                layoutPopup.open()
            }
        }
    }

    Popup {
        id: layoutPopup
        y: parent.height + root.font.pointSize * 0.4
        x: -width / 2 + parent.width / 2
        width: Math.max(root.font.pointSize * 12, layoutRow.width * 1.5)
        padding: root.font.pointSize * 0.4
        
        background: Rectangle {
            radius: config.RoundCorners !== "" ? parseInt(config.RoundCorners) / 2 : root.font.pointSize * 0.8
            color: config.BackgroundColor || "#1a1a1a"
            border.color: Qt.rgba(1, 1, 1, 0.20)
            border.width: root.font.pointSize * 0.08
        }
        
        contentItem: ListView {
            id: layoutListView
            implicitHeight: Math.min(root.font.pointSize * 15, contentHeight)
            clip: true
            model: layoutsCount
            ScrollIndicator.vertical: ScrollIndicator { }
            
            delegate: Rectangle {
                id: delegateItem
                width: parent.width
                height: root.font.pointSize * 2
                color: delegateMouse.containsMouse ? Qt.rgba(1, 1, 1, 0.15) : "transparent"
                radius: config.RoundCorners !== "" ? parseInt(config.RoundCorners) / 4 : root.font.pointSize * 0.4
                
                property var layoutObj: (typeof keyboard !== "undefined" && keyboard && keyboard.layouts) ? keyboard.layouts[index] : null
                property string shortName: layoutObj && layoutObj.shortName ? layoutObj.shortName.toUpperCase() : ""
                property string longName: layoutObj && layoutObj.longName ? layoutObj.longName : ""
                
                Row {
                    spacing: root.font.pointSize * 0.6
                    anchors.fill: parent
                    anchors.leftMargin: root.font.pointSize * 0.8
                    anchors.rightMargin: root.font.pointSize * 0.8
                    
                    Text {
                        text: delegateItem.shortName
                        color: index === keyboard.currentLayout ? (config.HoverUserIconColor || "#b7cef1") : (config.UserIconColor || "#ffffff")
                        font.pointSize: root.font.pointSize * 0.8
                        font.family: root.font.family
                        font.bold: true
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    
                    Text {
                        text: delegateItem.longName
                        color: index === keyboard.currentLayout ? (config.HoverUserIconColor || "#b7cef1") : (config.UserIconColor || "#ffffff")
                        font.pointSize: root.font.pointSize * 0.8
                        font.family: root.font.family
                        elide: Text.ElideRight
                        width: parent.width - x - root.font.pointSize * 0.8
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
                
                MouseArea {
                    id: delegateMouse
                    anchors.fill: parent
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    onClicked: {
                        keyboard.currentLayout = index
                        layoutPopup.close()
                    }
                }
            }
        }
        
        enter: Transition {
            NumberAnimation { property: "opacity"; from: 0; to: 1; duration: 150 }
        }
        exit: Transition {
            NumberAnimation { property: "opacity"; from: 1; to: 0; duration: 150 }
        }
    }

    Connections {
        target: keyboard
        ignoreUnknownSignals: true
        function onCurrentLayoutChanged() {
            kbdCanvas.requestPaint()
            updateCurrentShortName()
        }
    }
}