Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: ash/autoclick/common/autoclick_controller_common.cc

Issue 2193563002: ash: Refactor autoclick common code to ash/autoclick/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/events/event.h"
9 #include "ui/events/event_constants.h"
10 #include "ui/gfx/geometry/vector2d.h"
11 #include "ui/wm/core/coordinate_conversion.h"
12
13 namespace ash {
14
15 namespace {
16
17 // The threshold of mouse movement measured in DIP that will
18 // initiate a new autoclick.
19 const int kMovementThreshold = 20;
20
21 bool IsModifierKey(ui::KeyboardCode key_code) {
22 return key_code == ui::VKEY_SHIFT || key_code == ui::VKEY_LSHIFT ||
23 key_code == ui::VKEY_CONTROL || key_code == ui::VKEY_LCONTROL ||
24 key_code == ui::VKEY_RCONTROL || key_code == ui::VKEY_MENU ||
25 key_code == ui::VKEY_LMENU || key_code == ui::VKEY_RMENU;
26 }
27
28 } // namespace
29
30 AutoclickControllerCommon::AutoclickControllerCommon()
31 : mouse_event_flags_(ui::EF_NONE),
32 anchor_location_(-kMovementThreshold, -kMovementThreshold),
33 autoclick_ring_handler_(new AutoclickRingHandler()) {}
34
35 AutoclickControllerCommon::~AutoclickControllerCommon() {}
36
37 void AutoclickControllerCommon::HandleMouseEvent(const ui::MouseEvent& event,
38 aura::Window* target,
39 views::Widget* widget,
40 base::TimeDelta delay) {
41 if (event.type() == ui::ET_MOUSE_MOVED &&
42 !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
43 mouse_event_flags_ = event.flags();
44
45 gfx::Point mouse_location = event.location();
46 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event.target()),
47 &mouse_location);
48
49 // The distance between the mouse location and the anchor location
50 // must exceed a certain threshold to initiate a new autoclick countdown.
51 // This ensures that mouse jitter caused by poor motor control does not
52 // 1. initiate an unwanted autoclick from rest
53 // 2. prevent the autoclick from ever occuring when the mouse
54 // arrives at the target.
55 gfx::Vector2d delta = mouse_location - anchor_location_;
56 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) {
57 anchor_location_ = mouse_location;
58 autoclick_timer_->Reset();
59 autoclick_ring_handler_->StartGesture(delay, anchor_location_, target,
60 widget);
61 } else if (autoclick_timer_->IsRunning()) {
62 autoclick_ring_handler_->SetGestureCenter(mouse_location, target, widget);
63 }
64 } else if (event.type() == ui::ET_MOUSE_PRESSED) {
65 StopAutoclickTimer();
66 autoclick_ring_handler_->StopGesture();
67 } else if (event.type() == ui::ET_MOUSEWHEEL &&
68 autoclick_timer_->IsRunning()) {
69 autoclick_timer_->Reset();
70 autoclick_ring_handler_->StartGesture(delay, anchor_location_, target,
71 widget);
72 }
73 }
74
75 void AutoclickControllerCommon::HandleKeyEvent(const ui::KeyEvent& event) {
76 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
77 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN |
78 ui::EF_IS_EXTENDED_KEY;
79 int new_modifiers = event.flags() & modifier_mask;
80 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers;
81
82 if (!IsModifierKey(event.key_code())) {
83 StopAutoclickTimer();
84 autoclick_ring_handler_->StopGesture();
85 }
86 }
87
88 void AutoclickControllerCommon::HandleTouchEvent(const ui::TouchEvent& event) {
89 StopAutoclickTimer();
90 autoclick_ring_handler_->StopGesture();
91 }
92
93 void AutoclickControllerCommon::HandleGestureEvent(
94 const ui::GestureEvent& event) {
95 StopAutoclickTimer();
96 autoclick_ring_handler_->StopGesture();
97 }
98
99 void AutoclickControllerCommon::HandleScrollEvent(
100 const ui::ScrollEvent& event) {
101 StopAutoclickTimer();
102 autoclick_ring_handler_->StopGesture();
103 }
104
105 void AutoclickControllerCommon::ResetAutoclickTimer(base::Timer* new_timer) {
106 autoclick_timer_.reset(new_timer);
107 }
108
109 void AutoclickControllerCommon::StopAutoclickTimer() {
110 autoclick_timer_->Stop();
111 }
112
113 void AutoclickControllerCommon::UpdateAnchorLocation(
114 const gfx::Point& anchor_location) {
115 anchor_location_ = anchor_location;
116 }
117
118 int AutoclickControllerCommon::GetMouseEventFlags() {
119 return mouse_event_flags_;
120 }
121
122 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698