Unity 8
Showable.qml
1 /*
2  * Copyright (C) 2013 Canonical, Ltd.
3  * Copyright (C) 2019 UBports Foundation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 import QtQuick 2.4
19 
20 Item {
21  id: showable
22 
23  property bool available: true
24  property bool shown: true
25 
26  /* If your showable supports on demand content creation/destruction,
27  set this to false when destroyed and true when ready to be shown.
28  NOTE: You should load your content when "required" is true and
29  destroy when "required" is false
30  */
31  property bool created: true
32  property bool required
33  property bool __shouldShow: false
34  property bool __skipShowAnimation: false
35  property bool __skipHideAnimation: false
36 
37  property list<QtObject> hides
38  property Animation showAnimation
39  property Animation hideAnimation
40 
41  // automatically set the target on showAnimation and hideAnimation to be the
42  // showable itself
43  onShowAnimationChanged: if (showAnimation && showAnimation.hasOwnProperty("target")) showAnimation["target"] = showable
44  onHideAnimationChanged: if (hideAnimation && hideAnimation.hasOwnProperty("target")) hideAnimation["target"] = showable
45 
46  Component.onCompleted: required = shown;
47 
48  function __hideOthers() {
49  var i
50  for (i=0; i<hides.length; i++) {
51  hides[i].hide()
52  }
53  }
54 
55  function show() {
56  required = true;
57  if (created) {
58  __reallyShow();
59  } else {
60  __shouldShow = true;
61  }
62  }
63 
64  function showNow() {
65  __skipShowAnimation = true;
66  show();
67  }
68 
69  onCreatedChanged: {
70  if (created && __shouldShow) {
71  __reallyShow();
72  __shouldShow = false;
73  }
74  }
75 
76  function __reallyShow() {
77  if (showAnimation != undefined && showAnimation.running)
78  return;
79 
80  if (!available) {
81  __skipShowAnimation = false;
82  return false;
83  }
84 
85  __hideOthers();
86 
87  if (hideAnimation != undefined && hideAnimation.running) {
88  hideAnimation.stop();
89  }
90 
91  if (showAnimation != undefined) {
92  showAnimation.restart()
93  if (__skipShowAnimation || shown) {
94  showAnimation.complete();
95  }
96  } else {
97  visible = true;
98  }
99 
100  shown = true;
101  __skipShowAnimation = false;
102  return true;
103  }
104 
105  /*
106  Will be called right before starting the hideAnimation.
107  */
108  property var prepareToHide: function(){}
109 
110  function hide() {
111  if (hideAnimation != undefined && hideAnimation.running)
112  return;
113 
114  if (showAnimation != undefined && showAnimation.running) {
115  showAnimation.stop()
116  }
117 
118  if (typeof prepareToHide === "function") {
119  prepareToHide();
120  } else {
121  console.warn("Showable.prepareToHide should be a function, but it's a " +
122  (typeof prepareToHide) + " instead");
123  }
124 
125  if (hideAnimation != undefined) {
126  hideAnimation.restart()
127  if (__skipHideAnimation || !shown) {
128  hideAnimation.complete();
129  }
130  } else {
131  visible = false
132  required = false
133  }
134 
135  shown = false
136  __skipHideAnimation = false;
137  }
138 
139  function hideNow() {
140  __skipHideAnimation = true;
141  hide();
142  }
143 
144  Connections {
145  target: hideAnimation ? hideAnimation: null
146  onRunningChanged: {
147  if (!hideAnimation.running) {
148  required = false;
149  }
150  }
151  }
152 }