1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-25 14:02:53 -05:00
Files
DankMaterialShell/Widgets/DankToggle.qml
2025-07-25 18:19:22 -04:00

139 lines
3.7 KiB
QML

import QtQuick
import qs.Common
import qs.Widgets
Item {
id: toggle
property bool checked: false
property bool enabled: true
property bool toggling: false
property string text: ""
property string description: ""
signal clicked()
signal toggled(bool checked)
width: text ? parent.width : 48
height: text ? 60 : 24
StyledRect {
id: background
anchors.fill: parent
radius: toggle.text ? Theme.cornerRadius : 0
color: toggle.text ? Theme.surfaceHover : "transparent"
visible: toggle.text
StateLayer {
visible: toggle.text
disabled: !toggle.enabled
stateColor: Theme.primary
cornerRadius: parent.radius
onClicked: {
if (toggle.enabled) {
toggle.checked = !toggle.checked;
toggle.clicked();
toggle.toggled(toggle.checked);
}
}
}
}
Row {
id: textRow
anchors.left: parent.left
anchors.right: toggleTrack.left
anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: Theme.spacingM
anchors.rightMargin: Theme.spacingM
spacing: Theme.spacingXS
visible: toggle.text
Column {
anchors.verticalCenter: parent.verticalCenter
spacing: Theme.spacingXS
StyledText {
text: toggle.text
font.pixelSize: Appearance.fontSize.normal
font.weight: Font.Medium
opacity: toggle.enabled ? 1 : 0.4
}
StyledText {
text: toggle.description
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceVariantText
wrapMode: Text.WordWrap
width: Math.min(implicitWidth, toggle.width - 120)
visible: toggle.description.length > 0
}
}
}
StyledRect {
id: toggleTrack
width: toggle.text ? 48 : parent.width
height: toggle.text ? 24 : parent.height
anchors.right: parent.right
anchors.rightMargin: toggle.text ? Theme.spacingM : 0
anchors.verticalCenter: parent.verticalCenter
radius: height / 2
color: (toggle.checked && toggle.enabled) ? Theme.primary : Theme.surfaceVariantAlpha
opacity: toggle.toggling ? 0.6 : (toggle.enabled ? 1 : 0.4)
StyledRect {
id: toggleHandle
width: 20
height: 20
radius: 10
color: Theme.surface
anchors.verticalCenter: parent.verticalCenter
x: (toggle.checked && toggle.enabled) ? parent.width - width - 2 : 2
StyledRect {
anchors.centerIn: parent
width: parent.width + 2
height: parent.height + 2
radius: (parent.width + 2) / 2
color: "transparent"
border.color: Qt.rgba(0, 0, 0, 0.1)
border.width: 1
z: -1
}
Behavior on x {
NumberAnimation {
duration: Appearance.anim.durations.normal
easing.type: Easing.BezierSpline
easing.bezierCurve: Appearance.anim.curves.emphasizedDecel
}
}
}
StateLayer {
disabled: !toggle.enabled
stateColor: Theme.primary
cornerRadius: parent.radius
onClicked: {
if (toggle.enabled) {
toggle.checked = !toggle.checked;
toggle.clicked();
toggle.toggled(toggle.checked);
}
}
}
}
}