| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
|
| 20 |
Item { |
| 21 |
id: switchStyle |
| 22 |
|
| 23 |
/*! |
| 24 |
The padding between the thumb and the outside border of the switch. |
| 25 |
*/ |
| 26 |
property real thumbPadding: 0 |
| 27 |
|
| 28 |
implicitWidth: units.gu(5) |
| 29 |
implicitHeight: units.gu(2.5) |
| 30 |
LayoutMirroring.enabled: false |
| 31 |
LayoutMirroring.childrenInherit: true |
| 32 |
|
| 33 |
/*! |
| 34 |
The background color of the switch. |
| 35 |
*/ |
| 36 |
property color backgroundColor: styledItem.enabled ? theme.palette.normal.base : theme.palette.disabled.base |
| 37 |
|
| 38 |
/*! |
| 39 |
The background color of the checked switch. |
| 40 |
*/ |
| 41 |
property color checkedBackgroundColor: styledItem.enabled ? theme.palette.normal.positive : theme.palette.disabled.base |
| 42 |
/*! |
| 43 |
The background color of the thumb. |
| 44 |
*/ |
| 45 |
property color thumbColor: styledItem.enabled ? theme.palette.normal.raised : theme.palette.disabled.raised |
| 46 |
|
| 47 |
FocusShape { |
| 48 |
} |
| 49 |
|
| 50 |
LomiriShape { |
| 51 |
id: background |
| 52 |
aspect: LomiriShape.Inset |
| 53 |
anchors.fill: parent |
| 54 |
backgroundColor: switchStyle.backgroundColor |
| 55 |
clip: true |
| 56 |
|
| 57 |
LomiriShape { |
| 58 |
id: thumb |
| 59 |
aspect: LomiriShape.Inset |
| 60 |
states: [ |
| 61 |
State { |
| 62 |
name: "checked" |
| 63 |
when: styledItem.checked |
| 64 |
AnchorChanges { |
| 65 |
target: thumb |
| 66 |
anchors.left: undefined |
| 67 |
anchors.right: parent.right |
| 68 |
} |
| 69 |
PropertyChanges { |
| 70 |
target: background |
| 71 |
backgroundColor: switchStyle.checkedBackgroundColor |
| 72 |
} |
| 73 |
} |
| 74 |
] |
| 75 |
|
| 76 |
transitions: [ |
| 77 |
// Avoid animations on width changes (during initialization) |
| 78 |
// by explicitly setting from and to for the Transitions. |
| 79 |
Transition { |
| 80 |
from: "*" |
| 81 |
to: "checked" |
| 82 |
reversible: true |
| 83 |
enabled: animated |
| 84 |
AnchorAnimation { |
| 85 |
targets: thumb |
| 86 |
duration: LomiriAnimation.FastDuration |
| 87 |
easing: LomiriAnimation.StandardEasing |
| 88 |
} |
| 89 |
ColorAnimation { |
| 90 |
target: thumb |
| 91 |
properties: "backgroundColor" |
| 92 |
duration: LomiriAnimation.FastDuration |
| 93 |
easing: LomiriAnimation.StandardEasing |
| 94 |
} |
| 95 |
} |
| 96 |
] |
| 97 |
|
| 98 |
backgroundColor: switchStyle.thumbColor |
| 99 |
width: (background.width - switchStyle.thumbPadding * 3.0) / 2.0 |
| 100 |
anchors { |
| 101 |
top: parent.top |
| 102 |
bottom: parent.bottom |
| 103 |
left: parent.left |
| 104 |
topMargin: switchStyle.thumbPadding |
| 105 |
bottomMargin: switchStyle.thumbPadding |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
} |
| 110 |
} |