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