Unity 8
DashContent.qml
1 /*
2  * Copyright (C) 2013, 2014 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 Unity 0.2
20 import Utils 0.1
21 import "../Components"
22 
23 Item {
24  id: dashContent
25 
26  property bool forceNonInteractive: false
27  property alias scopes: dashContentList.model
28  property alias currentIndex: dashContentList.currentIndex
29  property int workaroundRestoreIndex: -1
30  readonly property string currentScopeId: dashContentList.currentItem ? dashContentList.currentItem.scopeId : ""
31  readonly property var currentScope: dashContentList.currentItem ? dashContentList.currentItem.theScope : null
32  readonly property bool subPageShown: dashContentList.currentItem && dashContentList.currentItem.item ?
33  dashContentList.currentItem.item.subPageShown : false
34  readonly property bool processing: dashContentList.currentItem && dashContentList.currentItem.item
35  && dashContentList.currentItem.item.processing || false
36  readonly property bool pageHeaderTotallyVisible: dashContentList.currentItem && dashContentList.currentItem.item
37  && dashContentList.currentItem.item.pageHeaderTotallyVisible || false
38 
39  signal scopeLoaded(string scopeId)
40  signal gotoScope(string scopeId)
41  signal openScope(var scope)
42  signal closePreview()
43 
44  // If we set the current scope index before the scopes have been added,
45  // then we need to wait until the loaded signals gets emitted from the scopes
46  property var set_current_index: undefined
47  Connections {
48  target: scopes
49  onLoadedChanged: {
50  if (scopes.loaded && set_current_index != undefined) {
51  setCurrentScopeAtIndex(set_current_index[0], set_current_index[1], set_current_index[2]);
52  set_current_index = undefined;
53  }
54  }
55  onRowsMoved: {
56  // FIXME This is to workaround a Qt bug with the model moving the current item
57  // when the list is ListView.SnapOneItem and ListView.StrictlyEnforceRange
58  // together with the code in Dash.qml
59  if (row == dashContentList.currentIndex || start == dashContentList.currentIndex) {
60  dashContent.workaroundRestoreIndex = dashContentList.currentIndex;
61  dashContentList.currentIndex = -1;
62  }
63  }
64  }
65 
66  function setCurrentScopeAtIndex(index, animate, reset) {
67  // if the scopes haven't loaded yet, then wait until they are.
68  if (!scopes.loaded) {
69  set_current_index = [ index, animate, reset ]
70  return;
71  }
72 
73  var storedMoveDuration = dashContentList.highlightMoveDuration
74  var storedMoveSpeed = dashContentList.highlightMoveVelocity
75  if (!animate) {
76  dashContentList.highlightMoveVelocity = units.gu(4167)
77  dashContentList.highlightMoveDuration = 0
78  }
79 
80  set_current_index = undefined;
81 
82  if (dashContentList.count > index) {
83  dashContentList.currentIndex = index
84 
85  if (reset) {
86  dashContentList.currentItem.item.positionAtBeginning()
87  dashContentList.currentItem.item.resetSearch()
88  }
89  }
90 
91  if (!animate) {
92  dashContentList.highlightMoveDuration = storedMoveDuration
93  dashContentList.highlightMoveVelocity = storedMoveSpeed
94  }
95  }
96 
97  Item {
98  id: dashContentListHolder
99 
100  anchors.fill: parent
101 
102  DashBackground {
103  anchors.fill: parent
104  }
105 
106  ListView {
107  id: dashContentList
108  objectName: "dashContentList"
109 
110  interactive: !dashContent.forceNonInteractive && dashContent.scopes.loaded && currentItem
111  && !currentItem.moving && !currentItem.subPageShown && !currentItem.extraPanelShown
112  anchors.fill: parent
113  orientation: ListView.Horizontal
114  boundsBehavior: Flickable.DragAndOvershootBounds
115  snapMode: ListView.SnapOneItem
116  highlightMoveDuration: 250
117  highlightRangeMode: ListView.StrictlyEnforceRange
118  // TODO Investigate if we can switch to a smaller cache buffer when/if UbuntuShape gets more performant
119  // 1073741823 is s^30 -1. A quite big number so that you have "infinite" cache, but not so
120  // big so that if you add if with itself you're outside the 2^31 int range
121  cacheBuffer: 1073741823
122  onMovementStarted: currentItem.item.showHeader();
123  clip: parent.x != 0
124 
125  // TODO QTBUG-40846 and QTBUG-40848
126  // The remove transition doesn't happen when removing the last item
127  // And can't work around it because index is reset to -1 regardless of
128  // ListView.delayRemove
129 
130  remove: Transition {
131  SequentialAnimation {
132  PropertyAction { property: "layer.enabled"; value: true }
133  PropertyAction { property: "ListView.delayRemove"; value: true }
134  ParallelAnimation {
135  PropertyAnimation { properties: "scale"; to: 0.25; duration: UbuntuAnimation.SnapDuration }
136  PropertyAnimation { properties: "y"; to: dashContent.height; duration: UbuntuAnimation.SnapDuration }
137  }
138  PropertyAction { property: "ListView.delayRemove"; value: false }
139  }
140  }
141  removeDisplaced: Transition {
142  PropertyAnimation { property: "x"; duration: UbuntuAnimation.SnapDecision }
143  }
144 
145  // If the number of items is less than the current index, then need to reset to another item.
146  onCountChanged: {
147  if (count > 0) {
148  if (currentIndex >= count) {
149  dashContent.setCurrentScopeAtIndex(count-1, true, true)
150  } else if (currentIndex < 0) {
151  // setting currentIndex directly, cause we don't want to loose set_current_index
152  dashContentList.currentIndex = 0
153  }
154  }
155  }
156 
157  delegate:
158  Loader {
159  id: loader
160  width: ListView.view.width
161  height: ListView.view.height
162  opacity: { // hide delegate if offscreen
163  var xPositionRelativetoView = ListView.view.contentX - x
164  return (xPositionRelativetoView > -width && xPositionRelativetoView < width) ? 1 : 0
165  }
166  asynchronous: true
167  source: "GenericScopeView.qml"
168  objectName: "scopeLoader" + index
169 
170  readonly property bool moving: item ? item.moving : false
171  readonly property bool extraPanelShown: item ? item.extraPanelShown : false
172  readonly property bool subPageShown: item ? item.subPageShown : false
173  readonly property var categoryView: item ? item.categoryView : null
174  readonly property var theScope: scope
175 
176  // these are needed for autopilot tests
177  readonly property string scopeId: scope.id
178  readonly property bool isCurrent: ListView.isCurrentItem
179  readonly property bool isLoaded: status == Loader.Ready
180 
181  onLoaded: {
182  item.objectName = scope.id
183  item.scope = Qt.binding(function() { return scope })
184  item.isCurrent = Qt.binding(function() { return visible && ListView.isCurrentItem })
185  dashContent.scopeLoaded(item.scope.id)
186  item.paginationCount = Qt.binding(function() { return dashContentList.count } )
187  item.paginationIndex = Qt.binding(function() { return dashContentList.currentIndex } )
188  item.visibleToParent = Qt.binding(function() { return loader.opacity != 0 });
189  item.holdingList = dashContentList;
190  item.forceNonInteractive = Qt.binding(function() { return dashContent.forceNonInteractive } )
191  }
192  Connections {
193  target: isCurrent ? scope : null
194  onGotoScope: {
195  // Note here scopeId is the signal parameter and not the loader property
196  dashContent.gotoScope(scopeId);
197  }
198  onOpenScope: {
199  dashContent.openScope(scope);
200  }
201  }
202  Connections {
203  target: dashContent
204  onClosePreview: if (item) item.closePreview()
205  }
206 
207  Component.onDestruction: active = false
208  }
209  }
210  }
211 }