on df73bd5 help sign in
unity8/qml/Dash/GenericScopeView.qml
394 lines 16 kB history raw
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 */
17import QtQuick 2.0
18import Ubuntu.Components 0.1
19import Utils 0.1
20import Unity 0.1
21import "../Components"
22import "../Components/ListItems" as ListItems
24FocusScope {
25 id: scopeView
27 property Scope scope
28 property SortFilterProxyModel categories: categoryFilter
29 property bool isCurrent
30 property alias moving: categoryView.moving
31 property int tabBarHeight: 0
32 property PageHeader pageHeader: null
33 property OpenEffect openEffect: null
34 property Item previewListView: null
36 signal endReached
37 signal movementStarted
38 signal positionedAtBeginning
40 property bool enableHeightBehaviorOnNextCreation: false
41 property var categoryView: categoryView
43 // FIXME delay the search so that daemons have time to settle, note that
44 // removing this will break ScopeView::test_changeScope
45 onScopeChanged: {
46 if (scope) {
47 timer.restart();
48 scope.activateApplication.connect(activateApp);
49 }
50 }
52 function activateApp(appId) {
53 shell.activateApplication(appId);
54 }
56 Binding {
57 target: scope
58 property: "isActive"
59 value: isCurrent
60 }
62 Timer {
63 id: timer
64 interval: 2000
65 onTriggered: scope.searchQuery = ""
66 }
68 SortFilterProxyModel {
69 id: categoryFilter
70 model: scope ? scope.categories : null
71 dynamicSortFilter: true
72 filterRole: Categories.RoleCount
73 filterRegExp: /^0$/
74 invertMatch: true
75 }
77 onIsCurrentChanged: {
78 pageHeader.resetSearch();
79 previewListView.open = false;
80 }
82 onMovementStarted: categoryView.showHeader()
84 onPositionedAtBeginning: categoryView.positionAtBeginning()
86 Binding {
87 target: scopeView.scope
88 property: "searchQuery"
89 value: pageHeader.searchQuery
90 when: isCurrent
91 }
93 Binding {
94 target: pageHeader
95 property: "searchQuery"
96 value: scopeView.scope.searchQuery
97 when: isCurrent
98 }
100 Connections {
101 target: panel
102 onSearchClicked: if (isCurrent) {
103 pageHeader.triggerSearch()
104 categoryView.showHeader()
105 }
106 }
108 Connections {
109 target: scopeView.scope
110 onShowDash: previewListView.open = false;
111 onHideDash: previewListView.open = false;
112 }
114 ScopeListView {
115 id: categoryView
116 objectName: "categoryListView"
117 anchors.fill: parent
118 model: scopeView.categories
119 forceNoClip: previewListView.onScreen
121 onAtYEndChanged: if (atYEnd) endReached()
122 onMovingChanged: if (moving && atYEnd) endReached()
124 property string expandedCategoryId: ""
125 signal correctExpandedCategory();
127 onContentYChanged: pageHeader.positionRealHeader();
128 onOriginYChanged: pageHeader.positionRealHeader();
129 onContentHeightChanged: pageHeader.positionRealHeader();
131 Behavior on contentY {
132 enabled: previewListView.open
133 UbuntuNumberAnimation {}
134 }
136 delegate: ListItems.Base {
137 id: baseItem
138 objectName: "dashCategory" + category
139 highlightWhenPressed: false
141 readonly property bool expandable: rendererLoader.item ? rendererLoader.item.expandable : false
142 readonly property bool filtered: rendererLoader.item ? rendererLoader.item.filter : true
143 readonly property string category: categoryId
145 Loader {
146 id: rendererLoader
147 anchors {
148 top: parent.top
149 left: parent.left
150 right: parent.right
151 }
153 source: getRenderer(model.renderer, model.contentType, model.rendererHint, results)
155 onLoaded: {
156 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
157 item.enableHeightBehavior = scopeView.enableHeightBehaviorOnNextCreation;
158 scopeView.enableHeightBehaviorOnNextCreation = false;
159 }
160 if (source.toString().indexOf("Apps/RunningApplicationsGrid.qml") != -1) {
161 // TODO: the running apps grid doesn't support standard scope results model yet
162 item.firstModel = Qt.binding(function() { return results.firstModel })
163 item.secondModel = Qt.binding(function() { return results.secondModel })
164 item.canEnableTerminationMode = Qt.binding(function() { return scopeView.isCurrent })
165 } else {
166 item.model = Qt.binding(function() { return results })
167 }
168 item.objectName = Qt.binding(function() { return categoryId })
169 if (item.expandable) {
170 var shouldFilter = categoryId != categoryView.expandedCategoryId;
171 if (shouldFilter != item.filter) {
172 item.filter = shouldFilter;
173 }
174 }
175 updateDelegateCreationRange();
176 }
178 Component.onDestruction: {
179 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
180 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
181 }
182 }
184 Connections {
185 target: rendererLoader.item
186 onClicked: {
187 // Prepare the preview in case activate() triggers a preview only
188 openEffect.positionPx = Math.max(mapToItem(categoryView, 0, itemY).y, pageHeader.height + categoryView.stickyHeaderHeight);
189 previewListView.categoryId = categoryId
190 previewListView.categoryDelegate = rendererLoader.item
191 previewListView.model = target.model;
192 previewListView.init = true;
193 previewListView.currentIndex = index;
195 var item = target.model.get(index);
197 if ((scopeView.scope.id == "applications.scope" && categoryId == "installed")
198 || (scopeView.scope.id == "home.scope" && categoryId == "applications.scope")) {
199 scopeView.scope.activate(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
200 item.comment, item.dndUri, item.metadata)
201 } else {
202 previewListView.open = true
204 scopeView.scope.preview(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
205 item.comment, item.dndUri, item.metadata)
206 }
207 }
208 onPressAndHold: {
209 openEffect.positionPx = Math.max(mapToItem(categoryView, 0, itemY).y, pageHeader.height + categoryView.stickyHeaderHeight);
210 previewListView.categoryId = categoryId
211 previewListView.categoryDelegate = rendererLoader.item
212 previewListView.model = target.model;
213 previewListView.init = true;
214 previewListView.currentIndex = index;
215 previewListView.open = true
217 var item = target.model.get(index)
218 scopeView.scope.preview(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
219 item.comment, item.dndUri, item.metadata)
220 }
221 }
222 Connections {
223 target: categoryView
224 onExpandedCategoryIdChanged: {
225 collapseAllButExpandedCategory();
226 }
227 onCorrectExpandedCategory: {
228 collapseAllButExpandedCategory();
229 }
230 function collapseAllButExpandedCategory() {
231 var item = rendererLoader.item;
232 if (item.expandable) {
233 var shouldFilter = categoryId != categoryView.expandedCategoryId;
234 if (shouldFilter != item.filter) {
235 // If the filter animation will be seen start it, otherwise, just flip the switch
236 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
237 var growingVisible = !shouldFilter && y + height < categoryView.height;
238 if (!previewListView.open || !shouldFilter) {
239 if (shrinkingVisible || growingVisible) {
240 item.startFilterAnimation(shouldFilter)
241 } else {
242 item.filter = shouldFilter;
243 }
244 if (!shouldFilter && !previewListView.open) {
245 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
246 }
247 }
248 }
249 }
250 }
251 onContentYChanged: rendererLoader.updateDelegateCreationRange();
252 onHeightChanged: rendererLoader.updateDelegateCreationRange();
253 }
255 function updateDelegateCreationRange() {
256 // Do not update the range if we are overshooting up or down, since we'll come back
257 // to the stable position and delete/create items without any reason
258 if (categoryView.contentY < categoryView.originY) {
259 return;
260 } else if (categoryView.contentY + categoryView.height > categoryView.contentHeight) {
261 return;
262 }
264 if (item.hasOwnProperty("delegateCreationBegin")) {
265 if (baseItem.y + baseItem.height <= 0) {
266 // Not visible (item at top of the list)
267 item.delegateCreationBegin = baseItem.height
268 item.delegateCreationEnd = baseItem.height
269 } else if (baseItem.y >= categoryView.height) {
270 // Not visible (item at bottom of the list)
271 item.delegateCreationBegin = 0
272 item.delegateCreationEnd = 0
273 } else {
274 item.delegateCreationBegin = Math.max(-baseItem.y, 0)
275 item.delegateCreationEnd = Math.min(categoryView.height + item.delegateCreationBegin, baseItem.height)
276 }
277 }
278 }
279 }
280 }
282 sectionProperty: "name"
283 sectionDelegate: ListItems.Header {
284 objectName: "dashSectionHeader" + delegate.category
285 property var delegate: categoryView.item(delegateIndex)
286 width: categoryView.width
287 text: section
288 image: {
289 if (delegate && delegate.expandable)
290 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
291 return "";
292 }
293 onClicked: {
294 if (categoryView.expandedCategoryId != delegate.category)
295 categoryView.expandedCategoryId = delegate.category;
296 else
297 categoryView.expandedCategoryId = "";
298 }
299 }
300 pageHeader: Item {
301 implicitHeight: scopeView.tabBarHeight
302 onHeightChanged: {
303 if (scopeView.pageHeader) {
304 scopeView.pageHeader.height = height;
305 }
306 }
307 onYChanged: positionRealHeader();
309 function positionRealHeader() {
310 if (scopeView.pageHeader) {
311 scopeView.pageHeader.y = y + parent.y;
312 }
313 }
314 }
315 }
317 function getDefaultRendererId(contentType) {
318 switch (contentType) {
319 default: return "grid";
320 }
321 }
323 function getRenderer(rendererId, contentType, rendererHint, results) {
324 if (rendererId == "default") {
325 rendererId = getDefaultRendererId(contentType);
326 }
327 if (rendererId == "carousel") {
328 // Selectively disable carousel, 6 is fixed for now, should change on the form factor
329 if (results.count <= 6)
330 rendererId = "grid"
331 }
332 switch (rendererId) {
333 case "carousel": {
334 switch (contentType) {
335 case "music": return "Music/MusicCarousel.qml";
336 case "video": return "Video/VideoCarousel.qml";
337 default: return "Generic/GenericCarousel.qml";
338 }
339 }
340 case "grid": {
341 switch (contentType) {
342 case "apps": {
343 if (rendererHint == "toggled")
344 return "Apps/DashPluginFilterGrid.qml";
345 else
346 return "Generic/GenericFilterGrid.qml";
347 }
348 case "music": return "Music/MusicFilterGrid.qml";
349 case "video": return "Video/VideoFilterGrid.qml";
350 case "weather": return "Generic/WeatherFilterGrid.qml";
351 default: return "Generic/GenericFilterGrid.qml";
352 }
353 }
354 case "special": {
355 switch (contentType) {
356 case "apps": return "Apps/RunningApplicationsGrid.qml";
357 default: return "Generic/GenericFilterGrid.qml";
358 }
359 }
360 default: return "Generic/GenericFilterGrid.qml";
361 }
362 }
364 Connections {
365 target: scopeView.scope
366 onPreviewReady: {
367 if (previewListView.init) {
368 // Preview was triggered because of a click on the item. Need to expand now.
369 if (!previewListView.open) {
370 previewListView.open = true
371 }
373 var index = previewListView.currentIndex
374 previewListView.currentIndex = -1
375 previewListView.currentIndex = index
376 previewListView.init = false
377 }
378 previewListView.currentItem.previewData = preview
379 }
380 }
382 // TODO: Move as InverseMouseArea to DashPreview
383 MouseArea {
384 objectName: "closePreviewMouseArea"
385 enabled: previewListView.onScreen
386 anchors {
387 fill: parent
388 topMargin: openEffect.bottomGapPx
389 }
390 onClicked: {
391 previewListView.open = false;
392 }
393 }