summaryrefslogtreecommitdiffstats
path: root/ash/launcher/tabbed_launcher_button.cc
blob: 398e2103caa5fcf96d788428c8ced2a172580a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/launcher/tabbed_launcher_button.h"

#include <algorithm>

#include "ash/launcher/launcher_button_host.h"
#include "ash/launcher/launcher_types.h"
#include "grit/ui_resources.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/animation/multi_animation.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/insets.h"

namespace ash {
namespace internal {

TabbedLauncherButton::IconView::IconView(
    TabbedLauncherButton* host)
    : host_(host) {
  if (!browser_image_) {
    ResourceBundle& rb = ResourceBundle::GetSharedInstance();

    browser_image_ = new SkBitmap(
        *rb.GetImageNamed(IDR_AURA_LAUNCHER_BROWSER).ToSkBitmap());
    incognito_browser_image_ = new SkBitmap(
        *rb.GetImageNamed(IDR_AURA_LAUNCHER_INCOGNITO_BROWSER).ToSkBitmap());
    browser_panel_image_ = new SkBitmap(
        *rb.GetImageNamed(IDR_AURA_LAUNCHER_BROWSER_PANEL).ToSkBitmap());
    incognito_browser_panel_image_ = new SkBitmap(
        *rb.GetImageNamed(
            IDR_AURA_LAUNCHER_INCOGNITO_BROWSER_PANEL).ToSkBitmap());
  }
  set_icon_size(0);
  if (host->is_incognito() == STATE_NOT_INCOGNITO)
    LauncherButton::IconView::SetImage(*browser_image_);
  else
    LauncherButton::IconView::SetImage(*incognito_browser_image_);
}

TabbedLauncherButton::IconView::~IconView() {
}

void TabbedLauncherButton::IconView::AnimationEnded(
    const ui::Animation* animation) {
  AnimationProgressed(animation);
  animating_image_ = SkBitmap();
}

void TabbedLauncherButton::IconView::AnimationProgressed(
    const ui::Animation* animation) {
  if (animation_->current_part_index() == 1)
    SchedulePaint();
}

void TabbedLauncherButton::IconView::SetTabImage(const SkBitmap& image) {
  if (image.empty()) {
    if (!image_.empty()) {
      // Pause for 500ms, then ease out for 200ms.
      ui::MultiAnimation::Parts animation_parts;
      animation_parts.push_back(ui::MultiAnimation::Part(500, ui::Tween::ZERO));
      animation_parts.push_back(
          ui::MultiAnimation::Part(200, ui::Tween::EASE_OUT));
      animation_.reset(new ui::MultiAnimation(animation_parts));
      animation_->set_continuous(false);
      animation_->set_delegate(this);
      animation_->Start();
      animating_image_ = image_;
      image_ = image;
    }
  } else {
    animation_.reset();
    SchedulePaint();
    image_ = image;
  }
}

void TabbedLauncherButton::IconView::OnPaint(gfx::Canvas* canvas) {
  LauncherButton::IconView::OnPaint(canvas);

  // Only non incognito icons show the tab image.
  if (host_->is_incognito() != STATE_NOT_INCOGNITO)
    return;

  if ((animation_.get() && animation_->is_animating() &&
      animation_->current_part_index() == 1)) {
    int x = (width() - animating_image_.width()) / 2;
    int y = (height() - animating_image_.height()) / 2;
    canvas->SaveLayerAlpha(animation_->CurrentValueBetween(255, 0));
    canvas->DrawBitmapInt(animating_image_, x, y);
    canvas->Restore();
  } else {
    int x = (width() - image_.width()) / 2;
    int y = (height() - image_.height()) / 2;
    canvas->DrawBitmapInt(image_, x, y);
  }
}

// static
SkBitmap* TabbedLauncherButton::IconView::browser_image_ = NULL;
SkBitmap* TabbedLauncherButton::IconView::incognito_browser_image_ = NULL;
SkBitmap* TabbedLauncherButton::IconView::browser_panel_image_ = NULL;
SkBitmap* TabbedLauncherButton::IconView::incognito_browser_panel_image_ = NULL;

TabbedLauncherButton* TabbedLauncherButton::Create(
    views::ButtonListener* listener,
    LauncherButtonHost* host,
    IncognitoState is_incognito) {
  TabbedLauncherButton* button =
      new TabbedLauncherButton(listener, host, is_incognito);
  button->Init();
  return button;
}

TabbedLauncherButton::TabbedLauncherButton(views::ButtonListener* listener,
                                           LauncherButtonHost* host,
                                           IncognitoState is_incognito)
    : LauncherButton(listener, host),
      is_incognito_(is_incognito) {
  set_accessibility_focusable(true);
}

TabbedLauncherButton::~TabbedLauncherButton() {
}

void TabbedLauncherButton::SetTabImage(const SkBitmap& image) {
  tabbed_icon_view()->SetTabImage(image);
}

void TabbedLauncherButton::GetAccessibleState(ui::AccessibleViewState* state) {
  state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
  state->name = host()->GetAccessibleName(this);
}

LauncherButton::IconView* TabbedLauncherButton::CreateIconView() {
  return new IconView(this);
}

}  // namespace internal
}  // namespace ash