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