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), |
| 19 } | 20 clipping_window_(NULL) {} |
| 20 | 21 |
| 21 NativeViewHostAura::~NativeViewHostAura() { | 22 NativeViewHostAura::~NativeViewHostAura() { |
| 23 if (clipping_window_) { | |
| 24 clipping_window_->RemoveObserver(this); | |
| 25 host_->native_view()->ClearProperty(views::kHostViewKey); | |
| 26 } | |
| 22 if (host_->native_view()) { | 27 if (host_->native_view()) { |
| 28 host_->native_view()->RemoveObserver(this); | |
| 23 host_->native_view()->ClearProperty(views::kHostViewKey); | 29 host_->native_view()->ClearProperty(views::kHostViewKey); |
| 24 host_->native_view()->RemoveObserver(this); | |
| 25 } | 30 } |
| 26 } | 31 } |
| 27 | 32 |
| 28 //////////////////////////////////////////////////////////////////////////////// | 33 //////////////////////////////////////////////////////////////////////////////// |
| 29 // NativeViewHostAura, NativeViewHostWrapper implementation: | 34 // NativeViewHostAura, NativeViewHostWrapper implementation: |
| 30 void NativeViewHostAura::NativeViewWillAttach() { | 35 void NativeViewHostAura::AttachNativeView() { |
| 31 host_->native_view()->AddObserver(this); | 36 host_->native_view()->AddObserver(this); |
| 32 host_->native_view()->SetProperty(views::kHostViewKey, | 37 host_->native_view()->SetProperty(views::kHostViewKey, |
| 33 static_cast<View*>(host_)); | 38 static_cast<View*>(host_)); |
| 39 AddClippingWindow(); | |
| 34 } | 40 } |
| 35 | 41 |
| 36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { | 42 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { |
| 37 if (!destroyed) { | 43 if (!destroyed) { |
| 44 host_->native_view()->RemoveObserver(this); | |
| 38 host_->native_view()->ClearProperty(views::kHostViewKey); | 45 host_->native_view()->ClearProperty(views::kHostViewKey); |
| 39 host_->native_view()->RemoveObserver(this); | |
| 40 host_->native_view()->Hide(); | 46 host_->native_view()->Hide(); |
| 41 if (host_->native_view()->parent()) | |
| 42 Widget::ReparentNativeView(host_->native_view(), NULL); | |
| 43 } | 47 } |
| 48 RemoveClippingWindow(); | |
|
sky
2013/10/24 18:16:50
If destroyed is true, I Don't think we should be d
rharrison
2013/10/24 19:28:17
Done.
| |
| 44 } | 49 } |
| 45 | 50 |
| 46 void NativeViewHostAura::AddedToWidget() { | 51 void NativeViewHostAura::AddedToWidget() { |
| 47 if (!host_->native_view()) | 52 if (!host_->native_view()) |
| 48 return; | 53 return; |
| 49 | 54 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()) | 55 if (host_->IsDrawn()) |
| 54 host_->native_view()->Show(); | 56 host_->native_view()->Show(); |
| 55 else | 57 else |
| 56 host_->native_view()->Hide(); | 58 host_->native_view()->Hide(); |
| 57 host_->Layout(); | 59 host_->Layout(); |
| 58 } | 60 } |
| 59 | 61 |
| 60 void NativeViewHostAura::RemovedFromWidget() { | 62 void NativeViewHostAura::RemovedFromWidget() { |
| 61 if (host_->native_view()) { | 63 if (host_->native_view()) { |
| 62 host_->native_view()->Hide(); | 64 host_->native_view()->Hide(); |
| 63 if (host_->native_view()->parent()) | |
| 64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | |
| 65 } | 65 } |
| 66 RemoveClippingWindow(); | |
| 66 } | 67 } |
| 67 | 68 |
| 68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 69 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
| 69 // Note that this does not pose a problem functionality wise - it might | 70 installed_clip_ = true; |
| 70 // however pose a speed degradation if not implemented. | 71 clip_rect_ = gfx::Rect(x + orig_bounds_.x(), |
| 71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 72 y + orig_bounds_.y(), |
| 73 w, | |
| 74 h); | |
| 75 UpdateClippingWindow(); | |
| 76 clipping_window_->layer()->SetMasksToBounds(true); | |
| 72 } | 77 } |
| 73 | 78 |
| 74 bool NativeViewHostAura::HasInstalledClip() { | 79 bool NativeViewHostAura::HasInstalledClip() { |
| 75 return installed_clip_; | 80 return installed_clip_; |
| 76 } | 81 } |
| 77 | 82 |
| 78 void NativeViewHostAura::UninstallClip() { | 83 void NativeViewHostAura::UninstallClip() { |
| 84 if (installed_clip_ == false) | |
| 85 return; | |
| 79 installed_clip_ = false; | 86 installed_clip_ = false; |
| 87 clipping_window_->layer()->SetMasksToBounds(false); | |
| 80 } | 88 } |
| 81 | 89 |
| 82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 90 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
| 83 // TODO: need to support fast resize. | 91 if (host_->fast_resize()) { |
| 84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 92 gfx::Rect native_view_bounds = host_->native_view()->bounds(); |
| 93 gfx::Point native_view_origin = | |
| 94 CalculateNativeViewOrigin(clipping_window_->bounds(), | |
| 95 native_view_bounds); | |
| 96 orig_bounds_ = gfx::Rect(x + native_view_origin.x(), | |
| 97 y + native_view_origin.y(), | |
| 98 native_view_bounds.width(), | |
| 99 native_view_bounds.height()); | |
| 100 | |
| 101 InstallClip(x - orig_bounds_.x(), | |
| 102 y - orig_bounds_.y(), | |
| 103 w, | |
| 104 h); | |
| 105 } else { | |
| 106 clip_rect_.Offset(x - orig_bounds_.x(), y - orig_bounds_.y()); | |
| 107 orig_bounds_ = gfx::Rect(x, y, w, h); | |
| 108 UpdateClippingWindow(); | |
| 109 } | |
| 85 host_->native_view()->Show(); | 110 host_->native_view()->Show(); |
| 86 } | 111 } |
| 87 | 112 |
| 88 void NativeViewHostAura::HideWidget() { | 113 void NativeViewHostAura::HideWidget() { |
| 89 host_->native_view()->Hide(); | 114 host_->native_view()->Hide(); |
| 90 } | 115 } |
| 91 | 116 |
| 92 void NativeViewHostAura::SetFocus() { | 117 void NativeViewHostAura::SetFocus() { |
| 93 aura::Window* window = host_->native_view(); | 118 aura::Window* window = host_->native_view(); |
| 94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 119 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
| 95 if (client) | 120 if (client) |
| 96 client->FocusWindow(window); | 121 client->FocusWindow(window); |
| 97 } | 122 } |
| 98 | 123 |
| 99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 124 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
| 100 return NULL; | 125 return NULL; |
| 101 } | 126 } |
| 102 | 127 |
| 128 void NativeViewHostAura::OnWindowDestroying(aura::Window* window) { | |
| 129 if (window == clipping_window_) { | |
| 130 clipping_window_->RemoveObserver(this); | |
| 131 clipping_window_ = NULL; | |
| 132 } | |
| 133 } | |
| 134 | |
| 103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 135 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
| 104 DCHECK(window == host_->native_view()); | 136 if (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 RemoveClippingWindow(); | |
| 160 clipping_window_ = new aura::Window(NULL); | |
| 161 clipping_window_->SetTransparent(true); | |
| 162 clipping_window_->Init(ui::LAYER_NOT_DRAWN); | |
| 163 clipping_window_->layer()->set_name("NativeViewHostAuraClip"); | |
| 164 clipping_window_->layer()->SetMasksToBounds(false); | |
| 165 clipping_window_->AddObserver(this); | |
|
sky
2013/10/24 18:16:50
Having the same object be a listener for two diffe
rharrison
2013/10/24 19:28:17
Done.
rharrison
2013/10/24 19:28:17
Done.
| |
| 166 | |
| 167 gfx::Rect bounds = host_->native_view()->bounds(); | |
| 168 orig_bounds_ = bounds; | |
| 169 clipping_window_->SetBounds(bounds); | |
| 170 bounds.set_origin(gfx::Point(0, 0)); | |
| 171 host_->native_view()->SetBounds(bounds); | |
| 172 | |
| 173 clipping_window_->AddChild(host_->native_view()); | |
| 174 clipping_window_->SetProperty(views::kHostViewKey, | |
| 175 static_cast<View*>(host_)); | |
| 176 Widget::ReparentNativeView(clipping_window_, | |
| 177 host_->GetWidget()->GetNativeView()); | |
| 178 clipping_window_->Show(); | |
| 179 } | |
| 180 | |
| 181 void NativeViewHostAura::RemoveClippingWindow() { | |
| 182 if (clipping_window_) { | |
| 183 if (host_->native_view()->parent() == clipping_window_) { | |
| 184 host_->native_view()->SetBounds(clipping_window_->bounds()); | |
| 185 if (clipping_window_->parent()) { | |
| 186 Widget::ReparentNativeView(host_->native_view(), NULL); | |
| 187 } else { | |
| 188 clipping_window_->RemoveChild(host_->native_view()); | |
| 189 } | |
| 190 } | |
| 191 delete clipping_window_; | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 void NativeViewHostAura::UpdateClippingWindow() { | |
| 196 if (!installed_clip_) | |
| 197 clip_rect_ = orig_bounds_; | |
| 198 | |
| 199 clipping_window_->SetBounds(clip_rect_); | |
| 200 | |
| 201 gfx::Rect native_view_bounds = orig_bounds_; | |
| 202 native_view_bounds.Offset(-clip_rect_.x(), -clip_rect_.y()); | |
| 203 host_->native_view()->SetBounds(native_view_bounds); | |
| 204 } | |
| 205 | |
| 114 } // namespace views | 206 } // namespace views |
| OLD | NEW |