on 8f21147 help sign in
unity8/plugins/Dash/CardCreator.js
525 lines 27 kB history raw
1/*
2 * Copyright (C) 2014 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 */
17.pragma library
19var kBackgroundLoaderCode = 'Loader {\n\
20 id: backgroundLoader; \n\
21 objectName: "backgroundLoader"; \n\
22 anchors.fill: parent; \n\
23 asynchronous: root.asynchronous; \n\
24 visible: status == Loader.Ready; \n\
25 sourceComponent: UbuntuShape { \n\
26 objectName: "background"; \n\
27 radius: "medium"; \n\
28 color: getColor(0) || "white"; \n\
29 gradientColor: getColor(1) || color; \n\
30 anchors.fill: parent; \n\
31 image: backgroundImage.source ? backgroundImage : null; \n\
32 property real luminance: 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; \n\
33 property Image backgroundImage: Image { \n\
34 objectName: "backgroundImage"; \n\
35 source: { \n\
36 if (cardData && typeof cardData["background"] === "string") return cardData["background"]; \n\
37 else if (template && typeof template["card-background"] === "string") return template["card-background"]; \n\
38 else return ""; \n\
39 } \n\
40 } \n\
41 function getColor(index) { \n\
42 if (cardData && typeof cardData["background"] === "object" \n\
43 && (cardData["background"]["type"] === "color" || cardData["background"]["type"] === "gradient")) { \n\
44 return cardData["background"]["elements"][index]; \n\
45 } else if (template && typeof template["card-background"] === "object" \n\
46 && (template["card-background"]["type"] === "color" || template["card-background"]["type"] === "gradient")) { \n\
47 return template["card-background"]["elements"][index]; \n\
48 } else return undefined; \n\
49 } \n\
50 } \n\
51 }\n';
53// %1 is used as anchors of artShapeHolder
54// %2 is used as image width
55// %3 is used as image height
56var kArtShapeHolderCode = 'Item { \n\
57 id: artShapeHolder; \n\
58 height: root.fixedArtShapeSize.height > 0 ? root.fixedArtShapeSize.height : artShapeLoader.height; \n\
59 width: root.fixedArtShapeSize.width > 0 ? root.fixedArtShapeSize.width : artShapeLoader.width; \n\
60 anchors { %1 } \n\
61 Loader { \n\
62 id: artShapeLoader; \n\
63 objectName: "artShapeLoader"; \n\
64 active: cardData && cardData["art"] || false; \n\
65 asynchronous: root.asynchronous; \n\
66 visible: status == Loader.Ready; \n\
67 sourceComponent: UbuntuShape { \n\
68 id: artShape; \n\
69 objectName: "artShape"; \n\
70 radius: "medium"; \n\
71 visible: image.status == Image.Ready; \n\
72 readonly property real fixedArtShapeSizeAspect: (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) ? root.fixedArtShapeSize.width / root.fixedArtShapeSize.height : -1; \n\
73 readonly property real aspect: fixedArtShapeSizeAspect > 0 ? fixedArtShapeSizeAspect : components !== undefined ? components["art"]["aspect-ratio"] : 1; \n\
74 readonly property bool aspectSmallerThanImageAspect: aspect < image.aspect; \n\
75 Component.onCompleted: { updateWidthHeightBindings(); if (artShapeBorderSource !== undefined) borderSource = artShapeBorderSource; } \n\
76 onAspectSmallerThanImageAspectChanged: updateWidthHeightBindings(); \n\
77 Connections { target: root; onFixedArtShapeSizeChanged: updateWidthHeightBindings(); } \n\
78 function updateWidthHeightBindings() { \n\
79 if (root.fixedArtShapeSize.height > 0 && root.fixedArtShapeSize.width > 0) { \n\
80 width = root.fixedArtShapeSize.width; \n\
81 height = root.fixedArtShapeSize.height; \n\
82 } else if (aspectSmallerThanImageAspect) { \n\
83 width = Qt.binding(function() { return !visible ? 0 : image.width }); \n\
84 height = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.height : width / image.aspect }); \n\
85 } else { \n\
86 width = Qt.binding(function() { return !visible ? 0 : image.fillMode === Image.PreserveAspectCrop ? image.width : height * image.aspect }); \n\
87 height = Qt.binding(function() { return !visible ? 0 : image.height }); \n\
88 } \n\
89 } \n\
90 image: Image { \n\
91 objectName: "artImage"; \n\
92 source: cardData && cardData["art"] || ""; \n\
93 cache: true; \n\
94 asynchronous: root.asynchronous; \n\
95 fillMode: components && components["art"]["fill-mode"] === "fit" ? Image.PreserveAspectFit: Image.PreserveAspectCrop; \n\
96 readonly property real aspect: implicitWidth / implicitHeight; \n\
97 width: %2; \n\
98 height: %3; \n\
99 } \n\
100 } \n\
101 } \n\
102 }\n';
104var kOverlayLoaderCode = 'Loader { \n\
105 id: overlayLoader; \n\
106 anchors { \n\
107 left: artShapeHolder.left; \n\
108 right: artShapeHolder.right; \n\
109 bottom: artShapeHolder.bottom; \n\
110 } \n\
111 active: artShapeLoader.active && artShapeLoader.item && artShapeLoader.item.image.status === Image.Ready || false; \n\
112 asynchronous: root.asynchronous; \n\
113 visible: showHeader && status == Loader.Ready; \n\
114 sourceComponent: ShaderEffect { \n\
115 id: overlay; \n\
116 height: fixedHeaderHeight > 0 ? fixedHeaderHeight : headerHeight; \n\
117 opacity: 0.6; \n\
118 property var source: ShaderEffectSource { \n\
119 id: shaderSource; \n\
120 sourceItem: artShapeLoader.item; \n\
121 onVisibleChanged: if (visible) scheduleUpdate(); \n\
122 live: false; \n\
123 sourceRect: Qt.rect(0, artShapeLoader.height - overlay.height, artShapeLoader.width, overlay.height); \n\
124 } \n\
125 vertexShader: " \n\
126 uniform highp mat4 qt_Matrix; \n\
127 attribute highp vec4 qt_Vertex; \n\
128 attribute highp vec2 qt_MultiTexCoord0; \n\
129 varying highp vec2 coord; \n\
130 void main() { \n\
131 coord = qt_MultiTexCoord0; \n\
132 gl_Position = qt_Matrix * qt_Vertex; \n\
133 }"; \n\
134 fragmentShader: " \n\
135 varying highp vec2 coord; \n\
136 uniform sampler2D source; \n\
137 uniform lowp float qt_Opacity; \n\
138 void main() { \n\
139 lowp vec4 tex = texture2D(source, coord); \n\
140 gl_FragColor = vec4(0, 0, 0, tex.a) * qt_Opacity; \n\
141 }"; \n\
142 } \n\
143 }\n';
145// %1 is used as anchors of row
146// %2 is used as first child of the row
147// %3 is used as second child of the row
148var kHeaderRow2Code = 'Row { \n\
149 id: row; \n\
150 objectName: "outerRow"; \n\
151 property real margins: units.gu(1); \n\
152 spacing: margins; \n\
153 anchors { %1 } \n\
154 anchors.right: parent.right; \n\
155 anchors.margins: margins;\n\
156 data: [ %2\n\
157 ,\n\
158 %3 \n\
159 ] \n\
160 }\n';
162// %1 is used as anchors of row
163// %2 is used as first child of the row
164// %3 is used as second child of the row
165// %4 is used as third child of the row
166var kHeaderRow3Code = 'Row { \n\
167 id: row; \n\
168 objectName: "outerRow"; \n\
169 property real margins: units.gu(1); \n\
170 spacing: margins; \n\
171 anchors { %1 } \n\
172 anchors.right: parent.right; \n\
173 anchors.margins: margins;\n\
174 data: [ %2\n\
175 ,\n\
176 %3 \n\
177 ,\n\
178 %4 \n\
179 ] \n\
180 }\n';
182// %1 is used as first child of the column
183// %2 is used as second child of the column
184var kHeaderColumnCode = 'Column { \n\
185 anchors.verticalCenter: parent.verticalCenter; \n\
186 spacing: units.dp(2); \n\
187 width: parent.width - x;\n\
188 data: [ %1\n\
189 ,\n\
190 %2 \n\
191 ] \n\
192 }\n';
194// %1 is used as anchors of mascotShapeLoader
195var kMascotShapeLoaderCode = 'Loader { \n\
196 id: mascotShapeLoader; \n\
197 objectName: "mascotShapeLoader"; \n\
198 asynchronous: root.asynchronous; \n\
199 active: mascotImage.status === Image.Ready; \n\
200 visible: showHeader && active && status == Loader.Ready; \n\
201 width: units.gu(6); \n\
202 height: units.gu(5.625); \n\
203 sourceComponent: UbuntuShape { image: mascotImage } \n\
204 anchors { %1 } \n\
205 }\n';
207// %1 is used as anchors of mascotImage
208// %2 is used as visible of mascotImage
209var kMascotImageCode = 'Image { \n\
210 id: mascotImage; \n\
211 objectName: "mascotImage"; \n\
212 anchors { %1 } \n\
213 readonly property int maxSize: Math.max(width, height) * 4; \n\
214 source: cardData && cardData["mascot"]; \n\
215 width: units.gu(6); \n\
216 height: units.gu(5.625); \n\
217 sourceSize { width: maxSize; height: maxSize } \n\
218 fillMode: Image.PreserveAspectCrop; \n\
219 horizontalAlignment: Image.AlignHCenter; \n\
220 verticalAlignment: Image.AlignVCenter; \n\
221 visible: %2; \n\
222 }\n';
224// %1 is used as anchors of titleLabel
225// %1 is used as color of titleLabel
226// %3 is used as extra condition for visible of titleLabel
227var kTitleLabelCode = 'Label { \n\
228 id: titleLabel; \n\
229 objectName: "titleLabel"; \n\
230 anchors { %1 } \n\
231 elide: Text.ElideRight; \n\
232 fontSize: "small"; \n\
233 wrapMode: Text.Wrap; \n\
234 maximumLineCount: 2; \n\
235 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
236 color: %2; \n\
237 visible: showHeader %3; \n\
238 text: root.title; \n\
239 font.weight: components && components["subtitle"] ? Font.DemiBold : Font.Normal; \n\
240 horizontalAlignment: root.headerAlignment; \n\
241 }\n';
243// %1 is used as anchors of subtitleLabel
244// %2 is used as color of subtitleLabel
245var kSubtitleLabelCode = 'Label { \n\
246 id: subtitleLabel; \n\
247 objectName: "subtitleLabel"; \n\
248 anchors { %1 } \n\
249 elide: Text.ElideRight; \n\
250 fontSize: "small"; \n\
251 font.pixelSize: Math.round(FontUtils.sizeToPixels(fontSize) * fontScale); \n\
252 color: %2; \n\
253 visible: titleLabel.visible && titleLabel.text; \n\
254 text: cardData && cardData["subtitle"] || ""; \n\
255 font.weight: Font.Light; \n\
256 horizontalAlignment: root.headerAlignment; \n\
257 }\n';
259// %1 is used as top anchor of summary
260// %2 is used as topMargin anchor of summary
261// %3 is used as color of summary
262var kSummaryLabelCode = 'Label { \n\
263 id: summary; \n\
264 objectName: "summaryLabel"; \n\
265 anchors { \n\
266 top: %1; \n\
267 left: parent.left; \n\
268 right: parent.right; \n\
269 margins: units.gu(1); \n\
270 topMargin: %2; \n\
271 } \n\
272 wrapMode: Text.Wrap; \n\
273 maximumLineCount: 5; \n\
274 elide: Text.ElideRight; \n\
275 text: cardData && cardData["summary"] || ""; \n\
276 height: text ? implicitHeight : 0; \n\
277 fontSize: "small"; \n\
278 color: %3; \n\
279 }\n';
281function cardString(template, components) {
282 var code;
283 code = 'AbstractButton { \n\
284 id: root; \n\
285 property var template; \n\
286 property var components; \n\
287 property var cardData; \n\
288 property var artShapeBorderSource: undefined; \n\
289 property real fontScale: 1.0; \n\
290 property int headerAlignment: Text.AlignLeft; \n\
291 property int fixedHeaderHeight: -1; \n\
292 property size fixedArtShapeSize: Qt.size(-1, -1); \n\
293 readonly property string title: cardData && cardData["title"] || ""; \n\
294 property bool asynchronous: true; \n\
295 property bool showHeader: true; \n\
296 implicitWidth: childrenRect.width; \n';
298 var hasArt = components["art"] && components["art"]["field"] || false;
299 var hasSummary = components["summary"] || false;
300 var artAndSummary = hasArt && hasSummary;
301 var isHorizontal = template["card-layout"] === "horizontal";
302 var hasBackground = !isHorizontal && (template["card-background"] || components["background"] || artAndSummary);
303 var hasTitle = components["title"] || false;
304 var hasMascot = components["mascot"] || false;
305 var headerAsOverlay = hasArt && template && template["overlay"] === true && (hasTitle || hasMascot);
306 var hasSubtitle = components["subtitle"] || false;
307 var hasHeaderRow = hasMascot && hasTitle;
309 if (hasBackground) {
310 code += kBackgroundLoaderCode;
311 }
313 if (hasArt) {
314 code += 'onArtShapeBorderSourceChanged: { if (artShapeBorderSource !== undefined && artShapeLoader.item) artShapeLoader.item.borderSource = artShapeBorderSource; } \n';
315 code += 'readonly property size artShapeSize: artShapeLoader.item ? Qt.size(artShapeLoader.item.width, artShapeLoader.item.height) : Qt.size(-1, -1);\n';
317 var widthCode, heightCode;
318 var anchors;
319 if (isHorizontal) {
320 anchors = 'left: parent.left';
321 if (hasMascot || hasTitle) {
322 widthCode = 'height * artShape.aspect'
323 heightCode = 'headerHeight';
324 } else {
325 // This side of the else is a bit silly, who wants an horizontal layout without mascot and title?
326 // So we define a "random" height of the image height + 2 gu for the margins
327 widthCode = 'height * artShape.aspect'
328 heightCode = 'units.gu(7.625)';
329 }
330 } else {
331 anchors = 'horizontalCenter: parent.horizontalCenter;';
332 widthCode = 'root.width'
333 heightCode = 'width / artShape.aspect';
334 }
336 code += kArtShapeHolderCode.arg(anchors).arg(widthCode).arg(heightCode);
337 } else {
338 code += 'readonly property size artShapeSize: Qt.size(-1, -1);\n'
339 }
341 if (headerAsOverlay) {
342 code += kOverlayLoaderCode;
343 }
345 var headerVerticalAnchors;
346 if (headerAsOverlay) {
347 headerVerticalAnchors = 'bottom: artShapeHolder.bottom; \n\
348 bottomMargin: units.gu(1);\n';
349 } else {
350 if (hasArt) {
351 if (isHorizontal) {
352 headerVerticalAnchors = 'top: artShapeHolder.top; \n\
353 topMargin: units.gu(1);\n';
354 } else {
355 headerVerticalAnchors = 'top: artShapeHolder.bottom; \n\
356 topMargin: units.gu(1);\n';
357 }
358 } else {
359 headerVerticalAnchors = 'top: parent.top; \n\
360 topMargin: units.gu(1);\n';
361 }
362 }
363 var headerLeftAnchor;
364 var headerLeftAnchorHasMagin = false;
365 if (isHorizontal && hasArt) {
366 headerLeftAnchor = 'left: artShapeHolder.right; \n\
367 leftMargin: units.gu(1);\n';
368 headerLeftAnchorHasMagin = true;
369 } else {
370 headerLeftAnchor = 'left: parent.left;\n';
371 }
373 if (hasHeaderRow) {
374 code += 'readonly property int headerHeight: row.height + row.margins * 2;\n'
375 } else if (hasMascot) {
376 code += 'readonly property int headerHeight: mascotImage.height + units.gu(1) * 2;\n'
377 } else if (hasSubtitle) {
378 code += 'readonly property int headerHeight: titleLabel.height + titleLabel.anchors.topMargin * 2 + subtitleLabel.height + subtitleLabel.anchors.topMargin;\n'
379 } else if (hasTitle) {
380 code += 'readonly property int headerHeight: titleLabel.height + titleLabel.anchors.topMargin * 2;\n'
381 } else {
382 code += 'readonly property int headerHeight: 0;\n'
383 }
385 var mascotShapeCode = "";
386 var mascotCode = "";
387 if (hasMascot) {
388 var useMascotShape = !hasBackground && !headerAsOverlay;
389 var anchors = "";
390 if (!hasHeaderRow) {
391 anchors += headerLeftAnchor;
392 anchors += headerVerticalAnchors;
393 if (!headerLeftAnchorHasMagin) {
394 anchors += 'leftMargin: units.gu(1);\n'
395 }
396 } else {
397 anchors = "verticalCenter: parent.verticalCenter;"
398 }
400 if (useMascotShape) {
401 mascotShapeCode = kMascotShapeLoaderCode.arg(anchors);
402 }
404 var mascotImageVisible = useMascotShape ? 'false' : 'showHeader';
405 mascotCode = kMascotImageCode.arg(anchors).arg(mascotImageVisible);
406 }
408 var summaryColorWithBackground = 'backgroundLoader.active && backgroundLoader.item && backgroundLoader.item.luminance < 0.7 ? "white" : "grey"';
410 var titleSubtitleCode = "";
411 if (hasTitle) {
412 var color;
413 if (headerAsOverlay) {
414 color = '"white"';
415 } else if (hasSummary) {
416 color = 'summary.color';
417 } else if (hasBackground) {
418 color = summaryColorWithBackground;
419 } else {
420 color = '"grey"';
421 }
423 var titleAnchors;
424 var subtitleAnchors;
425 if (hasMascot && hasSubtitle) {
426 // Using row + column
427 titleAnchors = 'left: parent.left; right: parent.right';
428 subtitleAnchors = titleAnchors;
429 } else if (hasMascot) {
430 // Using row + label
431 titleAnchors = 'verticalCenter: parent.verticalCenter;\n'
432 } else {
433 if (headerAsOverlay) {
434 // Using anchors to the overlay
435 titleAnchors = 'left: parent.left; \n\
436 leftMargin: units.gu(1); \n\
437 right: parent.right; \n\
438 top: overlayLoader.top; \n\
439 topMargin: units.gu(1);\n';
440 } else {
441 // Using anchors to the mascot/parent
442 titleAnchors = "right: parent.right;";
443 titleAnchors += headerLeftAnchor;
444 titleAnchors += headerVerticalAnchors;
445 }
446 subtitleAnchors = 'left: titleLabel.left; \n\
447 leftMargin: titleLabel.leftMargin; \n\
448 right: titleLabel.right; \n\
449 top: titleLabel.bottom; \n\
450 topMargin: units.dp(2);\n';
451 }
453 var titleLabelVisibleExtra = (headerAsOverlay ? '&& overlayLoader.active': '');
454 var titleCode = kTitleLabelCode.arg(titleAnchors).arg(color).arg(titleLabelVisibleExtra);
455 var subtitleCode = "";
456 if (hasSubtitle) {
457 subtitleCode += kSubtitleLabelCode.arg(subtitleAnchors).arg(color);
458 }
460 if (hasMascot && hasSubtitle) {
461 // If using row + column wrap the code in the column
462 titleSubtitleCode = kHeaderColumnCode.arg(titleCode).arg(subtitleCode);
463 } else {
464 titleSubtitleCode = titleCode + subtitleCode;
465 }
466 }
468 if (hasHeaderRow) {
469 if (mascotShapeCode != "") {
470 code += kHeaderRow3Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotShapeCode).arg(mascotCode).arg(titleSubtitleCode);
471 } else {
472 code += kHeaderRow2Code.arg(headerVerticalAnchors + headerLeftAnchor).arg(mascotCode).arg(titleSubtitleCode);
473 }
474 } else {
475 code += mascotShapeCode + mascotCode + titleSubtitleCode;
476 }
478 if (hasSummary) {
479 var summaryTopAnchor;
480 if (isHorizontal && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
481 else if (headerAsOverlay && hasArt) summaryTopAnchor = "artShapeHolder.bottom";
482 else if (hasHeaderRow) summaryTopAnchor = "row.bottom";
483 else if (hasMascot) summaryTopAnchor = "mascotImage.bottom";
484 else if (hasSubtitle) summaryTopAnchor = "subtitleLabel.bottom";
485 else if (hasTitle) summaryTopAnchor = "titleLabel.bottom";
486 else if (hasArt) summaryTopAnchor = "artShapeHolder.bottom";
487 else summaryTopAnchor = "parent.top";
489 var color;
490 if (hasBackground) {
491 color = summaryColorWithBackground;
492 } else {
493 color = '"grey"';
494 }
496 var summaryTopMargin = (hasMascot || hasSubtitle ? 'anchors.margins' : '0');
498 code += kSummaryLabelCode.arg(summaryTopAnchor).arg(summaryTopMargin).arg(color);
499 }
501 if (hasSummary) {
502 code += 'implicitHeight: summary.y + summary.height + (summary.text ? units.gu(1) : 0);\n';
503 } else if (hasHeaderRow) {
504 code += 'implicitHeight: row.y + row.height + units.gu(1);\n';
505 } else if (hasMascot) {
506 code += 'implicitHeight: mascotImage.y + mascotImage.height;\n';
507 } else if (hasSubtitle) {
508 code += 'implicitHeight: subtitleLabel.y + subtitleLabel.height + units.gu(1);\n';
509 } else if (hasTitle) {
510 code += 'implicitHeight: titleLabel.y + titleLabel.height + units.gu(1);\n';
511 }
512 // Close the AbstractButton
513 code += '}\n';
515 return code;
518function createCardComponent(parent, template, components) {
519 var imports = 'import QtQuick 2.2; \n\
520 import Ubuntu.Components 0.1; \n\
521 import Ubuntu.Thumbnailer 0.1;\n';
522 var card = cardString(template, components);
523 var code = imports + 'Component {\n' + card + '}\n';
524 return Qt.createQmlObject(code, parent, "createCardComponent");