Unity 8
SurfaceContainer.qml
1 /*
2  * Copyright 2014-2016 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  // Must be set from outside
28  property var surface: null
29 
30  // Might be changed from outside
31  property int requestedWidth: -1
32  property int requestedHeight: -1
33  property bool interactive
34  property int surfaceOrientationAngle: 0
35  property bool resizeSurface: true
36  property bool isPromptSurface: false
37  // FIME - dont export, use interactive property. Need to fix qtmir to handle consumesInputChanged
38  // to update surface activeFocus. See mock MirSurfaceItem.
39  property alias consumesInput: surfaceItem.consumesInput
40 
41  onSurfaceChanged: {
42  // Not a binding because animations might remove the surface from the surfaceItem
43  // programatically (in order to signal that a zombie surface is free for deletion),
44  // even though root.surface is still !null.
45  surfaceItem.surface = surface;
46  }
47 
48  InputWatcher {
49  target: surfaceItem
50  onTargetPressedChanged: {
51  if (targetPressed && root.interactive) {
52  root.focus = true;
53  root.forceActiveFocus();
54  }
55  }
56  }
57 
58  MirSurfaceItem {
59  id: surfaceItem
60  objectName: "surfaceItem"
61 
62  focus: true
63 
64  fillMode: MirSurfaceItem.PadOrCrop
65  consumesInput: true
66 
67  surfaceWidth: {
68  if (root.resizeSurface) {
69  if (root.requestedWidth >= 0) {
70  return root.requestedWidth;
71  } else {
72  return width;
73  }
74  } else {
75  return -1;
76  }
77  }
78 
79  surfaceHeight: {
80  if (root.resizeSurface) {
81  if (root.requestedHeight >= 0) {
82  return root.requestedHeight;
83  } else {
84  return height;
85  }
86  } else {
87  return -1;
88  }
89  }
90 
91  enabled: root.interactive
92  antialiasing: !root.interactive
93  orientationAngle: root.surfaceOrientationAngle
94  }
95 
96  TouchGate {
97  targetItem: surfaceItem
98  anchors.fill: root
99  enabled: surfaceItem.enabled
100  }
101 
102  // MirSurface size drives SurfaceContainer size
103  Binding {
104  target: surfaceItem; property: "width"; value: root.surface ? root.surface.size.width : 0
105  when: root.requestedWidth >= 0 && root.surface
106  }
107  Binding {
108  target: surfaceItem; property: "height"; value: root.surface ? root.surface.size.height : 0
109  when: root.requestedHeight >= 0 && root.surface
110  }
111  Binding {
112  target: root; property: "width"; value: surfaceItem.width
113  when: root.requestedWidth >= 0
114  }
115  Binding {
116  target: root; property: "height"; value: surfaceItem.height
117  when: root.requestedHeight >= 0
118  }
119 
120  // SurfaceContainer size drives MirSurface size
121  Binding {
122  target: surfaceItem; property: "width"; value: root.width
123  when: root.requestedWidth < 0
124  }
125  Binding {
126  target: surfaceItem; property: "height"; value: root.height
127  when: root.requestedHeight < 0
128  }
129 
130  Loader {
131  id: animationsLoader
132  objectName: "animationsLoader"
133  active: root.surface
134  source: {
135  if (root.isPromptSurface) {
136  return "PromptSurfaceAnimations.qml";
137  } else {
138  // Let ApplicationWindow do the animations
139  return "";
140  }
141  }
142  Binding {
143  target: animationsLoader.item
144  when: animationsLoader.item
145  property: "surfaceItem"
146  value: surfaceItem
147  }
148  Binding {
149  target: animationsLoader.item
150  when: animationsLoader.item
151  property: "container"
152  value: root
153  }
154  }
155 }