Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / web_contents / aura / overscroll_window_delegate.cc
blob76aa5836f433a1ee89ee003110ce4d7eb7f92464
1 // Copyright 2015 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.
5 #include "content/browser/web_contents/aura/overscroll_window_delegate.h"
7 #include "base/i18n/rtl.h"
8 #include "content/browser/frame_host/navigation_controller_impl.h"
9 #include "content/browser/frame_host/navigation_entry_impl.h"
10 #include "content/browser/renderer_host/overscroll_controller_delegate.h"
11 #include "content/public/browser/overscroll_configuration.h"
12 #include "ui/aura/window.h"
13 #include "ui/events/event.h"
14 #include "ui/gfx/image/image_png_rep.h"
16 namespace content {
18 OverscrollWindowDelegate::OverscrollWindowDelegate(
19 OverscrollControllerDelegate* delegate,
20 const gfx::Image& image)
21 : delegate_(delegate),
22 overscroll_mode_(OVERSCROLL_NONE),
23 delta_x_(0.f),
24 complete_threshold_ratio_(content::GetOverscrollConfig(
25 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE)),
26 start_threshold_touchscreen_(content::GetOverscrollConfig(
27 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN)),
28 start_threshold_touchpad_(content::GetOverscrollConfig(
29 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD)),
30 active_start_threshold_(0.f) {
31 SetImage(image);
34 OverscrollWindowDelegate::~OverscrollWindowDelegate() {
37 void OverscrollWindowDelegate::StartOverscroll() {
38 OverscrollMode old_mode = overscroll_mode_;
39 if (delta_x_ > 0)
40 overscroll_mode_ = OVERSCROLL_EAST;
41 else
42 overscroll_mode_ = OVERSCROLL_WEST;
43 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_);
46 void OverscrollWindowDelegate::ResetOverscroll() {
47 if (overscroll_mode_ == OVERSCROLL_NONE)
48 return;
49 delegate_->OnOverscrollModeChange(overscroll_mode_, OVERSCROLL_NONE);
50 overscroll_mode_ = OVERSCROLL_NONE;
51 delta_x_ = 0;
54 void OverscrollWindowDelegate::CompleteOrResetOverscroll() {
55 if (overscroll_mode_ == OVERSCROLL_NONE)
56 return;
57 int width = delegate_->GetVisibleBounds().width();
58 float ratio = (fabs(delta_x_)) / width;
59 if (ratio < complete_threshold_ratio_) {
60 ResetOverscroll();
61 return;
63 delegate_->OnOverscrollComplete(overscroll_mode_);
64 overscroll_mode_ = OVERSCROLL_NONE;
65 delta_x_ = 0;
68 void OverscrollWindowDelegate::UpdateOverscroll(float delta_x) {
69 float old_delta_x = delta_x_;
70 delta_x_ += delta_x;
71 if (overscroll_mode_ == OVERSCROLL_NONE) {
72 if (fabs(delta_x_) > active_start_threshold_)
73 StartOverscroll();
74 return;
76 if ((old_delta_x < 0 && delta_x_ > 0) || (old_delta_x > 0 && delta_x_ < 0)) {
77 ResetOverscroll();
78 return;
80 delegate_->OnOverscrollUpdate(delta_x_, 0.f);
83 void OverscrollWindowDelegate::OnKeyEvent(ui::KeyEvent* event) {
84 ResetOverscroll();
87 void OverscrollWindowDelegate::OnMouseEvent(ui::MouseEvent* event) {
88 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
89 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
90 ResetOverscroll();
94 void OverscrollWindowDelegate::OnScrollEvent(ui::ScrollEvent* event) {
95 active_start_threshold_ = start_threshold_touchpad_;
96 if (event->type() == ui::ET_SCROLL)
97 UpdateOverscroll(event->x_offset_ordinal());
98 else if (event->type() == ui::ET_SCROLL_FLING_START)
99 CompleteOrResetOverscroll();
100 else
101 ResetOverscroll();
102 event->SetHandled();
105 void OverscrollWindowDelegate::OnGestureEvent(ui::GestureEvent* event) {
106 active_start_threshold_ = start_threshold_touchscreen_;
107 switch (event->type()) {
108 case ui::ET_GESTURE_SCROLL_UPDATE:
109 UpdateOverscroll(event->details().scroll_x());
110 break;
112 case ui::ET_GESTURE_SCROLL_END:
113 CompleteOrResetOverscroll();
114 break;
116 case ui::ET_SCROLL_FLING_START:
117 CompleteOrResetOverscroll();
118 break;
120 case ui::ET_GESTURE_PINCH_BEGIN:
121 case ui::ET_GESTURE_PINCH_UPDATE:
122 case ui::ET_GESTURE_PINCH_END:
123 ResetOverscroll();
124 break;
126 default:
127 break;
129 event->SetHandled();
132 } // namespace content