Unity 8
 All Classes Functions
SpreadDelegate.qml
1 /*
2  * Copyright 2014 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  * Authors: Michael Zanetti <michael.zanetti@canonical.com>
17  * Daniel d'Andrada <daniel.dandrada@canonical.com>
18  */
19 
20 import QtQuick 2.0
21 import Ubuntu.Components 1.1
22 import "../Components"
23 
24 Item {
25  id: root
26 
27  // to be read from outside
28  readonly property bool dragged: dragArea.moving
29  signal clicked()
30  signal closed()
31 
32  // to be set from outside
33  property bool interactive: true
34  property bool dropShadow: true
35  property real maximizedAppTopMargin
36  property alias swipeToCloseEnabled: dragArea.enabled
37  property bool closeable
38  property alias application: appWindow.application
39  property int orientation
40 
41  Item {
42  objectName: "appWindowWithShadow"
43 
44  y: dragArea.distance
45  width: parent.width
46  height: parent.height
47 
48  BorderImage {
49  anchors {
50  fill: appWindow
51  margins: -units.gu(2)
52  }
53  source: "graphics/dropshadow2gu.sci"
54  opacity: root.dropShadow ? .3 : 0
55  Behavior on opacity { UbuntuNumberAnimation {} }
56  }
57 
58  ApplicationWindow {
59  id: appWindow
60  objectName: application ? "appWindow_" + application.appId : "appWindow_null"
61  anchors {
62  fill: parent
63  topMargin: appWindow.fullscreen ? 0 : maximizedAppTopMargin
64  }
65 
66  interactive: root.interactive
67  orientation: root.orientation
68  }
69  }
70 
71  DraggingArea {
72  id: dragArea
73  objectName: "dragArea"
74  anchors.fill: parent
75 
76  property bool moving: false
77  property real distance: 0
78 
79  readonly property real minSpeedToClose: units.gu(40)
80 
81  onDragValueChanged: {
82  if (!dragging) {
83  return;
84  }
85  moving = moving || Math.abs(dragValue) > units.gu(1)
86  if (moving) {
87  distance = dragValue;
88  }
89  }
90 
91  onClicked: {
92  if (!moving) {
93  root.clicked();
94  }
95  }
96 
97  onDragEnd: {
98  if (!root.closeable) {
99  animation.animate("center")
100  return;
101  }
102 
103  // velocity and distance values specified by design prototype
104  if ((dragVelocity < -minSpeedToClose && distance < -units.gu(8)) || distance < -root.height / 2) {
105  animation.animate("up")
106  } else if ((dragVelocity > minSpeedToClose && distance > units.gu(8)) || distance > root.height / 2) {
107  animation.animate("down")
108  } else {
109  animation.animate("center")
110  }
111  }
112 
113  UbuntuNumberAnimation {
114  id: animation
115  objectName: "closeAnimation"
116  target: dragArea
117  property: "distance"
118  property bool requestClose: false
119 
120  function animate(direction) {
121  animation.from = dragArea.distance;
122  switch (direction) {
123  case "up":
124  animation.to = -root.height * 1.5;
125  requestClose = true;
126  break;
127  case "down":
128  animation.to = root.height * 1.5;
129  requestClose = true;
130  break;
131  default:
132  animation.to = 0
133  }
134  animation.start();
135  }
136 
137  onRunningChanged: {
138  if (!running) {
139  dragArea.moving = false;
140  if (requestClose) {
141  root.closed();
142  } else {
143  dragArea.distance = 0;
144  }
145  }
146  }
147  }
148  }
149 }