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.
6 #include "ash/shell_factory.h"
7 #include "ash/wm/coordinate_conversion.h"
8 #include "ash/wm/window_properties.h"
9 #include "ui/aura/client/capture_client.h"
10 #include "ui/aura/client/screen_position_client.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/test/ui_controls_factory_aura.h"
14 #include "ui/aura/window_property.h"
15 #include "ui/base/test/ui_controls.h"
16 #include "ui/base/test/ui_controls_aura.h"
17 #include "ui/gfx/screen.h"
19 DECLARE_WINDOW_PROPERTY_TYPE(ui_controls::UIControlsAura
*)
25 using ui_controls::UIControlsAura
;
26 using ui_controls::MouseButton
;
28 DEFINE_OWNED_WINDOW_PROPERTY_KEY(UIControlsAura
, kUIControlsKey
, NULL
);
30 // Returns the UIControls object for RootWindow.
31 // kUIControlsKey is owned property and UIControls object
32 // will be deleted when the root window is deleted.
33 UIControlsAura
* GetUIControlsForRootWindow(aura::Window
* root_window
) {
34 UIControlsAura
* native_ui_control
=
35 root_window
->GetProperty(kUIControlsKey
);
36 if (!native_ui_control
) {
38 aura::test::CreateUIControlsAura(root_window
->GetDispatcher());
39 // Pass the ownership to the |root_window|.
40 root_window
->SetProperty(kUIControlsKey
, native_ui_control
);
42 return native_ui_control
;
45 // Returns the UIControls object for the RootWindow at the |point_in_screen|
46 // in virtual screen coordinates, and updates the |point| relative to the
47 // UIControlsAura's root window. NULL if there is no RootWindow under
48 // the |point_in_screen|.
49 UIControlsAura
* GetUIControlsAt(gfx::Point
* point_in_screen
) {
50 // TODO(mazda): Support the case passive grab is taken.
51 aura::Window
* root
= ash::wm::GetRootWindowAt(*point_in_screen
);
53 aura::client::ScreenPositionClient
* screen_position_client
=
54 aura::client::GetScreenPositionClient(root
);
55 if (screen_position_client
)
56 screen_position_client
->ConvertPointFromScreen(root
, point_in_screen
);
58 return GetUIControlsForRootWindow(root
);
63 class UIControlsAsh
: public UIControlsAura
{
67 virtual ~UIControlsAsh() {
70 // UIControslAura overrides:
71 virtual bool SendKeyPress(gfx::NativeWindow window
,
76 bool command
) OVERRIDE
{
77 return SendKeyPressNotifyWhenDone(
78 window
, key
, control
, shift
, alt
, command
, base::Closure());
81 virtual bool SendKeyPressNotifyWhenDone(
82 gfx::NativeWindow window
,
88 const base::Closure
& closure
) OVERRIDE
{
90 window
? window
->GetRootWindow() : ash::Shell::GetTargetRootWindow();
91 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(root
);
92 return ui_controls
&& ui_controls
->SendKeyPressNotifyWhenDone(
93 window
, key
, control
, shift
, alt
, command
, closure
);
96 virtual bool SendMouseMove(long x
, long y
) OVERRIDE
{
98 UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
99 return ui_controls
&& ui_controls
->SendMouseMove(p
.x(), p
.y());
102 virtual bool SendMouseMoveNotifyWhenDone(
105 const base::Closure
& closure
) OVERRIDE
{
107 UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
108 return ui_controls
&&
109 ui_controls
->SendMouseMoveNotifyWhenDone(p
.x(), p
.y(), closure
);
112 virtual bool SendMouseEvents(MouseButton type
, int state
) OVERRIDE
{
113 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
114 UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
115 return ui_controls
&& ui_controls
->SendMouseEvents(type
, state
);
118 virtual bool SendMouseEventsNotifyWhenDone(
119 MouseButton type
, int state
, const base::Closure
& closure
) OVERRIDE
{
120 gfx::Point
p(aura::Env::GetInstance()->last_mouse_location());
121 UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
122 return ui_controls
&& ui_controls
->SendMouseEventsNotifyWhenDone(
123 type
, state
, closure
);
126 virtual bool SendMouseClick(MouseButton type
) OVERRIDE
{
127 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
128 UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
129 return ui_controls
&& ui_controls
->SendMouseClick(type
);
132 virtual void RunClosureAfterAllPendingUIEvents(
133 const base::Closure
& closure
) OVERRIDE
{
134 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(
135 ash::Shell::GetTargetRootWindow());
137 ui_controls
->RunClosureAfterAllPendingUIEvents(closure
);
141 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh
);
144 ui_controls::UIControlsAura
* CreateAshUIControls() {
145 return new ash::test::UIControlsAsh();