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