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