summaryrefslogtreecommitdiffstats
path: root/ash/ui_controls_ash.cc
blob: 7463a53f92e7246a753c9821255a3b36e166b47b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shell.h"
#include "ash/shell_factory.h"
#include "ash/wm/coordinate_conversion.h"
#include "ash/wm/window_properties.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/root_window.h"
#include "ui/aura/ui_controls_aura.h"
#include "ui/gfx/screen.h"
#include "ui/ui_controls/ui_controls_aura.h"

namespace ash {
namespace internal {
namespace {

// Returns the UIControls object for RootWindow.
// kUIControlsKey is owned property and UIControls object
// will be deleted when the root window is deleted.
ui_controls::UIControlsAura* GetUIControlsForRootWindow(
    aura::RootWindow* root_window) {
  ui_controls::UIControlsAura* native_ui_control =
      root_window->GetProperty(kUIControlsKey);
  if (!native_ui_control) {
    native_ui_control = aura::CreateUIControlsAura(root_window);
    // Pass the ownership to the |root_window|.
    root_window->SetProperty(kUIControlsKey, native_ui_control);
  }
  return native_ui_control;
}

// Returns the UIControls object for the RootWindow at the |point| in
// absolute screen coordinates.  NULL if there is no RootWindow under the
// |point|.
ui_controls::UIControlsAura* GetUIControlsAt(const gfx::Point& point) {
  // If there is a capture events must be relative to it.
  aura::client::CaptureClient* capture_client =
      GetCaptureClient(ash::Shell::GetInstance()->GetPrimaryRootWindow());
  if (capture_client && capture_client->GetCaptureWindow()) {
    return GetUIControlsForRootWindow(
        capture_client->GetCaptureWindow()->GetRootWindow());
  }
  aura::RootWindow* root = wm::GetRootWindowAt(point);
  return root ? GetUIControlsForRootWindow(root) : NULL;
}

}  // namespace

class UIControlsAsh : public ui_controls::UIControlsAura {
 public:
  UIControlsAsh() {
  }
  virtual ~UIControlsAsh() {
  }

  // ui_controls::UIControslAura overrides:
  virtual bool SendKeyPress(gfx::NativeWindow window,
                            ui::KeyboardCode key,
                            bool control,
                            bool shift,
                            bool alt,
                            bool command) OVERRIDE {
    return SendKeyPressNotifyWhenDone(
        window, key, control, shift, alt, command, base::Closure());
  }

  virtual bool SendKeyPressNotifyWhenDone(
      gfx::NativeWindow window,
      ui::KeyboardCode key,
      bool control,
      bool shift,
      bool alt,
      bool command,
      const base::Closure& closure) OVERRIDE {
    aura::RootWindow* root =
        window ? window->GetRootWindow() : Shell::GetActiveRootWindow();
    ui_controls::UIControlsAura* ui_controls = GetUIControlsForRootWindow(root);
    return ui_controls && ui_controls->SendKeyPressNotifyWhenDone(
        window, key, control, shift, alt, command, closure);
  }

  virtual bool SendMouseMove(long x, long y) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls =
        GetUIControlsAt(gfx::Point(x, y));
    return ui_controls && ui_controls->SendMouseMove(x, y);
  }

  virtual bool SendMouseMoveNotifyWhenDone(
      long x,
      long y,
      const base::Closure& closure) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls =
        GetUIControlsAt(gfx::Point(x, y));
    return ui_controls &&
        ui_controls->SendMouseMoveNotifyWhenDone(x, y, closure);
  }

  virtual bool SendMouseEvents(ui_controls::MouseButton type,
                               int state) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls =
        GetUIControlsAt(gfx::Screen::GetCursorScreenPoint());
    return ui_controls && ui_controls->SendMouseEvents(type, state);
  }

  virtual bool SendMouseEventsNotifyWhenDone(
      ui_controls::MouseButton type,
      int state,
      const base::Closure& closure) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls =
        GetUIControlsAt(gfx::Screen::GetCursorScreenPoint());
    return ui_controls && ui_controls->SendMouseEventsNotifyWhenDone(
        type, state, closure);
  }

  virtual bool SendMouseClick(ui_controls::MouseButton type) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls =
        GetUIControlsAt(gfx::Screen::GetCursorScreenPoint());
    return ui_controls && ui_controls->SendMouseClick(type);
  }

  virtual void RunClosureAfterAllPendingUIEvents(
      const base::Closure& closure) OVERRIDE {
    ui_controls::UIControlsAura* ui_controls = GetUIControlsForRootWindow(
        Shell::GetActiveRootWindow());
    if (ui_controls)
      ui_controls->RunClosureAfterAllPendingUIEvents(closure);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(UIControlsAsh);
};

ui_controls::UIControlsAura* CreateUIControls() {
  return new UIControlsAsh();
}

}  // namespace internal
}  // namespace ash