Home | History | Annotate | Download | only in launcher
      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/launcher/tabbed_launcher_button.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "ash/launcher/launcher_button_host.h"
     10 #include "ash/launcher/launcher_types.h"
     11 #include "grit/ash_resources.h"
     12 #include "ui/base/accessibility/accessible_view_state.h"
     13 #include "ui/base/animation/multi_animation.h"
     14 #include "ui/base/resource/resource_bundle.h"
     15 #include "ui/gfx/canvas.h"
     16 #include "ui/gfx/image/image.h"
     17 #include "ui/gfx/insets.h"
     18 
     19 namespace {
     20 const int kIconOffsetY = 7;
     21 }
     22 
     23 namespace ash {
     24 namespace internal {
     25 
     26 TabbedLauncherButton::IconView::IconView(
     27     TabbedLauncherButton* host)
     28     : host_(host) {
     29   if (!browser_image_) {
     30     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
     31 
     32     browser_image_ = rb.GetImageNamed(IDR_AURA_LAUNCHER_BROWSER).ToImageSkia();
     33     incognito_browser_image_ =
     34         rb.GetImageNamed(IDR_AURA_LAUNCHER_INCOGNITO_BROWSER).ToImageSkia();
     35     browser_panel_image_ =
     36         rb.GetImageNamed(IDR_AURA_LAUNCHER_BROWSER_PANEL).ToImageSkia();
     37     incognito_browser_panel_image_ =
     38         rb.GetImageNamed(
     39             IDR_AURA_LAUNCHER_INCOGNITO_BROWSER_PANEL).ToImageSkia();
     40   }
     41   set_icon_size(0);
     42   if (host->is_incognito() == STATE_NOT_INCOGNITO)
     43     LauncherButton::IconView::SetImage(*browser_image_);
     44   else
     45     LauncherButton::IconView::SetImage(*incognito_browser_image_);
     46 }
     47 
     48 TabbedLauncherButton::IconView::~IconView() {
     49 }
     50 
     51 void TabbedLauncherButton::IconView::AnimationEnded(
     52     const ui::Animation* animation) {
     53   AnimationProgressed(animation);
     54   animating_image_ = gfx::ImageSkia();
     55 }
     56 
     57 void TabbedLauncherButton::IconView::AnimationProgressed(
     58     const ui::Animation* animation) {
     59   if (animation_->current_part_index() == 1)
     60     SchedulePaint();
     61 }
     62 
     63 void TabbedLauncherButton::IconView::SetTabImage(const gfx::ImageSkia& image) {
     64   if (image.isNull()) {
     65     if (!image_.isNull()) {
     66       // Pause for 500ms, then ease out for 200ms.
     67       ui::MultiAnimation::Parts animation_parts;
     68       animation_parts.push_back(ui::MultiAnimation::Part(500, ui::Tween::ZERO));
     69       animation_parts.push_back(
     70           ui::MultiAnimation::Part(200, ui::Tween::EASE_OUT));
     71       animation_.reset(new ui::MultiAnimation(
     72           animation_parts,
     73           ui::MultiAnimation::GetDefaultTimerInterval()));
     74       animation_->set_continuous(false);
     75       animation_->set_delegate(this);
     76       animation_->Start();
     77       animating_image_ = image_;
     78       image_ = image;
     79     }
     80   } else {
     81     animation_.reset();
     82     SchedulePaint();
     83     image_ = image;
     84   }
     85 }
     86 
     87 void TabbedLauncherButton::IconView::OnPaint(gfx::Canvas* canvas) {
     88   LauncherButton::IconView::OnPaint(canvas);
     89 
     90   // Only non incognito icons show the tab image.
     91   if (host_->is_incognito() != STATE_NOT_INCOGNITO)
     92     return;
     93 
     94   if ((animation_.get() && animation_->is_animating() &&
     95       animation_->current_part_index() == 1)) {
     96     int x = (width() - animating_image_.width()) / 2;
     97     canvas->SaveLayerAlpha(animation_->CurrentValueBetween(255, 0));
     98     canvas->DrawImageInt(animating_image_, x, kIconOffsetY);
     99     canvas->Restore();
    100   } else {
    101     int x = (width() - image_.width()) / 2;
    102     canvas->DrawImageInt(image_, x, kIconOffsetY);
    103   }
    104 }
    105 
    106 // static
    107 const gfx::ImageSkia* TabbedLauncherButton::IconView::browser_image_ = NULL;
    108 const gfx::ImageSkia* TabbedLauncherButton::IconView::incognito_browser_image_ =
    109     NULL;
    110 const gfx::ImageSkia* TabbedLauncherButton::IconView::browser_panel_image_ =
    111     NULL;
    112 const gfx::ImageSkia*
    113     TabbedLauncherButton::IconView::incognito_browser_panel_image_ = NULL;
    114 
    115 TabbedLauncherButton* TabbedLauncherButton::Create(
    116     views::ButtonListener* listener,
    117     LauncherButtonHost* host,
    118     ShelfLayoutManager* shelf_layout_manager,
    119     IncognitoState is_incognito) {
    120   TabbedLauncherButton* button = new TabbedLauncherButton(
    121       listener, host, shelf_layout_manager, is_incognito);
    122   button->Init();
    123   return button;
    124 }
    125 
    126 TabbedLauncherButton::TabbedLauncherButton(
    127     views::ButtonListener* listener,
    128     LauncherButtonHost* host,
    129     ShelfLayoutManager* shelf_layout_manager,
    130     IncognitoState is_incognito)
    131     : LauncherButton(listener, host, shelf_layout_manager),
    132       is_incognito_(is_incognito) {
    133   set_accessibility_focusable(true);
    134 }
    135 
    136 TabbedLauncherButton::~TabbedLauncherButton() {
    137 }
    138 
    139 void TabbedLauncherButton::SetTabImage(const gfx::ImageSkia& image) {
    140   tabbed_icon_view()->SetTabImage(image);
    141 }
    142 
    143 void TabbedLauncherButton::GetAccessibleState(ui::AccessibleViewState* state) {
    144   state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
    145   state->name = host()->GetAccessibleName(this);
    146 }
    147 
    148 LauncherButton::IconView* TabbedLauncherButton::CreateIconView() {
    149   return new IconView(this);
    150 }
    151 
    152 }  // namespace internal
    153 }  // namespace ash
    154