on xenial help sign in
Adds carousel dynamic switch
691e9b2Andrea Cimitan
qml/Dash/CardTool.qml+46−8
@@ -28,6 +28,22 @@ import QtQuick 2.0
28
28
29Item {
29Item {
30 id: cardTool
30 id: cardTool
31+
32+ /*!
33+ \brief Number of cards.
34+ */
35+ property int count
36+
37+ /*!
38+ \brief Width of the category view.
39+ */
40+ property real viewWidth
41+
42+ /*!
43+ \brief Scaling factor of selected Carousel item.
44+ */
45+ readonly property real carouselSelectedItemScaleFactor: 1.38 // XXX assuming 1.38 carousel scaling factor for cards
46+
31 /*!
47 /*!
32 \brief Template supplied for the category.
48 \brief Template supplied for the category.
33 */
49 */
@@ -39,9 +55,15 @@ Item {
39 property var components
55 property var components
40
56
41 /*!
57 /*!
42− \brief Width of the view, based on which carousel width is calculated.
58+ \brief The category layout for this card tool.
43 */
59 */
44− property var viewWidth
60+ property string categoryLayout: {
61+ var layout = template["category-layout"];
62+
63+ // carousel fallback mode to grid
64+ if (layout === "carousel" && count <= Math.ceil(carouselTool.realPathItemCount)) layout = "grid";
65+ return layout;
66+ }
45
67
46 // FIXME: Saviq
68 // FIXME: Saviq
47 // Only way for the card below to actually be laid out completely.
69 // Only way for the card below to actually be laid out completely.
@@ -56,7 +78,7 @@ Item {
56 If undefined, should use implicit width of the actual card.
78 If undefined, should use implicit width of the actual card.
57 */
79 */
58 readonly property var cardWidth: {
80 readonly property var cardWidth: {
59− switch (template !== undefined && template["category-layout"]) {
81+ switch (categoryLayout) {
60 case "grid":
82 case "grid":
61 case "vertical-journal":
83 case "vertical-journal":
62 if (template["card-layout"] === "horizontal") return units.gu(38);
84 if (template["card-layout"] === "horizontal") return units.gu(38);
@@ -66,10 +88,7 @@ Item {
66 }
88 }
67 return units.gu(18.5);
89 return units.gu(18.5);
68 case "carousel":
90 case "carousel":
69− if (viewWidth === undefined) return undefined
91+ return carouselTool.minimumTileWidth;
70− if (viewWidth <= units.gu(40)) return units.gu(18)
71− if (viewWidth >= units.gu(128)) return units.gu(26)
72− return units.gu(18 + Math.round((viewWidth - units.gu(40))/ units.gu(11)))
73 case undefined:
92 case undefined:
74 case "organic-grid":
93 case "organic-grid":
75 case "journal":
94 case "journal":
@@ -84,7 +103,7 @@ Item {
84 If undefined, should use implicit height of the actual card.
103 If undefined, should use implicit height of the actual card.
85 */
104 */
86 readonly property var cardHeight: {
105 readonly property var cardHeight: {
87− switch (template !== undefined && template["category-layout"]) {
106+ switch (categoryLayout) {
88 case "journal":
107 case "journal":
89 if (template["card-size"] >= 12 && template["card-size"] <= 38) return units.gu(template["card-size"]);
108 if (template["card-size"] >= 12 && template["card-size"] <= 38) return units.gu(template["card-size"]);
90 return units.gu(18.5);
109 return units.gu(18.5);
@@ -123,6 +142,25 @@ Item {
123 return (template["card-layout"] === "horizontal") ? Text.AlignLeft : Text.AlignHCenter;
142 return (template["card-layout"] === "horizontal") ? Text.AlignLeft : Text.AlignHCenter;
124 }
143 }
125
144
145+ QtObject {
146+ id: carouselTool
147+
148+ property real minimumTileWidth: {
149+ if (cardTool.viewWidth === undefined) return undefined;
150+ if (cardTool.viewWidth <= units.gu(40)) return units.gu(18);
151+ if (cardTool.viewWidth >= units.gu(128)) return units.gu(26);
152+ return units.gu(18 + Math.round((cardTool.viewWidth - units.gu(40)) / units.gu(11)));
153+ }
154+
155+ readonly property real pathItemCount: 4.8457 /// (848 / 175) reference values
156+
157+ property real realPathItemCount: {
158+ var scaledMinimumTileWidth = minimumTileWidth / cardTool.carouselSelectedItemScaleFactor;
159+ var tileWidth = Math.max(cardTool.viewWidth / pathItemCount, scaledMinimumTileWidth);
160+ return Math.min(cardTool.viewWidth / tileWidth, pathItemCount);
161+ }
162+ }
163+
126 Card {
164 Card {
127 id: card
165 id: card
128 objectName: "card"
166 objectName: "card"
tests/qmltests/Dash/tst_CardTool.qml+20
@@ -93,6 +93,7 @@ Rectangle {
93 CardTool {
93 CardTool {
94 id: cardTool
94 id: cardTool
95
95
96+ count: 8
96 template: Helpers.update(JSON.parse(Helpers.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['template'];
97 template: Helpers.update(JSON.parse(Helpers.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['template'];
97 components: Helpers.update(JSON.parse(Helpers.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['components'];
98 components: Helpers.update(JSON.parse(Helpers.defaultLayout), Helpers.tryParse(layoutArea.text, layoutError))['components'];
98 viewWidth: units.gu(Math.round(widthSlider.value))
99 viewWidth: units.gu(Math.round(widthSlider.value))
@@ -343,5 +344,24 @@ Rectangle {
343 tryCompare(cardTool, data.property, data.value);
344 tryCompare(cardTool, data.property, data.value);
344 }
345 }
345 }
346 }
347+
348+ function test_categoryLayout_data() {
349+ return [
350+ { tag: "Default Grid", layout_index: 0, count: 2, viewWidth: units.gu(40), layout: "grid" },
351+ { tag: "Long carousel", layout_index: 4, count: 6, viewWidth: units.gu(140), layout: "carousel" },
352+ { tag: "Long carousel fallback", layout_index: 4, count: 5, viewWidth: units.gu(140), layout: "grid" },
353+ { tag: "Short carousel", layout_index: 4, count: 4, viewWidth: units.gu(30), layout: "carousel" },
354+ { tag: "Short carousel fallback", layout_index: 4, count: 3, viewWidth: units.gu(30), layout: "grid" },
355+ { tag: "Journal", layout_index: 2, count: 8, viewWidth: units.gu(30), layout: "journal" }
356+ ]
357+ }
358+
359+ function test_categoryLayout(data) {
360+ selector.selectedIndex = 0;
361+ layoutSelector.selectedIndex = data.layout_index;
362+ cardTool.viewWidth = data.viewWidth;
363+ cardTool.count = data.count;
364+ compare(cardTool.categoryLayout, data.layout);
365+ }
346 }
366 }
347}
367}