Merge lp:~3v1n0/unity/animation-utils into lp:unity

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Christopher Townsend
Approved revision: no longer in the source branch.
Merged at revision: 3520
Proposed branch: lp:~3v1n0/unity/animation-utils
Merge into: lp:unity
Prerequisite: lp:~3v1n0/unity/launcher-nux-animation
Diff against target: 1003 lines (+445/-245)
12 files modified
dash/DashController.cpp (+2/-12)
hud/HudController.cpp (+2/-12)
launcher/Launcher.cpp (+12/-109)
launcher/LauncherIcon.cpp (+8/-22)
launcher/SwitcherController.cpp (+3/-17)
panel/PanelMenuView.cpp (+6/-23)
shortcuts/ShortcutController.cpp (+6/-24)
shutdown/SessionController.cpp (+6/-26)
tests/CMakeLists.txt (+1/-0)
tests/test_animation_utils.cpp (+271/-0)
unity-shared/AnimationUtils-inl.h (+52/-0)
unity-shared/AnimationUtils.h (+76/-0)
To merge this branch: bzr merge lp:~3v1n0/unity/animation-utils
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Christopher Townsend (community) Approve
Review via email: mp+185417@code.launchpad.net

Commit message

AnimationUtils: add utility functions to handle nux::Animation's start and reversion

Update the whole code using nux::animation to use these facility functions.

Description of the change

Added some utility functions to perform the simple operations of starting and reversing the animations.
It's something we do very often on the untiy code, and with this we can make sure we always do it properly instead of using copy and paste.

Replaced all the code around and added a full suite of unit tests to check the behaviour.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Christopher Townsend (townsend) wrote :

I get a text conflict in launcher/Launcher.cpp when merging this in. Going to go ahead and mark this as Needs Fixing before reviewing the rest.

review: Needs Fixing
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Ok, I've finally fixed the conflict ;)

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Christopher Townsend (townsend) wrote :

Getting a compile error:

/home/townsend/Reviews/test-marco-fixes/unity/launcher/Launcher.cpp: In member function ‘void unity::launcher::Launcher::SetHover(bool)’:
/home/townsend/Reviews/test-marco-fixes/unity/launcher/Launcher.cpp:1436:3: error: expected ‘;’ before ‘if’
   if (_folded)
   ^

review: Needs Fixing
Revision history for this message
Christopher Townsend (townsend) wrote :

Very nice:)

