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