Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: ui/views/controls/native/native_view_host_mac.mm

Issue 1796773003: Implement NativeWidgetMac::ReorderNativeViews (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implement NativeWidgetMac::ReorderNativeViews Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/views/views.gyp » ('j') | ui/views/widget/widget_mac_utils.mm » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #import <objc/runtime.h>
8 9
9 #include "base/logging.h"
10 #include "base/mac/foundation_util.h"
11 #import "ui/views/cocoa/bridged_content_view.h"
12 #include "ui/views/controls/native/native_view_host.h" 10 #include "ui/views/controls/native/native_view_host.h"
13 #include "ui/views/widget/widget.h" 11 #include "ui/views/widget/widget.h"
12 #include "ui/views/widget/widget_mac_utils.h"
14 13
15 namespace views { 14 namespace views {
16 namespace { 15 namespace {
17 16
18 // Reparents |native_view| to be a child of the native content view of 17 // Reparents |native_view| to be a child of the native content view of
19 // |new_parent|. 18 // |host| widget.
20 void ReparentNSView(NSView* native_view, Widget* new_parent) { 19 void ReparentNSView(NSView* native_view, NativeViewHost* host) {
21 DCHECK(native_view); 20 DCHECK(native_view);
22 // Mac's NativeViewHost has no support for hosting its own child widgets. 21 // Mac's NativeViewHost has no support for hosting its own child widgets.
23 // This check is probably overly restrictive, since the Widget containing the 22 // This check is probably overly restrictive, since the Widget containing the
24 // NativeViewHost _is_ allowed child Widgets. However, we don't know yet 23 // NativeViewHost _is_ allowed child Widgets. However, we don't know yet
25 // whether those child Widgets need to be distinguished from Widgets that code 24 // whether those child Widgets need to be distinguished from Widgets that code
26 // might want to associate with the hosted NSView instead. 25 // might want to associate with the hosted NSView instead.
27 { 26 {
28 Widget::Widgets child_widgets; 27 Widget::Widgets child_widgets;
29 Widget::GetAllChildWidgets(native_view, &child_widgets); 28 Widget::GetAllChildWidgets(native_view, &child_widgets);
30 CHECK_GE(1u, child_widgets.size()); // 1 (itself) or 0 if detached. 29 CHECK_GE(1u, child_widgets.size()); // 1 (itself) or 0 if detached.
31 } 30 }
32 31
33 if (!new_parent) { 32 if (!host) {
34 [native_view removeFromSuperview]; 33 [native_view removeFromSuperview];
35 return; 34 return;
36 } 35 }
37 36 AttachNSViewRelatedToHost(native_view, host);
38 BridgedContentView* new_superview =
39 base::mac::ObjCCastStrict<BridgedContentView>(
40 new_parent->GetNativeView());
41 DCHECK(new_superview);
42 [new_superview addSubview:native_view];
43 } 37 }
44 38
45 } // namespace 39 } // namespace
46 40
47 NativeViewHostMac::NativeViewHostMac(NativeViewHost* host) : host_(host) { 41 NativeViewHostMac::NativeViewHostMac(NativeViewHost* host) : host_(host) {
48 } 42 }
49 43
50 NativeViewHostMac::~NativeViewHostMac() { 44 NativeViewHostMac::~NativeViewHostMac() {
51 } 45 }
52 46
53 //////////////////////////////////////////////////////////////////////////////// 47 ////////////////////////////////////////////////////////////////////////////////
54 // NativeViewHostMac, NativeViewHostWrapper implementation: 48 // NativeViewHostMac, NativeViewHostWrapper implementation:
55 49
56 void NativeViewHostMac::AttachNativeView() { 50 void NativeViewHostMac::AttachNativeView() {
57 DCHECK(host_->native_view()); 51 DCHECK(host_->native_view());
58 DCHECK(!native_view_); 52 DCHECK(!native_view_);
59 native_view_.reset([host_->native_view() retain]); 53 native_view_.reset([host_->native_view() retain]);
60 ReparentNSView(host_->native_view(), host_->GetWidget()); 54 ReparentNSView(host_->native_view(), host_);
55 objc_setAssociatedObject(native_view_.get(), &kAssociatedViewHostKey,
56 [NSValue valueWithPointer:host_],
57 OBJC_ASSOCIATION_RETAIN_NONATOMIC);
61 } 58 }
62 59
63 void NativeViewHostMac::NativeViewDetaching(bool destroyed) { 60 void NativeViewHostMac::NativeViewDetaching(bool destroyed) {
64 // |destroyed| is only true if this class calls host_->NativeViewDestroyed(). 61 // |destroyed| is only true if this class calls host_->NativeViewDestroyed().
65 // Aura does this after observing an aura OnWindowDestroying, but NSViews 62 // Aura does this after observing an aura OnWindowDestroying, but NSViews
66 // are reference counted so there isn't a reliable signal. Instead, a 63 // are reference counted so there isn't a reliable signal. Instead, a
67 // reference is retained until the NativeViewHost is detached. 64 // reference is retained until the NativeViewHost is detached.
68 DCHECK(!destroyed); 65 DCHECK(!destroyed);
69 // |native_view_| can be nil here if RemovedFromWidget() is called before 66 // |native_view_| can be nil here if RemovedFromWidget() is called before
70 // NativeViewHost::Detach(). 67 // NativeViewHost::Detach().
71 DCHECK(!native_view_ || native_view_ == host_->native_view()); 68 DCHECK(!native_view_ || native_view_ == host_->native_view());
72 [host_->native_view() setHidden:YES]; 69 [host_->native_view() setHidden:YES];
73 ReparentNSView(host_->native_view(), NULL); 70 ReparentNSView(host_->native_view(), nullptr);
71 objc_setAssociatedObject(native_view_.get(), &kAssociatedViewHostKey, nil,
72 OBJC_ASSOCIATION_RETAIN_NONATOMIC);
74 native_view_.reset(); 73 native_view_.reset();
75 } 74 }
76 75
77 void NativeViewHostMac::AddedToWidget() { 76 void NativeViewHostMac::AddedToWidget() {
78 if (!host_->native_view()) 77 if (!host_->native_view())
79 return; 78 return;
80 79
81 AttachNativeView(); 80 AttachNativeView();
82 host_->Layout(); 81 host_->Layout();
83 } 82 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 return gfx::kNullCursor; 148 return gfx::kNullCursor;
150 } 149 }
151 150
152 // static 151 // static
153 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( 152 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper(
154 NativeViewHost* host) { 153 NativeViewHost* host) {
155 return new NativeViewHostMac(host); 154 return new NativeViewHostMac(host);
156 } 155 }
157 156
158 } // namespace views 157 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/views.gyp » ('j') | ui/views/widget/widget_mac_utils.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698