C

Qt Quick Ultralite Thermostat Demo

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

import QtQuick 2.15
import QtQuick.Templates 2.15
import QtQuickUltralite.Extras 2.0
import Thermo 1.0

Switch {
    id: control
    implicitWidth: Math.max(background.implicitWidth + leftInset + rightInset,
                            contentItem.implicitWidth + leftPadding + rightPadding)
    implicitHeight: Math.max(background.implicitHeight + topInset + bottomInset,
                             contentItem.implicitHeight + topPadding + bottomPadding)
    leftInset: 14
    topInset: 14
    rightInset: 14
    bottomInset: 14

    background: ColorizedImage {
        color: control.checked ? ColorStyle.greenLight : ColorStyle.greyMedium3
        source: "switch-bg.png"

        Row {
            anchors.centerIn: parent
            spacing: 8

            Image {
                source: "switch-i.png"
            }
            Image {
                source: "switch-o.png"
            }
        }
    }

    // TODO: move this to indicator when AbstractButton gets it.
    // We don't use text with our switches, but moving it to indicator
    // will allow us to remove this intermediate Item, since we have
    // full control over indicator's geometry.
    contentItem: Item {
        Image {
            source: "switch-handle.png"
            // 9 is the space on each side of the handle in the image.
            x: Math.max(-9 + control.leftInset, Math.min((parent.width - width - control.rightInset) + 9,
                control.visualPosition * parent.width - (width / 2)))
            y: (Theme.isBig ? -3 : -4) + control.topInset

            Behavior on x {
                enabled: GlobalState.placesLoaded
                NumberAnimation { duration: 150 }
            }
        }
    }
}