|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Controls |
| 3 | +import QtQuick.Controls.Basic |
| 4 | + |
| 5 | + |
| 6 | +Item { |
| 7 | + |
| 8 | + enum Size { |
| 9 | + Small, |
| 10 | + Middle, |
| 11 | + Large |
| 12 | + } |
| 13 | + |
| 14 | + enum Filler { |
| 15 | + None, |
| 16 | + Withchar |
| 17 | + } |
| 18 | + |
| 19 | + property string text: '' |
| 20 | + property string icon: '' |
| 21 | + property bool disabled: false |
| 22 | + property bool checked: false |
| 23 | + property int size: ESwitch.Size.Middle |
| 24 | + property int filler: ESwitch.Filler.None |
| 25 | + |
| 26 | + signal clicked() |
| 27 | + |
| 28 | + id: root |
| 29 | + implicitWidth: switchWidget.implicitWidth |
| 30 | + implicitHeight: switchWidget.implicitHeight |
| 31 | + |
| 32 | + Switch { |
| 33 | + id: switchWidget |
| 34 | + enabled: !root.disabled |
| 35 | + checked: root.checked |
| 36 | + onClicked: root.clicked() |
| 37 | + width: root.width |
| 38 | + indicator: Rectangle { |
| 39 | + function getBackgroundColor(checked, disabled) { |
| 40 | + if (disabled) { |
| 41 | + return checked ? ETheme.disabledCheckedColor : ETheme.disabledColor; |
| 42 | + } |
| 43 | + return checked ? ETheme.primaryColor : ETheme.secondaryBgColor; |
| 44 | + } |
| 45 | + |
| 46 | + implicitWidth: 40 + 2 * (root.size - 1) |
| 47 | + implicitHeight: 24 + 2 * (root.size - 1) |
| 48 | + x: switchWidget.leftPadding |
| 49 | + anchors.verticalCenter: parent.verticalCenter |
| 50 | + radius: width / 2 |
| 51 | + color: getBackgroundColor(switchWidget.checked, root.disabled) |
| 52 | + |
| 53 | + Rectangle { |
| 54 | + function getX(check, down) { |
| 55 | + if (check && down) |
| 56 | + return parent.width - 2 * radius - 8; |
| 57 | + else |
| 58 | + return check ? parent.width - width - 2 : 2 |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + id: handle |
| 63 | + x: getX(switchWidget.checked, switchWidget.down) |
| 64 | + anchors.verticalCenter: parent.verticalCenter |
| 65 | + radius: switchWidget.checked ? 9 + root.size : 7 + root.size |
| 66 | + height: 2 * radius |
| 67 | + width: switchWidget.down ? 2 * radius + 6 : 2 * radius |
| 68 | + color: ETheme.fontColor; |
| 69 | + |
| 70 | + Behavior on width { |
| 71 | + NumberAnimation { |
| 72 | + duration: 100 |
| 73 | + } |
| 74 | + } |
| 75 | + Behavior on x { |
| 76 | + NumberAnimation { |
| 77 | + duration: 100 |
| 78 | + easing.type: easing.InOutCubic |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Text { |
| 84 | + function getChar(filler, checked, down) { |
| 85 | + if (filler == ESwitch.Filler.None || down) { |
| 86 | + return ''; |
| 87 | + } else { |
| 88 | + return checked ? "开" : "关"; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + id: switchText |
| 93 | + font.pixelSize: ETheme.contentFontSize |
| 94 | + font.family: ETheme.fontFamily |
| 95 | + color: ETheme.fontColor |
| 96 | + x: switchWidget.checked ? (parent.width - handle.radius * 2 - font.pixelSize - 2) / 2 : (parent.width + handle.radius * 2 - font.pixelSize + 2) / 2; |
| 97 | + anchors.verticalCenter: parent.verticalCenter |
| 98 | + text: getChar(root.filler, switchWidget.checked, switchWidget.down) |
| 99 | + Behavior on x { |
| 100 | + NumberAnimation { |
| 101 | + duration: 100 |
| 102 | + easing.type: easing.OutExpo |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + contentItem: Text { |
| 110 | + text: root.text |
| 111 | + font.pixelSize: ETheme.contentFontSize |
| 112 | + font.family: ETheme.fontFamily |
| 113 | + color: ETheme.fontColor |
| 114 | + verticalAlignment: Text.AlignVCenter |
| 115 | + leftPadding: switchWidget.indicator.width + 5 |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments