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.3
18 import Ubuntu.Components 1.1
19 import Ubuntu.Components.ListItems 1.0 as ListItems
20 import Unity.Launcher 0.1
21 import Ubuntu.Components.Popups 0.1
22 import "../Components/ListItems"
23 import "../Components/"
24 
25 Rectangle {
26  id: root
27  color: "#B2000000"
28 
29  rotation: inverted ? 180 : 0
30 
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
37 
38  signal applicationSelected(string appId)
39  signal showDashHome()
40 
41  onXChanged: {
42  if (quickList.state == "open") {
43  quickList.state = ""
44  }
45  }
46 
47  Column {
48  id: mainColumn
49  anchors {
50  fill: parent
51  }
52 
53  Rectangle {
54  objectName: "buttonShowDashHome"
55  width: parent.width
56  height: units.gu(7)
57  color: UbuntuColors.orange
58  z: 1
59 
60  Image {
61  objectName: "dashItem"
62  width: units.gu(5)
63  height: width
64  anchors.centerIn: parent
65  source: "graphics/home.png"
66  rotation: root.rotation
67  }
68  MouseArea {
69  id: dashItem
70  anchors.fill: parent
71  onClicked: root.showDashHome()
72  }
73  }
74 
75  Item {
76  anchors.left: parent.left
77  anchors.right: parent.right
78  height: parent.height - dashItem.height - parent.spacing*2
79 
80  Item {
81  anchors.fill: parent
82  clip: true
83 
84  ListView {
85  id: launcherListView
86  objectName: "launcherListView"
87  anchors {
88  fill: parent
89  topMargin: -extensionSize + units.gu(0.5)
90  bottomMargin: -extensionSize + units.gu(1)
91  leftMargin: units.gu(0.5)
92  rightMargin: units.gu(0.5)
93  }
94  topMargin: extensionSize
95  bottomMargin: extensionSize
96  height: parent.height - dashItem.height - parent.spacing*2
97  model: root.model
98  cacheBuffer: itemHeight * 3
99  snapMode: interactive ? ListView.SnapToItem : ListView.NoSnap
100  highlightRangeMode: ListView.ApplyRange
101  preferredHighlightBegin: (height - itemHeight) / 2
102  preferredHighlightEnd: (height + itemHeight) / 2
103 
104  // The size of the area the ListView is extended to make sure items are not
105  // destroyed when dragging them outside the list. This needs to be at least
106  // itemHeight to prevent folded items from disappearing and DragArea limits
107  // need to be smaller than this size to avoid breakage.
108  property int extensionSize: 0
109 
110  // Setting extensionSize after the list has been populated because it has
111  // the potential to mess up with the intial positioning in combination
112  // with snapping to the center of the list. This catches all the cases
113  // where the item would be outside the list for more than itemHeight / 2.
114  // For the rest, give it a flick to scroll to the beginning. Note that
115  // the flicking alone isn't enough because in some cases it's not strong
116  // enough to overcome the snapping.
117  // https://bugreports.qt-project.org/browse/QTBUG-32251
118  Component.onCompleted: {
119  extensionSize = itemHeight * 3
120  flick(0, clickFlickSpeed)
121  }
122 
123  // The height of the area where icons start getting folded
124  property int foldingStartHeight: units.gu(6.5)
125  // The height of the area where the items reach the final folding angle
126  property int foldingStopHeight: foldingStartHeight - itemHeight - spacing
127  property int itemWidth: units.gu(7)
128  property int itemHeight: units.gu(6.5)
129  property int clickFlickSpeed: units.gu(60)
130  property int draggedIndex: dndArea.draggedIndex
131  property real realContentY: contentY - originY + topMargin
132  property int realItemHeight: itemHeight + spacing
133 
134  // In case the start dragging transition is running, we need to delay the
135  // move because the displaced transition would clash with it and cause items
136  // to be moved to wrong places
137  property bool draggingTransitionRunning: false
138  property int scheduledMoveTo: -1
139 
140  displaced: Transition {
141  NumberAnimation { properties: "x,y"; duration: UbuntuAnimation.FastDuration; easing: UbuntuAnimation.StandardEasing }
142  }
143 
144  delegate: FoldingLauncherDelegate {
145  id: launcherDelegate
146  objectName: "launcherDelegate" + index
147  // We need the appId in the delegate in order to find
148  // the right app when running autopilot tests for
149  // multiple apps.
150  readonly property string appId: model.appId
151  itemHeight: launcherListView.itemHeight
152  itemWidth: launcherListView.itemWidth
153  width: itemWidth
154  height: itemHeight
155  iconName: model.icon
156  count: model.count
157  countVisible: model.countVisible
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
164 
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  source: "graphics/divider-line.png"
172  }
173 
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  ]
207 
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.SnapDuration }
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  }
263 
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
274 
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
282 
283  onPressed: {
284  selectedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
285  }
286 
287  onClicked: {
288  var index = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
289  var clickedItem = launcherListView.itemAt(mouseX, mouseY + launcherListView.realContentY)
290 
291  // Check if we actually clicked an item or only at the spacing in between
292  if (clickedItem === null) {
293  return;
294  }
295 
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  }
307 
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  }
317 
318  onCanceled: {
319  selectedItem = undefined;
320  preDragging = false;
321  postDragging = false;
322  }
323 
324  onReleased: {
325  var droppedIndex = draggedIndex;
326  if (dragging) {
327  postDragging = true;
328  } else {
329  draggedIndex = -1;
330  }
331 
332  if (!selectedItem) {
333  return;
334  }
335 
336  selectedItem.dragging = false;
337  selectedItem = undefined;
338  preDragging = false;
339 
340  drag.target = undefined
341 
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  }
351 
352  onPressAndHold: {
353  if (Math.abs(selectedItem.angle) > 30) {
354  return;
355  }
356 
357  draggedIndex = Math.floor((mouseY + launcherListView.realContentY) / launcherListView.realItemHeight);
358 
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";
364 
365  launcherListView.interactive = false
366 
367  var yOffset = draggedIndex > 0 ? (mouseY + launcherListView.realContentY) % (draggedIndex * launcherListView.realItemHeight) : mouseY + launcherListView.realContentY
368 
369  fakeDragItem.iconName = launcherListView.model.get(draggedIndex).icon
370  fakeDragItem.x = units.gu(0.5)
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
378 
379  startX = mouseX
380  startY = mouseY
381  }
382 
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  }
399 
400  var itemCenterY = fakeDragItem.y + fakeDragItem.height / 2
401 
402  // Move it down by the the missing size to compensate index calculation with only expanded items
403  itemCenterY += (launcherListView.itemHeight - selectedItem.height) / 2
404 
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  }
414 
415  var newIndex = (itemCenterY + launcherListView.realContentY) / launcherListView.realItemHeight
416 
417  if (newIndex > draggedIndex + 1) {
418  newIndex = draggedIndex + 1
419  } else if (newIndex < draggedIndex) {
420  newIndex = draggedIndex -1
421  } else {
422  return
423  }
424 
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  }
458 
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
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.96 : 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  left: parent.left
501  leftMargin: (quickList.item.width - units.gu(1)) / 2 - width / 2
502  verticalCenter: parent.verticalCenter
503  verticalCenterOffset: (parent.height / 2 + units.dp(3)) * (quickList.offset > 0 ? 1 : -1)
504  }
505  height: units.gu(1)
506  width: units.gu(2)
507  source: "graphics/quicklist_tooltip.png"
508  rotation: quickList.offset > 0 ? 0 : 180
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: "#f5f5f5"
525  width: units.gu(30)
526  height: quickListColumn.height
527  visible: quickListShape.visible
528  anchors {
529  left: root.inverted ? undefined : parent.left
530  right: root.inverted ? parent.right : undefined
531  margins: units.gu(1)
532  }
533  y: itemCenter + 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 + (item.height/2) + height + units.gu(1) > parent.height ?
543  -(item.height/2) - height - units.gu(.5) :
544  (item.height/2) + units.gu(.5)
545 
546  Column {
547  id: quickListColumn
548  width: parent.width
549  height: childrenRect.height
550 
551  Repeater {
552  id: popoverRepeater
553  model: quickList.model
554 
555  ListItems.Standard {
556  objectName: "quickListEntry" + index
557  text: (model.clickable ? "" : "<b>") + model.label + (model.clickable ? "" : "</b>")
558  highlightWhenPressed: model.clickable
559 
560  // FIXME: This is a workaround for the theme not being context sensitive. I.e. the
561  // ListItems don't know that they are sitting in a themed Popover where the color
562  // needs to be inverted.
563  __foregroundColor: "black"
564 
565  onClicked: {
566  if (!model.clickable) {
567  return;
568  }
569  quickList.state = "";
570  // Unsetting model to prevent showing changing entries during fading out
571  // that may happen because of triggering an action.
572  LauncherModel.quickListActionInvoked(quickList.appId, index);
573  quickList.model = undefined;
574  }
575  }
576  }
577  }
578  }
579 }