Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_mac.h" | 5 #include "ui/views/controls/native/native_view_host_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | |
| 10 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 11 #import "ui/views/cocoa/bridged_content_view.h" | 10 #import "ui/views/cocoa/bridged_native_widget.h" |
| 12 #include "ui/views/controls/native/native_view_host.h" | 11 #include "ui/views/controls/native/native_view_host.h" |
| 12 #include "ui/views/widget/native_widget_mac.h" | |
| 13 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
| 14 | 14 |
| 15 namespace views { | 15 namespace views { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Reparents |native_view| to be a child of the native content view of | 18 void EnsureNativeViewHasNoChildWidgets(NSView* native_view) { |
| 19 // |new_parent|. | |
| 20 void ReparentNSView(NSView* native_view, Widget* new_parent) { | |
| 21 DCHECK(native_view); | 19 DCHECK(native_view); |
| 22 // Mac's NativeViewHost has no support for hosting its own child widgets. | 20 // Mac's NativeViewHost has no support for hosting its own child widgets. |
| 23 // This check is probably overly restrictive, since the Widget containing the | 21 // This check is probably overly restrictive, since the Widget containing the |
| 24 // NativeViewHost _is_ allowed child Widgets. However, we don't know yet | 22 // NativeViewHost _is_ allowed child Widgets. However, we don't know yet |
| 25 // whether those child Widgets need to be distinguished from Widgets that code | 23 // whether those child Widgets need to be distinguished from Widgets that code |
| 26 // might want to associate with the hosted NSView instead. | 24 // might want to associate with the hosted NSView instead. |
| 27 { | 25 { |
| 28 Widget::Widgets child_widgets; | 26 Widget::Widgets child_widgets; |
| 29 Widget::GetAllChildWidgets(native_view, &child_widgets); | 27 Widget::GetAllChildWidgets(native_view, &child_widgets); |
| 30 CHECK_GE(1u, child_widgets.size()); // 1 (itself) or 0 if detached. | 28 CHECK_GE(1u, child_widgets.size()); // 1 (itself) or 0 if detached. |
| 31 } | 29 } |
| 32 | |
| 33 if (!new_parent) { | |
| 34 [native_view removeFromSuperview]; | |
| 35 return; | |
| 36 } | |
| 37 | |
| 38 BridgedContentView* new_superview = | |
| 39 base::mac::ObjCCastStrict<BridgedContentView>( | |
| 40 new_parent->GetNativeView()); | |
| 41 DCHECK(new_superview); | |
| 42 [new_superview addSubview:native_view]; | |
| 43 } | 30 } |
| 44 | 31 |
| 45 } // namespace | 32 } // namespace |
| 46 | 33 |
| 47 NativeViewHostMac::NativeViewHostMac(NativeViewHost* host) : host_(host) { | 34 NativeViewHostMac::NativeViewHostMac(NativeViewHost* host) : host_(host) { |
| 48 } | 35 } |
| 49 | 36 |
| 50 NativeViewHostMac::~NativeViewHostMac() { | 37 NativeViewHostMac::~NativeViewHostMac() { |
| 51 } | 38 } |
| 52 | 39 |
| 53 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
| 54 // NativeViewHostMac, NativeViewHostWrapper implementation: | 41 // NativeViewHostMac, NativeViewHostWrapper implementation: |
| 55 | 42 |
| 56 void NativeViewHostMac::AttachNativeView() { | 43 void NativeViewHostMac::AttachNativeView() { |
| 57 DCHECK(host_->native_view()); | 44 DCHECK(host_->native_view()); |
| 58 DCHECK(!native_view_); | 45 DCHECK(!native_view_); |
| 59 native_view_.reset([host_->native_view() retain]); | 46 native_view_.reset([host_->native_view() retain]); |
| 60 ReparentNSView(host_->native_view(), host_->GetWidget()); | 47 |
| 48 EnsureNativeViewHasNoChildWidgets(native_view_.get()); | |
|
tapted
2016/03/17 23:04:15
nit: .get() shouldn't be required
| |
| 49 [host_->GetWidget()->GetNativeView() addSubview:native_view_.get()]; | |
|
tapted
2016/03/17 23:04:15
Yep - I like this change. But can you move this be
| |
| 50 BridgedNativeWidget* bridge = NativeWidgetMac::GetBridgeForNativeWindow( | |
| 51 host_->GetWidget()->GetNativeWindow()); | |
| 52 DCHECK(bridge); | |
| 53 bridge->SetAssociationForView(host_, native_view_.get()); | |
|
tapted
2016/03/17 23:04:15
nit: no .get()
| |
| 61 } | 54 } |
| 62 | 55 |
| 63 void NativeViewHostMac::NativeViewDetaching(bool destroyed) { | 56 void NativeViewHostMac::NativeViewDetaching(bool destroyed) { |
| 64 // |destroyed| is only true if this class calls host_->NativeViewDestroyed(). | 57 // |destroyed| is only true if this class calls host_->NativeViewDestroyed(). |
| 65 // Aura does this after observing an aura OnWindowDestroying, but NSViews | 58 // Aura does this after observing an aura OnWindowDestroying, but NSViews |
| 66 // are reference counted so there isn't a reliable signal. Instead, a | 59 // are reference counted so there isn't a reliable signal. Instead, a |
| 67 // reference is retained until the NativeViewHost is detached. | 60 // reference is retained until the NativeViewHost is detached. |
| 68 DCHECK(!destroyed); | 61 DCHECK(!destroyed); |
| 69 // |native_view_| can be nil here if RemovedFromWidget() is called before | 62 // |native_view_| can be nil here if RemovedFromWidget() is called before |
| 70 // NativeViewHost::Detach(). | 63 // NativeViewHost::Detach(). |
| 71 DCHECK(!native_view_ || native_view_ == host_->native_view()); | 64 DCHECK(!native_view_ || native_view_ == host_->native_view()); |
| 72 [host_->native_view() setHidden:YES]; | 65 [host_->native_view() setHidden:YES]; |
| 73 ReparentNSView(host_->native_view(), NULL); | 66 [host_->native_view() removeFromSuperview]; |
| 67 | |
| 68 EnsureNativeViewHasNoChildWidgets(host_->native_view()); | |
| 69 BridgedNativeWidget* bridge = NativeWidgetMac::GetBridgeForNativeWindow( | |
| 70 host_->GetWidget()->GetNativeWindow()); | |
| 71 // BridgedNativeWidget can be null when Widget is closing. | |
| 72 if (bridge) { | |
|
tapted
2016/03/17 23:04:15
nit: curlies not required
| |
| 73 bridge->ClearAssociationForView(host_); | |
| 74 } | |
| 75 | |
| 74 native_view_.reset(); | 76 native_view_.reset(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void NativeViewHostMac::AddedToWidget() { | 79 void NativeViewHostMac::AddedToWidget() { |
| 78 if (!host_->native_view()) | 80 if (!host_->native_view()) |
| 79 return; | 81 return; |
| 80 | 82 |
| 81 AttachNativeView(); | 83 AttachNativeView(); |
| 82 host_->Layout(); | 84 host_->Layout(); |
| 83 } | 85 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 return gfx::kNullCursor; | 151 return gfx::kNullCursor; |
| 150 } | 152 } |
| 151 | 153 |
| 152 // static | 154 // static |
| 153 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 155 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
| 154 NativeViewHost* host) { | 156 NativeViewHost* host) { |
| 155 return new NativeViewHostMac(host); | 157 return new NativeViewHostMac(host); |
| 156 } | 158 } |
| 157 | 159 |
| 158 } // namespace views | 160 } // namespace views |
| OLD | NEW |