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
155 itemFocused: model.focused
156 inverted: root.inverted
159 property bool dragging: false
163 objectName: "dropIndicator"
164 anchors.centerIn: parent
165 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
167 source: "graphics/divider-line.png"
173 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
175 target: launcherDelegate
183 target: launcherDelegate
188 target: dropIndicator
194 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
196 target: launcherDelegate
208 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
213 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
214 UbuntuNumberAnimation { properties: "angle,offset" }
219 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
220 UbuntuNumberAnimation { properties: "angle,offset" }
223 id: draggingTransition
226 SequentialAnimation {
227 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
229 UbuntuNumberAnimation { properties: "height" }
230 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
234 if (launcherListView.scheduledMoveTo > -1) {
235 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
236 dndArea.draggedIndex = launcherListView.scheduledMoveTo
237 launcherListView.scheduledMoveTo = -1
241 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
247 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
248 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
249 SequentialAnimation {
250 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
251 UbuntuNumberAnimation { properties: "height" }
252 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
253 PropertyAction { target: dndArea; property: "postDragging"; value: false }
254 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
262 objectName: "dndArea"
265 topMargin: launcherListView.topMargin
266 bottomMargin: launcherListView.bottomMargin
268 drag.minimumY: -launcherListView.topMargin
269 drag.maximumY: height + launcherListView.bottomMargin
271 property int draggedIndex: -1
272 property var selectedItem
273 property bool preDragging: false
274 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
275 property bool postDragging: false
280 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
284 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
285 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
287 // Check if we actually clicked an item or only at the spacing in between
288 if (clickedItem === null) {
292 // First/last item do the scrolling at more than 12 degrees
293 if (index == 0 || index == launcherListView.count - 1) {
294 if (clickedItem.angle > 12) {
295 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
296 } else if (clickedItem.angle < -12) {
297 launcherListView.flick(0, launcherListView.clickFlickSpeed);
299 root.applicationSelected(LauncherModel.get(index).appId);
304 // the rest launches apps up to an angle of 30 degrees
305 if (clickedItem.angle > 30) {
306 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
307 } else if (clickedItem.angle < -30) {
308 launcherListView.flick(0, launcherListView.clickFlickSpeed);
310 root.applicationSelected(LauncherModel.get(index).appId);
315 selectedItem = undefined;
317 postDragging = false;
321 var droppedIndex = draggedIndex;
332 selectedItem.dragging = false;
333 selectedItem = undefined;
336 drag.target = undefined
338 progressiveScrollingTimer.stop();
339 launcherListView.interactive = true;
340 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
341 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
343 if (droppedIndex == 0 && postDragging) {
344 launcherListView.flick(0, launcherListView.clickFlickSpeed);
349 if (Math.abs(selectedItem.angle) > 30) {
353 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
356 quickList.item = selectedItem;
357 quickList.model = launcherListView.model.get(draggedIndex).quickList;
358 quickList.appId = launcherListView.model.get(draggedIndex).appId;
359 quickList.state = "open";
361 launcherListView.interactive = false
363 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
365 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
366 fakeDragItem.x = units.gu(0.5)
367 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
368 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
369 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
370 fakeDragItem.count = LauncherModel.get(draggedIndex).count
371 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
372 fakeDragItem.flatten()
373 drag.target = fakeDragItem
380 if (draggedIndex >= 0) {
381 if (!selectedItem.dragging) {
382 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
383 if (!preDragging && distance > units.gu(1.5)) {
385 quickList.state = "";
387 if (distance > launcherListView.itemHeight) {
388 selectedItem.dragging = true
392 if (!selectedItem.dragging) {
396 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
398 // Move it down by the the missing size to compensate index calculation with only expanded items
399 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
401 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
402 progressiveScrollingTimer.downwards = false
403 progressiveScrollingTimer.start()
404 } else if (mouseY < launcherListView.realItemHeight) {
405 progressiveScrollingTimer.downwards = true
406 progressiveScrollingTimer.start()
408 progressiveScrollingTimer.stop()
411 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
413 if (newIndex > draggedIndex + 1) {
414 newIndex = draggedIndex + 1
415 } else if (newIndex < draggedIndex) {
416 newIndex = draggedIndex -1
421 if (newIndex >= 0 && newIndex < launcherListView.count) {
422 if (launcherListView.draggingTransitionRunning) {
423 launcherListView.scheduledMoveTo = newIndex
425 launcherListView.model.move(draggedIndex, newIndex)
426 draggedIndex = newIndex
433 id: progressiveScrollingTimer
437 property bool downwards: true
440 var minY = -launcherListView.topMargin
441 if (launcherListView.contentY > minY) {
442 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
445 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
446 if (launcherListView.contentY < maxY) {
447 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
457 objectName: "fakeDragItem"
458 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
459 itemWidth: launcherListView.itemWidth
460 itemHeight: launcherListView.itemHeight
463 rotation: root.rotation
465 pinned: dndArea.draggedIndex > -1 &&
466 LauncherModel.get(dndArea.draggedIndex).pinned &&
467 !dndArea.preDragging &&
471 fakeDragItemAnimation.start();
474 UbuntuNumberAnimation {
475 id: fakeDragItemAnimation
476 target: fakeDragItem;
477 properties: "angle,offset";
486 objectName: "quickListShape"
487 anchors.fill: quickList
488 opacity: quickList.state === "open" ? 0.8 : 0
490 rotation: root.rotation
492 Behavior on opacity {
493 UbuntuNumberAnimation {}
501 rightMargin: -units.dp(4)
502 verticalCenter: parent.verticalCenter
503 verticalCenterOffset: -quickList.offset
507 source: "graphics/quicklist_tooltip.png"
513 enabled: quickList.state == "open"
523 objectName: "quickList"
526 height: quickListColumn.height
527 visible: quickListShape.visible
529 left: root.inverted ? undefined : parent.right
530 right: root.inverted ? parent.left : undefined
533 y: itemCenter - (height / 2) + offset
534 rotation: root.rotation
537 property string appId
541 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) : units.gu(1)
542 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
543 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
548 height: childrenRect.height
552 model: quickList.model
555 objectName: "quickListEntry" + index
556 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
557 highlightWhenPressed: model.clickable
559 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
560 // ListItems don't know that they are sitting in a themed Popover where the color
561 // needs to be inverted.
562 __foregroundColor: Theme.palette.selected.backgroundText
565 if (!model.clickable) {
568 quickList.state = "";
569 // Unsetting model to prevent showing changing entries during fading out
570 // that may happen because of triggering an action.
571 LauncherModel.quickListActionInvoked(quickList.appId, index);
572 quickList.model = undefined;