OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/browser/web_contents/aura/overscroll_window_delegate.h" |
| 6 |
| 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" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 OverscrollWindowDelegate::OverscrollWindowDelegate( |
| 18 OverscrollControllerDelegate* delegate, |
| 19 const gfx::Image& image) |
| 20 : delegate_(delegate), |
| 21 overscroll_mode_(OVERSCROLL_NONE), |
| 22 delta_x_(0.f), |
| 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) { |
| 30 SetImage(image); |
| 31 } |
| 32 |
| 33 OverscrollWindowDelegate::~OverscrollWindowDelegate() { |
| 34 } |
| 35 |
| 36 void OverscrollWindowDelegate::StartOverscroll() { |
| 37 OverscrollMode old_mode = overscroll_mode_; |
| 38 if (delta_x_ > 0) |
| 39 overscroll_mode_ = OVERSCROLL_EAST; |
| 40 else |
| 41 overscroll_mode_ = OVERSCROLL_WEST; |
| 42 delegate_->OnOverscrollModeChange(old_mode, overscroll_mode_); |
| 43 } |
| 44 |
| 45 void OverscrollWindowDelegate::ResetOverscroll() { |
| 46 delegate_->OnOverscrollModeChange(overscroll_mode_, OVERSCROLL_NONE); |
| 47 overscroll_mode_ = OVERSCROLL_NONE; |
| 48 delta_x_ = 0; |
| 49 } |
| 50 |
| 51 void OverscrollWindowDelegate::CompleteOrResetOverscroll() { |
| 52 if (overscroll_mode_ == OVERSCROLL_NONE) |
| 53 return; |
| 54 int width = delegate_->GetVisibleBounds().width(); |
| 55 float ratio = (fabs(delta_x_)) / width; |
| 56 if (ratio < complete_threshold_ratio_) { |
| 57 ResetOverscroll(); |
| 58 return; |
| 59 } |
| 60 delegate_->OnOverscrollComplete(overscroll_mode_); |
| 61 overscroll_mode_ = OVERSCROLL_NONE; |
| 62 delta_x_ = 0; |
| 63 } |
| 64 |
| 65 void OverscrollWindowDelegate::UpdateOverscroll(float delta_x) { |
| 66 float old_delta_x = delta_x_; |
| 67 delta_x_ += delta_x; |
| 68 if (overscroll_mode_ == OVERSCROLL_NONE) { |
| 69 if (fabs(delta_x_) > active_start_threshold_) |
| 70 StartOverscroll(); |
| 71 return; |
| 72 } |
| 73 if ((old_delta_x < 0 && delta_x_ > 0) || (old_delta_x > 0 && delta_x_ < 0)) { |
| 74 ResetOverscroll(); |
| 75 return; |
| 76 } |
| 77 delegate_->OnOverscrollUpdate(delta_x_, 0.f); |
| 78 } |
| 79 |
| 80 void OverscrollWindowDelegate::OnKeyEvent(ui::KeyEvent* event) { |
| 81 ResetOverscroll(); |
| 82 } |
| 83 |
| 84 void OverscrollWindowDelegate::OnMouseEvent(ui::MouseEvent* event) { |
| 85 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) && |
| 86 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) { |
| 87 ResetOverscroll(); |
| 88 } |
| 89 } |
| 90 |
| 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(); |
| 97 else |
| 98 ResetOverscroll(); |
| 99 event->SetHandled(); |
| 100 } |
| 101 |
| 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()); |
| 107 break; |
| 108 |
| 109 case ui::ET_GESTURE_SCROLL_END: |
| 110 CompleteOrResetOverscroll(); |
| 111 break; |
| 112 |
| 113 case ui::ET_SCROLL_FLING_START: |
| 114 CompleteOrResetOverscroll(); |
| 115 break; |
| 116 |
| 117 case ui::ET_GESTURE_PINCH_BEGIN: |
| 118 case ui::ET_GESTURE_PINCH_UPDATE: |
| 119 case ui::ET_GESTURE_PINCH_END: |
| 120 ResetOverscroll(); |
| 121 break; |
| 122 |
| 123 default: |
| 124 break; |
| 125 } |
| 126 event->SetHandled(); |
| 127 } |
| 128 |
| 129 } // namespace content |
OLD | NEW |