LGTM!

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'dash/DashController.cpp'
2--- dash/DashController.cpp 2013-09-16 17:40:56 +0000
3+++ dash/DashController.cpp 2013-09-19 13:00:24 +0000
4@@ -25,6 +25,7 @@
5 #include "UnityCore/GSettingsScopes.h"
6
7 #include "ApplicationStarterImp.h"
8+#include "unity-shared/AnimationUtils.h"
9 #include "unity-shared/DashStyle.h"
10 #include "unity-shared/PanelStyle.h"
11 #include "unity-shared/UBusMessages.h"
12@@ -377,18 +378,7 @@
13 void Controller::StartShowHideTimeline()
14 {
15 EnsureDash();
16-
17- if (timeline_animator_.CurrentState() == nux::animation::Animation::State::Running)
18- {
19- timeline_animator_.Reverse();
20- }
21- else
22- {
23- if (visible_)
24- timeline_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
25- else
26- timeline_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
27- }
28+ animation::StartOrReverseIf(timeline_animator_, visible_);
29 }
30
31 void Controller::OnViewShowHideFrame(double opacity)
32
33=== modified file 'hud/HudController.cpp'
34--- hud/HudController.cpp 2013-03-20 01:14:35 +0000
35+++ hud/HudController.cpp 2013-09-19 13:00:24 +0000
36@@ -22,6 +22,7 @@
37 #include <Nux/HLayout.h>
38 #include <UnityCore/Variant.h>
39
40+#include "unity-shared/AnimationUtils.h"
41 #include "unity-shared/ApplicationManager.h"
42 #include "unity-shared/WindowManager.h"
43 #include "unity-shared/PanelStyle.h"
44@@ -437,18 +438,7 @@
45 void Controller::StartShowHideTimeline()
46 {
47 EnsureHud();
48-
49- if (timeline_animator_.CurrentState() == nux::animation::Animation::State::Running)
50- {
51- timeline_animator_.Reverse();
52- }
53- else
54- {
55- if (visible_)
56- timeline_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
57- else
58- timeline_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
59- }
60+ animation::StartOrReverseIf(timeline_animator_, visible_);
61 }
62
63 void Controller::OnViewShowHideFrame(double opacity)
64
65=== modified file 'launcher/Launcher.cpp'
66--- launcher/Launcher.cpp 2013-09-16 17:01:00 +0000
67+++ launcher/Launcher.cpp 2013-09-19 13:00:24 +0000
68@@ -44,6 +44,7 @@
69 #include "LauncherModel.h"
70 #include "QuicklistManager.h"
71 #include "QuicklistView.h"
72+#include "unity-shared/AnimationUtils.h"
73 #include "unity-shared/IconRenderer.h"
74 #include "unity-shared/TimeUtil.h"
75 #include "unity-shared/TextureCache.h"
76@@ -62,7 +63,6 @@
77 namespace unity
78 {
79 using ui::RenderArg;
80-namespace na = nux::animation;
81
82 namespace launcher
83 {
84@@ -257,7 +257,7 @@
85 if (_drag_out_delta_x >= DRAG_OUT_PIXELS - 90.0f)
86 _hide_machine.SetQuirk(LauncherHideMachine::MT_DRAG_OUT, true);
87
88- drag_out_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
89+ animation::StartOrReverse(drag_out_animation_, animation::Direction::BACKWARD);
90 _drag_gesture_ongoing = false;
91 }
92 #endif
93@@ -290,32 +290,7 @@
94 bool is_beyond_drag_threshold = MouseBeyondDragThreshold();
95
96 if (was_beyond_drag_threshold != is_beyond_drag_threshold)
97- {
98- if (!is_beyond_drag_threshold)
99- {
100- if (drag_icon_animation_.CurrentState() == na::Animation::State::Running)
101- {
102- if (drag_icon_animation_.GetFinishValue() != 1.0f)
103- drag_icon_animation_.Reverse();
104- }
105- else
106- {
107- drag_icon_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
108- }
109- }
110- else
111- {
112- if (drag_icon_animation_.CurrentState() == na::Animation::State::Running)
113- {
114- if (drag_icon_animation_.GetFinishValue() != 0.0f)
115- drag_icon_animation_.Reverse();
116- }
117- else
118- {
119- drag_icon_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
120- }
121- }
122- }
123+ animation::StartOrReverseIf(drag_icon_animation_, !is_beyond_drag_threshold);
124
125 EnsureScrollTimer();
126 }
127@@ -1218,7 +1193,7 @@
128 _hover_machine.SetQuirk(LauncherHoverMachine::PLACES_VISIBLE, true);
129
130 if (options()->hide_mode != LAUNCHER_HIDE_NEVER)
131- dash_showing_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
132+ animation::StartOrReverse(dash_showing_animation_, animation::Direction::FORWARD);
133 }
134 if (identity == "hud")
135 {
136@@ -1305,29 +1280,9 @@
137 _hide_machine.SetQuirk(LauncherHideMachine::MOUSE_MOVE_POST_REVEAL, false);
138 _hide_machine.SetQuirk(LauncherHideMachine::MT_DRAG_OUT, false);
139 SetStateMouseOverLauncher(false);
140+ }
141
142- if (auto_hide_animation_.CurrentState() == na::Animation::State::Running)
143- {
144- if (auto_hide_animation_.GetFinishValue() != 1.0f)
145- auto_hide_animation_.Reverse();
146- }
147- else
148- {
149- auto_hide_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
150- }
151- }
152- else
153- {
154- if (auto_hide_animation_.CurrentState() == na::Animation::State::Running)
155- {
156- if (auto_hide_animation_.GetFinishValue() != 0.0f)
157- auto_hide_animation_.Reverse();
158- }
159- else
160- {
161- auto_hide_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
162- }
163- }
164+ animation::StartOrReverseIf(auto_hide_animation_, hide_launcher);
165
166 _postreveal_mousemove_delta_x = 0;
167 _postreveal_mousemove_delta_y = 0;
168@@ -1476,34 +1431,10 @@
169 _hovered = hovered;
170
171 if (!IsInKeyNavMode() && _hovered)
172- {
173 _enter_y = _mouse_position.y;
174
175- if (_folded)
176- {
177- if (hover_animation_.CurrentState() == na::Animation::State::Running)
178- {
179- if (hover_animation_.GetFinishValue() != 1.0f)
180- hover_animation_.Reverse();
181- }
182- else
183- {
184- hover_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
185- }
186- }
187- }
188- else if (_folded)
189- {
190- if (hover_animation_.CurrentState() == na::Animation::State::Running)
191- {
192- if (hover_animation_.GetFinishValue() != 0.0f)
193- hover_animation_.Reverse();
194- }
195- else
196- {
197- hover_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
198- }
199- }
200+ if (_folded)
201+ animation::StartOrReverseIf(hover_animation_, _hovered);
202
203 if (IsOverlayOpen() && !_hide_machine.GetQuirk(LauncherHideMachine::EXTERNAL_DND_ACTIVE))
204 {
205@@ -2124,17 +2055,7 @@
206 }
207
208 if (MouseBeyondDragThreshold())
209- {
210- if (drag_icon_animation_.CurrentState() == na::Animation::State::Running)
211- {
212- if (drag_icon_animation_.GetFinishValue() != 1.0f)
213- drag_icon_animation_.Reverse();
214- }
215- else
216- {
217- drag_icon_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
218- }
219- }
220+ animation::StartOrReverse(drag_icon_animation_, animation::Direction::FORWARD);
221
222 _hide_machine.SetQuirk(LauncherHideMachine::INTERNAL_DND_ACTIVE, false);
223 }
224@@ -2518,7 +2439,7 @@
225
226 if (GetActionState() == ACTION_DRAG_LAUNCHER)
227 {
228- drag_over_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
229+ animation::StartOrReverse(drag_over_animation_, animation::Direction::BACKWARD);
230 }
231
232 _icon_mouse_down = nullptr;
233@@ -2739,30 +2660,12 @@
234 if (_dnd_hovered_icon)
235 _dnd_hovered_icon->SendDndLeave();
236
237- if (dnd_hide_animation_.CurrentState() == na::Animation::State::Running)
238- {
239- if (dnd_hide_animation_.GetFinishValue() != 1.0f)
240- dnd_hide_animation_.Reverse();
241- }
242- else
243- {
244- dnd_hide_animation_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
245- }
246-
247+ animation::StartOrReverse(dnd_hide_animation_, animation::Direction::FORWARD);
248 _drag_edge_touching = true;
249 }
250 else if (_mouse_position.x != 0 && _drag_edge_touching)
251 {
252- if (dnd_hide_animation_.CurrentState() == na::Animation::State::Running)
253- {
254- if (dnd_hide_animation_.GetFinishValue() != 0.0f)
255- dnd_hide_animation_.Reverse();
256- }
257- else
258- {
259- dnd_hide_animation_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
260- }
261-
262+ animation::StartOrReverse(dnd_hide_animation_, animation::Direction::BACKWARD);
263 _drag_edge_touching = false;
264 }
265 }
266
267=== modified file 'launcher/LauncherIcon.cpp'
268--- launcher/LauncherIcon.cpp 2013-07-31 01:27:07 +0000
269+++ launcher/LauncherIcon.cpp 2013-09-19 13:00:24 +0000
270@@ -31,6 +31,7 @@
271 #include "unity-shared/CairoTexture.h"
272 #include "LauncherIcon.h"
273 #include "Launcher.h"
274+#include "unity-shared/AnimationUtils.h"
275 #include "unity-shared/TimeUtil.h"
276
277 #include "QuicklistManager.h"
278@@ -117,13 +118,11 @@
279 mouse_click.connect(sigc::mem_fun(this, &LauncherIcon::RecvMouseClick));
280
281 _tooltip_fade_animator.updated.connect([this] (double opacity) {
282- if (_tooltip)
283- {
284- _tooltip->SetOpacity(opacity);
285-
286- if (opacity == 0.0f && _tooltip_fade_animator.GetStartValue() > _tooltip_fade_animator.GetFinishValue())
287- _tooltip->ShowWindow(false);
288- }
289+ if (_tooltip) _tooltip->SetOpacity(opacity);
290+ });
291+ _tooltip_fade_animator.finished.connect([this] {
292+ if (_tooltip && animation::GetDirection(_tooltip_fade_animator) == animation::Direction::BACKWARD)
293+ _tooltip->ShowWindow(false);
294 });
295 }
296
297@@ -501,10 +500,7 @@
298 _tooltip->ShowWindow(!tooltip_text().empty());
299 tooltip_visible.emit(_tooltip);
300
301- if (_tooltip_fade_animator.CurrentState() == nux::animation::Animation::State::Running)
302- _tooltip_fade_animator.Reverse();
303- else
304- _tooltip_fade_animator.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
305+ animation::StartOrReverse(_tooltip_fade_animator, animation::Direction::FORWARD);
306 }
307
308 void
309@@ -652,17 +648,7 @@
310 void LauncherIcon::HideTooltip()
311 {
312 if (_tooltip)
313- {
314- if (_tooltip_fade_animator.CurrentState() == nux::animation::Animation::State::Running &&
315- _tooltip_fade_animator.GetFinishValue() == 1.0)
316- {
317- _tooltip_fade_animator.Reverse();
318- }
319- else
320- {
321- _tooltip_fade_animator.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
322- }
323- }
324+ animation::StartOrReverse(_tooltip_fade_animator, animation::Direction::BACKWARD);
325
326 tooltip_visible.emit(nux::ObjectPtr<nux::View>(nullptr));
327 }
328
329=== modified file 'launcher/SwitcherController.cpp'
330--- launcher/SwitcherController.cpp 2013-09-11 19:32:34 +0000
331+++ launcher/SwitcherController.cpp 2013-09-19 13:00:24 +0000
332@@ -21,6 +21,7 @@
333 #include <Nux/Nux.h>
334 #include <Nux/HLayout.h>
335
336+#include "unity-shared/AnimationUtils.h"
337 #include "unity-shared/UBusMessages.h"
338 #include "unity-shared/WindowManager.h"
339 #include "unity-shared/UScreen.h"
340@@ -423,15 +424,7 @@
341 view_->live_background = true;
342 view_window_->ShowWindow(true);
343 view_window_->PushToFront();
344-
345- if (fade_animator_.CurrentState() == nux::animation::Animation::State::Running)
346- {
347- fade_animator_.Reverse();
348- }
349- else
350- {
351- fade_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
352- }
353+ animation::StartOrReverse(fade_animator_, animation::Direction::FORWARD);
354 }
355 }
356
357@@ -519,14 +512,7 @@
358
359 obj_->visible_ = false;
360
361- if (fade_animator_.CurrentState() == nux::animation::Animation::State::Running)
362- {
363- fade_animator_.Reverse();
364- }
365- else
366- {
367- fade_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
368- }
369+ animation::StartOrReverse(fade_animator_, animation::Direction::BACKWARD);
370 }
371
372 void Controller::Impl::DetailHide()
373
374=== modified file 'panel/PanelMenuView.cpp'
375--- panel/PanelMenuView.cpp 2013-07-01 21:30:40 +0000
376+++ panel/PanelMenuView.cpp 2013-09-19 13:00:24 +0000
377@@ -21,8 +21,9 @@
378 #include <Nux/Nux.h>
379 #include <NuxCore/Logger.h>
380
381+#include "PanelMenuView.h"
382+#include "unity-shared/AnimationUtils.h"
383 #include "unity-shared/CairoTexture.h"
384-#include "PanelMenuView.h"
385 #include "unity-shared/PanelStyle.h"
386 #include "unity-shared/UnitySettings.h"
387 #include "unity-shared/UBusMessages.h"
388@@ -34,8 +35,6 @@
389 #include "config.h"
390 #include <glib/gi18n-lib.h>
391
392-namespace na = nux::animation;
393-
394 namespace unity
395 {
396 DECLARE_LOGGER(logger, "unity.panel.menu");
397@@ -305,35 +304,19 @@
398
399 void PanelMenuView::StartFadeIn(int duration)
400 {
401- if (opacity_animator_.CurrentState() == na::Animation::State::Running)
402- {
403- if (opacity_animator_.GetFinishValue() != 1.0f)
404- opacity_animator_.Reverse();
405-
406- return;
407- }
408-
409 opacity_animator_.SetDuration(duration >= 0 ? duration : menus_fadein_);
410- opacity_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
411+ animation::StartOrReverse(opacity_animator_, animation::Direction::FORWARD);
412 }
413
414 void PanelMenuView::StartFadeOut(int duration)
415 {
416- if (opacity_animator_.CurrentState() == na::Animation::State::Running)
417- {
418- if (opacity_animator_.GetFinishValue() != 0.0f)
419- opacity_animator_.Reverse();
420-
421- return;
422- }
423-
424 opacity_animator_.SetDuration(duration >= 0 ? duration : menus_fadeout_);
425- opacity_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
426+ animation::StartOrReverse(opacity_animator_, animation::Direction::BACKWARD);
427 }
428
429 void PanelMenuView::OnFadeAnimatorUpdated(double progress)
430 {
431- if (opacity_animator_.GetFinishValue() == 1.0f) /* Fading in... */
432+ if (animation::GetDirection(opacity_animator_) == animation::Direction::FORWARD) /* Fading in... */
433 {
434 if (ShouldDrawMenus() && opacity() != 1.0f)
435 opacity = progress;
436@@ -341,7 +324,7 @@
437 if (ShouldDrawButtons() && window_buttons_->opacity() != 1.0f)
438 window_buttons_->opacity = progress;
439 }
440- else if (opacity_animator_.GetFinishValue() == 0.0f) /* Fading out... */
441+ else if (animation::GetDirection(opacity_animator_) == animation::Direction::BACKWARD) /* Fading out... */
442 {
443 if (!ShouldDrawMenus() && opacity() != 0.0f)
444 opacity = progress;
445
446=== modified file 'shortcuts/ShortcutController.cpp'
447--- shortcuts/ShortcutController.cpp 2013-02-18 18:05:05 +0000
448+++ shortcuts/ShortcutController.cpp 2013-09-19 13:00:24 +0000
449@@ -20,11 +20,10 @@
450 #include "ShortcutController.h"
451 #include "ShortcutModel.h"
452
453+#include "unity-shared/AnimationUtils.h"
454 #include "unity-shared/UBusMessages.h"
455 #include "unity-shared/UScreen.h"
456
457-namespace na = nux::animation;
458-
459 namespace unity
460 {
461 namespace shortcut
462@@ -129,16 +128,7 @@
463 if (visible_)
464 {
465 view_->live_background = true;
466-
467- if (fade_animator_.CurrentState() == na::Animation::State::Running)
468- {
469- if (fade_animator_.GetFinishValue() == 0.0f)
470- fade_animator_.Reverse();
471- }
472- else
473- {
474- fade_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
475- }
476+ animation::StartOrReverse(fade_animator_, animation::Direction::FORWARD);
477 }
478
479 return false;
480@@ -214,16 +204,7 @@
481 if (view_window_ && view_window_->GetOpacity() > 0.0f)
482 {
483 view_->live_background = false;
484-
485- if (fade_animator_.CurrentState() == na::Animation::State::Running)
486- {
487- if (fade_animator_.GetFinishValue() == 1.0f)
488- fade_animator_.Reverse();
489- }
490- else
491- {
492- fade_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
493- }
494+ animation::StartOrReverse(fade_animator_, animation::Direction::BACKWARD);
495 }
496 }
497
498@@ -258,12 +239,13 @@
499 void Controller::AddProperties(GVariantBuilder* builder)
500 {
501 bool animating = (fade_animator_.CurrentState() == na::Animation::State::Running);
502+ auto direction = animation::GetDirection(fade_animator_);
503
504 unity::variant::BuilderWrapper(builder)
505 .add("timeout_duration", SUPER_TAP_DURATION + FADE_DURATION)
506 .add("enabled", IsEnabled())
507- .add("about_to_show", (Visible() && animating && fade_animator_.GetFinishValue() == 1.0f))
508- .add("about_to_hide", (Visible() && animating && fade_animator_.GetFinishValue() == 0.0f))
509+ .add("about_to_show", (Visible() && animating && direction == animation::Direction::FORWARD))
510+ .add("about_to_hide", (Visible() && animating && direction == animation::Direction::FORWARD))
511 .add("visible", (Visible() && view_window_ && view_window_->GetOpacity() == 1.0f));
512 }
513
514
515=== modified file 'shutdown/SessionController.cpp'
516--- shutdown/SessionController.cpp 2013-03-19 18:47:01 +0000
517+++ shutdown/SessionController.cpp 2013-09-19 13:00:24 +0000
518@@ -19,6 +19,7 @@
519
520 #include "SessionController.h"
521
522+#include "unity-shared/AnimationUtils.h"
523 #include "unity-shared/UBusMessages.h"
524 #include "unity-shared/UScreen.h"
525 #include "unity-shared/WindowManager.h"
526@@ -58,13 +59,9 @@
527
528 ubus_manager_.SendMessage(UBUS_BACKGROUND_REQUEST_COLOUR_EMIT);
529
530- fade_animator_.updated.connect([this] (double opacity) {
531- if (!view_window_)
532- return;
533-
534- view_window_->SetOpacity(opacity);
535-
536- if (opacity == 0.0f && fade_animator_.GetFinishValue() == 0.0f)
537+ fade_animator_.updated.connect([this] (double opacity) { view_window_->SetOpacity(opacity); });
538+ fade_animator_.finished.connect([this] {
539+ if (animation::GetDirection(fade_animator_) == animation::Direction::BACKWARD)
540 CloseWindow();
541 });
542 }
543@@ -109,16 +106,7 @@
544 view_window_->PushToFront();
545 view_window_->SetInputFocus();
546 nux::GetWindowCompositor().SetKeyFocusArea(view_->key_focus_area());
547-
548- if (fade_animator_.CurrentState() == na::Animation::State::Running)
549- {
550- if (fade_animator_.GetFinishValue() == 0.0f)
551- fade_animator_.Reverse();
552- }
553- else
554- {
555- fade_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
556- }
557+ animation::StartOrReverse(fade_animator_, animation::Direction::FORWARD);
558 }
559
560 nux::Point Controller::GetOffsetPerMonitor(int monitor)
561@@ -189,15 +177,7 @@
562
563 void Controller::Hide()
564 {
565- if (fade_animator_.CurrentState() == na::Animation::State::Running)
566- {
567- if (fade_animator_.GetFinishValue() == 1.0f)
568- fade_animator_.Reverse();
569- }
570- else
571- {
572- fade_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
573- }
574+ animation::StartOrReverse(fade_animator_, animation::Direction::BACKWARD);
575 }
576
577 void Controller::CloseWindow()
578
579=== modified file 'tests/CMakeLists.txt'
580--- tests/CMakeLists.txt 2013-09-04 19:08:31 +0000
581+++ tests/CMakeLists.txt 2013-09-19 13:00:24 +0000
582@@ -132,6 +132,7 @@
583 test_main_xless.cpp
584 mock-application.cpp
585 test_abstract_interface_generator.cpp
586+ test_animation_utils.cpp
587 test_connection_manager.cpp
588 test_glib_dbus_object.cpp
589 test_glib_cancellable.cpp
590
591=== added file 'tests/test_animation_utils.cpp'
592--- tests/test_animation_utils.cpp 1970-01-01 00:00:00 +0000
593+++ tests/test_animation_utils.cpp 2013-09-19 13:00:24 +0000
594@@ -0,0 +1,271 @@
595+// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
596+/*
597+ * Copyright (C) 2013 Canonical Ltd
598+ *
599+ * This program is free software: you can redistribute it and/or modify
600+ * it under the terms of the GNU General Public License version 3 as
601+ * published by the Free Software Foundation.
602+ *
603+ * This program is distributed in the hope that it will be useful,
604+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
605+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
606+ * GNU General Public License for more details.
607+ *
608+ * You should have received a copy of the GNU General Public License
609+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
610+ *
611+ * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
612+ */
613+
614+#include <gmock/gmock.h>
615+#include <glib.h>
616+#include "unity-shared/AnimationUtils.h"
617+
618+namespace
619+{
620+
621+using namespace unity::animation;
622+
623+TEST(TestAnimationUtils, StartOrReverseNotRunning)
624+{
625+ na::AnimateValue<double> animation;
626+ double start = g_random_double_range(std::numeric_limits<double>::min(), -1);
627+ double finish = g_random_double_range(1, std::numeric_limits<double>::max());
628+
629+ StartOrReverse<double>(animation, start, finish);
630+ EXPECT_DOUBLE_EQ(start, animation.GetStartValue());
631+ EXPECT_DOUBLE_EQ(finish, animation.GetFinishValue());
632+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
633+}
634+
635+TEST(TestAnimationUtils, StartOrReverseKeepsRunning)
636+{
637+ na::AnimateValue<double> animation(3);
638+ double start = g_random_double_range(std::numeric_limits<double>::min(), -1);
639+ double finish = g_random_double_range(1, std::numeric_limits<double>::max());
640+
641+ animation.SetStartValue(start).SetFinishValue(finish).Start();
642+ ASSERT_EQ(na::Animation::State::Running, animation.CurrentState());
643+ animation.Advance(1);
644+ ASSERT_EQ(1, animation.CurrentTimePosition());
645+
646+ StartOrReverse<double>(animation, start, finish);
647+ EXPECT_DOUBLE_EQ(start, animation.GetStartValue());
648+ EXPECT_DOUBLE_EQ(finish, animation.GetFinishValue());
649+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
650+ EXPECT_EQ(1, animation.CurrentTimePosition());
651+}
652+
653+TEST(TestAnimationUtils, StartOrReverseReversesRunning)
654+{
655+ na::AnimateValue<double> animation(3);
656+ double start = g_random_double_range(std::numeric_limits<double>::min(), -1);
657+ double finish = g_random_double_range(1, std::numeric_limits<double>::max());
658+
659+ animation.SetStartValue(start).SetFinishValue(finish).Start();
660+ ASSERT_EQ(na::Animation::State::Running, animation.CurrentState());
661+ animation.Advance(1);
662+ ASSERT_EQ(1, animation.CurrentTimePosition());
663+
664+ StartOrReverse<double>(animation, finish, start);
665+ EXPECT_DOUBLE_EQ(finish, animation.GetStartValue());
666+ EXPECT_DOUBLE_EQ(start, animation.GetFinishValue());
667+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
668+ EXPECT_EQ(2, animation.CurrentTimePosition());
669+}
670+
671+TEST(TestAnimationUtils, StartOrReverseInValidRunning)
672+{
673+ na::AnimateValue<double> animation(3);
674+ double start = g_random_double_range(std::numeric_limits<double>::min(), -1);
675+ double finish = g_random_double_range(1, std::numeric_limits<double>::max());
676+
677+ animation.SetStartValue(start+1).SetFinishValue(finish-1).Start();
678+ ASSERT_EQ(na::Animation::State::Running, animation.CurrentState());
679+ animation.Advance(1);
680+ ASSERT_EQ(1, animation.CurrentTimePosition());
681+
682+ StartOrReverse<double>(animation, finish, start);
683+ EXPECT_DOUBLE_EQ(finish, animation.GetStartValue());
684+ EXPECT_DOUBLE_EQ(start, animation.GetFinishValue());
685+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
686+ EXPECT_EQ(0, animation.CurrentTimePosition());
687+}
688+
689+TEST(TestAnimationUtils, StartOrReverseDoubleForward)
690+{
691+ na::AnimateValue<double> animation;
692+ StartOrReverse(animation, Direction::FORWARD);
693+
694+ EXPECT_DOUBLE_EQ(0.0f, animation.GetStartValue());
695+ EXPECT_DOUBLE_EQ(1.0f, animation.GetFinishValue());
696+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
697+}
698+
699+TEST(TestAnimationUtils, StartOrReverseDoubleBackward)
700+{
701+ na::AnimateValue<double> animation;
702+ StartOrReverse(animation, Direction::BACKWARD);
703+
704+ EXPECT_DOUBLE_EQ(1.0f, animation.GetStartValue());
705+ EXPECT_DOUBLE_EQ(0.0f, animation.GetFinishValue());
706+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
707+}
708+
709+TEST(TestAnimationUtils, StartOrReverseIfDoubleTrue)
710+{
711+ na::AnimateValue<double> animation;
712+
713+ StartOrReverseIf(animation, true);
714+ EXPECT_DOUBLE_EQ(0.0f, animation.GetStartValue());
715+ EXPECT_DOUBLE_EQ(1.0f, animation.GetFinishValue());
716+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
717+}
718+
719+TEST(TestAnimationUtils, StartOrReverseIfDoubleFalse)
720+{
721+ na::AnimateValue<double> animation;
722+ StartOrReverseIf(animation, false);
723+
724+ EXPECT_DOUBLE_EQ(1.0f, animation.GetStartValue());
725+ EXPECT_DOUBLE_EQ(0.0f, animation.GetFinishValue());
726+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
727+}
728+
729+TEST(TestAnimationUtils, StartOrReverseFloatForward)
730+{
731+ na::AnimateValue<float> animation;
732+ StartOrReverse(animation, Direction::FORWARD);
733+
734+ EXPECT_FLOAT_EQ(0.0f, animation.GetStartValue());
735+ EXPECT_FLOAT_EQ(1.0f, animation.GetFinishValue());
736+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
737+}
738+
739+TEST(TestAnimationUtils, StartOrReverseFloatBackward)
740+{
741+ na::AnimateValue<float> animation;
742+ StartOrReverse(animation, Direction::BACKWARD);
743+
744+ EXPECT_FLOAT_EQ(1.0f, animation.GetStartValue());
745+ EXPECT_FLOAT_EQ(0.0f, animation.GetFinishValue());
746+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
747+}
748+
749+TEST(TestAnimationUtils, StartOrReverseIfFloatTrue)
750+{
751+ na::AnimateValue<float> animation;
752+ StartOrReverseIf(animation, true);
753+
754+ EXPECT_FLOAT_EQ(0.0f, animation.GetStartValue());
755+ EXPECT_FLOAT_EQ(1.0f, animation.GetFinishValue());
756+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
757+}
758+
759+TEST(TestAnimationUtils, StartOrReverseIfFloatFalse)
760+{
761+ na::AnimateValue<float> animation;
762+ StartOrReverseIf(animation, false);
763+
764+ EXPECT_FLOAT_EQ(1.0f, animation.GetStartValue());
765+ EXPECT_FLOAT_EQ(0.0f, animation.GetFinishValue());
766+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
767+}
768+
769+TEST(TestAnimationUtils, StartOrReverseIntForward)
770+{
771+ na::AnimateValue<int> animation;
772+ StartOrReverse(animation, Direction::FORWARD);
773+
774+ EXPECT_EQ(0, animation.GetStartValue());
775+ EXPECT_EQ(100, animation.GetFinishValue());
776+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
777+}
778+
779+TEST(TestAnimationUtils, StartOrReverseIntBackward)
780+{
781+ na::AnimateValue<int> animation;
782+ StartOrReverse(animation, Direction::BACKWARD);
783+
784+ EXPECT_EQ(100, animation.GetStartValue());
785+ EXPECT_EQ(0, animation.GetFinishValue());
786+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
787+}
788+
789+TEST(TestAnimationUtils, StartOrReverseIfIntTrue)
790+{
791+ na::AnimateValue<int> animation;
792+ StartOrReverseIf(animation, true);
793+
794+ EXPECT_EQ(0, animation.GetStartValue());
795+ EXPECT_EQ(100, animation.GetFinishValue());
796+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
797+}
798+
799+TEST(TestAnimationUtils, StartOrReverseIfIntFalse)
800+{
801+ na::AnimateValue<int> animation;
802+ StartOrReverseIf(animation, false);
803+
804+ EXPECT_EQ(100, animation.GetStartValue());
805+ EXPECT_EQ(0, animation.GetFinishValue());
806+ EXPECT_EQ(na::Animation::State::Running, animation.CurrentState());
807+}
808+
809+TEST(TestAnimationUtils, StartOrReverseExchanges)
810+{
811+ na::AnimateValue<double> animation;
812+
813+ for (int i = 0; i < 10; ++i)
814+ {
815+ StartOrReverse(animation, (i % 2) ? Direction::FORWARD : Direction::BACKWARD);
816+ ASSERT_DOUBLE_EQ((i % 2) ? 0.0f : 1.0f, animation.GetStartValue());
817+ ASSERT_DOUBLE_EQ((i % 2) ? 1.0f : 0.0f, animation.GetFinishValue());
818+ ASSERT_EQ(na::Animation::State::Running, animation.CurrentState());
819+ }
820+}
821+
822+TEST(TestAnimationUtils, StartOrReverseIfExchanges)
823+{
824+ na::AnimateValue<double> animation;
825+
826+ for (int i = 0; i < 10; ++i)
827+ {
828+ StartOrReverseIf(animation, (i % 2));
829+ ASSERT_DOUBLE_EQ((i % 2) ? 0.0f : 1.0f, animation.GetStartValue());
830+ ASSERT_DOUBLE_EQ((i % 2) ? 1.0f : 0.0f, animation.GetFinishValue());
831+ ASSERT_EQ(na::Animation::State::Running, animation.CurrentState());
832+ }
833+}
834+
835+TEST(TestAnimationUtils, GetDirection)
836+{
837+ na::AnimateValue<double> animation;
838+ EXPECT_EQ(Direction::FORWARD, GetDirection(animation));
839+}
840+
841+TEST(TestAnimationUtils, GetDirectionForward)
842+{
843+ na::AnimateValue<double> animation;
844+ animation.SetStartValue(0).SetFinishValue(10);
845+ EXPECT_EQ(Direction::FORWARD, GetDirection(animation));
846+}
847+
848+TEST(TestAnimationUtils, GetDirectionBackward)
849+{
850+ na::AnimateValue<double> animation;
851+ animation.SetStartValue(10).SetFinishValue(0);
852+ EXPECT_EQ(Direction::BACKWARD, GetDirection(animation));
853+}
854+
855+TEST(TestAnimationUtils, GetDirectionStartedReversed)
856+{
857+ na::AnimateValue<double> animation;
858+ StartOrReverse(animation, Direction::BACKWARD);
859+ EXPECT_EQ(Direction::BACKWARD, GetDirection(animation));
860+
861+ StartOrReverse(animation, Direction::FORWARD);
862+ EXPECT_EQ(Direction::FORWARD, GetDirection(animation));
863+}
864+
865+} // Namespace
866
867=== added file 'unity-shared/AnimationUtils-inl.h'
868--- unity-shared/AnimationUtils-inl.h 1970-01-01 00:00:00 +0000
869+++ unity-shared/AnimationUtils-inl.h 2013-09-19 13:00:24 +0000
870@@ -0,0 +1,52 @@
871+// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
872+/*
873+ * Copyright (C) 2013 Canonical Ltd
874+ *
875+ * This program is free software: you can redistribute it and/or modify
876+ * it under the terms of the GNU General Public License version 3 as
877+ * published by the Free Software Foundation.
878+ *
879+ * This program is distributed in the hope that it will be useful,
880+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
881+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
882+ * GNU General Public License for more details.
883+ *
884+ * You should have received a copy of the GNU General Public License
885+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
886+ *
887+ * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
888+ */
889+
890+#ifndef UNITY_ANIMATION_UTILS_INL
891+#define UNITY_ANIMATION_UTILS_INL
892+
893+namespace unity
894+{
895+namespace animation
896+{
897+
898+template <class VALUE_TYPE>
899+void StartOrReverse(na::AnimateValue<VALUE_TYPE>& animation, VALUE_TYPE start, VALUE_TYPE finish)
900+{
901+ if (animation.CurrentState() == na::Animation::State::Running)
902+ {
903+ if (animation.GetStartValue() == finish && animation.GetFinishValue() == start)
904+ {
905+ animation.Reverse();
906+ }
907+ else if (animation.GetStartValue() != start || animation.GetFinishValue() != finish)
908+ {
909+ animation.Stop();
910+ animation.SetStartValue(start).SetFinishValue(finish).Start();
911+ }
912+ }
913+ else
914+ {
915+ animation.SetStartValue(start).SetFinishValue(finish).Start();
916+ }
917+}
918+
919+} // animation namespace
920+} // unity namespace
921+
922+#endif // UNITY_ANIMATION_UTILS_INL
923
924=== added file 'unity-shared/AnimationUtils.h'
925--- unity-shared/AnimationUtils.h 1970-01-01 00:00:00 +0000
926+++ unity-shared/AnimationUtils.h 2013-09-19 13:00:24 +0000
927@@ -0,0 +1,76 @@
928+// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
929+/*
930+ * Copyright (C) 2013 Canonical Ltd
931+ *
932+ * This program is free software: you can redistribute it and/or modify
933+ * it under the terms of the GNU General Public License version 3 as
934+ * published by the Free Software Foundation.
935+ *
936+ * This program is distributed in the hope that it will be useful,
937+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
938+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
939+ * GNU General Public License for more details.
940+ *
941+ * You should have received a copy of the GNU General Public License
942+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
943+ *
944+ * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
945+ */
946+
947+#ifndef UNITY_ANIMATION_UTILS
948+#define UNITY_ANIMATION_UTILS
949+
950+#include <NuxCore/Animation.h>
951+
952+namespace na = nux::animation;
953+
954+namespace unity
955+{
956+namespace animation
957+{
958+
959+enum class Direction
960+{
961+ FORWARD,
962+ BACKWARD
963+};
964+
965+template <typename VALUE>
966+inline Direction GetDirection(na::AnimateValue<VALUE> const& animation)
967+{
968+ if (animation.GetFinishValue() < animation.GetStartValue())
969+ return Direction::BACKWARD;
970+
971+ return Direction::FORWARD;
972+}
973+
974+template <class VALUE_TYPE>
975+void StartOrReverse(na::AnimateValue<VALUE_TYPE>& animation, VALUE_TYPE start, VALUE_TYPE finish);
976+
977+template <typename VALUE>
978+inline void StartOrReverse(na::AnimateValue<VALUE>& animation, Direction dir)
979+{
980+ StartOrReverse<VALUE>(animation, (dir == Direction::FORWARD) ? 0.0f : 1.0f,
981+ (dir == Direction::FORWARD) ? 1.0f : 0.0f);
982+}
983+
984+template <>
985+inline void StartOrReverse(na::AnimateValue<int>& animation, Direction dir)
986+{
987+ StartOrReverse<int>(animation, (dir == Direction::FORWARD) ? 0 : 100,
988+ (dir == Direction::FORWARD) ? 100 : 0);
989+}
990+
991+template <typename VALUE>
992+inline void StartOrReverseIf(na::AnimateValue<VALUE>& animation, bool condition)
993+{
994+ StartOrReverse(animation, condition ? Direction::FORWARD : Direction::BACKWARD);
995+}
996+
997+} // animation namespace
998+} // unity namespace
999+
1000+#include "AnimationUtils-inl.h"
1001+
1002+#endif // UNITY_ANIMATION_UTILS
1003+