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. | |
sky
2013/11/19 21:00:09
Please add more description. In particular as Nati
rharrison
2013/11/20 15:50:13
Done.
| |
19 class ClippingWindowObserver : public aura::WindowObserver { | |
20 public: | |
21 explicit ClippingWindowObserver(NativeViewHostAura* host) : | |
sky
2013/11/19 21:00:09
nit: ':' on next line.
rharrison
2013/11/20 15:50:13
Done.
| |
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); | |
sky
2013/11/19 21:00:09
nit: unless there is a compelling reason, keep the
rharrison
2013/11/20 15:50:13
Done.
| |
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_; | |
85 clipping_window_ = NULL; | |
sky
2013/11/19 21:00:09
Should this be a DCHECK?
rharrison
2013/11/20 15:50:13
Talked with sadrul about this, I now understand wh
| |
86 } | |
43 } | 87 } |
88 DCHECK(!clipping_window_); | |
44 } | 89 } |
45 | 90 |
46 void NativeViewHostAura::AddedToWidget() { | 91 void NativeViewHostAura::AddedToWidget() { |
47 if (!host_->native_view()) | 92 if (!host_->native_view()) |
48 return; | 93 return; |
49 | 94 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()) | 95 if (host_->IsDrawn()) |
54 host_->native_view()->Show(); | 96 host_->native_view()->Show(); |
55 else | 97 else |
56 host_->native_view()->Hide(); | 98 host_->native_view()->Hide(); |
57 host_->Layout(); | 99 host_->Layout(); |
58 } | 100 } |
59 | 101 |
60 void NativeViewHostAura::RemovedFromWidget() { | 102 void NativeViewHostAura::RemovedFromWidget() { |
61 if (host_->native_view()) { | 103 if (host_->native_view()) { |
62 host_->native_view()->Hide(); | 104 host_->native_view()->Hide(); |
63 if (host_->native_view()->parent()) | |
64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | |
65 } | 105 } |
106 RemoveClippingWindow(); | |
66 } | 107 } |
67 | 108 |
68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 109 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
69 // Note that this does not pose a problem functionality wise - it might | 110 installed_clip_ = true; |
70 // however pose a speed degradation if not implemented. | 111 clip_rect_ = gfx::Rect(x + orig_bounds_.x(), |
71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 112 y + orig_bounds_.y(), |
113 w, | |
114 h); | |
115 UpdateClippingWindow(); | |
116 clipping_window_->layer()->SetMasksToBounds(true); | |
72 } | 117 } |
73 | 118 |
74 bool NativeViewHostAura::HasInstalledClip() { | 119 bool NativeViewHostAura::HasInstalledClip() { |
75 return installed_clip_; | 120 return installed_clip_; |
76 } | 121 } |
77 | 122 |
78 void NativeViewHostAura::UninstallClip() { | 123 void NativeViewHostAura::UninstallClip() { |
124 if (installed_clip_ == false) | |
125 return; | |
79 installed_clip_ = false; | 126 installed_clip_ = false; |
127 clipping_window_->layer()->SetMasksToBounds(false); | |
80 } | 128 } |
81 | 129 |
82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 130 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
83 // TODO: need to support fast resize. | 131 if (host_->fast_resize()) { |
84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 132 gfx::Rect native_view_bounds = host_->native_view()->bounds(); |
133 gfx::Point native_view_origin = | |
134 CalculateNativeViewOrigin(clipping_window_->bounds(), | |
135 native_view_bounds); | |
136 orig_bounds_ = gfx::Rect(x + native_view_origin.x(), | |
137 y + native_view_origin.y(), | |
138 native_view_bounds.width(), | |
139 native_view_bounds.height()); | |
140 | |
141 InstallClip(x - orig_bounds_.x(), | |
142 y - orig_bounds_.y(), | |
143 w, | |
144 h); | |
145 } else { | |
146 clip_rect_.Offset(x - orig_bounds_.x(), y - orig_bounds_.y()); | |
147 orig_bounds_ = gfx::Rect(x, y, w, h); | |
148 UpdateClippingWindow(); | |
149 } | |
85 host_->native_view()->Show(); | 150 host_->native_view()->Show(); |
86 } | 151 } |
87 | 152 |
88 void NativeViewHostAura::HideWidget() { | 153 void NativeViewHostAura::HideWidget() { |
89 host_->native_view()->Hide(); | 154 host_->native_view()->Hide(); |
90 } | 155 } |
91 | 156 |
92 void NativeViewHostAura::SetFocus() { | 157 void NativeViewHostAura::SetFocus() { |
93 aura::Window* window = host_->native_view(); | 158 aura::Window* window = host_->native_view(); |
94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 159 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
95 if (client) | 160 if (client) |
96 client->FocusWindow(window); | 161 client->FocusWindow(window); |
97 } | 162 } |
98 | 163 |
99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 164 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
100 return NULL; | 165 return NULL; |
101 } | 166 } |
102 | 167 |
103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 168 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
104 DCHECK(window == host_->native_view()); | 169 DCHECK(window == host_->native_view()); |
105 host_->NativeViewDestroyed(); | 170 host_->NativeViewDestroyed(); |
106 } | 171 } |
107 | 172 |
108 // static | 173 // static |
109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 174 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
110 NativeViewHost* host) { | 175 NativeViewHost* host) { |
111 return new NativeViewHostAura(host); | 176 return new NativeViewHostAura(host); |
112 } | 177 } |
113 | 178 |
179 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( | |
180 const gfx::Rect& input_rect, | |
181 const gfx::Rect& native_rect) const { | |
182 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * | |
183 (input_rect.width() - | |
184 native_rect.width())); | |
185 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * | |
186 (input_rect.height() - | |
187 native_rect.height())); | |
188 return gfx::Point(new_x, new_y); | |
189 } | |
190 | |
191 void NativeViewHostAura::AddClippingWindow() { | |
192 RemoveClippingWindow(); | |
193 clipping_window_ = new aura::Window(NULL); | |
194 clipping_window_->SetTransparent(true); | |
195 clipping_window_->Init(ui::LAYER_NOT_DRAWN); | |
196 clipping_window_->SetName("NativeViewHostAuraClip"); | |
197 clipping_window_->layer()->set_name("NativeViewHostAuraClip"); | |
sky
2013/11/19 21:00:09
Isn't the name of the layer already updated for yo
rharrison
2013/11/20 15:50:13
Done.
| |
198 clipping_window_->layer()->SetMasksToBounds(false); | |
199 clipping_window_observer_->SetClippingWindow(clipping_window_); | |
200 | |
201 gfx::Rect bounds = host_->native_view()->bounds(); | |
202 orig_bounds_ = bounds; | |
203 clipping_window_->SetBounds(bounds); | |
204 bounds.set_origin(gfx::Point(0, 0)); | |
205 host_->native_view()->SetBounds(bounds); | |
206 | |
207 clipping_window_->AddChild(host_->native_view()); | |
208 clipping_window_->SetProperty(views::kHostViewKey, | |
sky
2013/11/19 21:00:09
Shouldn't this only be set on the native_view()?
rharrison
2013/11/20 15:50:13
Done.
| |
209 static_cast<View*>(host_)); | |
210 Widget::ReparentNativeView(clipping_window_, | |
211 host_->GetWidget()->GetNativeView()); | |
212 clipping_window_->Show(); | |
213 host_->native_view()->SetName("ClippedContent"); | |
214 host_->native_view()->layer()->set_name("ClippedContent"); | |
sky
2013/11/19 21:00:09
Same comment about name.
rharrison
2013/11/20 15:50:13
Done.
| |
215 } | |
216 | |
217 void NativeViewHostAura::RemoveClippingWindow() { | |
218 if (clipping_window_) { | |
219 if (host_->native_view()->parent() == clipping_window_) { | |
220 host_->native_view()->SetBounds(clipping_window_->bounds()); | |
221 if (clipping_window_->parent()) { | |
222 Widget::ReparentNativeView(host_->native_view(), NULL); | |
223 } else { | |
224 clipping_window_->RemoveChild(host_->native_view()); | |
225 } | |
226 } | |
227 delete clipping_window_; | |
228 clipping_window_ = NULL; | |
sky
2013/11/19 21:00:09
DCHECK?
rharrison
2013/11/20 15:50:13
Done.
| |
229 } | |
230 DCHECK(!clipping_window_); | |
231 } | |
232 | |
233 void NativeViewHostAura::UpdateClippingWindow() { | |
234 if (!installed_clip_) | |
235 clip_rect_ = orig_bounds_; | |
236 | |
237 clipping_window_->SetBounds(clip_rect_); | |
238 | |
239 gfx::Rect native_view_bounds = orig_bounds_; | |
240 native_view_bounds.Offset(-clip_rect_.x(), -clip_rect_.y()); | |
241 host_->native_view()->SetBounds(native_view_bounds); | |
242 } | |
243 | |
114 } // namespace views | 244 } // namespace views |
OLD | NEW |