on xenial help sign in
Introduce the card creator cache
commitff90c011d00def85c30a55e0d30ccec0f3de9d00 authorAlbert Astals <albert.astals@canonical.com> dateTue 29 Apr 2014, 16:12 (12 years ago) parent6471f4f
4 files changed+31−34
qml/Dash/CardCreator.js+2−2 qml/Dash/CardCreatorCache.qml+24 qml/Dash/CardTool.qml+2−32 src/main.cpp+3
qml/Dash/CardCreator.js+2−2
@@ -19,7 +19,7 @@
1919
2020.pragma library
2121
22−function createCardComponent(parent, template, components, asynchronous) {
22+function createCardComponent(parent, template, components) {
2323 var imports = 'import QtQuick 2.2; \
2424 import Ubuntu.Components 0.1; \
2525 import Ubuntu.Thumbnailer 0.1;';
@@ -34,7 +34,7 @@ function createCardComponent(parent, template, components, asynchronous) {
3434 property int headerAlignment: Text.AlignLeft; \
3535 property size fixedArtShapeSize: Qt.size(-1, -1); \
3636 readonly property string title: cardData && cardData["title"] || ""; \
37− property bool asynchronous: ' + asynchronous + '; \
37+ property bool asynchronous: true; \
3838 property bool showHeader: true; \
3939 implicitWidth: childrenRect.width; ';
4040
qml/Dash/CardCreatorCache.qmlnew file+24
@@ -0,0 +1,24 @@
1+pragma Singleton
2+import QtQuick 2.2
3+import "CardCreator.js" as CardCreator
4+
5+QtObject {
6+ id: root
7+
8+ property var cache: new Object();
9+
10+ function getCardComponent(template, components) {
11+ if (template === undefined || components === undefined)
12+ return undefined;
13+
14+ var tString = JSON.stringify(template);
15+ var cString = JSON.stringify(components);
16+ var allString = tString + cString;
17+ var component = cache[allString];
18+ if (component === undefined) {
19+ component = CardCreator.createCardComponent(root, template, components);
20+ cache[allString] = component;
21+ }
22+ return component;
23+ }
24+}
qml/Dash/CardTool.qml+2−32
@@ -15,7 +15,7 @@
1515 */
1616
1717import QtQuick 2.0
18−import "CardCreator.js" as CardCreator
18+import Unity.Dash 1.0
1919
2020/*!
2121 \brief Tool for introspecting Card properties.
@@ -66,37 +66,7 @@ Item {
6666 return layout;
6767 }
6868
69− property var cardComponent: undefined
70−
71− onTemplateChanged: {
72−// console.log("onTemplateChanged", cardTool, JSON.stringify(template));
73− updateCardComponent();
74− }
75− onComponentsChanged: {
76−// console.log("onComponentsChanged", cardTool, JSON.stringify(components));
77− updateCardComponent();
78− }
79−
80− // For some reason we get spurious template and components changed signals, since we
81− // do not want to call createCardComponent without need we cache the stringified vars
82− // and do a second changed check
83− property string templateString: ""
84− property string componentsString: ""
85−
86− function updateCardComponent()
87− {
88− if (cardTool.template === undefined || cardTool.components === undefined)
89− return;
90−
91− var tString = JSON.stringify(template);
92− var cString = JSON.stringify(components);
93− if (tString != templateString || cString != componentsString) {
94− templateString = tString;
95− componentsString = cString;
96− console.log("CardTool CALLING createCardComponent", cardLoader, tString, cString);
97− cardComponent = CardCreator.createCardComponent(cardLoader, cardTool.template, cardTool.components, true /* async */);
98− }
99− }
69+ property var cardComponent: CardCreatorCache.getCardComponent(cardTool.template, cardTool.components);
10070
10171 // FIXME: Saviq
10272 // Only way for the card below to actually be laid out completely.
src/main.cpp+3
@@ -22,6 +22,7 @@
2222#include <QtGui/QGuiApplication>
2323#include <QtQml/QQmlEngine>
2424#include <QtQml/QQmlContext>
25+#include <QtQml/qqml.h>
2526#include <qpa/qplatformnativeinterface.h>
2627#include <QLibrary>
2728#include <QDebug>
@@ -106,6 +107,8 @@ int startShell(int argc, const char** argv, void* server)
106107 application->installNativeEventFilter(mouseTouchAdaptor);
107108 }
108109
110+ qmlRegisterSingletonType(QUrl("file://" + qmlDirectory() + "Dash/CardCreatorCache.qml"), "Unity.Dash", 1, 0, "CardCreatorCache");
111+
109112 QPlatformNativeInterface* nativeInterface = QGuiApplication::platformNativeInterface();
110113 /* Shell is declared as a system session so that it always receives all
111114 input events.