OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/native/native_view_host_aura.h" | 5 #include "ui/views/controls/native/native_view_host_aura.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/aura/client/focus_client.h" | 8 #include "ui/aura/client/focus_client.h" |
9 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
| 10 #include "ui/gfx/safe_integer_conversions.h" |
10 #include "ui/views/controls/native/native_view_host.h" | 11 #include "ui/views/controls/native/native_view_host.h" |
11 #include "ui/views/view_constants_aura.h" | 12 #include "ui/views/view_constants_aura.h" |
12 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
13 | 14 |
14 namespace views { | 15 namespace views { |
15 | 16 |
| 17 // Watches for the clipping window to enter its destructor, so it can clear the |
| 18 // NVHA's reference to the clipping window. |
| 19 class ClippingWindowObserver : public aura::WindowObserver { |
| 20 public: |
| 21 explicit ClippingWindowObserver(NativeViewHostAura* host) : |
| 22 host_(host), |
| 23 clipping_window_(NULL) {} |
| 24 |
| 25 virtual ~ClippingWindowObserver() { |
| 26 if (clipping_window_) |
| 27 clipping_window_->RemoveObserver(this); |
| 28 } |
| 29 |
| 30 void SetClippingWindow(aura::Window* window) { |
| 31 if (clipping_window_) |
| 32 clipping_window_->RemoveObserver(this); |
| 33 clipping_window_ = window; |
| 34 if (clipping_window_) |
| 35 clipping_window_->AddObserver(this); |
| 36 } |
| 37 |
| 38 // Overridden from aura::WindowObserver: |
| 39 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { |
| 40 host_->clipping_window_ = NULL; |
| 41 SetClippingWindow(NULL); |
| 42 } |
| 43 |
| 44 private: |
| 45 NativeViewHostAura* host_; |
| 46 aura::Window* clipping_window_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(ClippingWindowObserver); |
| 49 }; |
| 50 |
16 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) | 51 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) |
17 : host_(host), | 52 : host_(host), |
18 installed_clip_(false) { | 53 installed_clip_(false), |
19 } | 54 clipping_window_(NULL), |
| 55 clipping_window_observer_(new ClippingWindowObserver(this)) {} |
20 | 56 |
21 NativeViewHostAura::~NativeViewHostAura() { | 57 NativeViewHostAura::~NativeViewHostAura() { |
| 58 if (clipping_window_) |
| 59 host_->native_view()->ClearProperty(views::kHostViewKey); |
| 60 |
22 if (host_->native_view()) { | 61 if (host_->native_view()) { |
| 62 host_->native_view()->RemoveObserver(this); |
23 host_->native_view()->ClearProperty(views::kHostViewKey); | 63 host_->native_view()->ClearProperty(views::kHostViewKey); |
24 host_->native_view()->RemoveObserver(this); | |
25 } | 64 } |
26 } | 65 } |
27 | 66 |
28 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
29 // NativeViewHostAura, NativeViewHostWrapper implementation: | 68 // NativeViewHostAura, NativeViewHostWrapper implementation: |
30 void NativeViewHostAura::NativeViewWillAttach() { | 69 void NativeViewHostAura::AttachNativeView() { |
31 host_->native_view()->AddObserver(this); | 70 host_->native_view()->AddObserver(this); |
32 host_->native_view()->SetProperty(views::kHostViewKey, | 71 host_->native_view()->SetProperty(views::kHostViewKey, |
33 static_cast<View*>(host_)); | 72 static_cast<View*>(host_)); |
| 73 AddClippingWindow(); |
34 } | 74 } |
35 | 75 |
36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { | 76 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { |
37 if (!destroyed) { | 77 if (!destroyed) { |
| 78 host_->native_view()->RemoveObserver(this); |
38 host_->native_view()->ClearProperty(views::kHostViewKey); | 79 host_->native_view()->ClearProperty(views::kHostViewKey); |
39 host_->native_view()->RemoveObserver(this); | |
40 host_->native_view()->Hide(); | 80 host_->native_view()->Hide(); |
41 if (host_->native_view()->parent()) | 81 RemoveClippingWindow(); |
42 Widget::ReparentNativeView(host_->native_view(), NULL); | 82 } else { |
| 83 if (clipping_window_) |
| 84 delete clipping_window_; |
43 } | 85 } |
44 } | 86 } |
45 | 87 |
46 void NativeViewHostAura::AddedToWidget() { | 88 void NativeViewHostAura::AddedToWidget() { |
47 if (!host_->native_view()) | 89 if (!host_->native_view()) |
48 return; | 90 return; |
49 | 91 AddClippingWindow(); |
50 aura::Window* widget_window = host_->GetWidget()->GetNativeView(); | |
51 if (host_->native_view()->parent() != widget_window) | |
52 widget_window->AddChild(host_->native_view()); | |
53 if (host_->IsDrawn()) | 92 if (host_->IsDrawn()) |
54 host_->native_view()->Show(); | 93 host_->native_view()->Show(); |
55 else | 94 else |
56 host_->native_view()->Hide(); | 95 host_->native_view()->Hide(); |
57 host_->Layout(); | 96 host_->Layout(); |
58 } | 97 } |
59 | 98 |
60 void NativeViewHostAura::RemovedFromWidget() { | 99 void NativeViewHostAura::RemovedFromWidget() { |
61 if (host_->native_view()) { | 100 if (host_->native_view()) { |
62 host_->native_view()->Hide(); | 101 host_->native_view()->Hide(); |
63 if (host_->native_view()->parent()) | |
64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | |
65 } | 102 } |
| 103 RemoveClippingWindow(); |
66 } | 104 } |
67 | 105 |
68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 106 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
69 // Note that this does not pose a problem functionality wise - it might | 107 installed_clip_ = true; |
70 // however pose a speed degradation if not implemented. | 108 clip_rect_ = gfx::Rect(x + orig_bounds_.x(), |
71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 109 y + orig_bounds_.y(), |
| 110 w, |
| 111 h); |
| 112 UpdateClippingWindow(); |
| 113 clipping_window_->layer()->SetMasksToBounds(true); |
72 } | 114 } |
73 | 115 |
74 bool NativeViewHostAura::HasInstalledClip() { | 116 bool NativeViewHostAura::HasInstalledClip() { |
75 return installed_clip_; | 117 return installed_clip_; |
76 } | 118 } |
77 | 119 |
78 void NativeViewHostAura::UninstallClip() { | 120 void NativeViewHostAura::UninstallClip() { |
| 121 if (installed_clip_ == false) |
| 122 return; |
79 installed_clip_ = false; | 123 installed_clip_ = false; |
| 124 clipping_window_->layer()->SetMasksToBounds(false); |
80 } | 125 } |
81 | 126 |
82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 127 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
83 // TODO: need to support fast resize. | 128 if (host_->fast_resize()) { |
84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 129 gfx::Rect native_view_bounds = host_->native_view()->bounds(); |
| 130 gfx::Point native_view_origin = |
| 131 CalculateNativeViewOrigin(clipping_window_->bounds(), |
| 132 native_view_bounds); |
| 133 orig_bounds_ = gfx::Rect(x + native_view_origin.x(), |
| 134 y + native_view_origin.y(), |
| 135 native_view_bounds.width(), |
| 136 native_view_bounds.height()); |
| 137 |
| 138 InstallClip(x - orig_bounds_.x(), |
| 139 y - orig_bounds_.y(), |
| 140 w, |
| 141 h); |
| 142 } else { |
| 143 clip_rect_.Offset(x - orig_bounds_.x(), y - orig_bounds_.y()); |
| 144 orig_bounds_ = gfx::Rect(x, y, w, h); |
| 145 UpdateClippingWindow(); |
| 146 } |
85 host_->native_view()->Show(); | 147 host_->native_view()->Show(); |
86 } | 148 } |
87 | 149 |
88 void NativeViewHostAura::HideWidget() { | 150 void NativeViewHostAura::HideWidget() { |
89 host_->native_view()->Hide(); | 151 host_->native_view()->Hide(); |
90 } | 152 } |
91 | 153 |
92 void NativeViewHostAura::SetFocus() { | 154 void NativeViewHostAura::SetFocus() { |
93 aura::Window* window = host_->native_view(); | 155 aura::Window* window = host_->native_view(); |
94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 156 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
95 if (client) | 157 if (client) |
96 client->FocusWindow(window); | 158 client->FocusWindow(window); |
97 } | 159 } |
98 | 160 |
99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 161 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
100 return NULL; | 162 return NULL; |
101 } | 163 } |
102 | 164 |
103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 165 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
104 DCHECK(window == host_->native_view()); | 166 DCHECK(window == host_->native_view()); |
105 host_->NativeViewDestroyed(); | 167 host_->NativeViewDestroyed(); |
106 } | 168 } |
107 | 169 |
108 // static | 170 // static |
109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 171 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
110 NativeViewHost* host) { | 172 NativeViewHost* host) { |
111 return new NativeViewHostAura(host); | 173 return new NativeViewHostAura(host); |
112 } | 174 } |
113 | 175 |
| 176 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( |
| 177 const gfx::Rect& input_rect, |
| 178 const gfx::Rect& native_rect) const { |
| 179 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * |
| 180 (input_rect.width() - |
| 181 native_rect.width())); |
| 182 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * |
| 183 (input_rect.height() - |
| 184 native_rect.height())); |
| 185 return gfx::Point(new_x, new_y); |
| 186 } |
| 187 |
| 188 void NativeViewHostAura::AddClippingWindow() { |
| 189 RemoveClippingWindow(); |
| 190 clipping_window_ = new aura::Window(NULL); |
| 191 clipping_window_->SetTransparent(true); |
| 192 clipping_window_->Init(ui::LAYER_NOT_DRAWN); |
| 193 clipping_window_->layer()->set_name("NativeViewHostAuraClip"); |
| 194 clipping_window_->layer()->SetMasksToBounds(false); |
| 195 clipping_window_observer_->SetClippingWindow(clipping_window_); |
| 196 |
| 197 gfx::Rect bounds = host_->native_view()->bounds(); |
| 198 orig_bounds_ = bounds; |
| 199 clipping_window_->SetBounds(bounds); |
| 200 bounds.set_origin(gfx::Point(0, 0)); |
| 201 host_->native_view()->SetBounds(bounds); |
| 202 |
| 203 clipping_window_->AddChild(host_->native_view()); |
| 204 clipping_window_->SetProperty(views::kHostViewKey, |
| 205 static_cast<View*>(host_)); |
| 206 Widget::ReparentNativeView(clipping_window_, |
| 207 host_->GetWidget()->GetNativeView()); |
| 208 clipping_window_->Show(); |
| 209 } |
| 210 |
| 211 void NativeViewHostAura::RemoveClippingWindow() { |
| 212 if (clipping_window_) { |
| 213 if (host_->native_view()->parent() == clipping_window_) { |
| 214 host_->native_view()->SetBounds(clipping_window_->bounds()); |
| 215 if (clipping_window_->parent()) { |
| 216 Widget::ReparentNativeView(host_->native_view(), NULL); |
| 217 } else { |
| 218 clipping_window_->RemoveChild(host_->native_view()); |
| 219 } |
| 220 } |
| 221 delete clipping_window_; |
| 222 } |
| 223 } |
| 224 |
| 225 void NativeViewHostAura::UpdateClippingWindow() { |
| 226 if (!installed_clip_) |
| 227 clip_rect_ = orig_bounds_; |
| 228 |
| 229 clipping_window_->SetBounds(clip_rect_); |
| 230 |
| 231 gfx::Rect native_view_bounds = orig_bounds_; |
| 232 native_view_bounds.Offset(-clip_rect_.x(), -clip_rect_.y()); |
| 233 host_->native_view()->SetBounds(native_view_bounds); |
| 234 } |
| 235 |
114 } // namespace views | 236 } // namespace views |
OLD | NEW |