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/"
24 import "../Components/Flickables" as Flickables
30 rotation: inverted ? 180 : 0
33 property bool inverted: true
34 property bool dragging: false
35 property bool moving: launcherListView.moving || launcherListView.flicking
36 property bool preventHiding: moving || dndArea.draggedIndex >= 0 || quickList.state === "open" || dndArea.pressed
37 property int highlightIndex: -1
39 signal applicationSelected(string appId)
49 objectName: "buttonShowDashHome"
52 color: UbuntuColors.orange
56 objectName: "dashItem"
59 anchors.centerIn: parent
60 source: "graphics/home.png"
61 rotation: root.rotation
66 onClicked: root.showDashHome()
71 anchors.left: parent.left
72 anchors.right: parent.right
73 height: parent.height - dashItem.height - parent.spacing*2
81 objectName: "launcherListView"
84 topMargin: -extensionSize + units.gu(0.5)
85 bottomMargin: -extensionSize + units.gu(1)
86 leftMargin: units.gu(0.5)
87 rightMargin: units.gu(0.5)
89 topMargin: extensionSize
90 bottomMargin: extensionSize
91 height: parent.height - dashItem.height - parent.spacing*2
93 cacheBuffer: itemHeight * 3
94 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
95 highlightRangeMode: ListView.ApplyRange
96 preferredHighlightBegin: (height - itemHeight) / 2
97 preferredHighlightEnd: (height + itemHeight) / 2
99 // The size of the area the ListView is extended to make sure items are not
100 // destroyed when dragging them outside the list. This needs to be at least
101 // itemHeight to prevent folded items from disappearing and DragArea limits
102 // need to be smaller than this size to avoid breakage.
103 property int extensionSize: 0
105 // Setting extensionSize after the list has been populated because it has
106 // the potential to mess up with the intial positioning in combination
107 // with snapping to the center of the list. This catches all the cases
108 // where the item would be outside the list for more than itemHeight / 2.
109 // For the rest, give it a flick to scroll to the beginning. Note that
110 // the flicking alone isn't enough because in some cases it's not strong
111 // enough to overcome the snapping.
112 // https://bugreports.qt-project.org/browse/QTBUG-32251
113 Component.onCompleted: {
114 extensionSize = itemHeight * 3
115 flick(0, clickFlickSpeed)
118 // The height of the area where icons start getting folded
119 property int foldingStartHeight: units.gu(6.5)
120 // The height of the area where the items reach the final folding angle
121 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
122 property int itemWidth: units.gu(7)
123 property int itemHeight: units.gu(6.5)
124 property int clickFlickSpeed: units.gu(60)
125 property int draggedIndex: dndArea.draggedIndex
126 property real realContentY: contentY - originY + topMargin
127 property int realItemHeight: itemHeight + spacing
129 // In case the start dragging transition is running, we need to delay the
130 // move because the displaced transition would clash with it and cause items
131 // to be moved to wrong places
132 property bool draggingTransitionRunning: false
133 property int scheduledMoveTo: -1
135 displaced: Transition {
136 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
139 delegate: FoldingLauncherDelegate {
141 objectName: "launcherDelegate" + index
142 // We need the appId in the delegate in order to find
143 // the right app when running autopilot tests for
145 readonly property string appId: model.appId
146 itemHeight: launcherListView.itemHeight
147 itemWidth: launcherListView.itemWidth
152 countVisible: model.countVisible
153 progress: model.progress
154 itemFocused: model.focused
155 inverted: root.inverted
158 property bool dragging: false
162 objectName: "dropIndicator"
163 anchors.centerIn: parent
164 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
166 source: "graphics/divider-line.png"
172 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
174 target: launcherDelegate
182 target: launcherDelegate
187 target: dropIndicator
193 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
195 target: launcherDelegate
207 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
212 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
213 UbuntuNumberAnimation { properties: "angle,offset" }
218 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
219 UbuntuNumberAnimation { properties: "angle,offset" }
222 id: draggingTransition
225 SequentialAnimation {
226 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
228 UbuntuNumberAnimation { properties: "height" }
229 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
233 if (launcherListView.scheduledMoveTo > -1) {
234 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
235 dndArea.draggedIndex = launcherListView.scheduledMoveTo
236 launcherListView.scheduledMoveTo = -1
240 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
246 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
247 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
248 SequentialAnimation {
249 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
250 UbuntuNumberAnimation { properties: "height" }
251 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
252 PropertyAction { target: dndArea; property: "postDragging"; value: false }
253 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
261 objectName: "dndArea"
264 topMargin: launcherListView.topMargin
265 bottomMargin: launcherListView.bottomMargin
267 drag.minimumY: -launcherListView.topMargin
268 drag.maximumY: height + launcherListView.bottomMargin
270 property int draggedIndex: -1
271 property var selectedItem
272 property bool preDragging: false
273 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
274 property bool postDragging: false
279 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
283 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
284 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
286 // Check if we actually clicked an item or only at the spacing in between
287 if (clickedItem === null) {
291 // First/last item do the scrolling at more than 12 degrees
292 if (index == 0 || index == launcherListView.count - 1) {
293 if (clickedItem.angle > 12) {
294 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
295 } else if (clickedItem.angle < -12) {
296 launcherListView.flick(0, launcherListView.clickFlickSpeed);
298 root.applicationSelected(LauncherModel.get(index).appId);
303 // the rest launches apps up to an angle of 30 degrees
304 if (clickedItem.angle > 30) {
305 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
306 } else if (clickedItem.angle < -30) {
307 launcherListView.flick(0, launcherListView.clickFlickSpeed);
309 root.applicationSelected(LauncherModel.get(index).appId);
314 selectedItem = undefined;
316 postDragging = false;
320 var droppedIndex = draggedIndex;
331 selectedItem.dragging = false;
332 selectedItem = undefined;
335 drag.target = undefined
337 progressiveScrollingTimer.stop();
338 launcherListView.interactive = true;
339 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
340 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
342 if (droppedIndex == 0 && postDragging) {
343 launcherListView.flick(0, launcherListView.clickFlickSpeed);
348 if (Math.abs(selectedItem.angle) > 30) {
352 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
355 quickList.item = selectedItem;
356 quickList.model = launcherListView.model.get(draggedIndex).quickList;
357 quickList.appId = launcherListView.model.get(draggedIndex).appId;
358 quickList.state = "open";
360 launcherListView.interactive = false
362 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
364 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
365 fakeDragItem.x = units.gu(0.5)
366 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
367 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
368 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
369 fakeDragItem.count = LauncherModel.get(draggedIndex).count
370 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
371 fakeDragItem.flatten()
372 drag.target = fakeDragItem
379 if (draggedIndex >= 0) {
380 if (!selectedItem.dragging) {
381 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
382 if (!preDragging && distance > units.gu(1.5)) {
384 quickList.state = "";
386 if (distance > launcherListView.itemHeight) {
387 selectedItem.dragging = true
391 if (!selectedItem.dragging) {
395 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
397 // Move it down by the the missing size to compensate index calculation with only expanded items
398 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
400 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
401 progressiveScrollingTimer.downwards = false
402 progressiveScrollingTimer.start()
403 } else if (mouseY < launcherListView.realItemHeight) {
404 progressiveScrollingTimer.downwards = true
405 progressiveScrollingTimer.start()
407 progressiveScrollingTimer.stop()
410 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
412 if (newIndex > draggedIndex + 1) {
413 newIndex = draggedIndex + 1
414 } else if (newIndex < draggedIndex) {
415 newIndex = draggedIndex -1
420 if (newIndex >= 0 && newIndex < launcherListView.count) {
421 if (launcherListView.draggingTransitionRunning) {
422 launcherListView.scheduledMoveTo = newIndex
424 launcherListView.model.move(draggedIndex, newIndex)
425 draggedIndex = newIndex
432 id: progressiveScrollingTimer
436 property bool downwards: true
439 var minY = -launcherListView.topMargin
440 if (launcherListView.contentY > minY) {
441 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
444 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
445 if (launcherListView.contentY < maxY) {
446 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
456 objectName: "fakeDragItem"
457 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
458 itemWidth: launcherListView.itemWidth
459 itemHeight: launcherListView.itemHeight
462 rotation: root.rotation
466 fakeDragItemAnimation.start();
469 UbuntuNumberAnimation {
470 id: fakeDragItemAnimation
471 target: fakeDragItem;
472 properties: "angle,offset";
481 objectName: "quickListShape"
482 anchors.fill: quickList
483 opacity: quickList.state === "open" ? 0.8 : 0
485 rotation: root.rotation
487 Behavior on opacity {
488 UbuntuNumberAnimation {}
496 rightMargin: -units.dp(4)
497 verticalCenter: parent.verticalCenter
498 verticalCenterOffset: -quickList.offset
502 source: "graphics/quicklist_tooltip.png"
508 enabled: quickList.state == "open"
518 objectName: "quickList"
521 height: quickListColumn.height
522 visible: quickListShape.visible
524 left: root.inverted ? undefined : parent.right
525 right: root.inverted ? parent.left : undefined
528 y: itemCenter - (height / 2) + offset
529 rotation: root.rotation
532 property string appId
536 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) : units.gu(1)
537 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
538 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
543 height: childrenRect.height
547 model: quickList.model
550 objectName: "quickListEntry" + index
551 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
552 highlightWhenPressed: model.clickable
554 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
555 // ListItems don't know that they are sitting in a themed Popover where the color
556 // needs to be inverted.
557 __foregroundColor: Theme.palette.selected.backgroundText
560 if (!model.clickable) {
563 quickList.state = "";
564 // Unsetting model to prevent showing changing entries during fading out
565 // that may happen because of triggering an action.
566 LauncherModel.quickListActionInvoked(quickList.appId, index);
567 quickList.model = undefined;