1 // Copyright 2014 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/profiles/supervised_user_avatar_label.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/signin/signin_header_helper.h"
9 #include "chrome/browser/themes/theme_properties.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "chrome/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/gfx/canvas.h"
16 #include "ui/gfx/color_utils.h"
17 #include "ui/views/border.h"
18 #include "ui/views/painter.h"
22 // A custom border for the supervised user avatar label.
23 class SupervisedUserAvatarLabelBorder
: public views::Border
{
25 explicit SupervisedUserAvatarLabelBorder(bool label_on_right
);
28 void Paint(const views::View
& view
, gfx::Canvas
* canvas
) override
;
29 gfx::Insets
GetInsets() const override
;
30 gfx::Size
GetMinimumSize() const override
;
33 scoped_ptr
<views::Painter
> painter_
;
36 DISALLOW_COPY_AND_ASSIGN(SupervisedUserAvatarLabelBorder
);
39 SupervisedUserAvatarLabelBorder::SupervisedUserAvatarLabelBorder(
40 bool label_on_right
) {
41 const int kHorizontalInsetRight
= label_on_right
? 43 : 10;
42 const int kHorizontalInsetLeft
= label_on_right
? 10 : 43;
43 const int kVerticalInsetTop
= 2;
44 const int kVerticalInsetBottom
= 3;
45 // We want to align with the top of the tab. This works if the default font
46 // size is 13. If it is smaller, we need to increase the TopInset accordingly.
47 const int difference
= std::max
<int>(0, 13 - gfx::FontList().GetFontSize());
48 const int addToTop
= difference
/ 2;
49 const int addToBottom
= difference
- addToTop
;
50 insets_
= gfx::Insets(kVerticalInsetTop
+ addToTop
,
52 kVerticalInsetBottom
+ addToBottom
,
53 kHorizontalInsetRight
);
54 const int kImages
[] = IMAGE_GRID(IDR_SUPERVISED_USER_LABEL
);
55 painter_
.reset(views::Painter::CreateImageGridPainter(kImages
));
58 void SupervisedUserAvatarLabelBorder::Paint(
59 const views::View
& view
, gfx::Canvas
* canvas
) {
60 // Paint the default background using the image assets provided by UI. This
61 // includes a border with almost transparent white color.
62 painter_
->Paint(canvas
, view
.size());
64 // Repaint the inner part of the background in order to be able to change
65 // the colors according to the currently installed theme.
66 gfx::Rect
rect(1, 1, view
.size().width() - 2, view
.size().height() - 2);
69 SkColor background_color
= view
.GetThemeProvider()->GetColor(
70 ThemeProperties::COLOR_SUPERVISED_USER_LABEL_BACKGROUND
);
71 paint
.setStyle(SkPaint::kFill_Style
);
73 // Paint the inner border with a color slightly darker than the background.
74 SkAlpha kAlphaForBlending
= 230;
75 paint
.setColor(color_utils::AlphaBlend(
76 background_color
, SK_ColorBLACK
, kAlphaForBlending
));
77 canvas
->DrawRoundRect(rect
, kRadius
, paint
);
79 // Paint the inner background using the color provided by the ThemeProvider.
80 paint
.setColor(background_color
);
81 rect
= gfx::Rect(2, 2, view
.size().width() - 4, view
.size().height() - 4);
82 canvas
->DrawRoundRect(rect
, kRadius
, paint
);
85 gfx::Insets
SupervisedUserAvatarLabelBorder::GetInsets() const {
89 gfx::Size
SupervisedUserAvatarLabelBorder::GetMinimumSize() const {
91 size
.SetToMax(painter_
->GetMinimumSize());
97 SupervisedUserAvatarLabel::SupervisedUserAvatarLabel(BrowserView
* browser_view
)
99 l10n_util::GetStringUTF16(IDS_SUPERVISED_USER_AVATAR_LABEL
)),
100 browser_view_(browser_view
) {
101 SetLabelOnRight(false);
105 SupervisedUserAvatarLabel::~SupervisedUserAvatarLabel() {}
107 bool SupervisedUserAvatarLabel::OnMousePressed(const ui::MouseEvent
& event
) {
108 if (!LabelButton::OnMousePressed(event
))
111 browser_view_
->ShowAvatarBubbleFromAvatarButton(
112 BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT
,
113 signin::ManageAccountsParams());
117 void SupervisedUserAvatarLabel::UpdateLabelStyle() {
118 // |browser_view_| can be NULL in unit tests.
122 SkColor color_label
= browser_view_
->frame()->GetThemeProvider()->GetColor(
123 ThemeProperties::COLOR_SUPERVISED_USER_LABEL
);
124 for (size_t state
= 0; state
< STATE_COUNT
; ++state
)
125 SetTextColor(static_cast<ButtonState
>(state
), color_label
);
129 void SupervisedUserAvatarLabel::SetLabelOnRight(bool label_on_right
) {
130 SetBorder(scoped_ptr
<views::Border
>(
131 new SupervisedUserAvatarLabelBorder(label_on_right
)));