2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
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 General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import Ubuntu.Components 0.1
19 import Ubuntu.Components.ListItems 0.1 as ListItems
20 import Unity.Launcher 0.1
21 import Ubuntu.Components.Popups 0.1
22 import "../Components/ListItems"
23 import "../Components/"
29 rotation: inverted ? 180 : 0
32 property bool inverted: true
33 property bool dragging: false
34 property bool moving: launcherListView.moving || launcherListView.flicking
35 property bool preventHiding: moving || dndArea.draggedIndex >= 0 || quickList.state === "open" || dndArea.pressed
36 property int highlightIndex: -1
38 signal applicationSelected(string appId)
50 color: UbuntuColors.orange
54 objectName: "dashItem"
57 anchors.centerIn: parent
58 source: "graphics/home.png"
59 rotation: root.rotation
64 onClicked: root.showDashHome()
72 margins: -mainColumn.anchors.leftMargin
74 rotation: root.rotation
78 anchors.left: parent.left
79 anchors.right: parent.right
80 height: parent.height - dashItem.height - parent.spacing*2
88 objectName: "launcherListView"
91 topMargin: -extensionSize + units.gu(0.5)
92 bottomMargin: -extensionSize + units.gu(1)
93 leftMargin: units.gu(0.5)
94 rightMargin: units.gu(0.5)
96 topMargin: extensionSize
97 bottomMargin: extensionSize
98 height: parent.height - dashItem.height - parent.spacing*2
100 cacheBuffer: itemHeight * 3
101 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
102 highlightRangeMode: ListView.ApplyRange
103 preferredHighlightBegin: (height - itemHeight) / 2
104 preferredHighlightEnd: (height + itemHeight) / 2
106 // The size of the area the ListView is extended to make sure items are not
107 // destroyed when dragging them outside the list. This needs to be at least
108 // itemHeight to prevent folded items from disappearing and DragArea limits
109 // need to be smaller than this size to avoid breakage.
110 property int extensionSize: 0
112 // Setting extensionSize after the list has been populated because it has
113 // the potential to mess up with the intial positioning in combination
114 // with snapping to the center of the list. This catches all the cases
115 // where the item would be outside the list for more than itemHeight / 2.
116 // For the rest, give it a flick to scroll to the beginning. Note that
117 // the flicking alone isn't enough because in some cases it's not strong
118 // enough to overcome the snapping.
119 // https://bugreports.qt-project.org/browse/QTBUG-32251
120 Component.onCompleted: {
121 extensionSize = itemHeight * 3
122 flick(0, clickFlickSpeed)
125 // The height of the area where icons start getting folded
126 property int foldingStartHeight: units.gu(6.5)
127 // The height of the area where the items reach the final folding angle
128 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
129 property int itemWidth: units.gu(7)
130 property int itemHeight: units.gu(6.5)
131 property int clickFlickSpeed: units.gu(60)
132 property int draggedIndex: dndArea.draggedIndex
133 property real realContentY: contentY - originY + topMargin
134 property int realItemHeight: itemHeight + spacing
136 // In case the start dragging transition is running, we need to delay the
137 // move because the displaced transition would clash with it and cause items
138 // to be moved to wrong places
139 property bool draggingTransitionRunning: false
140 property int scheduledMoveTo: -1
142 displaced: Transition {
143 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
146 delegate: FoldingLauncherDelegate {
148 objectName: "launcherDelegate" + index
149 // We need the appId in the delegate in order to find
150 // the right app when running autopilot tests for
152 readonly property string appId: model.appId
153 itemHeight: launcherListView.itemHeight
154 itemWidth: launcherListView.itemWidth
159 countVisible: model.countVisible
160 progress: model.progress
161 clipCorner: model.pinned
162 itemFocused: model.focused
163 inverted: root.inverted
166 property bool dragging: false
170 objectName: "dropIndicator"
171 anchors.centerIn: parent
172 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
174 source: "graphics/divider-line.png"
180 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
182 target: launcherDelegate
190 target: launcherDelegate
195 target: dropIndicator
201 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
203 target: launcherDelegate
215 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
220 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
221 UbuntuNumberAnimation { properties: "angle,offset" }
226 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
227 UbuntuNumberAnimation { properties: "angle,offset" }
230 id: draggingTransition
233 SequentialAnimation {
234 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
236 UbuntuNumberAnimation { properties: "height" }
237 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
241 if (launcherListView.scheduledMoveTo > -1) {
242 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
243 dndArea.draggedIndex = launcherListView.scheduledMoveTo
244 launcherListView.scheduledMoveTo = -1
248 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
254 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
255 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
256 SequentialAnimation {
257 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
258 UbuntuNumberAnimation { properties: "height" }
259 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
260 PropertyAction { target: dndArea; property: "postDragging"; value: false }
261 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
269 objectName: "dndArea"
272 topMargin: launcherListView.topMargin
273 bottomMargin: launcherListView.bottomMargin
275 drag.minimumY: -launcherListView.topMargin
276 drag.maximumY: height + launcherListView.bottomMargin
278 property int draggedIndex: -1
279 property var selectedItem
280 property bool preDragging: false
281 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
282 property bool postDragging: false
287 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
291 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
292 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
294 // Check if we actually clicked an item or only at the spacing in between
295 if (clickedItem === null) {
299 // First/last item do the scrolling at more than 12 degrees
300 if (index == 0 || index == launcherListView.count - 1) {
301 if (clickedItem.angle > 12) {
302 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
303 } else if (clickedItem.angle < -12) {
304 launcherListView.flick(0, launcherListView.clickFlickSpeed);
306 root.applicationSelected(LauncherModel.get(index).appId);
311 // the rest launches apps up to an angle of 30 degrees
312 if (clickedItem.angle > 30) {
313 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
314 } else if (clickedItem.angle < -30) {
315 launcherListView.flick(0, launcherListView.clickFlickSpeed);
317 root.applicationSelected(LauncherModel.get(index).appId);
322 selectedItem = undefined;
324 postDragging = false;
328 var droppedIndex = draggedIndex;
339 selectedItem.dragging = false;
340 selectedItem = undefined;
343 drag.target = undefined
345 progressiveScrollingTimer.stop();
346 launcherListView.interactive = true;
347 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
348 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
350 if (droppedIndex == 0 && postDragging) {
351 launcherListView.flick(0, launcherListView.clickFlickSpeed);
356 if (Math.abs(selectedItem.angle) > 30) {
360 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
363 quickList.item = selectedItem;
364 quickList.model = launcherListView.model.get(draggedIndex).quickList;
365 quickList.appId = launcherListView.model.get(draggedIndex).appId;
366 quickList.state = "open";
368 launcherListView.interactive = false
370 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
372 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
373 fakeDragItem.x = units.gu(0.5)
374 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
375 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
376 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
377 fakeDragItem.count = LauncherModel.get(draggedIndex).count
378 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
379 fakeDragItem.flatten()
380 drag.target = fakeDragItem
387 if (draggedIndex >= 0) {
388 if (!selectedItem.dragging) {
389 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
390 if (!preDragging && distance > units.gu(1.5)) {
392 quickList.state = "";
394 if (distance > launcherListView.itemHeight) {
395 selectedItem.dragging = true
399 if (!selectedItem.dragging) {
403 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
405 // Move it down by the the missing size to compensate index calculation with only expanded items
406 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
408 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
409 progressiveScrollingTimer.downwards = false
410 progressiveScrollingTimer.start()
411 } else if (mouseY < launcherListView.realItemHeight) {
412 progressiveScrollingTimer.downwards = true
413 progressiveScrollingTimer.start()
415 progressiveScrollingTimer.stop()
418 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
420 if (newIndex > draggedIndex + 1) {
421 newIndex = draggedIndex + 1
422 } else if (newIndex < draggedIndex) {
423 newIndex = draggedIndex -1
428 if (newIndex >= 0 && newIndex < launcherListView.count) {
429 if (launcherListView.draggingTransitionRunning) {
430 launcherListView.scheduledMoveTo = newIndex
432 launcherListView.model.move(draggedIndex, newIndex)
433 draggedIndex = newIndex
440 id: progressiveScrollingTimer
444 property bool downwards: true
447 var minY = -launcherListView.topMargin
448 if (launcherListView.contentY > minY) {
449 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
452 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
453 if (launcherListView.contentY < maxY) {
454 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
464 objectName: "fakeDragItem"
465 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
466 itemWidth: launcherListView.itemWidth
467 itemHeight: launcherListView.itemHeight
470 rotation: root.rotation
472 clipCorner: dndArea.draggedIndex > -1 &&
473 LauncherModel.get(dndArea.draggedIndex).pinned &&
474 !dndArea.preDragging &&
478 fakeDragItemAnimation.start();
481 UbuntuNumberAnimation {
482 id: fakeDragItemAnimation
483 target: fakeDragItem;
484 properties: "angle,offset";
493 objectName: "quickListShape"
494 anchors.fill: quickList
495 opacity: quickList.state === "open" ? 0.8 : 0
497 rotation: root.rotation
499 Behavior on opacity {
500 UbuntuNumberAnimation {}
508 rightMargin: -units.dp(4)
509 verticalCenter: parent.verticalCenter
510 verticalCenterOffset: -quickList.offset
514 source: "graphics/quicklist_tooltip.png"
520 enabled: quickList.state == "open"
530 objectName: "quickList"
533 height: quickListColumn.height
534 visible: quickListShape.visible
536 left: root.inverted ? undefined : parent.right
537 right: root.inverted ? parent.left : undefined
540 y: itemCenter - (height / 2) + offset
541 rotation: root.rotation
544 property string appId
548 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) : units.gu(1)
549 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
550 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
555 height: childrenRect.height
559 model: quickList.model
562 objectName: "quickListEntry" + index
563 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
564 highlightWhenPressed: model.clickable
566 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
567 // ListItems don't know that they are sitting in a themed Popover where the color
568 // needs to be inverted.
569 __foregroundColor: Theme.palette.selected.backgroundText
572 if (!model.clickable) {
575 quickList.state = "";
576 // Unsetting model to prevent showing changing entries during fading out
577 // that may happen because of triggering an action.
578 LauncherModel.quickListActionInvoked(quickList.appId, index);
579 quickList.model = undefined;