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.
5 #include "ui/views/controls/native/native_view_host_aura.h"
7 #include "base/logging.h"
8 #include "ui/aura/client/aura_constants.h"
9 #include "ui/aura/client/focus_client.h"
10 #include "ui/aura/window.h"
11 #include "ui/aura/window_delegate.h"
12 #include "ui/base/cursor/cursor.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/views/controls/native/native_view_host.h"
15 #include "ui/views/view_constants_aura.h"
16 #include "ui/views/widget/widget.h"
20 class NativeViewHostAura::ClippingWindowDelegate
: public aura::WindowDelegate
{
22 ClippingWindowDelegate() : native_view_(NULL
) {}
23 ~ClippingWindowDelegate() override
{}
25 void set_native_view(aura::Window
* native_view
) {
26 native_view_
= native_view
;
29 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
30 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
31 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
32 const gfx::Rect
& new_bounds
) override
{}
33 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
34 return gfx::kNullCursor
;
36 int GetNonClientComponent(const gfx::Point
& point
) const override
{
39 bool ShouldDescendIntoChildForEventHandling(
41 const gfx::Point
& location
) override
{
44 bool CanFocus() override
{
45 // Ask the hosted native view's delegate because directly calling
46 // aura::Window::CanFocus() will call back into this when checking whether
48 return native_view_
&& native_view_
->delegate()
49 ? native_view_
->delegate()->CanFocus()
52 void OnCaptureLost() override
{}
53 void OnPaint(const ui::PaintContext
& context
) override
{}
54 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
55 void OnWindowDestroying(aura::Window
* window
) override
{}
56 void OnWindowDestroyed(aura::Window
* window
) override
{}
57 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
58 bool HasHitTestMask() const override
{ return false; }
59 void GetHitTestMask(gfx::Path
* mask
) const override
{}
62 aura::Window
* native_view_
;
65 NativeViewHostAura::NativeViewHostAura(NativeViewHost
* host
)
67 clipping_window_delegate_(new ClippingWindowDelegate()),
68 clipping_window_(clipping_window_delegate_
.get()) {
69 // Set the type so descendant views (including popups) get positioned
71 clipping_window_
.SetType(ui::wm::WINDOW_TYPE_CONTROL
);
72 clipping_window_
.Init(ui::LAYER_NOT_DRAWN
);
73 clipping_window_
.set_owned_by_parent(false);
74 clipping_window_
.SetName("NativeViewHostAuraClip");
75 clipping_window_
.layer()->SetMasksToBounds(true);
76 clipping_window_
.SetProperty(views::kHostViewKey
, static_cast<View
*>(host_
));
79 NativeViewHostAura::~NativeViewHostAura() {
80 if (host_
->native_view()) {
81 host_
->native_view()->RemoveObserver(this);
82 host_
->native_view()->ClearProperty(views::kHostViewKey
);
83 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
84 clipping_window_
.ClearProperty(views::kHostViewKey
);
85 if (host_
->native_view()->parent() == &clipping_window_
)
86 clipping_window_
.RemoveChild(host_
->native_view());
90 ////////////////////////////////////////////////////////////////////////////////
91 // NativeViewHostAura, NativeViewHostWrapper implementation:
92 void NativeViewHostAura::AttachNativeView() {
93 clipping_window_delegate_
->set_native_view(host_
->native_view());
94 host_
->native_view()->AddObserver(this);
95 host_
->native_view()->SetProperty(views::kHostViewKey
,
96 static_cast<View
*>(host_
));
100 void NativeViewHostAura::NativeViewDetaching(bool destroyed
) {
101 clipping_window_delegate_
->set_native_view(NULL
);
102 RemoveClippingWindow();
104 host_
->native_view()->RemoveObserver(this);
105 host_
->native_view()->ClearProperty(views::kHostViewKey
);
106 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
107 host_
->native_view()->Hide();
108 if (host_
->native_view()->parent())
109 Widget::ReparentNativeView(host_
->native_view(), NULL
);
113 void NativeViewHostAura::AddedToWidget() {
114 if (!host_
->native_view())
118 if (host_
->IsDrawn())
119 host_
->native_view()->Show();
121 host_
->native_view()->Hide();
125 void NativeViewHostAura::RemovedFromWidget() {
126 if (host_
->native_view()) {
127 host_
->native_view()->Hide();
128 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
129 if (host_
->native_view()->parent())
130 host_
->native_view()->parent()->RemoveChild(host_
->native_view());
131 RemoveClippingWindow();
135 void NativeViewHostAura::InstallClip(int x
, int y
, int w
, int h
) {
137 new gfx::Rect(host_
->ConvertRectToWidget(gfx::Rect(x
, y
, w
, h
))));
140 bool NativeViewHostAura::HasInstalledClip() {
144 void NativeViewHostAura::UninstallClip() {
148 void NativeViewHostAura::ShowWidget(int x
, int y
, int w
, int h
) {
151 if (host_
->fast_resize()) {
152 gfx::Point
origin(x
, y
);
153 views::View::ConvertPointFromWidget(host_
, &origin
);
154 InstallClip(origin
.x(), origin
.y(), w
, h
);
155 width
= host_
->native_view()->bounds().width();
156 height
= host_
->native_view()->bounds().height();
158 clipping_window_
.SetBounds(clip_rect_
? *clip_rect_
159 : gfx::Rect(x
, y
, w
, h
));
161 gfx::Point clip_offset
= clipping_window_
.bounds().origin();
162 host_
->native_view()->SetBounds(
163 gfx::Rect(x
- clip_offset
.x(), y
- clip_offset
.y(), width
, height
));
164 host_
->native_view()->Show();
165 clipping_window_
.Show();
168 void NativeViewHostAura::HideWidget() {
169 host_
->native_view()->Hide();
170 clipping_window_
.Hide();
173 void NativeViewHostAura::SetFocus() {
174 aura::Window
* window
= host_
->native_view();
175 aura::client::FocusClient
* client
= aura::client::GetFocusClient(window
);
177 client
->FocusWindow(window
);
180 gfx::NativeViewAccessible
NativeViewHostAura::GetNativeViewAccessible() {
184 gfx::NativeCursor
NativeViewHostAura::GetCursor(int x
, int y
) {
185 if (host_
->native_view())
186 return host_
->native_view()->GetCursor(gfx::Point(x
, y
));
187 return gfx::kNullCursor
;
190 void NativeViewHostAura::OnWindowDestroying(aura::Window
* window
) {
191 DCHECK(window
== host_
->native_view());
192 clipping_window_delegate_
->set_native_view(NULL
);
195 void NativeViewHostAura::OnWindowDestroyed(aura::Window
* window
) {
196 DCHECK(window
== host_
->native_view());
197 host_
->NativeViewDestroyed();
201 NativeViewHostWrapper
* NativeViewHostWrapper::CreateWrapper(
202 NativeViewHost
* host
) {
203 return new NativeViewHostAura(host
);
206 void NativeViewHostAura::AddClippingWindow() {
207 RemoveClippingWindow();
209 host_
->native_view()->SetProperty(aura::client::kHostWindowKey
,
210 host_
->GetWidget()->GetNativeView());
211 Widget::ReparentNativeView(host_
->native_view(),
213 if (host_
->GetWidget()->GetNativeView()) {
214 Widget::ReparentNativeView(&clipping_window_
,
215 host_
->GetWidget()->GetNativeView());
219 void NativeViewHostAura::RemoveClippingWindow() {
220 clipping_window_
.Hide();
221 if (host_
->native_view())
222 host_
->native_view()->ClearProperty(aura::client::kHostWindowKey
);
224 if (host_
->native_view()->parent() == &clipping_window_
) {
225 if (host_
->GetWidget() && host_
->GetWidget()->GetNativeView()) {
226 Widget::ReparentNativeView(host_
->native_view(),
227 host_
->GetWidget()->GetNativeView());
229 clipping_window_
.RemoveChild(host_
->native_view());
231 host_
->native_view()->SetBounds(clipping_window_
.bounds());
233 if (clipping_window_
.parent())
234 clipping_window_
.parent()->RemoveChild(&clipping_window_
);