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