tab.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/tabs/tab.h" 6 7#include <limits> 8 9#include "base/command_line.h" 10#include "base/debug/alias.h" 11#include "base/strings/utf_string_conversions.h" 12#include "chrome/browser/defaults.h" 13#include "chrome/browser/themes/theme_properties.h" 14#include "chrome/browser/ui/browser.h" 15#include "chrome/browser/ui/tab_contents/core_tab_helper.h" 16#include "chrome/browser/ui/tabs/tab_resources.h" 17#include "chrome/browser/ui/tabs/tab_utils.h" 18#include "chrome/browser/ui/view_ids.h" 19#include "chrome/browser/ui/views/tabs/tab_controller.h" 20#include "chrome/browser/ui/views/theme_image_mapper.h" 21#include "chrome/browser/ui/views/touch_uma/touch_uma.h" 22#include "chrome/common/chrome_switches.h" 23#include "grit/generated_resources.h" 24#include "grit/theme_resources.h" 25#include "grit/ui_resources.h" 26#include "third_party/skia/include/effects/SkGradientShader.h" 27#include "ui/base/accessibility/accessible_view_state.h" 28#include "ui/base/l10n/l10n_util.h" 29#include "ui/base/layout.h" 30#include "ui/base/models/list_selection_model.h" 31#include "ui/base/resource/resource_bundle.h" 32#include "ui/base/theme_provider.h" 33#include "ui/gfx/animation/animation_container.h" 34#include "ui/gfx/animation/multi_animation.h" 35#include "ui/gfx/animation/throb_animation.h" 36#include "ui/gfx/canvas.h" 37#include "ui/gfx/color_analysis.h" 38#include "ui/gfx/favicon_size.h" 39#include "ui/gfx/font.h" 40#include "ui/gfx/image/image_skia_operations.h" 41#include "ui/gfx/path.h" 42#include "ui/gfx/rect_conversions.h" 43#include "ui/gfx/skia_util.h" 44#include "ui/gfx/text_elider.h" 45#include "ui/views/border.h" 46#include "ui/views/controls/button/image_button.h" 47#include "ui/views/rect_based_targeting_utils.h" 48#include "ui/views/widget/tooltip_manager.h" 49#include "ui/views/widget/widget.h" 50#include "ui/views/window/non_client_view.h" 51 52#if defined(OS_WIN) 53#include "win8/util/win8_util.h" 54#endif 55 56#if defined(USE_ASH) 57#include "ui/aura/env.h" 58#endif 59 60namespace { 61 62// Padding around the "content" of a tab, occupied by the tab border graphics. 63 64int left_padding() { 65 static int value = -1; 66 if (value == -1) { 67 switch (ui::GetDisplayLayout()) { 68 case ui::LAYOUT_DESKTOP: 69 value = 22; 70 break; 71 case ui::LAYOUT_TOUCH: 72 value = 30; 73 break; 74 default: 75 NOTREACHED(); 76 } 77 } 78 return value; 79} 80 81int top_padding() { 82 static int value = -1; 83 if (value == -1) { 84 switch (ui::GetDisplayLayout()) { 85 case ui::LAYOUT_DESKTOP: 86 value = 7; 87 break; 88 case ui::LAYOUT_TOUCH: 89 value = 10; 90 break; 91 default: 92 NOTREACHED(); 93 } 94 } 95 return value; 96} 97 98int right_padding() { 99 static int value = -1; 100 if (value == -1) { 101 switch (ui::GetDisplayLayout()) { 102 case ui::LAYOUT_DESKTOP: 103 value = 17; 104 break; 105 case ui::LAYOUT_TOUCH: 106 value = 21; 107 break; 108 default: 109 NOTREACHED(); 110 } 111 } 112 return value; 113} 114 115int bottom_padding() { 116 static int value = -1; 117 if (value == -1) { 118 switch (ui::GetDisplayLayout()) { 119 case ui::LAYOUT_DESKTOP: 120 value = 5; 121 break; 122 case ui::LAYOUT_TOUCH: 123 value = 7; 124 break; 125 default: 126 NOTREACHED(); 127 } 128 } 129 return value; 130} 131 132// Height of the shadow at the top of the tab image assets. 133int drop_shadow_height() { 134 static int value = -1; 135 if (value == -1) { 136 switch (ui::GetDisplayLayout()) { 137 case ui::LAYOUT_DESKTOP: 138 value = 4; 139 break; 140 case ui::LAYOUT_TOUCH: 141 value = 5; 142 break; 143 default: 144 NOTREACHED(); 145 } 146 } 147 return value; 148} 149 150// Size of icon used for throbber and favicon next to tab title. 151int tab_icon_size() { 152 static int value = -1; 153 if (value == -1) { 154 switch (ui::GetDisplayLayout()) { 155 case ui::LAYOUT_DESKTOP: 156 value = gfx::kFaviconSize; 157 break; 158 case ui::LAYOUT_TOUCH: 159 value = 20; 160 break; 161 default: 162 NOTREACHED(); 163 } 164 } 165 return value; 166} 167 168// How long the pulse throb takes. 169const int kPulseDurationMs = 200; 170 171// Width of touch tabs. 172static const int kTouchWidth = 120; 173 174static const int kToolbarOverlap = 1; 175static const int kFaviconTitleSpacing = 4; 176// Additional vertical offset for title text relative to top of tab. 177// Ash text rendering may be different than Windows. 178static const int kTitleTextOffsetYAsh = 1; 179static const int kTitleTextOffsetY = 0; 180static const int kTitleCloseButtonSpacing = 3; 181static const int kStandardTitleWidth = 175; 182// Additional vertical offset for close button relative to top of tab. 183// Ash needs this to match the text vertical position. 184static const int kCloseButtonVertFuzzAsh = 1; 185static const int kCloseButtonVertFuzz = 0; 186// Additional horizontal offset for close button relative to title text. 187static const int kCloseButtonHorzFuzz = 3; 188 189// When a non-mini-tab becomes a mini-tab the width of the tab animates. If 190// the width of a mini-tab is >= kMiniTabRendererAsNormalTabWidth then the tab 191// is rendered as a normal tab. This is done to avoid having the title 192// immediately disappear when transitioning a tab from normal to mini-tab. 193static const int kMiniTabRendererAsNormalTabWidth = 194 browser_defaults::kMiniTabWidth + 30; 195 196// How opaque to make the hover state (out of 1). 197static const double kHoverOpacity = 0.33; 198 199// Opacity for non-active selected tabs. 200static const double kSelectedTabOpacity = .45; 201 202// Selected (but not active) tabs have their throb value scaled down by this. 203static const double kSelectedTabThrobScale = .5; 204 205// Durations for the various parts of the mini tab title animation. 206static const int kMiniTitleChangeAnimationDuration1MS = 1600; 207static const int kMiniTitleChangeAnimationStart1MS = 0; 208static const int kMiniTitleChangeAnimationEnd1MS = 1900; 209static const int kMiniTitleChangeAnimationDuration2MS = 0; 210static const int kMiniTitleChangeAnimationDuration3MS = 550; 211static const int kMiniTitleChangeAnimationStart3MS = 150; 212static const int kMiniTitleChangeAnimationEnd3MS = 800; 213static const int kMiniTitleChangeAnimationIntervalMS = 40; 214 215// Offset from the right edge for the start of the mini title change animation. 216static const int kMiniTitleChangeInitialXOffset = 6; 217 218// Radius of the radial gradient used for mini title change animation. 219static const int kMiniTitleChangeGradientRadius = 20; 220 221// Colors of the gradient used during the mini title change animation. 222static const SkColor kMiniTitleChangeGradientColor1 = SK_ColorWHITE; 223static const SkColor kMiniTitleChangeGradientColor2 = 224 SkColorSetARGB(0, 255, 255, 255); 225 226// Max number of images to cache. This has to be at least two since rounding 227// errors may lead to tabs in the same tabstrip having different sizes. 228const size_t kMaxImageCacheSize = 4; 229 230// Height of the miniature tab strip in immersive mode. 231const int kImmersiveTabHeight = 3; 232 233// Height of the small tab indicator rectangles in immersive mode. 234const int kImmersiveBarHeight = 2; 235 236// Color for active and inactive tabs in the immersive mode light strip. These 237// should be a little brighter than the color of the normal art assets for tabs, 238// which for active tabs is 230, 230, 230 and for inactive is 184, 184, 184. 239const SkColor kImmersiveActiveTabColor = SkColorSetRGB(235, 235, 235); 240const SkColor kImmersiveInactiveTabColor = SkColorSetRGB(190, 190, 190); 241 242// The minimum opacity (out of 1) when a tab (either active or inactive) is 243// throbbing in the immersive mode light strip. 244const double kImmersiveTabMinThrobOpacity = 0.66; 245 246// Number of steps in the immersive mode loading animation. 247const int kImmersiveLoadingStepCount = 32; 248 249const char kTabCloseButtonName[] = "TabCloseButton"; 250 251void DrawIconAtLocation(gfx::Canvas* canvas, 252 const gfx::ImageSkia& image, 253 int image_offset, 254 int dst_x, 255 int dst_y, 256 int icon_width, 257 int icon_height, 258 bool filter, 259 const SkPaint& paint) { 260 // NOTE: the clipping is a work around for 69528, it shouldn't be necessary. 261 canvas->Save(); 262 canvas->ClipRect(gfx::Rect(dst_x, dst_y, icon_width, icon_height)); 263 canvas->DrawImageInt(image, 264 image_offset, 0, icon_width, icon_height, 265 dst_x, dst_y, icon_width, icon_height, 266 filter, paint); 267 canvas->Restore(); 268} 269 270// Draws the icon image at the center of |bounds|. 271void DrawIconCenter(gfx::Canvas* canvas, 272 const gfx::ImageSkia& image, 273 int image_offset, 274 int icon_width, 275 int icon_height, 276 const gfx::Rect& bounds, 277 bool filter, 278 const SkPaint& paint) { 279 // Center the image within bounds. 280 int dst_x = bounds.x() - (icon_width - bounds.width()) / 2; 281 int dst_y = bounds.y() - (icon_height - bounds.height()) / 2; 282 DrawIconAtLocation(canvas, image, image_offset, dst_x, dst_y, icon_width, 283 icon_height, filter, paint); 284} 285 286chrome::HostDesktopType GetHostDesktopType(views::View* view) { 287 // Widget is NULL when tabs are detached. 288 views::Widget* widget = view->GetWidget(); 289 return chrome::GetHostDesktopTypeForNativeView( 290 widget ? widget->GetNativeView() : NULL); 291} 292 293} // namespace 294 295//////////////////////////////////////////////////////////////////////////////// 296// FaviconCrashAnimation 297// 298// A custom animation subclass to manage the favicon crash animation. 299class Tab::FaviconCrashAnimation : public gfx::LinearAnimation, 300 public gfx::AnimationDelegate { 301 public: 302 explicit FaviconCrashAnimation(Tab* target) 303 : gfx::LinearAnimation(1000, 25, this), 304 target_(target) { 305 } 306 virtual ~FaviconCrashAnimation() {} 307 308 // gfx::Animation overrides: 309 virtual void AnimateToState(double state) OVERRIDE { 310 const double kHidingOffset = 27; 311 312 if (state < .5) { 313 target_->SetFaviconHidingOffset( 314 static_cast<int>(floor(kHidingOffset * 2.0 * state))); 315 } else { 316 target_->DisplayCrashedFavicon(); 317 target_->SetFaviconHidingOffset( 318 static_cast<int>( 319 floor(kHidingOffset - ((state - .5) * 2.0 * kHidingOffset)))); 320 } 321 } 322 323 // gfx::AnimationDelegate overrides: 324 virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE { 325 target_->SetFaviconHidingOffset(0); 326 } 327 328 private: 329 Tab* target_; 330 331 DISALLOW_COPY_AND_ASSIGN(FaviconCrashAnimation); 332}; 333 334//////////////////////////////////////////////////////////////////////////////// 335// TabCloseButton 336// 337// This is a Button subclass that causes middle clicks to be forwarded to the 338// parent View by explicitly not handling them in OnMousePressed. 339class Tab::TabCloseButton : public views::ImageButton { 340 public: 341 explicit TabCloseButton(Tab* tab) : views::ImageButton(tab), tab_(tab) {} 342 virtual ~TabCloseButton() {} 343 344 // Overridden from views::View. 345 virtual View* GetEventHandlerForRect(const gfx::Rect& rect) OVERRIDE { 346 if (!views::UsePointBasedTargeting(rect)) 347 return View::GetEventHandlerForRect(rect); 348 349 // Ignore the padding set on the button. 350 gfx::Rect contents_bounds = GetContentsBounds(); 351 contents_bounds.set_x(GetMirroredXForRect(contents_bounds)); 352 353 // TODO(tdanderson): Remove this ifdef if rect-based targeting 354 // is turned on by default. 355#if defined(USE_ASH) 356 // Include the padding in hit-test for touch events. 357 if (aura::Env::GetInstance()->is_touch_down()) 358 contents_bounds = GetLocalBounds(); 359#elif defined(OS_WIN) 360 // TODO(sky): Use local-bounds if a touch-point is active. 361 // http://crbug.com/145258 362#endif 363 364 return contents_bounds.Intersects(rect) ? this : parent(); 365 } 366 367 // Overridden from views::View. 368 virtual View* GetTooltipHandlerForPoint(const gfx::Point& point) OVERRIDE { 369 // Tab close button has no children, so tooltip handler should be the same 370 // as the event handler. 371 // In addition, a hit test has to be performed for the point (as 372 // GetTooltipHandlerForPoint() is responsible for it). 373 if (!HitTestPoint(point)) 374 return NULL; 375 return GetEventHandlerForPoint(point); 376 } 377 378 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE { 379 if (tab_->controller()) 380 tab_->controller()->OnMouseEventInTab(this, event); 381 382 bool handled = ImageButton::OnMousePressed(event); 383 // Explicitly mark midle-mouse clicks as non-handled to ensure the tab 384 // sees them. 385 return event.IsOnlyMiddleMouseButton() ? false : handled; 386 } 387 388 virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE { 389 if (tab_->controller()) 390 tab_->controller()->OnMouseEventInTab(this, event); 391 CustomButton::OnMouseMoved(event); 392 } 393 394 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE { 395 if (tab_->controller()) 396 tab_->controller()->OnMouseEventInTab(this, event); 397 CustomButton::OnMouseReleased(event); 398 } 399 400 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { 401 // Consume all gesture events here so that the parent (Tab) does not 402 // start consuming gestures. 403 ImageButton::OnGestureEvent(event); 404 event->SetHandled(); 405 } 406 407 virtual bool HasHitTestMask() const OVERRIDE { 408 return true; 409 } 410 411 virtual void GetHitTestMask(HitTestSource source, 412 gfx::Path* path) const OVERRIDE { 413 // Use the button's contents bounds (which does not include padding) 414 // and the hit test mask of our parent |tab_| to determine if the 415 // button is hidden behind another tab. 416 gfx::Path tab_mask; 417 tab_->GetHitTestMask(source, &tab_mask); 418 419 gfx::Rect button_bounds(GetContentsBounds()); 420 button_bounds.set_x(GetMirroredXForRect(button_bounds)); 421 gfx::RectF tab_bounds_f(gfx::SkRectToRectF(tab_mask.getBounds())); 422 views::View::ConvertRectToTarget(tab_, this, &tab_bounds_f); 423 gfx::Rect tab_bounds = gfx::ToEnclosingRect(tab_bounds_f); 424 425 // If the button is hidden behind another tab, the hit test mask is empty. 426 // Otherwise set the hit test mask to be the contents bounds. 427 path->reset(); 428 if (tab_bounds.Contains(button_bounds)) { 429 // Include the padding in the hit test mask for touch events. 430 if (source == HIT_TEST_SOURCE_TOUCH) 431 button_bounds = GetLocalBounds(); 432 433 path->addRect(RectToSkRect(button_bounds)); 434 } 435 } 436 437 virtual const char* GetClassName() const OVERRIDE { 438 return kTabCloseButtonName; 439 } 440 441 private: 442 Tab* tab_; 443 444 DISALLOW_COPY_AND_ASSIGN(TabCloseButton); 445}; 446 447//////////////////////////////////////////////////////////////////////////////// 448// ImageCacheEntry 449 450Tab::ImageCacheEntry::ImageCacheEntry() 451 : resource_id(-1), 452 scale_factor(ui::SCALE_FACTOR_NONE) { 453} 454 455Tab::ImageCacheEntry::~ImageCacheEntry() {} 456 457//////////////////////////////////////////////////////////////////////////////// 458// Tab, statics: 459 460// static 461const char Tab::kViewClassName[] = "Tab"; 462 463// static 464Tab::TabImage Tab::tab_alpha_ = {0}; 465Tab::TabImage Tab::tab_active_ = {0}; 466Tab::TabImage Tab::tab_inactive_ = {0}; 467// static 468gfx::Font* Tab::font_ = NULL; 469// static 470int Tab::font_height_ = 0; 471// static 472Tab::ImageCache* Tab::image_cache_ = NULL; 473 474//////////////////////////////////////////////////////////////////////////////// 475// Tab, public: 476 477Tab::Tab(TabController* controller) 478 : controller_(controller), 479 closing_(false), 480 dragging_(false), 481 favicon_hiding_offset_(0), 482 loading_animation_frame_(0), 483 immersive_loading_step_(0), 484 should_display_crashed_favicon_(false), 485 animating_media_state_(TAB_MEDIA_STATE_NONE), 486 theme_provider_(NULL), 487 tab_activated_with_last_gesture_begin_(false), 488 hover_controller_(this), 489 showing_icon_(false), 490 showing_media_indicator_(false), 491 showing_close_button_(false), 492 close_button_color_(0) { 493 InitTabResources(); 494 495 // So we get don't get enter/exit on children and don't prematurely stop the 496 // hover. 497 set_notify_enter_exit_on_child(true); 498 499 set_id(VIEW_ID_TAB); 500 501 // Add the Close Button. 502 close_button_ = new TabCloseButton(this); 503 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 504 close_button_->SetImage(views::CustomButton::STATE_NORMAL, 505 rb.GetImageSkiaNamed(IDR_CLOSE_1)); 506 close_button_->SetImage(views::CustomButton::STATE_HOVERED, 507 rb.GetImageSkiaNamed(IDR_CLOSE_1_H)); 508 close_button_->SetImage(views::CustomButton::STATE_PRESSED, 509 rb.GetImageSkiaNamed(IDR_CLOSE_1_P)); 510 close_button_->SetAccessibleName( 511 l10n_util::GetStringUTF16(IDS_ACCNAME_CLOSE)); 512 // Disable animation so that the red danger sign shows up immediately 513 // to help avoid mis-clicks. 514 close_button_->SetAnimationDuration(0); 515 AddChildView(close_button_); 516 517 set_context_menu_controller(this); 518} 519 520Tab::~Tab() { 521} 522 523void Tab::set_animation_container(gfx::AnimationContainer* container) { 524 animation_container_ = container; 525 hover_controller_.SetAnimationContainer(container); 526} 527 528bool Tab::IsActive() const { 529 return controller() ? controller()->IsActiveTab(this) : true; 530} 531 532bool Tab::IsSelected() const { 533 return controller() ? controller()->IsTabSelected(this) : true; 534} 535 536void Tab::SetData(const TabRendererData& data) { 537 if (data_.Equals(data)) 538 return; 539 540 TabRendererData old(data_); 541 data_ = data; 542 543 if (data_.IsCrashed()) { 544 if (!should_display_crashed_favicon_ && !IsPerformingCrashAnimation()) { 545 data_.media_state = TAB_MEDIA_STATE_NONE; 546#if defined(OS_CHROMEOS) 547 // On Chrome OS, we reload killed tabs automatically when the user 548 // switches to them. Don't display animations for these unless they're 549 // selected (i.e. in the foreground) -- we won't reload these 550 // automatically since we don't want to get into a crash loop. 551 if (IsSelected() || 552 data_.crashed_status != base::TERMINATION_STATUS_PROCESS_WAS_KILLED) 553 StartCrashAnimation(); 554#else 555 StartCrashAnimation(); 556#endif 557 } 558 } else { 559 if (IsPerformingCrashAnimation()) 560 StopCrashAnimation(); 561 ResetCrashedFavicon(); 562 } 563 564 if (data_.media_state != old.media_state) { 565 if (data_.media_state != TAB_MEDIA_STATE_NONE) 566 animating_media_state_ = data_.media_state; 567 StartMediaIndicatorAnimation(); 568 } 569 570 if (old.mini != data_.mini) { 571 if (tab_animation_.get() && tab_animation_->is_animating()) { 572 tab_animation_->Stop(); 573 tab_animation_.reset(NULL); 574 } 575 } 576 577 DataChanged(old); 578 579 Layout(); 580 SchedulePaint(); 581} 582 583void Tab::UpdateLoadingAnimation(TabRendererData::NetworkState state) { 584 if (state == data_.network_state && 585 state == TabRendererData::NETWORK_STATE_NONE) { 586 // If the network state is none and hasn't changed, do nothing. Otherwise we 587 // need to advance the animation frame. 588 return; 589 } 590 591 TabRendererData::NetworkState old_state = data_.network_state; 592 data_.network_state = state; 593 AdvanceLoadingAnimation(old_state, state); 594} 595 596void Tab::StartPulse() { 597 gfx::ThrobAnimation* animation = new gfx::ThrobAnimation(this); 598 animation->SetSlideDuration(kPulseDurationMs); 599 if (animation_container_.get()) 600 animation->SetContainer(animation_container_.get()); 601 animation->StartThrobbing(std::numeric_limits<int>::max()); 602 tab_animation_.reset(animation); 603} 604 605void Tab::StopPulse() { 606 if (!tab_animation_.get()) 607 return; 608 tab_animation_->Stop(); 609 tab_animation_.reset(NULL); 610} 611 612void Tab::StartMiniTabTitleAnimation() { 613 // We can only do this animation if the tab is mini because we will 614 // upcast tab_animation back to MultiAnimation when we draw. 615 if (!data().mini) 616 return; 617 if (!tab_animation_.get()) { 618 gfx::MultiAnimation::Parts parts; 619 parts.push_back( 620 gfx::MultiAnimation::Part(kMiniTitleChangeAnimationDuration1MS, 621 gfx::Tween::EASE_OUT)); 622 parts.push_back( 623 gfx::MultiAnimation::Part(kMiniTitleChangeAnimationDuration2MS, 624 gfx::Tween::ZERO)); 625 parts.push_back( 626 gfx::MultiAnimation::Part(kMiniTitleChangeAnimationDuration3MS, 627 gfx::Tween::EASE_IN)); 628 parts[0].start_time_ms = kMiniTitleChangeAnimationStart1MS; 629 parts[0].end_time_ms = kMiniTitleChangeAnimationEnd1MS; 630 parts[2].start_time_ms = kMiniTitleChangeAnimationStart3MS; 631 parts[2].end_time_ms = kMiniTitleChangeAnimationEnd3MS; 632 base::TimeDelta timeout = 633 base::TimeDelta::FromMilliseconds(kMiniTitleChangeAnimationIntervalMS); 634 gfx::MultiAnimation* animation = new gfx::MultiAnimation(parts, timeout); 635 if (animation_container_.get()) 636 animation->SetContainer(animation_container_.get()); 637 animation->set_delegate(this); 638 tab_animation_.reset(animation); 639 } 640 tab_animation_->Start(); 641} 642 643void Tab::StopMiniTabTitleAnimation() { 644 if (!tab_animation_.get()) 645 return; 646 tab_animation_->Stop(); 647 tab_animation_.reset(NULL); 648} 649 650// static 651gfx::Size Tab::GetBasicMinimumUnselectedSize() { 652 InitTabResources(); 653 654 gfx::Size minimum_size; 655 minimum_size.set_width(left_padding() + right_padding()); 656 // Since we use image images, the real minimum height of the image is 657 // defined most accurately by the height of the end cap images. 658 minimum_size.set_height(tab_active_.image_l->height()); 659 return minimum_size; 660} 661 662gfx::Size Tab::GetMinimumUnselectedSize() { 663 return GetBasicMinimumUnselectedSize(); 664} 665 666// static 667gfx::Size Tab::GetMinimumSelectedSize() { 668 gfx::Size minimum_size = GetBasicMinimumUnselectedSize(); 669 minimum_size.set_width( 670 left_padding() + gfx::kFaviconSize + right_padding()); 671 return minimum_size; 672} 673 674// static 675gfx::Size Tab::GetStandardSize() { 676 gfx::Size standard_size = GetBasicMinimumUnselectedSize(); 677 standard_size.set_width( 678 standard_size.width() + kFaviconTitleSpacing + kStandardTitleWidth); 679 return standard_size; 680} 681 682// static 683int Tab::GetTouchWidth() { 684 return kTouchWidth; 685} 686 687// static 688int Tab::GetMiniWidth() { 689 return browser_defaults::kMiniTabWidth; 690} 691 692// static 693int Tab::GetImmersiveHeight() { 694 return kImmersiveTabHeight; 695} 696 697//////////////////////////////////////////////////////////////////////////////// 698// Tab, AnimationDelegate overrides: 699 700void Tab::AnimationProgressed(const gfx::Animation* animation) { 701 // Ignore if the pulse animation is being performed on active tab because 702 // it repaints the same image. See |Tab::PaintTabBackground()|. 703 if (animation == tab_animation_.get() && IsActive()) 704 return; 705 SchedulePaint(); 706} 707 708void Tab::AnimationCanceled(const gfx::Animation* animation) { 709 if (media_indicator_animation_ == animation) 710 animating_media_state_ = data_.media_state; 711 SchedulePaint(); 712} 713 714void Tab::AnimationEnded(const gfx::Animation* animation) { 715 if (media_indicator_animation_ == animation) 716 animating_media_state_ = data_.media_state; 717 SchedulePaint(); 718} 719 720//////////////////////////////////////////////////////////////////////////////// 721// Tab, views::ButtonListener overrides: 722 723void Tab::ButtonPressed(views::Button* sender, const ui::Event& event) { 724 const CloseTabSource source = 725 (event.type() == ui::ET_MOUSE_RELEASED && 726 (event.flags() & ui::EF_FROM_TOUCH) == 0) ? CLOSE_TAB_FROM_MOUSE : 727 CLOSE_TAB_FROM_TOUCH; 728 DCHECK_EQ(close_button_, sender); 729 controller()->CloseTab(this, source); 730 if (event.type() == ui::ET_GESTURE_TAP) 731 TouchUMA::RecordGestureAction(TouchUMA::GESTURE_TABCLOSE_TAP); 732} 733 734//////////////////////////////////////////////////////////////////////////////// 735// Tab, views::ContextMenuController overrides: 736 737void Tab::ShowContextMenuForView(views::View* source, 738 const gfx::Point& point, 739 ui::MenuSourceType source_type) { 740 if (controller() && !closing()) 741 controller()->ShowContextMenuForTab(this, point, source_type); 742} 743 744//////////////////////////////////////////////////////////////////////////////// 745// Tab, views::View overrides: 746 747void Tab::OnPaint(gfx::Canvas* canvas) { 748 // Don't paint if we're narrower than we can render correctly. (This should 749 // only happen during animations). 750 if (width() < GetMinimumUnselectedSize().width() && !data().mini) 751 return; 752 753 gfx::Rect clip; 754 if (controller()) { 755 if (!controller()->ShouldPaintTab(this, &clip)) 756 return; 757 if (!clip.IsEmpty()) { 758 canvas->Save(); 759 canvas->ClipRect(clip); 760 } 761 } 762 763 if (controller() && controller()->IsImmersiveStyle()) 764 PaintImmersiveTab(canvas); 765 else 766 PaintTab(canvas); 767 768 if (!clip.IsEmpty()) 769 canvas->Restore(); 770} 771 772void Tab::Layout() { 773 gfx::Rect lb = GetContentsBounds(); 774 if (lb.IsEmpty()) 775 return; 776 lb.Inset( 777 left_padding(), top_padding(), right_padding(), bottom_padding()); 778 779 // The height of the content of the Tab is the largest of the favicon, 780 // the title text and the close button graphic. 781 int content_height = std::max(tab_icon_size(), font_height_); 782 close_button_->set_border(NULL); 783 gfx::Size close_button_size(close_button_->GetPreferredSize()); 784 content_height = std::max(content_height, close_button_size.height()); 785 786 // Size the Favicon. 787 showing_icon_ = ShouldShowIcon(); 788 if (showing_icon_) { 789 // Use the size of the favicon as apps use a bigger favicon size. 790 int favicon_top = top_padding() + content_height / 2 - tab_icon_size() / 2; 791 int favicon_left = lb.x(); 792 favicon_bounds_.SetRect(favicon_left, favicon_top, 793 tab_icon_size(), tab_icon_size()); 794 MaybeAdjustLeftForMiniTab(&favicon_bounds_); 795 } else { 796 favicon_bounds_.SetRect(lb.x(), lb.y(), 0, 0); 797 } 798 799 // Size the Close button. 800 showing_close_button_ = ShouldShowCloseBox(); 801 const bool is_host_desktop_type_ash = 802 GetHostDesktopType(this) == chrome::HOST_DESKTOP_TYPE_ASH; 803 if (showing_close_button_) { 804 const int close_button_vert_fuzz = is_host_desktop_type_ash ? 805 kCloseButtonVertFuzzAsh : kCloseButtonVertFuzz; 806 int close_button_top = top_padding() + close_button_vert_fuzz + 807 (content_height - close_button_size.height()) / 2; 808 // If the ratio of the close button size to tab width exceeds the maximum. 809 // The close button should be as large as possible so that there is a larger 810 // hit-target for touch events. So the close button bounds extends to the 811 // edges of the tab. However, the larger hit-target should be active only 812 // for mouse events, and the close-image should show up in the right place. 813 // So a border is added to the button with necessary padding. The close 814 // button (BaseTab::TabCloseButton) makes sure the padding is a hit-target 815 // only for touch events. 816 int top_border = close_button_top; 817 int bottom_border = height() - (close_button_size.height() + top_border); 818 int left_border = kCloseButtonHorzFuzz; 819 int right_border = width() - (lb.width() + close_button_size.width() + 820 left_border); 821 close_button_->set_border(views::Border::CreateEmptyBorder(top_border, 822 left_border, bottom_border, right_border)); 823 close_button_->SetPosition(gfx::Point(lb.width(), 0)); 824 close_button_->SizeToPreferredSize(); 825 close_button_->SetVisible(true); 826 } else { 827 close_button_->SetBounds(0, 0, 0, 0); 828 close_button_->SetVisible(false); 829 } 830 831 showing_media_indicator_ = ShouldShowMediaIndicator(); 832 if (showing_media_indicator_) { 833 const gfx::Image& media_indicator_image = 834 chrome::GetTabMediaIndicatorImage(animating_media_state_); 835 media_indicator_bounds_.set_width(media_indicator_image.Width()); 836 media_indicator_bounds_.set_height(media_indicator_image.Height()); 837 media_indicator_bounds_.set_y( 838 top_padding() + 839 (content_height - media_indicator_bounds_.height()) / 2); 840 const int right = showing_close_button_ ? 841 close_button_->x() + close_button_->GetInsets().left() : lb.right(); 842 media_indicator_bounds_.set_x( 843 std::max(lb.x(), right - media_indicator_bounds_.width())); 844 MaybeAdjustLeftForMiniTab(&media_indicator_bounds_); 845 } else { 846 media_indicator_bounds_.SetRect(lb.x(), lb.y(), 0, 0); 847 } 848 849 const int title_text_offset = is_host_desktop_type_ash ? 850 kTitleTextOffsetYAsh : kTitleTextOffsetY; 851 int title_left = favicon_bounds_.right() + kFaviconTitleSpacing; 852 int title_top = top_padding() + title_text_offset + 853 (content_height - font_height_) / 2; 854 // Size the Title text to fill the remaining space. 855 if (!data().mini || width() >= kMiniTabRendererAsNormalTabWidth) { 856 // If the user has big fonts, the title will appear rendered too far down 857 // on the y-axis if we use the regular top padding, so we need to adjust it 858 // so that the text appears centered. 859 gfx::Size minimum_size = GetMinimumUnselectedSize(); 860 int text_height = title_top + font_height_ + bottom_padding(); 861 if (text_height > minimum_size.height()) 862 title_top -= (text_height - minimum_size.height()) / 2; 863 864 int title_width; 865 if (showing_media_indicator_) { 866 title_width = media_indicator_bounds_.x() - kTitleCloseButtonSpacing - 867 title_left; 868 } else if (close_button_->visible()) { 869 // The close button has an empty border with some padding (see details 870 // above where the close-button's bounds is set). Allow the title to 871 // overlap the empty padding. 872 title_width = close_button_->x() + close_button_->GetInsets().left() - 873 kTitleCloseButtonSpacing - title_left; 874 } else { 875 title_width = lb.width() - title_left; 876 } 877 title_width = std::max(title_width, 0); 878 title_bounds_.SetRect(title_left, title_top, title_width, font_height_); 879 } else { 880 title_bounds_.SetRect(title_left, title_top, 0, 0); 881 } 882 883 // Certain UI elements within the Tab (the favicon, etc.) are not represented 884 // as child Views (which is the preferred method). Instead, these UI elements 885 // are drawn directly on the canvas from within Tab::OnPaint(). The Tab's 886 // child Views (for example, the Tab's close button which is a views::Button 887 // instance) are automatically mirrored by the mirroring infrastructure in 888 // views. The elements Tab draws directly on the canvas need to be manually 889 // mirrored if the View's layout is right-to-left. 890 title_bounds_.set_x(GetMirroredXForRect(title_bounds_)); 891} 892 893void Tab::OnThemeChanged() { 894 LoadTabImages(); 895} 896 897const char* Tab::GetClassName() const { 898 return kViewClassName; 899} 900 901bool Tab::HasHitTestMask() const { 902 return true; 903} 904 905void Tab::GetHitTestMask(HitTestSource source, gfx::Path* path) const { 906 // When the window is maximized we don't want to shave off the edges or top 907 // shadow of the tab, such that the user can click anywhere along the top 908 // edge of the screen to select a tab. Ditto for immersive fullscreen. 909 const views::Widget* widget = GetWidget(); 910 bool include_top_shadow = 911 widget && (widget->IsMaximized() || widget->IsFullscreen()); 912 TabResources::GetHitTestMask(width(), height(), include_top_shadow, path); 913 914 // It is possible for a portion of the tab to be occluded if tabs are 915 // stacked, so modify the hit test mask to only include the visible 916 // region of the tab. 917 if (controller()) { 918 gfx::Rect clip; 919 controller()->ShouldPaintTab(this, &clip); 920 if (clip.size().GetArea()) { 921 SkRect intersection(path->getBounds()); 922 intersection.intersect(RectToSkRect(clip)); 923 path->reset(); 924 path->addRect(intersection); 925 } 926 } 927} 928 929bool Tab::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const { 930 // TODO(miu): Rectify inconsistent tooltip behavior. http://crbug.com/310947 931 932 if (data_.media_state != TAB_MEDIA_STATE_NONE) { 933 *tooltip = chrome::AssembleTabTooltipText(data_.title, data_.media_state); 934 return true; 935 } 936 937 if (data_.title.empty()) 938 return false; 939 940 // Only show the tooltip if the title is truncated. 941 if (font_->GetStringWidth(data_.title) > GetTitleBounds().width()) { 942 *tooltip = data_.title; 943 return true; 944 } 945 return false; 946} 947 948bool Tab::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* origin) const { 949 origin->set_x(title_bounds_.x() + 10); 950 origin->set_y(-views::TooltipManager::GetTooltipHeight() - 4); 951 return true; 952} 953 954ui::ThemeProvider* Tab::GetThemeProvider() const { 955 ui::ThemeProvider* tp = View::GetThemeProvider(); 956 return tp ? tp : theme_provider_; 957} 958 959bool Tab::OnMousePressed(const ui::MouseEvent& event) { 960 if (!controller()) 961 return false; 962 963 controller()->OnMouseEventInTab(this, event); 964 965 // Allow a right click from touch to drag, which corresponds to a long click. 966 if (event.IsOnlyLeftMouseButton() || 967 (event.IsOnlyRightMouseButton() && event.flags() & ui::EF_FROM_TOUCH)) { 968 ui::ListSelectionModel original_selection; 969 original_selection.Copy(controller()->GetSelectionModel()); 970 // Changing the selection may cause our bounds to change. If that happens 971 // the location of the event may no longer be valid. Create a copy of the 972 // event in the parents coordinate, which won't change, and recreate an 973 // event after changing so the coordinates are correct. 974 ui::MouseEvent event_in_parent(event, static_cast<View*>(this), parent()); 975 if (controller()->SupportsMultipleSelection()) { 976 if (event.IsShiftDown() && event.IsControlDown()) { 977 controller()->AddSelectionFromAnchorTo(this); 978 } else if (event.IsShiftDown()) { 979 controller()->ExtendSelectionTo(this); 980 } else if (event.IsControlDown()) { 981 controller()->ToggleSelected(this); 982 if (!IsSelected()) { 983 // Don't allow dragging non-selected tabs. 984 return false; 985 } 986 } else if (!IsSelected()) { 987 controller()->SelectTab(this); 988 } 989 } else if (!IsSelected()) { 990 controller()->SelectTab(this); 991 } 992 ui::MouseEvent cloned_event(event_in_parent, parent(), 993 static_cast<View*>(this)); 994 controller()->MaybeStartDrag(this, cloned_event, original_selection); 995 } 996 return true; 997} 998 999bool Tab::OnMouseDragged(const ui::MouseEvent& event) { 1000 if (controller()) 1001 controller()->ContinueDrag(this, event); 1002 return true; 1003} 1004 1005void Tab::OnMouseReleased(const ui::MouseEvent& event) { 1006 if (!controller()) 1007 return; 1008 1009 controller()->OnMouseEventInTab(this, event); 1010 1011 // Notify the drag helper that we're done with any potential drag operations. 1012 // Clean up the drag helper, which is re-created on the next mouse press. 1013 // In some cases, ending the drag will schedule the tab for destruction; if 1014 // so, bail immediately, since our members are already dead and we shouldn't 1015 // do anything else except drop the tab where it is. 1016 if (controller()->EndDrag(END_DRAG_COMPLETE)) 1017 return; 1018 1019 // Close tab on middle click, but only if the button is released over the tab 1020 // (normal windows behavior is to discard presses of a UI element where the 1021 // releases happen off the element). 1022 if (event.IsMiddleMouseButton()) { 1023 if (HitTestPoint(event.location())) { 1024 controller()->CloseTab(this, CLOSE_TAB_FROM_MOUSE); 1025 } else if (closing_) { 1026 // We're animating closed and a middle mouse button was pushed on us but 1027 // we don't contain the mouse anymore. We assume the user is clicking 1028 // quicker than the animation and we should close the tab that falls under 1029 // the mouse. 1030 Tab* closest_tab = controller()->GetTabAt(this, event.location()); 1031 if (closest_tab) 1032 controller()->CloseTab(closest_tab, CLOSE_TAB_FROM_MOUSE); 1033 } 1034 } else if (event.IsOnlyLeftMouseButton() && !event.IsShiftDown() && 1035 !event.IsControlDown()) { 1036 // If the tab was already selected mouse pressed doesn't change the 1037 // selection. Reset it now to handle the case where multiple tabs were 1038 // selected. 1039 controller()->SelectTab(this); 1040 } 1041} 1042 1043void Tab::OnMouseCaptureLost() { 1044 if (controller()) 1045 controller()->EndDrag(END_DRAG_CAPTURE_LOST); 1046} 1047 1048void Tab::OnMouseEntered(const ui::MouseEvent& event) { 1049 hover_controller_.Show(views::GlowHoverController::SUBTLE); 1050} 1051 1052void Tab::OnMouseMoved(const ui::MouseEvent& event) { 1053 hover_controller_.SetLocation(event.location()); 1054 if (controller()) 1055 controller()->OnMouseEventInTab(this, event); 1056} 1057 1058void Tab::OnMouseExited(const ui::MouseEvent& event) { 1059 hover_controller_.Hide(); 1060} 1061 1062void Tab::OnGestureEvent(ui::GestureEvent* event) { 1063 if (!controller()) { 1064 event->SetHandled(); 1065 return; 1066 } 1067 1068 switch (event->type()) { 1069 case ui::ET_GESTURE_BEGIN: { 1070 if (event->details().touch_points() != 1) 1071 return; 1072 1073 // See comment in OnMousePressed() as to why we copy the event. 1074 ui::GestureEvent event_in_parent(*event, static_cast<View*>(this), 1075 parent()); 1076 ui::ListSelectionModel original_selection; 1077 original_selection.Copy(controller()->GetSelectionModel()); 1078 tab_activated_with_last_gesture_begin_ = !IsActive(); 1079 if (!IsSelected()) 1080 controller()->SelectTab(this); 1081 gfx::Point loc(event->location()); 1082 views::View::ConvertPointToScreen(this, &loc); 1083 ui::GestureEvent cloned_event(event_in_parent, parent(), 1084 static_cast<View*>(this)); 1085 controller()->MaybeStartDrag(this, cloned_event, original_selection); 1086 break; 1087 } 1088 1089 case ui::ET_GESTURE_END: 1090 controller()->EndDrag(END_DRAG_COMPLETE); 1091 break; 1092 1093 case ui::ET_GESTURE_SCROLL_UPDATE: 1094 controller()->ContinueDrag(this, *event); 1095 break; 1096 1097 default: 1098 break; 1099 } 1100 event->SetHandled(); 1101} 1102 1103void Tab::GetAccessibleState(ui::AccessibleViewState* state) { 1104 state->role = ui::AccessibilityTypes::ROLE_PAGETAB; 1105 state->name = data_.title; 1106} 1107 1108//////////////////////////////////////////////////////////////////////////////// 1109// Tab, private 1110 1111const gfx::Rect& Tab::GetTitleBounds() const { 1112 return title_bounds_; 1113} 1114 1115const gfx::Rect& Tab::GetIconBounds() const { 1116 return favicon_bounds_; 1117} 1118 1119void Tab::MaybeAdjustLeftForMiniTab(gfx::Rect* bounds) const { 1120 if (!data().mini || width() >= kMiniTabRendererAsNormalTabWidth) 1121 return; 1122 const int mini_delta = kMiniTabRendererAsNormalTabWidth - GetMiniWidth(); 1123 const int ideal_delta = width() - GetMiniWidth(); 1124 const int ideal_x = (GetMiniWidth() - bounds->width()) / 2; 1125 bounds->set_x(bounds->x() + static_cast<int>( 1126 (1 - static_cast<float>(ideal_delta) / static_cast<float>(mini_delta)) * 1127 (ideal_x - bounds->x()))); 1128} 1129 1130void Tab::DataChanged(const TabRendererData& old) { 1131 if (data().blocked == old.blocked) 1132 return; 1133 1134 if (data().blocked) 1135 StartPulse(); 1136 else 1137 StopPulse(); 1138} 1139 1140void Tab::PaintTab(gfx::Canvas* canvas) { 1141 // See if the model changes whether the icons should be painted. 1142 const bool show_icon = ShouldShowIcon(); 1143 const bool show_media_indicator = ShouldShowMediaIndicator(); 1144 const bool show_close_button = ShouldShowCloseBox(); 1145 if (show_icon != showing_icon_ || 1146 show_media_indicator != showing_media_indicator_ || 1147 show_close_button != showing_close_button_) { 1148 Layout(); 1149 } 1150 1151 PaintTabBackground(canvas); 1152 1153 SkColor title_color = GetThemeProvider()-> 1154 GetColor(IsSelected() ? 1155 ThemeProperties::COLOR_TAB_TEXT : 1156 ThemeProperties::COLOR_BACKGROUND_TAB_TEXT); 1157 1158 if (!data().mini || width() > kMiniTabRendererAsNormalTabWidth) 1159 PaintTitle(canvas, title_color); 1160 1161 if (show_icon) 1162 PaintIcon(canvas); 1163 1164 if (show_media_indicator) 1165 PaintMediaIndicator(canvas); 1166 1167 // If the close button color has changed, generate a new one. 1168 if (!close_button_color_ || title_color != close_button_color_) { 1169 close_button_color_ = title_color; 1170 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1171 close_button_->SetBackground(close_button_color_, 1172 rb.GetImageSkiaNamed(IDR_CLOSE_1), 1173 rb.GetImageSkiaNamed(IDR_CLOSE_1_MASK)); 1174 } 1175} 1176 1177void Tab::PaintImmersiveTab(gfx::Canvas* canvas) { 1178 // Use transparency for the draw-attention animation. 1179 int alpha = 255; 1180 if (tab_animation_ && 1181 tab_animation_->is_animating() && 1182 !data().mini) { 1183 alpha = tab_animation_->CurrentValueBetween( 1184 255, static_cast<int>(255 * kImmersiveTabMinThrobOpacity)); 1185 } 1186 1187 // Draw a gray rectangle to represent the tab. This works for mini-tabs as 1188 // well as regular ones. The active tab has a brigher bar. 1189 SkColor color = 1190 IsActive() ? kImmersiveActiveTabColor : kImmersiveInactiveTabColor; 1191 gfx::Rect bar_rect = GetImmersiveBarRect(); 1192 canvas->FillRect(bar_rect, SkColorSetA(color, alpha)); 1193 1194 // Paint network activity indicator. 1195 // TODO(jamescook): Replace this placeholder animation with a real one. 1196 // For now, let's go with a Cylon eye effect, but in blue. 1197 if (data().network_state != TabRendererData::NETWORK_STATE_NONE) { 1198 const SkColor kEyeColor = SkColorSetARGB(alpha, 71, 138, 217); 1199 int eye_width = bar_rect.width() / 3; 1200 int eye_offset = bar_rect.width() * immersive_loading_step_ / 1201 kImmersiveLoadingStepCount; 1202 if (eye_offset + eye_width < bar_rect.width()) { 1203 // Draw a single indicator strip because it fits inside |bar_rect|. 1204 gfx::Rect eye_rect( 1205 bar_rect.x() + eye_offset, 0, eye_width, kImmersiveBarHeight); 1206 canvas->FillRect(eye_rect, kEyeColor); 1207 } else { 1208 // Draw two indicators to simulate the eye "wrapping around" to the left 1209 // side. The first part fills the remainder of the bar. 1210 int right_eye_width = bar_rect.width() - eye_offset; 1211 gfx::Rect right_eye_rect( 1212 bar_rect.x() + eye_offset, 0, right_eye_width, kImmersiveBarHeight); 1213 canvas->FillRect(right_eye_rect, kEyeColor); 1214 // The second part parts the remaining |eye_width| on the left. 1215 int left_eye_width = eye_offset + eye_width - bar_rect.width(); 1216 gfx::Rect left_eye_rect( 1217 bar_rect.x(), 0, left_eye_width, kImmersiveBarHeight); 1218 canvas->FillRect(left_eye_rect, kEyeColor); 1219 } 1220 } 1221} 1222 1223void Tab::PaintTabBackground(gfx::Canvas* canvas) { 1224 if (IsActive()) { 1225 PaintActiveTabBackground(canvas); 1226 } else { 1227 if (tab_animation_.get() && 1228 tab_animation_->is_animating() && 1229 data().mini) { 1230 gfx::MultiAnimation* animation = 1231 static_cast<gfx::MultiAnimation*>(tab_animation_.get()); 1232 PaintInactiveTabBackgroundWithTitleChange(canvas, animation); 1233 } else { 1234 PaintInactiveTabBackground(canvas); 1235 } 1236 1237 double throb_value = GetThrobValue(); 1238 if (throb_value > 0) { 1239 canvas->SaveLayerAlpha(static_cast<int>(throb_value * 0xff), 1240 GetLocalBounds()); 1241 PaintActiveTabBackground(canvas); 1242 canvas->Restore(); 1243 } 1244 } 1245} 1246 1247void Tab::PaintInactiveTabBackgroundWithTitleChange( 1248 gfx::Canvas* canvas, 1249 gfx::MultiAnimation* animation) { 1250 // Render the inactive tab background. We'll use this for clipping. 1251 gfx::Canvas background_canvas(size(), canvas->image_scale(), false); 1252 PaintInactiveTabBackground(&background_canvas); 1253 1254 gfx::ImageSkia background_image(background_canvas.ExtractImageRep()); 1255 1256 // Draw a radial gradient to hover_canvas. 1257 gfx::Canvas hover_canvas(size(), canvas->image_scale(), false); 1258 int radius = kMiniTitleChangeGradientRadius; 1259 int x0 = width() + radius - kMiniTitleChangeInitialXOffset; 1260 int x1 = radius; 1261 int x2 = -radius; 1262 int x; 1263 if (animation->current_part_index() == 0) { 1264 x = animation->CurrentValueBetween(x0, x1); 1265 } else if (animation->current_part_index() == 1) { 1266 x = x1; 1267 } else { 1268 x = animation->CurrentValueBetween(x1, x2); 1269 } 1270 SkPoint center_point; 1271 center_point.iset(x, 0); 1272 SkColor colors[2] = { kMiniTitleChangeGradientColor1, 1273 kMiniTitleChangeGradientColor2 }; 1274 skia::RefPtr<SkShader> shader = skia::AdoptRef( 1275 SkGradientShader::CreateRadial( 1276 center_point, SkIntToScalar(radius), colors, NULL, 2, 1277 SkShader::kClamp_TileMode)); 1278 SkPaint paint; 1279 paint.setShader(shader.get()); 1280 hover_canvas.DrawRect(gfx::Rect(x - radius, -radius, radius * 2, radius * 2), 1281 paint); 1282 1283 // Draw the radial gradient clipped to the background into hover_image. 1284 gfx::ImageSkia hover_image = gfx::ImageSkiaOperations::CreateMaskedImage( 1285 gfx::ImageSkia(hover_canvas.ExtractImageRep()), background_image); 1286 1287 // Draw the tab background to the canvas. 1288 canvas->DrawImageInt(background_image, 0, 0); 1289 1290 // And then the gradient on top of that. 1291 if (animation->current_part_index() == 2) { 1292 uint8 alpha = animation->CurrentValueBetween(255, 0); 1293 canvas->DrawImageInt(hover_image, 0, 0, alpha); 1294 } else { 1295 canvas->DrawImageInt(hover_image, 0, 0); 1296 } 1297} 1298 1299void Tab::PaintInactiveTabBackground(gfx::Canvas* canvas) { 1300 int tab_id; 1301 int frame_id; 1302 views::Widget* widget = GetWidget(); 1303 GetTabIdAndFrameId(widget, &tab_id, &frame_id); 1304 1305 // Explicitly map the id so we cache correctly. 1306 const chrome::HostDesktopType host_desktop_type = GetHostDesktopType(this); 1307 tab_id = chrome::MapThemeImage(host_desktop_type, tab_id); 1308 1309 // HasCustomImage() is only true if the theme provides the image. However, 1310 // even if the theme does not provide a tab background, the theme machinery 1311 // will make one if given a frame image. 1312 ui::ThemeProvider* theme_provider = GetThemeProvider(); 1313 const bool theme_provided_image = theme_provider->HasCustomImage(tab_id) || 1314 (frame_id != 0 && theme_provider->HasCustomImage(frame_id)); 1315 1316 const bool can_cache = !theme_provided_image && 1317 !hover_controller_.ShouldDraw(); 1318 1319 if (can_cache) { 1320 ui::ScaleFactor scale_factor = 1321 ui::GetSupportedScaleFactor(canvas->image_scale()); 1322 gfx::ImageSkia cached_image(GetCachedImage(tab_id, size(), scale_factor)); 1323 if (cached_image.width() == 0) { 1324 gfx::Canvas tmp_canvas(size(), canvas->image_scale(), false); 1325 PaintInactiveTabBackgroundUsingResourceId(&tmp_canvas, tab_id); 1326 cached_image = gfx::ImageSkia(tmp_canvas.ExtractImageRep()); 1327 SetCachedImage(tab_id, scale_factor, cached_image); 1328 } 1329 canvas->DrawImageInt(cached_image, 0, 0); 1330 } else { 1331 PaintInactiveTabBackgroundUsingResourceId(canvas, tab_id); 1332 } 1333} 1334 1335void Tab::PaintInactiveTabBackgroundUsingResourceId(gfx::Canvas* canvas, 1336 int tab_id) { 1337 // WARNING: the inactive tab background may be cached. If you change what it 1338 // is drawn from you may need to update whether it can be cached. 1339 1340 // The tab image needs to be lined up with the background image 1341 // so that it feels partially transparent. These offsets represent the tab 1342 // position within the frame background image. 1343 int offset = GetMirroredX() + background_offset_.x(); 1344 1345 gfx::ImageSkia* tab_bg = GetThemeProvider()->GetImageSkiaNamed(tab_id); 1346 1347 TabImage* tab_image = &tab_active_; 1348 TabImage* tab_inactive_image = &tab_inactive_; 1349 TabImage* alpha = &tab_alpha_; 1350 1351 // If the theme is providing a custom background image, then its top edge 1352 // should be at the top of the tab. Otherwise, we assume that the background 1353 // image is a composited foreground + frame image. 1354 int bg_offset_y = GetThemeProvider()->HasCustomImage(tab_id) ? 1355 0 : background_offset_.y(); 1356 1357 // We need a gfx::Canvas object to be able to extract the image from. 1358 // We draw everything to this canvas and then output it to the canvas 1359 // parameter in addition to using it to mask the hover glow if needed. 1360 gfx::Canvas background_canvas(size(), canvas->image_scale(), false); 1361 1362 // Draw left edge. Don't draw over the toolbar, as we're not the foreground 1363 // tab. 1364 gfx::ImageSkia tab_l = gfx::ImageSkiaOperations::CreateTiledImage( 1365 *tab_bg, offset, bg_offset_y, tab_image->l_width, height()); 1366 gfx::ImageSkia theme_l = 1367 gfx::ImageSkiaOperations::CreateMaskedImage(tab_l, *alpha->image_l); 1368 background_canvas.DrawImageInt(theme_l, 1369 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, 1370 0, 0, theme_l.width(), theme_l.height() - kToolbarOverlap, 1371 false); 1372 1373 // Draw right edge. Again, don't draw over the toolbar. 1374 gfx::ImageSkia tab_r = gfx::ImageSkiaOperations::CreateTiledImage(*tab_bg, 1375 offset + width() - tab_image->r_width, bg_offset_y, 1376 tab_image->r_width, height()); 1377 gfx::ImageSkia theme_r = 1378 gfx::ImageSkiaOperations::CreateMaskedImage(tab_r, *alpha->image_r); 1379 background_canvas.DrawImageInt(theme_r, 1380 0, 0, theme_r.width(), theme_r.height() - kToolbarOverlap, 1381 width() - theme_r.width(), 0, theme_r.width(), 1382 theme_r.height() - kToolbarOverlap, false); 1383 1384 // Draw center. Instead of masking out the top portion we simply skip over 1385 // it by incrementing by GetDropShadowHeight(), since it's a simple 1386 // rectangle. And again, don't draw over the toolbar. 1387 background_canvas.TileImageInt(*tab_bg, 1388 offset + tab_image->l_width, 1389 bg_offset_y + drop_shadow_height(), 1390 tab_image->l_width, 1391 drop_shadow_height(), 1392 width() - tab_image->l_width - tab_image->r_width, 1393 height() - drop_shadow_height() - kToolbarOverlap); 1394 1395 canvas->DrawImageInt( 1396 gfx::ImageSkia(background_canvas.ExtractImageRep()), 0, 0); 1397 1398 if (!GetThemeProvider()->HasCustomImage(tab_id) && 1399 hover_controller_.ShouldDraw()) { 1400 hover_controller_.Draw(canvas, gfx::ImageSkia( 1401 background_canvas.ExtractImageRep())); 1402 } 1403 1404 // Now draw the highlights/shadows around the tab edge. 1405 canvas->DrawImageInt(*tab_inactive_image->image_l, 0, 0); 1406 canvas->TileImageInt(*tab_inactive_image->image_c, 1407 tab_inactive_image->l_width, 0, 1408 width() - tab_inactive_image->l_width - 1409 tab_inactive_image->r_width, 1410 height()); 1411 canvas->DrawImageInt(*tab_inactive_image->image_r, 1412 width() - tab_inactive_image->r_width, 0); 1413} 1414 1415void Tab::PaintActiveTabBackground(gfx::Canvas* canvas) { 1416 gfx::ImageSkia* tab_background = 1417 GetThemeProvider()->GetImageSkiaNamed(IDR_THEME_TOOLBAR); 1418 int offset = GetMirroredX() + background_offset_.x(); 1419 1420 TabImage* tab_image = &tab_active_; 1421 TabImage* alpha = &tab_alpha_; 1422 1423 // Draw left edge. 1424 gfx::ImageSkia tab_l = gfx::ImageSkiaOperations::CreateTiledImage( 1425 *tab_background, offset, 0, tab_image->l_width, height()); 1426 gfx::ImageSkia theme_l = 1427 gfx::ImageSkiaOperations::CreateMaskedImage(tab_l, *alpha->image_l); 1428 canvas->DrawImageInt(theme_l, 0, 0); 1429 1430 // Draw right edge. 1431 gfx::ImageSkia tab_r = gfx::ImageSkiaOperations::CreateTiledImage( 1432 *tab_background, 1433 offset + width() - tab_image->r_width, 0, tab_image->r_width, height()); 1434 gfx::ImageSkia theme_r = 1435 gfx::ImageSkiaOperations::CreateMaskedImage(tab_r, *alpha->image_r); 1436 canvas->DrawImageInt(theme_r, width() - tab_image->r_width, 0); 1437 1438 // Draw center. Instead of masking out the top portion we simply skip over it 1439 // by incrementing by GetDropShadowHeight(), since it's a simple rectangle. 1440 canvas->TileImageInt(*tab_background, 1441 offset + tab_image->l_width, 1442 drop_shadow_height(), 1443 tab_image->l_width, 1444 drop_shadow_height(), 1445 width() - tab_image->l_width - tab_image->r_width, 1446 height() - drop_shadow_height()); 1447 1448 // Now draw the highlights/shadows around the tab edge. 1449 canvas->DrawImageInt(*tab_image->image_l, 0, 0); 1450 canvas->TileImageInt(*tab_image->image_c, tab_image->l_width, 0, 1451 width() - tab_image->l_width - tab_image->r_width, height()); 1452 canvas->DrawImageInt(*tab_image->image_r, width() - tab_image->r_width, 0); 1453} 1454 1455void Tab::PaintIcon(gfx::Canvas* canvas) { 1456 gfx::Rect bounds = GetIconBounds(); 1457 if (bounds.IsEmpty()) 1458 return; 1459 1460 bounds.set_x(GetMirroredXForRect(bounds)); 1461 1462 if (data().network_state != TabRendererData::NETWORK_STATE_NONE) { 1463 // Paint network activity (aka throbber) animation frame. 1464 ui::ThemeProvider* tp = GetThemeProvider(); 1465 gfx::ImageSkia frames(*tp->GetImageSkiaNamed( 1466 (data().network_state == TabRendererData::NETWORK_STATE_WAITING) ? 1467 IDR_THROBBER_WAITING : IDR_THROBBER)); 1468 1469 int icon_size = frames.height(); 1470 int image_offset = loading_animation_frame_ * icon_size; 1471 DrawIconCenter(canvas, frames, image_offset, 1472 icon_size, icon_size, 1473 bounds, false, SkPaint()); 1474 } else if (should_display_crashed_favicon_) { 1475 // Paint crash favicon. 1476 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1477 gfx::ImageSkia crashed_favicon(*rb.GetImageSkiaNamed(IDR_SAD_FAVICON)); 1478 bounds.set_y(bounds.y() + favicon_hiding_offset_); 1479 DrawIconCenter(canvas, crashed_favicon, 0, 1480 crashed_favicon.width(), 1481 crashed_favicon.height(), 1482 bounds, true, SkPaint()); 1483 } else if (!data().favicon.isNull()) { 1484 // Paint the normal favicon. 1485 DrawIconCenter(canvas, data().favicon, 0, 1486 data().favicon.width(), 1487 data().favicon.height(), 1488 bounds, true, SkPaint()); 1489 } 1490} 1491 1492void Tab::PaintMediaIndicator(gfx::Canvas* canvas) { 1493 if (media_indicator_bounds_.IsEmpty() || !media_indicator_animation_) 1494 return; 1495 1496 gfx::Rect bounds = media_indicator_bounds_; 1497 bounds.set_x(GetMirroredXForRect(bounds)); 1498 1499 SkPaint paint; 1500 paint.setAntiAlias(true); 1501 double opaqueness = media_indicator_animation_->GetCurrentValue(); 1502 if (data_.media_state == TAB_MEDIA_STATE_NONE) 1503 opaqueness = 1.0 - opaqueness; // Fading out, not in. 1504 paint.setAlpha(opaqueness * SK_AlphaOPAQUE); 1505 1506 const gfx::ImageSkia& media_indicator_image = 1507 *(chrome::GetTabMediaIndicatorImage(animating_media_state_). 1508 ToImageSkia()); 1509 DrawIconAtLocation(canvas, media_indicator_image, 0, 1510 bounds.x(), bounds.y(), media_indicator_image.width(), 1511 media_indicator_image.height(), true, paint); 1512} 1513 1514void Tab::PaintTitle(gfx::Canvas* canvas, SkColor title_color) { 1515 // Paint the Title. 1516 base::string16 title = data().title; 1517 if (title.empty()) { 1518 title = data().loading ? 1519 l10n_util::GetStringUTF16(IDS_TAB_LOADING_TITLE) : 1520 CoreTabHelper::GetDefaultTitle(); 1521 } else { 1522 Browser::FormatTitleForDisplay(&title); 1523 } 1524 1525 canvas->DrawFadeTruncatingStringRect(title, gfx::Canvas::TruncateFadeTail, 1526 gfx::FontList(*font_), title_color, GetTitleBounds()); 1527} 1528 1529void Tab::AdvanceLoadingAnimation(TabRendererData::NetworkState old_state, 1530 TabRendererData::NetworkState state) { 1531 static bool initialized = false; 1532 static int loading_animation_frame_count = 0; 1533 static int waiting_animation_frame_count = 0; 1534 static int waiting_to_loading_frame_count_ratio = 0; 1535 if (!initialized) { 1536 initialized = true; 1537 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1538 gfx::ImageSkia loading_animation(*rb.GetImageSkiaNamed(IDR_THROBBER)); 1539 loading_animation_frame_count = 1540 loading_animation.width() / loading_animation.height(); 1541 gfx::ImageSkia waiting_animation(*rb.GetImageSkiaNamed( 1542 IDR_THROBBER_WAITING)); 1543 waiting_animation_frame_count = 1544 waiting_animation.width() / waiting_animation.height(); 1545 waiting_to_loading_frame_count_ratio = 1546 waiting_animation_frame_count / loading_animation_frame_count; 1547 1548 base::debug::Alias(&loading_animation_frame_count); 1549 base::debug::Alias(&waiting_animation_frame_count); 1550 CHECK_NE(0, waiting_to_loading_frame_count_ratio) << 1551 "Number of frames in IDR_THROBBER must be equal to or greater " << 1552 "than the number of frames in IDR_THROBBER_WAITING. Please " << 1553 "investigate how this happened and update http://crbug.com/132590, " << 1554 "this is causing crashes in the wild."; 1555 } 1556 1557 // The waiting animation is the reverse of the loading animation, but at a 1558 // different rate - the following reverses and scales the animation_frame_ 1559 // so that the frame is at an equivalent position when going from one 1560 // animation to the other. 1561 if (state != old_state) { 1562 loading_animation_frame_ = loading_animation_frame_count - 1563 (loading_animation_frame_ / waiting_to_loading_frame_count_ratio); 1564 } 1565 1566 if (state == TabRendererData::NETWORK_STATE_WAITING) { 1567 loading_animation_frame_ = (loading_animation_frame_ + 1) % 1568 waiting_animation_frame_count; 1569 // Waiting steps backwards. 1570 immersive_loading_step_ = 1571 (immersive_loading_step_ - 1 + kImmersiveLoadingStepCount) % 1572 kImmersiveLoadingStepCount; 1573 } else if (state == TabRendererData::NETWORK_STATE_LOADING) { 1574 loading_animation_frame_ = (loading_animation_frame_ + 1) % 1575 loading_animation_frame_count; 1576 immersive_loading_step_ = (immersive_loading_step_ + 1) % 1577 kImmersiveLoadingStepCount; 1578 } else { 1579 loading_animation_frame_ = 0; 1580 immersive_loading_step_ = 0; 1581 } 1582 if (controller() && controller()->IsImmersiveStyle()) 1583 SchedulePaintInRect(GetImmersiveBarRect()); 1584 else 1585 ScheduleIconPaint(); 1586} 1587 1588int Tab::IconCapacity() const { 1589 if (height() < GetMinimumUnselectedSize().height()) 1590 return 0; 1591 const int available_width = 1592 std::max(0, width() - left_padding() - right_padding()); 1593 const int width_per_icon = tab_icon_size(); 1594 const int kPaddingBetweenIcons = 2; 1595 if (available_width >= width_per_icon && 1596 available_width < (width_per_icon + kPaddingBetweenIcons)) { 1597 return 1; 1598 } 1599 return available_width / (width_per_icon + kPaddingBetweenIcons); 1600} 1601 1602bool Tab::ShouldShowIcon() const { 1603 return chrome::ShouldTabShowFavicon( 1604 IconCapacity(), data().mini, IsActive(), data().show_icon, 1605 animating_media_state_); 1606} 1607 1608bool Tab::ShouldShowMediaIndicator() const { 1609 return chrome::ShouldTabShowMediaIndicator( 1610 IconCapacity(), data().mini, IsActive(), data().show_icon, 1611 animating_media_state_); 1612} 1613 1614bool Tab::ShouldShowCloseBox() const { 1615 return chrome::ShouldTabShowCloseButton( 1616 IconCapacity(), data().mini, IsActive()); 1617} 1618 1619double Tab::GetThrobValue() { 1620 bool is_selected = IsSelected(); 1621 double min = is_selected ? kSelectedTabOpacity : 0; 1622 double scale = is_selected ? kSelectedTabThrobScale : 1; 1623 1624 if (!data().mini) { 1625 if (tab_animation_.get() && tab_animation_->is_animating()) 1626 return tab_animation_->GetCurrentValue() * kHoverOpacity * scale + min; 1627 } 1628 1629 if (hover_controller_.ShouldDraw()) { 1630 return kHoverOpacity * hover_controller_.GetAnimationValue() * scale + 1631 min; 1632 } 1633 1634 return is_selected ? kSelectedTabOpacity : 0; 1635} 1636 1637void Tab::SetFaviconHidingOffset(int offset) { 1638 favicon_hiding_offset_ = offset; 1639 ScheduleIconPaint(); 1640} 1641 1642void Tab::DisplayCrashedFavicon() { 1643 should_display_crashed_favicon_ = true; 1644} 1645 1646void Tab::ResetCrashedFavicon() { 1647 should_display_crashed_favicon_ = false; 1648} 1649 1650void Tab::StopCrashAnimation() { 1651 crash_icon_animation_.reset(); 1652} 1653 1654void Tab::StartCrashAnimation() { 1655 crash_icon_animation_.reset(new FaviconCrashAnimation(this)); 1656 crash_icon_animation_->Start(); 1657} 1658 1659bool Tab::IsPerformingCrashAnimation() const { 1660 return crash_icon_animation_.get() && data_.IsCrashed(); 1661} 1662 1663void Tab::StartMediaIndicatorAnimation() { 1664 media_indicator_animation_ = 1665 chrome::CreateTabMediaIndicatorFadeAnimation(data_.media_state); 1666 media_indicator_animation_->set_delegate(this); 1667 media_indicator_animation_->Start(); 1668} 1669 1670void Tab::ScheduleIconPaint() { 1671 gfx::Rect bounds = GetIconBounds(); 1672 if (bounds.IsEmpty()) 1673 return; 1674 1675 // Extends the area to the bottom when sad_favicon is 1676 // animating. 1677 if (IsPerformingCrashAnimation()) 1678 bounds.set_height(height() - bounds.y()); 1679 bounds.set_x(GetMirroredXForRect(bounds)); 1680 SchedulePaintInRect(bounds); 1681} 1682 1683gfx::Rect Tab::GetImmersiveBarRect() const { 1684 // The main bar is as wide as the normal tab's horizontal top line. 1685 // This top line of the tab extends a few pixels left and right of the 1686 // center image due to pixels in the rounded corner images. 1687 const int kBarPadding = 1; 1688 int main_bar_left = tab_active_.l_width - kBarPadding; 1689 int main_bar_right = width() - tab_active_.r_width + kBarPadding; 1690 return gfx::Rect( 1691 main_bar_left, 0, main_bar_right - main_bar_left, kImmersiveBarHeight); 1692} 1693 1694void Tab::GetTabIdAndFrameId(views::Widget* widget, 1695 int* tab_id, 1696 int* frame_id) const { 1697 if (widget && widget->GetTopLevelWidget()->ShouldUseNativeFrame()) { 1698 *tab_id = IDR_THEME_TAB_BACKGROUND_V; 1699 *frame_id = 0; 1700 } else if (data().incognito) { 1701 *tab_id = IDR_THEME_TAB_BACKGROUND_INCOGNITO; 1702 *frame_id = IDR_THEME_FRAME_INCOGNITO; 1703#if defined(OS_WIN) 1704 } else if (win8::IsSingleWindowMetroMode()) { 1705 *tab_id = IDR_THEME_TAB_BACKGROUND_V; 1706 *frame_id = 0; 1707#endif 1708 } else { 1709 *tab_id = IDR_THEME_TAB_BACKGROUND; 1710 *frame_id = IDR_THEME_FRAME; 1711 } 1712} 1713 1714//////////////////////////////////////////////////////////////////////////////// 1715// Tab, private static: 1716 1717// static 1718void Tab::InitTabResources() { 1719 static bool initialized = false; 1720 if (initialized) 1721 return; 1722 1723 initialized = true; 1724 1725 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1726 font_ = new gfx::Font(rb.GetFont(ui::ResourceBundle::BaseFont)); 1727 font_height_ = font_->GetHeight(); 1728 1729 image_cache_ = new ImageCache(); 1730 1731 // Load the tab images once now, and maybe again later if the theme changes. 1732 LoadTabImages(); 1733} 1734 1735// static 1736void Tab::LoadTabImages() { 1737 // We're not letting people override tab images just yet. 1738 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 1739 1740 tab_alpha_.image_l = rb.GetImageSkiaNamed(IDR_TAB_ALPHA_LEFT); 1741 tab_alpha_.image_r = rb.GetImageSkiaNamed(IDR_TAB_ALPHA_RIGHT); 1742 1743 tab_active_.image_l = rb.GetImageSkiaNamed(IDR_TAB_ACTIVE_LEFT); 1744 tab_active_.image_c = rb.GetImageSkiaNamed(IDR_TAB_ACTIVE_CENTER); 1745 tab_active_.image_r = rb.GetImageSkiaNamed(IDR_TAB_ACTIVE_RIGHT); 1746 tab_active_.l_width = tab_active_.image_l->width(); 1747 tab_active_.r_width = tab_active_.image_r->width(); 1748 1749 tab_inactive_.image_l = rb.GetImageSkiaNamed(IDR_TAB_INACTIVE_LEFT); 1750 tab_inactive_.image_c = rb.GetImageSkiaNamed(IDR_TAB_INACTIVE_CENTER); 1751 tab_inactive_.image_r = rb.GetImageSkiaNamed(IDR_TAB_INACTIVE_RIGHT); 1752 tab_inactive_.l_width = tab_inactive_.image_l->width(); 1753 tab_inactive_.r_width = tab_inactive_.image_r->width(); 1754} 1755 1756// static 1757gfx::ImageSkia Tab::GetCachedImage(int resource_id, 1758 const gfx::Size& size, 1759 ui::ScaleFactor scale_factor) { 1760 for (ImageCache::const_iterator i = image_cache_->begin(); 1761 i != image_cache_->end(); ++i) { 1762 if (i->resource_id == resource_id && i->scale_factor == scale_factor && 1763 i->image.size() == size) { 1764 return i->image; 1765 } 1766 } 1767 return gfx::ImageSkia(); 1768} 1769 1770// static 1771void Tab::SetCachedImage(int resource_id, 1772 ui::ScaleFactor scale_factor, 1773 const gfx::ImageSkia& image) { 1774 DCHECK_NE(scale_factor, ui::SCALE_FACTOR_NONE); 1775 ImageCacheEntry entry; 1776 entry.resource_id = resource_id; 1777 entry.scale_factor = scale_factor; 1778 entry.image = image; 1779 image_cache_->push_front(entry); 1780 if (image_cache_->size() > kMaxImageCacheSize) 1781 image_cache_->pop_back(); 1782} 1783