on bf461a2 help sign in
unity8/Launcher/LauncherPanel.qml
537 lines 26 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 appId)
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 }
55 MouseArea {
56 id: dashItem
57 width: parent.width
58 height: units.gu(7)
59 onClicked: root.dashItemSelected(0)
60 z: 1
61 Image {
62 objectName: "dashItem"
63 width: units.gu(5)
64 height: width
65 anchors.centerIn: parent
66 source: "graphics/home.png"
67 rotation: root.rotation
68 }
69 }
70 ThinDivider {
71 anchors {
72 left: parent.left
73 right: parent.right
74 margins: -mainColumn.anchors.leftMargin
75 }
76 rotation: root.rotation
77 }
79 Item {
80 anchors.left: parent.left
81 anchors.right: parent.right
82 height: parent.height - dashItem.height - parent.spacing*2
84 Item {
85 anchors.fill: parent
86 clip: true
88 ListView {
89 id: launcherListView
90 objectName: "launcherListView"
91 anchors {
92 fill: parent
93 topMargin: -extensionSize + units.gu(0.5)
94 bottomMargin: -extensionSize + units.gu(1)
95 leftMargin: units.gu(0.5)
96 rightMargin: units.gu(0.5)
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: 0
115 // Setting extensionSize after the list has been populated because it has
116 // the potential to mess up with the intial positioning in combination
117 // with snapping to the center of the list. This catches all the cases
118 // where the item would be outside the list for more than itemHeight / 2.
119 // For the rest, give it a flick to scroll to the beginning. Note that
120 // the flicking alone isn't enough because in some cases it's not strong
121 // enough to overcome the snapping.
122 // https://bugreports.qt-project.org/browse/QTBUG-32251
123 Component.onCompleted: {
124 extensionSize = itemHeight * 3
125 flick(0, clickFlickSpeed)
126 }
128 // The height of the area where icons start getting folded
129 property int foldingStartHeight: units.gu(6.5)
130 // The height of the area where the items reach the final folding angle
131 property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
132 property int itemWidth: units.gu(7)
133 property int itemHeight: units.gu(6.5)
134 property int clickFlickSpeed: units.gu(60)
135 property int draggedIndex: dndArea.draggedIndex
136 property real realContentY: contentY - originY + topMargin
137 property int realItemHeight: itemHeight + spacing
139 // In case the start dragging transition is running, we need to delay the
140 // move because the displaced transition would clash with it and cause items
141 // to be moved to wrong places
142 property bool draggingTransitionRunning: false
143 property int scheduledMoveTo: -1
145 displaced: Transition {
146 NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
147 }
149 delegate: FoldingLauncherDelegate {
150 id: launcherDelegate
151 objectName: "launcherDelegate" + index
152 itemHeight: launcherListView.itemHeight
153 itemWidth: launcherListView.itemWidth
154 width: itemWidth
155 height: itemHeight
156 iconName: model.icon
157 count: model.count
158 progress: model.progress
159 itemFocused: model.focused
160 inverted: root.inverted
161 z: -Math.abs(offset)
162 maxAngle: 55
163 property bool dragging: false
165 ThinDivider {
166 id: dropIndicator
167 objectName: "dropIndicator"
168 anchors.centerIn: parent
169 width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
170 opacity: 0
171 }
173 states: [
174 State {
175 name: "selected"
176 when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
177 PropertyChanges {
178 target: launcherDelegate
179 itemOpacity: 0
180 }
181 },
182 State {
183 name: "dragging"
184 when: dragging
185 PropertyChanges {
186 target: launcherDelegate
187 height: units.gu(1)
188 itemOpacity: 0
189 }
190 PropertyChanges {
191 target: dropIndicator
192 opacity: 1
193 }
194 },
195 State {
196 name: "expanded"
197 when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
198 PropertyChanges {
199 target: launcherDelegate
200 angle: 0
201 offset: 0
202 itemOpacity: 0.6
203 }
204 }
205 ]
207 transitions: [
208 Transition {
209 from: ""
210 to: "selected"
211 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
212 },
213 Transition {
214 from: "*"
215 to: "expanded"
216 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
217 UbuntuNumberAnimation { properties: "angle,offset" }
218 },
219 Transition {
220 from: "expanded"
221 to: ""
222 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
223 UbuntuNumberAnimation { properties: "angle,offset" }
224 },
225 Transition {
226 id: draggingTransition
227 from: "selected"
228 to: "dragging"
229 SequentialAnimation {
230 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
231 ParallelAnimation {
232 UbuntuNumberAnimation { properties: "height" }
233 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
234 }
235 ScriptAction {
236 script: {
237 if (launcherListView.scheduledMoveTo > -1) {
238 launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
239 dndArea.draggedIndex = launcherListView.scheduledMoveTo
240 launcherListView.scheduledMoveTo = -1
241 }
242 }
243 }
244 PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
245 }
246 },
247 Transition {
248 from: "dragging"
249 to: "*"
250 NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
251 NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
252 SequentialAnimation {
253 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
254 UbuntuNumberAnimation { properties: "height" }
255 ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
256 PropertyAction { target: dndArea; property: "postDragging"; value: false }
257 PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
258 }
259 }
260 ]
261 }
263 MouseArea {
264 id: dndArea
265 objectName: "dndArea"
266 anchors {
267 fill: parent
268 topMargin: launcherListView.topMargin
269 bottomMargin: launcherListView.bottomMargin
270 }
271 drag.minimumY: -launcherListView.topMargin
272 drag.maximumY: height + launcherListView.bottomMargin
274 property int draggedIndex: -1
275 property var selectedItem
276 property bool preDragging: false
277 property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
278 property bool postDragging: false
279 property int startX
280 property int startY
281 property var quickListPopover: null
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 var quickListModel = launcherListView.model.get(draggedIndex).quickList
361 var quickListAppId = launcherListView.model.get(draggedIndex).appId
363 quickListPopover = PopupUtils.open(popoverComponent, selectedItem,
364 {model: quickListModel, appId: quickListAppId})
366 launcherListView.interactive = false
368 var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
370 fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
371 fakeDragItem.x = 0
372 fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
373 fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
374 fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
375 fakeDragItem.count = LauncherModel.get(draggedIndex).count
376 fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
377 fakeDragItem.flatten()
378 drag.target = fakeDragItem
380 startX = mouseX
381 startY = mouseY
382 }
384 onPositionChanged: {
385 if (draggedIndex >= 0) {
386 if (!selectedItem.dragging) {
387 var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
388 if (!preDragging && distance > units.gu(1.5)) {
389 preDragging = true;
390 PopupUtils.close(quickListPopover)
391 }
392 if (distance > launcherListView.itemHeight) {
393 selectedItem.dragging = true
394 preDragging = false;
395 }
396 }
397 if (!selectedItem.dragging) {
398 return
399 }
401 var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
403 // Move it down by the the missing size to compensate index calculation with only expanded items
404 itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
406 if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
407 progressiveScrollingTimer.downwards = false
408 progressiveScrollingTimer.start()
409 } else if (mouseY < launcherListView.realItemHeight) {
410 progressiveScrollingTimer.downwards = true
411 progressiveScrollingTimer.start()
412 } else {
413 progressiveScrollingTimer.stop()
414 }
416 var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
418 if (newIndex > draggedIndex + 1) {
419 newIndex = draggedIndex + 1
420 } else if (newIndex < draggedIndex) {
421 newIndex = draggedIndex -1
422 } else {
423 return
424 }
426 if (newIndex >= 0 && newIndex < launcherListView.count) {
427 if (launcherListView.draggingTransitionRunning) {
428 launcherListView.scheduledMoveTo = newIndex
429 } else {
430 launcherListView.model.move(draggedIndex, newIndex)
431 draggedIndex = newIndex
432 }
433 }
434 }
435 }
436 }
437 Timer {
438 id: progressiveScrollingTimer
439 interval: 2
440 repeat: true
441 running: false
442 property bool downwards: true
443 onTriggered: {
444 if (downwards) {
445 var minY = -launcherListView.topMargin
446 if (launcherListView.contentY > minY) {
447 launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
448 }
449 } else {
450 var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
451 if (launcherListView.contentY < maxY) {
452 launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
453 }
454 }
455 }
456 }
457 }
458 }
460 LauncherDelegate {
461 id: fakeDragItem
462 objectName: "fakeDragItem"
463 visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
464 itemWidth: launcherListView.itemWidth
465 itemHeight: launcherListView.itemHeight
466 height: itemHeight
467 width: itemWidth
468 rotation: root.rotation
469 itemOpacity: 0.9
471 function flatten() {
472 fakeDragItemAnimation.start();
473 }
475 UbuntuNumberAnimation {
476 id: fakeDragItemAnimation
477 target: fakeDragItem;
478 properties: "angle,offset";
479 to: 0
480 }
481 }
482 }
483 }
485 Component {
486 id: popoverComponent
488 Popover {
489 id: popover
490 property var model
491 property string appId
492 contentWidth: quickListColumn.width
493 foregroundStyle: QuicklistForegroundStyle {}
495 // FIXME: There's a bug in the Popover positioning that it covers the item in case it is rotated.
496 // https://bugs.launchpad.net/ubuntu-ui-toolkit/+bug/1204470
497 // For now, let's move the Popover around with callerMargin.
498 // Remove popupMargin once the bug is fixed.
499 property int popupMargin: root.inverted ? launcherListView.itemHeight : 0;
501 callerMargin: units.gu(1) + popupMargin
503 Column {
504 id: quickListColumn
505 width: units.gu(30)
506 height: childrenRect.height
508 Repeater {
509 id: popoverRepeater
510 model: popover.model
512 ListItems.Standard {
513 objectName: "quickListEntry" + index
514 text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
515 highlightWhenPressed: model.clickable
517 // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
518 // ListItems don't know that they are sitting in a themed Popover where the color
519 // needs to be inverted.
520 __foregroundColor: Theme.palette.selected.backgroundText
522 onClicked: {
523 if (!model.clickable) {
524 return;
525 }
526 PopupUtils.close(popover);
527 // Unsetting model to prevent showing changing entries during fading out
528 // that may happen because of triggering an action.
529 LauncherModel.quickListActionInvoked(appId, index);
530 popoverRepeater.model = undefined;
531 }
532 }
533 }
534 }
535 }
536 }