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/message_center/views/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()); |
33 SLIDE_RIGHT); | |
34 event->StopPropagation(); | 29 event->StopPropagation(); |
35 return; | 30 return; |
36 } | 31 } |
37 RestoreVisualState(); | 32 RestoreVisualState(); |
38 return; | 33 return; |
39 } | 34 } |
40 | 35 |
41 if (!event->IsScrollGestureEvent()) | 36 if (!event->IsScrollGestureEvent()) |
42 return; | 37 return; |
43 | 38 |
| 39 ui::Layer* layer = delegate_->GetSlideOutLayer(); |
| 40 int width = layer->bounds().width(); |
44 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { | 41 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) { |
45 gesture_amount_ = 0.f; | 42 gesture_amount_ = 0.f; |
46 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { | 43 } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { |
47 // The scroll-update events include the incremental scroll amount. | 44 // The scroll-update events include the incremental scroll amount. |
48 gesture_amount_ += event->details().scroll_x(); | 45 gesture_amount_ += event->details().scroll_x(); |
49 | 46 |
50 float scroll_amount; | 47 float scroll_amount; |
51 if (is_slide_out_enabled_) { | 48 if (enabled_) { |
52 scroll_amount = gesture_amount_; | 49 scroll_amount = gesture_amount_; |
53 layer()->SetOpacity(1.f - std::min(fabsf(scroll_amount) / width(), 1.f)); | 50 layer->SetOpacity(1.f - std::min(fabsf(scroll_amount) / width, 1.f)); |
54 } else { | 51 } else { |
55 if (gesture_amount_ >= 0) { | 52 if (gesture_amount_ >= 0) { |
56 scroll_amount = std::min(0.5f * gesture_amount_, | 53 scroll_amount = std::min(0.5f * gesture_amount_, |
57 width() * kScrollRatioForClosingNotification); | 54 width * kScrollRatioForClosingNotification); |
58 } else { | 55 } else { |
59 scroll_amount = | 56 scroll_amount = |
60 std::max(0.5f * gesture_amount_, | 57 std::max(0.5f * gesture_amount_, |
61 -1.f * width() * kScrollRatioForClosingNotification); | 58 -1.f * width * kScrollRatioForClosingNotification); |
62 } | 59 } |
63 } | 60 } |
64 | 61 |
65 gfx::Transform transform; | 62 gfx::Transform transform; |
66 transform.Translate(scroll_amount, 0.0); | 63 transform.Translate(scroll_amount, 0.0); |
67 layer()->SetTransform(transform); | 64 layer->SetTransform(transform); |
68 | |
69 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { | 65 } else if (event->type() == ui::ET_GESTURE_SCROLL_END) { |
70 float scrolled_ratio = fabsf(gesture_amount_) / width(); | 66 float scrolled_ratio = fabsf(gesture_amount_) / width; |
71 if (is_slide_out_enabled_ && | 67 if (enabled_ && scrolled_ratio >= kScrollRatioForClosingNotification) { |
72 scrolled_ratio >= kScrollRatioForClosingNotification) { | 68 SlideOutAndClose(gesture_amount_); |
73 SlideOutAndClose(gesture_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | |
74 event->StopPropagation(); | 69 event->StopPropagation(); |
75 return; | 70 return; |
76 } | 71 } |
77 RestoreVisualState(); | 72 RestoreVisualState(); |
78 } | 73 } |
79 | 74 |
80 event->SetHandled(); | 75 event->SetHandled(); |
81 } | 76 } |
82 | 77 |
83 void SlideOutView::RestoreVisualState() { | 78 void SlideOutController::RestoreVisualState() { |
| 79 ui::Layer* layer = delegate_->GetSlideOutLayer(); |
84 // Restore the layer state. | 80 // Restore the layer state. |
85 const int kSwipeRestoreDurationMS = 150; | 81 const int kSwipeRestoreDurationMS = 150; |
86 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 82 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
87 settings.SetTransitionDuration( | 83 settings.SetTransitionDuration( |
88 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 84 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
89 layer()->SetTransform(gfx::Transform()); | 85 layer->SetTransform(gfx::Transform()); |
90 layer()->SetOpacity(1.f); | 86 layer->SetOpacity(1.f); |
91 } | 87 } |
92 | 88 |
93 void SlideOutView::SlideOutAndClose(SlideDirection direction) { | 89 void SlideOutController::SlideOutAndClose(int direction) { |
| 90 ui::Layer* layer = delegate_->GetSlideOutLayer(); |
94 const int kSwipeOutTotalDurationMS = 150; | 91 const int kSwipeOutTotalDurationMS = 150; |
95 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); | 92 int swipe_out_duration = kSwipeOutTotalDurationMS * layer->opacity(); |
96 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 93 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
97 settings.SetTransitionDuration( | 94 settings.SetTransitionDuration( |
98 base::TimeDelta::FromMilliseconds(swipe_out_duration)); | 95 base::TimeDelta::FromMilliseconds(swipe_out_duration)); |
99 settings.AddObserver(this); | 96 settings.AddObserver(this); |
100 | 97 |
101 gfx::Transform transform; | 98 gfx::Transform transform; |
102 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); | 99 int width = layer->bounds().width(); |
103 layer()->SetTransform(transform); | 100 transform.Translate(direction < 0 ? -width : width, 0.0); |
104 layer()->SetOpacity(0.f); | 101 layer->SetTransform(transform); |
| 102 layer->SetOpacity(0.f); |
105 } | 103 } |
106 | 104 |
107 void SlideOutView::OnImplicitAnimationsCompleted() { | 105 void SlideOutController::OnImplicitAnimationsCompleted() { |
108 OnSlideOut(); | 106 delegate_->OnSlideOut(); |
109 } | 107 } |
110 | 108 |
111 } // namespace views | 109 } // namespace views |
OLD | NEW |