on 79a23b2 help sign in
unity8/Dash/GenericScopeView.qml
530 lines 21 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 "../Components"
20import "../Components/ListItems" as ListItems
22ScopeView {
23 id: scopeView
24 readonly property alias previewShown: previewListView.onScreen
25 property bool enableHeightBehaviorOnNextCreation: false
27 moving: categoryView.moving
29 onIsCurrentChanged: {
30 pageHeader.resetSearch();
31 previewListView.open = false;
32 }
34 onMovementStarted: categoryView.showHeader()
36 onPositionedAtBeginning: categoryView.positionAtBeginning()
38 Binding {
39 target: scopeView.scope
40 property: "searchQuery"
41 value: pageHeader.searchQuery
42 }
44 Binding {
45 target: pageHeader
46 property: "searchQuery"
47 value: scopeView.scope.searchQuery
48 }
50 Connections {
51 target: panel
52 onSearchClicked: if (isCurrent) {
53 pageHeader.triggerSearch()
54 categoryView.showHeader()
55 }
56 }
58 Connections {
59 target: scopeView.scope
60 onShowDash: previewListView.open = false;
61 onHideDash: previewListView.open = false;
62 }
64 ScopeListView {
65 id: categoryView
66 objectName: "categoryListView"
67 anchors.fill: parent
68 model: scopeView.categories
69 forceNoClip: previewListView.onScreen
71 onAtYEndChanged: if (atYEnd) endReached()
72 onMovingChanged: if (moving && atYEnd) endReached()
74 property string expandedCategoryId: ""
75 signal correctExpandedCategory();
77 Behavior on contentY {
78 enabled: previewListView.open
79 UbuntuNumberAnimation {}
80 }
82 delegate: ListItems.Base {
83 id: baseItem
84 highlightWhenPressed: false
86 readonly property bool expandable: rendererLoader.item ? rendererLoader.item.expandable : false
87 readonly property bool filtered: rendererLoader.item ? rendererLoader.item.filter : true
88 readonly property string category: categoryId
90 Loader {
91 id: rendererLoader
92 anchors {
93 top: parent.top
94 left: parent.left
95 right: parent.right
96 }
98 source: getRenderer(model.renderer, model.contentType, model.rendererHint)
100 onLoaded: {
101 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
102 item.enableHeightBehavior = scopeView.enableHeightBehaviorOnNextCreation;
103 scopeView.enableHeightBehaviorOnNextCreation = false;
104 }
105 if (source.toString().indexOf("Apps/RunningApplicationsGrid.qml") != -1) {
106 // TODO: the running apps grid doesn't support standard scope results model yet
107 item.firstModel = Qt.binding(function() { return results.firstModel })
108 item.secondModel = Qt.binding(function() { return results.secondModel })
109 item.canEnableTerminationMode = Qt.binding(function() { return scopeView.isCurrent })
110 } else {
111 item.model = Qt.binding(function() { return results })
112 }
113 item.objectName = Qt.binding(function() { return categoryId })
114 if (item.expandable) {
115 var shouldFilter = categoryId != categoryView.expandedCategoryId;
116 if (shouldFilter != item.filter) {
117 item.filter = shouldFilter;
118 }
119 }
120 updateDelegateCreationRange();
121 }
123 Component.onDestruction: {
124 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
125 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
126 }
127 }
129 Connections {
130 target: rendererLoader.item
131 onClicked: {
132 // Prepare the preview in case activate() triggers a preview only
133 effect.positionPx = mapToItem(categoryView, 0, itemY).y
134 previewListView.categoryId = categoryId
135 previewListView.categoryDelegate = rendererLoader.item
136 previewListView.model = target.model;
137 previewListView.init = true;
138 previewListView.currentIndex = index;
140 var item = target.model.get(index);
142 if ((scopeView.scope.id == "applications.scope" && categoryId == "installed")
143 || (scopeView.scope.id == "home.scope" && categoryId == "applications.scope")) {
144 scopeView.scope.activate(item.uri, item.icon, item.category, 0, item.mimetype, item.title,
145 item.comment, item.dndUri, item.metadata)
146 } else {
147 previewListView.open = true
149 scopeView.scope.preview( item.uri, item.icon, item.category, 0, item.mimetype, item.title,
150 item.comment, item.dndUri, item.metadata)
151 }
152 }
153 onPressAndHold: {
154 effect.positionPx = mapToItem(categoryView, 0, itemY).y
155 previewListView.categoryId = categoryId
156 previewListView.categoryDelegate = rendererLoader.item
157 previewListView.model = target.model;
158 previewListView.init = true;
159 previewListView.currentIndex = index;
160 previewListView.open = true
162 var item = target.model.get(index)
163 scopeView.scope.preview( item.uri, item.icon, item.category, 0, item.mimetype, item.title,
164 item.comment, item.dndUri, item.metadata)
165 }
166 }
167 Connections {
168 target: categoryView
169 onExpandedCategoryIdChanged: {
170 collapseAllButExpandedCategory();
171 }
172 onCorrectExpandedCategory: {
173 collapseAllButExpandedCategory();
174 }
175 function collapseAllButExpandedCategory() {
176 var item = rendererLoader.item;
177 if (item.expandable) {
178 var shouldFilter = categoryId != categoryView.expandedCategoryId;
179 if (shouldFilter != item.filter) {
180 // If the filter animation will be seen start it, otherwise, just flip the switch
181 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
182 var growingVisible = !shouldFilter && y + height < categoryView.height;
183 if (!previewListView.open || !shouldFilter) {
184 if (shrinkingVisible || growingVisible) {
185 item.startFilterAnimation(shouldFilter)
186 } else {
187 item.filter = shouldFilter;
188 }
189 if (!shouldFilter && !previewListView.open) {
190 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
191 }
192 }
193 }
194 }
195 }
196 onContentYChanged: rendererLoader.updateDelegateCreationRange();
197 onHeightChanged: rendererLoader.updateDelegateCreationRange();
198 }
200 function updateDelegateCreationRange() {
201 // Do not update the range if we are overshooting up or down, since we'll come back
202 // to the stable position and delete/create items without any reason
203 if (categoryView.contentY < categoryView.originY) {
204 return;
205 } else if (categoryView.contentY + categoryView.height > categoryView.contentHeight) {
206 return;
207 }
209 if (item.hasOwnProperty("delegateCreationBegin")) {
210 if (baseItem.y + baseItem.height <= 0) {
211 // Not visible (item at top of the list)
212 item.delegateCreationBegin = baseItem.height
213 item.delegateCreationEnd = baseItem.height
214 } else if (baseItem.y >= categoryView.height) {
215 // Not visible (item at bottom of the list)
216 item.delegateCreationBegin = 0
217 item.delegateCreationEnd = 0
218 } else {
219 item.delegateCreationBegin = Math.max(-baseItem.y, 0)
220 item.delegateCreationEnd = Math.min(categoryView.height + item.delegateCreationBegin, baseItem.height)
221 }
222 }
223 }
224 }
225 }
227 sectionProperty: "name"
228 sectionDelegate: ListItems.Header {
229 property var delegate: categoryView.item(delegateIndex)
230 width: categoryView.width
231 text: section
232 image: {
233 if (delegate && delegate.expandable)
234 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
235 return "";
236 }
237 onClicked: {
238 if (categoryView.expandedCategoryId != delegate.category)
239 categoryView.expandedCategoryId = delegate.category;
240 else
241 categoryView.expandedCategoryId = "";
242 }
243 }
244 pageHeader: PageHeader {
245 id: pageHeader
246 objectName: "pageHeader"
247 width: categoryView.width
248 text: scopeView.scope.name
249 searchEntryEnabled: true
250 scope: scopeView.scope
251 }
252 }
254 function getDefaultRendererId(contentType) {
255 switch (contentType) {
256 default: return "grid";
257 }
258 }
260 function getRenderer(rendererId, contentType, rendererHint) {
261 if (rendererId == "default") {
262 rendererId = getDefaultRendererId(contentType);
263 }
264 switch (rendererId) {
265 case "carousel": {
266 switch (contentType) {
267 case "music": return "Music/MusicCarouselLoader.qml";
268 case "video": return "Video/VideoCarouselLoader.qml";
269 default: return "Generic/GenericCarouselLoader.qml";
270 }
271 }
272 case "grid": {
273 switch (contentType) {
274 case "apps": {
275 if (rendererHint == "toggled")
276 return "Apps/DashPluginFilterGrid.qml";
277 else
278 return "Generic/GenericFilterGrid.qml";
279 }
280 case "music": return "Music/MusicFilterGrid.qml";
281 case "video": return "Video/VideoFilterGrid.qml";
282 case "weather": return "Generic/WeatherFilterGrid.qml";
283 default: return "Generic/GenericFilterGrid.qml";
284 }
285 }
286 case "special": {
287 switch (contentType) {
288 case "apps": return "Apps/RunningApplicationsGrid.qml";
289 default: return "Generic/GenericFilterGrid.qml";
290 }
291 }
292 default: return "Generic/GenericFilterGrid.qml";
293 }
294 }
296 OpenEffect {
297 id: effect
298 objectName: "openEffect"
299 anchors {
300 fill: parent
301 bottomMargin: -bottomOverflow
302 }
303 sourceItem: categoryView
305 enabled: gap > 0.0
307 topGapPx: (1 - gap) * positionPx
308 topOpacity: (1 - gap * 1.2)
309 bottomGapPx: positionPx + gap * (targetBottomGapPx - positionPx)
310 bottomOverflow: units.gu(20)
311 live: !expansionAnimation.running
313 property int targetBottomGapPx: height - units.gu(8) - bottomOverflow
314 property real gap: previewListView.open ? 1.0 : 0.0
316 Behavior on gap {
317 NumberAnimation {
318 id: expansionAnimation
319 duration: 200
320 easing.type: Easing.InOutQuad
321 onRunningChanged: {
322 if (!previewListView.open && !running) {
323 previewListView.onScreen = false
324 }
325 }
326 }
327 }
328 Behavior on positionPx {
329 enabled: previewListView.open
330 UbuntuNumberAnimation {}
331 }
332 }
334 Connections {
335 target: scopeView.scope
336 onPreviewReady: {
337 if (previewListView.init) {
338 // Preview was triggered because of a click on the item. Need to expand now.
339 if (!previewListView.open) {
340 previewListView.open = true
341 }
343 var index = previewListView.currentIndex
344 previewListView.currentIndex = -1
345 previewListView.currentIndex = index
346 previewListView.init = false
347 }
348 previewListView.currentItem.previewData = preview
349 }
350 }
352 PreviewDelegateMapper {
353 id: previewDelegateMapper
354 }
356 ListView {
357 id: previewListView
358 objectName: "previewListView"
359 height: effect.bottomGapPx - effect.topGapPx
360 anchors {
361 top: parent.top
362 topMargin: effect.topGapPx
363 left: parent.left
364 right: parent.right
365 }
366 orientation: ListView.Horizontal
367 highlightRangeMode: ListView.StrictlyEnforceRange
368 snapMode: ListView.SnapOneItem
369 boundsBehavior: Flickable.DragAndOvershootBounds
370 highlightMoveDuration: 250
371 flickDeceleration: units.gu(625)
372 maximumFlickVelocity: width * 5
373 cacheBuffer: 0
375 // To be set before opening the preview
376 property string categoryId: ""
377 property var categoryDelegate
379 // because the ListView is built asynchronous, setting the
380 // currentIndex directly won't work. We need to refresh it
381 // when the first preview is ready to be displayed.
382 property bool init: true
384 onCurrentIndexChanged: {
385 var row = Math.floor(currentIndex / categoryDelegate.columns);
386 if (categoryDelegate.collapsedRowCount <= row) {
387 categoryView.expandedCategoryId = categoryId
388 }
390 if (open) {
391 categoryDelegate.highlightIndex = currentIndex
392 }
394 if (!init && model !== undefined) {
395 var item = model.get(currentIndex)
396 scopeView.scope.preview( item.uri, item.icon, item.category, 0, item.mimetype, item.title, item.comment, item.dndUri, item.metadata)
397 }
399 var itemY = categoryView.contentItem.mapFromItem(categoryDelegate.currentItem).y;
400 var newContentY = itemY - effect.positionPx - categoryDelegate.verticalSpacing;
401 var effectAdjust = effect.positionPx;
402 if (newContentY < 0) {
403 effectAdjust += newContentY;
404 newContentY = 0;
405 }
406 if (newContentY > Math.max(0, categoryView.contentHeight - categoryView.height)) {
407 effectAdjust += -(categoryView.contentHeight - categoryView.height) + newContentY
408 newContentY = categoryView.contentHeight - categoryView.height;
409 }
411 effect.positionPx = effectAdjust;
412 categoryView.contentY = newContentY - categoryView.originY;
413 }
415 property bool open: false
416 property bool onScreen: false
418 onOpenChanged: {
419 if (open) {
420 onScreen = true;
421 categoryDelegate.highlightIndex = currentIndex;
422 pageHeader.unfocus();
423 } else {
424 // Cancel any pending preview requests or actions
425 if (previewListView.currentItem.previewData !== undefined) {
426 previewListView.currentItem.previewData.cancelAction();
427 }
428 scopeView.scope.cancelActivation();
429 model = undefined;
430 categoryView.correctExpandedCategory();
431 categoryDelegate.highlightIndex = -1;
432 }
433 }
435 Rectangle {
436 anchors.fill: parent
437 color: Qt.rgba(0, 0, 0, .3)
438 z: -1
439 }
441 delegate: Loader {
442 id: previewLoader
443 objectName: "previewLoader" + index
444 height: previewListView.height
445 width: previewListView.width
446 asynchronous: true
447 source: previewListView.onScreen ?
448 (previewData !== undefined ? previewDelegateMapper.map(previewData.rendererName) : "DashPreviewPlaceholder.qml") : ""
450 onPreviewDataChanged: {
451 if (previewData !== undefined && source.toString().indexOf("DashPreviewPlaceholder.qml") != -1) {
452 previewLoader.opacity = 0;
453 }
454 }
456 onSourceChanged: {
457 if (previewData !== undefined) {
458 fadeIn.start()
459 }
460 }
462 PropertyAnimation {
463 id: fadeIn
464 target: previewLoader
465 property: "opacity"
466 from: 0.0
467 to: 1.0
468 duration: UbuntuAnimation.BriskDuration
469 }
471 property var previewData
472 property bool valid: item !== null
474 onLoaded: {
475 if (previewListView.onScreen && previewData !== undefined) {
476 item.previewData = Qt.binding(function() { return previewData })
477 }
478 }
480 Connections {
481 ignoreUnknownSignals: true
482 target: item
483 onClose: {
484 previewListView.open = false
485 }
486 }
488 function closePreviewSpinner() {
489 if(item) {
490 item.showProcessingAction = false;
491 }
492 }
493 }
494 }
496 Image {
497 objectName: "pointerArrow"
498 anchors {
499 top: previewListView.bottom
500 left: parent.left
501 leftMargin: previewListView.categoryDelegate !== undefined && previewListView.categoryDelegate.currentItem ?
502 previewListView.categoryDelegate.currentItem.center + (-width + margins) / 2 : 0
504 Behavior on leftMargin {
505 SmoothedAnimation {
506 duration: UbuntuAnimation.FastDuration
507 }
508 }
509 }
510 height: units.gu(1)
511 width: units.gu(2)
512 property int margins: previewListView.categoryDelegate ? previewListView.categoryDelegate.margins : 0
513 opacity: previewListView.open ? .5 : 0
515 source: "graphics/tooltip_arrow.png"
516 }
518 // TODO: Move as InverseMouseArea to DashPreview
519 MouseArea {
520 objectName: "closePreviewMouseArea"
521 enabled: previewListView.onScreen
522 anchors {
523 fill: parent
524 topMargin: effect.bottomGapPx
525 }
526 onClicked: {
527 previewListView.open = false;
528 }
529 }