Unity 8
SurfaceContainer.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 Ubuntu.Components 1.3
19 import Ubuntu.Gestures 0.1 // For TouchGate
20 import Utils 0.1 // for InputWatcher
21 import Unity.Application 0.1 // for MirSurfaceItem
22 
23 FocusScope {
24  id: root
25  objectName: "surfaceContainer"
26 
27  property var surface: null
28  property bool hadSurface: false
29  property bool interactive
30  property int surfaceOrientationAngle: 0
31  property string name: surface ? surface.name : ""
32  property bool resizeSurface: true
33 
34  property int requestedWidth: -1
35  property int requestedHeight: -1
36 
37  onSurfaceChanged: {
38  if (surface) {
39  surfaceItem.surface = surface;
40  root.hadSurface = false;
41  }
42  }
43 
44  InputWatcher {
45  target: surfaceItem
46  onTargetPressedChanged: {
47  if (targetPressed && root.interactive) {
48  root.focus = true;
49  root.forceActiveFocus();
50  }
51  }
52  }
53 
54  MirSurfaceItem {
55  id: surfaceItem
56  objectName: "surfaceItem"
57 
58  fillMode: MirSurfaceItem.PadOrCrop
59  consumesInput: true
60 
61  surfaceWidth: {
62  if (root.resizeSurface) {
63  if (root.requestedWidth >= 0) {
64  return root.requestedWidth;
65  } else {
66  return width;
67  }
68  } else {
69  return -1;
70  }
71  }
72 
73  surfaceHeight: {
74  if (root.resizeSurface) {
75  if (root.requestedHeight >= 0) {
76  return root.requestedHeight;
77  } else {
78  return height;
79  }
80  } else {
81  return -1;
82  }
83  }
84 
85  enabled: root.interactive
86  focus: true
87  antialiasing: !root.interactive
88  orientationAngle: root.surfaceOrientationAngle
89  }
90 
91  // MirSurface size drives SurfaceContainer size
92  Binding {
93  target: surfaceItem; property: "width"; value: root.surface ? root.surface.size.width : 0
94  when: root.requestedWidth >= 0 && root.surface
95  }
96  Binding {
97  target: surfaceItem; property: "height"; value: root.surface ? root.surface.size.height : 0
98  when: root.requestedHeight >= 0 && root.surface
99  }
100  Binding {
101  target: root; property: "width"; value: surfaceItem.width
102  when: root.requestedWidth >= 0
103  }
104  Binding {
105  target: root; property: "height"; value: surfaceItem.height
106  when: root.requestedHeight >= 0
107  }
108 
109  // SurfaceContainer size drives MirSurface size
110  Binding {
111  target: surfaceItem; property: "width"; value: root.width
112  when: root.requestedWidth < 0
113  }
114  Binding {
115  target: surfaceItem; property: "height"; value: root.height
116  when: root.requestedHeight < 0
117  }
118 
119 
120  TouchGate {
121  targetItem: surfaceItem
122  anchors.fill: root
123  enabled: surfaceItem.enabled
124  }
125 
126  states: [
127  State {
128  name: "zombie"
129  when: surfaceItem.surface && !surfaceItem.live
130  }
131  ]
132  transitions: [
133  Transition {
134  from: ""; to: "zombie"
135  SequentialAnimation {
136  UbuntuNumberAnimation { target: surfaceItem; property: "opacity"; to: 0.0
137  duration: UbuntuAnimation.BriskDuration }
138  PropertyAction { target: surfaceItem; property: "visible"; value: false }
139  ScriptAction { script: {
140  surfaceItem.surface = null;
141  root.hadSurface = true;
142  } }
143  }
144  },
145  Transition {
146  from: "zombie"; to: ""
147  ScriptAction { script: {
148  surfaceItem.opacity = 1.0;
149  surfaceItem.visible = true;
150  } }
151  }
152  ]
153 }