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 
40  Item {
41  objectName: "appWindowWithShadow"
42 
43  y: dragArea.distance
44  width: parent.width
45  height: parent.height
46 
47  BorderImage {
48  anchors {
49  fill: appWindow
50  margins: -units.gu(2)
51  }
52  source: "graphics/dropshadow2gu.sci"
53  opacity: root.dropShadow ? .3 : 0
54  Behavior on opacity { UbuntuNumberAnimation {} }
55  }
56 
57  ApplicationWindow {
58  id: appWindow
59  anchors {
60  fill: parent
61  topMargin: appWindow.fullscreen ? 0 : maximizedAppTopMargin
62  }
63 
64  interactive: root.interactive
65  }
66  }
67 
68  DraggingArea {
69  id: dragArea
70  objectName: "dragArea"
71  anchors.fill: parent
72 
73  property bool moving: false
74  property real distance: 0
75 
76  readonly property real minSpeedToClose: units.gu(40)
77 
78  onDragValueChanged: {
79  if (!dragging) {
80  return;
81  }
82  moving = moving || Math.abs(dragValue) > units.gu(1)
83  if (moving) {
84  distance = dragValue;
85  }
86  }
87 
88  onClicked: {
89  if (!moving) {
90  root.clicked();
91  }
92  }
93 
94  onDragEnd: {
95  if (!root.closeable) {
96  animation.animate("center")
97  return;
98  }
99 
100  // velocity and distance values specified by design prototype
101  if ((dragVelocity < -minSpeedToClose && distance < -units.gu(8)) || distance < -root.height / 2) {
102  animation.animate("up")
103  } else if ((dragVelocity > minSpeedToClose && distance > units.gu(8)) || distance > root.height / 2) {
104  animation.animate("down")
105  } else {
106  animation.animate("center")
107  }
108  }
109 
110  UbuntuNumberAnimation {
111  id: animation
112  objectName: "closeAnimation"
113  target: dragArea
114  property: "distance"
115  property bool requestClose: false
116 
117  function animate(direction) {
118  animation.from = dragArea.distance;
119  switch (direction) {
120  case "up":
121  animation.to = -root.height * 1.5;
122  requestClose = true;
123  break;
124  case "down":
125  animation.to = root.height * 1.5;
126  requestClose = true;
127  break;
128  default:
129  animation.to = 0
130  }
131  animation.start();
132  }
133 
134  onRunningChanged: {
135  if (!running) {
136  dragArea.moving = false;
137  if (requestClose) {
138  root.closed();
139  } else {
140  dragArea.distance = 0;
141  }
142  }
143  }
144  }
145  }
146 }