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