OLD | NEW |
(Empty) | |
| 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/new_avatar_button.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/profiles/profile_metrics.h" |
| 9 #include "chrome/browser/themes/theme_properties.h" |
| 10 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 11 #include "chrome/browser/ui/views/profile_chooser_view.h" |
| 12 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/base/theme_provider.h" |
| 17 #include "ui/gfx/canvas.h" |
| 18 #include "ui/gfx/color_utils.h" |
| 19 #include "ui/views/border.h" |
| 20 #include "ui/views/painter.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int kInset = 10; |
| 25 |
| 26 |
| 27 // AvatarGlassButtonBorder ---------------------------------------------------- |
| 28 class AvatarGlassButtonBorder : public views::TextButtonDefaultBorder { |
| 29 public: |
| 30 AvatarGlassButtonBorder() { |
| 31 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 32 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 33 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 34 |
| 35 SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); |
| 36 set_normal_painter(views::Painter::CreateImageGridPainter(kNormalImageSet)); |
| 37 set_hot_painter(views::Painter::CreateImageGridPainter(kHotImageSet)); |
| 38 set_pushed_painter(views::Painter::CreateImageGridPainter(kPushedImageSet)); |
| 39 }; |
| 40 |
| 41 private: |
| 42 virtual ~AvatarGlassButtonBorder() { |
| 43 }; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(AvatarGlassButtonBorder); |
| 46 }; |
| 47 |
| 48 |
| 49 // AvatarThemedButtonBorder --------------------------------------------------- |
| 50 |
| 51 class AvatarThemedButtonBorder : public views::TextButtonDefaultBorder { |
| 52 public: |
| 53 AvatarThemedButtonBorder() { |
| 54 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| 55 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| 56 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| 57 |
| 58 SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); |
| 59 set_normal_painter(views::Painter::CreateImageGridPainter(kNormalImageSet)); |
| 60 set_hot_painter(views::Painter::CreateImageGridPainter(kHotImageSet)); |
| 61 set_pushed_painter(views::Painter::CreateImageGridPainter(kPushedImageSet)); |
| 62 }; |
| 63 |
| 64 private: |
| 65 virtual ~AvatarThemedButtonBorder() { |
| 66 }; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(AvatarThemedButtonBorder); |
| 69 }; |
| 70 |
| 71 } // namespace |
| 72 |
| 73 |
| 74 // NewAvatarButton ------------------------------------------------------------ |
| 75 NewAvatarButton::NewAvatarButton( |
| 76 BrowserView* browser_view, |
| 77 const string16& profile_name) |
| 78 : MenuButton(NULL, string16(), NULL, true), |
| 79 browser_view_(browser_view), |
| 80 avatar_bubble_align_right_(true) { |
| 81 SetText(profile_name); |
| 82 set_animate_on_state_change(false); |
| 83 |
| 84 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 85 SetFont(rb->GetFont(ui::ResourceBundle::BaseFont)); |
| 86 |
| 87 if (browser_view->frame()->ShouldUseNativeFrame()) { |
| 88 set_border(new AvatarGlassButtonBorder); |
| 89 set_menu_marker( |
| 90 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 91 } else { |
| 92 set_border(new AvatarThemedButtonBorder); |
| 93 set_menu_marker( |
| 94 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); |
| 95 } |
| 96 SchedulePaint(); |
| 97 } |
| 98 |
| 99 NewAvatarButton::~NewAvatarButton() { |
| 100 } |
| 101 |
| 102 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { |
| 103 // From TextButton::PaintButton, draw everything but the text. |
| 104 OnPaintBackground(canvas); |
| 105 OnPaintBorder(canvas); |
| 106 OnPaintFocusBorder(canvas); |
| 107 |
| 108 gfx::Rect rect; |
| 109 // In RTL languages the marker gets drawn leftmost, so account for its offset. |
| 110 if (base::i18n::IsRTL()) |
| 111 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); |
| 112 else |
| 113 rect = gfx::Rect(kInset, 0, size().width(), size().height()); |
| 114 // TODO(noms): This should be DrawStringRectWithHalo but that function |
| 115 // has a bug at the moment and incorrectly draws the background. |
| 116 canvas->DrawStringRectWithFlags( |
| 117 text(), |
| 118 gfx::FontList(ui::ResourceBundle::GetSharedInstance().GetFont( |
| 119 ui::ResourceBundle::BaseFont)), |
| 120 SK_ColorBLACK, |
| 121 rect, |
| 122 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 123 |
| 124 // From MenuButton::PaintButton, paint the marker |
| 125 PaintMenuMarker(canvas); |
| 126 } |
| 127 |
| 128 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { |
| 129 if (!TextButton::OnMousePressed(event)) |
| 130 return false; |
| 131 ShowAvatarBubble(); |
| 132 return true; |
| 133 } |
| 134 |
| 135 void NewAvatarButton::ShowAvatarBubble() { |
| 136 gfx::Point origin; |
| 137 views::View::ConvertPointToScreen(this, &origin); |
| 138 gfx::Rect bounds(origin, size()); |
| 139 |
| 140 views::BubbleBorder::Arrow arrow_side = avatar_bubble_align_right_ ? |
| 141 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::TOP_LEFT; |
| 142 |
| 143 ProfileChooserView::ShowBubble( |
| 144 this, arrow_side, |
| 145 views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, bounds, |
| 146 browser_view_->browser()); |
| 147 |
| 148 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE); |
| 149 } |
OLD | NEW |