on 551c708 help sign in
unity8/qml/Dash/GenericScopeView.qml
400 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
144 readonly property var item: rendererLoader.item
146 Loader {
147 id: rendererLoader
148 anchors {
149 top: parent.top
150 left: parent.left
151 right: parent.right
152 }
154 source: getRenderer(model.renderer, model.contentType, model.rendererHint, results)
156 onLoaded: {
157 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
158 item.enableHeightBehavior = scopeView.enableHeightBehaviorOnNextCreation;
159 scopeView.enableHeightBehaviorOnNextCreation = false;
160 }
161 if (source.toString().indexOf("Apps/RunningApplicationsGrid.qml") != -1) {
162 // TODO: the running apps grid doesn't support standard scope results model yet
163 item.firstModel = Qt.binding(function() { return results.firstModel })
164 item.secondModel = Qt.binding(function() { return results.secondModel })
165 item.canEnableTerminationMode = Qt.binding(function() { return scopeView.isCurrent })
166 } else {
167 item.model = Qt.binding(function() { return results })
168 }
169 item.objectName = Qt.binding(function() { return categoryId })
170 if (item.expandable) {
171 var shouldFilter = categoryId != categoryView.expandedCategoryId;
172 if (shouldFilter != item.filter) {
173 item.filter = shouldFilter;
174 }
175 }
176 updateDelegateCreationRange();
177 }
179 Component.onDestruction: {
180 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
181 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
182 }
183 }
185 Connections {
186 target: rendererLoader.item
187 onClicked: {
188 // Prepare the preview in case activate() triggers a preview only
189 openEffect.positionPx = Math.max(mapToItem(categoryView, 0, itemY).y, pageHeader.height + categoryView.stickyHeaderHeight);
190 previewListView.categoryId = categoryId
191 previewListView.categoryDelegate = rendererLoader.item
192 previewListView.model = target.model;
193 previewListView.init = true;
194 previewListView.currentIndex = index;
196 var item = target.model.get(index);
198 if ((scopeView.scope.id == "applications.scope" && categoryId == "installed")
199 || (scopeView.scope.id == "home.scope" && categoryId == "applications.scope")) {
200 scopeView.scope.activate(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
201 item.comment, item.dndUri, item.metadata)
202 } else {
203 previewListView.open = true
205 scopeView.scope.preview(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
206 item.comment, item.dndUri, item.metadata)
207 }
208 }
209 onPressAndHold: {
210 openEffect.positionPx = Math.max(mapToItem(categoryView, 0, itemY).y, pageHeader.height + categoryView.stickyHeaderHeight);
211 previewListView.categoryId = categoryId
212 previewListView.categoryDelegate = rendererLoader.item
213 previewListView.model = target.model;
214 previewListView.init = true;
215 previewListView.currentIndex = index;
216 previewListView.open = true
218 var item = target.model.get(index)
219 scopeView.scope.preview(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
220 item.comment, item.dndUri, item.metadata)
221 }
222 }
223 Connections {
224 target: categoryView
225 onExpandedCategoryIdChanged: {
226 collapseAllButExpandedCategory();
227 }
228 onCorrectExpandedCategory: {
229 collapseAllButExpandedCategory();
230 }
231 function collapseAllButExpandedCategory() {
232 var item = rendererLoader.item;
233 if (item.expandable) {
234 var shouldFilter = categoryId != categoryView.expandedCategoryId;
235 if (shouldFilter != item.filter) {
236 // If the filter animation will be seen start it, otherwise, just flip the switch
237 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
238 var growingVisible = !shouldFilter && y + height < categoryView.height;
239 if (!previewListView.open || !shouldFilter) {
240 if (shrinkingVisible || growingVisible) {
241 item.startFilterAnimation(shouldFilter)
242 } else {
243 item.filter = shouldFilter;
244 }
245 if (!shouldFilter && !previewListView.open) {
246 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
247 }
248 }
249 }
250 }
251 }
252 onOriginYChanged: rendererLoader.updateDelegateCreationRange();
253 onContentYChanged: rendererLoader.updateDelegateCreationRange();
254 onHeightChanged: rendererLoader.updateDelegateCreationRange();
255 onContentHeightChanged: rendererLoader.updateDelegateCreationRange();
256 }
258 function updateDelegateCreationRange() {
259 // Do not update the range if we are overshooting up or down, since we'll come back
260 // to the stable position and delete/create items without any reason
261 if (categoryView.contentY < categoryView.originY) {
262 return;
263 } else if (categoryView.contentY + categoryView.height > categoryView.contentHeight) {
264 return;
265 }
267 if (item && item.hasOwnProperty("delegateCreationBegin")) {
268 if (baseItem.y + baseItem.height <= 0) {
269 // Not visible (item at top of the list)
270 item.delegateCreationBegin = baseItem.height
271 item.delegateCreationEnd = baseItem.height
272 } else if (baseItem.y >= categoryView.height) {
273 // Not visible (item at bottom of the list)
274 item.delegateCreationBegin = 0
275 item.delegateCreationEnd = 0
276 } else {
277 item.delegateCreationBegin = Math.max(-baseItem.y, 0)
278 item.delegateCreationEnd = Math.min(categoryView.height + item.delegateCreationBegin, baseItem.height)
279 }
280 }
281 }
282 }
284 onHeightChanged: rendererLoader.updateDelegateCreationRange();
285 onYChanged: rendererLoader.updateDelegateCreationRange();
286 }
288 sectionProperty: "name"
289 sectionDelegate: ListItems.Header {
290 objectName: "dashSectionHeader" + delegate.category
291 property var delegate: categoryView.item(delegateIndex)
292 width: categoryView.width
293 text: section
294 image: {
295 if (delegate && delegate.expandable)
296 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
297 return "";
298 }
299 onClicked: {
300 if (categoryView.expandedCategoryId != delegate.category)
301 categoryView.expandedCategoryId = delegate.category;
302 else
303 categoryView.expandedCategoryId = "";
304 }
305 }
306 pageHeader: Item {
307 implicitHeight: scopeView.tabBarHeight
308 onHeightChanged: {
309 if (scopeView.pageHeader) {
310 scopeView.pageHeader.height = height;
311 }
312 }
313 onYChanged: positionRealHeader();
315 function positionRealHeader() {
316 if (scopeView.pageHeader) {
317 scopeView.pageHeader.y = y + parent.y;
318 }
319 }
320 }
321 }
323 function getDefaultRendererId(contentType) {
324 switch (contentType) {
325 default: return "grid";
326 }
327 }
329 function getRenderer(rendererId, contentType, rendererHint, results) {
330 if (rendererId == "default") {
331 rendererId = getDefaultRendererId(contentType);
332 }
333 if (rendererId == "carousel") {
334 // Selectively disable carousel, 6 is fixed for now, should change on the form factor
335 if (results.count <= 6)
336 rendererId = "grid"
337 }
338 switch (rendererId) {
339 case "carousel": {
340 switch (contentType) {
341 case "music": return "Music/MusicCarousel.qml";
342 case "video": return "Video/VideoCarousel.qml";
343 default: return "Generic/GenericCarousel.qml";
344 }
345 }
346 case "grid": {
347 switch (contentType) {
348 case "apps": {
349 if (rendererHint == "toggled")
350 return "Apps/DashPluginFilterGrid.qml";
351 else
352 return "Generic/GenericFilterGrid.qml";
353 }
354 case "music": return "Music/MusicFilterGrid.qml";
355 case "video": return "Video/VideoFilterGrid.qml";
356 case "weather": return "Generic/WeatherFilterGrid.qml";
357 default: return "Generic/GenericFilterGrid.qml";
358 }
359 }
360 case "special": {
361 switch (contentType) {
362 case "apps": return "Apps/RunningApplicationsGrid.qml";
363 default: return "Generic/GenericFilterGrid.qml";
364 }
365 }
366 default: return "Generic/GenericFilterGrid.qml";
367 }
368 }
370 Connections {
371 target: scopeView.scope
372 onPreviewReady: {
373 if (previewListView.init) {
374 // Preview was triggered because of a click on the item. Need to expand now.
375 if (!previewListView.open) {
376 previewListView.open = true
377 }
379 var index = previewListView.currentIndex
380 previewListView.currentIndex = -1
381 previewListView.currentIndex = index
382 previewListView.init = false
383 }
384 previewListView.currentItem.previewData = preview
385 }
386 }
388 // TODO: Move as InverseMouseArea to DashPreview
389 MouseArea {
390 objectName: "closePreviewMouseArea"
391 enabled: previewListView.onScreen
392 anchors {
393 fill: parent
394 topMargin: openEffect.bottomGapPx
395 }
396 onClicked: {
397 previewListView.open = false;
398 }
399 }