Chromium Code Reviews| 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 |
| 16 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) | 17 NativeViewHostAura::NativeViewHostAura(NativeViewHost* host) |
| 17 : host_(host), | 18 : host_(host), |
| 18 installed_clip_(false) { | 19 installed_clip_(false), |
| 20 clipping_window_(NULL) { | |
| 21 clipping_window_.SetTransparent(true); | |
|
sky
2013/10/22 19:57:32
What necessitated all these changes?
xiyuan
2013/10/22 20:12:46
This is to fix the views_unittest memory leak. The
sky
2013/10/22 21:32:18
Can you add a unit test to verify we don't leak?
rharrison
2013/10/24 14:40:15
Done.
| |
| 22 clipping_window_.Init(ui::LAYER_NOT_DRAWN); | |
| 23 clipping_window_.set_owned_by_parent(false); | |
| 24 clipping_window_.layer()->set_name("NativeViewHostAuraClip"); | |
| 25 clipping_window_.layer()->SetMasksToBounds(false); | |
| 19 } | 26 } |
| 20 | 27 |
| 21 NativeViewHostAura::~NativeViewHostAura() { | 28 NativeViewHostAura::~NativeViewHostAura() { |
| 22 if (host_->native_view()) { | 29 if (host_->native_view()) { |
| 23 host_->native_view()->ClearProperty(views::kHostViewKey); | 30 host_->native_view()->ClearProperty(views::kHostViewKey); |
| 24 host_->native_view()->RemoveObserver(this); | 31 host_->native_view()->RemoveObserver(this); |
| 32 RemoveClippingWindow(); | |
| 25 } | 33 } |
| 26 } | 34 } |
| 27 | 35 |
| 28 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
| 29 // NativeViewHostAura, NativeViewHostWrapper implementation: | 37 // NativeViewHostAura, NativeViewHostWrapper implementation: |
| 30 void NativeViewHostAura::NativeViewWillAttach() { | 38 void NativeViewHostAura::AttachNativeView() { |
| 31 host_->native_view()->AddObserver(this); | 39 host_->native_view()->AddObserver(this); |
| 32 host_->native_view()->SetProperty(views::kHostViewKey, | 40 host_->native_view()->SetProperty(views::kHostViewKey, |
| 33 static_cast<View*>(host_)); | 41 static_cast<View*>(host_)); |
| 42 AddClippingWindow(); | |
| 34 } | 43 } |
| 35 | 44 |
| 36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { | 45 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { |
| 37 if (!destroyed) { | 46 if (!destroyed) { |
| 47 RemoveClippingWindow(); | |
| 38 host_->native_view()->ClearProperty(views::kHostViewKey); | 48 host_->native_view()->ClearProperty(views::kHostViewKey); |
| 39 host_->native_view()->RemoveObserver(this); | 49 host_->native_view()->RemoveObserver(this); |
| 40 host_->native_view()->Hide(); | 50 host_->native_view()->Hide(); |
| 41 if (host_->native_view()->parent()) | 51 if (host_->native_view()->parent()) |
| 42 Widget::ReparentNativeView(host_->native_view(), NULL); | 52 Widget::ReparentNativeView(host_->native_view(), NULL); |
| 53 } else { | |
| 54 clipping_window_.Hide(); | |
| 55 if (clipping_window_.parent()) | |
| 56 clipping_window_.parent()->RemoveChild(&clipping_window_); | |
| 43 } | 57 } |
| 44 } | 58 } |
| 45 | 59 |
| 46 void NativeViewHostAura::AddedToWidget() { | 60 void NativeViewHostAura::AddedToWidget() { |
| 47 if (!host_->native_view()) | 61 if (!host_->native_view()) |
| 48 return; | 62 return; |
| 49 | 63 |
| 50 aura::Window* widget_window = host_->GetWidget()->GetNativeView(); | 64 if (host_->native_view()->parent() != host_->GetWidget()->GetNativeView()) |
| 51 if (host_->native_view()->parent() != widget_window) | 65 AddClippingWindow(); |
| 52 widget_window->AddChild(host_->native_view()); | 66 |
| 53 if (host_->IsDrawn()) | 67 if (host_->IsDrawn()) |
| 54 host_->native_view()->Show(); | 68 host_->native_view()->Show(); |
| 55 else | 69 else |
| 56 host_->native_view()->Hide(); | 70 host_->native_view()->Hide(); |
| 57 host_->Layout(); | 71 host_->Layout(); |
| 58 } | 72 } |
| 59 | 73 |
| 60 void NativeViewHostAura::RemovedFromWidget() { | 74 void NativeViewHostAura::RemovedFromWidget() { |
| 61 if (host_->native_view()) { | 75 if (host_->native_view()) { |
| 62 host_->native_view()->Hide(); | 76 host_->native_view()->Hide(); |
| 77 RemoveClippingWindow(); | |
| 63 if (host_->native_view()->parent()) | 78 if (host_->native_view()->parent()) |
| 64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | 79 host_->native_view()->parent()->RemoveChild(host_->native_view()); |
| 65 } | 80 } |
| 66 } | 81 } |
| 67 | 82 |
| 68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 83 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
| 69 // Note that this does not pose a problem functionality wise - it might | 84 installed_clip_ = true; |
| 70 // however pose a speed degradation if not implemented. | 85 clip_rect_ = gfx::Rect(x + orig_bounds_.origin().x(), |
| 71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 86 y + orig_bounds_.origin().y(), |
| 87 w, | |
| 88 h); | |
| 89 UpdateClippingWindow(); | |
| 90 clipping_window_.layer()->SetMasksToBounds(true); | |
| 72 } | 91 } |
| 73 | 92 |
| 74 bool NativeViewHostAura::HasInstalledClip() { | 93 bool NativeViewHostAura::HasInstalledClip() { |
| 75 return installed_clip_; | 94 return installed_clip_; |
| 76 } | 95 } |
| 77 | 96 |
| 78 void NativeViewHostAura::UninstallClip() { | 97 void NativeViewHostAura::UninstallClip() { |
| 98 if (installed_clip_ == false) | |
| 99 return; | |
| 79 installed_clip_ = false; | 100 installed_clip_ = false; |
| 101 clipping_window_.layer()->SetMasksToBounds(false); | |
| 80 } | 102 } |
| 81 | 103 |
| 82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 104 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
| 83 // TODO: need to support fast resize. | 105 if (host_->fast_resize()) { |
| 84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 106 UninstallClip(); |
| 107 InstallClip(x - orig_bounds_.origin().x(), | |
| 108 y - orig_bounds_.origin().y(), | |
| 109 w, | |
| 110 h); | |
| 111 } else { | |
| 112 clip_rect_.Offset(x - orig_bounds_.origin().x(), | |
| 113 y - orig_bounds_.origin().y()); | |
| 114 orig_bounds_ = gfx::Rect(x, y, w, h); | |
| 115 UpdateClippingWindow(); | |
| 116 } | |
| 85 host_->native_view()->Show(); | 117 host_->native_view()->Show(); |
| 86 } | 118 } |
| 87 | 119 |
| 88 void NativeViewHostAura::HideWidget() { | 120 void NativeViewHostAura::HideWidget() { |
| 89 host_->native_view()->Hide(); | 121 host_->native_view()->Hide(); |
| 90 } | 122 } |
| 91 | 123 |
| 92 void NativeViewHostAura::SetFocus() { | 124 void NativeViewHostAura::SetFocus() { |
| 93 aura::Window* window = host_->native_view(); | 125 aura::Window* window = host_->native_view(); |
| 94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 126 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
| 95 if (client) | 127 if (client) |
| 96 client->FocusWindow(window); | 128 client->FocusWindow(window); |
| 97 } | 129 } |
| 98 | 130 |
| 99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 131 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
| 100 return NULL; | 132 return NULL; |
| 101 } | 133 } |
| 102 | 134 |
| 103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 135 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
| 104 DCHECK(window == host_->native_view()); | 136 DCHECK(window == host_->native_view()); |
| 105 host_->NativeViewDestroyed(); | 137 host_->NativeViewDestroyed(); |
| 106 } | 138 } |
| 107 | 139 |
| 108 // static | 140 // static |
| 109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 141 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
| 110 NativeViewHost* host) { | 142 NativeViewHost* host) { |
| 111 return new NativeViewHostAura(host); | 143 return new NativeViewHostAura(host); |
| 112 } | 144 } |
| 113 | 145 |
| 146 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( | |
| 147 const gfx::Rect& input_rect, | |
| 148 const gfx::Rect& native_rect) const { | |
| 149 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * | |
| 150 (input_rect.width() - | |
| 151 native_rect.width())); | |
| 152 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * | |
| 153 (input_rect.height() - | |
| 154 native_rect.height())); | |
| 155 return gfx::Point(new_x, new_y); | |
| 156 } | |
| 157 | |
| 158 void NativeViewHostAura::AddClippingWindow() { | |
| 159 gfx::Rect bounds = host_->native_view()->bounds(); | |
| 160 orig_bounds_ = bounds; | |
| 161 | |
| 162 if (host_->GetWidget()->GetNativeView()) { | |
| 163 Widget::ReparentNativeView(&clipping_window_, | |
| 164 host_->GetWidget()->GetNativeView()); | |
| 165 } | |
| 166 Widget::ReparentNativeView(host_->native_view(), | |
| 167 &clipping_window_); | |
| 168 | |
| 169 clipping_window_.SetBounds(bounds); | |
| 170 bounds.set_origin(gfx::Point(0, 0)); | |
| 171 host_->native_view()->SetBounds(bounds); | |
| 172 clipping_window_.Show(); | |
| 173 } | |
| 174 | |
| 175 void NativeViewHostAura::RemoveClippingWindow() { | |
| 176 if (host_->native_view()->parent() == &clipping_window_) { | |
| 177 if (host_->GetWidget()->GetNativeView()) { | |
| 178 Widget::ReparentNativeView(host_->native_view(), | |
| 179 host_->GetWidget()->GetNativeView()); | |
| 180 } else { | |
| 181 clipping_window_.RemoveChild(host_->native_view()); | |
| 182 } | |
| 183 host_->native_view()->SetBounds(clipping_window_.bounds()); | |
| 184 } | |
| 185 clipping_window_.Hide(); | |
| 186 if (clipping_window_.parent()) | |
| 187 clipping_window_.parent()->RemoveChild(&clipping_window_); | |
| 188 } | |
| 189 | |
| 190 void NativeViewHostAura::UpdateClippingWindow() { | |
| 191 if (installed_clip_) { | |
| 192 clipping_window_.SetBounds(clip_rect_); | |
| 193 gfx::Rect native_view_bounds = host_->native_view()->bounds(); | |
| 194 gfx::Point native_view_origin = | |
| 195 CalculateNativeViewOrigin(clipping_window_.bounds(), | |
| 196 native_view_bounds); | |
| 197 | |
| 198 native_view_bounds.set_origin(native_view_origin); | |
| 199 host_->native_view()->SetBounds(native_view_bounds); | |
| 200 } else { | |
| 201 clip_rect_ = orig_bounds_; | |
| 202 clipping_window_.SetBounds(orig_bounds_); | |
| 203 host_->native_view()->SetBounds(gfx::Rect(orig_bounds_.size())); | |
| 204 } | |
| 205 } | |
| 206 | |
| 114 } // namespace views | 207 } // namespace views |
| OLD | NEW |