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