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 QtQml.StateMachine 1.0 as DSM
19 import Ubuntu.Components 1.3
20 import Unity.Launcher 0.1
21 import Ubuntu.Components.Popups 1.3
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 || dndArea.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
73 onWheel: wheel.accepted = true;
83 objectName: "buttonShowDashHome"
86 color: UbuntuColors.orange
87 readonly property bool highlighted: root.highlightIndex == -1;
90 objectName: "dashItem"
91 width: parent.width * .6
93 anchors.centerIn: parent
94 source: "graphics/home.png"
95 rotation: root.rotation
100 activeFocusOnPress: false
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
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 height: visible ? units.dp(2) : 0
304 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
306 source: "graphics/divider-line.png"
312 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
314 target: launcherDelegate
322 target: launcherDelegate
327 target: dropIndicator
333 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
335 target: launcherDelegate
347 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
352 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
353 UbuntuNumberAnimation { properties: "angle,offset" }
358 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
359 UbuntuNumberAnimation { properties: "angle,offset" }
362 id: draggingTransition
365 SequentialAnimation {
366 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
368 UbuntuNumberAnimation { properties: "height" }
369 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
373 if (launcherListView.scheduledMoveTo > -1) {
374 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
375 dndArea.draggedIndex = launcherListView.scheduledMoveTo
376 launcherListView.scheduledMoveTo = -1
380 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
386 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
387 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
388 SequentialAnimation {
389 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
390 UbuntuNumberAnimation { properties: "height" }
391 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
392 PropertyAction { target: dndArea; property: "postDragging"; value: false }
393 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
401 objectName: "dndArea"
402 acceptedButtons: Qt.LeftButton | Qt.RightButton
406 topMargin: launcherListView.topMargin
407 bottomMargin: launcherListView.bottomMargin
409 drag.minimumY: -launcherListView.topMargin
410 drag.maximumY: height + launcherListView.bottomMargin
412 property int draggedIndex: -1
413 property var selectedItem
414 property bool preDragging: false
415 property bool dragging: !!selectedItem && selectedItem.dragging
416 property bool postDragging: false
424 function processPress(mouse) {
425 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
429 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
430 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
432 // Check if we actually clicked an item or only at the spacing in between
433 if (clickedItem === null) {
437 if (mouse.button & Qt.RightButton) { // context menu
439 quickList.open(index);
445 // First/last item do the scrolling at more than 12 degrees
446 if (index == 0 || index == launcherListView.count - 1) {
447 if (clickedItem.angle > 12 || clickedItem.angle < -12) {
448 launcherListView.moveToIndex(index);
450 root.applicationSelected(LauncherModel.get(index).appId);
455 // the rest launches apps up to an angle of 30 degrees
456 if (clickedItem.angle > 30 || clickedItem.angle < -30) {
457 launcherListView.moveToIndex(index);
459 root.applicationSelected(LauncherModel.get(index).appId);
471 function endDrag(dragItem) {
472 var droppedIndex = draggedIndex;
483 selectedItem.dragging = false;
484 selectedItem = undefined;
487 dragItem.target = undefined
489 progressiveScrollingTimer.stop();
490 launcherListView.interactive = true;
491 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
492 snapToBottomAnimation.start();
493 } else if (droppedIndex < 2 && postDragging) {
494 snapToTopAnimation.start();
499 processPressAndHold(mouse, drag);
502 function processPressAndHold(mouse, dragItem) {
503 if (Math.abs(selectedItem.angle) > 30) {
509 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
511 quickList.open(draggedIndex)
513 launcherListView.interactive = false
515 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
517 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
518 fakeDragItem.x = units.gu(0.5)
519 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
520 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
521 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
522 fakeDragItem.count = LauncherModel.get(draggedIndex).count
523 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
524 fakeDragItem.flatten()
525 dragItem.target = fakeDragItem
532 processPositionChanged(mouse)
535 function processPositionChanged(mouse) {
536 if (draggedIndex >= 0) {
537 if (!selectedItem.dragging) {
538 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
539 if (!preDragging && distance > units.gu(1.5)) {
541 quickList.state = "";
543 if (distance > launcherListView.itemHeight) {
544 selectedItem.dragging = true
548 if (!selectedItem.dragging) {
552 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
554 // Move it down by the the missing size to compensate index calculation with only expanded items
555 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
557 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
558 progressiveScrollingTimer.downwards = false
559 progressiveScrollingTimer.start()
560 } else if (mouseY < launcherListView.realItemHeight) {
561 progressiveScrollingTimer.downwards = true
562 progressiveScrollingTimer.start()
564 progressiveScrollingTimer.stop()
567 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
569 if (newIndex > draggedIndex + 1) {
570 newIndex = draggedIndex + 1
571 } else if (newIndex < draggedIndex) {
572 newIndex = draggedIndex -1
577 if (newIndex >= 0 && newIndex < launcherListView.count) {
578 if (launcherListView.draggingTransitionRunning) {
579 launcherListView.scheduledMoveTo = newIndex
581 launcherListView.model.move(draggedIndex, newIndex)
582 draggedIndex = newIndex
589 id: progressiveScrollingTimer
593 property bool downwards: true
596 var minY = -launcherListView.topMargin
597 if (launcherListView.contentY > minY) {
598 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
601 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
602 if (launcherListView.contentY < maxY) {
603 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
613 objectName: "fakeDragItem"
614 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
615 itemWidth: launcherListView.itemWidth
616 itemHeight: launcherListView.itemHeight
619 rotation: root.rotation
621 onVisibleChanged: if (!visible) iconName = "";
624 fakeDragItemAnimation.start();
627 UbuntuNumberAnimation {
628 id: fakeDragItemAnimation
629 target: fakeDragItem;
630 properties: "angle,offset";
639 objectName: "quickListShape"
640 anchors.fill: quickList
641 opacity: quickList.state === "open" ? 0.95 : 0
643 rotation: root.rotation
644 aspect: UbuntuShape.Flat
646 Behavior on opacity {
647 UbuntuNumberAnimation {}
650 source: ShaderEffectSource {
651 sourceItem: quickList
658 rightMargin: -units.dp(4)
659 verticalCenter: parent.verticalCenter
660 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
664 source: "graphics/quicklist_tooltip.png"
670 anchors.fill: quickListShape
671 enabled: quickList.state == "open" || pressed
674 quickList.state = "";
675 quickList.focus = false;
676 root.kbdNavigationCancelled();
679 // Forward for dragging to work when quickList is open
682 var m = mapToItem(dndArea, mouseX, mouseY)
683 dndArea.processPress(m)
687 var m = mapToItem(dndArea, mouseX, mouseY)
688 dndArea.processPressAndHold(m, drag)
692 var m = mapToItem(dndArea, mouseX, mouseY)
693 dndArea.processPositionChanged(m)
697 dndArea.endDrag(drag);
701 dndArea.endDrag(drag);
707 objectName: "quickList"
708 color: theme.palette.normal.background
709 // Because we're setting left/right anchors depending on orientation, it will break the
710 // width setting after rotating twice. This makes sure we also re-apply width on rotation
711 width: root.inverted ? units.gu(30) : units.gu(30)
712 height: quickListColumn.height
713 visible: quickListShape.visible
715 left: root.inverted ? undefined : parent.right
716 right: root.inverted ? parent.left : undefined
719 y: itemCenter - (height / 2) + offset
720 rotation: root.rotation
723 property string appId
725 property int selectedIndex: -1
731 if (selectedIndex >= popoverRepeater.count) {
734 event.accepted = true;
738 if (selectedIndex < 0) {
739 selectedIndex = popoverRepeater.count - 1;
741 event.accepted = true;
745 quickList.selectedIndex = -1;
746 quickList.focus = false;
748 event.accepted = true;
753 if (quickList.selectedIndex >= 0) {
754 LauncherModel.quickListActionInvoked(quickList.appId, quickList.selectedIndex)
756 quickList.selectedIndex = -1;
757 quickList.focus = false;
759 root.kbdNavigationCancelled();
760 event.accepted = true;
766 property int itemCenter: item ? root.mapFromItem(quickList.item, 0, 0).y + (item.height / 2) + quickList.item.offset : units.gu(1)
767 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
768 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
770 function open(index) {
771 var itemPosition = index * launcherListView.itemHeight;
772 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
773 item = launcherListView.itemAt(launcherListView.width / 2, itemPosition + launcherListView.itemHeight / 2);
774 quickList.model = launcherListView.model.get(index).quickList;
775 quickList.appId = launcherListView.model.get(index).appId;
776 quickList.state = "open";
781 height: quickListColumn.height
786 height: childrenRect.height
790 model: quickList.model
793 objectName: "quickListEntry" + index
794 selected: index === quickList.selectedIndex
795 height: label.implicitHeight + label.anchors.topMargin + label.anchors.bottomMargin
796 color: model.clickable ? (selected ? theme.palette.highlighted.background : "transparent") : theme.palette.disabled.background
797 highlightColor: !model.clickable ? quickList.color : undefined // make disabled items visually unclickable
798 divider.colorFrom: UbuntuColors.inkstone
799 divider.colorTo: UbuntuColors.inkstone
800 divider.visible: model.hasSeparator
805 anchors.leftMargin: units.gu(3) // 2 GU for checkmark, 3 GU total
806 anchors.rightMargin: units.gu(2)
807 anchors.topMargin: units.gu(2)
808 anchors.bottomMargin: units.gu(2)
809 verticalAlignment: Label.AlignVCenter
811 fontSize: index == 0 ? "medium" : "small"
812 font.weight: index == 0 ? Font.Medium : Font.Light
813 color: model.clickable ? theme.palette.normal.backgroundText : theme.palette.disabled.backgroundText
817 if (!model.clickable) {
821 quickList.state = "";
822 // Unsetting model to prevent showing changing entries during fading out
823 // that may happen because of triggering an action.
824 LauncherModel.quickListActionInvoked(quickList.appId, index);
825 quickList.focus = false;
826 root.kbdNavigationCancelled();
827 quickList.model = undefined;
837 objectName: "tooltipShape"
839 visible: tooltipShownState.active
840 rotation: root.rotation
841 y: itemCenter - (height / 2)
844 left: root.inverted ? undefined : parent.right
845 right: root.inverted ? parent.left : undefined
849 readonly property var hoveredItem: dndArea.containsMouse ? launcherListView.itemAt(dndArea.mouseX, dndArea.mouseY + launcherListView.realContentY) : null
850 readonly property int itemCenter: !hoveredItem ? 0 : root.mapFromItem(hoveredItem, 0, 0).y + (hoveredItem.height / 2) + hoveredItem.offset
852 text: !hoveredItem ? "" : hoveredItem.name
856 id: tooltipStateMachine
857 initialState: tooltipHiddenState
861 id: tooltipHiddenState
863 DSM.SignalTransition {
864 targetState: tooltipShownState
865 signal: tooltipShape.hoveredItemChanged
866 // !dndArea.pressed allows us to filter out touch input events
867 guard: tooltipShape.hoveredItem !== null && !dndArea.pressed && !root.moving
872 id: tooltipShownState
874 DSM.SignalTransition {
875 targetState: tooltipHiddenState
876 signal: tooltipShape.hoveredItemChanged
877 guard: tooltipShape.hoveredItem === null
880 DSM.SignalTransition {
881 targetState: tooltipDismissedState
882 signal: dndArea.onPressed
885 DSM.SignalTransition {
886 targetState: tooltipDismissedState
887 signal: quickList.stateChanged
888 guard: quickList.state === "open"
893 id: tooltipDismissedState
895 DSM.SignalTransition {
896 targetState: tooltipHiddenState
897 signal: dndArea.positionChanged
898 guard: quickList.state != "open" && !dndArea.pressed && !dndArea.moving
901 DSM.SignalTransition {
902 targetState: tooltipHiddenState
903 signal: dndArea.exited
904 guard: quickList.state != "open"