on xenial help sign in
Refactoring code for the Loader
53f787cAndrea Cimitan
qml/Dash/GenericScopeView.qml+23−44
@@ -87,8 +87,7 @@ FocusScope {
87 }
87 }
88 subPageLoader.initialIndex = -1;
88 subPageLoader.initialIndex = -1;
89 subPageLoader.initialIndex = index;
89 subPageLoader.initialIndex = index;
90− subPageLoader.subPage = "preview";
90+ subPageLoader.openSubPage("preview");
91− subPageLoader.openSubPage();
92 }
91 }
93
92
94 Binding {
93 Binding {
@@ -107,8 +106,8 @@ FocusScope {
107 }
106 }
108
107
109 onIsCurrentChanged: {
108 onIsCurrentChanged: {
110− if (showPageHeader) {
109+ if (pageHeaderLoader.item && showPageHeader) {
111− pageHeaderLoader.item.resetSearch();
110+ pageHeaderLoader.item.resetSearch();
112 }
111 }
113 subPageLoader.closeSubPage();
112 subPageLoader.closeSubPage();
114 }
113 }
@@ -442,10 +441,7 @@ FocusScope {
442
441
443 onBackClicked: scopeView.backClicked()
442 onBackClicked: scopeView.backClicked()
444
443
445− onSettingsClicked: {
446− subPageLoader.subPage = "settings";
447− subPageLoader.openSubPage();
444+ onSettingsClicked: subPageLoader.openSubPage("settings")
448− }
449 }
445 }
450 }
446 }
451 }
447 }
@@ -478,52 +474,35 @@ FocusScope {
478 readonly property bool previewShown: open && subPage == "preview"
474 readonly property bool previewShown: open && subPage == "preview"
479 readonly property bool settingsShown: open && subPage == "settings"
475 readonly property bool settingsShown: open && subPage == "settings"
480
476
481− function updateBindings() {
477+ function openSubPage(page) {
482− if (status === Loader.Ready) {
483− item.scope = Qt.binding(function() { return subPageLoader.scope } )
484− item.scopeStyle = Qt.binding(function() { return subPageLoader.scopeStyle } )
485− if (item.hasOwnProperty("open")) item.open = Qt.binding(function() { return subPageLoader.open } )
486− if (item.hasOwnProperty("initialIndex")) item.initialIndex = Qt.binding(function() { return subPageLoader.initialIndex } )
487− if (item.hasOwnProperty("model")) item.model = Qt.binding(function() { return subPageLoader.model } )
488− }
489− }
490−
491− function updateSource() {
492− switch (subPage) {
478+ subPage = page;
493− case "preview": source = "PreviewListView.qml"; break;
494− case "settings": source = "ScopeSettingsPage.qml"; break;
495− default: source = ""; break;
496− }
497− }
498−
499− function openSubPage() {
500− // FIXME adding the following check before opening makes more sense to me,
501− // but it doesn't seem to work in qmltestrunner::GenericScopeView::test_previewOpenClose()
502− // if (!visible && status === Loader.Ready), x is different to width.
503− // Let's just check status for now.
504− if (status === Loader.Ready) open = true;
479+ open = true;
505 }
480 }
506
481
507 function closeSubPage() {
482 function closeSubPage() {
508 open = false;
483 open = false;
509− subPage = "";
510 }
484 }
511
485
512− onSubPageChanged: {
486+ source: visible && subPage && (subPage == "preview" ? "PreviewListView.qml" : "ScopeSettingsPage.qml") || ""
513− // let's update the source only when the loader is not visible
514− if (!visible) updateSource();
515− }
516
487
517− onVisibleChanged: {
488+ onLoaded: {
518− // let's update the source only when the loader is not visible
489+ if (status === Loader.Ready) {
519− if (!visible) updateSource();
490+ item.scope = Qt.binding(function() { return subPageLoader.scope } )
491+ item.scopeStyle = Qt.binding(function() { return subPageLoader.scopeStyle } )
492+ if (subPage == "preview") {
493+ item.open = Qt.binding(function() { return subPageLoader.open } )
494+ item.initialIndex = Qt.binding(function() { return subPageLoader.initialIndex } )
495+ item.model = Qt.binding(function() { return subPageLoader.model } )
496+ }
497+ }
520 }
498 }
521
499
522− onLoaded: updateBindings()
500+ onOpenChanged: pageHeaderLoader.item.unfocus();
523
501
524− onOpenChanged: {
502+ onVisibleChanged: {
525− if (open) updateBindings();
503+ if (!visible) {
526− pageHeaderLoader.item.unfocus();
504+ subPage = "";
505+ }
527 }
506 }
528
507
529 Connections {
508 Connections {