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