summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/tabs/side_tab.cc
blob: 54795fb0ff833c712fe22166cadcf4c6c8c64a6e (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2010 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 "chrome/browser/views/tabs/side_tab.h"

#include "app/resource_bundle.h"
#include "app/theme_provider.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "gfx/canvas.h"
#include "gfx/path.h"
#include "gfx/skia_util.h"
#include "grit/app_resources.h"
#include "grit/theme_resources.h"
#include "views/controls/button/image_button.h"

namespace {
const int kVerticalTabHeight = 27;
const int kIconSize = 16;
const int kIconTitleSpacing = 4;
const int kTitleCloseSpacing = 4;
const SkScalar kRoundRectRadius = 5;
const SkColor kTabBackgroundColor = SK_ColorWHITE;
const SkAlpha kBackgroundTabAlpha = 170;
static const int kHoverDurationMs = 900;

static SkBitmap* waiting_animation_frames = NULL;
static SkBitmap* loading_animation_frames = NULL;
static int loading_animation_frame_count = 0;
static int waiting_animation_frame_count = 0;
static int waiting_to_loading_frame_count_ratio = 0;
};

// static
gfx::Font* SideTab::font_ = NULL;
SkBitmap* SideTab::close_button_n_ = NULL;
SkBitmap* SideTab::close_button_h_ = NULL;
SkBitmap* SideTab::close_button_p_ = NULL;

////////////////////////////////////////////////////////////////////////////////
// SideTab, public:

SideTab::SideTab(SideTabModel* model)
    : model_(model),
      close_button_(NULL),
      current_state_(SideTabStripModel::NetworkState_None),
      loading_animation_frame_(0) {
  InitClass();

  views::ImageButton* close_button = new views::ImageButton(this);
  close_button->SetImage(views::CustomButton::BS_NORMAL, close_button_n_);
  close_button->SetImage(views::CustomButton::BS_HOT, close_button_h_);
  close_button->SetImage(views::CustomButton::BS_PUSHED, close_button_p_);
  close_button_ = close_button;
  AddChildView(close_button_);

  hover_animation_.reset(new SlideAnimation(this));
  hover_animation_->SetSlideDuration(kHoverDurationMs);

  SetContextMenuController(this);
}

SideTab::~SideTab() {
}

void SideTab::SetNetworkState(SideTabStripModel::NetworkState state) {
  if (current_state_ != state) {
    // The waiting animation is the reverse of the loading animation, but at a
    // different rate - the following reverses and scales the animation_frame_
    // so that the frame is at an equivalent position when going from one
    // animation to the other.
    if (current_state_ == SideTabStripModel::NetworkState_Waiting &&
        current_state_ == SideTabStripModel::NetworkState_Loading) {
      loading_animation_frame_ = loading_animation_frame_count -
          (loading_animation_frame_ / waiting_to_loading_frame_count_ratio);
    }
    current_state_ = state;
  }

  if (current_state_ != SideTabStripModel::NetworkState_None) {
    loading_animation_frame_ = ++loading_animation_frame_ %
        ((current_state_ == SideTabStripModel::NetworkState_Waiting) ?
            waiting_animation_frame_count :
            loading_animation_frame_count);
  } else {
    loading_animation_frame_ = 0;
  }
  SchedulePaint();
}

////////////////////////////////////////////////////////////////////////////////
// SideTab, AnimationDelegate implementation:

void SideTab::AnimationProgressed(const Animation* animation) {
  SchedulePaint();
}

void SideTab::AnimationCanceled(const Animation* animation) {
  AnimationEnded(animation);
}

void SideTab::AnimationEnded(const Animation* animation) {
  SchedulePaint();
}

////////////////////////////////////////////////////////////////////////////////
// SideTab, views::ButtonListener implementation:

void SideTab::ButtonPressed(views::Button* sender, const views::Event& event) {
  DCHECK(sender == close_button_);
  model_->CloseTab(this);
}

////////////////////////////////////////////////////////////////////////////////
// SideTab, views::ContextMenuController implementation:

void SideTab::ShowContextMenu(views::View* source,
                              const gfx::Point& p,
                              bool is_mouse_gesture) {
  model_->ShowContextMenu(this, p);
}

////////////////////////////////////////////////////////////////////////////////
// SideTab, views::View overrides:

void SideTab::Layout() {
  int icon_y;
  int icon_x = icon_y = (height() - kIconSize) / 2;
  icon_bounds_.SetRect(icon_x, icon_y, kIconSize, kIconSize);

  gfx::Size ps = close_button_->GetPreferredSize();
  int close_y = (height() - ps.height()) / 2;
  close_button_->SetBounds(
      std::max(0, width() - ps.width() - close_y),
      close_y,
      ps.width(),
      ps.height());

  int title_y = (height() - font_->height()) / 2;
  int title_x = icon_bounds_.right() + kIconTitleSpacing;
  title_bounds_.SetRect(
      title_x,
      title_y,
      std::max(0, close_button_->x() - kTitleCloseSpacing - title_x),
      font_->height());
}

void SideTab::Paint(gfx::Canvas* canvas) {
  SkPaint paint;
  paint.setColor(kTabBackgroundColor);
  paint.setAntiAlias(true);
  gfx::Path tab_shape;
  FillTabShapePath(&tab_shape);
  canvas->drawPath(tab_shape, paint);

  PaintIcon(canvas);
  canvas->DrawStringInt(UTF16ToWideHack(model_->GetTitle(this)), *font_,
                        SK_ColorBLACK, title_bounds_.x(), title_bounds_.y(),
                        title_bounds_.width(), title_bounds_.height());

  if (!model_->IsSelected(this) &&
      GetThemeProvider()->ShouldUseNativeFrame()) {
    // Make sure un-selected tabs are somewhat transparent.
    SkPaint paint;

    SkAlpha opacity = kBackgroundTabAlpha;
    if (hover_animation_->is_animating())
      opacity = static_cast<SkAlpha>(hover_animation_->GetCurrentValue() * 255);

    paint.setColor(SkColorSetARGB(kBackgroundTabAlpha, 255, 255, 255));
    paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
    paint.setStyle(SkPaint::kFill_Style);
    paint.setAntiAlias(true);
    canvas->FillRectInt(0, 0, width(), height(), paint);
  }
}

gfx::Size SideTab::GetPreferredSize() {
  return gfx::Size(0, 27);
}

void SideTab::OnMouseEntered(const views::MouseEvent& event) {
  hover_animation_->SetTweenType(Tween::EASE_OUT);
  hover_animation_->Show();
}

void SideTab::OnMouseExited(const views::MouseEvent& event) {
  hover_animation_->SetTweenType(Tween::EASE_IN);
  hover_animation_->Hide();
}

bool SideTab::OnMousePressed(const views::MouseEvent& event) {
  if (event.IsOnlyLeftMouseButton())
    model_->SelectTab(this);
  return true;
}

////////////////////////////////////////////////////////////////////////////////
// SideTab, private:

#define CUBIC_ARC_FACTOR    ((SK_ScalarSqrt2 - SK_Scalar1) * 4 / 3)

void SideTab::FillTabShapePath(gfx::Path* path) {
  SkScalar s = SkScalarMul(kRoundRectRadius, CUBIC_ARC_FACTOR);
  path->moveTo(SkIntToScalar(kRoundRectRadius), 0);
  path->cubicTo(SkIntToScalar(kRoundRectRadius) - s, 0, 0,
                SkIntToScalar(kRoundRectRadius) - s, 0,
                SkIntToScalar(kRoundRectRadius));
  path->lineTo(0, SkIntToScalar(height() - kRoundRectRadius));
  path->cubicTo(0, SkIntToScalar(height() - kRoundRectRadius) + s,
                SkIntToScalar(kRoundRectRadius) - s, SkIntToScalar(height()),
                SkIntToScalar(kRoundRectRadius),
                SkIntToScalar(height()));
  path->lineTo(SkIntToScalar(width()), SkIntToScalar(height()));
  path->lineTo(SkIntToScalar(width()), 0);
  path->lineTo(SkIntToScalar(kRoundRectRadius), 0);
  path->close();
}

void SideTab::PaintIcon(gfx::Canvas* canvas) {
  if (current_state_ == SideTabStripModel::NetworkState_None) {
    canvas->DrawBitmapInt(model_->GetIcon(this), 0, 0, kIconSize, kIconSize,
                          icon_bounds_.x(), icon_bounds_.y(),
                          icon_bounds_.width(), icon_bounds_.height(), false);
  } else {
    PaintLoadingAnimation(canvas);
  }
}

void SideTab::PaintLoadingAnimation(gfx::Canvas* canvas) {
  SkBitmap* frames =
      (current_state_ == SideTabStripModel::NetworkState_Waiting) ?
          waiting_animation_frames : loading_animation_frames;
  int image_size = frames->height();
  int image_offset = loading_animation_frame_ * image_size;
  int dst_y = (height() - image_size) / 2;
  canvas->DrawBitmapInt(*frames, image_offset, 0, image_size,
                        image_size, icon_bounds_.x(), dst_y, image_size,
                        image_size, false);
}

// static
void SideTab::InitClass() {
  static bool initialized = false;
  if (!initialized) {
    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
    font_ = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont));

    close_button_n_ = rb.GetBitmapNamed(IDR_TAB_CLOSE);
    close_button_h_ = rb.GetBitmapNamed(IDR_TAB_CLOSE_H);
    close_button_p_ = rb.GetBitmapNamed(IDR_TAB_CLOSE_P);

    // The loading animation image is a strip of states. Each state must be
    // square, so the height must divide the width evenly.
    loading_animation_frames = rb.GetBitmapNamed(IDR_THROBBER);
    DCHECK(loading_animation_frames);
    DCHECK(loading_animation_frames->width() %
           loading_animation_frames->height() == 0);
    loading_animation_frame_count =
        loading_animation_frames->width() / loading_animation_frames->height();

    waiting_animation_frames = rb.GetBitmapNamed(IDR_THROBBER_WAITING);
    DCHECK(waiting_animation_frames);
    DCHECK(waiting_animation_frames->width() %
           waiting_animation_frames->height() == 0);
    waiting_animation_frame_count =
        waiting_animation_frames->width() / waiting_animation_frames->height();

    waiting_to_loading_frame_count_ratio =
        waiting_animation_frame_count / loading_animation_frame_count;

    initialized = true;
  }
}