| 1 |
/* |
| 2 |
* Copyright 2013 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 "mathUtils.js" as MathUtils |
| 19 |
|
| 20 |
/*! |
| 21 |
\internal |
| 22 |
\qmltype GenericToolbar |
| 23 |
\inqmlmodule Lomiri.Components 0.1 |
| 24 |
\ingroup ubuntu |
| 25 |
*/ |
| 26 |
Item { |
| 27 |
id: bottomBar |
| 28 |
|
| 29 |
default property alias contents: bar.data |
| 30 |
|
| 31 |
/*! |
| 32 |
When active, the bar is visible, otherwise it is hidden. |
| 33 |
Use bottom edge swipe up/down to activate/deactivate the bar. |
| 34 |
The active property is not updated until the swipe gesture is completed. |
| 35 |
*/ |
| 36 |
property bool active: false |
| 37 |
|
| 38 |
/*! |
| 39 |
Disable bottom edge swipe to activate/deactivate the toolbar. |
| 40 |
*/ |
| 41 |
property bool lock: false |
| 42 |
|
| 43 |
/*! |
| 44 |
How much of the toolbar to show when starting interaction. |
| 45 |
*/ |
| 46 |
property real hintSize: units.gu(1) |
| 47 |
|
| 48 |
/*! |
| 49 |
Notify when the toolbar is fully visible |
| 50 |
*/ |
| 51 |
readonly property bool ready: bar.y == 0 |
| 52 |
|
| 53 |
anchors { |
| 54 |
left: parent.left |
| 55 |
right: parent.right |
| 56 |
bottom: parent.bottom |
| 57 |
} |
| 58 |
|
| 59 |
onActiveChanged: { |
| 60 |
if (active) state = "spread"; |
| 61 |
else state = ""; |
| 62 |
} |
| 63 |
|
| 64 |
onLockChanged: { |
| 65 |
if (state == "hint" || state == "moving") { |
| 66 |
draggingArea.finishMoving(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
states: [ |
| 71 |
State { |
| 72 |
name: "hint" |
| 73 |
PropertyChanges { |
| 74 |
target: bar |
| 75 |
y: bar.height - bottomBar.hintSize |
| 76 |
} |
| 77 |
}, |
| 78 |
State { |
| 79 |
name: "moving" |
| 80 |
PropertyChanges { |
| 81 |
target: bar |
| 82 |
y: MathUtils.clamp(bar.height, draggingArea.mouseY - internal.movingDelta, 0, bar.height) |
| 83 |
} |
| 84 |
}, |
| 85 |
State { |
| 86 |
name: "spread" |
| 87 |
PropertyChanges { |
| 88 |
target: bar |
| 89 |
y: 0 |
| 90 |
} |
| 91 |
}, |
| 92 |
State { |
| 93 |
name: "" |
| 94 |
PropertyChanges { |
| 95 |
target: bar |
| 96 |
y: bar.height |
| 97 |
} |
| 98 |
} |
| 99 |
] |
| 100 |
|
| 101 |
transitions: [ |
| 102 |
Transition { |
| 103 |
to: "" |
| 104 |
PropertyAnimation { |
| 105 |
target: bar |
| 106 |
properties: "y" |
| 107 |
duration: 175 |
| 108 |
easing.type: Easing.OutQuad |
| 109 |
} |
| 110 |
}, |
| 111 |
Transition { |
| 112 |
to: "hint" |
| 113 |
PropertyAnimation { |
| 114 |
target: bar |
| 115 |
properties: "y" |
| 116 |
duration: 175 |
| 117 |
easing.type: Easing.OutQuad |
| 118 |
} |
| 119 |
}, |
| 120 |
Transition { |
| 121 |
to: "spread" |
| 122 |
PropertyAnimation { |
| 123 |
target: bar |
| 124 |
properties: "y" |
| 125 |
duration: 175 |
| 126 |
easing.type: Easing.OutQuad |
| 127 |
} |
| 128 |
} |
| 129 |
] |
| 130 |
|
| 131 |
QtObject { |
| 132 |
id: internal |
| 133 |
property string previousState: "" |
| 134 |
property int movingDelta |
| 135 |
} |
| 136 |
|
| 137 |
onStateChanged: { |
| 138 |
if (state == "hint") { |
| 139 |
internal.movingDelta = bottomBar.hintSize + draggingArea.initialY - bar.height; |
| 140 |
} else if (state == "moving" && internal.previousState == "spread") { |
| 141 |
internal.movingDelta = draggingArea.initialY; |
| 142 |
} else if (state == "spread") { |
| 143 |
bottomBar.active = true; |
| 144 |
} else if (state == "") { |
| 145 |
bottomBar.active = false; |
| 146 |
} |
| 147 |
internal.previousState = state; |
| 148 |
} |
| 149 |
|
| 150 |
DraggingArea { |
| 151 |
orientation: Qt.Vertical |
| 152 |
id: draggingArea |
| 153 |
anchors { |
| 154 |
bottom: parent.bottom |
| 155 |
left: parent.left |
| 156 |
right: parent.right |
| 157 |
} |
| 158 |
height: bottomBar.active ? bar.height + units.gu(1) : units.gu(3) |
| 159 |
zeroVelocityCounts: true |
| 160 |
propagateComposedEvents: true |
| 161 |
visible: !bottomBar.lock |
| 162 |
|
| 163 |
property int initialY |
| 164 |
onPressed: { |
| 165 |
initialY = mouseY; |
| 166 |
if (bottomBar.state == "") bottomBar.state = "hint"; |
| 167 |
else bottomBar.state = "moving"; |
| 168 |
mouse.accepted = false |
| 169 |
} |
| 170 |
|
| 171 |
onPositionChanged: { |
| 172 |
if (bottomBar.state == "hint" && mouseY < initialY) { |
| 173 |
bottomBar.state = "moving"; |
| 174 |
} |
| 175 |
mouse.accepted = false |
| 176 |
} |
| 177 |
|
| 178 |
onReleased: { |
| 179 |
finishMoving() |
| 180 |
mouse.accepted = false |
| 181 |
} |
| 182 |
// Mouse cursor moving out of the window while pressed on desktop |
| 183 |
// TODO: Comment it for now since this is causing toolbar to flick on device due the several mouse areas overlaping |
| 184 |
// onCanceled: finishMoving() |
| 185 |
|
| 186 |
// FIXME: Make all parameters below themable. |
| 187 |
// The value of 44 was copied from the Launcher. |
| 188 |
function finishMoving() { |
| 189 |
if (draggingArea.dragVelocity < -44) { |
| 190 |
bottomBar.state = "spread"; |
| 191 |
} else if (draggingArea.dragVelocity > 44) { |
| 192 |
bottomBar.state = ""; |
| 193 |
} else { |
| 194 |
bottomBar.state = (bar.y < bar.height / 2) ? "spread" : ""; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
Item { |
| 200 |
id: bar |
| 201 |
|
| 202 |
objectName: "GenericToolbar.Bar" |
| 203 |
height: parent.height |
| 204 |
anchors { |
| 205 |
left: parent.left |
| 206 |
right: parent.right |
| 207 |
} |
| 208 |
|
| 209 |
visible: y != height |
| 210 |
y: bottomBar.active ? 0 : height |
| 211 |
} |
| 212 |
} |