Skip to content

Commit 2368bff

Browse files
Merge pull request #387 from Reve4Mevol/master
Feat: Add ESwitch
2 parents 65352a9 + dd7bf37 commit 2368bff

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

Editor/Qml/ESwitch.qml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

Editor/Qml/ETheme.qml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ QtObject {
1313
property color disabledColor: Qt.color('#676563')
1414
property color fontColor: Qt.color('#ecf0f1')
1515
property color linkFontColor: Qt.color('#91b9c4')
16+
property color secondaryBgColor: Qt.color('#8e8e8e')
17+
property color disabledCheckedColor: Qt.color('#c9a9a6')
1618

1719
property FontLoader normalFont: FontLoader { source: Qt.url('Resource/Font/MiSans-Normal.ttf') }
1820
property FontLoader boldFont: FontLoader { source: Qt.url('Resource/Font/MiSans-Bold.ttf') }

Editor/Qml/EWidgetSamples.qml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,45 @@ Rectangle {
272272
EIcon { name: 'folder' }
273273
EIcon { name: 'folder-import' }
274274
}
275+
276+
RowLayout {
277+
Layout.leftMargin: 5
278+
Layout.topMargin: 15
279+
EText {
280+
text: 'Switches'
281+
style: EText.Style.Title1
282+
}
283+
}
284+
RowLayout {
285+
ESwitch {
286+
text: 'Small Switch'
287+
size: ESwitch.Size.Small
288+
}
289+
ESwitch {
290+
text: 'Middle Switch'
291+
size: ESwitch.Size.Middle
292+
}
293+
ESwitch {
294+
text: 'Large Switch'
295+
size: ESwitch.Size.Large
296+
}
297+
}
298+
RowLayout {
299+
ESwitch {
300+
text: 'Disabled Checked'
301+
disabled: true
302+
checked: true
303+
}
304+
ESwitch {
305+
text: 'Disabled Unchecked'
306+
disabled: true
307+
checked: false
308+
}
309+
ESwitch {
310+
text: 'Filled Symbol'
311+
filler: ESwitch.Filler.Withchar
312+
}
313+
}
275314
}
276315
}
277316
}

0 commit comments

Comments
 (0)