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/gfx/image/image_png_rep.h"
17 OverscrollWindowDelegate::OverscrollWindowDelegate(
18 OverscrollControllerDelegate
* delegate
,
19 const gfx::Image
& image
)
20 : delegate_(delegate
),
21 overscroll_mode_(OVERSCROLL_NONE
),
23 complete_threshold_ratio_(content::GetOverscrollConfig(
24 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE
)),
25 start_threshold_touchscreen_(content::GetOverscrollConfig(
26 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN
)),
27 start_threshold_touchpad_(content::GetOverscrollConfig(
28 content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD
)),
29 active_start_threshold_(0.f
) {
33 OverscrollWindowDelegate::~OverscrollWindowDelegate() {
36 void OverscrollWindowDelegate::StartOverscroll() {
37 OverscrollMode old_mode
= overscroll_mode_
;
39 overscroll_mode_
= OVERSCROLL_EAST
;
41 overscroll_mode_
= OVERSCROLL_WEST
;
42 delegate_
->OnOverscrollModeChange(old_mode
, overscroll_mode_
);
45 void OverscrollWindowDelegate::ResetOverscroll() {
46 delegate_
->OnOverscrollModeChange(overscroll_mode_
, OVERSCROLL_NONE
);
47 overscroll_mode_
= OVERSCROLL_NONE
;
51 void OverscrollWindowDelegate::CompleteOrResetOverscroll() {
52 if (overscroll_mode_
== OVERSCROLL_NONE
)
54 int width
= delegate_
->GetVisibleBounds().width();
55 float ratio
= (fabs(delta_x_
)) / width
;
56 if (ratio
< complete_threshold_ratio_
) {
60 delegate_
->OnOverscrollComplete(overscroll_mode_
);
61 overscroll_mode_
= OVERSCROLL_NONE
;
65 void OverscrollWindowDelegate::UpdateOverscroll(float delta_x
) {
66 float old_delta_x
= delta_x_
;
68 if (overscroll_mode_
== OVERSCROLL_NONE
) {
69 if (fabs(delta_x_
) > active_start_threshold_
)
73 if ((old_delta_x
< 0 && delta_x_
> 0) || (old_delta_x
> 0 && delta_x_
< 0)) {
77 delegate_
->OnOverscrollUpdate(delta_x_
, 0.f
);
80 void OverscrollWindowDelegate::OnKeyEvent(ui::KeyEvent
* event
) {
84 void OverscrollWindowDelegate::OnMouseEvent(ui::MouseEvent
* event
) {
85 if (!(event
->flags() & ui::EF_IS_SYNTHESIZED
) &&
86 event
->type() != ui::ET_MOUSE_CAPTURE_CHANGED
) {
91 void OverscrollWindowDelegate::OnScrollEvent(ui::ScrollEvent
* event
) {
92 active_start_threshold_
= start_threshold_touchpad_
;
93 if (event
->type() == ui::ET_SCROLL
)
94 UpdateOverscroll(event
->x_offset_ordinal());
95 else if (event
->type() == ui::ET_SCROLL_FLING_START
)
96 CompleteOrResetOverscroll();
102 void OverscrollWindowDelegate::OnGestureEvent(ui::GestureEvent
* event
) {
103 active_start_threshold_
= start_threshold_touchscreen_
;
104 switch (event
->type()) {
105 case ui::ET_GESTURE_SCROLL_UPDATE
:
106 UpdateOverscroll(event
->details().scroll_x());
109 case ui::ET_GESTURE_SCROLL_END
:
110 CompleteOrResetOverscroll();
113 case ui::ET_SCROLL_FLING_START
:
114 CompleteOrResetOverscroll();
117 case ui::ET_GESTURE_PINCH_BEGIN
:
118 case ui::ET_GESTURE_PINCH_UPDATE
:
119 case ui::ET_GESTURE_PINCH_END
:
129 } // namespace content