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 SetPaintToLayer(true); |
16 SetFillsBoundsOpaquely(false); | 16 SetFillsBoundsOpaquely(false); |
17 } | 17 } |
18 | 18 |
19 SlideOutView::~SlideOutView() { | 19 SlideOutView::~SlideOutView() { |
20 } | 20 } |
21 | 21 |
22 ui::EventResult SlideOutView::OnGestureEvent(ui::GestureEvent* event) { | 22 void SlideOutView::OnGestureEvent(ui::GestureEvent* event) { |
23 if (event->type() == ui::ET_SCROLL_FLING_START) { | 23 if (event->type() == ui::ET_SCROLL_FLING_START) { |
24 // The threshold for the fling velocity is computed empirically. | 24 // The threshold for the fling velocity is computed empirically. |
25 // The unit is in pixels/second. | 25 // The unit is in pixels/second. |
26 const float kFlingThresholdForClose = 800.f; | 26 const float kFlingThresholdForClose = 800.f; |
27 if (fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { | 27 if (fabsf(event->details().velocity_x()) > kFlingThresholdForClose) { |
28 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : | 28 SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT : |
29 SLIDE_RIGHT); | 29 SLIDE_RIGHT); |
30 return ui::ER_CONSUMED; | 30 event->StopPropagation(); |
| 31 return; |
31 } | 32 } |
32 RestoreVisualState(); | 33 RestoreVisualState(); |
33 return ui::ER_UNHANDLED; | 34 return; |
34 } | 35 } |
35 | 36 |
36 if (!event->IsScrollGestureEvent()) | 37 if (!event->IsScrollGestureEvent()) |
37 return ui::ER_UNHANDLED; | 38 return; |
38 | 39 |
39 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | 40 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { |
40 gesture_scroll_amount_ = 0.f; | 41 gesture_scroll_amount_ = 0.f; |
41 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | 42 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
42 // The scroll-update events include the incremental scroll amount. | 43 // The scroll-update events include the incremental scroll amount. |
43 gesture_scroll_amount_ += event->details().scroll_x(); | 44 gesture_scroll_amount_ += event->details().scroll_x(); |
44 | 45 |
45 gfx::Transform transform; | 46 gfx::Transform transform; |
46 transform.Translate(gesture_scroll_amount_, 0.0); | 47 transform.Translate(gesture_scroll_amount_, 0.0); |
47 layer()->SetTransform(transform); | 48 layer()->SetTransform(transform); |
48 layer()->SetOpacity( | 49 layer()->SetOpacity( |
49 1.f - std::min(fabsf(gesture_scroll_amount_) / width(), 1.f)); | 50 1.f - std::min(fabsf(gesture_scroll_amount_) / width(), 1.f)); |
50 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | 51 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { |
51 const float kScrollRatioForClosingNotification = 0.5f; | 52 const float kScrollRatioForClosingNotification = 0.5f; |
52 float scrolled_ratio = fabsf(gesture_scroll_amount_) / width(); | 53 float scrolled_ratio = fabsf(gesture_scroll_amount_) / width(); |
53 if (scrolled_ratio >= kScrollRatioForClosingNotification) { | 54 if (scrolled_ratio >= kScrollRatioForClosingNotification) { |
54 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | 55 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); |
55 return ui::ER_CONSUMED; | 56 event->StopPropagation(); |
| 57 return; |
56 } | 58 } |
57 RestoreVisualState(); | 59 RestoreVisualState(); |
58 } | 60 } |
59 | 61 |
60 return ui::ER_HANDLED; | 62 event->SetHandled(); |
61 } | 63 } |
62 | 64 |
63 void SlideOutView::RestoreVisualState() { | 65 void SlideOutView::RestoreVisualState() { |
64 // Restore the layer state. | 66 // Restore the layer state. |
65 const int kSwipeRestoreDurationMS = 150; | 67 const int kSwipeRestoreDurationMS = 150; |
66 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 68 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
67 settings.SetTransitionDuration( | 69 settings.SetTransitionDuration( |
68 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 70 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
69 layer()->SetTransform(gfx::Transform()); | 71 layer()->SetTransform(gfx::Transform()); |
70 layer()->SetOpacity(1.f); | 72 layer()->SetOpacity(1.f); |
(...skipping 11 matching lines...) Expand all Loading... |
82 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); | 84 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); |
83 layer()->SetTransform(transform); | 85 layer()->SetTransform(transform); |
84 layer()->SetOpacity(0.f); | 86 layer()->SetOpacity(0.f); |
85 } | 87 } |
86 | 88 |
87 void SlideOutView::OnImplicitAnimationsCompleted() { | 89 void SlideOutView::OnImplicitAnimationsCompleted() { |
88 OnSlideOut(); | 90 OnSlideOut(); |
89 } | 91 } |
90 | 92 |
91 } // namespace views | 93 } // namespace views |
OLD | NEW |