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  pinned: model.pinned
155  itemFocused: model.focused
156  inverted: root.inverted
157  z: -Math.abs(offset)
158  maxAngle: 55
159  property bool dragging: false
160 
161  ThinDivider {
162  id: dropIndicator
163  objectName: "dropIndicator"
164  anchors.centerIn: parent
165  width: parent.width + mainColumn.anchors.leftMargin + mainColumn.anchors.rightMargin
166  opacity: 0
167  source: "graphics/divider-line.png"
168  }
169 
170  states: [
171  State {
172  name: "selected"
173  when: dndArea.selectedItem === launcherDelegate && fakeDragItem.visible && !dragging
174  PropertyChanges {
175  target: launcherDelegate
176  itemOpacity: 0
177  }
178  },
179  State {
180  name: "dragging"
181  when: dragging
182  PropertyChanges {
183  target: launcherDelegate
184  height: units.gu(1)
185  itemOpacity: 0
186  }
187  PropertyChanges {
188  target: dropIndicator
189  opacity: 1
190  }
191  },
192  State {
193  name: "expanded"
194  when: dndArea.draggedIndex >= 0 && (dndArea.preDragging || dndArea.dragging || dndArea.postDragging) && dndArea.draggedIndex != index
195  PropertyChanges {
196  target: launcherDelegate
197  angle: 0
198  offset: 0
199  itemOpacity: 0.6
200  }
201  }
202  ]
203 
204  transitions: [
205  Transition {
206  from: ""
207  to: "selected"
208  NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
209  },
210  Transition {
211  from: "*"
212  to: "expanded"
213  NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.FastDuration }
214  UbuntuNumberAnimation { properties: "angle,offset" }
215  },
216  Transition {
217  from: "expanded"
218  to: ""
219  NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
220  UbuntuNumberAnimation { properties: "angle,offset" }
221  },
222  Transition {
223  id: draggingTransition
224  from: "selected"
225  to: "dragging"
226  SequentialAnimation {
227  PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: true }
228  ParallelAnimation {
229  UbuntuNumberAnimation { properties: "height" }
230  NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.FastDuration }
231  }
232  ScriptAction {
233  script: {
234  if (launcherListView.scheduledMoveTo > -1) {
235  launcherListView.model.move(dndArea.draggedIndex, launcherListView.scheduledMoveTo)
236  dndArea.draggedIndex = launcherListView.scheduledMoveTo
237  launcherListView.scheduledMoveTo = -1
238  }
239  }
240  }
241  PropertyAction { target: launcherListView; property: "draggingTransitionRunning"; value: false }
242  }
243  },
244  Transition {
245  from: "dragging"
246  to: "*"
247  NumberAnimation { target: dropIndicator; properties: "opacity"; duration: UbuntuAnimation.SnapDuration }
248  NumberAnimation { properties: "itemOpacity"; duration: UbuntuAnimation.BriskDuration }
249  SequentialAnimation {
250  ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
251  UbuntuNumberAnimation { properties: "height" }
252  ScriptAction { script: if (index == launcherListView.count-1) launcherListView.flick(0, -launcherListView.clickFlickSpeed); }
253  PropertyAction { target: dndArea; property: "postDragging"; value: false }
254  PropertyAction { target: dndArea; property: "draggedIndex"; value: -1 }
255  }
256  }
257  ]
258  }
259 
260  MouseArea {
261  id: dndArea
262  objectName: "dndArea"
263  anchors {
264  fill: parent
265  topMargin: launcherListView.topMargin
266  bottomMargin: launcherListView.bottomMargin
267  }
268  drag.minimumY: -launcherListView.topMargin
269  drag.maximumY: height + launcherListView.bottomMargin
270 
271  property int draggedIndex: -1
272  property var selectedItem
273  property bool preDragging: false
274  property bool dragging: selectedItem !== undefined && selectedItem !== null && selectedItem.dragging
275  property bool postDragging: false
276  property int startX
277  property int startY
278 
279  onPressed: {
280  selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
281  }
282 
283  onClicked: {
284  var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
285  var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
286 
287  // Check if we actually clicked an item or only at the spacing in between
288  if (clickedItem === null) {
289  return;
290  }
291 
292  // First/last item do the scrolling at more than 12 degrees
293  if (index == 0 || index == launcherListView.count - 1) {
294  if (clickedItem.angle > 12) {
295  launcherListView.flick(0, -launcherListView.clickFlickSpeed);
296  } else if (clickedItem.angle < -12) {
297  launcherListView.flick(0, launcherListView.clickFlickSpeed);
298  } else {
299  root.applicationSelected(LauncherModel.get(index).appId);
300  }
301  return;
302  }
303 
304  // the rest launches apps up to an angle of 30 degrees
305  if (clickedItem.angle > 30) {
306  launcherListView.flick(0, -launcherListView.clickFlickSpeed);
307  } else if (clickedItem.angle < -30) {
308  launcherListView.flick(0, launcherListView.clickFlickSpeed);
309  } else {
310  root.applicationSelected(LauncherModel.get(index).appId);
311  }
312  }
313 
314  onCanceled: {
315  selectedItem = undefined;
316  preDragging = false;
317  postDragging = false;
318  }
319 
320  onReleased: {
321  var droppedIndex = draggedIndex;
322  if (dragging) {
323  postDragging = true;
324  } else {
325  draggedIndex = -1;
326  }
327 
328  if (!selectedItem) {
329  return;
330  }
331 
332  selectedItem.dragging = false;
333  selectedItem = undefined;
334  preDragging = false;
335 
336  drag.target = undefined
337 
338  progressiveScrollingTimer.stop();
339  launcherListView.interactive = true;
340  if (droppedIndex >= launcherListView.count - 2 && postDragging) {
341  launcherListView.flick(0, -launcherListView.clickFlickSpeed);
342  }
343  if (droppedIndex == 0 && postDragging) {
344  launcherListView.flick(0, launcherListView.clickFlickSpeed);
345  }
346  }
347 
348  onPressAndHold: {
349  if (Math.abs(selectedItem.angle) > 30) {
350  return;
351  }
352 
353  draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
354 
355  // Opening QuickList
356  quickList.item = selectedItem;
357  quickList.model = launcherListView.model.get(draggedIndex).quickList;
358  quickList.appId = launcherListView.model.get(draggedIndex).appId;
359  quickList.state = "open";
360 
361  launcherListView.interactive = false
362 
363  var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
364 
365  fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
366  fakeDragItem.x = units.gu(0.5)
367  fakeDragItem.y = mouseY - yOffset + launcherListView.anchors.topMargin + launcherListView.topMargin
368  fakeDragItem.angle = selectedItem.angle * (root.inverted ? -1 : 1)
369  fakeDragItem.offset = selectedItem.offset * (root.inverted ? -1 : 1)
370  fakeDragItem.count = LauncherModel.get(draggedIndex).count
371  fakeDragItem.progress = LauncherModel.get(draggedIndex).progress
372  fakeDragItem.flatten()
373  drag.target = fakeDragItem
374 
375  startX = mouseX
376  startY = mouseY
377  }
378 
379  onPositionChanged: {
380  if (draggedIndex >= 0) {
381  if (!selectedItem.dragging) {
382  var distance = Math.max(Math.abs(mouseX - startX), Math.abs(mouseY - startY))
383  if (!preDragging && distance > units.gu(1.5)) {
384  preDragging = true;
385  quickList.state = "";
386  }
387  if (distance > launcherListView.itemHeight) {
388  selectedItem.dragging = true
389  preDragging = false;
390  }
391  }
392  if (!selectedItem.dragging) {
393  return
394  }
395 
396  var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
397 
398  // Move it down by the the missing size to compensate index calculation with only expanded items
399  itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
400 
401  if (mouseY > launcherListView.height - launcherListView.topMargin - launcherListView.bottomMargin - launcherListView.realItemHeight) {
402  progressiveScrollingTimer.downwards = false
403  progressiveScrollingTimer.start()
404  } else if (mouseY < launcherListView.realItemHeight) {
405  progressiveScrollingTimer.downwards = true
406  progressiveScrollingTimer.start()
407  } else {
408  progressiveScrollingTimer.stop()
409  }
410 
411  var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
412 
413  if (newIndex > draggedIndex + 1) {
414  newIndex = draggedIndex + 1
415  } else if (newIndex < draggedIndex) {
416  newIndex = draggedIndex -1
417  } else {
418  return
419  }
420 
421  if (newIndex >= 0 && newIndex < launcherListView.count) {
422  if (launcherListView.draggingTransitionRunning) {
423  launcherListView.scheduledMoveTo = newIndex
424  } else {
425  launcherListView.model.move(draggedIndex, newIndex)
426  draggedIndex = newIndex
427  }
428  }
429  }
430  }
431  }
432  Timer {
433  id: progressiveScrollingTimer
434  interval: 2
435  repeat: true
436  running: false
437  property bool downwards: true
438  onTriggered: {
439  if (downwards) {
440  var minY = -launcherListView.topMargin
441  if (launcherListView.contentY > minY) {
442  launcherListView.contentY = Math.max(launcherListView.contentY - units.dp(2), minY)
443  }
444  } else {
445  var maxY = launcherListView.contentHeight - launcherListView.height + launcherListView.topMargin + launcherListView.originY
446  if (launcherListView.contentY < maxY) {
447  launcherListView.contentY = Math.min(launcherListView.contentY + units.dp(2), maxY)
448  }
449  }
450  }
451  }
452  }
453  }
454 
455  LauncherDelegate {
456  id: fakeDragItem
457  objectName: "fakeDragItem"
458  visible: dndArea.draggedIndex >= 0 && !dndArea.postDragging
459  itemWidth: launcherListView.itemWidth
460  itemHeight: launcherListView.itemHeight
461  height: itemHeight
462  width: itemWidth
463  rotation: root.rotation
464  itemOpacity: 0.9
465  pinned: dndArea.draggedIndex > -1 &&
466  LauncherModel.get(dndArea.draggedIndex).pinned &&
467  !dndArea.preDragging &&
468  !dndArea.dragging
469 
470  function flatten() {
471  fakeDragItemAnimation.start();
472  }
473 
474  UbuntuNumberAnimation {
475  id: fakeDragItemAnimation
476  target: fakeDragItem;
477  properties: "angle,offset";
478  to: 0
479  }
480  }
481  }
482  }
483 
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
491 
492  Behavior on opacity {
493  UbuntuNumberAnimation {}
494  }
495 
496  image: quickList
497 
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  }
510 
511  InverseMouseArea {
512  anchors.fill: parent
513  enabled: quickList.state == "open"
514  onClicked: {
515  quickList.state = ""
516  }
517  }
518 
519  }
520 
521  Rectangle {
522  id: quickList
523  objectName: "quickList"
524  color: "#221e1c"
525  width: units.gu(30)
526  height: quickListColumn.height
527  visible: quickListShape.visible
528  anchors {
529  left: root.inverted ? undefined : parent.right
530  right: root.inverted ? parent.left : undefined
531  margins: units.gu(1)
532  }
533  y: itemCenter - (height / 2) + offset
534  rotation: root.rotation
535 
536  property var model
537  property string appId
538  property var item
539 
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
544 
545  Column {
546  id: quickListColumn
547  width: parent.width
548  height: childrenRect.height
549 
550  Repeater {
551  id: popoverRepeater
552  model: quickList.model
553 
554  ListItems.Standard {
555  objectName: "quickListEntry" + index
556  text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
557  highlightWhenPressed: model.clickable
558 
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
563 
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  }
578 }