1 // Copyright (c) 2012 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/root_window.h"
12 #include "ui/aura/ui_controls_aura.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/ui_controls/ui_controls_aura.h"
20 // Returns the UIControls object for RootWindow.
21 // kUIControlsKey is owned property and UIControls object
22 // will be deleted when the root window is deleted.
23 ui_controls::UIControlsAura
* GetUIControlsForRootWindow(
24 aura::RootWindow
* root_window
) {
25 ui_controls::UIControlsAura
* native_ui_control
=
26 root_window
->GetProperty(kUIControlsKey
);
27 if (!native_ui_control
) {
28 native_ui_control
= aura::CreateUIControlsAura(root_window
);
29 // Pass the ownership to the |root_window|.
30 root_window
->SetProperty(kUIControlsKey
, native_ui_control
);
32 return native_ui_control
;
35 // Returns the UIControls object for the RootWindow at the |point| in
36 // virtual screen coordinates, and updates the |point| relative to the
37 // UIControlsAura's root window. NULL if there is no RootWindow under
39 ui_controls::UIControlsAura
* GetUIControlsAt(gfx::Point
* point
) {
40 // If there is a capture events must be relative to it.
41 aura::client::CaptureClient
* capture_client
=
42 GetCaptureClient(ash::Shell::GetInstance()->GetPrimaryRootWindow());
43 aura::RootWindow
* root
= NULL
;
44 if (capture_client
&& capture_client
->GetCaptureWindow())
45 root
= capture_client
->GetCaptureWindow()->GetRootWindow();
47 root
= wm::GetRootWindowAt(*point
);
49 aura::client::ScreenPositionClient
* screen_position_client
=
50 aura::client::GetScreenPositionClient(root
);
51 if (screen_position_client
)
52 screen_position_client
->ConvertPointFromScreen(root
, point
);
54 return GetUIControlsForRootWindow(root
);
59 class UIControlsAsh
: public ui_controls::UIControlsAura
{
63 virtual ~UIControlsAsh() {
66 // ui_controls::UIControslAura overrides:
67 virtual bool SendKeyPress(gfx::NativeWindow window
,
72 bool command
) OVERRIDE
{
73 return SendKeyPressNotifyWhenDone(
74 window
, key
, control
, shift
, alt
, command
, base::Closure());
77 virtual bool SendKeyPressNotifyWhenDone(
78 gfx::NativeWindow window
,
84 const base::Closure
& closure
) OVERRIDE
{
85 aura::RootWindow
* root
=
86 window
? window
->GetRootWindow() : Shell::GetActiveRootWindow();
87 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(root
);
88 return ui_controls
&& ui_controls
->SendKeyPressNotifyWhenDone(
89 window
, key
, control
, shift
, alt
, command
, closure
);
92 virtual bool SendMouseMove(long x
, long y
) OVERRIDE
{
94 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
95 return ui_controls
&& ui_controls
->SendMouseMove(p
.x(), p
.y());
98 virtual bool SendMouseMoveNotifyWhenDone(
101 const base::Closure
& closure
) OVERRIDE
{
103 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
104 return ui_controls
&&
105 ui_controls
->SendMouseMoveNotifyWhenDone(p
.x(), p
.y(), closure
);
108 virtual bool SendMouseEvents(ui_controls::MouseButton type
,
109 int state
) OVERRIDE
{
110 gfx::Point
p(gfx::Screen::GetCursorScreenPoint());
111 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
112 return ui_controls
&& ui_controls
->SendMouseEvents(type
, state
);
115 virtual bool SendMouseEventsNotifyWhenDone(
116 ui_controls::MouseButton type
,
118 const base::Closure
& closure
) OVERRIDE
{
119 gfx::Point
p(gfx::Screen::GetCursorScreenPoint());
120 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
121 return ui_controls
&& ui_controls
->SendMouseEventsNotifyWhenDone(
122 type
, state
, closure
);
125 virtual bool SendMouseClick(ui_controls::MouseButton type
) OVERRIDE
{
126 gfx::Point
p(gfx::Screen::GetCursorScreenPoint());
127 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
128 return ui_controls
&& ui_controls
->SendMouseClick(type
);
131 virtual void RunClosureAfterAllPendingUIEvents(
132 const base::Closure
& closure
) OVERRIDE
{
133 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(
134 Shell::GetActiveRootWindow());
136 ui_controls
->RunClosureAfterAllPendingUIEvents(closure
);
140 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh
);
143 ui_controls::UIControlsAura
* CreateUIControls() {
144 return new UIControlsAsh();
147 } // namespace internal