diff options
author | Gabriel de Dietrich <gabriel.dedietrich@digia.com> | 2014-08-07 13:28:03 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@digia.com> | 2014-08-08 06:07:34 +0200 |
commit | b0bdc570476a3d3e35bd94eed3691f39a1292765 (patch) | |
tree | 6722cd525a17ad11fd2e02fc44e296760e49b5d3 /src/controls/Styles/Android | |
parent | ba0febc4319f172f072f04bf37f71432c70b70ca (diff) | |
download | qtquickcontrols-b0bdc570476a3d3e35bd94eed3691f39a1292765.tar.gz |
SwitchStyle: Make it work with pre-Holo Android
The Switch control was introduced in Android 4.0 together with
the Holo UI. Therefore, there's no way we can extract assets
on devices running earlier versions. In that case, we fall back
to the Base style Switch visuals.
Change-Id: I48d8a5cd94999cd5687fab62c8d16beb4fc53b5c
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Diffstat (limited to 'src/controls/Styles/Android')
-rw-r--r-- | src/controls/Styles/Android/SwitchStyle.qml | 129 |
1 files changed, 68 insertions, 61 deletions
diff --git a/src/controls/Styles/Android/SwitchStyle.qml b/src/controls/Styles/Android/SwitchStyle.qml index 60d8f2ef..13bbd356 100644 --- a/src/controls/Styles/Android/SwitchStyle.qml +++ b/src/controls/Styles/Android/SwitchStyle.qml @@ -41,88 +41,95 @@ import QtQuick 2.4 import QtQuick.Window 2.2 import QtQuick.Controls 1.2 import QtQuick.Controls.Private 1.0 +import QtQuick.Controls.Styles 1.2 import QtQuick.Controls.Styles.Android 1.0 import "drawables" -Style { +SwitchStyle { property Switch control: __control + readonly property bool hasHoloSwitch: !!AndroidStyle.styleDef.switchStyle - property Component panel: Item { - id: panel + Component.onCompleted: if (hasHoloSwitch) panel = holoPanel - // TODO: API level < 14 - readonly property var styleDef: AndroidStyle.styleDef.switchStyle - - implicitWidth: Math.max(styleDef.Switch_switchMinWidth, Math.max(track.implicitWidth, 2 * thumb.implicitWidth)) - implicitHeight: Math.max(track.implicitHeight, thumb.implicitHeight) - - property real min: track.padding.left - property real max: track.width - thumb.width - track.padding.left - property var __handle: thumb - - DrawableLoader { - id: track - anchors.fill: parent - pressed: control.pressed - checked: control.checked - focused: control.activeFocus - window_focused: control.window && control.window.active - styleDef: panel.styleDef.Switch_track - } + Component { + id: holoPanel Item { - id: thumb - - x: control.checked ? max : min - - FontMetrics { - id: metrics - font: label.font - } + id: panel - readonly property real maxTextWidth: Math.max(metrics.boundingRect(panel.styleDef.Switch_textOn).width, - metrics.boundingRect(panel.styleDef.Switch_textOff).width) + readonly property var styleDef: AndroidStyle.styleDef.switchStyle - implicitWidth: Math.max(loader.implicitWidth, maxTextWidth + 2 * panel.styleDef.Switch_thumbTextPadding) - implicitHeight: Math.max(loader.implicitHeight, metrics.height) + implicitWidth: Math.max(styleDef.Switch_switchMinWidth, Math.max(track.implicitWidth, 2 * thumb.implicitWidth)) + implicitHeight: Math.max(track.implicitHeight, thumb.implicitHeight) - anchors.top: parent.top - anchors.bottom: parent.bottom - - Behavior on x { - id: behavior - enabled: thumb.status === Loader.Ready - NumberAnimation { - duration: 150 - easing.type: Easing.OutCubic - } - } + property real min: track.padding.left + property real max: track.width - thumb.width - track.padding.left + property var __handle: thumb DrawableLoader { - id: loader - anchors.left: parent.left - anchors.right: parent.right - anchors.leftMargin: -padding.left - anchors.rightMargin: -padding.right - anchors.verticalCenter: parent.verticalCenter + id: track + anchors.fill: parent pressed: control.pressed checked: control.checked focused: control.activeFocus window_focused: control.window && control.window.active - styleDef: panel.styleDef.Switch_thumb + styleDef: panel.styleDef.Switch_track } - LabelStyle { - id: label - text: control.checked ? panel.styleDef.Switch_textOn : panel.styleDef.Switch_textOff + Item { + id: thumb - pressed: control.pressed - focused: control.activeFocus - selected: control.checked - window_focused: control.window && control.window.active - styleDef: panel.styleDef.Switch_switchTextAppearance + x: control.checked ? max : min - anchors.fill: parent + FontMetrics { + id: metrics + font: label.font + } + + readonly property real maxTextWidth: Math.max(metrics.boundingRect(panel.styleDef.Switch_textOn).width, + metrics.boundingRect(panel.styleDef.Switch_textOff).width) + + implicitWidth: Math.max(loader.implicitWidth, maxTextWidth + 2 * panel.styleDef.Switch_thumbTextPadding) + implicitHeight: Math.max(loader.implicitHeight, metrics.height) + + anchors.top: parent.top + anchors.bottom: parent.bottom + + Behavior on x { + id: behavior + enabled: thumb.status === Loader.Ready + NumberAnimation { + duration: 150 + easing.type: Easing.OutCubic + } + } + + DrawableLoader { + id: loader + anchors.left: parent.left + anchors.right: parent.right + anchors.leftMargin: -padding.left + anchors.rightMargin: -padding.right + anchors.verticalCenter: parent.verticalCenter + pressed: control.pressed + checked: control.checked + focused: control.activeFocus + window_focused: control.window && control.window.active + styleDef: panel.styleDef.Switch_thumb + } + + LabelStyle { + id: label + text: control.checked ? panel.styleDef.Switch_textOn : panel.styleDef.Switch_textOff + + pressed: control.pressed + focused: control.activeFocus + selected: control.checked + window_focused: control.window && control.window.active + styleDef: panel.styleDef.Switch_switchTextAppearance + + anchors.fill: parent + } } } } |