Home | History | Annotate | Download | only in views
      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/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/resource/resource_bundle.h"
     15 #include "ui/base/theme_provider.h"
     16 #include "ui/events/event.h"
     17 #include "ui/gfx/canvas.h"
     18 #include "ui/gfx/color_utils.h"
     19 #include "ui/views/painter.h"
     20 
     21 namespace {
     22 
     23 // A special text button border for the managed user avatar label.
     24 class AvatarLabelBorder: public views::TextButtonBorder {
     25  public:
     26   explicit AvatarLabelBorder();
     27 
     28   // views::TextButtonBorder:
     29   virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE;
     30   virtual gfx::Size GetMinimumSize() const OVERRIDE;
     31 
     32  private:
     33   scoped_ptr<views::Painter> painter_;
     34 
     35   DISALLOW_COPY_AND_ASSIGN(AvatarLabelBorder);
     36 };
     37 
     38 AvatarLabelBorder::AvatarLabelBorder() {
     39   const int kHorizontalInsetRight = 10;
     40   const int kHorizontalInsetLeft = 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 BaseFont size
     44   // is 13. If it is smaller, we need to increase the TopInset accordingly.
     45   gfx::Font font = ui::ResourceBundle::GetSharedInstance().GetFont(
     46       ui::ResourceBundle::BaseFont);
     47   int difference = (font.GetFontSize() < 13) ? 13 - font.GetFontSize() : 0;
     48   int addToTop = difference / 2;
     49   int addToBottom = difference - addToTop;
     50   SetInsets(gfx::Insets(kVerticalInsetTop + addToTop,
     51                         kHorizontalInsetLeft,
     52                         kVerticalInsetBottom + addToBottom,
     53                         kHorizontalInsetRight));
     54   const int kImages[] = IMAGE_GRID(IDR_MANAGED_USER_LABEL);
     55   painter_.reset(views::Painter::CreateImageGridPainter(kImages));
     56 }
     57 
     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());
     62 
     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);
     66   SkPaint paint;
     67   int kRadius = 2;
     68   SkColor background_color = view.GetThemeProvider()->GetColor(
     69       ThemeProperties::COLOR_MANAGED_USER_LABEL_BACKGROUND);
     70   paint.setStyle(SkPaint::kFill_Style);
     71 
     72   // For the inner border, use a color which is slightly darker than the
     73   // background color.
     74   SkAlpha kAlphaForBlending = 230;
     75   paint.setColor(color_utils::AlphaBlend(
     76       background_color, SK_ColorBLACK, kAlphaForBlending));
     77   canvas->DrawRoundRect(rect, kRadius, paint);
     78 
     79   // Now paint the inner background using the color provided by the
     80   // ThemeProvider.
     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);
     84 }
     85 
     86 gfx::Size AvatarLabelBorder::GetMinimumSize() const {
     87   gfx::Size size(4, 4);
     88   size.SetToMax(painter_->GetMinimumSize());
     89   return size;
     90 }
     91 
     92 }  // namespace
     93 
     94 AvatarLabel::AvatarLabel(BrowserView* browser_view)
     95     : TextButton(NULL,
     96                  l10n_util::GetStringUTF16(IDS_MANAGED_USER_AVATAR_LABEL)),
     97       browser_view_(browser_view) {
     98   SetFont(ui::ResourceBundle::GetSharedInstance().GetFont(
     99       ui::ResourceBundle::BaseFont));
    100   ClearMaxTextSize();
    101   set_border(new AvatarLabelBorder);
    102   UpdateLabelStyle();
    103 }
    104 
    105 AvatarLabel::~AvatarLabel() {}
    106 
    107 bool AvatarLabel::OnMousePressed(const ui::MouseEvent& event) {
    108   if (!TextButton::OnMousePressed(event))
    109     return false;
    110 
    111   browser_view_->ShowAvatarBubbleFromAvatarButton();
    112   return true;
    113 }
    114 
    115 void AvatarLabel::UpdateLabelStyle() {
    116   SkColor color_label = browser_view_->frame()->GetThemeProvider()->GetColor(
    117       ThemeProperties::COLOR_MANAGED_USER_LABEL);
    118   SetEnabledColor(color_label);
    119   SetHighlightColor(color_label);
    120   SetHoverColor(color_label);
    121   SetDisabledColor(color_label);
    122   SchedulePaint();
    123 }
    124