Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / avatar_label.cc
1 // Copyright 2013 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/avatar_label.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/themes/theme_properties.h"
9 #include "chrome/browser/ui/views/frame/browser_view.h"
10 #include "grit/generated_resources.h"
11 #include "grit/theme_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/base/theme_provider.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/color_utils.h"
16 #include "ui/views/painter.h"
17
18 namespace {
19
20 // A custom border for the managed user avatar label.
21 class AvatarLabelBorder : public views::Border {
22  public:
23   explicit AvatarLabelBorder(bool label_on_right);
24
25   // views::Border:
26   virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE;
27   virtual gfx::Insets GetInsets() const OVERRIDE;
28   virtual gfx::Size GetMinimumSize() const OVERRIDE;
29
30  private:
31   scoped_ptr<views::Painter> painter_;
32   gfx::Insets insets_;
33
34   DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder);
35 };
36
37 AvatarLabelBorder::AvatarLabelBorder(bool label_on_right) {
38   const int kHorizontalInsetRight = label_on_right ? 43 : 10;
39   const int kHorizontalInsetLeft = label_on_right ? 10 : 43;
40   const int kVerticalInsetTop = 2;
41   const int kVerticalInsetBottom = 3;
42   // We want to align with the top of the tab. This works if the default font
43   // size is 13. If it is smaller, we need to increase the TopInset accordingly.
44   const int difference = std::max<int>(0, 13 - gfx::FontList().GetFontSize());
45   const int addToTop = difference / 2;
46   const int addToBottom = difference - addToTop;
47   insets_ = gfx::Insets(kVerticalInsetTop + addToTop,
48                         kHorizontalInsetLeft,
49                         kVerticalInsetBottom + addToBottom,
50                         kHorizontalInsetRight);
51   const int kImages[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL);
52   painter_.reset(views::Painter::CreateImageGridPainter(kImages));
53 }
54
55 void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) {
56   // Paint the default background using the image assets provided by UI. This
57   // includes a border with almost transparent white color.
58   painter_->Paint(canvas, view.size());
59
60   // Repaint the inner part of the background in order to be able to change
61   // the colors according to the currently installed theme.
62   gfx::Rect rect(1, 1, view.size().width() - 2, view.size().height() - 2);
63   SkPaint paint;
64   int kRadius = 2;
65   SkColor background_color = view.GetThemeProvider()->GetColor(
66       ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
67   paint.setStyle(SkPaint::kFill_Style);
68
69   // Paint the inner border with a color slightly darker than the background.
70   SkAlpha kAlphaForBlending = 230;
71   paint.setColor(color_utils::AlphaBlend(
72       background_color, SK_ColorBLACK, kAlphaForBlending));
73   canvas->DrawRoundRect(rect, kRadius, paint);
74
75   // Paint the inner background using the color provided by the ThemeProvider.
76   paint.setColor(background_color);
77   rect = gfx::Rect(2, 2, view.size().width() - 4, view.size().height() - 4);
78   canvas->DrawRoundRect(rect, kRadius, paint);
79 }
80
81 gfx::Insets AvatarLabelBorder::GetInsets() const {
82   return insets_;
83 }
84
85 gfx::Size AvatarLabelBorder::GetMinimumSize() const {
86   gfx::Size size(4, 4);
87   size.SetToMax(painter_->GetMinimumSize());
88   return size;
89 }
90
91 }  // namespace
92
93 AvatarLabel::AvatarLabel(BrowserView* browser_view)
94     : LabelButton(NULL,
95                   l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)),
96       browser_view_(browser_view) {
97   SetLabelOnRight(false);
98   UpdateLabelStyle();
99 }
100
101 AvatarLabel::~AvatarLabel() {}
102
103 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) {
104   if (!LabelButton::OnMousePressed(event))
105     return false;
106
107   browser_view_->ShowAvatarBubbleFromAvatarButton();
108   return true;
109 }
110
111 void AvatarLabel::UpdateLabelStyle() {
112   // |browser_view_| can be NULL in unit tests.
113   if (!browser_view_)
114     return;
115
116   SkColor color_label = browser_view_->frame()->GetThemeProvider()->GetColor(
117       ThemeProperties::COLOR_MANAGED_USER_LABEL);
118   for (size_t state = 0; state < STATE_COUNT; ++state)
119     SetTextColor(static_cast<ButtonState>(state), color_label);
120   SchedulePaint();
121 }
122
123 void AvatarLabel::SetLabelOnRight(bool label_on_right) {
124   SetBorder(scoped_ptr<views::Border>(new AvatarLabelBorder(label_on_right)));
125 }