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 1.1
19 import Ubuntu.Components.ListItems 1.0 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)
42 if (quickList.state == "open") {
54 objectName: "buttonShowDashHome"
57 color: UbuntuColors.orange
61 objectName: "dashItem"
64 anchors.centerIn: parent
65 source: "graphics/home.png"
66 rotation: root.rotation
71 onClicked: root.showDashHome()
76 anchors.left: parent.left
77 anchors.right: parent.right
78 height: parent.height - dashItem.height - parent.spacing*2
86 objectName: "launcherListView"
89 topMargin: -extensionSize + units.gu(0.5)
90 bottomMargin: -extensionSize + units.gu(1)
91 leftMargin: units.gu(0.5)
92 rightMargin: units.gu(0.5)
94 topMargin: extensionSize
95 bottomMargin: extensionSize
96 height: parent.height - dashItem.height - parent.spacing*2
98 cacheBuffer: itemHeight * 3
99 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
100 highlightRangeMode: ListView.ApplyRange
101 preferredHighlightBegin: (height - itemHeight) / 2
102 preferredHighlightEnd: (height + itemHeight) / 2
104 // The size of the area the ListView is extended to make sure items are not
105 // destroyed when dragging them outside the list. This needs to be at least
106 // itemHeight to prevent folded items from disappearing and DragArea limits
107 // need to be smaller than this size to avoid breakage.
108 property int extensionSize: 0
110 // Setting extensionSize after the list has been populated because it has
111 // the potential to mess up with the intial positioning in combination
112 // with snapping to the center of the list. This catches all the cases
113 // where the item would be outside the list for more than itemHeight / 2.
114 // For the rest, give it a flick to scroll to the beginning. Note that
115 // the flicking alone isn't enough because in some cases it's not strong
116 // enough to overcome the snapping.
117 // https://bugreports.qt-project.org/browse/QTBUG-32251
118 Component.onCompleted: {
119 extensionSize = itemHeight * 3
120 flick(0, clickFlickSpeed)
123 // The height of the area where icons start getting folded
124 property int foldingStartHeight: units.gu(6.5)
125 // The height of the area where the items reach the final folding angle
126 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
127 property int itemWidth: units.gu(7)
128 property int itemHeight: units.gu(6.5)
129 property int clickFlickSpeed: units.gu(60)
130 property int draggedIndex: dndArea.draggedIndex
131 property real realContentY: contentY - originY + topMargin
132 property int realItemHeight: itemHeight + spacing
134 // In case the start dragging transition is running, we need to delay the
135 // move because the displaced transition would clash with it and cause items
136 // to be moved to wrong places
137 property bool draggingTransitionRunning: false
138 property int scheduledMoveTo: -1
140 displaced: Transition {
141 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
144 delegate: FoldingLauncherDelegate {
146 objectName: "launcherDelegate" + index
147 // We need the appId in the delegate in order to find
148 // the right app when running autopilot tests for
150 readonly property string appId: model.appId
151 itemHeight: launcherListView.itemHeight
152 itemWidth: launcherListView.itemWidth
157 countVisible: model.countVisible
158 progress: model.progress
159 itemFocused: model.focused
160 inverted: root.inverted
163 property bool dragging: false
167 objectName: "dropIndicator"
168 anchors.centerIn: parent
169 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
171 source: "graphics/divider-line.png"
177 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
179 target: launcherDelegate
187 target: launcherDelegate
192 target: dropIndicator
198 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
200 target: launcherDelegate
212 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
217 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
218 UbuntuNumberAnimation { properties: "angle,offset" }
223 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
224 UbuntuNumberAnimation { properties: "angle,offset" }
227 id: draggingTransition
230 SequentialAnimation {
231 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
233 UbuntuNumberAnimation { properties: "height" }
234 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
238 if (launcherListView.scheduledMoveTo > -1) {
239 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
240 dndArea.draggedIndex = launcherListView.scheduledMoveTo
241 launcherListView.scheduledMoveTo = -1
245 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
251 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
252 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
253 SequentialAnimation {
254 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
255 UbuntuNumberAnimation { properties: "height" }
256 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
257 PropertyAction { target: dndArea; property: "postDragging"; value: false }
258 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
266 objectName: "dndArea"
269 topMargin: launcherListView.topMargin
270 bottomMargin: launcherListView.bottomMargin
272 drag.minimumY: -launcherListView.topMargin
273 drag.maximumY: height + launcherListView.bottomMargin
275 property int draggedIndex: -1
276 property var selectedItem
277 property bool preDragging: false
278 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
279 property bool postDragging: false
284 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
288 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
289 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
291 // Check if we actually clicked an item or only at the spacing in between
292 if (clickedItem === null) {
296 // First/last item do the scrolling at more than 12 degrees
297 if (index == 0 || index == launcherListView.count - 1) {
298 if (clickedItem.angle > 12) {
299 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
300 } else if (clickedItem.angle < -12) {
301 launcherListView.flick(0, launcherListView.clickFlickSpeed);
303 root.applicationSelected(LauncherModel.get(index).appId);
308 // the rest launches apps up to an angle of 30 degrees
309 if (clickedItem.angle > 30) {
310 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
311 } else if (clickedItem.angle < -30) {
312 launcherListView.flick(0, launcherListView.clickFlickSpeed);
314 root.applicationSelected(LauncherModel.get(index).appId);
319 selectedItem = undefined;
321 postDragging = false;
325 var droppedIndex = draggedIndex;
336 selectedItem.dragging = false;
337 selectedItem = undefined;
340 drag.target = undefined
342 progressiveScrollingTimer.stop();
343 launcherListView.interactive = true;
344 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
345 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
347 if (droppedIndex == 0 && postDragging) {
348 launcherListView.flick(0, launcherListView.clickFlickSpeed);
353 if (Math.abs(selectedItem.angle) > 30) {
357 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
360 quickList.item = selectedItem;
361 quickList.model = launcherListView.model.get(draggedIndex).quickList;
362 quickList.appId = launcherListView.model.get(draggedIndex).appId;
363 quickList.state = "open";
365 launcherListView.interactive = false
367 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
369 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
370 fakeDragItem.x = units.gu(0.5)
371 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
372 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
373 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
374 fakeDragItem.count = LauncherModel.get(draggedIndex).count
375 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
376 fakeDragItem.flatten()
377 drag.target = fakeDragItem
384 if (draggedIndex >= 0) {
385 if (!selectedItem.dragging) {
386 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
387 if (!preDragging && distance > units.gu(1.5)) {
389 quickList.state = "";
391 if (distance > launcherListView.itemHeight) {
392 selectedItem.dragging = true
396 if (!selectedItem.dragging) {
400 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
402 // Move it down by the the missing size to compensate index calculation with only expanded items
403 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
405 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
406 progressiveScrollingTimer.downwards = false
407 progressiveScrollingTimer.start()
408 } else if (mouseY < launcherListView.realItemHeight) {
409 progressiveScrollingTimer.downwards = true
410 progressiveScrollingTimer.start()
412 progressiveScrollingTimer.stop()
415 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
417 if (newIndex > draggedIndex + 1) {
418 newIndex = draggedIndex + 1
419 } else if (newIndex < draggedIndex) {
420 newIndex = draggedIndex -1
425 if (newIndex >= 0 && newIndex < launcherListView.count) {
426 if (launcherListView.draggingTransitionRunning) {
427 launcherListView.scheduledMoveTo = newIndex
429 launcherListView.model.move(draggedIndex, newIndex)
430 draggedIndex = newIndex
437 id: progressiveScrollingTimer
441 property bool downwards: true
444 var minY = -launcherListView.topMargin
445 if (launcherListView.contentY > minY) {
446 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
449 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
450 if (launcherListView.contentY < maxY) {
451 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
461 objectName: "fakeDragItem"
462 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
463 itemWidth: launcherListView.itemWidth
464 itemHeight: launcherListView.itemHeight
467 rotation: root.rotation
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.96 : 0
490 rotation: root.rotation
492 Behavior on opacity {
493 UbuntuNumberAnimation {}
501 leftMargin: (quickList.item.width - units.gu(1)) / 2 - width / 2
502 verticalCenter: parent.verticalCenter
503 verticalCenterOffset: (parent.height / 2 + units.dp(3)) * (quickList.offset > 0 ? 1 : -1)
507 source: "graphics/quicklist_tooltip.png"
508 rotation: quickList.offset > 0 ? 0 : 180
513 enabled: quickList.state == "open"
523 objectName: "quickList"
526 height: quickListColumn.height
527 visible: quickListShape.visible
529 left: root.inverted ? undefined : parent.left
530 right: root.inverted ? parent.right : undefined
533 y: itemCenter + 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 + (item.height/2) + height + units.gu(1) > parent.height ?
543 -(item.height/2) - height - units.gu(.5) :
544 (item.height/2) + units.gu(.5)
549 height: childrenRect.height
553 model: quickList.model
556 objectName: "quickListEntry" + index
557 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
558 highlightWhenPressed: model.clickable
560 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
561 // ListItems don't know that they are sitting in a themed Popover where the color
562 // needs to be inverted.
563 __foregroundColor: "black"
566 if (!model.clickable) {
569 quickList.state = "";
570 // Unsetting model to prevent showing changing entries during fading out
571 // that may happen because of triggering an action.
572 LauncherModel.quickListActionInvoked(quickList.appId, index);
573 quickList.model = undefined;