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

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: interface 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 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 bool IsModifierKey(const ui::KeyboardCode key_code) {
23 return key_code == ui::VKEY_SHIFT || key_code == ui::VKEY_LSHIFT ||
24 key_code == ui::VKEY_CONTROL || key_code == ui::VKEY_LCONTROL ||
25 key_code == ui::VKEY_RCONTROL || key_code == ui::VKEY_MENU ||
26 key_code == ui::VKEY_LMENU || key_code == ui::VKEY_RMENU;
27 }
28
29 } // namespace
30
31 AutoclickControllerCommon::AutoclickControllerCommon(base::TimeDelta delay,
32 Delegate* delegate)
33 : delay_(delay),
34 mouse_event_flags_(ui::EF_NONE),
35 delegate_(delegate),
36 widget_(nullptr),
37 anchor_location_(-kMovementThreshold, -kMovementThreshold),
38 autoclick_ring_handler_(new AutoclickRingHandler()) {
39 InitClickTimer();
40 }
41
42 AutoclickControllerCommon::~AutoclickControllerCommon() {}
43
44 void AutoclickControllerCommon::HandleMouseEvent(const ui::MouseEvent& event) {
45 gfx::Point mouse_location = event.location();
46 if (event.type() == ui::ET_MOUSE_MOVED &&
sky 2016/08/15 19:27:00 It seems like this code wants to observe drag even
riajiang 2016/08/16 00:44:24 Why is it also observing drag events? I thought it
sky 2016/08/16 02:40:01 You are right, I missed that. Sorry.
47 !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
48 mouse_event_flags_ = event.flags();
49
50 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event.target()),
sky 2016/08/15 19:27:00 Have you thought of how you will wire this up for
riajiang 2016/08/16 00:44:24 True... just found a comment that says event.targe
sadrul 2016/08/16 00:47:35 Ideally, we would have crbug.com/608547 fixed, and
riajiang 2016/08/17 15:23:48 Done.
51 &mouse_location);
52 UpdateRingWidget(mouse_location);
53
54 // The distance between the mouse location and the anchor location
55 // must exceed a certain threshold to initiate a new autoclick countdown.
56 // This ensures that mouse jitter caused by poor motor control does not
57 // 1. initiate an unwanted autoclick from rest
58 // 2. prevent the autoclick from ever occuring when the mouse
59 // arrives at the target.
60 gfx::Vector2d delta = mouse_location - anchor_location_;
61 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) {
62 anchor_location_ = mouse_location;
63 autoclick_timer_->Reset();
64 autoclick_ring_handler_->StartGesture(delay_, anchor_location_,
65 widget_.get());
66 } else if (autoclick_timer_->IsRunning()) {
67 autoclick_ring_handler_->SetGestureCenter(mouse_location, widget_.get());
68 }
69 } else if (event.type() == ui::ET_MOUSE_PRESSED) {
70 CancelAutoclick();
71 } else if (event.type() == ui::ET_MOUSEWHEEL &&
72 autoclick_timer_->IsRunning()) {
73 autoclick_timer_->Reset();
74 UpdateRingWidget(mouse_location);
75 autoclick_ring_handler_->StartGesture(delay_, anchor_location_,
76 widget_.get());
77 }
78 }
79
80 void AutoclickControllerCommon::HandleKeyEvent(const ui::KeyEvent& event) {
81 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
82 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN |
83 ui::EF_IS_EXTENDED_KEY;
84 int new_modifiers = event.flags() & modifier_mask;
85 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers;
86
87 if (!IsModifierKey(event.key_code()))
88 CancelAutoclick();
89 }
90
91 void AutoclickControllerCommon::HandleTouchEvent(const ui::TouchEvent& event) {
92 CancelAutoclick();
93 }
94
95 void AutoclickControllerCommon::HandleGestureEvent(
96 const ui::GestureEvent& event) {
97 CancelAutoclick();
98 }
99
100 void AutoclickControllerCommon::HandleScrollEvent(
101 const ui::ScrollEvent& event) {
102 CancelAutoclick();
103 }
104
105 void AutoclickControllerCommon::SetAutoclickDelay(const base::TimeDelta delay) {
106 delay_ = delay;
107 InitClickTimer();
108 }
109
110 void AutoclickControllerCommon::CancelAutoclick() {
111 autoclick_timer_->Stop();
112 autoclick_ring_handler_->StopGesture();
113 delegate_->OnAutoclickCanceled();
114 }
115
116 void AutoclickControllerCommon::InitClickTimer() {
117 autoclick_timer_.reset(new base::Timer(
118 FROM_HERE, delay_, base::Bind(&AutoclickControllerCommon::DoAutoclick,
119 base::Unretained(this)),
120 false));
121 }
122
123 void AutoclickControllerCommon::DoAutoclick() {
124 gfx::Point screen_location =
125 display::Screen::GetScreen()->GetCursorScreenPoint();
126 anchor_location_ = screen_location;
127 delegate_->DoAutoclick(screen_location, mouse_event_flags_);
128 }
129
130 void AutoclickControllerCommon::UpdateRingWidget(
131 const gfx::Point& mouse_location) {
132 if (!widget_)
133 widget_.reset(
134 (delegate_->CreateAutoclickRingWidget(mouse_location)).release());
135 else
136 delegate_->UpdateAutoclickRingWidget(widget_.get(), mouse_location);
sadrul 2016/08/15 17:23:04 Use {}
riajiang 2016/08/16 00:44:24 Done.
137 }
138
139 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698