Unity 8
FakeMaximizeDelegate.qml
1 /*
2  * Copyright (C) 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 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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU 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 Utils 0.1 // For EdgeBarrierSettings
20 import "../Components/PanelState"
21 
22 Rectangle {
23  id: fakeRectangle
24  visible: opacity > 0 && target && !target.anyMaximized // we go from 0.2 to 0.5
25  enabled: visible
26  color: "#ffffff"
27  border.width: units.dp(2)
28  border.color: "#99ffffff"
29 
30  scale: progress > 0 && progress <= hintThreshold ? MathUtils.projectValue(progress, 0.0, 1.0, 1, 2) : 1
31  opacity: progress > 0 ? MathUtils.projectValue(progress, 0.0, 1.0, 0.2, 0.5) : 0
32 
33  property int edge: -1 // Item.TransformOrigin
34  property var target // appDelegate
35  property int leftMargin
36  property real appContainerWidth
37  property real appContainerHeight
38  property PanelState panelState
39 
40  readonly property real hintThreshold: 0.1
41 
42  // Edge push progress
43  // Value range is [0.0, 1.0]
44  readonly property real progress: priv.directProgress != -1 ? priv.directProgress : priv.accumulatedProgress
45 
46  signal passed(int origin)
47 
48  QtObject {
49  id: priv
50 
51  readonly property real accumulatedProgress: MathUtils.clamp(accumulatedPush / EdgeBarrierSettings.pushThreshold, 0.0, 1.0)
52  property real directProgress: -1
53  property real accumulatedPush: 0
54 
55  function push(amount) {
56  if (accumulatedPush === EdgeBarrierSettings.pushThreshold) {
57  // NO-OP
58  return;
59  }
60 
61  if (accumulatedPush + amount > EdgeBarrierSettings.pushThreshold) {
62  accumulatedPush = EdgeBarrierSettings.pushThreshold;
63  } else {
64  accumulatedPush += amount;
65  }
66 
67  if (accumulatedPush === EdgeBarrierSettings.pushThreshold) {
68  passed(edge);
69  // commit(); // NB: uncomment to have automatic maximization on 100% progress
70  }
71  }
72 
73  function setup(edge) {
74  if (edge !== fakeRectangle.edge) {
75  stop(); // a different edge, start anew
76  }
77  fakeRectangle.x = target.x;
78  fakeRectangle.y = target.y;
79  fakeRectangle.width = target.width;
80  fakeRectangle.height = target.height;
81  fakeRectangle.edge = edge;
82  fakeRectangle.transformOrigin = edge;
83  }
84 
85  function processAnimation(amount, animation, isProgress) {
86  if (isProgress) {
87  priv.directProgress = amount;
88  } else {
89  priv.directProgress = -1;
90  priv.push(amount);
91  }
92 
93  if (progress > hintThreshold) { // above 10% we start the full preview animation
94  animation.start();
95  }
96  }
97  }
98 
99  function commit() {
100  if (progress > hintThreshold && edge != -1) {
101  if (edge == Item.Top) {
102  target.requestMaximize();
103  } else if (edge == Item.Left) {
104  target.requestMaximizeLeft();
105  } else if (edge == Item.Right) {
106  target.requestMaximizeRight();
107  } else if (edge == Item.TopLeft) {
108  target.requestMaximizeTopLeft();
109  } else if (edge == Item.TopRight) {
110  target.requestMaximizeTopRight();
111  } else if (edge == Item.BottomLeft) {
112  target.requestMaximizeBottomLeft();
113  } else if (edge == Item.BottomRight) {
114  target.requestMaximizeBottomRight();
115  }
116  } else {
117  stop();
118  }
119  }
120 
121  function stop() {
122  priv.accumulatedPush = 0;
123  priv.directProgress = -1;
124  edge = -1;
125  }
126 
127  function maximize(amount, isProgress) {
128  if (fakeRectangle.edge != Item.Top) {
129  priv.setup(Item.Top);
130  }
131  priv.processAnimation(amount, fakeMaximizeAnimation, isProgress);
132  }
133 
134  function maximizeLeft(amount, isProgress) {
135  if (fakeRectangle.edge != Item.Left) {
136  priv.setup(Item.Left);
137  }
138  priv.processAnimation(amount, fakeMaximizeLeftAnimation, isProgress);
139  }
140 
141  function maximizeRight(amount, isProgress) {
142  if (fakeRectangle.edge != Item.Right) {
143  priv.setup(Item.Right);
144  }
145  priv.processAnimation(amount, fakeMaximizeRightAnimation, isProgress);
146  }
147 
148  function maximizeTopLeft(amount, isProgress) {
149  if (fakeRectangle.edge != Item.TopLeft) {
150  priv.setup(Item.TopLeft);
151  }
152  priv.processAnimation(amount, fakeMaximizeTopLeftAnimation, isProgress);
153  }
154 
155  function maximizeTopRight(amount, isProgress) {
156  if (fakeRectangle.edge != Item.TopRight) {
157  priv.setup(Item.TopRight);
158  }
159  priv.processAnimation(amount, fakeMaximizeTopRightAnimation, isProgress);
160  }
161 
162  function maximizeBottomLeft(amount, isProgress) {
163  if (fakeRectangle.edge != Item.BottomLeft) {
164  priv.setup(Item.BottomLeft);
165  }
166  priv.processAnimation(amount, fakeMaximizeBottomLeftAnimation, isProgress);
167  }
168 
169  function maximizeBottomRight(amount, isProgress) {
170  if (fakeRectangle.edge != Item.BottomRight) {
171  priv.setup(Item.BottomRight);
172  }
173  priv.processAnimation(amount, fakeMaximizeBottomRightAnimation, isProgress);
174  }
175 
176  Behavior on opacity { UbuntuNumberAnimation { duration: UbuntuAnimation.BriskDuration } }
177  Behavior on scale { UbuntuNumberAnimation { duration: UbuntuAnimation.BriskDuration } }
178 
179  ParallelAnimation {
180  id: fakeMaximizeAnimation
181  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: leftMargin }
182  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: panelState.panelHeight }
183  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: appContainerWidth - leftMargin }
184  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: appContainerHeight }
185  }
186 
187  ParallelAnimation {
188  id: fakeMaximizeLeftAnimation
189  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: leftMargin }
190  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: panelState.panelHeight }
191  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
192  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: appContainerHeight - panelState.panelHeight }
193  }
194 
195  ParallelAnimation {
196  id: fakeMaximizeRightAnimation
197  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth + leftMargin)/2 }
198  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: panelState.panelHeight }
199  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
200  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: appContainerHeight - panelState.panelHeight }
201  }
202 
203  ParallelAnimation {
204  id: fakeMaximizeTopLeftAnimation
205  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: leftMargin }
206  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: panelState.panelHeight }
207  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
208  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: (appContainerHeight - panelState.panelHeight)/2 }
209  }
210 
211  ParallelAnimation {
212  id: fakeMaximizeTopRightAnimation
213  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth + leftMargin)/2 }
214  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: panelState.panelHeight }
215  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
216  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: (appContainerHeight - panelState.panelHeight)/2 }
217  }
218 
219  ParallelAnimation {
220  id: fakeMaximizeBottomLeftAnimation
221  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: leftMargin }
222  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: (appContainerHeight + panelState.panelHeight)/2 }
223  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
224  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: appContainerHeight/2 }
225  }
226 
227  ParallelAnimation {
228  id: fakeMaximizeBottomRightAnimation
229  UbuntuNumberAnimation { target: fakeRectangle; properties: "x"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth + leftMargin)/2 }
230  UbuntuNumberAnimation { target: fakeRectangle; properties: "y"; duration: UbuntuAnimation.BriskDuration; to: (appContainerHeight + panelState.panelHeight)/2 }
231  UbuntuNumberAnimation { target: fakeRectangle; properties: "width"; duration: UbuntuAnimation.BriskDuration; to: (appContainerWidth - leftMargin)/2 }
232  UbuntuNumberAnimation { target: fakeRectangle; properties: "height"; duration: UbuntuAnimation.BriskDuration; to: appContainerHeight/2 }
233  }
234 }