Unity 8
SessionContainer.qml
1 /*
2  * Copyright 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 Lesser 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 Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 import QtQuick 2.0
18 import "Animations"
19 
20 FocusScope {
21  id: root
22  objectName: "sessionContainer"
23  property QtObject session
24  readonly property var childSessions: session ? session.childSessions : null
25  readonly property alias surface: _surfaceContainer.surface
26  property alias interactive: _surfaceContainer.interactive
27  property int orientation
28 
29  readonly property alias surfaceContainer: _surfaceContainer
30  SurfaceContainer {
31  id: _surfaceContainer
32  anchors.fill: parent
33  surface: session ? session.surface : null
34  orientation: root.orientation
35  }
36 
37  Repeater {
38  id: childSessionsRepeater
39  model: root.childSessions
40 
41  delegate: Loader {
42  objectName: "childDelegate" + index
43  anchors.fill: surfaceContainer
44 
45  // Only way to do recursive qml items.
46  source: Qt.resolvedUrl("SessionContainer.qml")
47 
48  z: index
49 
50  // Since a Loader is a FocusScope, propagate its focus to the loaded Item
51  Binding {
52  target: item; when: item
53  property: "focus"; value: focus
54  }
55 
56  Binding {
57  target: item; when: item
58  property: "interactive"; value: index == (childSessionsRepeater.count - 1) && root.interactive
59  }
60 
61  Binding {
62  target: item; when: item
63  property: "session"; value: modelData
64  }
65 
66  Binding {
67  target: item; when: item
68  property: "width"; value: root.width
69  }
70 
71  Binding {
72  target: item; when: item
73  property: "height"; value: root.height
74  }
75 
76  Binding {
77  target: item; when: item
78  property: "orientation"; value: root.orientation
79  }
80  }
81  }
82 
83  states: [
84  State {
85  name: "rootSession"
86  when: root.session && !root.session.parentSession
87  },
88 
89  State {
90  name: "childSession"
91  when: root.session && root.session.parentSession !== null && root.session.live
92  && !root.session.surface
93  },
94 
95  State {
96  name: "childSessionReady"
97  when: root.session && root.session.parentSession !== null && root.session.live
98  && root.session.surface !== null
99  },
100 
101  State {
102  name: "childSessionZombie"
103  when: root.session && root.session.parentSession !== null && !root.session.live
104  }
105  ]
106 
107  transitions: [
108  Transition {
109  to: "childSessionReady"
110  ScriptAction { script: { if (!surfaceContainer.hadSurface) { animateIn(swipeFromBottom); } } }
111  },
112  Transition {
113  to: "childSessionZombie"
114  ScriptAction { script: { animateOut(); } }
115  }
116  ]
117 
118  function animateIn(component) {
119  var animation = component.createObject(root, { "container": root, });
120  animation.start();
121 
122  var tmp = d.animations;
123  tmp.push(animation);
124  d.animations = tmp;
125  }
126 
127  function animateOut() {
128  if (d.animations.length > 0) {
129  var tmp = d.animations;
130  var popped = tmp.pop();
131  popped.completed.connect(function() { root.session.release(); } );
132  popped.end();
133  d.animations = tmp;
134  }
135  }
136 
137  Component {
138  id: swipeFromBottom
139  SwipeFromBottomAnimation {}
140  }
141 
142  QtObject {
143  id: d
144  property var animations: []
145 
146  property var focusedChild: {
147  if (childSessionsRepeater.count == 0) {
148  return _surfaceContainer;
149  } else {
150  return childSessionsRepeater.itemAt(childSessionsRepeater.count - 1);
151  }
152  }
153  onFocusedChildChanged: {
154  focusedChild.focus = true;
155  }
156  }
157 }