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 SlideOutView::SlideOutView() |
14 : gesture_scroll_amount_(0.f) { | 14 : gesture_scroll_amount_(0.f) { |
15 SetPaintToLayer(true); | 15 // If accelerated compositing is not available, this widget tracks the |
16 SetFillsBoundsOpaquely(false); | 16 // OnSlideOut event but does not render any visible changes. |
| 17 if (get_use_acceleration_when_possible()) { |
| 18 SetPaintToLayer(true); |
| 19 SetFillsBoundsOpaquely(false); |
| 20 } |
17 } | 21 } |
18 | 22 |
19 SlideOutView::~SlideOutView() { | 23 SlideOutView::~SlideOutView() { |
20 } | 24 } |
21 | 25 |
22 void SlideOutView::OnGestureEvent(ui::GestureEvent* event) { | 26 void SlideOutView::OnGestureEvent(ui::GestureEvent* event) { |
23 if (event->type() == ui::ET_SCROLL_FLING_START) { | 27 if (event->type() == ui::ET_SCROLL_FLING_START) { |
24 // The threshold for the fling velocity is computed empirically. | 28 // The threshold for the fling velocity is computed empirically. |
25 // The unit is in pixels/second. | 29 // The unit is in pixels/second. |
26 const float kFlingThresholdForClose = 800.f; | 30 const float kFlingThresholdForClose = 800.f; |
27 if (fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { | 31 if (fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { |
28 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : | 32 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : |
29 SLIDE_RIGHT); | 33 SLIDE_RIGHT); |
30 event->StopPropagation(); | 34 event->StopPropagation(); |
31 return; | 35 return; |
32 } | 36 } |
33 RestoreVisualState(); | 37 RestoreVisualState(); |
34 return; | 38 return; |
35 } | 39 } |
36 | 40 |
37 if (!event->IsScrollGestureEvent()) | 41 if (!event->IsScrollGestureEvent()) |
38 return; | 42 return; |
39 | 43 |
40 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | 44 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { |
41 gesture_scroll_amount_ = 0.f; | 45 gesture_scroll_amount_ = 0.f; |
42 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | 46 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
43 // The scroll-update events include the incremental scroll amount. | 47 // The scroll-update events include the incremental scroll amount. |
44 gesture_scroll_amount_ += event->details().scroll_x(); | 48 gesture_scroll_amount_ += event->details().scroll_x(); |
45 | 49 |
46 gfx::Transform transform; | 50 if (get_use_acceleration_when_possible()) { |
47 transform.Translate(gesture_scroll_amount_, 0.0); | 51 gfx::Transform transform; |
48 layer()->SetTransform(transform); | 52 transform.Translate(gesture_scroll_amount_, 0.0); |
49 layer()->SetOpacity( | 53 layer()->SetTransform(transform); |
50 1.f - std::min(fabsf(gesture_scroll_amount_) / width(), 1.f)); | 54 layer()->SetOpacity( |
| 55 1.f - std::min(fabsf(gesture_scroll_amount_) / width(), 1.f)); |
| 56 } |
| 57 |
51 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | 58 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { |
52 const float kScrollRatioForClosingNotification = 0.5f; | 59 const float kScrollRatioForClosingNotification = 0.5f; |
53 float scrolled_ratio = fabsf(gesture_scroll_amount_) / width(); | 60 float scrolled_ratio = fabsf(gesture_scroll_amount_) / width(); |
54 if (scrolled_ratio >= kScrollRatioForClosingNotification) { | 61 if (scrolled_ratio >= kScrollRatioForClosingNotification) { |
55 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | 62 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); |
56 event->StopPropagation(); | 63 event->StopPropagation(); |
57 return; | 64 return; |
58 } | 65 } |
59 RestoreVisualState(); | 66 RestoreVisualState(); |
60 } | 67 } |
61 | 68 |
62 event->SetHandled(); | 69 event->SetHandled(); |
63 } | 70 } |
64 | 71 |
65 void SlideOutView::RestoreVisualState() { | 72 void SlideOutView::RestoreVisualState() { |
| 73 if (!get_use_acceleration_when_possible()) |
| 74 return; |
| 75 |
66 // Restore the layer state. | 76 // Restore the layer state. |
67 const int kSwipeRestoreDurationMS = 150; | 77 const int kSwipeRestoreDurationMS = 150; |
68 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 78 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
69 settings.SetTransitionDuration( | 79 settings.SetTransitionDuration( |
70 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 80 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
71 layer()->SetTransform(gfx::Transform()); | 81 layer()->SetTransform(gfx::Transform()); |
72 layer()->SetOpacity(1.f); | 82 layer()->SetOpacity(1.f); |
73 } | 83 } |
74 | 84 |
75 void SlideOutView::SlideOutAndClose(SlideDirection direction) { | 85 void SlideOutView::SlideOutAndClose(SlideDirection direction) { |
| 86 if (!get_use_acceleration_when_possible()) { |
| 87 // No animations, so don't wait to fire the OnSlideOut event. |
| 88 OnSlideOut(); |
| 89 return; |
| 90 } |
76 const int kSwipeOutTotalDurationMS = 150; | 91 const int kSwipeOutTotalDurationMS = 150; |
77 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); | 92 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); |
78 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 93 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
79 settings.SetTransitionDuration( | 94 settings.SetTransitionDuration( |
80 base::TimeDelta::FromMilliseconds(swipe_out_duration)); | 95 base::TimeDelta::FromMilliseconds(swipe_out_duration)); |
81 settings.AddObserver(this); | 96 settings.AddObserver(this); |
82 | 97 |
83 gfx::Transform transform; | 98 gfx::Transform transform; |
84 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); | 99 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); |
85 layer()->SetTransform(transform); | 100 layer()->SetTransform(transform); |
86 layer()->SetOpacity(0.f); | 101 layer()->SetOpacity(0.f); |
87 } | 102 } |
88 | 103 |
89 void SlideOutView::OnImplicitAnimationsCompleted() { | 104 void SlideOutView::OnImplicitAnimationsCompleted() { |
90 OnSlideOut(); | 105 OnSlideOut(); |
91 } | 106 } |
92 | 107 |
93 } // namespace views | 108 } // namespace views |
OLD | NEW |