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.
5 #include "chrome/browser/ui/views/avatar_label.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/themes/theme_properties.h"
9 #include "chrome/browser/ui/views/avatar_menu_bubble_view.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "grit/generated_resources.h"
12 #include "grit/theme_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/theme_provider.h"
15 #include "ui/events/event.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/color_utils.h"
18 #include "ui/gfx/font_list.h"
19 #include "ui/views/painter.h"
23 // A special text button border for the managed user avatar label.
24 class AvatarLabelBorder
: public views::TextButtonBorder
{
26 explicit AvatarLabelBorder(bool label_on_right
);
28 // views::TextButtonBorder:
29 virtual void Paint(const views::View
& view
, gfx::Canvas
* canvas
) OVERRIDE
;
30 virtual gfx::Size
GetMinimumSize() const OVERRIDE
;
33 scoped_ptr
<views::Painter
> painter_
;
35 DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder
);
38 AvatarLabelBorder::AvatarLabelBorder(bool label_on_right
) {
39 const int kHorizontalInsetRight
= label_on_right
? 43 : 10;
40 const int kHorizontalInsetLeft
= label_on_right
? 10 : 43;
41 const int kVerticalInsetTop
= 2;
42 const int kVerticalInsetBottom
= 3;
43 // We want to align with the top of the tab. This works if the default font
44 // size is 13. If it is smaller, we need to increase the TopInset accordingly.
45 const gfx::FontList font_list
;
47 (font_list
.GetFontSize() < 13) ? 13 - font_list
.GetFontSize() : 0;
48 int addToTop
= difference
/ 2;
49 int addToBottom
= difference
- addToTop
;
50 SetInsets(gfx::Insets(kVerticalInsetTop
+ addToTop
,
52 kVerticalInsetBottom
+ addToBottom
,
53 kHorizontalInsetRight
));
54 const int kImages
[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL
);
55 painter_
.reset(views::Painter::CreateImageGridPainter(kImages
));
58 void AvatarLabelBorder::Paint(const views::View
& view
, gfx::Canvas
* canvas
) {
59 // Paint the default background using the image assets provided by UI. This
60 // includes a border with almost transparent white color.
61 painter_
->Paint(canvas
, view
.size());
63 // Now repaint the inner part of the background in order to be able to change
64 // the colors according to the currently installed theme.
65 gfx::Rect
rect(1, 1, view
.size().width() - 2, view
.size().height() - 2);
68 SkColor background_color
= view
.GetThemeProvider()->GetColor(
69 ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND
);
70 paint
.setStyle(SkPaint::kFill_Style
);
72 // For the inner border, use a color which is slightly darker than the
74 SkAlpha kAlphaForBlending
= 230;
75 paint
.setColor(color_utils::AlphaBlend(
76 background_color
, SK_ColorBLACK
, kAlphaForBlending
));
77 canvas
->DrawRoundRect(rect
, kRadius
, paint
);
79 // Now paint the inner background using the color provided by the
81 paint
.setColor(background_color
);
82 rect
= gfx::Rect(2, 2, view
.size().width() - 4, view
.size().height() - 4);
83 canvas
->DrawRoundRect(rect
, kRadius
, paint
);
86 gfx::Size
AvatarLabelBorder::GetMinimumSize() const {
88 size
.SetToMax(painter_
->GetMinimumSize());
94 AvatarLabel::AvatarLabel(BrowserView
* browser_view
)
96 l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL
)),
97 browser_view_(browser_view
) {
99 SetLabelOnRight(false);
103 AvatarLabel::~AvatarLabel() {}
105 bool AvatarLabel::OnMousePressed(const ui::MouseEvent
& event
) {
106 if (!TextButton::OnMousePressed(event
))
109 browser_view_
->ShowAvatarBubbleFromAvatarButton();
113 void AvatarLabel::UpdateLabelStyle() {
114 // |browser_view_| can be NULL in unit tests.
118 SkColor color_label
= browser_view_
->frame()->GetThemeProvider()->GetColor(
119 ThemeProperties::COLOR_MANAGED_USER_LABEL
);
120 SetEnabledColor(color_label
);
121 SetHighlightColor(color_label
);
122 SetHoverColor(color_label
);
123 SetDisabledColor(color_label
);
127 void AvatarLabel::SetLabelOnRight(bool label_on_right
) {
128 set_border(new AvatarLabelBorder(label_on_right
));