Skip to content

Commit

Permalink
Merge pull request #98 from kounkou/clean-up
Browse files Browse the repository at this point in the history
(chore) cleanup and factorize Switch component
  • Loading branch information
kounkou authored Nov 5, 2024
2 parents 4615044 + 6dad9dd commit 48aca46
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 141 deletions.
2 changes: 1 addition & 1 deletion qml/PracticePage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ Rectangle {

contentItem: Text {
text: languageComboBox.currentText
font: languageComboBox.font
font.bold: sessionObject.isFontBold
elide: Text.ElideRight
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
Expand Down
85 changes: 6 additions & 79 deletions qml/SettingsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -162,102 +162,29 @@ Rectangle {
Component {
id: appaeranceSwitchComponent

SwitchDelegate {
ToggleSwitch {
id: control

checked: itemName === "Automatic theme" ? sessionObject.automaticThemeSetting : sessionObject.fontSetting

onToggled: {
switchChecked: itemName === "Automatic theme" ? sessionObject.automaticThemeSetting : sessionObject.fontSetting
onCheckedChangedExternally: {
if (itemName === "Automatic theme") {
sessionObject.automaticThemeSetting = checked;
} else {
sessionObject.fontSetting = checked;
}
sessionObject.saveSession();
}

indicator: Rectangle {
implicitWidth: 50
implicitHeight: 26
x: control.width - width - control.rightPadding + 3
y: parent.height / 2 - height / 2
radius: 13
color: control.checked ? themeObject.buttonEasyColor : "#cccccc"
border.color: control.checked ? themeObject.buttonEasyColor : "#cccccc"

Rectangle {
x: control.checked ? parent.width - width - 3 : 3
width: 22
height: 22
radius: 11
anchors.verticalCenter: parent.verticalCenter
color: control.down ? "#cccccc" : "#ffffff"
border.color: control.checked ? (control.down ? themeObject.buttonEasyColor : "#21be2b") : "#999999"

Behavior on x {
NumberAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
}
}
}

background: Rectangle {
implicitWidth: 100
implicitHeight: 40
visible: false
color: control.down ? "#bdbebf" : "#eeeeee"
}
}
}

Component {
id: boldTextSwitchComponent

SwitchDelegate {
ToggleSwitch {
id: control

checked: sessionObject.isFontBold

onToggled: {
switchChecked: sessionObject.isFontBold
onCheckedChangedExternally: {
sessionObject.isFontBold = checked
sessionObject.saveSession();
}

indicator: Rectangle {
implicitWidth: 50
implicitHeight: 26
x: control.width - width - control.rightPadding + 3
y: parent.height / 2 - height / 2
radius: 13
color: control.checked ? themeObject.buttonEasyColor : "#cccccc"
border.color: control.checked ? themeObject.buttonEasyColor : "#cccccc"
opacity: enabled ? 1.0 : 0.5

Rectangle {
x: control.checked ? parent.width - width - 3 : 3
width: 22
height: 22
radius: 11
anchors.verticalCenter: parent.verticalCenter
color: control.down ? "#cccccc" : "#ffffff"
border.color: control.checked ? (control.down ? themeObject.buttonEasyColor : "#21be2b") : "#999999"

Behavior on x {
NumberAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
}
}
}

background: Rectangle {
implicitWidth: 100
implicitHeight: 40
visible: false
color: control.down ? "#bdbebf" : "#eeeeee"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions qml/Theme.qml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ QtObject {
property color codeTextColor: theme === "light" ? "#333333" : "#E0E0E0" // Dark grey for light theme, soft grey for dark theme

property color averageLineColor: theme === "light" ? "#2E7D32" : "#66BB6A"

property color buttonDownColor: theme === "light" ? "#CCCCCC" : "#CCCCCC"
property color buttonUpColor: theme === "light" ? "#FFFFFF" : "#FFFFFF"

}
64 changes: 64 additions & 0 deletions qml/ToggleSwitch.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import QtQuick 2.15
import QtQuick.Controls 2.15

SwitchDelegate {
id: root

property alias switchText: root.text
property alias switchChecked: root.checked
property alias switchEnabled: root.enabled

signal checkedChangedExternally(bool checked)

font.bold: sessionObject.isFontBold
anchors.horizontalCenter: parent.horizontalCenter

contentItem: Text {
rightPadding: root.indicator.width + root.spacing
text: root.text
font: root.font
opacity: root.enabled ? 1.0 : 0.5
color: themeObject.textColor
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}

onCheckedChanged: {
checkedChangedExternally(root.checked)
sessionObject.saveSession()
}

indicator: Rectangle {
implicitWidth: 50
implicitHeight: 26
x: root.width - width - root.rightPadding + 3
y: parent.height / 2 - height / 2
radius: 13
color: root.checked ? themeObject.buttonEasyColor : themeObject.buttonDownColor
border.color: root.checked ? themeObject.buttonEasyColor : themeObject.buttonDownColor
opacity: root.enabled ? 1.0 : 0.5

Rectangle {
x: root.checked ? parent.width - width - 3 : 3
width: 22
height: 22
radius: 11
anchors.verticalCenter: parent.verticalCenter
color: root.down ? themeObject.buttonDownColor : themeObject.buttonUpColor
border.color: root.checked ? (root.down ? themeObject.buttonEasyColor : "#21be2b") : "#999999"

Behavior on x {
NumberAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
}
}
}

background: Rectangle {
implicitWidth: 100
implicitHeight: 40
visible: false
}
}
67 changes: 6 additions & 61 deletions qml/WelcomePage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Rectangle {

Text {
text: "What would you like to do?"
font.bold: sessionObject.isFontBold
font.pixelSize: 14
color: themeObject.textColor
anchors.horizontalCenter: parent.horizontalCenter
Expand All @@ -58,13 +59,6 @@ Rectangle {
page: practicePage
}

// CustomButton {
// id: reviewButton

// buttonText: "Review algorithms"
// page: reviewPage
// }

CustomButton {
id: stats
anchors.horizontalCenter: parent.horizontalCenter
Expand All @@ -90,63 +84,14 @@ Rectangle {
page: aboutPage
}

SwitchDelegate {
ToggleSwitch {
id: themeToggle

font.bold: sessionObject.isFontBold
text: themeObject.theme === "light" ? "Light Theme" : "Dark Theme"
checked: themeObject.theme === "dark"
anchors.horizontalCenter: parent.horizontalCenter
enabled: !sessionObject.automaticThemeSetting

contentItem: Text {
rightPadding: themeToggle.indicator.width + themeToggle.spacing
text: themeToggle.text
font: themeToggle.font
opacity: enabled ? 1.0 : 0.5
color: themeObject.textColor
elide: Text.ElideRight
verticalAlignment: Text.AlignVCenter
}

onCheckedChanged: {
switchText: themeObject.theme === "light" ? "Light Theme" : "Dark Theme"
switchChecked: themeObject.theme === "dark"
switchEnabled: !sessionObject.automaticThemeSetting
onCheckedChangedExternally: {
themeObject.theme = checked ? "dark" : "light"
sessionObject.saveSession()
}

indicator: Rectangle {
implicitWidth: 50
implicitHeight: 26
x: themeToggle.width - width - themeToggle.rightPadding + 3
y: parent.height / 2 - height / 2
radius: 13
color: themeToggle.checked ? themeObject.buttonEasyColor : "#cccccc"
border.color: themeToggle.checked ? themeObject.buttonEasyColor : "#cccccc"
opacity: enabled ? 1.0 : 0.5

Rectangle {
x: themeToggle.checked ? parent.width - width - 3 : 3
width: 22
height: 22
radius: 11
anchors.verticalCenter: parent.verticalCenter
color: themeToggle.down ? "#cccccc" : "#ffffff"
border.color: themeToggle.checked ? (themeToggle.down ? themeObject.buttonEasyColor : "#21be2b") : "#999999"

Behavior on x {
NumberAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
}
}
}

background: Rectangle {
implicitWidth: 100
implicitHeight: 40
visible: false
color: themeToggle.down ? "#bdbebf" : "#eeeeee"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<file alias="Session.qml">qml/Session.qml</file>
<file alias="LoginPage.qml">qml/LoginPage.qml</file>
<file alias="SettingsPage.qml">qml/SettingsPage.qml</file>
<file alias="ToggleSwitch.qml">qml/ToggleSwitch.qml</file>
<file alias="handler.js">logic/handler.js</file>

<file alias="adsense-ad.html">qml/adsense-ad.html</file>
Expand Down

0 comments on commit 48aca46

Please sign in to comment.