| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" | 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/win/windows_version.h" | 8 #include "base/win/windows_version.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/profiles/profiles_state.h" | 11 #include "chrome/browser/profiles/profiles_state.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 14 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/color_utils.h" | 20 #include "ui/gfx/color_utils.h" |
| 21 #include "ui/gfx/font_list.h" | 21 #include "ui/gfx/font_list.h" |
| 22 #include "ui/gfx/text_constants.h" | 22 #include "ui/gfx/text_constants.h" |
| 23 #include "ui/gfx/text_elider.h" | 23 #include "ui/gfx/text_elider.h" |
| 24 #include "ui/views/border.h" | 24 #include "ui/views/border.h" |
| 25 #include "ui/views/controls/button/label_button_border.h" | 25 #include "ui/views/controls/button/label_button_border.h" |
| 26 #include "ui/views/painter.h" | 26 #include "ui/views/painter.h" |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Text padding within the button border. | |
| 31 const int kLeftRightInset = 7; | |
| 32 const int kTopBottomInset = 2; | |
| 33 | |
| 34 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | 30 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], |
| 35 const int hot_image_set[], | 31 const int hot_image_set[], |
| 36 const int pushed_image_set[]) { | 32 const int pushed_image_set[]) { |
| 37 scoped_ptr<views::LabelButtonBorder> border( | 33 scoped_ptr<views::LabelButtonBorder> border( |
| 38 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); | 34 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); |
| 39 | 35 |
| 40 border->SetPainter(false, views::Button::STATE_NORMAL, | 36 border->SetPainter(false, views::Button::STATE_NORMAL, |
| 41 views::Painter::CreateImageGridPainter(normal_image_set)); | 37 views::Painter::CreateImageGridPainter(normal_image_set)); |
| 42 border->SetPainter(false, views::Button::STATE_HOVERED, | 38 border->SetPainter(false, views::Button::STATE_HOVERED, |
| 43 views::Painter::CreateImageGridPainter(hot_image_set)); | 39 views::Painter::CreateImageGridPainter(hot_image_set)); |
| 44 border->SetPainter(false, views::Button::STATE_PRESSED, | 40 border->SetPainter(false, views::Button::STATE_PRESSED, |
| 45 views::Painter::CreateImageGridPainter(pushed_image_set)); | 41 views::Painter::CreateImageGridPainter(pushed_image_set)); |
| 46 | 42 |
| 47 border->set_insets(gfx::Insets(kTopBottomInset, kLeftRightInset, | 43 const int kLeftRightInset = 10; |
| 48 kTopBottomInset, kLeftRightInset)); | 44 const int kTopInset = 0; |
| 45 const int kBottomInset = 4; |
| 46 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, |
| 47 kBottomInset, kLeftRightInset)); |
| 49 | 48 |
| 50 return border.PassAs<views::Border>(); | 49 return border.PassAs<views::Border>(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 base::string16 GetElidedText(const base::string16& original_text) { | 52 base::string16 GetElidedText(const base::string16& original_text) { |
| 54 // Maximum characters the button can be before the text will get elided. | 53 // Maximum characters the button can be before the text will get elided. |
| 55 const int kMaxCharactersToDisplay = 15; | 54 const int kMaxCharactersToDisplay = 15; |
| 56 const gfx::FontList font_list; | 55 const gfx::FontList font_list; |
| 57 return gfx::ElideText(original_text, font_list, | 56 return gfx::ElideText(original_text, font_list, |
| 58 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), | 57 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 views::ButtonListener* listener, | 72 views::ButtonListener* listener, |
| 74 const base::string16& profile_name, | 73 const base::string16& profile_name, |
| 75 AvatarButtonStyle button_style, | 74 AvatarButtonStyle button_style, |
| 76 Browser* browser) | 75 Browser* browser) |
| 77 : MenuButton(listener, GetButtonText(browser->profile()), NULL, true), | 76 : MenuButton(listener, GetButtonText(browser->profile()), NULL, true), |
| 78 browser_(browser) { | 77 browser_(browser) { |
| 79 set_animate_on_state_change(false); | 78 set_animate_on_state_change(false); |
| 80 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); | 79 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); |
| 81 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); | 80 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); |
| 82 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); | 81 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); |
| 83 SetHaloColor(SK_ColorDKGRAY); | 82 SetTextShadows(gfx::ShadowValues(10, |
| 84 SetHorizontalAlignment(gfx::ALIGN_RIGHT); | 83 gfx::ShadowValue(gfx::Point(), 1.0f, SK_ColorDKGRAY))); |
| 84 SetTextSubpixelRenderingEnabled(false); |
| 85 | 85 |
| 86 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 86 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 87 | |
| 88 bool is_win8 = false; | |
| 89 #if defined(OS_WIN) | |
| 90 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | |
| 91 #endif | |
| 92 | |
| 93 if (button_style == THEMED_BUTTON) { | 87 if (button_style == THEMED_BUTTON) { |
| 94 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 88 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| 95 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); | 89 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| 96 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); | 90 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| 97 | 91 |
| 98 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 92 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 99 set_menu_marker( | 93 set_menu_marker( |
| 100 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); | 94 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); |
| 101 } else if (is_win8) { | 95 #if defined(OS_WIN) |
| 96 } else if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| 102 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); | 97 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); |
| 103 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); | 98 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); |
| 104 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); | 99 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); |
| 105 | 100 |
| 106 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 101 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 107 set_menu_marker( | 102 set_menu_marker( |
| 108 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); | 103 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); |
| 104 #endif |
| 109 } else { | 105 } else { |
| 110 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 106 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 111 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 107 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 112 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 108 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 113 | 109 |
| 114 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 110 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 115 set_menu_marker( | 111 set_menu_marker( |
| 116 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 112 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 117 } | 113 } |
| 118 | 114 |
| 119 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | 115 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
| 120 | 116 |
| 121 // Subscribe to authentication error changes so that the avatar button | 117 // Subscribe to authentication error changes so that the avatar button can |
| 122 // can update itself. Note that guest mode profiles won't have a token | 118 // update itself. Note that guest mode profiles won't have a token service. |
| 123 // service. | |
| 124 SigninErrorController* error = | 119 SigninErrorController* error = |
| 125 profiles::GetSigninErrorController(browser_->profile()); | 120 profiles::GetSigninErrorController(browser_->profile()); |
| 126 if (error) { | 121 if (error) { |
| 127 error->AddObserver(this); | 122 error->AddObserver(this); |
| 128 OnErrorChanged(); | 123 OnErrorChanged(); |
| 129 } | 124 } |
| 130 | 125 |
| 131 SchedulePaint(); | 126 SchedulePaint(); |
| 132 } | 127 } |
| 133 | 128 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // We want the button to resize if the new text is shorter. | 170 // We want the button to resize if the new text is shorter. |
| 176 SetText(GetButtonText(browser_->profile())); | 171 SetText(GetButtonText(browser_->profile())); |
| 177 set_min_size(gfx::Size()); | 172 set_min_size(gfx::Size()); |
| 178 InvalidateLayout(); | 173 InvalidateLayout(); |
| 179 | 174 |
| 180 // Because the width of the button might have changed, the parent browser | 175 // Because the width of the button might have changed, the parent browser |
| 181 // frame needs to recalculate the button bounds and redraw it. | 176 // frame needs to recalculate the button bounds and redraw it. |
| 182 if (parent()) | 177 if (parent()) |
| 183 parent()->Layout(); | 178 parent()->Layout(); |
| 184 } | 179 } |
| OLD | NEW |