on 2b5d9df help sign in
unity8/Dash/GenericScopeView.qml
368 lines 14 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: previewLoader.onScreen
25 property bool enableHeightBehaviorOnNextCreation: false
27 moving: categoryView.moving
29 onIsCurrentChanged: {
30 pageHeader.resetSearch();
31 previewLoader.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: previewLoader.open = false;
61 onHideDash: previewLoader.open = false;
62 onActivated: previewLoader.closePreviewSpinner();
63 }
65 ScopeListView {
66 id: categoryView
67 objectName: "categoryListView"
68 anchors.fill: parent
69 model: scopeView.categories
70 forceNoClip: previewLoader.onScreen
72 onAtYEndChanged: if (atYEnd) endReached()
73 onMovingChanged: if (moving && atYEnd) endReached()
75 property string expandedCategoryId: ""
77 delegate: ListItems.Base {
78 id: baseItem
79 highlightWhenPressed: false
81 readonly property bool expandable: rendererLoader.item ? rendererLoader.item.expandable : false
82 readonly property bool filtered: rendererLoader.item ? rendererLoader.item.filter : true
83 readonly property string category: categoryId
85 Loader {
86 id: rendererLoader
87 anchors {
88 top: parent.top
89 left: parent.left
90 right: parent.right
91 }
93 source: getRenderer(model.renderer, model.contentType, model.rendererHint)
95 onLoaded: {
96 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
97 item.enableHeightBehavior = scopeView.enableHeightBehaviorOnNextCreation;
98 scopeView.enableHeightBehaviorOnNextCreation = false;
99 }
100 if (source.toString().indexOf("Apps/RunningApplicationsGrid.qml") != -1) {
101 // TODO: the running apps grid doesn't support standard scope results model yet
102 item.firstModel = Qt.binding(function() { return results.firstModel })
103 item.secondModel = Qt.binding(function() { return results.secondModel })
104 item.canEnableTerminationMode = Qt.binding(function() { return scopeView.isCurrent })
105 } else {
106 item.model = Qt.binding(function() { return results })
107 }
108 item.objectName = Qt.binding(function() { return categoryId })
109 if (item.expandable) {
110 var shouldFilter = categoryId != categoryView.expandedCategoryId;
111 if (shouldFilter != item.filter) {
112 item.filter = shouldFilter;
113 }
114 }
115 updateDelegateCreationRange();
116 }
118 Component.onDestruction: {
119 if (item.enableHeightBehavior !== undefined && item.enableHeightBehaviorOnNextCreation !== undefined) {
120 scopeView.enableHeightBehaviorOnNextCreation = item.enableHeightBehaviorOnNextCreation;
121 }
122 }
124 Connections {
125 target: rendererLoader.item
126 onClicked: {
127 effect.positionPx = mapToItem(categoryView, 0, itemY).y
128 scopeView.scope.activate(delegateItem.model.uri,
129 delegateItem.model.icon,
130 delegateItem.model.category,
131 0,
132 delegateItem.model.mimetype,
133 delegateItem.model.title,
134 delegateItem.model.comment,
135 delegateItem.model.dndUri,
136 delegateItem.model.metadata)
137 }
138 onPressAndHold: {
139 effect.positionPx = mapToItem(categoryView, 0, itemY).y
140 scopeView.scope.preview( delegateItem.model.uri,
141 delegateItem.model.icon,
142 delegateItem.model.category,
143 0,
144 delegateItem.model.mimetype,
145 delegateItem.model.title,
146 delegateItem.model.comment,
147 delegateItem.model.dndUri,
148 delegateItem.model.metadata)
149 }
150 }
151 Connections {
152 target: categoryView
153 onExpandedCategoryIdChanged: {
154 var item = rendererLoader.item;
155 if (item.expandable) {
156 var shouldFilter = categoryId != categoryView.expandedCategoryId;
157 if (shouldFilter != item.filter) {
158 // If the filter animation will be seen start it, otherwise, just flip the switch
159 var shrinkingVisible = shouldFilter && y + item.collapsedHeight < categoryView.height;
160 var growingVisible = !shouldFilter && y + height < categoryView.height;
161 if (shrinkingVisible || growingVisible) {
162 item.startFilterAnimation(shouldFilter)
163 } else {
164 item.filter = shouldFilter;
165 }
166 if (!shouldFilter) {
167 categoryView.maximizeVisibleArea(index, item.uncollapsedHeight);
168 }
169 }
170 }
171 }
172 onContentYChanged: rendererLoader.updateDelegateCreationRange();
173 onHeightChanged: rendererLoader.updateDelegateCreationRange();
174 }
176 function updateDelegateCreationRange() {
177 // Do not update the range if we are overshooting up or down, since we'll come back
178 // to the stable position and delete/create items without any reason
179 if (categoryView.contentY < categoryView.originY) {
180 return;
181 } else if (categoryView.contentY + categoryView.height > categoryView.contentHeight) {
182 return;
183 }
185 if (item.hasOwnProperty("delegateCreationBegin")) {
186 if (baseItem.y + baseItem.height <= 0) {
187 // Not visible (item at top of the list)
188 item.delegateCreationBegin = baseItem.height
189 item.delegateCreationEnd = baseItem.height
190 } else if (baseItem.y >= categoryView.height) {
191 // Not visible (item at bottom of the list)
192 item.delegateCreationBegin = 0
193 item.delegateCreationEnd = 0
194 } else {
195 item.delegateCreationBegin = Math.max(-baseItem.y, 0)
196 item.delegateCreationEnd = Math.min(categoryView.height + item.delegateCreationBegin, baseItem.height)
197 }
198 }
199 }
200 }
201 }
203 sectionProperty: "name"
204 sectionDelegate: ListItems.Header {
205 property var delegate: categoryView.item(delegateIndex)
206 width: categoryView.width
207 text: section
208 image: {
209 if (delegate && delegate.expandable)
210 return delegate.filtered ? "graphics/header_handlearrow.png" : "graphics/header_handlearrow2.png"
211 return "";
212 }
213 onClicked: {
214 if (categoryView.expandedCategoryId != delegate.category)
215 categoryView.expandedCategoryId = delegate.category;
216 else
217 categoryView.expandedCategoryId = "";
218 }
219 }
220 pageHeader: PageHeader {
221 id: pageHeader
222 objectName: "pageHeader"
223 width: categoryView.width
224 text: scopeView.scope.name
225 searchEntryEnabled: true
226 scope: scopeView.scope
227 }
228 }
230 function getDefaultRendererId(contentType) {
231 switch (contentType) {
232 default: return "grid";
233 }
234 }
236 function getRenderer(rendererId, contentType, rendererHint) {
237 if (rendererId == "default") {
238 rendererId = getDefaultRendererId(contentType);
239 }
240 switch (rendererId) {
241 case "grid": {
242 switch (contentType) {
243 case "video": return "Video/VideoFilterGrid.qml";
244 case "music": return "Music/MusicFilterGrid.qml";
245 case "apps": {
246 if (rendererHint == "toggled")
247 return "Apps/DashPluginFilterGrid.qml";
248 else
249 return "Generic/GenericFilterGrid.qml";
250 }
251 case "weather": return "Generic/WeatherFilterGrid.qml";
252 default: return "Generic/GenericFilterGrid.qml";
253 }
254 }
255 case "carousel": {
256 switch (contentType) {
257 case "music": return "Music/MusicCarousel.qml";
258 default: return "Generic/GenericCarousel.qml";
259 }
260 }
261 case "special": {
262 switch (contentType) {
263 case "apps": return "Apps/RunningApplicationsGrid.qml";
264 default: return "Generic/GenericFilterGrid.qml";
265 }
266 }
267 default: return "Generic/GenericFilterGrid.qml";
268 }
269 }
271 OpenEffect {
272 id: effect
273 anchors {
274 fill: parent
275 bottomMargin: -bottomOverflow
276 }
277 sourceItem: categoryView
279 enabled: gap > 0.0
281 topGapPx: (1 - gap) * positionPx
282 topOpacity: (1 - gap * 1.2)
283 bottomGapPx: positionPx + gap * (targetBottomGapPx - positionPx)
284 bottomOverflow: units.gu(20)
285 bottomOpacity: 1 - (gap * 0.8)
287 property int targetBottomGapPx: height - units.gu(8) - bottomOverflow
288 property real gap: previewLoader.open ? 1.0 : 0.0
290 Behavior on gap {
291 NumberAnimation {
292 duration: 200
293 easing.type: Easing.InOutQuad
294 onRunningChanged: {
295 if (!previewLoader.open && !running) {
296 previewLoader.onScreen = false
297 }
298 }
299 }
300 }
301 }
303 Connections {
304 target: scopeView.scope
305 onPreviewReady: {
306 previewLoader.previewData = preview
307 previewLoader.open = true
308 }
309 }
311 PreviewDelegateMapper {
312 id: previewDelegateMapper
313 }
315 Connections {
316 ignoreUnknownSignals: true
317 target: previewLoader.valid ? previewLoader.item : null
318 onClose: {
319 previewLoader.open = false
320 }
321 }
323 Loader {
324 objectName: "previewLoader"
325 id: previewLoader
326 property var previewData
327 height: effect.bottomGapPx - effect.topGapPx
328 anchors {
329 top: parent.top
330 topMargin: effect.topGapPx
331 left: parent.left
332 right: parent.right
333 }
334 source: onScreen ? previewDelegateMapper.map(previewLoader.previewData.rendererName) : ""
336 property bool open: false
337 property bool onScreen: false
338 property bool valid: item !== null
340 onOpenChanged: {
341 if (open) {
342 onScreen = true
343 }
344 }
346 onLoaded: {
347 item.previewData = Qt.binding(function() { return previewLoader.previewData })
348 }
350 function closePreviewSpinner() {
351 if(item) {
352 item.showProcessingAction = false;
353 }
354 }
355 }
357 // TODO: Move as InverseMouseArea to DashPreview
358 MouseArea {
359 enabled: previewLoader.onScreen
360 anchors {
361 fill: parent
362 topMargin: effect.bottomGapPx
363 }
364 onClicked: {
365 previewLoader.open = false;
366 }
367 }