avatar_label.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/profiles/avatar_label.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/signin/signin_header_helper.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/themes/theme_properties.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/frame/browser_view.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/grit/generated_resources.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/theme_resources.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/theme_provider.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/canvas.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/color_utils.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/border.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/painter.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A custom border for the supervised user avatar label. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AvatarLabelBorder : public views::Border { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch explicit AvatarLabelBorder(bool label_on_right); 26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch 27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // views::Border: 28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; 29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch virtual gfx::Insets GetInsets() const OVERRIDE; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual gfx::Size GetMinimumSize() const OVERRIDE; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<views::Painter> painter_; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::Insets insets_; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder); 37}; 38 39AvatarLabelBorder::AvatarLabelBorder(bool label_on_right) { 40 const int kHorizontalInsetRight = label_on_right ? 43 : 10; 41 const int kHorizontalInsetLeft = label_on_right ? 10 : 43; 42 const int kVerticalInsetTop = 2; 43 const int kVerticalInsetBottom = 3; 44 // We want to align with the top of the tab. This works if the default font 45 // size is 13. If it is smaller, we need to increase the TopInset accordingly. 46 const int difference = std::max<int>(0, 13 - gfx::FontList().GetFontSize()); 47 const int addToTop = difference / 2; 48 const int addToBottom = difference - addToTop; 49 insets_ = gfx::Insets(kVerticalInsetTop + addToTop, 50 kHorizontalInsetLeft, 51 kVerticalInsetBottom + addToBottom, 52 kHorizontalInsetRight); 53 const int kImages[] = IMAGE_GRID(IDR_SUPERVISED_USER_LABEL); 54 painter_.reset(views::Painter::CreateImageGridPainter(kImages)); 55} 56 57void AvatarLabelBorder::Paint(const views::View& view, gfx::Canvas* canvas) { 58 // Paint the default background using the image assets provided by UI. This 59 // includes a border with almost transparent white color. 60 painter_->Paint(canvas, view.size()); 61 62 // Repaint the inner part of the background in order to be able to change 63 // the colors according to the currently installed theme. 64 gfx::Rect rect(1, 1, view.size().width() - 2, view.size().height() - 2); 65 SkPaint paint; 66 int kRadius = 2; 67 SkColor background_color = view.GetThemeProvider()->GetColor( 68 ThemeProperties::COLOR_SUPERVISED_USER_LABEL_BACKGROUND); 69 paint.setStyle(SkPaint::kFill_Style); 70 71 // Paint the inner border with a color slightly darker than the background. 72 SkAlpha kAlphaForBlending = 230; 73 paint.setColor(color_utils::AlphaBlend( 74 background_color, SK_ColorBLACK, kAlphaForBlending)); 75 canvas->DrawRoundRect(rect, kRadius, paint); 76 77 // Paint the inner background using the color provided by the ThemeProvider. 78 paint.setColor(background_color); 79 rect = gfx::Rect(2, 2, view.size().width() - 4, view.size().height() - 4); 80 canvas->DrawRoundRect(rect, kRadius, paint); 81} 82 83gfx::Insets AvatarLabelBorder::GetInsets() const { 84 return insets_; 85} 86 87gfx::Size AvatarLabelBorder::GetMinimumSize() const { 88 gfx::Size size(4, 4); 89 size.SetToMax(painter_->GetMinimumSize()); 90 return size; 91} 92 93} // namespace 94 95AvatarLabel::AvatarLabel(BrowserView* browser_view) 96 : LabelButton(NULL, 97 l10n_util::GetStringUTF16(IDS_SUPERVISED_USER_AVATAR_LABEL)), 98 browser_view_(browser_view) { 99 SetLabelOnRight(false); 100 UpdateLabelStyle(); 101} 102 103AvatarLabel::~AvatarLabel() {} 104 105bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) { 106 if (!LabelButton::OnMousePressed(event)) 107 return false; 108 109 browser_view_->ShowAvatarBubbleFromAvatarButton( 110 BrowserWindow::AVATAR_BUBBLE_MODE_DEFAULT, 111 signin::ManageAccountsParams()); 112 return true; 113} 114 115void AvatarLabel::UpdateLabelStyle() { 116 // |browser_view_| can be NULL in unit tests. 117 if (!browser_view_) 118 return; 119 120 SkColor color_label = browser_view_->frame()->GetThemeProvider()->GetColor( 121 ThemeProperties::COLOR_SUPERVISED_USER_LABEL); 122 for (size_t state = 0; state < STATE_COUNT; ++state) 123 SetTextColor(static_cast<ButtonState>(state), color_label); 124 SchedulePaint(); 125} 126 127void AvatarLabel::SetLabelOnRight(bool label_on_right) { 128 SetBorder(scoped_ptr<views::Border>(new AvatarLabelBorder(label_on_right))); 129} 130