panel_view.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/views/panels/panel_view.h" 6 7#include <map> 8#include "base/logging.h" 9#include "base/message_loop/message_loop.h" 10#include "base/strings/utf_string_conversions.h" 11#include "chrome/app/chrome_command_ids.h" 12#include "chrome/browser/profiles/profile.h" 13#include "chrome/browser/ui/host_desktop.h" 14#include "chrome/browser/ui/panels/panel.h" 15#include "chrome/browser/ui/panels/panel_bounds_animation.h" 16#include "chrome/browser/ui/panels/panel_manager.h" 17#include "chrome/browser/ui/panels/stacked_panel_collection.h" 18#include "chrome/browser/ui/views/panels/panel_frame_view.h" 19#include "content/public/browser/render_view_host.h" 20#include "content/public/browser/render_widget_host_view.h" 21#include "content/public/browser/web_contents.h" 22#include "content/public/browser/web_contents_view.h" 23#include "ui/gfx/image/image.h" 24#include "ui/gfx/path.h" 25#include "ui/gfx/screen.h" 26#include "ui/views/controls/button/image_button.h" 27#include "ui/views/controls/webview/webview.h" 28#include "ui/views/widget/widget.h" 29 30#if defined(OS_WIN) 31#include "base/win/windows_version.h" 32#include "chrome/browser/shell_integration.h" 33#include "chrome/browser/ui/views/panels/taskbar_window_thumbnailer_win.h" 34#include "ui/base/win/shell.h" 35#include "ui/gfx/icon_util.h" 36#include "ui/views/win/hwnd_util.h" 37#endif 38 39namespace { 40 41// If the height of a stacked panel shrinks below this threshold during the 42// user resizing, it will be treated as minimized. 43const int kStackedPanelHeightShrinkThresholdToBecomeMinimized = 44 panel::kTitlebarHeight + 20; 45 46// Supported accelerators. 47// Note: We can't use the acclerator table defined in chrome/browser/ui/views 48// due to checkdeps violation. 49struct AcceleratorMapping { 50 ui::KeyboardCode keycode; 51 int modifiers; 52 int command_id; 53}; 54const AcceleratorMapping kPanelAcceleratorMap[] = { 55 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW }, 56 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW }, 57 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW }, 58 { ui::VKEY_R, ui::EF_CONTROL_DOWN, IDC_RELOAD }, 59 { ui::VKEY_F5, ui::EF_NONE, IDC_RELOAD }, 60 { ui::VKEY_R, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, 61 IDC_RELOAD_IGNORING_CACHE }, 62 { ui::VKEY_F5, ui::EF_CONTROL_DOWN, IDC_RELOAD_IGNORING_CACHE }, 63 { ui::VKEY_F5, ui::EF_SHIFT_DOWN, IDC_RELOAD_IGNORING_CACHE }, 64 { ui::VKEY_ESCAPE, ui::EF_NONE, IDC_STOP }, 65 { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS }, 66 { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS }, 67 { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL }, 68 { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL }, 69 { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS }, 70 { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS }, 71 { ui::VKEY_I, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_DEV_TOOLS }, 72 { ui::VKEY_J, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, 73 IDC_DEV_TOOLS_CONSOLE }, 74}; 75 76const std::map<ui::Accelerator, int>& GetAcceleratorTable() { 77 static std::map<ui::Accelerator, int>* accelerators = NULL; 78 if (!accelerators) { 79 accelerators = new std::map<ui::Accelerator, int>(); 80 for (size_t i = 0; i < arraysize(kPanelAcceleratorMap); ++i) { 81 ui::Accelerator accelerator(kPanelAcceleratorMap[i].keycode, 82 kPanelAcceleratorMap[i].modifiers); 83 (*accelerators)[accelerator] = kPanelAcceleratorMap[i].command_id; 84 } 85 } 86 return *accelerators; 87} 88 89// NativePanelTesting implementation. 90class NativePanelTestingWin : public NativePanelTesting { 91 public: 92 explicit NativePanelTestingWin(PanelView* panel_view); 93 94 private: 95 virtual void PressLeftMouseButtonTitlebar( 96 const gfx::Point& mouse_location, panel::ClickModifier modifier) OVERRIDE; 97 virtual void ReleaseMouseButtonTitlebar( 98 panel::ClickModifier modifier) OVERRIDE; 99 virtual void DragTitlebar(const gfx::Point& mouse_location) OVERRIDE; 100 virtual void CancelDragTitlebar() OVERRIDE; 101 virtual void FinishDragTitlebar() OVERRIDE; 102 virtual bool VerifyDrawingAttention() const OVERRIDE; 103 virtual bool VerifyActiveState(bool is_active) OVERRIDE; 104 virtual bool VerifyAppIcon() const OVERRIDE; 105 virtual bool VerifySystemMinimizeState() const OVERRIDE; 106 virtual bool IsWindowVisible() const OVERRIDE; 107 virtual bool IsWindowSizeKnown() const OVERRIDE; 108 virtual bool IsAnimatingBounds() const OVERRIDE; 109 virtual bool IsButtonVisible( 110 panel::TitlebarButtonType button_type) const OVERRIDE; 111 virtual panel::CornerStyle GetWindowCornerStyle() const OVERRIDE; 112 virtual bool EnsureApplicationRunOnForeground() OVERRIDE; 113 114 PanelView* panel_view_; 115}; 116 117NativePanelTestingWin::NativePanelTestingWin(PanelView* panel_view) 118 : panel_view_(panel_view) { 119} 120 121void NativePanelTestingWin::PressLeftMouseButtonTitlebar( 122 const gfx::Point& mouse_location, panel::ClickModifier modifier) { 123 panel_view_->OnTitlebarMousePressed(mouse_location); 124} 125 126void NativePanelTestingWin::ReleaseMouseButtonTitlebar( 127 panel::ClickModifier modifier) { 128 panel_view_->OnTitlebarMouseReleased(modifier); 129} 130 131void NativePanelTestingWin::DragTitlebar(const gfx::Point& mouse_location) { 132 panel_view_->OnTitlebarMouseDragged(mouse_location); 133} 134 135void NativePanelTestingWin::CancelDragTitlebar() { 136 panel_view_->OnTitlebarMouseCaptureLost(); 137} 138 139void NativePanelTestingWin::FinishDragTitlebar() { 140 panel_view_->OnTitlebarMouseReleased(panel::NO_MODIFIER); 141} 142 143bool NativePanelTestingWin::VerifyDrawingAttention() const { 144 base::MessageLoop::current()->RunUntilIdle(); 145 return panel_view_->GetFrameView()->GetPaintState() == 146 PanelFrameView::PAINT_FOR_ATTENTION; 147} 148 149bool NativePanelTestingWin::VerifyActiveState(bool is_active) { 150 return panel_view_->GetFrameView()->GetPaintState() == 151 (is_active ? PanelFrameView::PAINT_AS_ACTIVE 152 : PanelFrameView::PAINT_AS_INACTIVE); 153} 154 155bool NativePanelTestingWin::VerifyAppIcon() const { 156#if defined(OS_WIN) 157 // We only care about Windows 7 and later. 158 if (base::win::GetVersion() < base::win::VERSION_WIN7) 159 return true; 160 161 HWND native_window = views::HWNDForWidget(panel_view_->window()); 162 HICON app_icon = reinterpret_cast<HICON>( 163 ::SendMessage(native_window, WM_GETICON, ICON_BIG, 0L)); 164 if (!app_icon) 165 return false; 166 scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(app_icon)); 167 return bitmap.get() && 168 bitmap->width() == panel::kPanelAppIconSize && 169 bitmap->height() == panel::kPanelAppIconSize; 170#else 171 return true; 172#endif 173} 174 175bool NativePanelTestingWin::VerifySystemMinimizeState() const { 176#if defined(OS_WIN) 177 HWND native_window = views::HWNDForWidget(panel_view_->window()); 178 WINDOWPLACEMENT placement; 179 if (!::GetWindowPlacement(native_window, &placement)) 180 return false; 181 if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_SHOWMINIMIZED) 182 return true; 183 184 // If the panel window has owner window, as in stacked mode, check its owner 185 // window. Note that owner window, instead of parent window, is returned 186 // though GWL_HWNDPARENT contains 'parent'. 187 HWND owner_window = 188 reinterpret_cast<HWND>(::GetWindowLongPtr(native_window, 189 GWLP_HWNDPARENT)); 190 if (!owner_window || !::GetWindowPlacement(owner_window, &placement)) 191 return false; 192 return placement.showCmd == SW_MINIMIZE || 193 placement.showCmd == SW_SHOWMINIMIZED; 194#else 195 return true; 196#endif 197} 198 199bool NativePanelTestingWin::IsWindowVisible() const { 200#if defined(OS_WIN) 201 HWND native_window = views::HWNDForWidget(panel_view_->window()); 202 return ::IsWindowVisible(native_window) == TRUE; 203#else 204 return panel_view_->visible(); 205#endif 206} 207 208bool NativePanelTestingWin::IsWindowSizeKnown() const { 209 return true; 210} 211 212bool NativePanelTestingWin::IsAnimatingBounds() const { 213 return panel_view_->IsAnimatingBounds(); 214} 215 216bool NativePanelTestingWin::IsButtonVisible( 217 panel::TitlebarButtonType button_type) const { 218 PanelFrameView* frame_view = panel_view_->GetFrameView(); 219 220 switch (button_type) { 221 case panel::CLOSE_BUTTON: 222 return frame_view->close_button()->visible(); 223 case panel::MINIMIZE_BUTTON: 224 return frame_view->minimize_button()->visible(); 225 case panel::RESTORE_BUTTON: 226 return frame_view->restore_button()->visible(); 227 default: 228 NOTREACHED(); 229 } 230 return false; 231} 232 233panel::CornerStyle NativePanelTestingWin::GetWindowCornerStyle() const { 234 return panel_view_->GetFrameView()->corner_style(); 235} 236 237bool NativePanelTestingWin::EnsureApplicationRunOnForeground() { 238 // Not needed on views. 239 return true; 240} 241 242} // namespace 243 244// static 245NativePanel* Panel::CreateNativePanel(Panel* panel, 246 const gfx::Rect& bounds, 247 bool always_on_top) { 248 return new PanelView(panel, bounds, always_on_top); 249} 250 251// The panel window has to be created as always-on-top. We cannot create it 252// as non-always-on-top and then change it to always-on-top because Windows 253// system might deny making a window always-on-top if the application is not 254// a foreground application. 255PanelView::PanelView(Panel* panel, const gfx::Rect& bounds, bool always_on_top) 256 : panel_(panel), 257 bounds_(bounds), 258 window_(NULL), 259 window_closed_(false), 260 web_view_(NULL), 261 always_on_top_(always_on_top), 262 focused_(false), 263 user_resizing_(false), 264#if defined(OS_WIN) 265 user_resizing_interior_stacked_panel_edge_(false), 266#endif 267 mouse_pressed_(false), 268 mouse_dragging_state_(NO_DRAGGING), 269 is_drawing_attention_(false), 270 force_to_paint_as_inactive_(false), 271 old_focused_view_(NULL) { 272 window_ = new views::Widget; 273 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); 274 params.delegate = this; 275 params.remove_standard_frame = true; 276 params.keep_on_top = always_on_top; 277 params.bounds = bounds; 278 window_->Init(params); 279 window_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); 280 window_->set_focus_on_creation(false); 281 window_->AddObserver(this); 282 283 web_view_ = new views::WebView(NULL); 284 AddChildView(web_view_); 285 286 OnViewWasResized(); 287 288 // Register accelarators supported by panels. 289 views::FocusManager* focus_manager = GetFocusManager(); 290 const std::map<ui::Accelerator, int>& accelerator_table = 291 GetAcceleratorTable(); 292 for (std::map<ui::Accelerator, int>::const_iterator iter = 293 accelerator_table.begin(); 294 iter != accelerator_table.end(); ++iter) { 295 focus_manager->RegisterAccelerator( 296 iter->first, ui::AcceleratorManager::kNormalPriority, this); 297 } 298 299#if defined(OS_WIN) 300 ui::win::SetAppIdForWindow( 301 ShellIntegration::GetAppModelIdForProfile(UTF8ToWide(panel->app_name()), 302 panel->profile()->GetPath()), 303 views::HWNDForWidget(window_)); 304 ui::win::PreventWindowFromPinning(views::HWNDForWidget(window_)); 305#endif 306} 307 308PanelView::~PanelView() { 309} 310 311void PanelView::ShowPanel() { 312 ShowPanelInactive(); 313 ActivatePanel(); 314} 315 316void PanelView::ShowPanelInactive() { 317 if (window_->IsVisible()) 318 return; 319 window_->ShowInactive(); 320 // No animation is used for initial creation of a panel on Win. 321 // Signal immediately that pending actions can be performed. 322 panel_->manager()->OnPanelAnimationEnded(panel_.get()); 323} 324 325gfx::Rect PanelView::GetPanelBounds() const { 326 return bounds_; 327} 328 329void PanelView::SetPanelBounds(const gfx::Rect& bounds) { 330 SetBoundsInternal(bounds, true); 331} 332 333void PanelView::SetPanelBoundsInstantly(const gfx::Rect& bounds) { 334 SetBoundsInternal(bounds, false); 335} 336 337void PanelView::SetBoundsInternal(const gfx::Rect& new_bounds, bool animate) { 338 if (bounds_ == new_bounds) 339 return; 340 341 bounds_ = new_bounds; 342 343 if (!animate) { 344 // If no animation is in progress, apply bounds change instantly. Otherwise, 345 // continue the animation with new target bounds. 346 if (!IsAnimatingBounds()) 347 SetWidgetBounds(bounds_); 348 return; 349 } 350 351 animation_start_bounds_ = window_->GetWindowBoundsInScreen(); 352 353 bounds_animator_.reset(new PanelBoundsAnimation( 354 this, panel_.get(), animation_start_bounds_, new_bounds)); 355 bounds_animator_->Start(); 356} 357 358#if defined(OS_WIN) 359bool PanelView::FilterMessage(HWND hwnd, 360 UINT message, 361 WPARAM w_param, 362 LPARAM l_param, 363 LRESULT* l_result) { 364 switch (message) { 365 case WM_SIZING: 366 if (w_param == WMSZ_BOTTOM) 367 user_resizing_interior_stacked_panel_edge_ = true; 368 break; 369 } 370 return false; 371} 372#endif 373 374void PanelView::AnimationEnded(const gfx::Animation* animation) { 375 panel_->manager()->OnPanelAnimationEnded(panel_.get()); 376} 377 378void PanelView::AnimationProgressed(const gfx::Animation* animation) { 379 gfx::Rect new_bounds = bounds_animator_->CurrentValueBetween( 380 animation_start_bounds_, bounds_); 381 SetWidgetBounds(new_bounds); 382} 383 384void PanelView::SetWidgetBounds(const gfx::Rect& new_bounds) { 385#if defined(OS_WIN) 386 // An overlapped window is a top-level window that has a titlebar, border, 387 // and client area. The Windows system will automatically put the shadow 388 // around the whole window. Also the system will enforce the minimum height 389 // (38 pixels based on observation) for the overlapped window such that it 390 // will always has the space for the titlebar. 391 // 392 // On contrast, a popup window is a bare minimum window without border and 393 // titlebar by default. It is often used for the popup menu and the window 394 // with short life. The Windows system does not add the shadow around the 395 // whole window though CS_DROPSHADOW class style could be passed to add the 396 // drop shadow which is only around the right and bottom edges. 397 // 398 // The height of the title-only or minimized panel is smaller than the minimum 399 // overlapped window height. If the panel still uses the overlapped window 400 // style, Windows system will automatically increase the window height. To 401 // work around this limitation, we temporarily change the window style to 402 // popup when the height to set is smaller than the minimum overlapped window 403 // height and then restore the window style to overlapped when the height 404 // grows. 405 static const int kMinimumOverlappedWindowHeight = 38; 406 gfx::Rect old_bounds = GetWidget()->GetRestoredBounds(); 407 if (old_bounds.height() > kMinimumOverlappedWindowHeight && 408 new_bounds.height() <= kMinimumOverlappedWindowHeight) { 409 // When the panel height shrinks below the minimum overlapped window height, 410 // change the window style to popup such that we can show the title-only 411 // and minimized panel without additional height being added by the system. 412 UpdateWindowAttribute(GWL_STYLE, 413 WS_POPUP, 414 WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU, 415 true); 416 } else if (old_bounds.height() <= kMinimumOverlappedWindowHeight && 417 new_bounds.height() > kMinimumOverlappedWindowHeight) { 418 // Change the window style back to overlappped when the panel height grow 419 // taller than the minimum overlapped window height. 420 UpdateWindowAttribute(GWL_STYLE, 421 WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU, 422 WS_POPUP, 423 true); 424 } 425#endif 426 427 GetWidget()->SetBounds(new_bounds); 428} 429 430void PanelView::ClosePanel() { 431 // We're already closing. Do nothing. 432 if (window_closed_) 433 return; 434 435 if (!panel_->ShouldCloseWindow()) 436 return; 437 438 // Cancel any currently running animation since we're closing down. 439 if (bounds_animator_.get()) 440 bounds_animator_.reset(); 441 442 if (panel_->GetWebContents()) { 443 // Still have web contents. Allow renderer to shut down. 444 // When web contents are destroyed, we will be called back again. 445 panel_->OnWindowClosing(); 446 return; 447 } 448 449 panel_->OnNativePanelClosed(); 450 if (window_) 451 window_->Close(); 452 window_closed_ = true; 453} 454 455void PanelView::ActivatePanel() { 456 window_->Activate(); 457} 458 459void PanelView::DeactivatePanel() { 460 if (!focused_) 461 return; 462 463#if defined(OS_WIN) 464 // Need custom behavior for always-on-top panels to avoid 465 // the OS activating a minimized panel when this one is 466 // deactivated. 467 if (always_on_top_) { 468 ::SetForegroundWindow(::GetDesktopWindow()); 469 return; 470 } 471#endif 472 473 window_->Deactivate(); 474} 475 476bool PanelView::IsPanelActive() const { 477 return focused_; 478} 479 480void PanelView::PreventActivationByOS(bool prevent_activation) { 481#if defined(OS_WIN) 482 // Set the flags "NoActivate" to make sure the minimized panels do not get 483 // activated by the OS. In addition, set "AppWindow" to make sure the 484 // minimized panels do appear in the taskbar and Alt-Tab menu if it is not 485 // in a stack. 486 int value_to_change = WS_EX_NOACTIVATE; 487 if (!panel_->stack()) 488 value_to_change |= WS_EX_APPWINDOW; 489 if (prevent_activation) 490 UpdateWindowAttribute(GWL_EXSTYLE, value_to_change, 0, false); 491 else 492 UpdateWindowAttribute(GWL_EXSTYLE, 0, value_to_change, false); 493#endif 494} 495 496gfx::NativeWindow PanelView::GetNativePanelWindow() { 497 return window_->GetNativeWindow(); 498} 499 500void PanelView::UpdatePanelTitleBar() { 501 UpdateWindowTitle(); 502 UpdateWindowIcon(); 503} 504 505void PanelView::UpdatePanelLoadingAnimations(bool should_animate) { 506 GetFrameView()->UpdateThrobber(); 507} 508 509void PanelView::PanelWebContentsFocused(content::WebContents* contents) { 510 web_view_->OnWebContentsFocused(contents); 511} 512 513void PanelView::PanelCut() { 514 // Nothing to do since we do not have panel-specific system menu. 515 NOTREACHED(); 516} 517 518void PanelView::PanelCopy() { 519 // Nothing to do since we do not have panel-specific system menu. 520 NOTREACHED(); 521} 522 523void PanelView::PanelPaste() { 524 // Nothing to do since we do not have panel-specific system menu. 525 NOTREACHED(); 526} 527 528void PanelView::DrawAttention(bool draw_attention) { 529 DCHECK((panel_->attention_mode() & Panel::USE_PANEL_ATTENTION) != 0); 530 531 if (is_drawing_attention_ == draw_attention) 532 return; 533 is_drawing_attention_ = draw_attention; 534 GetFrameView()->SchedulePaint(); 535 536 if ((panel_->attention_mode() & Panel::USE_SYSTEM_ATTENTION) != 0) { 537#if defined(OS_WIN) 538 // The default implementation of Widget::FlashFrame only flashes 5 times. 539 // We need more than that. 540 FLASHWINFO fwi; 541 fwi.cbSize = sizeof(fwi); 542 fwi.hwnd = views::HWNDForWidget(window_); 543 if (draw_attention) { 544 fwi.dwFlags = FLASHW_ALL; 545 fwi.uCount = panel::kNumberOfTimesToFlashPanelForAttention; 546 fwi.dwTimeout = 0; 547 } else { 548 // TODO(jianli): calling FlashWindowEx with FLASHW_STOP flag for the 549 // panel window has the same problem as the stack window. However, 550 // we cannot take the similar fix since there is no background window 551 // to replace for the regular panel window. More investigation is needed. 552 fwi.dwFlags = FLASHW_STOP; 553 } 554 ::FlashWindowEx(&fwi); 555#else 556 window_->FlashFrame(draw_attention); 557#endif 558 } 559} 560 561bool PanelView::IsDrawingAttention() const { 562 return is_drawing_attention_; 563} 564 565void PanelView::HandlePanelKeyboardEvent( 566 const content::NativeWebKeyboardEvent& event) { 567 views::FocusManager* focus_manager = GetFocusManager(); 568 if (focus_manager->shortcut_handling_suspended()) 569 return; 570 571 ui::Accelerator accelerator( 572 static_cast<ui::KeyboardCode>(event.windowsKeyCode), 573 content::GetModifiersFromNativeWebKeyboardEvent(event)); 574 if (event.type == WebKit::WebInputEvent::KeyUp) 575 accelerator.set_type(ui::ET_KEY_RELEASED); 576 focus_manager->ProcessAccelerator(accelerator); 577} 578 579void PanelView::FullScreenModeChanged(bool is_full_screen) { 580 if (is_full_screen) { 581 if (window_->IsVisible() && always_on_top_) 582 window_->Hide(); 583 } else { 584 if (!window_->IsVisible()) { 585 ShowPanelInactive(); 586 587#if defined(OS_WIN) 588 // When hiding and showing again a top-most window that belongs to a 589 // background application (i.e. the application is not a foreground one), 590 // the window may loose top-most placement even though its WS_EX_TOPMOST 591 // bit is still set. Re-issuing SetWindowsPos() returns the window to its 592 // top-most placement. 593 if (always_on_top_) 594 window_->SetAlwaysOnTop(true); 595#endif 596 } 597 } 598} 599 600bool PanelView::IsPanelAlwaysOnTop() const { 601 return always_on_top_; 602} 603 604void PanelView::SetPanelAlwaysOnTop(bool on_top) { 605 if (always_on_top_ == on_top) 606 return; 607 always_on_top_ = on_top; 608 609 window_->SetAlwaysOnTop(on_top); 610 window_->non_client_view()->Layout(); 611 window_->client_view()->Layout(); 612} 613 614void PanelView::UpdatePanelMinimizeRestoreButtonVisibility() { 615 GetFrameView()->UpdateTitlebarMinimizeRestoreButtonVisibility(); 616} 617 618void PanelView::SetWindowCornerStyle(panel::CornerStyle corner_style) { 619 GetFrameView()->SetWindowCornerStyle(corner_style); 620} 621 622void PanelView::PanelExpansionStateChanging(Panel::ExpansionState old_state, 623 Panel::ExpansionState new_state) { 624#if defined(OS_WIN) 625 // Live preview is only available since Windows 7. 626 if (base::win::GetVersion() < base::win::VERSION_WIN7) 627 return; 628 629 if (panel_->collection()->type() != PanelCollection::DOCKED) 630 return; 631 632 bool is_minimized = old_state != Panel::EXPANDED; 633 bool will_be_minimized = new_state != Panel::EXPANDED; 634 if (is_minimized == will_be_minimized) 635 return; 636 637 HWND native_window = views::HWNDForWidget(window_); 638 639 if (!thumbnailer_.get()) { 640 DCHECK(native_window); 641 thumbnailer_.reset(new TaskbarWindowThumbnailerWin(native_window, NULL)); 642 } 643 644 // Cache the image at this point. 645 if (will_be_minimized) { 646 // If the panel is still active (we will deactivate the minimizd panel at 647 // later time), we need to paint it immediately as inactive so that we can 648 // take a snapshot of inactive panel. 649 if (focused_) { 650 force_to_paint_as_inactive_ = true; 651 ::RedrawWindow(native_window, NULL, NULL, 652 RDW_NOCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW); 653 } 654 655 // Start the thumbnailer and capture the snapshot now. 656 thumbnailer_->Start(); 657 thumbnailer_->CaptureSnapshot(); 658 } else { 659 force_to_paint_as_inactive_ = false; 660 thumbnailer_->Stop(); 661 } 662 663#endif 664} 665 666gfx::Size PanelView::WindowSizeFromContentSize( 667 const gfx::Size& content_size) const { 668 gfx::Size frame = GetFrameView()->NonClientAreaSize(); 669 return gfx::Size(content_size.width() + frame.width(), 670 content_size.height() + frame.height()); 671} 672 673gfx::Size PanelView::ContentSizeFromWindowSize( 674 const gfx::Size& window_size) const { 675 gfx::Size frame = GetFrameView()->NonClientAreaSize(); 676 return gfx::Size(window_size.width() - frame.width(), 677 window_size.height() - frame.height()); 678} 679 680int PanelView::TitleOnlyHeight() const { 681 return panel::kTitlebarHeight; 682} 683 684void PanelView::MinimizePanelBySystem() { 685 window_->Minimize(); 686} 687 688bool PanelView::IsPanelMinimizedBySystem() const { 689 return window_->IsMinimized(); 690} 691 692bool PanelView::IsPanelShownOnActiveDesktop() const { 693#if defined(OS_WIN) 694 // Virtual desktop is not supported by the native Windows system. 695 return true; 696#else 697 NOTIMPLEMENTED(); 698 return true; 699#endif 700} 701 702void PanelView::ShowShadow(bool show) { 703#if defined(OS_WIN) 704 // The overlapped window has the shadow while the popup window does not have 705 // the shadow. 706 int overlap_style = WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU; 707 int popup_style = WS_POPUP; 708 UpdateWindowAttribute(GWL_STYLE, 709 show ? overlap_style : popup_style, 710 show ? popup_style : overlap_style, 711 true); 712#endif 713} 714 715void PanelView::AttachWebContents(content::WebContents* contents) { 716 web_view_->SetWebContents(contents); 717} 718 719void PanelView::DetachWebContents(content::WebContents* contents) { 720 web_view_->SetWebContents(NULL); 721} 722 723NativePanelTesting* PanelView::CreateNativePanelTesting() { 724 return new NativePanelTestingWin(this); 725} 726 727void PanelView::OnDisplayChanged() { 728 panel_->manager()->display_settings_provider()->OnDisplaySettingsChanged(); 729} 730 731void PanelView::OnWorkAreaChanged() { 732 panel_->manager()->display_settings_provider()->OnDisplaySettingsChanged(); 733} 734 735bool PanelView::WillProcessWorkAreaChange() const { 736 return true; 737} 738 739views::View* PanelView::GetContentsView() { 740 return this; 741} 742 743views::NonClientFrameView* PanelView::CreateNonClientFrameView( 744 views::Widget* widget) { 745 PanelFrameView* frame_view = new PanelFrameView(this); 746 frame_view->Init(); 747 return frame_view; 748} 749 750bool PanelView::CanResize() const { 751 return true; 752} 753 754bool PanelView::CanMaximize() const { 755 return false; 756} 757 758string16 PanelView::GetWindowTitle() const { 759 return panel_->GetWindowTitle(); 760} 761 762gfx::ImageSkia PanelView::GetWindowAppIcon() { 763 gfx::Image app_icon = panel_->app_icon(); 764 if (app_icon.IsEmpty()) 765 return GetWindowIcon(); 766 else 767 return *app_icon.ToImageSkia(); 768} 769 770gfx::ImageSkia PanelView::GetWindowIcon() { 771 gfx::Image icon = panel_->GetCurrentPageIcon(); 772 return icon.IsEmpty() ? gfx::ImageSkia() : *icon.ToImageSkia(); 773} 774 775void PanelView::WindowClosing() { 776 // When closing a panel via window.close, API or the close button, 777 // ClosePanel() is called first, destroying the native |window_| 778 // which results in this method being called. ClosePanel() sets 779 // |window_closed_| to NULL. 780 // If we still have a |window_closed_| here, the close was triggered by the 781 // OS, (e.g. clicking on taskbar menu), which destroys the native |window_| 782 // without invoking ClosePanel() beforehand. 783 if (!window_closed_) { 784 panel_->OnWindowClosing(); 785 ClosePanel(); 786 DCHECK(window_closed_); 787 } 788} 789 790void PanelView::DeleteDelegate() { 791 delete this; 792} 793 794void PanelView::OnWindowBeginUserBoundsChange() { 795 user_resizing_ = true; 796 panel_->OnPanelStartUserResizing(); 797 798#if defined(OS_WIN) 799 StackedPanelCollection* stack = panel_->stack(); 800 if (stack) { 801 // Listen to WM_SIZING message in order to find out whether the interior 802 // edge is being resized such that the specific maximum size could be 803 // passed to the system. 804 if (panel_->stack()->GetPanelBelow(panel_.get())) { 805 ui::HWNDSubclass::AddFilterToTarget(views::HWNDForWidget(window_), this); 806 user_resizing_interior_stacked_panel_edge_ = false; 807 } 808 809 // Keep track of the original full size of the resizing panel such that it 810 // can be restored to this size once it is shrunk to minimized state. 811 original_full_size_of_resizing_panel_ = panel_->full_size(); 812 813 // Keep track of the original full size of the panel below the resizing 814 // panel such that it can be restored to this size once it is shrunk to 815 // minimized state. 816 Panel* below_panel = stack->GetPanelBelow(panel_.get()); 817 if (below_panel && !below_panel->IsMinimized()) { 818 original_full_size_of_panel_below_resizing_panel_ = 819 below_panel->full_size(); 820 } 821 } 822#endif 823} 824 825void PanelView::OnWindowEndUserBoundsChange() { 826 user_resizing_ = false; 827 panel_->OnPanelEndUserResizing(); 828 829 // No need to proceed with post-resizing update when there is no size change. 830 gfx::Rect new_bounds = window_->GetWindowBoundsInScreen(); 831 if (bounds_ == new_bounds) 832 return; 833 bounds_ = new_bounds; 834 835 panel_->IncreaseMaxSize(bounds_.size()); 836 panel_->set_full_size(bounds_.size()); 837 838#if defined(OS_WIN) 839 StackedPanelCollection* stack = panel_->stack(); 840 if (stack) { 841 // No need to listen to WM_SIZING message any more. 842 ui::HWNDSubclass::RemoveFilterFromAllTargets(this); 843 844 // If the height of resizing panel shrinks close to the titlebar height, 845 // treate it as minimized. This could occur when the user is dragging 846 // 1) the top edge of the top panel downward to shrink it; or 847 // 2) the bottom edge of any panel upward to shrink it. 848 if (panel_->GetBounds().height() < 849 kStackedPanelHeightShrinkThresholdToBecomeMinimized) { 850 stack->MinimizePanel(panel_.get()); 851 panel_->set_full_size(original_full_size_of_resizing_panel_); 852 } 853 854 // If the height of panel below the resizing panel shrinks close to the 855 // titlebar height, treat it as minimized. This could occur when the user 856 // is dragging the bottom edge of non-bottom panel downward to expand it 857 // and also shrink the panel below. 858 Panel* below_panel = stack->GetPanelBelow(panel_.get()); 859 if (below_panel && !below_panel->IsMinimized() && 860 below_panel->GetBounds().height() < 861 kStackedPanelHeightShrinkThresholdToBecomeMinimized) { 862 stack->MinimizePanel(below_panel); 863 below_panel->set_full_size( 864 original_full_size_of_panel_below_resizing_panel_); 865 } 866 } 867#endif 868 869 panel_->collection()->RefreshLayout(); 870} 871 872views::Widget* PanelView::GetWidget() { 873 return window_; 874} 875 876const views::Widget* PanelView::GetWidget() const { 877 return window_; 878} 879 880void PanelView::UpdateLoadingAnimations(bool should_animate) { 881 GetFrameView()->UpdateThrobber(); 882} 883 884void PanelView::UpdateWindowTitle() { 885 window_->UpdateWindowTitle(); 886 GetFrameView()->UpdateTitle(); 887} 888 889void PanelView::UpdateWindowIcon() { 890 window_->UpdateWindowIcon(); 891 GetFrameView()->UpdateIcon(); 892} 893 894void PanelView::Layout() { 895 // |web_view_| might not be created yet when the window is first created. 896 if (web_view_) 897 web_view_->SetBounds(0, 0, width(), height()); 898 OnViewWasResized(); 899} 900 901gfx::Size PanelView::GetMinimumSize() { 902 // If the panel is minimized, it can be rendered to very small size, like 903 // 4-pixel lines when it is docked. Otherwise, its size should not be less 904 // than its minimum size. 905 return panel_->IsMinimized() ? gfx::Size() : 906 gfx::Size(panel::kPanelMinWidth, panel::kPanelMinHeight); 907} 908 909gfx::Size PanelView::GetMaximumSize() { 910 // If the user is resizing a stacked panel by its bottom edge, make sure its 911 // height cannot grow more than what the panel below it could offer. This is 912 // because growing a stacked panel by y amount will shrink the panel below it 913 // by same amount and we do not want the panel below it being shrunk to be 914 // smaller than the titlebar. 915#if defined(OS_WIN) 916 if (panel_->stack() && user_resizing_interior_stacked_panel_edge_) { 917 Panel* below_panel = panel_->stack()->GetPanelBelow(panel_.get()); 918 if (below_panel && !below_panel->IsMinimized()) { 919 return gfx::Size(0, below_panel->GetBounds().bottom() - 920 panel_->GetBounds().y() - panel::kTitlebarHeight); 921 } 922 } 923#endif 924 return gfx::Size(); 925} 926 927bool PanelView::AcceleratorPressed(const ui::Accelerator& accelerator) { 928 if (mouse_pressed_ && accelerator.key_code() == ui::VKEY_ESCAPE) { 929 OnTitlebarMouseCaptureLost(); 930 return true; 931 } 932 933 // No other accelerator is allowed when the drag begins. 934 if (mouse_dragging_state_ == DRAGGING_STARTED) 935 return true; 936 937 const std::map<ui::Accelerator, int>& accelerator_table = 938 GetAcceleratorTable(); 939 std::map<ui::Accelerator, int>::const_iterator iter = 940 accelerator_table.find(accelerator); 941 DCHECK(iter != accelerator_table.end()); 942 return panel_->ExecuteCommandIfEnabled(iter->second); 943} 944 945void PanelView::OnWidgetDestroying(views::Widget* widget) { 946 window_ = NULL; 947} 948 949void PanelView::OnWidgetActivationChanged(views::Widget* widget, bool active) { 950#if defined(OS_WIN) 951 // WM_NCACTIVATED could be sent when an active window is being destroyed on 952 // Windows. We need to guard against this. 953 if (window_closed_) 954 return; 955 956 bool focused = active; 957 if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_NATIVE) { 958 // The panel window is in focus (actually accepting keystrokes) if it is 959 // active and belongs to a foreground application. 960 focused = active && 961 views::HWNDForWidget(widget) == ::GetForegroundWindow(); 962 } 963#else 964 NOTIMPLEMENTED(); 965 bool focused = active; 966#endif 967 968 if (focused_ == focused) 969 return; 970 focused_ = focused; 971 972 // Expand the panel if the minimized panel is activated by means other than 973 // clicking on its titlebar. This is the workaround to support restoring the 974 // minimized panel by other means, like alt-tabbing, win-tabbing, or clicking 975 // the taskbar icon. Note that this workaround does not work for one edge 976 // case: the mouse happens to be at the minimized panel when the user tries to 977 // bring up the panel with the above alternatives. 978 // When the user clicks on the minimized panel, the panel expansion will be 979 // done when we process the mouse button pressed message. 980#if defined(OS_WIN) 981 if (focused_ && panel_->IsMinimized() && 982 panel_->collection()->type() == PanelCollection::DOCKED && 983 gfx::Screen::GetScreenFor(widget->GetNativeWindow())-> 984 GetWindowUnderCursor() != widget->GetNativeWindow()) { 985 panel_->Restore(); 986 } 987#endif 988 989 panel()->OnActiveStateChanged(focused); 990 991 // Give web contents view a chance to set focus to the appropriate element. 992 if (focused_) { 993 content::WebContents* web_contents = panel_->GetWebContents(); 994 if (web_contents) 995 web_contents->GetView()->RestoreFocus(); 996 } 997} 998 999void PanelView::OnWidgetBoundsChanged(views::Widget* widget, 1000 const gfx::Rect& new_bounds) { 1001 if (user_resizing_) 1002 panel()->collection()->OnPanelResizedByMouse(panel(), new_bounds); 1003} 1004 1005bool PanelView::OnTitlebarMousePressed(const gfx::Point& mouse_location) { 1006 mouse_pressed_ = true; 1007 mouse_dragging_state_ = NO_DRAGGING; 1008 last_mouse_location_ = mouse_location; 1009 return true; 1010} 1011 1012bool PanelView::OnTitlebarMouseDragged(const gfx::Point& mouse_location) { 1013 if (!mouse_pressed_) 1014 return false; 1015 1016 if (mouse_dragging_state_ == NO_DRAGGING && 1017 ExceededDragThreshold(mouse_location - last_mouse_location_)) { 1018 // When a drag begins, we do not want to the client area to still receive 1019 // the focus. We do not need to do this for the unfocused minimized panel. 1020 if (!panel_->IsMinimized()) { 1021 old_focused_view_ = GetFocusManager()->GetFocusedView(); 1022 GetFocusManager()->SetFocusedView(GetFrameView()); 1023 } 1024 1025 panel_->manager()->StartDragging(panel_.get(), last_mouse_location_); 1026 mouse_dragging_state_ = DRAGGING_STARTED; 1027 } 1028 if (mouse_dragging_state_ == DRAGGING_STARTED) { 1029 panel_->manager()->Drag(mouse_location); 1030 1031 // Once in drag, update |last_mouse_location_| on each drag fragment, since 1032 // we already dragged the panel up to the current mouse location. 1033 last_mouse_location_ = mouse_location; 1034 } 1035 return true; 1036} 1037 1038bool PanelView::OnTitlebarMouseReleased(panel::ClickModifier modifier) { 1039 if (mouse_dragging_state_ != NO_DRAGGING) { 1040 // Ensure dragging a minimized panel does not leave it activated. 1041 // Windows activates a panel on mouse-down, regardless of our attempts 1042 // to prevent activation of a minimized panel. Now that we know mouse-down 1043 // resulted in a mouse-drag, we need to ensure the minimized panel is 1044 // deactivated. 1045 if (panel_->IsMinimized() && focused_) 1046 panel_->Deactivate(); 1047 1048 if (mouse_dragging_state_ == DRAGGING_STARTED) { 1049 // When a drag ends, restore the focus. 1050 if (old_focused_view_) { 1051 GetFocusManager()->SetFocusedView(old_focused_view_); 1052 old_focused_view_ = NULL; 1053 } 1054 return EndDragging(false); 1055 } 1056 1057 // The panel drag was cancelled before the mouse is released. Do not 1058 // treat this as a click. 1059 return true; 1060 } 1061 1062 panel_->OnTitlebarClicked(modifier); 1063 return true; 1064} 1065 1066bool PanelView::OnTitlebarMouseCaptureLost() { 1067 if (mouse_dragging_state_ == DRAGGING_STARTED) 1068 return EndDragging(true); 1069 return true; 1070} 1071 1072bool PanelView::EndDragging(bool cancelled) { 1073 // Only handle clicks that started in our window. 1074 if (!mouse_pressed_) 1075 return false; 1076 mouse_pressed_ = false; 1077 1078 mouse_dragging_state_ = DRAGGING_ENDED; 1079 panel_->manager()->EndDragging(cancelled); 1080 return true; 1081} 1082 1083PanelFrameView* PanelView::GetFrameView() const { 1084 return static_cast<PanelFrameView*>(window_->non_client_view()->frame_view()); 1085} 1086 1087bool PanelView::IsAnimatingBounds() const { 1088 if (bounds_animator_.get() && bounds_animator_->is_animating()) 1089 return true; 1090 StackedPanelCollection* stack = panel_->stack(); 1091 if (!stack) 1092 return false; 1093 return stack->IsAnimatingPanelBounds(panel_.get()); 1094} 1095 1096bool PanelView::IsWithinResizingArea(const gfx::Point& mouse_location) const { 1097 gfx::Rect bounds = window_->GetWindowBoundsInScreen(); 1098 DCHECK(bounds.Contains(mouse_location)); 1099 return mouse_location.x() < bounds.x() + kResizeInsideBoundsSize || 1100 mouse_location.x() >= bounds.right() - kResizeInsideBoundsSize || 1101 mouse_location.y() < bounds.y() + kResizeInsideBoundsSize || 1102 mouse_location.y() >= bounds.bottom() - kResizeInsideBoundsSize; 1103} 1104 1105#if defined(OS_WIN) 1106void PanelView::UpdateWindowAttribute(int attribute_index, 1107 int attribute_value_to_set, 1108 int attribute_value_to_reset, 1109 bool update_frame) { 1110 HWND native_window = views::HWNDForWidget(window_); 1111 int value = ::GetWindowLong(native_window, attribute_index); 1112 int expected_value = value; 1113 if (attribute_value_to_set) 1114 expected_value |= attribute_value_to_set; 1115 if (attribute_value_to_reset) 1116 expected_value &= ~attribute_value_to_reset; 1117 if (value != expected_value) 1118 ::SetWindowLong(native_window, attribute_index, expected_value); 1119 1120 // Per MSDN, if any of the frame styles is changed, SetWindowPos with the 1121 // SWP_FRAMECHANGED flag must be called in order for the cached window data 1122 // to be updated properly. 1123 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx 1124 if (update_frame) { 1125 ::SetWindowPos(native_window, NULL, 0, 0, 0, 0, 1126 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | 1127 SWP_NOZORDER | SWP_NOACTIVATE); 1128 } 1129} 1130#endif 1131 1132void PanelView::OnViewWasResized() { 1133#if defined(OS_WIN) && !defined(USE_AURA) 1134 content::WebContents* web_contents = panel_->GetWebContents(); 1135 if (!web_view_ || !web_contents) 1136 return; 1137 1138 // When the panel is frameless or has thin frame, the mouse resizing should 1139 // also be triggered from the part of client area that is close to the window 1140 // frame. 1141 int width = web_view_->size().width(); 1142 int height = web_view_->size().height(); 1143 // Compute the thickness of the client area that needs to be counted towards 1144 // mouse resizing. 1145 int thickness_for_mouse_resizing = 1146 kResizeInsideBoundsSize - GetFrameView()->BorderThickness(); 1147 DCHECK(thickness_for_mouse_resizing > 0); 1148 SkRegion* region = new SkRegion; 1149 region->op(0, 0, thickness_for_mouse_resizing, height, SkRegion::kUnion_Op); 1150 region->op(width - thickness_for_mouse_resizing, 0, width, height, 1151 SkRegion::kUnion_Op); 1152 region->op(0, height - thickness_for_mouse_resizing, width, height, 1153 SkRegion::kUnion_Op); 1154 web_contents->GetRenderViewHost()->GetView()->SetClickthroughRegion(region); 1155#endif 1156} 1157