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