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 "ash/frame/custom_frame_view_ash.h" 6 7 #include <algorithm> 8 #include <vector> 9 10 #include "ash/ash_switches.h" 11 #include "ash/frame/caption_buttons/frame_caption_button_container_view.h" 12 #include "ash/frame/default_header_painter.h" 13 #include "ash/frame/frame_border_hit_test_controller.h" 14 #include "ash/frame/frame_util.h" 15 #include "ash/frame/header_painter.h" 16 #include "ash/session/session_state_delegate.h" 17 #include "ash/shell.h" 18 #include "ash/shell_observer.h" 19 #include "ash/wm/immersive_fullscreen_controller.h" 20 #include "ash/wm/window_state.h" 21 #include "ash/wm/window_state_delegate.h" 22 #include "ash/wm/window_state_observer.h" 23 #include "base/command_line.h" 24 #include "ui/aura/client/aura_constants.h" 25 #include "ui/aura/window.h" 26 #include "ui/aura/window_observer.h" 27 #include "ui/gfx/canvas.h" 28 #include "ui/gfx/image/image.h" 29 #include "ui/gfx/rect.h" 30 #include "ui/gfx/rect_conversions.h" 31 #include "ui/gfx/size.h" 32 #include "ui/views/controls/image_view.h" 33 #include "ui/views/view.h" 34 #include "ui/views/view_targeter.h" 35 #include "ui/views/widget/widget.h" 36 #include "ui/views/widget/widget_delegate.h" 37 38 namespace { 39 40 /////////////////////////////////////////////////////////////////////////////// 41 // CustomFrameViewAshWindowStateDelegate 42 43 // Handles a user's fullscreen request (Shift+F4/F4). Puts the window into 44 // immersive fullscreen if immersive fullscreen is enabled for non-browser 45 // windows. 46 class CustomFrameViewAshWindowStateDelegate 47 : public ash::wm::WindowStateDelegate, 48 public ash::wm::WindowStateObserver, 49 public aura::WindowObserver { 50 public: 51 CustomFrameViewAshWindowStateDelegate( 52 ash::wm::WindowState* window_state, 53 ash::CustomFrameViewAsh* custom_frame_view) 54 : window_state_(NULL) { 55 immersive_fullscreen_controller_.reset( 56 new ash::ImmersiveFullscreenController); 57 custom_frame_view->InitImmersiveFullscreenControllerForView( 58 immersive_fullscreen_controller_.get()); 59 60 // Add a window state observer to exit fullscreen properly in case 61 // fullscreen is exited without going through 62 // WindowState::ToggleFullscreen(). This is the case when exiting 63 // immersive fullscreen via the "Restore" window control. 64 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048 65 window_state_ = window_state; 66 window_state_->AddObserver(this); 67 window_state_->window()->AddObserver(this); 68 } 69 virtual ~CustomFrameViewAshWindowStateDelegate() { 70 if (window_state_) { 71 window_state_->RemoveObserver(this); 72 window_state_->window()->RemoveObserver(this); 73 } 74 } 75 private: 76 // Overridden from ash::wm::WindowStateDelegate: 77 virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) OVERRIDE { 78 bool enter_fullscreen = !window_state->IsFullscreen(); 79 if (enter_fullscreen) { 80 window_state->window()->SetProperty(aura::client::kShowStateKey, 81 ui::SHOW_STATE_FULLSCREEN); 82 } else { 83 window_state->Restore(); 84 } 85 if (immersive_fullscreen_controller_) { 86 immersive_fullscreen_controller_->SetEnabled( 87 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER, 88 enter_fullscreen); 89 } 90 return true; 91 } 92 // Overridden from aura::WindowObserver: 93 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { 94 window_state_->RemoveObserver(this); 95 window_state_->window()->RemoveObserver(this); 96 window_state_ = NULL; 97 } 98 // Overridden from ash::wm::WindowStateObserver: 99 virtual void OnPostWindowStateTypeChange( 100 ash::wm::WindowState* window_state, 101 ash::wm::WindowStateType old_type) OVERRIDE { 102 if (!window_state->IsFullscreen() && 103 !window_state->IsMinimized() && 104 immersive_fullscreen_controller_.get() && 105 immersive_fullscreen_controller_->IsEnabled()) { 106 immersive_fullscreen_controller_->SetEnabled( 107 ash::ImmersiveFullscreenController::WINDOW_TYPE_OTHER, 108 false); 109 } 110 } 111 112 ash::wm::WindowState* window_state_; 113 scoped_ptr<ash::ImmersiveFullscreenController> 114 immersive_fullscreen_controller_; 115 116 DISALLOW_COPY_AND_ASSIGN(CustomFrameViewAshWindowStateDelegate); 117 }; 118 119 } // namespace 120 121 namespace ash { 122 123 /////////////////////////////////////////////////////////////////////////////// 124 // CustomFrameViewAsh::HeaderView 125 126 // View which paints the header. It slides off and on screen in immersive 127 // fullscreen. 128 class CustomFrameViewAsh::HeaderView 129 : public views::View, 130 public ImmersiveFullscreenController::Delegate, 131 public ShellObserver { 132 public: 133 // |frame| is the widget that the caption buttons act on. 134 explicit HeaderView(views::Widget* frame); 135 virtual ~HeaderView(); 136 137 // Schedules a repaint for the entire title. 138 void SchedulePaintForTitle(); 139 140 // Tells the window controls to reset themselves to the normal state. 141 void ResetWindowControls(); 142 143 // Returns the amount of the view's pixels which should be on screen. 144 int GetPreferredOnScreenHeight() const; 145 146 // Returns the view's preferred height. 147 int GetPreferredHeight() const; 148 149 // Returns the view's minimum width. 150 int GetMinimumWidth() const; 151 152 void UpdateAvatarIcon(); 153 154 void SizeConstraintsChanged(); 155 156 // views::View: 157 virtual void Layout() OVERRIDE; 158 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 159 virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE; 160 161 // ShellObserver: 162 virtual void OnMaximizeModeStarted() OVERRIDE; 163 virtual void OnMaximizeModeEnded() OVERRIDE; 164 165 FrameCaptionButtonContainerView* caption_button_container() { 166 return caption_button_container_; 167 } 168 169 views::View* avatar_icon() const { 170 return avatar_icon_; 171 } 172 173 private: 174 // ImmersiveFullscreenController::Delegate: 175 virtual void OnImmersiveRevealStarted() OVERRIDE; 176 virtual void OnImmersiveRevealEnded() OVERRIDE; 177 virtual void OnImmersiveFullscreenExited() OVERRIDE; 178 virtual void SetVisibleFraction(double visible_fraction) OVERRIDE; 179 virtual std::vector<gfx::Rect> GetVisibleBoundsInScreen() const OVERRIDE; 180 181 // The widget that the caption buttons act on. 182 views::Widget* frame_; 183 184 // Helper for painting the header. 185 scoped_ptr<DefaultHeaderPainter> header_painter_; 186 187 views::ImageView* avatar_icon_; 188 189 // View which contains the window caption buttons. 190 FrameCaptionButtonContainerView* caption_button_container_; 191 192 // The fraction of the header's height which is visible while in fullscreen. 193 // This value is meaningless when not in fullscreen. 194 double fullscreen_visible_fraction_; 195 196 DISALLOW_COPY_AND_ASSIGN(HeaderView); 197 }; 198 199 CustomFrameViewAsh::HeaderView::HeaderView(views::Widget* frame) 200 : frame_(frame), 201 header_painter_(new ash::DefaultHeaderPainter), 202 avatar_icon_(NULL), 203 caption_button_container_(NULL), 204 fullscreen_visible_fraction_(0) { 205 FrameCaptionButtonContainerView::MinimizeAllowed minimize_allowed = 206 frame_->widget_delegate()->CanMinimize() ? 207 FrameCaptionButtonContainerView::MINIMIZE_ALLOWED : 208 FrameCaptionButtonContainerView::MINIMIZE_DISALLOWED; 209 caption_button_container_ = new FrameCaptionButtonContainerView(frame_, 210 minimize_allowed); 211 caption_button_container_->UpdateSizeButtonVisibility(); 212 AddChildView(caption_button_container_); 213 214 header_painter_->Init(frame_, this, caption_button_container_); 215 UpdateAvatarIcon(); 216 217 Shell::GetInstance()->AddShellObserver(this); 218 } 219 220 CustomFrameViewAsh::HeaderView::~HeaderView() { 221 Shell::GetInstance()->RemoveShellObserver(this); 222 } 223 224 void CustomFrameViewAsh::HeaderView::SchedulePaintForTitle() { 225 header_painter_->SchedulePaintForTitle(); 226 } 227 228 void CustomFrameViewAsh::HeaderView::ResetWindowControls() { 229 caption_button_container_->ResetWindowControls(); 230 } 231 232 int CustomFrameViewAsh::HeaderView::GetPreferredOnScreenHeight() const { 233 if (frame_->IsFullscreen()) { 234 return static_cast<int>( 235 GetPreferredHeight() * fullscreen_visible_fraction_); 236 } 237 return GetPreferredHeight(); 238 } 239 240 int CustomFrameViewAsh::HeaderView::GetPreferredHeight() const { 241 return header_painter_->GetHeaderHeightForPainting(); 242 } 243 244 int CustomFrameViewAsh::HeaderView::GetMinimumWidth() const { 245 return header_painter_->GetMinimumHeaderWidth(); 246 } 247 248 void CustomFrameViewAsh::HeaderView::UpdateAvatarIcon() { 249 SessionStateDelegate* delegate = 250 Shell::GetInstance()->session_state_delegate(); 251 aura::Window* window = frame_->GetNativeView(); 252 bool show = delegate->ShouldShowAvatar(window); 253 if (!show) { 254 if (!avatar_icon_) 255 return; 256 delete avatar_icon_; 257 avatar_icon_ = NULL; 258 } else { 259 gfx::ImageSkia image = GetAvatarImageForContext( 260 delegate->GetBrowserContextForWindow(window)).AsImageSkia(); 261 DCHECK(!image.isNull()); 262 DCHECK_EQ(image.width(), image.height()); 263 if (!avatar_icon_) { 264 avatar_icon_ = new views::ImageView(); 265 AddChildView(avatar_icon_); 266 } 267 avatar_icon_->SetImage(image); 268 } 269 header_painter_->UpdateLeftHeaderView(avatar_icon_); 270 Layout(); 271 } 272 273 void CustomFrameViewAsh::HeaderView::SizeConstraintsChanged() { 274 caption_button_container_->ResetWindowControls(); 275 caption_button_container_->UpdateSizeButtonVisibility(); 276 Layout(); 277 } 278 279 /////////////////////////////////////////////////////////////////////////////// 280 // CustomFrameViewAsh::HeaderView, views::View overrides: 281 282 void CustomFrameViewAsh::HeaderView::Layout() { 283 header_painter_->LayoutHeader(); 284 } 285 286 void CustomFrameViewAsh::HeaderView::OnPaint(gfx::Canvas* canvas) { 287 bool paint_as_active = 288 frame_->non_client_view()->frame_view()->ShouldPaintAsActive(); 289 caption_button_container_->SetPaintAsActive(paint_as_active); 290 291 HeaderPainter::Mode header_mode = paint_as_active ? 292 HeaderPainter::MODE_ACTIVE : HeaderPainter::MODE_INACTIVE; 293 header_painter_->PaintHeader(canvas, header_mode); 294 } 295 296 void CustomFrameViewAsh::HeaderView:: 297 ChildPreferredSizeChanged(views::View* child) { 298 // FrameCaptionButtonContainerView animates the visibility changes in 299 // UpdateSizeButtonVisibility(false). Due to this a new size is not available 300 // until the completion of the animation. Layout in response to the preferred 301 // size changes. 302 if (child != caption_button_container_) 303 return; 304 parent()->Layout(); 305 } 306 307 /////////////////////////////////////////////////////////////////////////////// 308 // CustomFrameViewAsh::HeaderView, ShellObserver overrides: 309 310 void CustomFrameViewAsh::HeaderView::OnMaximizeModeStarted() { 311 caption_button_container_->UpdateSizeButtonVisibility(); 312 parent()->Layout(); 313 } 314 315 void CustomFrameViewAsh::HeaderView::OnMaximizeModeEnded() { 316 caption_button_container_->UpdateSizeButtonVisibility(); 317 parent()->Layout(); 318 } 319 320 /////////////////////////////////////////////////////////////////////////////// 321 // CustomFrameViewAsh::HeaderView, 322 // ImmersiveFullscreenController::Delegate overrides: 323 324 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealStarted() { 325 fullscreen_visible_fraction_ = 0; 326 SetPaintToLayer(true); 327 SetFillsBoundsOpaquely(false); 328 parent()->Layout(); 329 } 330 331 void CustomFrameViewAsh::HeaderView::OnImmersiveRevealEnded() { 332 fullscreen_visible_fraction_ = 0; 333 SetPaintToLayer(false); 334 parent()->Layout(); 335 } 336 337 void CustomFrameViewAsh::HeaderView::OnImmersiveFullscreenExited() { 338 fullscreen_visible_fraction_ = 0; 339 SetPaintToLayer(false); 340 parent()->Layout(); 341 } 342 343 void CustomFrameViewAsh::HeaderView::SetVisibleFraction( 344 double visible_fraction) { 345 if (fullscreen_visible_fraction_ != visible_fraction) { 346 fullscreen_visible_fraction_ = visible_fraction; 347 parent()->Layout(); 348 } 349 } 350 351 std::vector<gfx::Rect> 352 CustomFrameViewAsh::HeaderView::GetVisibleBoundsInScreen() const { 353 // TODO(pkotwicz): Implement views::View::ConvertRectToScreen(). 354 gfx::Rect visible_bounds(GetVisibleBounds()); 355 gfx::Point visible_origin_in_screen(visible_bounds.origin()); 356 views::View::ConvertPointToScreen(this, &visible_origin_in_screen); 357 std::vector<gfx::Rect> bounds_in_screen; 358 bounds_in_screen.push_back( 359 gfx::Rect(visible_origin_in_screen, visible_bounds.size())); 360 return bounds_in_screen; 361 } 362 363 /////////////////////////////////////////////////////////////////////////////// 364 // CustomFrameViewAsh::OverlayView 365 366 // View which takes up the entire widget and contains the HeaderView. HeaderView 367 // is a child of OverlayView to avoid creating a larger texture than necessary 368 // when painting the HeaderView to its own layer. 369 class CustomFrameViewAsh::OverlayView : public views::View, 370 public views::ViewTargeterDelegate { 371 public: 372 explicit OverlayView(HeaderView* header_view); 373 virtual ~OverlayView(); 374 375 // views::View: 376 virtual void Layout() OVERRIDE; 377 378 private: 379 // views::ViewTargeterDelegate: 380 virtual bool DoesIntersectRect(const views::View* target, 381 const gfx::Rect& rect) const OVERRIDE; 382 383 HeaderView* header_view_; 384 385 DISALLOW_COPY_AND_ASSIGN(OverlayView); 386 }; 387 388 CustomFrameViewAsh::OverlayView::OverlayView(HeaderView* header_view) 389 : header_view_(header_view) { 390 AddChildView(header_view); 391 SetEventTargeter( 392 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); 393 } 394 395 CustomFrameViewAsh::OverlayView::~OverlayView() { 396 } 397 398 /////////////////////////////////////////////////////////////////////////////// 399 // CustomFrameViewAsh::OverlayView, views::View overrides: 400 401 void CustomFrameViewAsh::OverlayView::Layout() { 402 // Layout |header_view_| because layout affects the result of 403 // GetPreferredOnScreenHeight(). 404 header_view_->Layout(); 405 406 int onscreen_height = header_view_->GetPreferredOnScreenHeight(); 407 if (onscreen_height == 0) { 408 header_view_->SetVisible(false); 409 } else { 410 int height = header_view_->GetPreferredHeight(); 411 header_view_->SetBounds(0, onscreen_height - height, width(), height); 412 header_view_->SetVisible(true); 413 } 414 } 415 416 /////////////////////////////////////////////////////////////////////////////// 417 // CustomFrameViewAsh::OverlayView, views::ViewTargeterDelegate overrides: 418 419 bool CustomFrameViewAsh::OverlayView::DoesIntersectRect( 420 const views::View* target, 421 const gfx::Rect& rect) const { 422 CHECK_EQ(target, this); 423 // Grab events in the header view. Return false for other events so that they 424 // can be handled by the client view. 425 return header_view_->HitTestRect(rect); 426 } 427 428 //////////////////////////////////////////////////////////////////////////////// 429 // CustomFrameViewAsh, public: 430 431 // static 432 const char CustomFrameViewAsh::kViewClassName[] = "CustomFrameViewAsh"; 433 434 CustomFrameViewAsh::CustomFrameViewAsh(views::Widget* frame) 435 : frame_(frame), 436 header_view_(new HeaderView(frame)), 437 frame_border_hit_test_controller_( 438 new FrameBorderHitTestController(frame_)) { 439 // |header_view_| is set as the non client view's overlay view so that it can 440 // overlay the web contents in immersive fullscreen. 441 frame->non_client_view()->SetOverlayView(new OverlayView(header_view_)); 442 443 // A delegate for a more complex way of fullscreening the window may already 444 // be set. This is the case for packaged apps. 445 wm::WindowState* window_state = wm::GetWindowState(frame->GetNativeWindow()); 446 if (!window_state->HasDelegate()) { 447 window_state->SetDelegate(scoped_ptr<wm::WindowStateDelegate>( 448 new CustomFrameViewAshWindowStateDelegate( 449 window_state, this))); 450 } 451 } 452 453 CustomFrameViewAsh::~CustomFrameViewAsh() { 454 } 455 456 void CustomFrameViewAsh::InitImmersiveFullscreenControllerForView( 457 ImmersiveFullscreenController* immersive_fullscreen_controller) { 458 immersive_fullscreen_controller->Init(header_view_, frame_, header_view_); 459 } 460 461 //////////////////////////////////////////////////////////////////////////////// 462 // CustomFrameViewAsh, views::NonClientFrameView overrides: 463 464 gfx::Rect CustomFrameViewAsh::GetBoundsForClientView() const { 465 gfx::Rect client_bounds = bounds(); 466 client_bounds.Inset(0, NonClientTopBorderHeight(), 0, 0); 467 return client_bounds; 468 } 469 470 gfx::Rect CustomFrameViewAsh::GetWindowBoundsForClientBounds( 471 const gfx::Rect& client_bounds) const { 472 gfx::Rect window_bounds = client_bounds; 473 window_bounds.Inset(0, -NonClientTopBorderHeight(), 0, 0); 474 return window_bounds; 475 } 476 477 int CustomFrameViewAsh::NonClientHitTest(const gfx::Point& point) { 478 return FrameBorderHitTestController::NonClientHitTest(this, 479 header_view_->caption_button_container(), point); 480 } 481 482 void CustomFrameViewAsh::GetWindowMask(const gfx::Size& size, 483 gfx::Path* window_mask) { 484 // No window masks in Aura. 485 } 486 487 void CustomFrameViewAsh::ResetWindowControls() { 488 header_view_->ResetWindowControls(); 489 } 490 491 void CustomFrameViewAsh::UpdateWindowIcon() { 492 } 493 494 void CustomFrameViewAsh::UpdateWindowTitle() { 495 header_view_->SchedulePaintForTitle(); 496 } 497 498 void CustomFrameViewAsh::SizeConstraintsChanged() { 499 header_view_->SizeConstraintsChanged(); 500 } 501 502 //////////////////////////////////////////////////////////////////////////////// 503 // CustomFrameViewAsh, views::View overrides: 504 505 gfx::Size CustomFrameViewAsh::GetPreferredSize() const { 506 gfx::Size pref = frame_->client_view()->GetPreferredSize(); 507 gfx::Rect bounds(0, 0, pref.width(), pref.height()); 508 return frame_->non_client_view()->GetWindowBoundsForClientBounds( 509 bounds).size(); 510 } 511 512 const char* CustomFrameViewAsh::GetClassName() const { 513 return kViewClassName; 514 } 515 516 gfx::Size CustomFrameViewAsh::GetMinimumSize() const { 517 gfx::Size min_client_view_size(frame_->client_view()->GetMinimumSize()); 518 return gfx::Size( 519 std::max(header_view_->GetMinimumWidth(), min_client_view_size.width()), 520 NonClientTopBorderHeight() + min_client_view_size.height()); 521 } 522 523 gfx::Size CustomFrameViewAsh::GetMaximumSize() const { 524 gfx::Size max_client_size(frame_->client_view()->GetMaximumSize()); 525 int width = 0; 526 int height = 0; 527 528 if (max_client_size.width() > 0) 529 width = std::max(header_view_->GetMinimumWidth(), max_client_size.width()); 530 if (max_client_size.height() > 0) 531 height = NonClientTopBorderHeight() + max_client_size.height(); 532 533 return gfx::Size(width, height); 534 } 535 536 void CustomFrameViewAsh::SchedulePaintInRect(const gfx::Rect& r) { 537 // We may end up here before |header_view_| has been added to the Widget. 538 if (header_view_->GetWidget()) { 539 // The HeaderView is not a child of CustomFrameViewAsh. Redirect the paint 540 // to HeaderView instead. 541 gfx::RectF to_paint(r); 542 views::View::ConvertRectToTarget(this, header_view_, &to_paint); 543 header_view_->SchedulePaintInRect(gfx::ToEnclosingRect(to_paint)); 544 } else { 545 views::NonClientFrameView::SchedulePaintInRect(r); 546 } 547 } 548 549 void CustomFrameViewAsh::VisibilityChanged(views::View* starting_from, 550 bool is_visible) { 551 if (is_visible) 552 header_view_->UpdateAvatarIcon(); 553 } 554 555 //////////////////////////////////////////////////////////////////////////////// 556 // CustomFrameViewAsh, views::ViewTargeterDelegate overrides: 557 558 views::View* CustomFrameViewAsh::GetHeaderView() { 559 return header_view_; 560 } 561 562 const views::View* CustomFrameViewAsh::GetAvatarIconViewForTest() const { 563 return header_view_->avatar_icon(); 564 } 565 566 //////////////////////////////////////////////////////////////////////////////// 567 // CustomFrameViewAsh, private: 568 569 // views::NonClientFrameView: 570 bool CustomFrameViewAsh::DoesIntersectRect(const views::View* target, 571 const gfx::Rect& rect) const { 572 CHECK_EQ(target, this); 573 // NonClientView hit tests the NonClientFrameView first instead of going in 574 // z-order. Return false so that events get to the OverlayView. 575 return false; 576 } 577 578 FrameCaptionButtonContainerView* CustomFrameViewAsh:: 579 GetFrameCaptionButtonContainerViewForTest() { 580 return header_view_->caption_button_container(); 581 } 582 583 int CustomFrameViewAsh::NonClientTopBorderHeight() const { 584 return frame_->IsFullscreen() ? 0 : header_view_->GetPreferredHeight(); 585 } 586 587 } // namespace ash 588