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.3
19 import Ubuntu.Components.ListItems 1.3 as ListItems
20 import Unity.Launcher 0.1
21 import Ubuntu.Components.Popups 1.3
22 import "../Components/ListItems"
23 import "../Components/"
29 rotation: inverted ? 180 : 0
32 property bool inverted: false
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 || mouseEventEater.containsMouse || dashItem.hovered
37 property int highlightIndex: -1
39 signal applicationSelected(string appId)
43 if (quickList.state == "open") {
61 objectName: "buttonShowDashHome"
69 topMargin: -units.gu(2)
71 aspect: UbuntuShape.Flat
72 backgroundColor: UbuntuColors.orange
76 objectName: "dashItem"
79 anchors.centerIn: parent
80 source: "graphics/home.png"
81 rotation: root.rotation
86 onClicked: root.showDashHome()
91 anchors.left: parent.left
92 anchors.right: parent.right
93 height: parent.height - dashItem.height - parent.spacing*2
96 id: launcherListViewItem
102 objectName: "launcherListView"
105 topMargin: -extensionSize + units.gu(0.5)
106 bottomMargin: -extensionSize + units.gu(1)
107 leftMargin: units.gu(0.5)
108 rightMargin: units.gu(0.5)
110 topMargin: extensionSize
111 bottomMargin: extensionSize
112 height: parent.height - dashItem.height - parent.spacing*2
114 cacheBuffer: itemHeight * 3
115 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
116 highlightRangeMode: ListView.ApplyRange
117 preferredHighlightBegin: (height - itemHeight) / 2
118 preferredHighlightEnd: (height + itemHeight) / 2
120 // for the single peeking icon, when alert-state is set on delegate
121 property int peekingIndex: -1
123 // The size of the area the ListView is extended to make sure items are not
124 // destroyed when dragging them outside the list. This needs to be at least
125 // itemHeight to prevent folded items from disappearing and DragArea limits
126 // need to be smaller than this size to avoid breakage.
127 property int extensionSize: 0
129 // Setting extensionSize after the list has been populated because it has
130 // the potential to mess up with the intial positioning in combination
131 // with snapping to the center of the list. This catches all the cases
132 // where the item would be outside the list for more than itemHeight / 2.
133 // For the rest, give it a flick to scroll to the beginning. Note that
134 // the flicking alone isn't enough because in some cases it's not strong
135 // enough to overcome the snapping.
136 // https://bugreports.qt-project.org/browse/QTBUG-32251
137 Component.onCompleted: {
138 extensionSize = itemHeight * 3
139 flick(0, clickFlickSpeed)
142 // The height of the area where icons start getting folded
143 property int foldingStartHeight: units.gu(6.5)
144 // The height of the area where the items reach the final folding angle
145 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
146 property int itemWidth: units.gu(7)
147 property int itemHeight: units.gu(6.5)
148 property int clickFlickSpeed: units.gu(60)
149 property int draggedIndex: dndArea.draggedIndex
150 property real realContentY: contentY - originY + topMargin
151 property int realItemHeight: itemHeight + spacing
153 // In case the start dragging transition is running, we need to delay the
154 // move because the displaced transition would clash with it and cause items
155 // to be moved to wrong places
156 property bool draggingTransitionRunning: false
157 property int scheduledMoveTo: -1
159 UbuntuNumberAnimation {
160 id: snapToBottomAnimation
161 target: launcherListView
163 to: launcherListView.originY + launcherListView.topMargin
166 UbuntuNumberAnimation {
167 id: snapToTopAnimation
168 target: launcherListView
170 to: launcherListView.contentHeight - launcherListView.height + launcherListView.originY - launcherListView.topMargin
173 UbuntuNumberAnimation {
175 target: launcherListView
177 function moveTo(contentY) {
178 from = launcherListView.contentY;
184 displaced: Transition {
185 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
188 delegate: FoldingLauncherDelegate {
190 objectName: "launcherDelegate" + index
191 // We need the appId in the delegate in order to find
192 // the right app when running autopilot tests for
194 readonly property string appId: model.appId
195 itemHeight: launcherListView.itemHeight
196 itemWidth: launcherListView.itemWidth
201 countVisible: model.countVisible
202 progress: model.progress
203 itemRunning: model.running
204 itemFocused: model.focused
205 inverted: root.inverted
206 alerting: model.alerting
209 property bool dragging: false
211 SequentialAnimation {
215 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 1 : 0 }
216 PropertyAction { target: launcherListViewItem; property: "clip"; value: 0 }
218 UbuntuNumberAnimation {
219 target: launcherDelegate
223 to: (units.gu(.5) + launcherListView.width * .5) * (root.inverted ? -1 : 1)
224 duration: UbuntuAnimation.BriskDuration
228 UbuntuNumberAnimation {
229 target: launcherDelegate
234 duration: UbuntuAnimation.BriskDuration
237 PropertyAction { target: launcherListViewItem; property: "clip"; value: 1 }
238 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 0 : 1 }
243 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
244 var itemPosition = index * launcherListView.itemHeight;
245 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
246 var distanceToEnd = index == 0 || index == launcherListView.count - 1 ? 0 : launcherListView.itemHeight
247 if (itemPosition + launcherListView.itemHeight + distanceToEnd > launcherListView.contentY + launcherListView.topMargin + height) {
248 moveAnimation.moveTo(itemPosition + launcherListView.itemHeight - launcherListView.topMargin - height + distanceToEnd);
249 } else if (itemPosition - distanceToEnd < launcherListView.contentY + launcherListView.topMargin) {
250 moveAnimation.moveTo(itemPosition - distanceToEnd - launcherListView.topMargin);
252 if (!dragging && launcher.state !== "visible") {
253 peekingAnimation.start()
257 if (launcherListView.peekingIndex === -1) {
258 launcherListView.peekingIndex = index
261 if (launcherListView.peekingIndex === index) {
262 launcherListView.peekingIndex = -1
269 objectName: "dropIndicator"
270 anchors.centerIn: parent
271 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
273 source: "graphics/divider-line.png"
279 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
281 target: launcherDelegate
289 target: launcherDelegate
294 target: dropIndicator
300 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
302 target: launcherDelegate
314 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
319 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
320 UbuntuNumberAnimation { properties: "angle,offset" }
325 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
326 UbuntuNumberAnimation { properties: "angle,offset" }
329 id: draggingTransition
332 SequentialAnimation {
333 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
335 UbuntuNumberAnimation { properties: "height" }
336 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
340 if (launcherListView.scheduledMoveTo > -1) {
341 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
342 dndArea.draggedIndex = launcherListView.scheduledMoveTo
343 launcherListView.scheduledMoveTo = -1
347 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
353 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
354 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
355 SequentialAnimation {
356 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
357 UbuntuNumberAnimation { properties: "height" }
358 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
359 PropertyAction { target: dndArea; property: "postDragging"; value: false }
360 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
368 objectName: "dndArea"
369 acceptedButtons: Qt.LeftButton | Qt.RightButton
372 topMargin: launcherListView.topMargin
373 bottomMargin: launcherListView.bottomMargin
375 drag.minimumY: -launcherListView.topMargin
376 drag.maximumY: height + launcherListView.bottomMargin
378 property int draggedIndex: -1
379 property var selectedItem
380 property bool preDragging: false
381 property bool dragging: !!selectedItem && selectedItem.dragging
382 property bool postDragging: false
390 function processPress(mouse) {
391 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
395 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
396 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
398 // Check if we actually clicked an item or only at the spacing in between
399 if (clickedItem === null) {
403 if (mouse.button & Qt.RightButton) { // context menu
405 quickList.item = clickedItem;
406 quickList.model = launcherListView.model.get(index).quickList;
407 quickList.appId = launcherListView.model.get(index).appId;
408 quickList.state = "open";
414 // First/last item do the scrolling at more than 12 degrees
415 if (index == 0 || index == launcherListView.count - 1) {
416 if (clickedItem.angle > 12) {
417 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
418 } else if (clickedItem.angle < -12) {
419 launcherListView.flick(0, launcherListView.clickFlickSpeed);
421 root.applicationSelected(LauncherModel.get(index).appId);
426 // the rest launches apps up to an angle of 30 degrees
427 if (clickedItem.angle > 30) {
428 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
429 } else if (clickedItem.angle < -30) {
430 launcherListView.flick(0, launcherListView.clickFlickSpeed);
432 root.applicationSelected(LauncherModel.get(index).appId);
444 function endDrag(dragItem) {
445 var droppedIndex = draggedIndex;
456 selectedItem.dragging = false;
457 selectedItem = undefined;
460 dragItem.target = undefined
462 progressiveScrollingTimer.stop();
463 launcherListView.interactive = true;
464 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
465 snapToBottomAnimation.start();
466 } else if (droppedIndex < 2 && postDragging) {
467 snapToTopAnimation.start();
472 processPressAndHold(mouse, drag);
475 function processPressAndHold(mouse, dragItem) {
476 if (Math.abs(selectedItem.angle) > 30) {
482 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
485 quickList.item = selectedItem;
486 quickList.model = launcherListView.model.get(draggedIndex).quickList;
487 quickList.appId = launcherListView.model.get(draggedIndex).appId;
488 quickList.state = "open";
490 launcherListView.interactive = false
492 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
494 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
495 fakeDragItem.x = units.gu(0.5)
496 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
497 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
498 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
499 fakeDragItem.count = LauncherModel.get(draggedIndex).count
500 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
501 fakeDragItem.flatten()
502 dragItem.target = fakeDragItem
509 processPositionChanged(mouse)
512 function processPositionChanged(mouse) {
513 if (draggedIndex >= 0) {
514 if (!selectedItem.dragging) {
515 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
516 if (!preDragging && distance > units.gu(1.5)) {
518 quickList.state = "";
520 if (distance > launcherListView.itemHeight) {
521 selectedItem.dragging = true
525 if (!selectedItem.dragging) {
529 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
531 // Move it down by the the missing size to compensate index calculation with only expanded items
532 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
534 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
535 progressiveScrollingTimer.downwards = false
536 progressiveScrollingTimer.start()
537 } else if (mouseY < launcherListView.realItemHeight) {
538 progressiveScrollingTimer.downwards = true
539 progressiveScrollingTimer.start()
541 progressiveScrollingTimer.stop()
544 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
546 if (newIndex > draggedIndex + 1) {
547 newIndex = draggedIndex + 1
548 } else if (newIndex < draggedIndex) {
549 newIndex = draggedIndex -1
554 if (newIndex >= 0 && newIndex < launcherListView.count) {
555 if (launcherListView.draggingTransitionRunning) {
556 launcherListView.scheduledMoveTo = newIndex
558 launcherListView.model.move(draggedIndex, newIndex)
559 draggedIndex = newIndex
566 id: progressiveScrollingTimer
570 property bool downwards: true
573 var minY = -launcherListView.topMargin
574 if (launcherListView.contentY > minY) {
575 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
578 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
579 if (launcherListView.contentY < maxY) {
580 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
590 objectName: "fakeDragItem"
591 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
592 itemWidth: launcherListView.itemWidth
593 itemHeight: launcherListView.itemHeight
596 rotation: root.rotation
598 onVisibleChanged: if (!visible) iconName = "";
601 fakeDragItemAnimation.start();
604 UbuntuNumberAnimation {
605 id: fakeDragItemAnimation
606 target: fakeDragItem;
607 properties: "angle,offset";
616 objectName: "quickListShape"
617 anchors.fill: quickList
618 opacity: quickList.state === "open" ? 0.8 : 0
620 rotation: root.rotation
622 Behavior on opacity {
623 UbuntuNumberAnimation {}
631 rightMargin: -units.dp(4)
632 verticalCenter: parent.verticalCenter
633 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
637 source: "graphics/quicklist_tooltip.png"
643 anchors.fill: quickListShape
644 enabled: quickList.state == "open" || pressed
650 // Forward for dragging to work when quickList is open
653 var m = mapToItem(dndArea, mouseX, mouseY)
654 dndArea.processPress(m)
658 var m = mapToItem(dndArea, mouseX, mouseY)
659 dndArea.processPressAndHold(m, drag)
663 var m = mapToItem(dndArea, mouseX, mouseY)
664 dndArea.processPositionChanged(m)
668 dndArea.endDrag(drag);
672 dndArea.endDrag(drag);
678 objectName: "quickList"
680 // Because we're setting left/right anchors depending on orientation, it will break the
681 // width setting after rotating twice. This makes sure we also re-apply width on rotation
682 width: root.inverted ? units.gu(30) : units.gu(30)
683 height: quickListColumn.height
684 visible: quickListShape.visible
686 left: root.inverted ? undefined : parent.right
687 right: root.inverted ? parent.left : undefined
690 y: itemCenter - (height / 2) + offset
691 rotation: root.rotation
694 property string appId
698 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) + quickList.item.offset : units.gu(1)
699 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
700 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
705 height: childrenRect.height
709 model: quickList.model
712 objectName: "quickListEntry" + index
713 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
714 highlightWhenPressed: model.clickable
716 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
717 // ListItems don't know that they are sitting in a themed Popover where the color
718 // needs to be inverted.
719 __foregroundColor: "black"
722 if (!model.clickable) {
726 quickList.state = "";
727 // Unsetting model to prevent showing changing entries during fading out
728 // that may happen because of triggering an action.
729 LauncherModel.quickListActionInvoked(quickList.appId, index);
730 quickList.model = undefined;