OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/slide_out_view.h" | 5 #include "ui/views/controls/slide_out_view.h" |
6 | 6 |
7 #include "ui/compositor/layer.h" | 7 #include "ui/compositor/layer.h" |
8 #include "ui/compositor/scoped_layer_animation_settings.h" | 8 #include "ui/compositor/scoped_layer_animation_settings.h" |
9 #include "ui/gfx/transform.h" | 9 #include "ui/gfx/transform.h" |
10 | 10 |
11 namespace views { | 11 namespace views { |
12 | 12 |
13 SlideOutView::SlideOutView() | 13 bool SlideOutView::disable_animations_ = false; |
14 : gesture_scroll_amount_(0.f) { | 14 |
| 15 void SlideOutView::DisableAnimationsForTesting() { |
| 16 disable_animations_ = true; |
| 17 } |
| 18 |
| 19 SlideOutView::SlideOutView() { |
15 // If accelerated compositing is not available, this widget tracks the | 20 // If accelerated compositing is not available, this widget tracks the |
16 // OnSlideOut event but does not render any visible changes. | 21 // OnSlideOut event but does not render any visible changes. |
17 SetPaintToLayer(true); | 22 SetPaintToLayer(true); |
18 SetFillsBoundsOpaquely(false); | 23 SetFillsBoundsOpaquely(false); |
19 } | 24 } |
20 | 25 |
21 SlideOutView::~SlideOutView() { | 26 SlideOutView::~SlideOutView() { |
22 } | 27 } |
23 | 28 |
24 void SlideOutView::OnGestureEvent(ui::GestureEvent* event) { | 29 void SlideOutView::OnGestureEvent(ui::GestureEvent* event) { |
| 30 const float kScrollRatioForClosingNotification = 0.5f; |
| 31 |
25 if (event->type() == ui::ET_SCROLL_FLING_START) { | 32 if (event->type() == ui::ET_SCROLL_FLING_START) { |
26 // The threshold for the fling velocity is computed empirically. | 33 // The threshold for the fling velocity is computed empirically. |
27 // The unit is in pixels/second. | 34 // The unit is in pixels/second. |
28 const float kFlingThresholdForClose = 800.f; | 35 const float kFlingThresholdForClose = 800.f; |
29 if (fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { | 36 if (closable_ && |
| 37 fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { |
30 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : | 38 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : |
31 SLIDE_RIGHT); | 39 SLIDE_RIGHT); |
32 event->StopPropagation(); | 40 event->StopPropagation(); |
33 return; | 41 return; |
34 } | 42 } |
35 RestoreVisualState(); | 43 RestoreVisualState(); |
36 return; | 44 return; |
37 } | 45 } |
38 | 46 |
39 if (!event->IsScrollGestureEvent()) | 47 if (!event->IsScrollGestureEvent()) |
40 return; | 48 return; |
41 | 49 |
42 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | 50 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { |
43 gesture_scroll_amount_ = 0.f; | 51 gesture_amount_ = 0.f; |
44 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | 52 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
45 // The scroll-update events include the incremental scroll amount. | 53 // The scroll-update events include the incremental scroll amount. |
46 gesture_scroll_amount_ += event->details().scroll_x(); | 54 gesture_amount_ += event->details().scroll_x(); |
| 55 |
| 56 float scroll_amount; |
| 57 if (closable_) { |
| 58 scroll_amount = gesture_amount_; |
| 59 layer()->SetOpacity( |
| 60 1.f - std::min(fabsf(scroll_amount) / width(), 1.f)); |
| 61 } else { |
| 62 if (gesture_amount_ >= 0) { |
| 63 scroll_amount = std::min(0.5f * gesture_amount_, |
| 64 width() * kScrollRatioForClosingNotification); |
| 65 } else { |
| 66 scroll_amount = std::max(0.5f * gesture_amount_, |
| 67 -1.f * width() * kScrollRatioForClosingNotification); |
| 68 } |
| 69 layer()->SetOpacity(1.f); |
| 70 } |
47 | 71 |
48 gfx::Transform transform; | 72 gfx::Transform transform; |
49 transform.Translate(gesture_scroll_amount_, 0.0); | 73 transform.Translate(scroll_amount, 0.0); |
50 layer()->SetTransform(transform); | 74 layer()->SetTransform(transform); |
51 layer()->SetOpacity( | |
52 1.f - std::min(fabsf(gesture_scroll_amount_) / width(), 1.f)); | |
53 | 75 |
54 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | 76 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { |
55 const float kScrollRatioForClosingNotification = 0.5f; | 77 float scrolled_ratio = fabsf(gesture_amount_) / width(); |
56 float scrolled_ratio = fabsf(gesture_scroll_amount_) / width(); | 78 if (closable_ && scrolled_ratio >= kScrollRatioForClosingNotification) { |
57 if (scrolled_ratio >= kScrollRatioForClosingNotification) { | 79 SlideOutAndClose(gesture_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); |
58 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | |
59 event->StopPropagation(); | 80 event->StopPropagation(); |
60 return; | 81 return; |
61 } | 82 } |
62 RestoreVisualState(); | 83 RestoreVisualState(); |
63 } | 84 } |
64 | 85 |
65 event->SetHandled(); | 86 event->SetHandled(); |
66 } | 87 } |
67 | 88 |
68 void SlideOutView::RestoreVisualState() { | 89 void SlideOutView::RestoreVisualState() { |
69 // Restore the layer state. | 90 // Restore the layer state. |
70 const int kSwipeRestoreDurationMS = 150; | 91 const int kSwipeRestoreDurationMS = 150; |
71 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 92 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
72 settings.SetTransitionDuration( | 93 settings.SetTransitionDuration( |
73 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 94 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
74 layer()->SetTransform(gfx::Transform()); | 95 layer()->SetTransform(gfx::Transform()); |
75 layer()->SetOpacity(1.f); | 96 layer()->SetOpacity(1.f); |
76 } | 97 } |
77 | 98 |
78 void SlideOutView::SlideOutAndClose(SlideDirection direction) { | 99 void SlideOutView::SlideOutAndClose(SlideDirection direction) { |
79 const int kSwipeOutTotalDurationMS = 150; | 100 const int kSwipeOutTotalDurationMS = 150; |
80 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); | 101 if (!disable_animations_) { |
81 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 102 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); |
82 settings.SetTransitionDuration( | 103 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
83 base::TimeDelta::FromMilliseconds(swipe_out_duration)); | 104 settings.SetTransitionDuration( |
84 settings.AddObserver(this); | 105 base::TimeDelta::FromMilliseconds(swipe_out_duration)); |
| 106 settings.AddObserver(this); |
| 107 } |
85 | 108 |
86 gfx::Transform transform; | 109 gfx::Transform transform; |
87 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); | 110 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); |
88 layer()->SetTransform(transform); | 111 layer()->SetTransform(transform); |
89 layer()->SetOpacity(0.f); | 112 layer()->SetOpacity(0.f); |
| 113 |
| 114 if (disable_animations_) |
| 115 OnImplicitAnimationsCompleted(); |
90 } | 116 } |
91 | 117 |
92 void SlideOutView::OnImplicitAnimationsCompleted() { | 118 void SlideOutView::OnImplicitAnimationsCompleted() { |
93 OnSlideOut(); | 119 OnSlideOut(); |
94 } | 120 } |
95 | 121 |
96 } // namespace views | 122 } // namespace views |
OLD | NEW |