summaryrefslogtreecommitdiffstats
path: root/ui/views/controls/slide_out_view.cc
blob: 2775c0889af36b3935a5ad5d26f18c11e4db04cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/slide_out_view.h"

#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/transform.h"

namespace views {

SlideOutView::SlideOutView() {
  // If accelerated compositing is not available, this widget tracks the
  // OnSlideOut event but does not render any visible changes.
  SetPaintToLayer(true);
  layer()->SetFillsBoundsOpaquely(false);
}

SlideOutView::~SlideOutView() {
}

void SlideOutView::OnGestureEvent(ui::GestureEvent* event) {
  const float kScrollRatioForClosingNotification = 0.5f;

  if (event->type() == ui::ET_SCROLL_FLING_START) {
    // The threshold for the fling velocity is computed empirically.
    // The unit is in pixels/second.
    const float kFlingThresholdForClose = 800.f;
    if (is_slide_out_enabled_ &&
        fabsf(event->details().velocity_x()) > kFlingThresholdForClose) {
      SlideOutAndClose(event->details().velocity_x() < 0 ? SLIDE_LEFT :
                       SLIDE_RIGHT);
      event->StopPropagation();
      return;
    }
    RestoreVisualState();
    return;
  }

  if (!event->IsScrollGestureEvent())
    return;

  if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
    gesture_amount_ = 0.f;
  } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
    // The scroll-update events include the incremental scroll amount.
    gesture_amount_ += event->details().scroll_x();

    float scroll_amount;
    if (is_slide_out_enabled_) {
      scroll_amount = gesture_amount_;
      layer()->SetOpacity(1.f - std::min(fabsf(scroll_amount) / width(), 1.f));
    } else {
      if (gesture_amount_ >= 0) {
        scroll_amount = std::min(0.5f * gesture_amount_,
                                 width() * kScrollRatioForClosingNotification);
      } else {
        scroll_amount =
            std::max(0.5f * gesture_amount_,
                     -1.f * width() * kScrollRatioForClosingNotification);
      }
    }

    gfx::Transform transform;
    transform.Translate(scroll_amount, 0.0);
    layer()->SetTransform(transform);

  } else if (event->type() == ui::ET_GESTURE_SCROLL_END) {
    float scrolled_ratio = fabsf(gesture_amount_) / width();
    if (is_slide_out_enabled_ &&
        scrolled_ratio >= kScrollRatioForClosingNotification) {
      SlideOutAndClose(gesture_amount_ < 0 ? SLIDE_LEFT : SLIDE_RIGHT);
      event->StopPropagation();
      return;
    }
    RestoreVisualState();
  }

  event->SetHandled();
}

void SlideOutView::RestoreVisualState() {
  // Restore the layer state.
  const int kSwipeRestoreDurationMS = 150;
  ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
  settings.SetTransitionDuration(
      base::TimeDelta::FromMilliseconds(kSwipeRestoreDurationMS));
  layer()->SetTransform(gfx::Transform());
  layer()->SetOpacity(1.f);
}

void SlideOutView::SlideOutAndClose(SlideDirection direction) {
  const int kSwipeOutTotalDurationMS = 150;
  int swipe_out_duration = kSwipeOutTotalDurationMS * layer()->opacity();
  ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
  settings.SetTransitionDuration(
      base::TimeDelta::FromMilliseconds(swipe_out_duration));
  settings.AddObserver(this);

  gfx::Transform transform;
  transform.Translate(direction == SLIDE_LEFT ? -width() : width(), 0.0);
  layer()->SetTransform(transform);
  layer()->SetOpacity(0.f);
}

void SlideOutView::OnImplicitAnimationsCompleted() {
  OnSlideOut();
}

}  // namespace views