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 "chrome/browser/ui/views/extensions/shell_window_views.h" | 5 #include "chrome/browser/ui/views/extensions/shell_window_views.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
| 10 #include "content/public/browser/render_view_host.h" |
| 11 #include "content/public/browser/render_widget_host_view.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_view.h" |
10 #include "ui/base/hit_test.h" | 14 #include "ui/base/hit_test.h" |
11 #include "ui/gfx/path.h" | 15 #include "ui/gfx/path.h" |
12 #include "ui/gfx/scoped_sk_region.h" | 16 #include "ui/gfx/scoped_sk_region.h" |
13 #include "ui/views/layout/fill_layout.h" | 17 #include "ui/views/layout/fill_layout.h" |
14 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
15 #include "ui/views/window/non_client_view.h" | 19 #include "ui/views/window/non_client_view.h" |
16 | 20 |
17 #if defined(OS_WIN) && !defined(USE_AURA) | 21 #if defined(OS_WIN) && !defined(USE_AURA) |
18 #include "chrome/browser/shell_integration.h" | 22 #include "chrome/browser/shell_integration.h" |
19 #include "chrome/browser/web_applications/web_app.h" | 23 #include "chrome/browser/web_applications/web_app.h" |
20 #include "content/public/browser/render_view_host.h" | |
21 #include "content/public/browser/render_widget_host_view.h" | |
22 #include "ui/base/win/shell.h" | 24 #include "ui/base/win/shell.h" |
23 #endif | 25 #endif |
24 | 26 |
25 #if defined(USE_ASH) | 27 #if defined(USE_ASH) |
26 #include "ash/wm/custom_frame_view_ash.h" | 28 #include "ash/wm/custom_frame_view_ash.h" |
27 #endif | 29 #endif |
28 | 30 |
29 // Number of pixels around the edge of the window that can be dragged to | 31 // Number of pixels around the edge of the window that can be dragged to |
30 // resize the window. | 32 // resize the window. |
31 static const int kResizeBorderWidth = 5; | 33 static const int kResizeBorderWidth = 5; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 if (y >= height() - kResizeBorderWidth) | 91 if (y >= height() - kResizeBorderWidth) |
90 return HTBOTTOM; | 92 return HTBOTTOM; |
91 return HTCAPTION; | 93 return HTCAPTION; |
92 } | 94 } |
93 | 95 |
94 void ShellWindowFrameView::GetWindowMask(const gfx::Size& size, | 96 void ShellWindowFrameView::GetWindowMask(const gfx::Size& size, |
95 gfx::Path* window_mask) { | 97 gfx::Path* window_mask) { |
96 // Don't touch it. | 98 // Don't touch it. |
97 } | 99 } |
98 | 100 |
99 ShellWindowViews::ShellWindowViews(ExtensionHost* host) | 101 ShellWindowViews::ShellWindowViews(Profile* profile, |
100 : ShellWindow(host) { | 102 const Extension* extension, |
101 host_->view()->SetContainer(this); | 103 const GURL& url) |
| 104 : ShellWindow(profile, extension, url), |
| 105 initialized_(false) { |
102 window_ = new views::Widget; | 106 window_ = new views::Widget; |
103 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | 107 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); |
104 params.delegate = this; | 108 params.delegate = this; |
105 params.remove_standard_frame = true; | 109 params.remove_standard_frame = true; |
106 gfx::Rect bounds(10, 10, kDefaultWidth, kDefaultHeight); | 110 gfx::Rect bounds(10, 10, kDefaultWidth, kDefaultHeight); |
107 params.bounds = bounds; | 111 params.bounds = bounds; |
108 window_->Init(params); | 112 window_->Init(params); |
109 #if defined(OS_WIN) && !defined(USE_AURA) | 113 #if defined(OS_WIN) && !defined(USE_AURA) |
110 std::string app_name = web_app::GenerateApplicationNameFromExtensionId( | 114 std::string app_name = web_app::GenerateApplicationNameFromExtensionId( |
111 host_->extension()->id()); | 115 extension->id()); |
112 ui::win::SetAppIdForWindow( | 116 ui::win::SetAppIdForWindow( |
113 ShellIntegration::GetAppId(UTF8ToWide(app_name), | 117 ShellIntegration::GetAppId(UTF8ToWide(app_name), |
114 host_->profile()->GetPath()), | 118 profile->GetPath()), |
115 GetWidget()->GetTopLevelWidget()->GetNativeWindow()); | 119 GetWidget()->GetTopLevelWidget()->GetNativeWindow()); |
116 #endif | 120 #endif |
117 AddChildView(host_->view()); | |
118 SetLayoutManager(new views::FillLayout); | 121 SetLayoutManager(new views::FillLayout); |
119 Layout(); | 122 Layout(); |
120 | 123 |
121 window_->Show(); | 124 window_->Show(); |
122 } | 125 } |
123 | 126 |
124 ShellWindowViews::~ShellWindowViews() { | 127 ShellWindowViews::~ShellWindowViews() { |
| 128 if (initialized_) |
| 129 NativeViewHost::Detach(); |
125 } | 130 } |
126 | 131 |
127 bool ShellWindowViews::IsActive() const { | 132 bool ShellWindowViews::IsActive() const { |
128 return window_->IsActive(); | 133 return window_->IsActive(); |
129 } | 134 } |
130 | 135 |
131 bool ShellWindowViews::IsMaximized() const { | 136 bool ShellWindowViews::IsMaximized() const { |
132 return window_->IsMaximized(); | 137 return window_->IsMaximized(); |
133 } | 138 } |
134 | 139 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // aura. | 232 // aura. |
228 ash::CustomFrameViewAsh* frame = new ash::CustomFrameViewAsh(); | 233 ash::CustomFrameViewAsh* frame = new ash::CustomFrameViewAsh(); |
229 frame->Init(widget); | 234 frame->Init(widget); |
230 return frame; | 235 return frame; |
231 #else | 236 #else |
232 return new ShellWindowFrameView(); | 237 return new ShellWindowFrameView(); |
233 #endif | 238 #endif |
234 } | 239 } |
235 | 240 |
236 string16 ShellWindowViews::GetWindowTitle() const { | 241 string16 ShellWindowViews::GetWindowTitle() const { |
237 return UTF8ToUTF16(host_->extension()->name()); | 242 return UTF8ToUTF16(extension()->name()); |
238 } | 243 } |
239 | 244 |
240 views::Widget* ShellWindowViews::GetWidget() { | 245 views::Widget* ShellWindowViews::GetWidget() { |
241 return window_; | 246 return window_; |
242 } | 247 } |
243 | 248 |
244 const views::Widget* ShellWindowViews::GetWidget() const { | 249 const views::Widget* ShellWindowViews::GetWidget() const { |
245 return window_; | 250 return window_; |
246 } | 251 } |
247 | 252 |
248 void ShellWindowViews::OnViewWasResized() { | 253 void ShellWindowViews::OnViewWasResized() { |
249 // TODO(jeremya): this doesn't seem like a terribly elegant way to keep the | 254 // TODO(jeremya): this doesn't seem like a terribly elegant way to keep the |
250 // window shape in sync. | 255 // window shape in sync. |
251 #if defined(OS_WIN) && !defined(USE_AURA) | 256 #if defined(OS_WIN) && !defined(USE_AURA) |
252 gfx::Size sz = host_->view()->size(); | 257 gfx::Size sz = size(); |
253 int height = sz.height(), width = sz.width(); | 258 int height = sz.height(), width = sz.width(); |
254 int radius = 1; | 259 int radius = 1; |
255 gfx::Path path; | 260 gfx::Path path; |
256 if (GetWidget()->IsMaximized()) { | 261 if (GetWidget()->IsMaximized()) { |
257 // Don't round the corners when the window is maximized. | 262 // Don't round the corners when the window is maximized. |
258 path.addRect(0, 0, width, height); | 263 path.addRect(0, 0, width, height); |
259 } else { | 264 } else { |
260 path.moveTo(0, radius); | 265 path.moveTo(0, radius); |
261 path.lineTo(radius, 0); | 266 path.lineTo(radius, 0); |
262 path.lineTo(width - radius, 0); | 267 path.lineTo(width - radius, 0); |
263 path.lineTo(width, radius); | 268 path.lineTo(width, radius); |
264 path.lineTo(width, height - radius - 1); | 269 path.lineTo(width, height - radius - 1); |
265 path.lineTo(width - radius - 1, height); | 270 path.lineTo(width - radius - 1, height); |
266 path.lineTo(radius + 1, height); | 271 path.lineTo(radius + 1, height); |
267 path.lineTo(0, height - radius - 1); | 272 path.lineTo(0, height - radius - 1); |
268 path.close(); | 273 path.close(); |
269 } | 274 } |
270 SetWindowRgn(host_->view()->native_view(), path.CreateNativeRegion(), 1); | 275 SetWindowRgn(native_view(), path.CreateNativeRegion(), 1); |
271 | 276 |
272 SkRegion* rgn = new SkRegion; | 277 SkRegion* rgn = new SkRegion; |
273 if (caption_region_.Get()) | 278 if (caption_region_.Get()) |
274 rgn->op(*caption_region_.Get(), SkRegion::kUnion_Op); | 279 rgn->op(*caption_region_.Get(), SkRegion::kUnion_Op); |
275 if (!GetWidget()->IsMaximized()) { | 280 if (!GetWidget()->IsMaximized()) { |
276 rgn->op(0, 0, width, kResizeBorderWidth, SkRegion::kUnion_Op); | 281 rgn->op(0, 0, width, kResizeBorderWidth, SkRegion::kUnion_Op); |
277 rgn->op(0, 0, kResizeBorderWidth, height, SkRegion::kUnion_Op); | 282 rgn->op(0, 0, kResizeBorderWidth, height, SkRegion::kUnion_Op); |
278 rgn->op(width - kResizeBorderWidth, 0, width, height, SkRegion::kUnion_Op); | 283 rgn->op(width - kResizeBorderWidth, 0, width, height, SkRegion::kUnion_Op); |
279 rgn->op(0, height - kResizeBorderWidth, width, height, SkRegion::kUnion_Op); | 284 rgn->op(0, height - kResizeBorderWidth, width, height, SkRegion::kUnion_Op); |
280 } | 285 } |
281 host_->render_view_host()->GetView()->SetClickthroughRegion(rgn); | 286 web_contents()->GetRenderViewHost()->GetView()->SetClickthroughRegion(rgn); |
282 #endif | 287 #endif |
283 } | 288 } |
284 | 289 |
| 290 gfx::NativeCursor ShellWindowViews::GetCursor(const views::MouseEvent& event) { |
| 291 return gfx::kNullCursor; |
| 292 } |
| 293 |
| 294 void ShellWindowViews::SetVisible(bool is_visible) { |
| 295 if (is_visible != visible()) { |
| 296 NativeViewHost::SetVisible(is_visible); |
| 297 |
| 298 // Also tell RenderWidgetHostView the new visibility. Despite its name, it |
| 299 // is not part of the View hierarchy and does not know about the change |
| 300 // unless we tell it. |
| 301 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| 302 if (rvh->GetView()) { |
| 303 if (is_visible) |
| 304 rvh->GetView()->Show(); |
| 305 else |
| 306 rvh->GetView()->Hide(); |
| 307 } |
| 308 } |
| 309 } |
| 310 |
| 311 void ShellWindowViews::ViewHierarchyChanged(bool is_add, |
| 312 views::View *parent, |
| 313 views::View *child) { |
| 314 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); |
| 315 if (is_add && GetWidget() && !initialized_) { |
| 316 initialized_ = true; |
| 317 NativeViewHost::Attach(web_contents()->GetView()->GetNativeView()); |
| 318 } |
| 319 } |
| 320 |
| 321 void ShellWindowViews::PreferredSizeChanged() { |
| 322 View::PreferredSizeChanged(); |
| 323 } |
| 324 |
| 325 bool ShellWindowViews::SkipDefaultKeyEventProcessing(const views::KeyEvent& e) { |
| 326 // Let the tab key event be processed by the renderer (instead of moving the |
| 327 // focus to the next focusable view). Also handle Backspace, since otherwise |
| 328 // (on Windows at least), pressing Backspace, when focus is on a text field |
| 329 // within the ExtensionView, will navigate the page back instead of erasing a |
| 330 // character. |
| 331 return (e.key_code() == ui::VKEY_TAB || e.key_code() == ui::VKEY_BACK); |
| 332 } |
| 333 |
| 334 void ShellWindowViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 335 // Propagate the new size to RenderWidgetHostView. |
| 336 // We can't send size zero because RenderWidget DCHECKs that. |
| 337 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| 338 if (rvh->GetView() && !bounds().IsEmpty()) { |
| 339 rvh->GetView()->SetSize(size()); |
| 340 OnViewWasResized(); |
| 341 } |
| 342 } |
| 343 |
285 // static | 344 // static |
286 ShellWindow* ShellWindow::CreateShellWindow(ExtensionHost* host) { | 345 ShellWindow* ShellWindow::CreateImpl(Profile* profile, |
287 return new ShellWindowViews(host); | 346 const Extension* extension, |
| 347 const GURL& url) { |
| 348 return new ShellWindowViews(profile, extension, url); |
288 } | 349 } |
OLD | NEW |