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