on xenial help sign in
A bit of refactoring

- Make the code more declarative. Using more bindings and less function calls Easier to read and hopefully more robust - Wrap the super-long lines

commitc336164703f3054c8fdab824f476e03b6d9ee2c0 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateFri 15 Aug 2014, 12:57 (12 years ago) parentf6a0965
2 files changed+20−26
qml/Dash/GenericScopeView.qml+16−25 tests/qmltests/Dash/tst_GenericScopeView.qml+4−1
qml/Dash/GenericScopeView.qml+16−25
@@ -47,11 +47,6 @@ FocusScope {
4747
4848 signal backClicked()
4949
50− onScopeChanged: {
51− floatingSeeLess.companionTo = null;
52− floatingSeeLess.companionBase = null;
53− }
54−
5550 function positionAtBeginning() {
5651 categoryView.positionAtBeginning()
5752 }
@@ -149,7 +144,8 @@ FocusScope {
149144 x: previewListView.open ? -width : 0
150145 Behavior on x { UbuntuNumberAnimation { } }
151146 width: parent.width
152− height: floatingSeeLess.visible ? parent.height - floatingSeeLess.height + floatingSeeLess.yOffset : parent.height
147+ height: floatingSeeLess.visible ? parent.height - floatingSeeLess.height + floatingSeeLess.yOffset
148+ : parent.height
153149 clip: height != parent.height
154150
155151 model: scopeView.categories
@@ -157,7 +153,7 @@ FocusScope {
157153 pixelAligned: true
158154 interactive: !navigationShown
159155
160− property string expandedCategoryId: ""
156+ property Item expandedCategoryItem: null
161157
162158 readonly property bool pageHeaderTotallyVisible: scopeView.showPageHeader &&
163159 ((headerItemShownHeight == 0 && categoryView.contentY <= categoryView.originY) || (headerItemShownHeight == pageHeaderLoader.item.height))
@@ -168,6 +164,8 @@ FocusScope {
168164 highlightWhenPressed: false
169165 showDivider: false
170166
167+ property Item seeAllButton: seeAll
168+
171169 readonly property bool expandable: {
172170 if (categoryView.model.count === 1) return false;
173171 if (cardTool.template && cardTool.template["collapsed-rows"] === 0) return false;
@@ -197,7 +195,7 @@ FocusScope {
197195 // This can happen with the VJ that doesn't know how height it will be on creation
198196 // so doesn't set expandable until a bit too late for onLoaded
199197 if (expandable) {
200− var shouldExpand = baseItem.category === categoryView.expandedCategoryId;
198+ var shouldExpand = baseItem === categoryView.expandedCategoryItem;
201199 baseItem.expand(shouldExpand, false /*animate*/);
202200 }
203201 }
@@ -248,7 +246,7 @@ FocusScope {
248246 item.objectName = Qt.binding(function() { return categoryId })
249247 item.scopeStyle = scopeView.scopeStyle;
250248 if (baseItem.expandable) {
251− var shouldExpand = categoryId === categoryView.expandedCategoryId;
249+ var shouldExpand = baseItem === categoryView.expandedCategoryItem;
252250 baseItem.expand(shouldExpand, false /*animate*/);
253251 }
254252 updateDelegateCreationRange();
@@ -285,13 +283,13 @@ FocusScope {
285283 }
286284 Connections {
287285 target: categoryView
288− onExpandedCategoryIdChanged: {
286+ onExpandedCategoryItemChanged: {
289287 collapseAllButExpandedCategory();
290288 }
291289 function collapseAllButExpandedCategory() {
292290 var item = rendererLoader.item;
293291 if (baseItem.expandable) {
294− var shouldExpand = categoryId === categoryView.expandedCategoryId;
292+ var shouldExpand = baseItem === categoryView.expandedCategoryItem;
295293 if (shouldExpand != baseItem.expanded) {
296294 // If the filter animation will be seen start it, otherwise, just flip the switch
297295 var shrinkingVisible = !shouldExpand && y + item.collapsedHeight + seeAll.height < categoryView.height;
@@ -355,12 +353,10 @@ FocusScope {
355353 height: seeAllLabel.visible ? seeAllLabel.font.pixelSize + units.gu(4) : 0
356354
357355 onClicked: {
358− if (categoryView.expandedCategoryId != baseItem.category) {
359− categoryView.expandedCategoryId = baseItem.category;
360− floatingSeeLess.companionTo = seeAll;
361− floatingSeeLess.companionBase = baseItem;
356+ if (categoryView.expandedCategoryItem !== baseItem) {
357+ categoryView.expandedCategoryItem = baseItem;
362358 } else {
363− categoryView.expandedCategoryId = "";
359+ categoryView.expandedCategoryItem = null;
364360 }
365361 }
366362
@@ -455,8 +451,8 @@ FocusScope {
455451 id: floatingSeeLess
456452 objectName: "floatingSeeLess"
457453
458− property var companionTo: null
459− property var companionBase: null
454+ property Item companionTo: companionBase ? companionBase.seeAllButton : null
455+ property Item companionBase: categoryView.expandedCategoryItem
460456 property bool showBecausePosition: false
461457 property real yOffset: 0
462458
@@ -468,7 +464,7 @@ FocusScope {
468464 height: seeLessLabel.font.pixelSize + units.gu(4)
469465 visible: companionTo && showBecausePosition
470466
471− onClicked: categoryView.expandedCategoryId = "";
467+ onClicked: categoryView.expandedCategoryItem = null;
472468
473469 function updateVisibility() {
474470 var companionPos = companionTo.mapToItem(floatingSeeLess, 0, 0);
@@ -477,11 +473,6 @@ FocusScope {
477473 var posToBase = floatingSeeLess.mapToItem(companionBase, 0, -yOffset).y;
478474 yOffset = Math.max(0, companionBase.item.collapsedHeight - posToBase);
479475 yOffset = Math.min(yOffset, height);
480−
481− if (!showBecausePosition && categoryView.expandedCategoryId === "") {
482− companionTo = null;
483− companionBase = null;
484− }
485476 }
486477
487478 Label {
@@ -502,7 +493,7 @@ FocusScope {
502493 }
503494
504495 Connections {
505− target: floatingSeeLess.companionTo ? floatingSeeLess.companionTo : null
496+ target: floatingSeeLess.companionTo
506497 onYChanged: floatingSeeLess.updateVisibility();
507498 }
508499 }
tests/qmltests/Dash/tst_GenericScopeView.qml+4−1
@@ -217,7 +217,10 @@ Item {
217217 var floatingSeeLess = findChild(genericScopeView, "floatingSeeLess");
218218 mouseClick(seeAll, seeAll.width / 2, seeAll.height / 2);
219219 tryCompare(category, "expanded", true);
220− tryCompareFunction(function() { return category.item.height + floatingSeeLess.height == genericScopeView.height - category.item.displayMarginBeginning - category.item.displayMarginEnd; }, true);
220+ tryCompareFunction(function() {
221+ return category.item.height + floatingSeeLess.height ==
222+ genericScopeView.height - category.item.displayMarginBeginning - category.item.displayMarginEnd;
223+ }, true);
221224 mouseClick(floatingSeeLess, floatingSeeLess.width / 2, floatingSeeLess.height / 2);
222225 tryCompare(category, "expanded", false);
223226 }