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
244 property bool dragging: false
246 SequentialAnimation {
250 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 1 : 0 }
251 PropertyAction { target: launcherListViewItem; property: "clip"; value: 0 }
253 UbuntuNumberAnimation {
254 target: launcherDelegate
258 to: (units.gu(.5) + launcherListView.width * .5) * (root.inverted ? -1 : 1)
259 duration: UbuntuAnimation.BriskDuration
263 UbuntuNumberAnimation {
264 target: launcherDelegate
269 duration: UbuntuAnimation.BriskDuration
272 PropertyAction { target: launcherListViewItem; property: "clip"; value: 1 }
273 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 0 : 1 }
278 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
279 launcherListView.moveToIndex(index)
280 if (!dragging && launcher.state !== "visible") {
281 peekingAnimation.start()
285 if (launcherListView.peekingIndex === -1) {
286 launcherListView.peekingIndex = index
289 if (launcherListView.peekingIndex === index) {
290 launcherListView.peekingIndex = -1
297 objectName: "dropIndicator"
298 anchors.centerIn: parent
299 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
301 source: "graphics/divider-line.png"
307 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
309 target: launcherDelegate
317 target: launcherDelegate
322 target: dropIndicator
328 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
330 target: launcherDelegate
342 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
347 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
348 UbuntuNumberAnimation { properties: "angle,offset" }
353 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
354 UbuntuNumberAnimation { properties: "angle,offset" }
357 id: draggingTransition
360 SequentialAnimation {
361 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
363 UbuntuNumberAnimation { properties: "height" }
364 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
368 if (launcherListView.scheduledMoveTo > -1) {
369 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
370 dndArea.draggedIndex = launcherListView.scheduledMoveTo
371 launcherListView.scheduledMoveTo = -1
375 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
381 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
382 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
383 SequentialAnimation {
384 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
385 UbuntuNumberAnimation { properties: "height" }
386 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
387 PropertyAction { target: dndArea; property: "postDragging"; value: false }
388 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
396 objectName: "dndArea"
397 acceptedButtons: Qt.LeftButton | Qt.RightButton
400 topMargin: launcherListView.topMargin
401 bottomMargin: launcherListView.bottomMargin
403 drag.minimumY: -launcherListView.topMargin
404 drag.maximumY: height + launcherListView.bottomMargin
406 property int draggedIndex: -1
407 property var selectedItem
408 property bool preDragging: false
409 property bool dragging: !!selectedItem && selectedItem.dragging
410 property bool postDragging: false
418 function processPress(mouse) {
419 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
423 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
424 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
426 // Check if we actually clicked an item or only at the spacing in between
427 if (clickedItem === null) {
431 if (mouse.button & Qt.RightButton) { // context menu
433 quickList.open(index);
439 // First/last item do the scrolling at more than 12 degrees
440 if (index == 0 || index == launcherListView.count - 1) {
441 if (clickedItem.angle > 12 || clickedItem.angle < -12) {
442 launcherListView.moveToIndex(index);
444 root.applicationSelected(LauncherModel.get(index).appId);
449 // the rest launches apps up to an angle of 30 degrees
450 if (clickedItem.angle > 30 || clickedItem.angle < -30) {
451 launcherListView.moveToIndex(index);
453 root.applicationSelected(LauncherModel.get(index).appId);
465 function endDrag(dragItem) {
466 var droppedIndex = draggedIndex;
477 selectedItem.dragging = false;
478 selectedItem = undefined;
481 dragItem.target = undefined
483 progressiveScrollingTimer.stop();
484 launcherListView.interactive = true;
485 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
486 snapToBottomAnimation.start();
487 } else if (droppedIndex < 2 && postDragging) {
488 snapToTopAnimation.start();
493 processPressAndHold(mouse, drag);
496 function processPressAndHold(mouse, dragItem) {
497 if (Math.abs(selectedItem.angle) > 30) {
503 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
505 quickList.open(draggedIndex)
507 launcherListView.interactive = false
509 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
511 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
512 fakeDragItem.x = units.gu(0.5)
513 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
514 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
515 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
516 fakeDragItem.count = LauncherModel.get(draggedIndex).count
517 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
518 fakeDragItem.flatten()
519 dragItem.target = fakeDragItem
526 processPositionChanged(mouse)
529 function processPositionChanged(mouse) {
530 if (draggedIndex >= 0) {
531 if (!selectedItem.dragging) {
532 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
533 if (!preDragging && distance > units.gu(1.5)) {
535 quickList.state = "";
537 if (distance > launcherListView.itemHeight) {
538 selectedItem.dragging = true
542 if (!selectedItem.dragging) {
546 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
548 // Move it down by the the missing size to compensate index calculation with only expanded items
549 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
551 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
552 progressiveScrollingTimer.downwards = false
553 progressiveScrollingTimer.start()
554 } else if (mouseY < launcherListView.realItemHeight) {
555 progressiveScrollingTimer.downwards = true
556 progressiveScrollingTimer.start()
558 progressiveScrollingTimer.stop()
561 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
563 if (newIndex > draggedIndex + 1) {
564 newIndex = draggedIndex + 1
565 } else if (newIndex < draggedIndex) {
566 newIndex = draggedIndex -1
571 if (newIndex >= 0 && newIndex < launcherListView.count) {
572 if (launcherListView.draggingTransitionRunning) {
573 launcherListView.scheduledMoveTo = newIndex
575 launcherListView.model.move(draggedIndex, newIndex)
576 draggedIndex = newIndex
583 id: progressiveScrollingTimer
587 property bool downwards: true
590 var minY = -launcherListView.topMargin
591 if (launcherListView.contentY > minY) {
592 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
595 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
596 if (launcherListView.contentY < maxY) {
597 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
607 objectName: "fakeDragItem"
608 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
609 itemWidth: launcherListView.itemWidth
610 itemHeight: launcherListView.itemHeight
613 rotation: root.rotation
615 onVisibleChanged: if (!visible) iconName = "";
618 fakeDragItemAnimation.start();
621 UbuntuNumberAnimation {
622 id: fakeDragItemAnimation
623 target: fakeDragItem;
624 properties: "angle,offset";
633 objectName: "quickListShape"
634 anchors.fill: quickList
635 opacity: quickList.state === "open" ? 0.95 : 0
637 rotation: root.rotation
638 aspect: UbuntuShape.Flat
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"
699 color: theme.palette.normal.background
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";
772 height: quickListColumn.height
777 height: childrenRect.height
781 model: quickList.model
784 objectName: "quickListEntry" + index
785 selected: index === quickList.selectedIndex
786 height: label.implicitHeight + label.anchors.topMargin + label.anchors.bottomMargin
787 color: model.clickable ? (selected ? theme.palette.highlighted.background : "transparent") : theme.palette.disabled.background
788 highlightColor: !model.clickable ? quickList.color : undefined // make disabled items visually unclickable
789 divider.colorFrom: UbuntuColors.inkstone
790 divider.colorTo: UbuntuColors.inkstone
795 anchors.leftMargin: units.gu(3) // 2 GU for checkmark, 3 GU total
796 anchors.rightMargin: units.gu(2)
797 anchors.topMargin: units.gu(2)
798 anchors.bottomMargin: units.gu(2)
799 verticalAlignment: Label.AlignVCenter
801 fontSize: index == 0 ? "medium" : "small"
802 font.weight: index == 0 ? Font.Medium : Font.Light
803 color: model.clickable ? theme.palette.normal.backgroundText : theme.palette.disabled.backgroundText
807 if (!model.clickable) {
811 quickList.state = "";
812 // Unsetting model to prevent showing changing entries during fading out
813 // that may happen because of triggering an action.
814 LauncherModel.quickListActionInvoked(quickList.appId, index);
815 quickList.focus = false;
816 root.kbdNavigationCancelled();
817 quickList.model = undefined;