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/test/ui_controls_factory_aura.h"
13 #include "ui/aura/window_property.h"
14 #include "ui/aura/window_tree_host.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
->GetHost());
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 |point_in_screen|.
46 UIControlsAura
* GetUIControlsAt(const gfx::Point
& point_in_screen
) {
47 // TODO(mazda): Support the case passive grab is taken.
48 return GetUIControlsForRootWindow(ash::wm::GetRootWindowAt(point_in_screen
));
53 class UIControlsAsh
: public UIControlsAura
{
57 virtual ~UIControlsAsh() {
60 // UIControslAura overrides:
61 virtual bool SendKeyPress(gfx::NativeWindow window
,
66 bool command
) OVERRIDE
{
67 return SendKeyPressNotifyWhenDone(
68 window
, key
, control
, shift
, alt
, command
, base::Closure());
71 virtual bool SendKeyPressNotifyWhenDone(
72 gfx::NativeWindow window
,
78 const base::Closure
& closure
) OVERRIDE
{
80 window
? window
->GetRootWindow() : ash::Shell::GetTargetRootWindow();
81 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(root
);
82 return ui_controls
&& ui_controls
->SendKeyPressNotifyWhenDone(
83 window
, key
, control
, shift
, alt
, command
, closure
);
86 virtual bool SendMouseMove(long x
, long y
) OVERRIDE
{
88 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
89 return ui_controls
&& ui_controls
->SendMouseMove(p
.x(), p
.y());
92 virtual bool SendMouseMoveNotifyWhenDone(
95 const base::Closure
& closure
) OVERRIDE
{
97 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
99 ui_controls
->SendMouseMoveNotifyWhenDone(p
.x(), p
.y(), closure
);
102 virtual bool SendMouseEvents(MouseButton type
, int state
) OVERRIDE
{
103 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
104 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
105 return ui_controls
&& ui_controls
->SendMouseEvents(type
, state
);
108 virtual bool SendMouseEventsNotifyWhenDone(
109 MouseButton type
, int state
, const base::Closure
& closure
) OVERRIDE
{
110 gfx::Point
p(aura::Env::GetInstance()->last_mouse_location());
111 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
112 return ui_controls
&& ui_controls
->SendMouseEventsNotifyWhenDone(
113 type
, state
, closure
);
116 virtual bool SendMouseClick(MouseButton type
) OVERRIDE
{
117 gfx::Point
p(ash::Shell::GetScreen()->GetCursorScreenPoint());
118 UIControlsAura
* ui_controls
= GetUIControlsAt(p
);
119 return ui_controls
&& ui_controls
->SendMouseClick(type
);
122 virtual void RunClosureAfterAllPendingUIEvents(
123 const base::Closure
& closure
) OVERRIDE
{
124 UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(
125 ash::Shell::GetTargetRootWindow());
127 ui_controls
->RunClosureAfterAllPendingUIEvents(closure
);
131 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh
);
134 ui_controls::UIControlsAura
* CreateAshUIControls() {
135 return new ash::test::UIControlsAsh();