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_screen|
36 // in virtual screen coordinates, and updates the |point| relative to the
37 // UIControlsAura's root window. NULL if there is no RootWindow under
38 // the |point_in_screen|.
39 ui_controls::UIControlsAura
* GetUIControlsAt(gfx::Point
* point_in_screen
) {
40 // TODO(mazda): Support the case passive grab is taken.
41 aura::RootWindow
* root
= wm::GetRootWindowAt(*point_in_screen
);
43 aura::client::ScreenPositionClient
* screen_position_client
=
44 aura::client::GetScreenPositionClient(root
);
45 if (screen_position_client
)
46 screen_position_client
->ConvertPointFromScreen(root
, point_in_screen
);
48 return GetUIControlsForRootWindow(root
);
53 class UIControlsAsh
: public ui_controls::UIControlsAura
{
57 virtual ~UIControlsAsh() {
60 // ui_controls::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
{
79 aura::RootWindow
* root
=
80 window
? window
->GetRootWindow() : Shell::GetActiveRootWindow();
81 ui_controls::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 ui_controls::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 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
99 ui_controls
->SendMouseMoveNotifyWhenDone(p
.x(), p
.y(), closure
);
102 virtual bool SendMouseEvents(ui_controls::MouseButton type
,
103 int state
) OVERRIDE
{
104 gfx::Point
p(Shell::GetScreen()->GetCursorScreenPoint());
105 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
106 return ui_controls
&& ui_controls
->SendMouseEvents(type
, state
);
109 virtual bool SendMouseEventsNotifyWhenDone(
110 ui_controls::MouseButton type
,
112 const base::Closure
& closure
) OVERRIDE
{
113 gfx::Point
p(Shell::GetScreen()->GetCursorScreenPoint());
114 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
115 return ui_controls
&& ui_controls
->SendMouseEventsNotifyWhenDone(
116 type
, state
, closure
);
119 virtual bool SendMouseClick(ui_controls::MouseButton type
) OVERRIDE
{
120 gfx::Point
p(Shell::GetScreen()->GetCursorScreenPoint());
121 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsAt(&p
);
122 return ui_controls
&& ui_controls
->SendMouseClick(type
);
125 virtual void RunClosureAfterAllPendingUIEvents(
126 const base::Closure
& closure
) OVERRIDE
{
127 ui_controls::UIControlsAura
* ui_controls
= GetUIControlsForRootWindow(
128 Shell::GetActiveRootWindow());
130 ui_controls
->RunClosureAfterAllPendingUIEvents(closure
);
134 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh
);
137 ui_controls::UIControlsAura
* CreateUIControls() {
138 return new UIControlsAsh();
141 } // namespace internal