summaryrefslogtreecommitdiffstats
path: root/ui/views/widget/desktop_native_widget_helper_aura.cc
blob: 598b366c21858739c1af149cb8f7d316f92a8619 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// 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 "ui/views/widget/desktop_native_widget_helper_aura.h"

#include "ui/aura/client/dispatcher_client.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/desktop/desktop_activation_client.h"
#include "ui/aura/desktop/desktop_cursor_client.h"
#include "ui/aura/desktop/desktop_dispatcher_client.h"
#include "ui/aura/focus_manager.h"
#include "ui/aura/root_window.h"
#include "ui/aura/shared/compound_event_filter.h"
#include "ui/aura/shared/input_method_event_filter.h"
#include "ui/aura/shared/root_window_capture_client.h"
#include "ui/aura/window_property.h"
#include "ui/views/widget/native_widget_aura.h"

#if defined(OS_WIN)
#include "ui/base/win/hwnd_subclass.h"
#include "ui/views/widget/widget_message_filter.h"
#elif defined(USE_X11)
#include "ui/views/widget/x11_desktop_handler.h"
#include "ui/views/widget/x11_desktop_window_move_client.h"
#include "ui/views/widget/x11_window_event_filter.h"
#endif

DECLARE_WINDOW_PROPERTY_TYPE(aura::Window*);

