| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "ash/autoclick/common/autoclick_controller_common.h" |
| 6 |
| 7 #include "ui/aura/window.h" |
| 8 #include "ui/display/screen.h" |
| 9 #include "ui/events/event.h" |
| 10 #include "ui/events/event_constants.h" |
| 11 #include "ui/gfx/geometry/vector2d.h" |
| 12 #include "ui/wm/core/coordinate_conversion.h" |
| 13 |
| 14 namespace ash { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // The threshold of mouse movement measured in DIP that will |
| 19 // initiate a new autoclick. |
| 20 const int kMovementThreshold = 20; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 AutoclickControllerCommon::AutoclickControllerCommon(base::TimeDelta delay, |
| 25 Delegate* delegate) |
| 26 : delay_(delay), |
| 27 mouse_event_flags_(ui::EF_NONE), |
| 28 delegate_(delegate), |
| 29 widget_(nullptr), |
| 30 anchor_location_(-kMovementThreshold, -kMovementThreshold), |
| 31 autoclick_ring_handler_(new AutoclickRingHandler()) { |
| 32 InitClickTimer(); |
| 33 } |
| 34 |
| 35 AutoclickControllerCommon::~AutoclickControllerCommon() {} |
| 36 |
| 37 void AutoclickControllerCommon::HandleMouseEvent(const ui::MouseEvent& event) { |
| 38 gfx::Point mouse_location = event.location(); |
| 39 if (event.type() == ui::ET_MOUSE_MOVED && |
| 40 !(event.flags() & ui::EF_IS_SYNTHESIZED)) { |
| 41 mouse_event_flags_ = event.flags(); |
| 42 |
| 43 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event.target()), |
| 44 &mouse_location); |
| 45 UpdateRingWidget(mouse_location); |
| 46 |
| 47 // The distance between the mouse location and the anchor location |
| 48 // must exceed a certain threshold to initiate a new autoclick countdown. |
| 49 // This ensures that mouse jitter caused by poor motor control does not |
| 50 // 1. initiate an unwanted autoclick from rest |
| 51 // 2. prevent the autoclick from ever occuring when the mouse |
| 52 // arrives at the target. |
| 53 gfx::Vector2d delta = mouse_location - anchor_location_; |
| 54 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { |
| 55 anchor_location_ = mouse_location; |
| 56 autoclick_timer_->Reset(); |
| 57 autoclick_ring_handler_->StartGesture(delay_, anchor_location_, |
| 58 widget_.get()); |
| 59 } else if (autoclick_timer_->IsRunning()) { |
| 60 autoclick_ring_handler_->SetGestureCenter(mouse_location, widget_.get()); |
| 61 } |
| 62 } else if (event.type() == ui::ET_MOUSE_PRESSED) { |
| 63 StopAutoclickTimer(); |
| 64 StopGesture(); |
| 65 } else if (event.type() == ui::ET_MOUSEWHEEL && |
| 66 autoclick_timer_->IsRunning()) { |
| 67 autoclick_timer_->Reset(); |
| 68 UpdateRingWidget(mouse_location); |
| 69 autoclick_ring_handler_->StartGesture(delay_, anchor_location_, |
| 70 widget_.get()); |
| 71 } |
| 72 } |
| 73 |
| 74 void AutoclickControllerCommon::HandleKeyEvent(const ui::KeyEvent& event) { |
| 75 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 76 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | |
| 77 ui::EF_IS_EXTENDED_KEY; |
| 78 int new_modifiers = event.flags() & modifier_mask; |
| 79 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers; |
| 80 |
| 81 if (!IsModifierKey(event.key_code())) { |
| 82 StopAutoclickTimer(); |
| 83 StopGesture(); |
| 84 } |
| 85 } |
| 86 |
| 87 void AutoclickControllerCommon::HandleTouchEvent(const ui::TouchEvent& event) { |
| 88 StopAutoclickTimer(); |
| 89 StopGesture(); |
| 90 } |
| 91 |
| 92 void AutoclickControllerCommon::HandleGestureEvent( |
| 93 const ui::GestureEvent& event) { |
| 94 StopAutoclickTimer(); |
| 95 StopGesture(); |
| 96 } |
| 97 |
| 98 void AutoclickControllerCommon::HandleScrollEvent( |
| 99 const ui::ScrollEvent& event) { |
| 100 StopAutoclickTimer(); |
| 101 StopGesture(); |
| 102 } |
| 103 |
| 104 void AutoclickControllerCommon::SetAutoclickDelay(const base::TimeDelta delay) { |
| 105 delay_ = delay; |
| 106 InitClickTimer(); |
| 107 } |
| 108 |
| 109 void AutoclickControllerCommon::StopAutoclickTimer() { |
| 110 autoclick_timer_->Stop(); |
| 111 } |
| 112 |
| 113 void AutoclickControllerCommon::StopGesture() { |
| 114 autoclick_ring_handler_->StopGesture(); |
| 115 } |
| 116 |
| 117 bool AutoclickControllerCommon::IsModifierKey(const ui::KeyboardCode key_code) { |
| 118 return key_code == ui::VKEY_SHIFT || key_code == ui::VKEY_LSHIFT || |
| 119 key_code == ui::VKEY_CONTROL || key_code == ui::VKEY_LCONTROL || |
| 120 key_code == ui::VKEY_RCONTROL || key_code == ui::VKEY_MENU || |
| 121 key_code == ui::VKEY_LMENU || key_code == ui::VKEY_RMENU; |
| 122 } |
| 123 |
| 124 void AutoclickControllerCommon::InitClickTimer() { |
| 125 autoclick_timer_.reset(new base::Timer( |
| 126 FROM_HERE, delay_, base::Bind(&AutoclickControllerCommon::DoAutoclick, |
| 127 base::Unretained(this)), |
| 128 false)); |
| 129 } |
| 130 |
| 131 void AutoclickControllerCommon::DoAutoclick() { |
| 132 gfx::Point screen_location = |
| 133 display::Screen::GetScreen()->GetCursorScreenPoint(); |
| 134 anchor_location_ = screen_location; |
| 135 delegate_->DoAutoclick(screen_location, mouse_event_flags_); |
| 136 } |
| 137 |
| 138 void AutoclickControllerCommon::UpdateRingWidget( |
| 139 const gfx::Point& mouse_location) { |
| 140 if (!widget_) |
| 141 widget_.reset( |
| 142 (delegate_->CreateAutoclickRingWidget(mouse_location)).release()); |
| 143 else |
| 144 delegate_->UpdateAutoclickRingWidget(widget_.get(), mouse_location); |
| 145 } |
| 146 |
| 147 } // namespace ash |
| OLD | NEW |