on 157e33e help sign in
unity8/qml/Launcher/LauncherPanel.qml
733 lines 34 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 */
17import QtQuick 2.4
18import Ubuntu.Components 1.3
19import Ubuntu.Components.ListItems 1.3 as ListItems
20import Unity.Launcher 0.1
21import Ubuntu.Components.Popups 1.3
22import "../Components/ListItems"
23import "../Components/"
25Rectangle {
26 id: root
27 color: "#B2000000"
29 rotation: inverted ? 180 : 0
31 property var model
32 property bool inverted: false
33 property bool dragging: false
34 property bool moving: launcherListView.moving || launcherListView.flicking
35 property bool preventHiding: moving || dndArea.draggedIndex >= 0 || quickList.state === "open" || dndArea.pressed
36 || mouseEventEater.containsMouse || dashItem.hovered
37 property int highlightIndex: -1
39 signal applicationSelected(string appId)
40 signal showDashHome()
42 onXChanged: {
43 if (quickList.state == "open") {
44 quickList.state = ""
45 }
46 }
48 MouseArea {
49 id: mouseEventEater
50 anchors.fill: parent
51 hoverEnabled: true
52 }
54 Column {
55 id: mainColumn
56 anchors {
57 fill: parent
58 }
60 Item {
61 objectName: "buttonShowDashHome"
62 width: parent.width
63 height: units.gu(7)
64 clip: true
66 UbuntuShape {
67 anchors {
68 fill: parent
69 topMargin: -units.gu(2)
70 }
71 aspect: UbuntuShape.Flat
72 backgroundColor: UbuntuColors.orange
73 }
75 Image {
76 objectName: "dashItem"
77 width: units.gu(5)
78 height: width
79 anchors.centerIn: parent
80 source: "graphics/home.png"
81 rotation: root.rotation
82 }
83 AbstractButton {
84 id: dashItem
85 anchors.fill: parent
86 onClicked: root.showDashHome()
87 }
88 }
90 Item {
91 anchors.left: parent.left
92 anchors.right: parent.right
93 height: parent.height - dashItem.height - parent.spacing*2
95 Item {
96 id: launcherListViewItem
97 anchors.fill: parent
98 clip: true
100 ListView {
101 id: launcherListView
102 objectName: "launcherListView"
103 anchors {
104 fill: parent
105 topMargin: -extensionSize + units.gu(0.5)
106 bottomMargin: -extensionSize + units.gu(1)
107 leftMargin: units.gu(0.5)
108 rightMargin: units.gu(0.5)
109 }
110 topMargin: extensionSize
111 bottomMargin: extensionSize
112 height: parent.height - dashItem.height - parent.spacing*2
113 model: root.model
114 cacheBuffer: itemHeight * 3
115 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
116 highlightRangeMode: ListView.ApplyRange
117 preferredHighlightBegin: (height - itemHeight) / 2
118 preferredHighlightEnd: (height + itemHeight) / 2
120 // for the single peeking icon, when alert-state is set on delegate
121 property int peekingIndex: -1
123 // The size of the area the ListView is extended to make sure items are not
124 // destroyed when dragging them outside the list. This needs to be at least
125 // itemHeight to prevent folded items from disappearing and DragArea limits
126 // need to be smaller than this size to avoid breakage.
127 property int extensionSize: 0
129 // Setting extensionSize after the list has been populated because it has
130 // the potential to mess up with the intial positioning in combination
131 // with snapping to the center of the list. This catches all the cases
132 // where the item would be outside the list for more than itemHeight / 2.
133 // For the rest, give it a flick to scroll to the beginning. Note that
134 // the flicking alone isn't enough because in some cases it's not strong
135 // enough to overcome the snapping.
136 // https://bugreports.qt-project.org/browse/QTBUG-32251
137 Component.onCompleted: {
138 extensionSize = itemHeight * 3
139 flick(0, clickFlickSpeed)
140 }
142 // The height of the area where icons start getting folded
143 property int foldingStartHeight: units.gu(6.5)
144 // The height of the area where the items reach the final folding angle
145 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
146 property int itemWidth: units.gu(7)
147 property int itemHeight: units.gu(6.5)
148 property int clickFlickSpeed: units.gu(60)
149 property int draggedIndex: dndArea.draggedIndex
150 property real realContentY: contentY - originY + topMargin
151 property int realItemHeight: itemHeight + spacing
153 // In case the start dragging transition is running, we need to delay the
154 // move because the displaced transition would clash with it and cause items
155 // to be moved to wrong places
156 property bool draggingTransitionRunning: false
157 property int scheduledMoveTo: -1
159 UbuntuNumberAnimation {
160 id: snapToBottomAnimation
161 target: launcherListView
162 property: "contentY"
163 to: launcherListView.originY + launcherListView.topMargin
164 }
166 UbuntuNumberAnimation {
167 id: snapToTopAnimation
168 target: launcherListView
169 property: "contentY"
170 to: launcherListView.contentHeight - launcherListView.height + launcherListView.originY - launcherListView.topMargin
171 }
173 UbuntuNumberAnimation {
174 id: moveAnimation
175 target: launcherListView
176 property: "contentY"
177 function moveTo(contentY) {
178 from = launcherListView.contentY;
179 to = contentY;
180 start();
181 }
182 }
184 displaced: Transition {
185 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
186 }
188 delegate: FoldingLauncherDelegate {
189 id: launcherDelegate
190 objectName: "launcherDelegate" + index
191 // We need the appId in the delegate in order to find
192 // the right app when running autopilot tests for
193 // multiple apps.
194 readonly property string appId: model.appId
195 itemHeight: launcherListView.itemHeight
196 itemWidth: launcherListView.itemWidth
197 width: itemWidth
198 height: itemHeight
199 iconName: model.icon
200 count: model.count
201 countVisible: model.countVisible
202 progress: model.progress
203 itemRunning: model.running
204 itemFocused: model.focused
205 inverted: root.inverted
206 alerting: model.alerting
207 z: -Math.abs(offset)
208 maxAngle: 55
209 property bool dragging: false
211 SequentialAnimation {
212 id: peekingAnimation
214 // revealing
215 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 1 : 0 }
216 PropertyAction { target: launcherListViewItem; property: "clip"; value: 0 }
218 UbuntuNumberAnimation {
219 target: launcherDelegate
220 alwaysRunToEnd: true
221 loops: 1
222 properties: "x"
223 to: (units.gu(.5) + launcherListView.width * .5) * (root.inverted ? -1 : 1)
224 duration: UbuntuAnimation.BriskDuration
225 }
227 // hiding
228 UbuntuNumberAnimation {
229 target: launcherDelegate
230 alwaysRunToEnd: true
231 loops: 1
232 properties: "x"
233 to: 0
234 duration: UbuntuAnimation.BriskDuration
235 }
237 PropertyAction { target: launcherListViewItem; property: "clip"; value: 1 }
238 PropertyAction { target: root; property: "visible"; value: (launcher.visibleWidth === 0) ? 0 : 1 }
239 }
241 onAlertingChanged: {
242 if(alerting) {
243 if (!dragging && (launcherListView.peekingIndex === -1 || launcher.visibleWidth > 0)) {
244 var itemPosition = index * launcherListView.itemHeight;
245 var height = launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin
246 var distanceToEnd = index == 0 || index == launcherListView.count - 1 ? 0 : launcherListView.itemHeight
247 if (itemPosition + launcherListView.itemHeight + distanceToEnd > launcherListView.contentY + launcherListView.topMargin + height) {
248 moveAnimation.moveTo(itemPosition + launcherListView.itemHeight - launcherListView.topMargin - height + distanceToEnd);
249 } else if (itemPosition - distanceToEnd < launcherListView.contentY + launcherListView.topMargin) {
250 moveAnimation.moveTo(itemPosition - distanceToEnd - launcherListView.topMargin);
251 }
252 if (!dragging && launcher.state !== "visible") {
253 peekingAnimation.start()
254 }
255 }
257 if (launcherListView.peekingIndex === -1) {
258 launcherListView.peekingIndex = index
259 }
260 } else {
261 if (launcherListView.peekingIndex === index) {
262 launcherListView.peekingIndex = -1
263 }
264 }
265 }
267 ThinDivider {
268 id: dropIndicator
269 objectName: "dropIndicator"
270 anchors.centerIn: parent
271 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
272 opacity: 0
273 source: "graphics/divider-line.png"
274 }
276 states: [
277 State {
278 name: "selected"
279 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
280 PropertyChanges {
281 target: launcherDelegate
282 itemOpacity: 0
283 }
284 },
285 State {
286 name: "dragging"
287 when: dragging
288 PropertyChanges {
289 target: launcherDelegate
290 height: units.gu(1)
291 itemOpacity: 0
292 }
293 PropertyChanges {
294 target: dropIndicator
295 opacity: 1
296 }
297 },
298 State {
299 name: "expanded"
300 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
301 PropertyChanges {
302 target: launcherDelegate
303 angle: 0
304 offset: 0
305 itemOpacity: 0.6
306 }
307 }
308 ]
310 transitions: [
311 Transition {
312 from: ""
313 to: "selected"
314 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
315 },
316 Transition {
317 from: "*"
318 to: "expanded"
319 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
320 UbuntuNumberAnimation { properties: "angle,offset" }
321 },
322 Transition {
323 from: "expanded"
324 to: ""
325 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
326 UbuntuNumberAnimation { properties: "angle,offset" }
327 },
328 Transition {
329 id: draggingTransition
330 from: "selected"
331 to: "dragging"
332 SequentialAnimation {
333 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
334 ParallelAnimation {
335 UbuntuNumberAnimation { properties: "height" }
336 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
337 }
338 ScriptAction {
339 script: {
340 if (launcherListView.scheduledMoveTo > -1) {
341 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
342 dndArea.draggedIndex = launcherListView.scheduledMoveTo
343 launcherListView.scheduledMoveTo = -1
344 }
345 }
346 }
347 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
348 }
349 },
350 Transition {
351 from: "dragging"
352 to: "*"
353 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
354 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
355 SequentialAnimation {
356 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
357 UbuntuNumberAnimation { properties: "height" }
358 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
359 PropertyAction { target: dndArea; property: "postDragging"; value: false }
360 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
361 }
362 }
363 ]
364 }
366 MouseArea {
367 id: dndArea
368 objectName: "dndArea"
369 acceptedButtons: Qt.LeftButton | Qt.RightButton
370 anchors {
371 fill: parent
372 topMargin: launcherListView.topMargin
373 bottomMargin: launcherListView.bottomMargin
374 }
375 drag.minimumY: -launcherListView.topMargin
376 drag.maximumY: height + launcherListView.bottomMargin
378 property int draggedIndex: -1
379 property var selectedItem
380 property bool preDragging: false
381 property bool dragging: !!selectedItem && selectedItem.dragging
382 property bool postDragging: false
383 property int startX
384 property int startY
386 onPressed: {
387 processPress(mouse);
388 }
390 function processPress(mouse) {
391 selectedItem = launcherListView.itemAt(mouse.x, mouse.y + launcherListView.realContentY)
392 }
394 onClicked: {
395 Haptics.play();
396 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
397 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
399 // Check if we actually clicked an item or only at the spacing in between
400 if (clickedItem === null) {
401 return;
402 }
404 if (mouse.button & Qt.RightButton) { // context menu
405 // Opening QuickList
406 quickList.item = clickedItem;
407 quickList.model = launcherListView.model.get(index).quickList;
408 quickList.appId = launcherListView.model.get(index).appId;
409 quickList.state = "open";
410 return
411 }
413 // First/last item do the scrolling at more than 12 degrees
414 if (index == 0 || index == launcherListView.count - 1) {
415 if (clickedItem.angle > 12) {
416 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
417 } else if (clickedItem.angle < -12) {
418 launcherListView.flick(0, launcherListView.clickFlickSpeed);
419 } else {
420 root.applicationSelected(LauncherModel.get(index).appId);
421 }
422 return;
423 }
425 // the rest launches apps up to an angle of 30 degrees
426 if (clickedItem.angle > 30) {
427 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
428 } else if (clickedItem.angle < -30) {
429 launcherListView.flick(0, launcherListView.clickFlickSpeed);
430 } else {
431 root.applicationSelected(LauncherModel.get(index).appId);
432 }
433 }
435 onCanceled: {
436 endDrag(drag);
437 }
439 onReleased: {
440 endDrag(drag);
441 }
443 function endDrag(dragItem) {
444 var droppedIndex = draggedIndex;
445 if (dragging) {
446 postDragging = true;
447 } else {
448 draggedIndex = -1;
449 }
451 if (!selectedItem) {
452 return;
453 }
455 selectedItem.dragging = false;
456 selectedItem = undefined;
457 preDragging = false;
459 dragItem.target = undefined
461 progressiveScrollingTimer.stop();
462 launcherListView.interactive = true;
463 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
464 snapToBottomAnimation.start();
465 } else if (droppedIndex < 2 && postDragging) {
466 snapToTopAnimation.start();
467 }
468 }
470 onPressAndHold: {
471 processPressAndHold(mouse, drag);
472 }
474 function processPressAndHold(mouse, dragItem) {
475 if (Math.abs(selectedItem.angle) > 30) {
476 return;
477 }
479 Haptics.play();
481 draggedIndex = Math.floor((mouse.y + launcherListView.realContentY) / launcherListView.realItemHeight);
483 // Opening QuickList
484 quickList.item = selectedItem;
485 quickList.model = launcherListView.model.get(draggedIndex).quickList;
486 quickList.appId = launcherListView.model.get(draggedIndex).appId;
487 quickList.state = "open";
489 launcherListView.interactive = false
491 var yOffset = draggedIndex > 0 ? (mouse.y + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouse.y + launcherListView.realContentY
493 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
494 fakeDragItem.x = units.gu(0.5)
495 fakeDragItem.y = mouse.y - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
496 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
497 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
498 fakeDragItem.count = LauncherModel.get(draggedIndex).count
499 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
500 fakeDragItem.flatten()
501 dragItem.target = fakeDragItem
503 startX = mouse.x
504 startY = mouse.y
505 }
507 onPositionChanged: {
508 processPositionChanged(mouse)
509 }
511 function processPositionChanged(mouse) {
512 if (draggedIndex >= 0) {
513 if (!selectedItem.dragging) {
514 var distance = Math.max(Math.abs(mouse.x - startX), Math.abs(mouse.y - startY))
515 if (!preDragging && distance > units.gu(1.5)) {
516 preDragging = true;
517 quickList.state = "";
518 }
519 if (distance > launcherListView.itemHeight) {
520 selectedItem.dragging = true
521 preDragging = false;
522 }
523 }
524 if (!selectedItem.dragging) {
525 return
526 }
528 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
530 // Move it down by the the missing size to compensate index calculation with only expanded items
531 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
533 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
534 progressiveScrollingTimer.downwards = false
535 progressiveScrollingTimer.start()
536 } else if (mouseY < launcherListView.realItemHeight) {
537 progressiveScrollingTimer.downwards = true
538 progressiveScrollingTimer.start()
539 } else {
540 progressiveScrollingTimer.stop()
541 }
543 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
545 if (newIndex > draggedIndex + 1) {
546 newIndex = draggedIndex + 1
547 } else if (newIndex < draggedIndex) {
548 newIndex = draggedIndex -1
549 } else {
550 return
551 }
553 if (newIndex >= 0 && newIndex < launcherListView.count) {
554 if (launcherListView.draggingTransitionRunning) {
555 launcherListView.scheduledMoveTo = newIndex
556 } else {
557 launcherListView.model.move(draggedIndex, newIndex)
558 draggedIndex = newIndex
559 }
560 }
561 }
562 }
563 }
564 Timer {
565 id: progressiveScrollingTimer
566 interval: 2
567 repeat: true
568 running: false
569 property bool downwards: true
570 onTriggered: {
571 if (downwards) {
572 var minY = -launcherListView.topMargin
573 if (launcherListView.contentY > minY) {
574 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
575 }
576 } else {
577 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
578 if (launcherListView.contentY < maxY) {
579 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
580 }
581 }
582 }
583 }
584 }
585 }
587 LauncherDelegate {
588 id: fakeDragItem
589 objectName: "fakeDragItem"
590 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
591 itemWidth: launcherListView.itemWidth
592 itemHeight: launcherListView.itemHeight
593 height: itemHeight
594 width: itemWidth
595 rotation: root.rotation
596 itemOpacity: 0.9
598 function flatten() {
599 fakeDragItemAnimation.start();
600 }
602 UbuntuNumberAnimation {
603 id: fakeDragItemAnimation
604 target: fakeDragItem;
605 properties: "angle,offset";
606 to: 0
607 }
608 }
609 }
610 }
612 UbuntuShapeForItem {
613 id: quickListShape
614 objectName: "quickListShape"
615 anchors.fill: quickList
616 opacity: quickList.state === "open" ? 0.8 : 0
617 visible: opacity > 0
618 rotation: root.rotation
620 Behavior on opacity {
621 UbuntuNumberAnimation {}
622 }
624 image: quickList
626 Image {
627 anchors {
628 right: parent.left
629 rightMargin: -units.dp(4)
630 verticalCenter: parent.verticalCenter
631 verticalCenterOffset: -quickList.offset * (root.inverted ? -1 : 1)
632 }
633 height: units.gu(1)
634 width: units.gu(2)
635 source: "graphics/quicklist_tooltip.png"
636 rotation: 90
637 }
638 }
639 InverseMouseArea {
640 anchors.fill: quickListShape
641 enabled: quickList.state == "open" || pressed
643 onClicked: {
644 quickList.state = ""
645 }
647 // Forward for dragging to work when quickList is open
649 onPressed: {
650 var m = mapToItem(dndArea, mouseX, mouseY)
651 dndArea.processPress(m)
652 }
654 onPressAndHold: {
655 var m = mapToItem(dndArea, mouseX, mouseY)
656 dndArea.processPressAndHold(m, drag)
657 }
659 onPositionChanged: {
660 var m = mapToItem(dndArea, mouseX, mouseY)
661 dndArea.processPositionChanged(m)
662 }
664 onCanceled: {
665 dndArea.endDrag(drag);
666 }
668 onReleased: {
669 dndArea.endDrag(drag);
670 }
671 }
673 Rectangle {
674 id: quickList
675 objectName: "quickList"
676 color: "#f5f5f5"
677 // Because we're setting left/right anchors depending on orientation, it will break the
678 // width setting after rotating twice. This makes sure we also re-apply width on rotation
679 width: root.inverted ? units.gu(30) : units.gu(30)
680 height: quickListColumn.height
681 visible: quickListShape.visible
682 anchors {
683 left: root.inverted ? undefined : parent.right
684 right: root.inverted ? parent.left : undefined
685 margins: units.gu(1)
686 }
687 y: itemCenter - (height / 2) + offset
688 rotation: root.rotation
690 property var model
691 property string appId
692 property var item
694 // internal
695 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) : units.gu(1)
696 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
697 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
699 Column {
700 id: quickListColumn
701 width: parent.width
702 height: childrenRect.height
704 Repeater {
705 id: popoverRepeater
706 model: quickList.model
708 ListItems.Standard {
709 objectName: "quickListEntry" + index
710 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
711 highlightWhenPressed: model.clickable
713 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
714 // ListItems don't know that they are sitting in a themed Popover where the color
715 // needs to be inverted.
716 __foregroundColor: "black"
718 onClicked: {
719 if (!model.clickable) {
720 return;
721 }
722 Haptics.play();
723 quickList.state = "";
724 // Unsetting model to prevent showing changing entries during fading out
725 // that may happen because of triggering an action.
726 LauncherModel.quickListActionInvoked(quickList.appId, index);
727 quickList.model = undefined;
728 }
729 }
730 }
731 }
732 }