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()) |
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(); |
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 installed_clip_ = true; |
70 // however pose a speed degradation if not implemented. | 84 |
71 LOG(WARNING) << "NativeViewHostAura::InstallClip is not implemented yet."; | 85 clip_rect_ = gfx::Rect(x, y, w, h); |
| 86 UpdateClippingWindow(); |
| 87 clipping_window_.layer()->SetMasksToBounds(true); |
72 } | 88 } |
73 | 89 |
74 bool NativeViewHostAura::HasInstalledClip() { | 90 bool NativeViewHostAura::HasInstalledClip() { |
75 return installed_clip_; | 91 return installed_clip_; |
76 } | 92 } |
77 | 93 |
78 void NativeViewHostAura::UninstallClip() { | 94 void NativeViewHostAura::UninstallClip() { |
| 95 if (installed_clip_ == false) |
| 96 return; |
| 97 |
| 98 clipping_window_.layer()->SetMasksToBounds(false); |
| 99 clipping_window_.layer()->SetBounds(orig_bounds_); |
| 100 gfx::Rect layer_bounds = orig_bounds_; |
| 101 layer_bounds.set_origin(gfx::Point(0,0)); |
| 102 host_->native_view()->layer()->SetBounds(layer_bounds); |
| 103 |
79 installed_clip_ = false; | 104 installed_clip_ = false; |
80 } | 105 } |
81 | 106 |
82 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { | 107 void NativeViewHostAura::ShowWidget(int x, int y, int w, int h) { |
83 // TODO: need to support fast resize. | 108 if (host_->fast_resize()) { |
84 host_->native_view()->SetBounds(gfx::Rect(x, y, w, h)); | 109 gfx::Rect bounds = host_->native_view()->layer()->bounds(); |
| 110 gfx::Point native_origin = |
| 111 CalculateNativeViewOrigin(gfx::Rect(0, 0, w, h), bounds); |
| 112 orig_bounds_ = gfx::Rect(x + native_origin.x(), |
| 113 y + native_origin.y(), |
| 114 bounds.width(), |
| 115 bounds.height()); |
| 116 |
| 117 UninstallClip(); |
| 118 InstallClip(-native_origin.x(), -native_origin.y(), w, h); |
| 119 } else { |
| 120 orig_bounds_ = gfx::Rect(x, y, w, h); |
| 121 UpdateClippingWindow(); |
| 122 } |
85 host_->native_view()->Show(); | 123 host_->native_view()->Show(); |
86 } | 124 } |
87 | 125 |
88 void NativeViewHostAura::HideWidget() { | 126 void NativeViewHostAura::HideWidget() { |
89 host_->native_view()->Hide(); | 127 host_->native_view()->Hide(); |
90 } | 128 } |
91 | 129 |
92 void NativeViewHostAura::SetFocus() { | 130 void NativeViewHostAura::SetFocus() { |
93 aura::Window* window = host_->native_view(); | 131 aura::Window* window = host_->native_view(); |
94 aura::client::FocusClient* client = aura::client::GetFocusClient(window); | 132 aura::client::FocusClient* client = aura::client::GetFocusClient(window); |
95 if (client) | 133 if (client) |
96 client->FocusWindow(window); | 134 client->FocusWindow(window); |
97 } | 135 } |
98 | 136 |
99 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { | 137 gfx::NativeViewAccessible NativeViewHostAura::GetNativeViewAccessible() { |
100 return NULL; | 138 return NULL; |
101 } | 139 } |
102 | 140 |
103 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { | 141 void NativeViewHostAura::OnWindowDestroyed(aura::Window* window) { |
104 DCHECK(window == host_->native_view()); | 142 if (window == host_->native_view()) |
105 host_->NativeViewDestroyed(); | 143 host_->NativeViewDestroyed(); |
106 } | 144 } |
107 | 145 |
108 // static | 146 // static |
109 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( | 147 NativeViewHostWrapper* NativeViewHostWrapper::CreateWrapper( |
110 NativeViewHost* host) { | 148 NativeViewHost* host) { |
111 return new NativeViewHostAura(host); | 149 return new NativeViewHostAura(host); |
112 } | 150 } |
113 | 151 |
| 152 gfx::Point NativeViewHostAura::CalculateNativeViewOrigin( |
| 153 const gfx::Rect& input_rect, |
| 154 const gfx::Rect& native_rect) const { |
| 155 int new_x = gfx::ToRoundedInt(host_->GetWidthScaleFactor() * |
| 156 (input_rect.width() - |
| 157 native_rect.width())); |
| 158 int new_y = gfx::ToRoundedInt(host_->GetHeightScaleFactor() * |
| 159 (input_rect.height() - |
| 160 native_rect.height())); |
| 161 return gfx::Point(new_x, new_y); |
| 162 } |
| 163 |
| 164 void NativeViewHostAura::AddClippingWindow() { |
| 165 gfx::Rect bounds = host_->native_view()->bounds(); |
| 166 |
| 167 if (host_->GetWidget()->GetNativeView()) |
| 168 Widget::ReparentNativeView(&clipping_window_, |
| 169 host_->GetWidget()->GetNativeView()); |
| 170 Widget::ReparentNativeView(host_->native_view(), |
| 171 &clipping_window_); |
| 172 |
| 173 clipping_window_.SetBounds(bounds); |
| 174 bounds.set_origin(gfx::Point(0,0)); |
| 175 host_->native_view()->SetBounds(bounds); |
| 176 |
| 177 clipping_window_.Show(); |
| 178 } |
| 179 |
| 180 void NativeViewHostAura::RemoveClippingWindow() { |
| 181 if (host_->native_view()->parent() == &clipping_window_) { |
| 182 if (host_->GetWidget()->GetNativeView()) { |
| 183 Widget::ReparentNativeView(host_->native_view(), |
| 184 host_->GetWidget()->GetNativeView()); |
| 185 } else { |
| 186 clipping_window_.RemoveChild(host_->native_view()); |
| 187 } |
| 188 |
| 189 clipping_window_.Hide(); |
| 190 host_->native_view()->SetBounds(clipping_window_.bounds()); |
| 191 if (clipping_window_.parent()) |
| 192 clipping_window_.parent()->RemoveChild(&clipping_window_); |
| 193 } |
| 194 } |
| 195 |
| 196 void NativeViewHostAura::UpdateClippingWindow() { |
| 197 if (installed_clip_) { |
| 198 gfx::Rect clip_bounds(clip_rect_); |
| 199 clip_bounds.Offset(orig_bounds_.x(), orig_bounds_.y()); |
| 200 clipping_window_.SetBounds(clip_bounds); |
| 201 host_->native_view()->SetBounds( |
| 202 gfx::Rect(-clip_rect_.x(), -clip_rect_.y(), |
| 203 orig_bounds_.width(), orig_bounds_.height())); |
| 204 } else { |
| 205 clipping_window_.SetBounds(orig_bounds_); |
| 206 host_->native_view()->SetBounds(gfx::Rect(orig_bounds_.size())); |
| 207 } |
| 208 } |
| 209 |
114 } // namespace views | 210 } // namespace views |
OLD | NEW |