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; |
sky
2016/02/16 16:13:06
// static
| |
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 (slide_out_enabled_ && |
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 (slide_out_enabled_) { | |
58 scroll_amount = gesture_amount_; | |
59 layer()->SetOpacity(1.f - std::min(fabsf(scroll_amount) / width(), 1.f)); | |
60 } else { | |
sadrul
2016/02/17 17:29:26
Define kScrollRatioForClosingNotification here.
yoshiki
2016/02/22 13:39:27
We can't. kScrollRatioForClosingNotification is us
| |
61 if (gesture_amount_ >= 0) { | |
62 scroll_amount = std::min(0.5f * gesture_amount_, | |
63 width() * kScrollRatioForClosingNotification); | |
64 } else { | |
65 scroll_amount = | |
66 std::max(0.5f * gesture_amount_, | |
67 -1.f * width() * kScrollRatioForClosingNotification); | |
68 } | |
69 layer()->SetOpacity(1.f); | |
sadrul
2016/02/17 17:29:26
Do you need to SetOpacity() here at all?
yoshiki
2016/02/22 13:39:27
Removed
| |
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 (slide_out_enabled_ && |
57 if (scrolled_ratio >= kScrollRatioForClosingNotification) { | 79 scrolled_ratio >= kScrollRatioForClosingNotification) { |
58 SlideOutAndClose(gesture_scroll_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); | 80 SlideOutAndClose(gesture_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT); |
59 event->StopPropagation(); | 81 event->StopPropagation(); |
60 return; | 82 return; |
61 } | 83 } |
62 RestoreVisualState(); | 84 RestoreVisualState(); |
63 } | 85 } |
64 | 86 |
65 event->SetHandled(); | 87 event->SetHandled(); |
66 } | 88 } |
67 | 89 |
68 void SlideOutView::RestoreVisualState() { | 90 void SlideOutView::RestoreVisualState() { |
69 // Restore the layer state. | 91 // Restore the layer state. |
70 const int kSwipeRestoreDurationMS = 150; | 92 const int kSwipeRestoreDurationMS = 150; |
71 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 93 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
72 settings.SetTransitionDuration( | 94 settings.SetTransitionDuration( |
73 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); | 95 base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS)); |
74 layer()->SetTransform(gfx::Transform()); | 96 layer()->SetTransform(gfx::Transform()); |
75 layer()->SetOpacity(1.f); | 97 layer()->SetOpacity(1.f); |
76 } | 98 } |
77 | 99 |
78 void SlideOutView::SlideOutAndClose(SlideDirection direction) { | 100 void SlideOutView::SlideOutAndClose(SlideDirection direction) { |
79 const int kSwipeOutTotalDurationMS = 150; | 101 const int kSwipeOutTotalDurationMS = 150; |
80 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); | 102 if (!disable_animations_) { |
81 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | 103 int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity(); |
82 settings.SetTransitionDuration( | 104 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); |
83 base::TimeDelta::FromMilliseconds(swipe_out_duration)); | 105 settings.SetTransitionDuration( |
84 settings.AddObserver(this); | 106 base::TimeDelta::FromMilliseconds(swipe_out_duration)); |
107 settings.AddObserver(this); | |
108 } | |
85 | 109 |
86 gfx::Transform transform; | 110 gfx::Transform transform; |
87 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); | 111 transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0); |
88 layer()->SetTransform(transform); | 112 layer()->SetTransform(transform); |
89 layer()->SetOpacity(0.f); | 113 layer()->SetOpacity(0.f); |
114 | |
115 if (disable_animations_) | |
116 OnImplicitAnimationsCompleted(); | |
sadrul
2016/02/17 17:29:26
Can you use ScopedAnimationDurationScaleMode with
yoshiki
2016/02/22 13:39:27
I didn't know that. I can remove the flag with Sco
| |
90 } | 117 } |
91 | 118 |
92 void SlideOutView::OnImplicitAnimationsCompleted() { | 119 void SlideOutView::OnImplicitAnimationsCompleted() { |
93 OnSlideOut(); | 120 OnSlideOut(); |
94 } | 121 } |
95 | 122 |
96 } // namespace views | 123 } // namespace views |
OLD | NEW |