| 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/win/windows_version.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/profiles/profile_manager.h" | |
| 10 #include "chrome/browser/profiles/profiles_state.h" | |
| 11 #include "chrome/browser/ui/browser.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/gfx/canvas.h" | |
| 17 #include "ui/gfx/color_utils.h" | |
| 18 #include "ui/gfx/font_list.h" | |
| 19 #include "ui/gfx/text_elider.h" | |
| 20 #include "ui/views/border.h" | |
| 21 #include "ui/views/painter.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Text padding within the button border. | |
| 26 const int kInset = 10; | |
| 27 | |
| 28 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | |
| 29 const int hot_image_set[], | |
| 30 const int pushed_image_set[]) { | |
| 31 scoped_ptr<views::TextButtonDefaultBorder> border( | |
| 32 new views::TextButtonDefaultBorder()); | |
| 33 | |
| 34 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); | |
| 35 border->set_normal_painter( | |
| 36 views::Painter::CreateImageGridPainter(normal_image_set)); | |
| 37 border->set_hot_painter( | |
| 38 views::Painter::CreateImageGridPainter(hot_image_set)); | |
| 39 border->set_pushed_painter( | |
| 40 views::Painter::CreateImageGridPainter(pushed_image_set)); | |
| 41 | |
| 42 return border.PassAs<views::Border>(); | |
| 43 } | |
| 44 | |
| 45 base::string16 GetElidedText(const base::string16& original_text) { | |
| 46 // Maximum characters the button can be before the text will get elided. | |
| 47 const int kMaxCharactersToDisplay = 15; | |
| 48 | |
| 49 const gfx::FontList font_list; | |
| 50 return gfx::ElideText( | |
| 51 original_text, | |
| 52 font_list, | |
| 53 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), | |
| 54 gfx::ELIDE_AT_END); | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 NewAvatarButton::NewAvatarButton( | |
| 60 views::ButtonListener* listener, | |
| 61 const base::string16& profile_name, | |
| 62 AvatarButtonStyle button_style, | |
| 63 Browser* browser) | |
| 64 : MenuButton(listener, GetElidedText(profile_name), NULL, true), | |
| 65 browser_(browser) { | |
| 66 set_animate_on_state_change(false); | |
| 67 | |
| 68 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | |
| 69 | |
| 70 bool is_win8 = false; | |
| 71 #if defined(OS_WIN) | |
| 72 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | |
| 73 #endif | |
| 74 | |
| 75 if (button_style == THEMED_BUTTON) { | |
| 76 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | |
| 77 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); | |
| 78 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); | |
| 79 | |
| 80 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | |
| 81 set_menu_marker( | |
| 82 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); | |
| 83 } else if (is_win8) { | |
| 84 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); | |
| 85 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); | |
| 86 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); | |
| 87 | |
| 88 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | |
| 89 set_menu_marker( | |
| 90 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); | |
| 91 } else { | |
| 92 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | |
| 93 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | |
| 94 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | |
| 95 | |
| 96 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | |
| 97 set_menu_marker( | |
| 98 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | |
| 99 } | |
| 100 | |
| 101 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | |
| 102 SchedulePaint(); | |
| 103 } | |
| 104 | |
| 105 NewAvatarButton::~NewAvatarButton() { | |
| 106 g_browser_process->profile_manager()-> | |
| 107 GetProfileInfoCache().RemoveObserver(this); | |
| 108 } | |
| 109 | |
| 110 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { | |
| 111 // From TextButton::PaintButton, draw everything but the text. | |
| 112 OnPaintBackground(canvas); | |
| 113 OnPaintBorder(canvas); | |
| 114 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); | |
| 115 | |
| 116 gfx::Rect rect; | |
| 117 // In RTL languages the marker gets drawn leftmost, so account for its offset. | |
| 118 if (base::i18n::IsRTL()) | |
| 119 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); | |
| 120 else | |
| 121 rect = gfx::Rect(kInset, 0, size().width(), size().height()); | |
| 122 | |
| 123 canvas->DrawStringRectWithHalo( | |
| 124 text(), | |
| 125 gfx::FontList(), | |
| 126 SK_ColorWHITE, | |
| 127 SK_ColorDKGRAY, | |
| 128 rect, | |
| 129 gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
| 130 | |
| 131 // From MenuButton::PaintButton, paint the marker | |
| 132 PaintMenuMarker(canvas); | |
| 133 } | |
| 134 | |
| 135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | |
| 136 UpdateAvatarButtonAndRelayoutParent(); | |
| 137 } | |
| 138 | |
| 139 void NewAvatarButton::OnProfileWasRemoved( | |
| 140 const base::FilePath& profile_path, | |
| 141 const base::string16& profile_name) { | |
| 142 UpdateAvatarButtonAndRelayoutParent(); | |
| 143 } | |
| 144 | |
| 145 void NewAvatarButton::OnProfileNameChanged( | |
| 146 const base::FilePath& profile_path, | |
| 147 const base::string16& old_profile_name) { | |
| 148 UpdateAvatarButtonAndRelayoutParent(); | |
| 149 } | |
| 150 | |
| 151 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | |
| 152 // We want the button to resize if the new text is shorter. | |
| 153 ClearMaxTextSize(); | |
| 154 SetText(GetElidedText( | |
| 155 profiles::GetAvatarNameForProfile(browser_->profile()))); | |
| 156 | |
| 157 // Because the width of the button might have changed, the parent browser | |
| 158 // frame needs to recalculate the button bounds and redraw it. | |
| 159 parent()->Layout(); | |
| 160 } | |
| OLD | NEW |