aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/Switch.qml
blob: f14c9862d461e97877846583aba8d2ba938efc1b (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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import QtQuick

// A themed toggle. The stock QtQuick.Controls Switch follows no style the
// shell defines, and every other control here is hand-rolled, so this is too.
// It trades the Controls keyboard handling for a consistent look; Button.qml
// already makes the same trade.
Rectangle {
    id: sw

    property bool checked: false
    property bool enabled: true

    signal toggled

    implicitWidth: 42
    implicitHeight: 24
    radius: height / 2
    opacity: sw.enabled ? 1 : 0.45
    color: sw.checked ? Qt.alpha(Theme.accent, 0.85) : Qt.alpha(Theme.surface, 0.9)
    border.width: 1
    border.color: Qt.alpha(Theme.text, 0.15)

    Rectangle {
        x: sw.checked ? sw.width - width - 3 : 3
        anchors.verticalCenter: parent.verticalCenter
        width: 18; height: 18; radius: 9
        color: Theme.text
        Behavior on x { NumberAnimation { duration: 120 } }
    }

    MouseArea {
        anchors.fill: parent
        cursorShape: sw.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
        onClicked: if (sw.enabled) { sw.checked = !sw.checked; sw.toggled(); }
    }
}