on 1ea033d help sign in
unity8/qml/Launcher/LauncherPanel.qml
585 lines 28 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.0
18import Ubuntu.Components 0.1
19import Ubuntu.Components.ListItems 0.1 as ListItems
20import Unity.Launcher 0.1
21import Ubuntu.Components.Popups 0.1
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: true
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 property int highlightIndex: -1
38 signal applicationSelected(string appId)
39 signal showDashHome()
41 Column {
42 id: mainColumn
43 anchors {
44 fill: parent
45 }
47 Rectangle {
48 width: parent.width
49 height: units.gu(7)
50 color: UbuntuColors.orange
51 z: 1
53 Image {
54 objectName: "dashItem"
55 width: units.gu(5)
56 height: width
57 anchors.centerIn: parent
58 source: "graphics/home.png"
59 rotation: root.rotation
60 }
61 MouseArea {
62 id: dashItem
63 anchors.fill: parent
64 onClicked: root.showDashHome()
65 }
66 }
68 ThinDivider {
69 anchors {
70 left: parent.left
71 right: parent.right
72 margins: -mainColumn.anchors.leftMargin
73 }
74 rotation: root.rotation
75 }
77 Item {
78 anchors.left: parent.left
79 anchors.right: parent.right
80 height: parent.height - dashItem.height - parent.spacing*2
82 Item {
83 anchors.fill: parent
84 clip: true
86 ListView {
87 id: launcherListView
88 objectName: "launcherListView"
89 anchors {
90 fill: parent
91 topMargin: -extensionSize + units.gu(0.5)
92 bottomMargin: -extensionSize + units.gu(1)
93 leftMargin: units.gu(0.5)
94 rightMargin: units.gu(0.5)
95 }
96 topMargin: extensionSize
97 bottomMargin: extensionSize
98 height: parent.height - dashItem.height - parent.spacing*2
99 model: root.model
100 cacheBuffer: itemHeight * 3
101 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
102 highlightRangeMode: ListView.ApplyRange
103 preferredHighlightBegin: (height - itemHeight) / 2
104 preferredHighlightEnd: (height + itemHeight) / 2
106 // The size of the area the ListView is extended to make sure items are not
107 // destroyed when dragging them outside the list. This needs to be at least
108 // itemHeight to prevent folded items from disappearing and DragArea limits
109 // need to be smaller than this size to avoid breakage.
110 property int extensionSize: 0
112 // Setting extensionSize after the list has been populated because it has
113 // the potential to mess up with the intial positioning in combination
114 // with snapping to the center of the list. This catches all the cases
115 // where the item would be outside the list for more than itemHeight / 2.
116 // For the rest, give it a flick to scroll to the beginning. Note that
117 // the flicking alone isn't enough because in some cases it's not strong
118 // enough to overcome the snapping.
119 // https://bugreports.qt-project.org/browse/QTBUG-32251
120 Component.onCompleted: {
121 extensionSize = itemHeight * 3
122 flick(0, clickFlickSpeed)
123 }
125 // The height of the area where icons start getting folded
126 property int foldingStartHeight: units.gu(6.5)
127 // The height of the area where the items reach the final folding angle
128 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
129 property int itemWidth: units.gu(7)
130 property int itemHeight: units.gu(6.5)
131 property int clickFlickSpeed: units.gu(60)
132 property int draggedIndex: dndArea.draggedIndex
133 property real realContentY: contentY - originY + topMargin
134 property int realItemHeight: itemHeight + spacing
136 // In case the start dragging transition is running, we need to delay the
137 // move because the displaced transition would clash with it and cause items
138 // to be moved to wrong places
139 property bool draggingTransitionRunning: false
140 property int scheduledMoveTo: -1
142 displaced: Transition {
143 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
144 }
146 delegate: FoldingLauncherDelegate {
147 id: launcherDelegate
148 objectName: "launcherDelegate" + index
149 // We need the appId in the delegate in order to find
150 // the right app when running autopilot tests for
151 // multiple apps.
152 readonly property string appId: model.appId
153 itemHeight: launcherListView.itemHeight
154 itemWidth: launcherListView.itemWidth
155 width: itemWidth
156 height: itemHeight
157 iconName: model.icon
158 count: model.count
159 countVisible: model.countVisible
160 progress: model.progress
161 clipCorner: model.pinned
162 itemFocused: model.focused
163 inverted: root.inverted
164 z: -Math.abs(offset)
165 maxAngle: 55
166 property bool dragging: false
168 ThinDivider {
169 id: dropIndicator
170 objectName: "dropIndicator"
171 anchors.centerIn: parent
172 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
173 opacity: 0
174 source: "graphics/divider-line.png"
175 }
177 states: [
178 State {
179 name: "selected"
180 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
181 PropertyChanges {
182 target: launcherDelegate
183 itemOpacity: 0
184 }
185 },
186 State {
187 name: "dragging"
188 when: dragging
189 PropertyChanges {
190 target: launcherDelegate
191 height: units.gu(1)
192 itemOpacity: 0
193 }
194 PropertyChanges {
195 target: dropIndicator
196 opacity: 1
197 }
198 },
199 State {
200 name: "expanded"
201 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
202 PropertyChanges {
203 target: launcherDelegate
204 angle: 0
205 offset: 0
206 itemOpacity: 0.6
207 }
208 }
209 ]
211 transitions: [
212 Transition {
213 from: ""
214 to: "selected"
215 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
216 },
217 Transition {
218 from: "*"
219 to: "expanded"
220 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
221 UbuntuNumberAnimation { properties: "angle,offset" }
222 },
223 Transition {
224 from: "expanded"
225 to: ""
226 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
227 UbuntuNumberAnimation { properties: "angle,offset" }
228 },
229 Transition {
230 id: draggingTransition
231 from: "selected"
232 to: "dragging"
233 SequentialAnimation {
234 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
235 ParallelAnimation {
236 UbuntuNumberAnimation { properties: "height" }
237 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
238 }
239 ScriptAction {
240 script: {
241 if (launcherListView.scheduledMoveTo > -1) {
242 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
243 dndArea.draggedIndex = launcherListView.scheduledMoveTo
244 launcherListView.scheduledMoveTo = -1
245 }
246 }
247 }
248 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
249 }
250 },
251 Transition {
252 from: "dragging"
253 to: "*"
254 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
255 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
256 SequentialAnimation {
257 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
258 UbuntuNumberAnimation { properties: "height" }
259 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
260 PropertyAction { target: dndArea; property: "postDragging"; value: false }
261 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
262 }
263 }
264 ]
265 }
267 MouseArea {
268 id: dndArea
269 objectName: "dndArea"
270 anchors {
271 fill: parent
272 topMargin: launcherListView.topMargin
273 bottomMargin: launcherListView.bottomMargin
274 }
275 drag.minimumY: -launcherListView.topMargin
276 drag.maximumY: height + launcherListView.bottomMargin
278 property int draggedIndex: -1
279 property var selectedItem
280 property bool preDragging: false
281 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
282 property bool postDragging: false
283 property int startX
284 property int startY
286 onPressed: {
287 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
288 }
290 onClicked: {
291 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
292 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
294 // Check if we actually clicked an item or only at the spacing in between
295 if (clickedItem === null) {
296 return;
297 }
299 // First/last item do the scrolling at more than 12 degrees
300 if (index == 0 || index == launcherListView.count - 1) {
301 if (clickedItem.angle > 12) {
302 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
303 } else if (clickedItem.angle < -12) {
304 launcherListView.flick(0, launcherListView.clickFlickSpeed);
305 } else {
306 root.applicationSelected(LauncherModel.get(index).appId);
307 }
308 return;
309 }
311 // the rest launches apps up to an angle of 30 degrees
312 if (clickedItem.angle > 30) {
313 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
314 } else if (clickedItem.angle < -30) {
315 launcherListView.flick(0, launcherListView.clickFlickSpeed);
316 } else {
317 root.applicationSelected(LauncherModel.get(index).appId);
318 }
319 }
321 onCanceled: {
322 selectedItem = undefined;
323 preDragging = false;
324 postDragging = false;
325 }
327 onReleased: {
328 var droppedIndex = draggedIndex;
329 if (dragging) {
330 postDragging = true;
331 } else {
332 draggedIndex = -1;
333 }
335 if (!selectedItem) {
336 return;
337 }
339 selectedItem.dragging = false;
340 selectedItem = undefined;
341 preDragging = false;
343 drag.target = undefined
345 progressiveScrollingTimer.stop();
346 launcherListView.interactive = true;
347 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
348 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
349 }
350 if (droppedIndex == 0 && postDragging) {
351 launcherListView.flick(0, launcherListView.clickFlickSpeed);
352 }
353 }
355 onPressAndHold: {
356 if (Math.abs(selectedItem.angle) > 30) {
357 return;
358 }
360 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
362 // Opening QuickList
363 quickList.item = selectedItem;
364 quickList.model = launcherListView.model.get(draggedIndex).quickList;
365 quickList.appId = launcherListView.model.get(draggedIndex).appId;
366 quickList.state = "open";
368 launcherListView.interactive = false
370 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
372 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
373 fakeDragItem.x = units.gu(0.5)
374 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
375 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
376 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
377 fakeDragItem.count = LauncherModel.get(draggedIndex).count
378 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
379 fakeDragItem.flatten()
380 drag.target = fakeDragItem
382 startX = mouseX
383 startY = mouseY
384 }
386 onPositionChanged: {
387 if (draggedIndex >= 0) {
388 if (!selectedItem.dragging) {
389 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
390 if (!preDragging && distance > units.gu(1.5)) {
391 preDragging = true;
392 quickList.state = "";
393 }
394 if (distance > launcherListView.itemHeight) {
395 selectedItem.dragging = true
396 preDragging = false;
397 }
398 }
399 if (!selectedItem.dragging) {
400 return
401 }
403 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
405 // Move it down by the the missing size to compensate index calculation with only expanded items
406 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
408 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
409 progressiveScrollingTimer.downwards = false
410 progressiveScrollingTimer.start()
411 } else if (mouseY < launcherListView.realItemHeight) {
412 progressiveScrollingTimer.downwards = true
413 progressiveScrollingTimer.start()
414 } else {
415 progressiveScrollingTimer.stop()
416 }
418 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
420 if (newIndex > draggedIndex + 1) {
421 newIndex = draggedIndex + 1
422 } else if (newIndex < draggedIndex) {
423 newIndex = draggedIndex -1
424 } else {
425 return
426 }
428 if (newIndex >= 0 && newIndex < launcherListView.count) {
429 if (launcherListView.draggingTransitionRunning) {
430 launcherListView.scheduledMoveTo = newIndex
431 } else {
432 launcherListView.model.move(draggedIndex, newIndex)
433 draggedIndex = newIndex
434 }
435 }
436 }
437 }
438 }
439 Timer {
440 id: progressiveScrollingTimer
441 interval: 2
442 repeat: true
443 running: false
444 property bool downwards: true
445 onTriggered: {
446 if (downwards) {
447 var minY = -launcherListView.topMargin
448 if (launcherListView.contentY > minY) {
449 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
450 }
451 } else {
452 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
453 if (launcherListView.contentY < maxY) {
454 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
455 }
456 }
457 }
458 }
459 }
460 }
462 LauncherDelegate {
463 id: fakeDragItem
464 objectName: "fakeDragItem"
465 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
466 itemWidth: launcherListView.itemWidth
467 itemHeight: launcherListView.itemHeight
468 height: itemHeight
469 width: itemWidth
470 rotation: root.rotation
471 itemOpacity: 0.9
472 clipCorner: dndArea.draggedIndex > -1 &&
473 LauncherModel.get(dndArea.draggedIndex).pinned &&
474 !dndArea.preDragging &&
475 !dndArea.dragging
477 function flatten() {
478 fakeDragItemAnimation.start();
479 }
481 UbuntuNumberAnimation {
482 id: fakeDragItemAnimation
483 target: fakeDragItem;
484 properties: "angle,offset";
485 to: 0
486 }
487 }
488 }
489 }
491 UbuntuShapeForItem {
492 id: quickListShape
493 objectName: "quickListShape"
494 anchors.fill: quickList
495 opacity: quickList.state === "open" ? 0.8 : 0
496 visible: opacity > 0
497 rotation: root.rotation
499 Behavior on opacity {
500 UbuntuNumberAnimation {}
501 }
503 image: quickList
505 Image {
506 anchors {
507 right: parent.left
508 rightMargin: -units.dp(4)
509 verticalCenter: parent.verticalCenter
510 verticalCenterOffset: -quickList.offset
511 }
512 height: units.gu(1)
513 width: units.gu(2)
514 source: "graphics/quicklist_tooltip.png"
515 rotation: 90
516 }
518 InverseMouseArea {
519 anchors.fill: parent
520 enabled: quickList.state == "open"
521 onClicked: {
522 quickList.state = ""
523 }
524 }
526 }
528 Rectangle {
529 id: quickList
530 objectName: "quickList"
531 color: "#221e1c"
532 width: units.gu(30)
533 height: quickListColumn.height
534 visible: quickListShape.visible
535 anchors {
536 left: root.inverted ? undefined : parent.right
537 right: root.inverted ? parent.left : undefined
538 margins: units.gu(1)
539 }
540 y: itemCenter - (height / 2) + offset
541 rotation: root.rotation
543 property var model
544 property string appId
545 property var item
547 // internal
548 property int itemCenter: item ? root.mapFromItem(quickList.item).y + (item.height / 2) : units.gu(1)
549 property int offset: itemCenter + (height/2) + units.gu(1) > parent.height ? -itemCenter - (height/2) - units.gu(1) + parent.height :
550 itemCenter - (height/2) < units.gu(1) ? (height/2) - itemCenter + units.gu(1) : 0
552 Column {
553 id: quickListColumn
554 width: parent.width
555 height: childrenRect.height
557 Repeater {
558 id: popoverRepeater
559 model: quickList.model
561 ListItems.Standard {
562 objectName: "quickListEntry" + index
563 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
564 highlightWhenPressed: model.clickable
566 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
567 // ListItems don't know that they are sitting in a themed Popover where the color
568 // needs to be inverted.
569 __foregroundColor: Theme.palette.selected.backgroundText
571 onClicked: {
572 if (!model.clickable) {
573 return;
574 }
575 quickList.state = "";
576 // Unsetting model to prevent showing changing entries during fading out
577 // that may happen because of triggering an action.
578 LauncherModel.quickListActionInvoked(quickList.appId, index);
579 quickList.model = undefined;
580 }
581 }
582 }
583 }
584 }