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