namespace views {

DEFINE_WINDOW_PROPERTY_KEY(
    aura::Window*, kViewsWindowForRootWindow, NULL);

namespace {

// Client that always offsets by the toplevel RootWindow of the passed
// in child NativeWidgetAura.
class DesktopScreenPositionClient
    : public aura::client::ScreenPositionClient {
 public:
  DesktopScreenPositionClient() {}
  virtual ~DesktopScreenPositionClient() {}

  // aura::client::ScreenPositionClient overrides:
  virtual void ConvertPointToScreen(const aura::Window* window,
                                    gfx::Point* point) OVERRIDE {
    const aura::RootWindow* root_window = window->GetRootWindow();
    aura::Window::ConvertPointToTarget(window, root_window, point);
    gfx::Point origin = root_window->GetHostOrigin();
    point->Offset(origin.x(), origin.y());
  }

  virtual void ConvertPointFromScreen(const aura::Window* window,
                                      gfx::Point* point) OVERRIDE {
    const aura::RootWindow* root_window = window->GetRootWindow();
    gfx::Point origin = root_window->GetHostOrigin();
    point->Offset(-origin.x(), -origin.y());
    aura::Window::ConvertPointToTarget(root_window, window, point);
  }

  virtual void SetBounds(aura::Window* window,
                         const gfx::Rect& bounds,
                         const gfx::Display& display) OVERRIDE {
    // TODO: Use the 3rd parameter, |display|.
    gfx::Point origin = bounds.origin();
    aura::RootWindow* root = window->GetRootWindow();
    aura::Window::ConvertPointToTarget(window->parent(), root, &origin);

#if !defined(OS_WIN)
    if  (window->type() == aura::client::WINDOW_TYPE_CONTROL) {
      window->SetBounds(gfx::Rect(origin, bounds.size()));
      return;
    } else if (window->type() == aura::client::WINDOW_TYPE_POPUP) {
      // The caller expects windows we consider "embedded" to be placed in the
      // screen coordinate system. So we need to offset the root window's
      // position (which is in screen coordinates) from these bounds.
      gfx::Point host_origin = root->GetHostOrigin();
      origin.Offset(-host_origin.x(), -host_origin.y());
      window->SetBounds(gfx::Rect(origin, bounds.size()));
      return;
    }
#endif  // !defined(OS_WIN)
    root->SetHostBounds(bounds);
    window->SetBounds(gfx::Rect(bounds.size()));
  }
};

}  // namespace

DesktopNativeWidgetHelperAura::DesktopNativeWidgetHelperAura(
    NativeWidgetAura* widget)
    : widget_(widget),
      window_(NULL),
      root_window_event_filter_(NULL),
      is_embedded_window_(false) {
}

DesktopNativeWidgetHelperAura::~DesktopNativeWidgetHelperAura() {
  if (window_)
    window_->RemoveObserver(this);

  if (root_window_event_filter_) {
#if defined(USE_X11)
    root_window_event_filter_->RemoveFilter(x11_window_move_client_.get());
    root_window_event_filter_->RemoveFilter(x11_window_event_filter_.get());
#endif

    root_window_event_filter_->RemoveFilter(input_method_filter_.get());
  }
}

// static
aura::Window* DesktopNativeWidgetHelperAura::GetViewsWindowForRootWindow(
    aura::RootWindow* root) {
  return root ? root->GetProperty(kViewsWindowForRootWindow) : NULL;
}

void DesktopNativeWidgetHelperAura::PreInitialize(
    aura::Window* window,
    const Widget::InitParams& params) {
#if !defined(OS_WIN)
  // We don't want the status bubble or the omnibox to get their own root
  // window on the desktop; on Linux
  //
  // TODO(erg): This doesn't map perfectly to what I want to do. TYPE_POPUP is
  // used for lots of stuff, like dragged tabs, and I only want this to trigger
  // for the status bubble and the omnibox.
  if (params.type == Widget::InitParams::TYPE_POPUP ||
      params.type == Widget::InitParams::TYPE_BUBBLE) {
    is_embedded_window_ = true;
    return;
  } else if (params.type == Widget::InitParams::TYPE_CONTROL) {
    return;
  }
#endif

  gfx::Rect bounds = params.bounds;
  if (bounds.IsEmpty()) {
    // We must pass some non-zero value when we initialize a RootWindow. This
    // will probably be SetBounds()ed soon.
    bounds.set_size(gfx::Size(100, 100));
  }

  aura::FocusManager* focus_manager = NULL;
  aura::DesktopActivationClient* activation_client = NULL;
#if defined(USE_X11)
  focus_manager = X11DesktopHandler::get()->get_focus_manager();
  activation_client = X11DesktopHandler::get()->get_activation_client();
#else
  // TODO(ben): This is almost certainly wrong; I suspect that the windows
  // build will need a singleton like above.
  focus_manager = new aura::FocusManager;
  activation_client = new aura::DesktopActivationClient(focus_manager);
#endif

  root_window_.reset(new aura::RootWindow(bounds));
  root_window_->SetProperty(kViewsWindowForRootWindow, window);
  root_window_->Init();
  root_window_->set_focus_manager(focus_manager);

  // No event filter for aura::Env. Create CompoundEvnetFilter per RootWindow.
  root_window_event_filter_ = new aura::shared::CompoundEventFilter;
  // Pass ownership of the filter to the root_window.
  root_window_->SetEventFilter(root_window_event_filter_);

  input_method_filter_.reset(new aura::shared::InputMethodEventFilter());
  input_method_filter_->SetInputMethodPropertyInRootWindow(root_window_.get());
  root_window_event_filter_->AddFilter(input_method_filter_.get());

  capture_client_.reset(
      new aura::shared::RootWindowCaptureClient(root_window_.get()));

  cursor_client_.reset(new aura::DesktopCursorClient(root_window_.get()));
  aura::client::SetCursorClient(root_window_.get(), cursor_client_.get());

#if defined(USE_X11)
  x11_window_event_filter_.reset(
      new X11WindowEventFilter(root_window_.get(), activation_client, widget_));
  x11_window_event_filter_->SetUseHostWindowBorders(false);
  root_window_event_filter_->AddFilter(x11_window_event_filter_.get());
#endif

  root_window_->AddRootWindowObserver(this);

  window_ = window;
  window_->AddObserver(this);

  aura::client::SetActivationClient(root_window_.get(), activation_client);
  aura::client::SetDispatcherClient(root_window_.get(),
                                    new aura::DesktopDispatcherClient);
#if defined(USE_X11)
  // TODO(ben): A window implementation of this will need to be written.
  x11_window_move_client_.reset(new X11DesktopWindowMoveClient);
  root_window_event_filter_->AddFilter(x11_window_move_client_.get());
  aura::client::SetWindowMoveClient(root_window_.get(),
                                    x11_window_move_client_.get());
#endif

  position_client_.reset(new DesktopScreenPositionClient());
  aura::client::SetScreenPositionClient(root_window_.get(),
                                        position_client_.get());
}

void DesktopNativeWidgetHelperAura::PostInitialize() {
#if defined(OS_WIN)
  DCHECK(root_window_->GetAcceleratedWidget());
  hwnd_message_filter_.reset(new WidgetMessageFilter(root_window_.get(),
      widget_->GetWidget()));
  ui::HWNDSubclass::AddFilterToTarget(root_window_->GetAcceleratedWidget(),
                                      hwnd_message_filter_.get());
#endif
}

aura::RootWindow* DesktopNativeWidgetHelperAura::GetRootWindow() {
  return root_window_.get();
}

gfx::Rect DesktopNativeWidgetHelperAura::ModifyAndSetBounds(
    const gfx::Rect& bounds) {
  gfx::Rect out_bounds = bounds;
  if (root_window_.get() && !out_bounds.IsEmpty()) {
    // TODO(scottmg): This avoids the AdjustWindowRect that ash wants to
    // adjust the top level on Windows.
#if !defined(OS_WIN)
    root_window_->SetHostBounds(out_bounds);
#endif
    out_bounds.set_x(0);
    out_bounds.set_y(0);
  } else if (is_embedded_window_) {
    // The caller expects windows we consider "embedded" to be placed in the
    // screen coordinate system. So we need to offset the root window's
    // position (which is in screen coordinates) from these bounds.
    aura::RootWindow* root =
        widget_->GetNativeWindow()->GetRootWindow()->GetRootWindow();
    gfx::Point point = root->GetHostOrigin();
    out_bounds.set_x(out_bounds.x() - point.x());
    out_bounds.set_y(out_bounds.y() - point.y());
  }

  return out_bounds;
}

////////////////////////////////////////////////////////////////////////////////
// DesktopNativeWidgetHelperAura, aura::WindowObserver implementation:
void DesktopNativeWidgetHelperAura::OnWindowVisibilityChanged(
    aura::Window* window,
    bool visible) {
  DCHECK_EQ(window, window_);

  // Since we're trying to hide the main window, hide the OS level root as well.
  if (visible)
    root_window_->ShowRootWindow();
  else
    root_window_->HideRootWindow();
}

////////////////////////////////////////////////////////////////////////////////
// DesktopNativeWidgetHelperAura, aura::RootWindowObserver implementation:

void DesktopNativeWidgetHelperAura::OnRootWindowResized(
    const aura::RootWindow* root,
    const gfx::Size& old_size) {
  DCHECK_EQ(root, root_window_.get());
  widget_->SetBounds(gfx::Rect(root->GetHostOrigin(),
                               root->GetHostSize()));
}

void DesktopNativeWidgetHelperAura::OnRootWindowHostCloseRequested(
    const aura::RootWindow* root) {
  DCHECK_EQ(root, root_window_.get());
  widget_->GetWidget()->Close();
}

void DesktopNativeWidgetHelperAura::OnRootWindowMoved(
    const aura::RootWindow* root,
    const gfx::Point& new_origin) {
  widget_->GetWidget()->OnNativeWidgetMove();
}

}  // namespace views