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/focus_manager.h" | 8 #include "ui/aura/focus_manager.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); | |
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 | |
43 AddClippingWindow(); | |
34 } | 44 } |
35 | 45 |
36 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { | 46 void NativeViewHostAura::NativeViewDetaching(bool destroyed) { |
47 RemoveClippingWindow(); | |
37 if (!destroyed) { | 48 if (!destroyed) { |
38 host_->native_view()->ClearProperty(views::kHostViewKey); | 49 host_->native_view()->ClearProperty(views::kHostViewKey); |
39 host_->native_view()->RemoveObserver(this); | 50 host_->native_view()->RemoveObserver(this); |
40 host_->native_view()->Hide(); | 51 host_->native_view()->Hide(); |
52 | |
41 if (host_->native_view()->parent()) | 53 if (host_->native_view()->parent()) |
42 Widget::ReparentNativeView(host_->native_view(), NULL); | 54 Widget::ReparentNativeView(host_->native_view(), NULL); |
43 } | 55 } |
44 } | 56 } |
45 | 57 |
46 void NativeViewHostAura::AddedToWidget() { | 58 void NativeViewHostAura::AddedToWidget() { |
47 if (!host_->native_view()) | 59 if (!host_->native_view()) |
48 return; | 60 return; |
49 | 61 |
50 aura::Window* widget_window = host_->GetWidget()->GetNativeView(); | 62 if (host_->native_view()->parent() != |
51 if (host_->native_view()->parent() != widget_window) | 63 host_->GetWidget()->GetNativeView()) |
sky
2013/10/02 20:49:00
nit: I think you can fit this on one line.
rharrison
2013/10/09 17:42:20
Done.
| |
52 widget_window->AddChild(host_->native_view()); | 64 AddClippingWindow(); |
65 | |
53 if (host_->IsDrawn()) | 66 if (host_->IsDrawn()) |
54 host_->native_view()->Show(); | 67 host_->native_view()->Show(); |
55 else | 68 else |
56 host_->native_view()->Hide(); | 69 host_->native_view()->Hide(); |
57 host_->Layout(); | 70 host_->Layout(); |
58 } | 71 } |
59 | 72 |
60 void NativeViewHostAura::RemovedFromWidget() { | 73 void NativeViewHostAura::RemovedFromWidget() { |
61 if (host_->native_view()) { | 74 if (host_->native_view()) { |
62 host_->native_view()->Hide(); | 75 host_->native_view()->Hide(); |
76 RemoveClippingWindow(); | |
sky
2013/10/02 20:49:00
Why do you need anything more than removing the cl
rharrison
2013/10/09 17:42:20
If we do that then the native will still be attach
sky
2013/10/09 20:21:37
You're right. My question should be, can we just r
| |
63 if (host_->native_view()->parent()) | 77 if (host_->native_view()->parent()) |
64 host_->native_view()->parent()->RemoveChild(host_->native_view()); | 78 host_->native_view()->parent()->RemoveChild(host_->native_view()); |
65 } | 79 } |
66 } | 80 } |
67 | 81 |
68 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { | 82 void NativeViewHostAura::InstallClip(int x, int y, int w, int h) { |
69 // Note that this does not pose a problem functionality wise - it might | 83 gfx::Rect input_rect(x, y, w, h); |
xiyuan
2013/10/02 20:47:29
nit: seems not used.
rharrison
2013/10/09 17:42:20
Done.
| |
70 // however pose a speed degradation if not implemented. | 84 |
71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 85 if (!installed_clip_) { |
sky
2013/10/02 20:49:00
nit: no {}
rharrison
2013/10/09 17:42:20
Done.
| |
86 orig_bounds_ = clipping_window_.layer()->bounds(); | |
xiyuan
2013/10/02 20:47:29
What if InstallClip is called while it has a clip?
rharrison
2013/10/09 17:42:20
Done.
| |
87 } | |
88 | |
89 installed_clip_ = true; | |
90 | |
91 gfx::Rect clipping_bounds(x, y, w, h); | |
92 clipping_window_.layer()->SetMasksToBounds(true); | |
93 clipping_window_.layer()->SetBounds(clipping_bounds); | |
xiyuan
2013/10/02 20:47:29
host_ calls InstallClip with (x,y) in its own coor
rharrison
2013/10/09 17:42:20
You are correct, I have fixed this.
| |
94 | |
95 gfx::Rect native_bounds = host_->native_view()->layer()->bounds(); | |
96 | |
97 gfx::Point native_origin = CalculateNativeViewOrigin(clipping_bounds, | |
98 native_bounds); | |
99 native_bounds.set_origin(native_origin); | |
100 host_->native_view()->layer()->SetBounds(native_bounds); | |
72 } | 101 } |
73 | 102 |
74 bool NativeViewHostAura::HasInstalledClip() { | 103 bool NativeViewHostAura::HasInstalledClip() { |
75 return installed_clip_; | 104 return installed_clip_; |
76 } | 105 } |
77 | 106 |
78 void NativeViewHostAura::UninstallClip() { | 107 void NativeViewHostAura::UninstallClip() { |
108 if (installed_clip_ == false) | |
109 return; | |
110 | |
111 clipping_window_.layer()->SetMasksToBounds(false); | |
112 clipping_window_.layer()->SetBounds(orig_bounds_); | |
sky
2013/10/02 20:49:00
ShowWidget is invoked right after this. Can you wa
rharrison
2013/10/09 17:42:20
I think you meant InstallClip maybe? I want to avo
sky
2013/10/09 20:21:37
Isn't there already this dependency though? Native
| |
113 gfx::Rect layer_bounds = orig_bounds_; | |
114 layer_bounds.set_origin(gfx::Point(0,0)); | |
115 host_->native_view()->layer()->SetBounds(layer_bounds); | |
116 | |
79 installed_clip_ = false; | 117 installed_clip_ = false; |
80 } | 118 } |
81 | 119 |
82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 120 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
83 // TODO: need to support fast resize. | 121 if (host_->fast_resize()) { |
xiyuan
2013/10/02 20:47:29
Is it possible to make it work for non fast resize
rharrison
2013/10/09 17:42:20
The fast v. non-fast resize paths have semantic/fu
| |
84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 122 UninstallClip(); |
123 InstallClip(x, y, w, h); | |
xiyuan
2013/10/02 20:47:29
ShowWidget's (x,y) is in host_->GetWidget()'s coor
rharrison
2013/10/09 17:42:20
I agree, Done.
| |
124 } else { | |
125 clipping_window_.SetBounds(gfx::Rect(x, y, w, h)); | |
126 host_->native_view()->SetBounds(gfx::Rect(0, 0, w, h)); | |
127 } | |
85 host_->native_view()->Show(); | 128 host_->native_view()->Show(); |
86 } | 129 } |
87 | 130 |
88 void NativeViewHostAura::HideWidget() { | 131 void NativeViewHostAura::HideWidget() { |
89 host_->native_view()->Hide(); | 132 host_->native_view()->Hide(); |
90 } | 133 } |
91 | 134 |
92 void NativeViewHostAura::SetFocus() { | 135 void NativeViewHostAura::SetFocus() { |
93 aura::Window* window = host_->native_view(); | 136 aura::Window* window = host_->native_view(); |
94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 137 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
95 if (client) | 138 if (client) |
96 client->FocusWindow(window); | 139 client->FocusWindow(window); |
97 } | 140 } |
98 | 141 |
99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 142 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
100 return NULL; | 143 return NULL; |
101 } | 144 } |
102 | 145 |
103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 146 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
104 DCHECK(window == host_->native_view()); | 147 if (window == host_->native_view()) |
sky
2013/10/02 20:49:00
Why the if and not the DCHECK? This only adds one
rharrison
2013/10/09 17:42:20
Done.
| |
105 host_->NativeViewDestroyed(); | 148 host_->NativeViewDestroyed(); |
149 | |
106 } | 150 } |
107 | 151 |
108 // static | 152 // static |
109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 153 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
110 NativeViewHost* host) { | 154 NativeViewHost* host) { |
111 return new NativeViewHostAura(host); | 155 return new NativeViewHostAura(host); |
112 } | 156 } |
113 | 157 |
158 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( | |
159 const gfx::Rect& input_rect, | |
160 const gfx::Rect& native_rect) const { | |
161 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * | |
162 (input_rect.width() - | |
163 native_rect.width())); | |
164 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * | |
165 (input_rect.height() - | |
166 native_rect.height())); | |
167 return gfx::Point(new_x, new_y); | |
168 } | |
169 | |
170 void NativeViewHostAura::AddClippingWindow() { | |
171 gfx::Rect bounds = host_->native_view()->bounds(); | |
172 | |
173 if (host_->GetWidget()->GetNativeView()) | |
174 Widget::ReparentNativeView(&clipping_window_, | |
175 host_->GetWidget()->GetNativeView()); | |
176 Widget::ReparentNativeView(host_->native_view(), | |
177 &clipping_window_); | |
178 | |
179 clipping_window_.SetBounds(bounds); | |
180 bounds.set_origin(gfx::Point(0,0)); | |
181 host_->native_view()->SetBounds(bounds); | |
182 | |
183 clipping_window_.Show(); | |
184 } | |
185 | |
186 void NativeViewHostAura::RemoveClippingWindow() { | |
187 if (host_->native_view()->parent() == &clipping_window_) { | |
188 if (host_->GetWidget()->GetNativeView()) { | |
189 Widget::ReparentNativeView(host_->native_view(), | |
190 host_->GetWidget()->GetNativeView()); | |
191 } else { | |
192 clipping_window_.RemoveChild(host_->native_view()); | |
193 } | |
194 | |
195 clipping_window_.Hide(); | |
196 host_->native_view()->SetBounds(clipping_window_.bounds()); | |
197 if (clipping_window_.parent()) | |
198 clipping_window_.parent()->RemoveChild(&clipping_window_); | |
199 } | |
200 } | |
201 | |
114 } // namespace views | 202 } // namespace views |
OLD | NEW |