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 Unity.Launcher 0.1
20 import Ubuntu.Components.Popups 1.3
21 import "../Components/ListItems"
22 import "../Components/"
28 rotation: inverted ? 180 : 0
31 property bool inverted: false
32 property bool dragging: false
33 property bool moving: launcherListView.moving || launcherListView.flicking
34 property bool preventHiding: moving || dndArea.draggedIndex >= 0 || quickList.state === "open" || dndArea.pressed
35 || mouseEventEater.containsMouse || dashItem.hovered
36 property int highlightIndex: -2
37 property bool shortcutHintsShown: false
39 signal applicationSelected(string appId)
41 signal kbdNavigationCancelled()
44 if (quickList.state == "open") {
49 function highlightNext() {
51 if (highlightIndex >= launcherListView.count) {
54 launcherListView.moveToIndex(Math.max(highlightIndex, 0));
56 function highlightPrevious() {
58 if (highlightIndex <= -2) {
59 highlightIndex = launcherListView.count - 1;
61 launcherListView.moveToIndex(Math.max(highlightIndex, 0));
63 function openQuicklist(index) {
64 quickList.open(index);
65 quickList.selectedIndex = 0;
66 quickList.focus = true;
82 objectName: "buttonShowDashHome"
85 color: UbuntuColors.orange
86 readonly property bool highlighted: root.highlightIndex == -1;
89 objectName: "dashItem"
90 width: parent.width * .6
92 anchors.centerIn: parent
93 source: "graphics/home.png"
94 rotation: root.rotation
99 activeFocusOnPress: false
100 onClicked: root.showDashHome()
103 objectName: "bfbFocusHighlight"
105 border.color: "white"
106 border.width: units.dp(1)
108 visible: parent.highlighted
113 anchors.left: parent.left
114 anchors.right: parent.right
115 height: parent.height - dashItem.height - parent.spacing*2
118 id: launcherListViewItem
124 objectName: "launcherListView"
127 topMargin: -extensionSize + width * .15
128 bottomMargin: -extensionSize + width * .15
130 topMargin: extensionSize
131 bottomMargin: extensionSize
132 height: parent.height - dashItem.height - parent.spacing*2
134 cacheBuffer: itemHeight * 3
135 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
136 highlightRangeMode: ListView.ApplyRange
137 preferredHighlightBegin: (height - itemHeight) / 2
138 preferredHighlightEnd: (height + itemHeight) / 2
140 // for the single peeking icon, when alert-state is set on delegate
141 property int peekingIndex: -1
143 // The size of the area the ListView is extended to make sure items are not
144 // destroyed when dragging them outside the list. This needs to be at least
145 // itemHeight to prevent folded items from disappearing and DragArea limits
146 // need to be smaller than this size to avoid breakage.
147 property int extensionSize: 0
149 // Setting extensionSize after the list has been populated because it has
150 // the potential to mess up with the intial positioning in combination
151 // with snapping to the center of the list. This catches all the cases
152 // where the item would be outside the list for more than itemHeight / 2.
153 // For the rest, give it a flick to scroll to the beginning. Note that
154 // the flicking alone isn't enough because in some cases it's not strong
155 // enough to overcome the snapping.
156 // https://bugreports.qt-project.org/browse/QTBUG-32251
157 Component.onCompleted: {
158 extensionSize = itemHeight * 3
159 flick(0, clickFlickSpeed)
162 // The height of the area where icons start getting folded
163 property int foldingStartHeight: itemHeight
164 // The height of the area where the items reach the final folding angle
165 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
166 property int itemWidth: width * .75
167 property int itemHeight: itemWidth * 15 / 16 + units.gu(1)
168 property int clickFlickSpeed: units.gu(60)
169 property int draggedIndex: dndArea.draggedIndex
170 property real realContentY: contentY - originY + topMargin
171 property int realItemHeight: itemHeight + spacing
173 // In case the start dragging transition is running, we need to delay the
174 // move because the displaced transition would clash with it and cause items
175 // to be moved to wrong places
176 property bool draggingTransitionRunning: false
177 property int scheduledMoveTo: -1
179 UbuntuNumberAnimation {
180 id: snapToBottomAnimation
181 target: launcherListView
183 to: launcherListView.originY + launcherListView.topMargin
186 UbuntuNumberAnimation {
187 id: snapToTopAnimation
188 target: launcherListView
190 to: launcherListView.contentHeight - launcherListView.height + launcherListView.originY - launcherListView.topMargin
193 UbuntuNumberAnimation {
195 objectName: "moveAnimation"
196 target: launcherListView
198 function moveTo(contentY) {
199 from = launcherListView.contentY;
204 function moveToIndex(index) {
205 var totalItemHeight = launcherListView.itemHeight + launcherListView.spacing
206 var itemPosition = index * totalItemHeight;
207 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
208 var distanceToEnd = index == 0 || index == launcherListView.count - 1 ? 0 : totalItemHeight
209 if (itemPosition + totalItemHeight + distanceToEnd > launcherListView.contentY + launcherListView.originY + launcherListView.topMargin + height) {
210 moveAnimation.moveTo(itemPosition + launcherListView.itemHeight - launcherListView.topMargin - height + distanceToEnd - launcherListView.originY);
211 } else if (itemPosition - distanceToEnd < launcherListView.contentY - launcherListView.originY + launcherListView.topMargin) {
212 moveAnimation.moveTo(itemPosition - distanceToEnd - launcherListView.topMargin + launcherListView.originY);
216 displaced: Transition {
217 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
220 delegate: FoldingLauncherDelegate {
222 objectName: "launcherDelegate" + index
223 // We need the appId in the delegate in order to find
224 // the right app when running autopilot tests for
226 readonly property string appId: model.appId
228 itemHeight: launcherListView.itemHeight
229 itemWidth: launcherListView.itemWidth
234 countVisible: model.countVisible
235 progress: model.progress
236 itemRunning: model.running
237 itemFocused: model.focused
238 inverted: root.inverted
239 alerting: model.alerting
240 highlighted: root.highlightIndex == index
241 shortcutHintShown: root.shortcutHintsShown && index <= 9
242 surfaceCount: model.surfaceCount
245 property bool dragging: false
247 SequentialAnimation {
251 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 1 : 0 }
252 PropertyAction { target: launcherListViewItem; property: "clip"; value: 0 }
254 UbuntuNumberAnimation {
255 target: launcherDelegate
259 to: (units.gu(.5) + launcherListView.width * .5) * (root.inverted ? -1 : 1)
260 duration: UbuntuAnimation.BriskDuration
264 UbuntuNumberAnimation {
265 target: launcherDelegate
270 duration: UbuntuAnimation.BriskDuration
273 PropertyAction { target: launcherListViewItem; property: "clip"; value: 1 }
274 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 0 : 1 }
275 PropertyAction { target: launcherListView; property: "peekingIndex"; value: -1 }
280 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
281 launcherListView.moveToIndex(index)
282 if (!dragging && launcher.state !== "visible") {
283 peekingAnimation.start()
287 if (launcherListView.peekingIndex === -1) {
288 launcherListView.peekingIndex = index
291 if (launcherListView.peekingIndex === index) {
292 launcherListView.peekingIndex = -1
299 objectName: "dropIndicator"
300 anchors.centerIn: parent
301 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
303 source: "graphics/divider-line.png"
309 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
311 target: launcherDelegate
319 target: launcherDelegate
324 target: dropIndicator
330 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
332 target: launcherDelegate
344 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
349 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
350 UbuntuNumberAnimation { properties: "angle,offset" }
355 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
356 UbuntuNumberAnimation { properties: "angle,offset" }
359 id: draggingTransition
362 SequentialAnimation {
363 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
365 UbuntuNumberAnimation { properties: "height" }
366 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
370 if (launcherListView.scheduledMoveTo > -1) {
371 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
372 dndArea.draggedIndex = launcherListView.scheduledMoveTo
373 launcherListView.scheduledMoveTo = -1
377 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
383 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
384 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
385 SequentialAnimation {
386 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
387 UbuntuNumberAnimation { properties: "height" }
388 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
389 PropertyAction { target: dndArea; property: "postDragging"; value: false }
390 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
398 objectName: "dndArea"
399 acceptedButtons: Qt.LeftButton | Qt.RightButton
402 topMargin: launcherListView.topMargin
403 bottomMargin: launcherListView.bottomMargin
405 drag.minimumY: -launcherListView.topMargin
406 drag.maximumY: height + launcherListView.bottomMargin
408 property int draggedIndex: -1
409 property var selectedItem
410 property bool preDragging: false
411 property bool dragging: !!selectedItem && selectedItem.dragging
412 property bool postDragging: false
420 function processPress(mouse) {
421 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
425 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
426 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
428 // Check if we actually clicked an item or only at the spacing in between
429 if (clickedItem === null) {
433 if (mouse.button & Qt.RightButton) { // context menu
435 quickList.open(index);
441 // First/last item do the scrolling at more than 12 degrees
442 if (index == 0 || index == launcherListView.count - 1) {
443 if (clickedItem.angle > 12 || clickedItem.angle < -12) {
444 launcherListView.moveToIndex(index);
446 root.applicationSelected(LauncherModel.get(index).appId);
451 // the rest launches apps up to an angle of 30 degrees
452 if (clickedItem.angle > 30 || clickedItem.angle < -30) {
453 launcherListView.moveToIndex(index);
455 root.applicationSelected(LauncherModel.get(index).appId);
467 function endDrag(dragItem) {
468 var droppedIndex = draggedIndex;
479 selectedItem.dragging = false;
480 selectedItem = undefined;
483 dragItem.target = undefined
485 progressiveScrollingTimer.stop();
486 launcherListView.interactive = true;
487 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
488 snapToBottomAnimation.start();
489 } else if (droppedIndex < 2 && postDragging) {
490 snapToTopAnimation.start();
495 processPressAndHold(mouse, drag);
498 function processPressAndHold(mouse, dragItem) {
499 if (Math.abs(selectedItem.angle) > 30) {
505 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
507 quickList.open(draggedIndex)
509 launcherListView.interactive = false
511 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
513 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
514 fakeDragItem.x = units.gu(0.5)
515 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
516 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
517 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
518 fakeDragItem.count = LauncherModel.get(draggedIndex).count
519 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
520 fakeDragItem.flatten()
521 dragItem.target = fakeDragItem
528 processPositionChanged(mouse)
531 function processPositionChanged(mouse) {
532 if (draggedIndex >= 0) {
533 if (!selectedItem.dragging) {
534 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
535 if (!preDragging && distance > units.gu(1.5)) {
537 quickList.state = "";
539 if (distance > launcherListView.itemHeight) {
540 selectedItem.dragging = true
544 if (!selectedItem.dragging) {
548 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
550 // Move it down by the the missing size to compensate index calculation with only expanded items
551 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
553 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
554 progressiveScrollingTimer.downwards = false
555 progressiveScrollingTimer.start()
556 } else if (mouseY < launcherListView.realItemHeight) {
557 progressiveScrollingTimer.downwards = true
558 progressiveScrollingTimer.start()
560 progressiveScrollingTimer.stop()
563 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
565 if (newIndex > draggedIndex + 1) {
566 newIndex = draggedIndex + 1
567 } else if (newIndex < draggedIndex) {
568 newIndex = draggedIndex -1
573 if (newIndex >= 0 && newIndex < launcherListView.count) {
574 if (launcherListView.draggingTransitionRunning) {
575 launcherListView.scheduledMoveTo = newIndex
577 launcherListView.model.move(draggedIndex, newIndex)
578 draggedIndex = newIndex
585 id: progressiveScrollingTimer
589 property bool downwards: true
592 var minY = -launcherListView.topMargin
593 if (launcherListView.contentY > minY) {
594 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
597 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
598 if (launcherListView.contentY < maxY) {
599 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
609 objectName: "fakeDragItem"
610 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
611 itemWidth: launcherListView.itemWidth
612 itemHeight: launcherListView.itemHeight
615 rotation: root.rotation
617 onVisibleChanged: if (!visible) iconName = "";
620 fakeDragItemAnimation.start();
623 UbuntuNumberAnimation {
624 id: fakeDragItemAnimation
625 target: fakeDragItem;
626 properties: "angle,offset";
635 objectName: "quickListShape"
636 anchors.fill: quickList
637 opacity: quickList.state === "open" ? 0.95 : 0
639 rotation: root.rotation
640 aspect: UbuntuShape.Flat
642 Behavior on opacity {
643 UbuntuNumberAnimation {}
651 rightMargin: -units.dp(4)
652 verticalCenter: parent.verticalCenter
653 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
657 source: "graphics/quicklist_tooltip.png"
663 anchors.fill: quickListShape
664 enabled: quickList.state == "open" || pressed
667 quickList.state = "";
668 quickList.focus = false;
669 root.kbdNavigationCancelled();
672 // Forward for dragging to work when quickList is open
675 var m = mapToItem(dndArea, mouseX, mouseY)
676 dndArea.processPress(m)
680 var m = mapToItem(dndArea, mouseX, mouseY)
681 dndArea.processPressAndHold(m, drag)
685 var m = mapToItem(dndArea, mouseX, mouseY)
686 dndArea.processPositionChanged(m)
690 dndArea.endDrag(drag);
694 dndArea.endDrag(drag);
700 objectName: "quickList"
701 color: theme.palette.normal.background
702 // Because we're setting left/right anchors depending on orientation, it will break the
703 // width setting after rotating twice. This makes sure we also re-apply width on rotation
704 width: root.inverted ? units.gu(30) : units.gu(30)
705 height: quickListColumn.height
706 visible: quickListShape.visible
708 left: root.inverted ? undefined : parent.right
709 right: root.inverted ? parent.left : undefined
712 y: itemCenter - (height / 2) + offset
713 rotation: root.rotation
716 property string appId
718 property int selectedIndex: -1
724 if (selectedIndex >= popoverRepeater.count) {
727 event.accepted = true;
731 if (selectedIndex < 0) {
732 selectedIndex = popoverRepeater.count - 1;
734 event.accepted = true;
738 quickList.selectedIndex = -1;
739 quickList.focus = false;
741 event.accepted = true;
746 if (quickList.selectedIndex >= 0) {
747 LauncherModel.quickListActionInvoked(quickList.appId, quickList.selectedIndex)
749 quickList.selectedIndex = -1;
750 quickList.focus = false;
752 root.kbdNavigationCancelled();
753 event.accepted = true;
759 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) + quickList.item.offset : units.gu(1)
760 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
761 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
763 function open(index) {
764 var itemPosition = index * launcherListView.itemHeight;
765 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
766 item = launcherListView.itemAt(launcherListView.width / 2, itemPosition + launcherListView.itemHeight / 2);
767 quickList.model = launcherListView.model.get(index).quickList;
768 quickList.appId = launcherListView.model.get(index).appId;
769 quickList.state = "open";
774 height: quickListColumn.height
779 height: childrenRect.height
783 model: quickList.model
786 objectName: "quickListEntry" + index
787 selected: index === quickList.selectedIndex
788 height: label.implicitHeight + label.anchors.topMargin + label.anchors.bottomMargin
789 color: model.clickable ? (selected ? theme.palette.highlighted.background : "transparent") : theme.palette.disabled.background
790 highlightColor: !model.clickable ? quickList.color : undefined // make disabled items visually unclickable
791 divider.colorFrom: UbuntuColors.inkstone
792 divider.colorTo: UbuntuColors.inkstone
797 anchors.leftMargin: units.gu(3) // 2 GU for checkmark, 3 GU total
798 anchors.rightMargin: units.gu(2)
799 anchors.topMargin: units.gu(2)
800 anchors.bottomMargin: units.gu(2)
801 verticalAlignment: Label.AlignVCenter
803 fontSize: index == 0 ? "medium" : "small"
804 font.weight: index == 0 ? Font.Medium : Font.Light
805 color: model.clickable ? theme.palette.normal.backgroundText : theme.palette.disabled.backgroundText
809 if (!model.clickable) {
813 quickList.state = "";
814 // Unsetting model to prevent showing changing entries during fading out
815 // that may happen because of triggering an action.
816 LauncherModel.quickListActionInvoked(quickList.appId, index);
817 quickList.focus = false;
818 root.kbdNavigationCancelled();
819 quickList.model = undefined;