1 // Copyright 2014 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.
6 #include "base/logging.h"
7 #include "ui/aura/client/screen_position_client.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/test/aura_test_utils.h"
10 #include "ui/aura/test/ui_controls_factory_aura.h"
11 #include "ui/aura/window_tree_host.h"
12 #include "ui/base/test/ui_controls_aura.h"
18 class UIControlsOzone
: public ui_controls::UIControlsAura
{
20 UIControlsOzone(WindowTreeHost
* host
) : host_(host
) {}
22 virtual bool SendKeyPress(gfx::NativeWindow window
,
27 bool command
) OVERRIDE
{
28 return SendKeyPressNotifyWhenDone(
29 window
, key
, control
, shift
, alt
, command
, base::Closure());
31 virtual bool SendKeyPressNotifyWhenDone(
32 gfx::NativeWindow window
,
38 const base::Closure
& closure
) OVERRIDE
{
39 DCHECK(!command
); // No command key on Aura
41 int flags
= button_down_mask_
;
44 flags
|= ui::EF_CONTROL_DOWN
;
45 PostKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_CONTROL
, flags
);
49 flags
|= ui::EF_SHIFT_DOWN
;
50 PostKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_SHIFT
, flags
);
54 flags
|= ui::EF_ALT_DOWN
;
55 PostKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_MENU
, flags
);
58 PostKeyEvent(ui::ET_KEY_PRESSED
, key
, flags
);
59 PostKeyEvent(ui::ET_KEY_RELEASED
, key
, flags
);
62 flags
&= ~ui::EF_ALT_DOWN
;
63 PostKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_MENU
, flags
);
67 flags
&= ~ui::EF_SHIFT_DOWN
;
68 PostKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_SHIFT
, flags
);
72 flags
&= ~ui::EF_CONTROL_DOWN
;
73 PostKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_CONTROL
, flags
);
76 RunClosureAfterAllPendingUIEvents(closure
);
80 virtual bool SendMouseMove(long screen_x
, long screen_y
) OVERRIDE
{
81 return SendMouseMoveNotifyWhenDone(screen_x
, screen_y
, base::Closure());
83 virtual bool SendMouseMoveNotifyWhenDone(
86 const base::Closure
& closure
) OVERRIDE
{
87 gfx::Point
root_location(screen_x
, screen_y
);
88 aura::client::ScreenPositionClient
* screen_position_client
=
89 aura::client::GetScreenPositionClient(host_
->window());
90 if (screen_position_client
) {
91 screen_position_client
->ConvertPointFromScreen(host_
->window(),
94 gfx::Point root_current_location
=
95 QueryLatestMousePositionRequestInHost(host_
);
96 host_
->ConvertPointFromHost(&root_current_location
);
98 if (button_down_mask_
)
99 PostMouseEvent(ui::ET_MOUSE_DRAGGED
, root_location
, 0, 0);
101 PostMouseEvent(ui::ET_MOUSE_MOVED
, root_location
, 0, 0);
103 RunClosureAfterAllPendingUIEvents(closure
);
106 virtual bool SendMouseEvents(ui_controls::MouseButton type
,
107 int state
) OVERRIDE
{
108 return SendMouseEventsNotifyWhenDone(type
, state
, base::Closure());
110 virtual bool SendMouseEventsNotifyWhenDone(
111 ui_controls::MouseButton type
,
113 const base::Closure
& closure
) OVERRIDE
{
114 gfx::Point loc
= aura::Env::GetInstance()->last_mouse_location();
115 aura::client::ScreenPositionClient
* screen_position_client
=
116 aura::client::GetScreenPositionClient(host_
->window());
117 if (screen_position_client
) {
118 screen_position_client
->ConvertPointFromScreen(host_
->window(), &loc
);
123 case ui_controls::LEFT
:
124 flag
= ui::EF_LEFT_MOUSE_BUTTON
;
126 case ui_controls::MIDDLE
:
127 flag
= ui::EF_MIDDLE_MOUSE_BUTTON
;
129 case ui_controls::RIGHT
:
130 flag
= ui::EF_RIGHT_MOUSE_BUTTON
;
137 if (state
& ui_controls::DOWN
) {
138 button_down_mask_
|= flag
;
139 PostMouseEvent(ui::ET_MOUSE_PRESSED
, loc
, button_down_mask_
| flag
, flag
);
141 if (state
& ui_controls::UP
) {
142 button_down_mask_
&= ~flag
;
144 ui::ET_MOUSE_RELEASED
, loc
, button_down_mask_
| flag
, flag
);
147 RunClosureAfterAllPendingUIEvents(closure
);
150 virtual bool SendMouseClick(ui_controls::MouseButton type
) OVERRIDE
{
151 return SendMouseEvents(type
, ui_controls::UP
| ui_controls::DOWN
);
153 virtual void RunClosureAfterAllPendingUIEvents(
154 const base::Closure
& closure
) OVERRIDE
{
155 if (!closure
.is_null())
156 base::MessageLoop::current()->PostTask(FROM_HERE
, closure
);
160 void PostKeyEvent(ui::EventType type
, ui::KeyboardCode key_code
, int flags
) {
161 base::MessageLoop::current()->PostTask(
163 base::Bind(&UIControlsOzone::PostKeyEventTask
,
164 base::Unretained(this),
170 void PostKeyEventTask(ui::EventType type
,
171 ui::KeyboardCode key_code
,
173 // Do not rewrite injected events. See crbug.com/136465.
174 flags
|= ui::EF_FINAL
;
176 ui::KeyEvent
key_event(type
, key_code
, flags
);
177 host_
->PostNativeEvent(&key_event
);
180 void PostMouseEvent(ui::EventType type
,
181 const gfx::PointF
& location
,
183 int changed_button_flags
) {
184 base::MessageLoop::current()->PostTask(
186 base::Bind(&UIControlsOzone::PostMouseEventTask
,
187 base::Unretained(this),
191 changed_button_flags
));
194 void PostMouseEventTask(ui::EventType type
,
195 const gfx::PointF
& location
,
197 int changed_button_flags
) {
198 ui::MouseEvent
mouse_event(
199 type
, location
, location
, flags
, changed_button_flags
);
201 // This hack is necessary to set the repeat count for clicks.
202 ui::MouseEvent
mouse_event2(&mouse_event
);
204 host_
->PostNativeEvent(&mouse_event2
);
207 WindowTreeHost
* host_
;
209 // Mask of the mouse buttons currently down.
210 unsigned button_down_mask_
= 0;
212 DISALLOW_COPY_AND_ASSIGN(UIControlsOzone
);
217 ui_controls::UIControlsAura
* CreateUIControlsAura(WindowTreeHost
* host
) {
218 return new UIControlsOzone(host
);