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