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