diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2023-10-24 14:17:46 +0800 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2023-10-26 13:50:52 +0800 |
commit | 9187e07d36db8c262b51ba7d1ce35615b31ee918 (patch) | |
tree | cb826e00f33f3408e49d91dd70ddb42a80ea3547 | |
parent | a45eceefa787698e3042a0f913927cbf9ac69768 (diff) |
Windows: add Switch
Task-number: QTBUG-115165
Change-Id: Ib1de7e40282a86a194577de2a24291013934dd55
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r-- | src/quickcontrols/windows/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/quickcontrols/windows/Switch.qml | 82 | ||||
-rw-r--r-- | tests/auto/quickcontrols/customization/tst_customization.cpp | 3 |
3 files changed, 85 insertions, 1 deletions
diff --git a/src/quickcontrols/windows/CMakeLists.txt b/src/quickcontrols/windows/CMakeLists.txt index 992dee61ff..0f61213a70 100644 --- a/src/quickcontrols/windows/CMakeLists.txt +++ b/src/quickcontrols/windows/CMakeLists.txt @@ -18,6 +18,7 @@ set(qml_files "SelectionRectangle.qml" "Slider.qml" "SpinBox.qml" + "Switch.qml" "TextArea.qml" "TextField.qml" "ScrollBar.qml" diff --git a/src/quickcontrols/windows/Switch.qml b/src/quickcontrols/windows/Switch.qml new file mode 100644 index 0000000000..9f66dc7585 --- /dev/null +++ b/src/quickcontrols/windows/Switch.qml @@ -0,0 +1,82 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +import QtQuick +import QtQuick.Templates as T + +T.Switch { + id: control + + implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, + implicitContentWidth + leftPadding + rightPadding) + implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, + implicitContentHeight + topPadding + bottomPadding, + implicitIndicatorHeight + topPadding + bottomPadding) + + padding: 6 + spacing: 6 + + readonly property bool __notCustomizable: true + readonly property Item __focusFrameTarget: indicator + readonly property Item __focusFrameStyleItem: indicator + + indicator: Rectangle { + x: control.text ? (control.mirrored ? control.width - width - control.rightPadding : control.leftPadding) + : control.leftPadding + (control.availableWidth - width) / 2 + y: control.topPadding + (control.availableHeight - height) / 2 + implicitWidth: 40 + implicitHeight: 16 + radius: 3 + color: Qt.darker(control.palette.button, control.down ? 1.2 : 1.1) + border.color: Qt.darker(control.palette.window, 1.4) + + readonly property bool __ignoreNotCustomizable: true + readonly property real __focusFrameRadius: 2 + + // Checked indicator. + Rectangle { + x: control.mirrored ? parent.children[1].x : 0 + width: control.mirrored ? parent.width - parent.children[1].x : parent.children[1].x + parent.children[1].width + height: parent.height + radius: 3 + color: Qt.darker(control.palette.highlight, control.down ? 1.1 : 1) + border.color: Qt.darker(control.palette.highlight, 1.35) + border.width: control.enabled ? 1 : 0 + opacity: control.checked ? 1 : 0 + + Behavior on opacity { + enabled: !control.down + NumberAnimation { duration: 80 } + } + } + + // Handle. + Rectangle { + x: Math.max(0, Math.min(parent.width - width, control.visualPosition * parent.width - (width / 2))) + y: (parent.height - height) / 2 + width: 20 + height: 16 + radius: 3 + color: Qt.lighter(control.palette.button, control.down ? 1 : (control.hovered ? 1.07 : 1.045)) + border.width: 1 + border.color: Qt.darker(control.palette.window, 1.4) + + Behavior on x { + enabled: !control.down + SmoothedAnimation { velocity: 200 } + } + } + } + + contentItem: Text { + leftPadding: control.indicator && !control.mirrored ? control.indicator.width + control.spacing : 0 + rightPadding: control.indicator && control.mirrored ? control.indicator.width + control.spacing : 0 + text: control.text + font: control.font + color: control.palette.windowText + elide: Text.ElideRight + verticalAlignment: Text.AlignVCenter + + readonly property bool __ignoreNotCustomizable: true + } +} diff --git a/tests/auto/quickcontrols/customization/tst_customization.cpp b/tests/auto/quickcontrols/customization/tst_customization.cpp index 48c8affc97..af22d04fb5 100644 --- a/tests/auto/quickcontrols/customization/tst_customization.cpp +++ b/tests/auto/quickcontrols/customization/tst_customization.cpp @@ -375,7 +375,8 @@ void tst_customization::override_data() "Windows", { "Button", "CheckBox", "ComboBox", "Frame", "GroupBox", "ProgressBar", "RadioButton", - "RangeSlider", "SelectionRectangle", "ScrollBar", "Slider", "SpinBox", "TextArea", "TextField" + "RangeSlider", "SelectionRectangle", "ScrollBar", "Slider", "SpinBox", "Switch", + "TextArea", "TextField" } } }; |