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