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