on 436b607 help sign in
unity8/qml/Components/Showable.qml
139 lines 3.7 kB history raw
1/*
2 * Copyright (C) 2013 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 */
17import QtQuick 2.0
19Item {
20 id: showable
22 property bool available: true
23 property bool shown: true
25 /* If your showable supports on demand content creation/destruction,
26 set this to false when destroyed and true when ready to be shown.
27 NOTE: You should load your content when "required" is true and
28 destroy when "required" is false
29 */
30 property bool created: true
31 property bool required
32 property bool __shouldShow: false
33 property bool __skipShowAnimation: false
35 property list<QtObject> hides
36 property var showAnimation
37 property var hideAnimation
39 // automatically set the target on showAnimation and hideAnimation to be the
40 // showable itself
41 onShowAnimationChanged: if (showAnimation) showAnimation["target"] = showable
42 onHideAnimationChanged: if (hideAnimation) hideAnimation["target"] = showable
44 Component.onCompleted: required = shown;
46 function __hideOthers() {
47 var i
48 for (i=0; i<hides.length; i++) {
49 hides[i].hide()
50 }
51 }
53 function show() {
54 required = true;
55 if (created) {
56 __reallyShow();
57 } else {
58 __shouldShow = true;
59 }
60 }
62 function showNow() {
63 __skipShowAnimation = true;
64 show();
65 }
67 onCreatedChanged: {
68 if (created && __shouldShow) {
69 __reallyShow();
70 __shouldShow = false;
71 }
72 }
74 function __reallyShow() {
75 if (!available) {
76 __skipShowAnimation = false;
77 return false;
78 }
80 __hideOthers();
82 if (hideAnimation != undefined && hideAnimation.running) {
83 hideAnimation.stop();
84 }
86 if (showAnimation != undefined) {
87 if (!showAnimation.running) {
88 showAnimation.restart()
89 }
90 if (__skipShowAnimation) {
91 showAnimation.complete();
92 }
93 } else {
94 visible = true;
95 }
97 shown = true;
98 __skipShowAnimation = false;
99 return true;
100 }
102 /*
103 Will be called right before starting the hideAnimation.
104 */
105 property var prepareToHide: function(){}
107 function hide() {
108 if (showAnimation != undefined && showAnimation.running) {
109 showAnimation.stop()
110 }
112 if (typeof prepareToHide === "function") {
113 prepareToHide();
114 } else {
115 console.warn("Showable.prepareToHide should be a function, but it's a " +
116 (typeof prepareToHide) + " instead");
117 }
119 if (hideAnimation != undefined) {
120 if (!hideAnimation.running) {
121 hideAnimation.restart()
122 }
123 } else {
124 visible = false
125 required = false
126 }
128 shown = false
129 }
131 Connections {
132 target: hideAnimation ? hideAnimation: null
133 onRunningChanged: {
134 if (!hideAnimation.running) {
135 required = false;
136 }
137 }
138 }