2 * Copyright (C) 2013-2016 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;
72 acceptedButtons: Qt.AllButtons
74 onWheel: wheel.accepted = true;
84 objectName: "buttonShowDashHome"
87 color: UbuntuColors.orange
88 readonly property bool highlighted: root.highlightIndex == -1;
91 objectName: "dashItem"
92 width: parent.width * .6
94 anchors.centerIn: parent
95 source: "graphics/home.png"
96 rotation: root.rotation
101 activeFocusOnPress: false
102 onClicked: root.showDashHome()
105 objectName: "bfbFocusHighlight"
107 border.color: "white"
108 border.width: units.dp(1)
110 visible: parent.highlighted
115 anchors.left: parent.left
116 anchors.right: parent.right
117 height: parent.height - dashItem.height - parent.spacing*2
120 id: launcherListViewItem
126 objectName: "launcherListView"
129 topMargin: -extensionSize + width * .15
130 bottomMargin: -extensionSize + width * .15
132 topMargin: extensionSize
133 bottomMargin: extensionSize
134 height: parent.height - dashItem.height - parent.spacing*2
136 cacheBuffer: itemHeight * 3
137 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
138 highlightRangeMode: ListView.ApplyRange
139 preferredHighlightBegin: (height - itemHeight) / 2
140 preferredHighlightEnd: (height + itemHeight) / 2
142 // for the single peeking icon, when alert-state is set on delegate
143 property int peekingIndex: -1
145 // The size of the area the ListView is extended to make sure items are not
146 // destroyed when dragging them outside the list. This needs to be at least
147 // itemHeight to prevent folded items from disappearing and DragArea limits
148 // need to be smaller than this size to avoid breakage.
149 property int extensionSize: 0
151 // Setting extensionSize after the list has been populated because it has
152 // the potential to mess up with the intial positioning in combination
153 // with snapping to the center of the list. This catches all the cases
154 // where the item would be outside the list for more than itemHeight / 2.
155 // For the rest, give it a flick to scroll to the beginning. Note that
156 // the flicking alone isn't enough because in some cases it's not strong
157 // enough to overcome the snapping.
158 // https://bugreports.qt-project.org/browse/QTBUG-32251
159 Component.onCompleted: {
160 extensionSize = itemHeight * 3
161 flick(0, clickFlickSpeed)
164 // The height of the area where icons start getting folded
165 property int foldingStartHeight: itemHeight
166 // The height of the area where the items reach the final folding angle
167 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
168 property int itemWidth: width * .75
169 property int itemHeight: itemWidth * 15 / 16 + units.gu(1)
170 property int clickFlickSpeed: units.gu(60)
171 property int draggedIndex: dndArea.draggedIndex
172 property real realContentY: contentY - originY + topMargin
173 property int realItemHeight: itemHeight + spacing
175 // In case the start dragging transition is running, we need to delay the
176 // move because the displaced transition would clash with it and cause items
177 // to be moved to wrong places
178 property bool draggingTransitionRunning: false
179 property int scheduledMoveTo: -1
181 UbuntuNumberAnimation {
182 id: snapToBottomAnimation
183 target: launcherListView
185 to: launcherListView.originY + launcherListView.topMargin
188 UbuntuNumberAnimation {
189 id: snapToTopAnimation
190 target: launcherListView
192 to: launcherListView.contentHeight - launcherListView.height + launcherListView.originY - launcherListView.topMargin
195 UbuntuNumberAnimation {
197 objectName: "moveAnimation"
198 target: launcherListView
200 function moveTo(contentY) {
201 from = launcherListView.contentY;
206 function moveToIndex(index) {
207 var totalItemHeight = launcherListView.itemHeight + launcherListView.spacing
208 var itemPosition = index * totalItemHeight;
209 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
210 var distanceToEnd = index == 0 || index == launcherListView.count - 1 ? 0 : totalItemHeight
211 if (itemPosition + totalItemHeight + distanceToEnd > launcherListView.contentY + launcherListView.originY + launcherListView.topMargin + height) {
212 moveAnimation.moveTo(itemPosition + launcherListView.itemHeight - launcherListView.topMargin - height + distanceToEnd - launcherListView.originY);
213 } else if (itemPosition - distanceToEnd < launcherListView.contentY - launcherListView.originY + launcherListView.topMargin) {
214 moveAnimation.moveTo(itemPosition - distanceToEnd - launcherListView.topMargin + launcherListView.originY);
218 displaced: Transition {
219 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
222 delegate: FoldingLauncherDelegate {
224 objectName: "launcherDelegate" + index
225 // We need the appId in the delegate in order to find
226 // the right app when running autopilot tests for
228 readonly property string appId: model.appId
230 itemHeight: launcherListView.itemHeight
231 itemWidth: launcherListView.itemWidth
236 countVisible: model.countVisible
237 progress: model.progress
238 itemRunning: model.running
239 itemFocused: model.focused
240 inverted: root.inverted
241 alerting: model.alerting
242 highlighted: root.highlightIndex == index
243 shortcutHintShown: root.shortcutHintsShown && index <= 9
244 surfaceCount: model.surfaceCount
247 property bool dragging: false
249 SequentialAnimation {
253 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 1 : 0 }
254 PropertyAction { target: launcherListViewItem; property: "clip"; value: 0 }
256 UbuntuNumberAnimation {
257 target: launcherDelegate
261 to: (units.gu(.5) + launcherListView.width * .5) * (root.inverted ? -1 : 1)
262 duration: UbuntuAnimation.BriskDuration
266 UbuntuNumberAnimation {
267 target: launcherDelegate
272 duration: UbuntuAnimation.BriskDuration
275 PropertyAction { target: launcherListViewItem; property: "clip"; value: 1 }
276 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 0 : 1 }
277 PropertyAction { target: launcherListView; property: "peekingIndex"; value: -1 }
282 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
283 launcherListView.moveToIndex(index)
284 if (!dragging && launcher.state !== "visible") {
285 peekingAnimation.start()
289 if (launcherListView.peekingIndex === -1) {
290 launcherListView.peekingIndex = index
293 if (launcherListView.peekingIndex === index) {
294 launcherListView.peekingIndex = -1
301 objectName: "dropIndicator"
302 anchors.centerIn: parent
303 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
305 source: "graphics/divider-line.png"
311 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
313 target: launcherDelegate
321 target: launcherDelegate
326 target: dropIndicator
332 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
334 target: launcherDelegate
346 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
351 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
352 UbuntuNumberAnimation { properties: "angle,offset" }
357 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
358 UbuntuNumberAnimation { properties: "angle,offset" }
361 id: draggingTransition
364 SequentialAnimation {
365 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
367 UbuntuNumberAnimation { properties: "height" }
368 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
372 if (launcherListView.scheduledMoveTo > -1) {
373 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
374 dndArea.draggedIndex = launcherListView.scheduledMoveTo
375 launcherListView.scheduledMoveTo = -1
379 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
385 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
386 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
387 SequentialAnimation {
388 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
389 UbuntuNumberAnimation { properties: "height" }
390 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
391 PropertyAction { target: dndArea; property: "postDragging"; value: false }
392 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
400 objectName: "dndArea"
401 acceptedButtons: Qt.LeftButton | Qt.RightButton
404 topMargin: launcherListView.topMargin
405 bottomMargin: launcherListView.bottomMargin
407 drag.minimumY: -launcherListView.topMargin
408 drag.maximumY: height + launcherListView.bottomMargin
410 property int draggedIndex: -1
411 property var selectedItem
412 property bool preDragging: false
413 property bool dragging: !!selectedItem && selectedItem.dragging
414 property bool postDragging: false
422 function processPress(mouse) {
423 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
427 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
428 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
430 // Check if we actually clicked an item or only at the spacing in between
431 if (clickedItem === null) {
435 if (mouse.button & Qt.RightButton) { // context menu
437 quickList.open(index);
443 // First/last item do the scrolling at more than 12 degrees
444 if (index == 0 || index == launcherListView.count - 1) {
445 if (clickedItem.angle > 12 || clickedItem.angle < -12) {
446 launcherListView.moveToIndex(index);
448 root.applicationSelected(LauncherModel.get(index).appId);
453 // the rest launches apps up to an angle of 30 degrees
454 if (clickedItem.angle > 30 || clickedItem.angle < -30) {
455 launcherListView.moveToIndex(index);
457 root.applicationSelected(LauncherModel.get(index).appId);
469 function endDrag(dragItem) {
470 var droppedIndex = draggedIndex;
481 selectedItem.dragging = false;
482 selectedItem = undefined;
485 dragItem.target = undefined
487 progressiveScrollingTimer.stop();
488 launcherListView.interactive = true;
489 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
490 snapToBottomAnimation.start();
491 } else if (droppedIndex < 2 && postDragging) {
492 snapToTopAnimation.start();
497 processPressAndHold(mouse, drag);
500 function processPressAndHold(mouse, dragItem) {
501 if (Math.abs(selectedItem.angle) > 30) {
507 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
509 quickList.open(draggedIndex)
511 launcherListView.interactive = false
513 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
515 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
516 fakeDragItem.x = units.gu(0.5)
517 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
518 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
519 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
520 fakeDragItem.count = LauncherModel.get(draggedIndex).count
521 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
522 fakeDragItem.flatten()
523 dragItem.target = fakeDragItem
530 processPositionChanged(mouse)
533 function processPositionChanged(mouse) {
534 if (draggedIndex >= 0) {
535 if (!selectedItem.dragging) {
536 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
537 if (!preDragging && distance > units.gu(1.5)) {
539 quickList.state = "";
541 if (distance > launcherListView.itemHeight) {
542 selectedItem.dragging = true
546 if (!selectedItem.dragging) {
550 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
552 // Move it down by the the missing size to compensate index calculation with only expanded items
553 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
555 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
556 progressiveScrollingTimer.downwards = false
557 progressiveScrollingTimer.start()
558 } else if (mouseY < launcherListView.realItemHeight) {
559 progressiveScrollingTimer.downwards = true
560 progressiveScrollingTimer.start()
562 progressiveScrollingTimer.stop()
565 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
567 if (newIndex > draggedIndex + 1) {
568 newIndex = draggedIndex + 1
569 } else if (newIndex < draggedIndex) {
570 newIndex = draggedIndex -1
575 if (newIndex >= 0 && newIndex < launcherListView.count) {
576 if (launcherListView.draggingTransitionRunning) {
577 launcherListView.scheduledMoveTo = newIndex
579 launcherListView.model.move(draggedIndex, newIndex)
580 draggedIndex = newIndex
587 id: progressiveScrollingTimer
591 property bool downwards: true
594 var minY = -launcherListView.topMargin
595 if (launcherListView.contentY > minY) {
596 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
599 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
600 if (launcherListView.contentY < maxY) {
601 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
611 objectName: "fakeDragItem"
612 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
613 itemWidth: launcherListView.itemWidth
614 itemHeight: launcherListView.itemHeight
617 rotation: root.rotation
619 onVisibleChanged: if (!visible) iconName = "";
622 fakeDragItemAnimation.start();
625 UbuntuNumberAnimation {
626 id: fakeDragItemAnimation
627 target: fakeDragItem;
628 properties: "angle,offset";
637 objectName: "quickListShape"
638 anchors.fill: quickList
639 opacity: quickList.state === "open" ? 0.95 : 0
641 rotation: root.rotation
642 aspect: UbuntuShape.Flat
644 Behavior on opacity {
645 UbuntuNumberAnimation {}
653 rightMargin: -units.dp(4)
654 verticalCenter: parent.verticalCenter
655 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
659 source: "graphics/quicklist_tooltip.png"
665 anchors.fill: quickListShape
666 enabled: quickList.state == "open" || pressed
669 quickList.state = "";
670 quickList.focus = false;
671 root.kbdNavigationCancelled();
674 // Forward for dragging to work when quickList is open
677 var m = mapToItem(dndArea, mouseX, mouseY)
678 dndArea.processPress(m)
682 var m = mapToItem(dndArea, mouseX, mouseY)
683 dndArea.processPressAndHold(m, drag)
687 var m = mapToItem(dndArea, mouseX, mouseY)
688 dndArea.processPositionChanged(m)
692 dndArea.endDrag(drag);
696 dndArea.endDrag(drag);
702 objectName: "quickList"
703 color: theme.palette.normal.background
704 // Because we're setting left/right anchors depending on orientation, it will break the
705 // width setting after rotating twice. This makes sure we also re-apply width on rotation
706 width: root.inverted ? units.gu(30) : units.gu(30)
707 height: quickListColumn.height
708 visible: quickListShape.visible
710 left: root.inverted ? undefined : parent.right
711 right: root.inverted ? parent.left : undefined
714 y: itemCenter - (height / 2) + offset
715 rotation: root.rotation
718 property string appId
720 property int selectedIndex: -1
726 if (selectedIndex >= popoverRepeater.count) {
729 event.accepted = true;
733 if (selectedIndex < 0) {
734 selectedIndex = popoverRepeater.count - 1;
736 event.accepted = true;
740 quickList.selectedIndex = -1;
741 quickList.focus = false;
743 event.accepted = true;
748 if (quickList.selectedIndex >= 0) {
749 LauncherModel.quickListActionInvoked(quickList.appId, quickList.selectedIndex)
751 quickList.selectedIndex = -1;
752 quickList.focus = false;
754 root.kbdNavigationCancelled();
755 event.accepted = true;
761 property int itemCenter: item ? root.mapFromItem(quickList.item, 0, 0).y + (item.height / 2) + quickList.item.offset : units.gu(1)
762 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
763 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
765 function open(index) {
766 var itemPosition = index * launcherListView.itemHeight;
767 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
768 item = launcherListView.itemAt(launcherListView.width / 2, itemPosition + launcherListView.itemHeight / 2);
769 quickList.model = launcherListView.model.get(index).quickList;
770 quickList.appId = launcherListView.model.get(index).appId;
771 quickList.state = "open";
776 height: quickListColumn.height
781 height: childrenRect.height
785 model: quickList.model
788 objectName: "quickListEntry" + index
789 selected: index === quickList.selectedIndex
790 height: label.implicitHeight + label.anchors.topMargin + label.anchors.bottomMargin
791 color: model.clickable ? (selected ? theme.palette.highlighted.background : "transparent") : theme.palette.disabled.background
792 highlightColor: !model.clickable ? quickList.color : undefined // make disabled items visually unclickable
793 divider.colorFrom: UbuntuColors.inkstone
794 divider.colorTo: UbuntuColors.inkstone
799 anchors.leftMargin: units.gu(3) // 2 GU for checkmark, 3 GU total
800 anchors.rightMargin: units.gu(2)
801 anchors.topMargin: units.gu(2)
802 anchors.bottomMargin: units.gu(2)
803 verticalAlignment: Label.AlignVCenter
805 fontSize: index == 0 ? "medium" : "small"
806 font.weight: index == 0 ? Font.Medium : Font.Light
807 color: model.clickable ? theme.palette.normal.backgroundText : theme.palette.disabled.backgroundText
811 if (!model.clickable) {
815 quickList.state = "";
816 // Unsetting model to prevent showing changing entries during fading out
817 // that may happen because of triggering an action.
818 LauncherModel.quickListActionInvoked(quickList.appId, index);
819 quickList.focus = false;
820 root.kbdNavigationCancelled();
821 quickList.model = undefined;