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 GlobalShortcut 1.0
23 import "../Components/ListItems"
24 import "../Components/"
30 rotation: inverted ? 180 : 0
33 property bool inverted: false
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 || mouseEventEater.containsMouse || dashItem.hovered
38 property int highlightIndex: -2
39 property bool shortcutHintsShown: false
41 signal applicationSelected(string appId)
43 signal kbdNavigationCancelled()
46 if (quickList.state == "open") {
51 function highlightNext() {
53 if (highlightIndex >= launcherListView.count) {
56 launcherListView.moveToIndex(Math.max(highlightIndex, 0));
58 function highlightPrevious() {
60 if (highlightIndex <= -2) {
61 highlightIndex = launcherListView.count - 1;
63 launcherListView.moveToIndex(Math.max(highlightIndex, 0));
65 function openQuicklist(index) {
66 quickList.open(index);
67 quickList.selectedIndex = 0;
68 quickList.focus = 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 onClicked: root.showDashHome()
104 objectName: "bfbFocusHighlight"
106 border.color: "white"
107 border.width: units.dp(1)
109 visible: parent.highlighted
114 anchors.left: parent.left
115 anchors.right: parent.right
116 height: parent.height - dashItem.height - parent.spacing*2
119 id: launcherListViewItem
125 objectName: "launcherListView"
128 topMargin: -extensionSize + width * .15
129 bottomMargin: -extensionSize + width * .15
131 topMargin: extensionSize
132 bottomMargin: extensionSize
133 height: parent.height - dashItem.height - parent.spacing*2
135 cacheBuffer: itemHeight * 3
136 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
137 highlightRangeMode: ListView.ApplyRange
138 preferredHighlightBegin: (height - itemHeight) / 2
139 preferredHighlightEnd: (height + itemHeight) / 2
141 // for the single peeking icon, when alert-state is set on delegate
142 property int peekingIndex: -1
144 // The size of the area the ListView is extended to make sure items are not
145 // destroyed when dragging them outside the list. This needs to be at least
146 // itemHeight to prevent folded items from disappearing and DragArea limits
147 // need to be smaller than this size to avoid breakage.
148 property int extensionSize: 0
150 // Setting extensionSize after the list has been populated because it has
151 // the potential to mess up with the intial positioning in combination
152 // with snapping to the center of the list. This catches all the cases
153 // where the item would be outside the list for more than itemHeight / 2.
154 // For the rest, give it a flick to scroll to the beginning. Note that
155 // the flicking alone isn't enough because in some cases it's not strong
156 // enough to overcome the snapping.
157 // https://bugreports.qt-project.org/browse/QTBUG-32251
158 Component.onCompleted: {
159 extensionSize = itemHeight * 3
160 flick(0, clickFlickSpeed)
163 // The height of the area where icons start getting folded
164 property int foldingStartHeight: itemHeight
165 // The height of the area where the items reach the final folding angle
166 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
167 property int itemWidth: width * .75
168 property int itemHeight: itemWidth * 15 / 16 + units.gu(1)
169 property int clickFlickSpeed: units.gu(60)
170 property int draggedIndex: dndArea.draggedIndex
171 property real realContentY: contentY - originY + topMargin
172 property int realItemHeight: itemHeight + spacing
174 // In case the start dragging transition is running, we need to delay the
175 // move because the displaced transition would clash with it and cause items
176 // to be moved to wrong places
177 property bool draggingTransitionRunning: false
178 property int scheduledMoveTo: -1
180 UbuntuNumberAnimation {
181 id: snapToBottomAnimation
182 target: launcherListView
184 to: launcherListView.originY + launcherListView.topMargin
187 UbuntuNumberAnimation {
188 id: snapToTopAnimation
189 target: launcherListView
191 to: launcherListView.contentHeight - launcherListView.height + launcherListView.originY - launcherListView.topMargin
194 UbuntuNumberAnimation {
196 objectName: "moveAnimation"
197 target: launcherListView
199 function moveTo(contentY) {
200 from = launcherListView.contentY;
205 function moveToIndex(index) {
206 var totalItemHeight = launcherListView.itemHeight + launcherListView.spacing
207 var itemPosition = index * totalItemHeight;
208 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
209 var distanceToEnd = index == 0 || index == launcherListView.count - 1 ? 0 : totalItemHeight
210 if (itemPosition + totalItemHeight + distanceToEnd > launcherListView.contentY + launcherListView.originY + launcherListView.topMargin + height) {
211 moveAnimation.moveTo(itemPosition + launcherListView.itemHeight - launcherListView.topMargin - height + distanceToEnd - launcherListView.originY);
212 } else if (itemPosition - distanceToEnd < launcherListView.contentY - launcherListView.originY + launcherListView.topMargin) {
213 moveAnimation.moveTo(itemPosition - distanceToEnd - launcherListView.topMargin + launcherListView.originY);
217 displaced: Transition {
218 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
221 delegate: FoldingLauncherDelegate {
223 objectName: "launcherDelegate" + index
224 // We need the appId in the delegate in order to find
225 // the right app when running autopilot tests for
227 readonly property string appId: model.appId
229 itemHeight: launcherListView.itemHeight
230 itemWidth: launcherListView.itemWidth
235 countVisible: model.countVisible
236 progress: model.progress
237 itemRunning: model.running
238 itemFocused: model.focused
239 inverted: root.inverted
240 alerting: model.alerting
241 highlighted: root.highlightIndex == index
242 shortcutHintShown: root.shortcutHintsShown && index <= 9
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 }
279 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
280 launcherListView.moveToIndex(index)
281 if (!dragging && launcher.state !== "visible") {
282 peekingAnimation.start()
286 if (launcherListView.peekingIndex === -1) {
287 launcherListView.peekingIndex = index
290 if (launcherListView.peekingIndex === index) {
291 launcherListView.peekingIndex = -1
298 objectName: "dropIndicator"
299 anchors.centerIn: parent
300 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
302 source: "graphics/divider-line.png"
308 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
310 target: launcherDelegate
318 target: launcherDelegate
323 target: dropIndicator
329 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
331 target: launcherDelegate
343 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
348 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
349 UbuntuNumberAnimation { properties: "angle,offset" }
354 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
355 UbuntuNumberAnimation { properties: "angle,offset" }
358 id: draggingTransition
361 SequentialAnimation {
362 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
364 UbuntuNumberAnimation { properties: "height" }
365 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
369 if (launcherListView.scheduledMoveTo > -1) {
370 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
371 dndArea.draggedIndex = launcherListView.scheduledMoveTo
372 launcherListView.scheduledMoveTo = -1
376 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
382 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
383 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
384 SequentialAnimation {
385 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
386 UbuntuNumberAnimation { properties: "height" }
387 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
388 PropertyAction { target: dndArea; property: "postDragging"; value: false }
389 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
397 objectName: "dndArea"
398 acceptedButtons: Qt.LeftButton | Qt.RightButton
401 topMargin: launcherListView.topMargin
402 bottomMargin: launcherListView.bottomMargin
404 drag.minimumY: -launcherListView.topMargin
405 drag.maximumY: height + launcherListView.bottomMargin
407 property int draggedIndex: -1
408 property var selectedItem
409 property bool preDragging: false
410 property bool dragging: !!selectedItem && selectedItem.dragging
411 property bool postDragging: false
419 function processPress(mouse) {
420 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
424 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
425 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
427 // Check if we actually clicked an item or only at the spacing in between
428 if (clickedItem === null) {
432 if (mouse.button & Qt.RightButton) { // context menu
434 quickList.open(index);
440 // First/last item do the scrolling at more than 12 degrees
441 if (index == 0 || index == launcherListView.count - 1) {
442 if (clickedItem.angle > 12 || clickedItem.angle < -12) {
443 launcherListView.moveToIndex(index);
445 root.applicationSelected(LauncherModel.get(index).appId);
450 // the rest launches apps up to an angle of 30 degrees
451 if (clickedItem.angle > 30 || clickedItem.angle < -30) {
452 launcherListView.moveToIndex(index);
454 root.applicationSelected(LauncherModel.get(index).appId);
466 function endDrag(dragItem) {
467 var droppedIndex = draggedIndex;
478 selectedItem.dragging = false;
479 selectedItem = undefined;
482 dragItem.target = undefined
484 progressiveScrollingTimer.stop();
485 launcherListView.interactive = true;
486 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
487 snapToBottomAnimation.start();
488 } else if (droppedIndex < 2 && postDragging) {
489 snapToTopAnimation.start();
494 processPressAndHold(mouse, drag);
497 function processPressAndHold(mouse, dragItem) {
498 if (Math.abs(selectedItem.angle) > 30) {
504 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
506 quickList.open(draggedIndex)
508 launcherListView.interactive = false
510 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
512 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
513 fakeDragItem.x = units.gu(0.5)
514 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
515 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
516 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
517 fakeDragItem.count = LauncherModel.get(draggedIndex).count
518 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
519 fakeDragItem.flatten()
520 dragItem.target = fakeDragItem
527 processPositionChanged(mouse)
530 function processPositionChanged(mouse) {
531 if (draggedIndex >= 0) {
532 if (!selectedItem.dragging) {
533 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
534 if (!preDragging && distance > units.gu(1.5)) {
536 quickList.state = "";
538 if (distance > launcherListView.itemHeight) {
539 selectedItem.dragging = true
543 if (!selectedItem.dragging) {
547 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
549 // Move it down by the the missing size to compensate index calculation with only expanded items
550 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
552 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
553 progressiveScrollingTimer.downwards = false
554 progressiveScrollingTimer.start()
555 } else if (mouseY < launcherListView.realItemHeight) {
556 progressiveScrollingTimer.downwards = true
557 progressiveScrollingTimer.start()
559 progressiveScrollingTimer.stop()
562 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
564 if (newIndex > draggedIndex + 1) {
565 newIndex = draggedIndex + 1
566 } else if (newIndex < draggedIndex) {
567 newIndex = draggedIndex -1
572 if (newIndex >= 0 && newIndex < launcherListView.count) {
573 if (launcherListView.draggingTransitionRunning) {
574 launcherListView.scheduledMoveTo = newIndex
576 launcherListView.model.move(draggedIndex, newIndex)
577 draggedIndex = newIndex
584 id: progressiveScrollingTimer
588 property bool downwards: true
591 var minY = -launcherListView.topMargin
592 if (launcherListView.contentY > minY) {
593 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
596 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
597 if (launcherListView.contentY < maxY) {
598 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
608 objectName: "fakeDragItem"
609 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
610 itemWidth: launcherListView.itemWidth
611 itemHeight: launcherListView.itemHeight
614 rotation: root.rotation
616 onVisibleChanged: if (!visible) iconName = "";
619 fakeDragItemAnimation.start();
622 UbuntuNumberAnimation {
623 id: fakeDragItemAnimation
624 target: fakeDragItem;
625 properties: "angle,offset";
634 objectName: "quickListShape"
635 anchors.fill: quickList
636 opacity: quickList.state === "open" ? 0.8 : 0
638 rotation: root.rotation
640 Behavior on opacity {
641 UbuntuNumberAnimation {}
649 rightMargin: -units.dp(4)
650 verticalCenter: parent.verticalCenter
651 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
655 source: "graphics/quicklist_tooltip.png"
661 anchors.fill: quickListShape
662 enabled: quickList.state == "open" || pressed
665 quickList.state = "";
666 quickList.focus = false;
667 root.kbdNavigationCancelled();
670 // Forward for dragging to work when quickList is open
673 var m = mapToItem(dndArea, mouseX, mouseY)
674 dndArea.processPress(m)
678 var m = mapToItem(dndArea, mouseX, mouseY)
679 dndArea.processPressAndHold(m, drag)
683 var m = mapToItem(dndArea, mouseX, mouseY)
684 dndArea.processPositionChanged(m)
688 dndArea.endDrag(drag);
692 dndArea.endDrag(drag);
698 objectName: "quickList"
700 // Because we're setting left/right anchors depending on orientation, it will break the
701 // width setting after rotating twice. This makes sure we also re-apply width on rotation
702 width: root.inverted ? units.gu(30) : units.gu(30)
703 height: quickListColumn.height
704 visible: quickListShape.visible
706 left: root.inverted ? undefined : parent.right
707 right: root.inverted ? parent.left : undefined
710 y: itemCenter - (height / 2) + offset
711 rotation: root.rotation
714 property string appId
716 property int selectedIndex: -1
722 if (selectedIndex >= popoverRepeater.count) {
725 event.accepted = true;
729 if (selectedIndex < 0) {
730 selectedIndex = popoverRepeater.count - 1;
732 event.accepted = true;
736 quickList.selectedIndex = -1;
737 quickList.focus = false;
739 event.accepted = true;
744 if (quickList.selectedIndex >= 0) {
745 LauncherModel.quickListActionInvoked(quickList.appId, quickList.selectedIndex)
747 quickList.selectedIndex = -1;
748 quickList.focus = false;
750 root.kbdNavigationCancelled();
751 event.accepted = true;
757 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) + quickList.item.offset : units.gu(1)
758 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
759 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
761 function open(index) {
762 var itemPosition = index * launcherListView.itemHeight;
763 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
764 item = launcherListView.itemAt(launcherListView.width / 2, itemPosition + launcherListView.itemHeight / 2);
765 quickList.model = launcherListView.model.get(index).quickList;
766 quickList.appId = launcherListView.model.get(index).appId;
767 quickList.state = "open";
773 height: childrenRect.height
777 model: quickList.model
780 objectName: "quickListEntry" + index
781 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
782 highlightWhenPressed: model.clickable
783 selected: index === quickList.selectedIndex
785 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
786 // ListItems don't know that they are sitting in a themed Popover where the color
787 // needs to be inverted.
788 __foregroundColor: "black"
791 if (!model.clickable) {
795 quickList.state = "";
796 // Unsetting model to prevent showing changing entries during fading out
797 // that may happen because of triggering an action.
798 LauncherModel.quickListActionInvoked(quickList.appId, index);
799 quickList.focus = false;
800 root.kbdNavigationCancelled();
801 quickList.model = undefined;