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"
18 OverscrollWindowDelegate::OverscrollWindowDelegate(
19 OverscrollControllerDelegate
* delegate
,
20 const gfx::Image
& image
)
21 : delegate_(delegate
),
22 overscroll_mode_(OVERSCROLL_NONE
),
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
) {
34 OverscrollWindowDelegate::~OverscrollWindowDelegate() {
37 void OverscrollWindowDelegate::StartOverscroll() {
38 OverscrollMode old_mode
= overscroll_mode_
;
40 overscroll_mode_
= OVERSCROLL_EAST
;
42 overscroll_mode_
= OVERSCROLL_WEST
;
43 delegate_
->OnOverscrollModeChange(old_mode
, overscroll_mode_
);
46 void OverscrollWindowDelegate::ResetOverscroll() {
47 if (overscroll_mode_
== OVERSCROLL_NONE
)
49 delegate_
->OnOverscrollModeChange(overscroll_mode_
, OVERSCROLL_NONE
);
50 overscroll_mode_
= OVERSCROLL_NONE
;
54 void OverscrollWindowDelegate::CompleteOrResetOverscroll() {
55 if (overscroll_mode_
== OVERSCROLL_NONE
)
57 int width
= delegate_
->GetVisibleBounds().width();
58 float ratio
= (fabs(delta_x_
)) / width
;
59 if (ratio
< complete_threshold_ratio_
) {
63 delegate_
->OnOverscrollComplete(overscroll_mode_
);
64 overscroll_mode_
= OVERSCROLL_NONE
;
68 void OverscrollWindowDelegate::UpdateOverscroll(float delta_x
) {
69 float old_delta_x
= delta_x_
;
71 if (overscroll_mode_
== OVERSCROLL_NONE
) {
72 if (fabs(delta_x_
) > active_start_threshold_
)
76 if ((old_delta_x
< 0 && delta_x_
> 0) || (old_delta_x
> 0 && delta_x_
< 0)) {
80 delegate_
->OnOverscrollUpdate(delta_x_
, 0.f
);
83 void OverscrollWindowDelegate::OnKeyEvent(ui::KeyEvent
* event
) {
87 void OverscrollWindowDelegate::OnMouseEvent(ui::MouseEvent
* event
) {
88 if (!(event
->flags() & ui::EF_IS_SYNTHESIZED
) &&
89 event
->type() != ui::ET_MOUSE_CAPTURE_CHANGED
) {
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();
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());
112 case ui::ET_GESTURE_SCROLL_END
:
113 CompleteOrResetOverscroll();
116 case ui::ET_SCROLL_FLING_START
:
117 CompleteOrResetOverscroll();
120 case ui::ET_GESTURE_PINCH_BEGIN
:
121 case ui::ET_GESTURE_PINCH_UPDATE
:
122 case ui::ET_GESTURE_PINCH_END
:
132 } // namespace content