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