on db537b1 help sign in
unity8/Launcher/LauncherPanel.qml
517 lines 25 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"
25Item {
26 id: root
28 rotation: inverted ? 180 : 0
30 property var model
31 property bool inverted: true
32 property bool dragging: false
33 property bool moving: launcherListView.moving || launcherListView.flicking
34 property bool preventHiding: moving || dndArea.draggedIndex >= 0 || dndArea.quickListPopover !== null || dndArea.pressed
35 property int highlightIndex: -1
37 signal applicationSelected(string desktopFile)
38 signal dashItemSelected(int index)
40 BorderImage {
41 id: background
42 source: "graphics/launcher_bg.sci"
43 anchors.fill: parent
44 anchors.rightMargin: root.inverted ? 0 : -units.gu(1)
45 anchors.leftMargin: root.inverted ? -units.gu(1) : 0
46 rotation: root.rotation
47 }
49 Column {
50 id: mainColumn
51 anchors {
52 fill: parent
53 leftMargin: units.gu(0.5)
54 rightMargin: units.gu(0.5)
55 }
57 MouseArea {
58 id: dashItem
59 width: parent.width
60 height: units.gu(7)
61 onClicked: root.dashItemSelected(0)
62 z: 1
63 Image {
64 objectName: "dashItem"
65 width: units.gu(5)
66 height: width
67 anchors.centerIn: parent
68 source: "graphics/home.png"
69 rotation: root.rotation
70 }
71 }
72 ThinDivider {
73 anchors {
74 left: parent.left
75 right: parent.right
76 margins: -mainColumn.anchors.leftMargin
77 }
78 rotation: root.rotation
79 }
81 Item {
82 anchors.left: parent.left
83 anchors.right: parent.right
84 height: parent.height - dashItem.height - parent.spacing*2
86 Item {
87 anchors.fill: parent
88 clip: true
90 ListView {
91 id: launcherListView
92 objectName: "launcherListView"
93 anchors {
94 fill: parent
95 topMargin: -extensionSize + units.gu(0.5)
96 bottomMargin: -extensionSize + units.gu(1)
97 }
98 topMargin: extensionSize
99 bottomMargin: extensionSize
100 height: parent.height - dashItem.height - parent.spacing*2
101 model: root.model
102 cacheBuffer: itemHeight * 3
103 snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
104 highlightRangeMode: ListView.ApplyRange
105 preferredHighlightBegin: (height - itemHeight) / 2
106 preferredHighlightEnd: (height + itemHeight) / 2
107 spacing: units.gu(0.5)
109 // The size of the area the ListView is extended to make sure items are not
110 // destroyed when dragging them outside the list. This needs to be at least
111 // itemHeight to prevent folded items from disappearing and DragArea limits
112 // need to be smaller than this size to avoid breakage.
113 property int extensionSize: itemHeight * 3
115 // The height of the area where icons start getting folded
116 property int foldingStartHeight: units.gu(6.5)
117 // The height of the area where the items reach the final folding angle
118 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
119 property int itemWidth: units.gu(7)
120 property int itemHeight: units.gu(6.5)
121 property int clickFlickSpeed: units.gu(60)
122 property int draggedIndex: dndArea.draggedIndex
123 property real realContentY: contentY - originY + topMargin
124 property int realItemHeight: itemHeight + spacing
126 // In case the start dragging transition is running, we need to delay the
127 // move because the displaced transition would clash with it and cause items
128 // to be moved to wrong places
129 property bool draggingTransitionRunning: false
130 property int scheduledMoveTo: -1
132 displaced: Transition {
133 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
134 }
136 delegate: FoldingLauncherDelegate {
137 id: launcherDelegate
138 objectName: "launcherDelegate" + index
139 itemHeight: launcherListView.itemHeight
140 itemWidth: launcherListView.itemWidth
141 width: itemWidth
142 height: itemHeight
143 iconName: model.icon
144 count: model.count
145 progress: model.progress
146 inverted: root.inverted
147 z: -Math.abs(offset)
148 maxAngle: 55
149 property bool dragging: false
151 ThinDivider {
152 id: dropIndicator
153 objectName: "dropIndicator"
154 anchors.centerIn: parent
155 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
156 opacity: 0
157 }
159 states: [
160 State {
161 name: "selected"
162 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
163 PropertyChanges {
164 target: launcherDelegate
165 itemOpacity: 0
166 }
167 },
168 State {
169 name: "dragging"
170 when: dragging
171 PropertyChanges {
172 target: launcherDelegate
173 height: units.gu(1)
174 itemOpacity: 0
175 }
176 PropertyChanges {
177 target: dropIndicator
178 opacity: 1
179 }
180 },
181 State {
182 name: "expanded"
183 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
184 PropertyChanges {
185 target: launcherDelegate
186 angle: 0
187 offset: 0
188 itemOpacity: 0.6
189 }
190 }
191 ]
193 transitions: [
194 Transition {
195 from: ""
196 to: "selected"
197 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
198 },
199 Transition {
200 from: "*"
201 to: "expanded"
202 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
203 UbuntuNumberAnimation { properties: "angle,offset" }
204 },
205 Transition {
206 from: "expanded"
207 to: ""
208 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
209 UbuntuNumberAnimation { properties: "angle,offset" }
210 },
211 Transition {
212 id: draggingTransition
213 from: "selected"
214 to: "dragging"
215 SequentialAnimation {
216 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
217 ParallelAnimation {
218 UbuntuNumberAnimation { properties: "height" }
219 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
220 }
221 ScriptAction {
222 script: {
223 if (launcherListView.scheduledMoveTo > -1) {
224 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
225 dndArea.draggedIndex = launcherListView.scheduledMoveTo
226 launcherListView.scheduledMoveTo = -1
227 }
228 }
229 }
230 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
231 }
232 },
233 Transition {
234 from: "dragging"
235 to: "*"
236 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
237 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
238 SequentialAnimation {
239 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
240 UbuntuNumberAnimation { properties: "height" }
241 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
242 PropertyAction { target: dndArea; property: "postDragging"; value: false }
243 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
244 }
245 }
246 ]
247 }
249 MouseArea {
250 id: dndArea
251 objectName: "dndArea"
252 anchors {
253 fill: parent
254 topMargin: launcherListView.topMargin
255 bottomMargin: launcherListView.bottomMargin
256 }
257 drag.minimumY: -launcherListView.topMargin
258 drag.maximumY: height + launcherListView.bottomMargin
260 property int draggedIndex: -1
261 property var selectedItem
262 property bool preDragging: false
263 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
264 property bool postDragging: false
265 property int startX
266 property int startY
267 property var quickListPopover: null
269 onPressed: {
270 selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
271 }
273 onClicked: {
274 var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
275 var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
277 // Check if we actually clicked an item or only at the spacing in between
278 if (clickedItem === null) {
279 return;
280 }
282 // First/last item do the scrolling at more than 12 degrees
283 if (index == 0 || index == launcherListView.count - 1) {
284 if (clickedItem.angle > 12) {
285 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
286 } else if (clickedItem.angle < -12) {
287 launcherListView.flick(0, launcherListView.clickFlickSpeed);
288 } else {
289 root.applicationSelected(LauncherModel.get(index).desktopFile);
290 }
291 return;
292 }
294 // the rest launches apps up to an angle of 30 degrees
295 if (clickedItem.angle > 30) {
296 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
297 } else if (clickedItem.angle < -30) {
298 launcherListView.flick(0, launcherListView.clickFlickSpeed);
299 } else {
300 root.applicationSelected(LauncherModel.get(index).desktopFile);
301 }
302 }
304 onCanceled: {
305 selectedItem = undefined;
306 preDragging = false;
307 postDragging = false;
308 }
310 onReleased: {
311 var droppedIndex = draggedIndex;
312 if (dragging) {
313 postDragging = true;
314 } else {
315 draggedIndex = -1;
316 }
318 if (!selectedItem) {
319 return;
320 }
322 selectedItem.dragging = false;
323 selectedItem = undefined;
324 preDragging = false;
326 drag.target = undefined
328 progressiveScrollingTimer.stop();
329 launcherListView.interactive = true;
330 if (droppedIndex >= launcherListView.count - 2 && postDragging) {
331 launcherListView.flick(0, -launcherListView.clickFlickSpeed);
332 }
333 if (droppedIndex == 0 && postDragging) {
334 launcherListView.flick(0, launcherListView.clickFlickSpeed);
335 }
336 }
338 onPressAndHold: {
339 if (Math.abs(selectedItem.angle) > 30) {
340 return;
341 }
343 draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
345 // Opening QuickList
346 var quickListModel = launcherListView.model.get(draggedIndex).quickList
347 var quickListAppId = launcherListView.model.get(draggedIndex).appId
349 quickListPopover = PopupUtils.open(popoverComponent, selectedItem,
350 {model: quickListModel, appId: quickListAppId})
352 launcherListView.interactive = false
354 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
356 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
357 fakeDragItem.x = 0
358 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
359 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
360 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
361 fakeDragItem.count = LauncherModel.get(draggedIndex).count
362 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
363 fakeDragItem.flatten()
364 drag.target = fakeDragItem
366 startX = mouseX
367 startY = mouseY
368 }
370 onPositionChanged: {
371 if (draggedIndex >= 0) {
372 if (!selectedItem.dragging) {
373 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
374 if (!preDragging && distance > units.gu(1.5)) {
375 preDragging = true;
376 PopupUtils.close(quickListPopover)
377 }
378 if (distance > launcherListView.itemHeight) {
379 selectedItem.dragging = true
380 preDragging = false;
381 }
382 }
383 if (!selectedItem.dragging) {
384 return
385 }
387 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
389 // Move it down by the the missing size to compensate index calculation with only expanded items
390 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
392 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
393 progressiveScrollingTimer.downwards = false
394 progressiveScrollingTimer.start()
395 } else if (mouseY < launcherListView.realItemHeight) {
396 progressiveScrollingTimer.downwards = true
397 progressiveScrollingTimer.start()
398 } else {
399 progressiveScrollingTimer.stop()
400 }
402 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
404 if (newIndex > draggedIndex + 1) {
405 newIndex = draggedIndex + 1
406 } else if (newIndex < draggedIndex) {
407 newIndex = draggedIndex -1
408 } else {
409 return
410 }
412 if (newIndex >= 0 && newIndex < launcherListView.count) {
413 if (launcherListView.draggingTransitionRunning) {
414 launcherListView.scheduledMoveTo = newIndex
415 } else {
416 launcherListView.model.move(draggedIndex, newIndex)
417 draggedIndex = newIndex
418 }
419 }
420 }
421 }
422 }
423 Timer {
424 id: progressiveScrollingTimer
425 interval: 2
426 repeat: true
427 running: false
428 property bool downwards: true
429 onTriggered: {
430 if (downwards) {
431 var minY = -launcherListView.topMargin
432 if (launcherListView.contentY > minY) {
433 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
434 }
435 } else {
436 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
437 if (launcherListView.contentY < maxY) {
438 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
439 }
440 }
441 }
442 }
443 }
444 }
446 LauncherDelegate {
447 id: fakeDragItem
448 objectName: "fakeDragItem"
449 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
450 itemWidth: launcherListView.itemWidth
451 itemHeight: launcherListView.itemHeight
452 height: itemHeight
453 width: itemWidth
454 rotation: root.rotation
455 itemOpacity: 0.9
457 function flatten() {
458 fakeDragItemAnimation.start();
459 }
461 UbuntuNumberAnimation {
462 id: fakeDragItemAnimation
463 target: fakeDragItem;
464 properties: "angle,offset";
465 to: 0
466 }
467 }
468 }
469 }
471 Component {
472 id: popoverComponent
474 Popover {
475 id: popover
476 property var model
477 property string appId
478 contentWidth: quickListColumn.width
479 foregroundStyle: QuicklistForegroundStyle {}
481 // FIXME: There's a bug in the Popover positioning that it covers the item in case it is rotated.
482 // https://bugs.launchpad.net/ubuntu-ui-toolkit/+bug/1204470
483 // For now, let's move the Popover around with callerMargin.
484 // Remove popupMargin once the bug is fixed.
485 property int popupMargin: root.inverted ? launcherListView.itemHeight : 0;
487 callerMargin: units.gu(1) + popupMargin
489 Column {
490 id: quickListColumn
491 width: units.gu(30)
492 height: childrenRect.height
494 Repeater {
495 id: popoverRepeater
496 model: popover.model
498 ListItems.Standard {
499 objectName: "quickListEntry" + index
500 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
501 highlightWhenPressed: model.clickable
502 onClicked: {
503 if (!model.clickable) {
504 return;
505 }
506 PopupUtils.close(popover);
507 // Unsetting model to prevent showing changing entries during fading out
508 // that may happen because of triggering an action.
509 LauncherModel.quickListActionInvoked(appId, index);
510 popoverRepeater.model = undefined;
511 }
512 }
513 }
514 }
515 }
516 }