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