| 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/win/windows_version.h" | 7 #include "base/win/windows_version.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/profiles/profiles_state.h" | 10 #include "chrome/browser/profiles/profiles_state.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 12 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
| 18 #include "ui/gfx/font_list.h" | 20 #include "ui/gfx/font_list.h" |
| 19 #include "ui/gfx/text_elider.h" | 21 #include "ui/gfx/text_elider.h" |
| 20 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
| 21 #include "ui/views/painter.h" | 23 #include "ui/views/painter.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } // namespace | 59 } // namespace |
| 58 | 60 |
| 59 NewAvatarButton::NewAvatarButton( | 61 NewAvatarButton::NewAvatarButton( |
| 60 views::ButtonListener* listener, | 62 views::ButtonListener* listener, |
| 61 const base::string16& profile_name, | 63 const base::string16& profile_name, |
| 62 AvatarButtonStyle button_style, | 64 AvatarButtonStyle button_style, |
| 63 Browser* browser) | 65 Browser* browser) |
| 64 : MenuButton(listener, GetElidedText(profile_name), NULL, true), | 66 : MenuButton(listener, GetElidedText(profile_name), NULL, true), |
| 65 browser_(browser) { | 67 browser_(browser) { |
| 66 set_animate_on_state_change(false); | 68 set_animate_on_state_change(false); |
| 69 set_icon_placement(ICON_ON_RIGHT); |
| 67 | 70 |
| 68 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 71 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 69 | 72 |
| 70 bool is_win8 = false; | 73 bool is_win8 = false; |
| 71 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
| 72 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | 75 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 73 #endif | 76 #endif |
| 74 | 77 |
| 75 if (button_style == THEMED_BUTTON) { | 78 if (button_style == THEMED_BUTTON) { |
| 76 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 79 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 95 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 93 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 96 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 94 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 97 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 95 | 98 |
| 96 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 99 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 97 set_menu_marker( | 100 set_menu_marker( |
| 98 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 101 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 99 } | 102 } |
| 100 | 103 |
| 101 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | 104 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
| 105 |
| 106 // Subscribe to authentication error changes so that the avatar button |
| 107 // can update itself. |
| 108 SigninErrorController* error = |
| 109 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 110 signin_error_controller(); |
| 111 error->AddObserver(this); |
| 112 OnErrorChanged(); |
| 113 |
| 102 SchedulePaint(); | 114 SchedulePaint(); |
| 103 } | 115 } |
| 104 | 116 |
| 105 NewAvatarButton::~NewAvatarButton() { | 117 NewAvatarButton::~NewAvatarButton() { |
| 106 g_browser_process->profile_manager()-> | 118 g_browser_process->profile_manager()-> |
| 107 GetProfileInfoCache().RemoveObserver(this); | 119 GetProfileInfoCache().RemoveObserver(this); |
| 120 SigninErrorController* error = |
| 121 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 122 signin_error_controller(); |
| 123 error->RemoveObserver(this); |
| 108 } | 124 } |
| 109 | 125 |
| 110 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { | 126 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 111 // From TextButton::PaintButton, draw everything but the text. | 127 // Get text bounds, and then adjust for the top and RTL languages. |
| 112 OnPaintBackground(canvas); | 128 gfx::Rect rect = GetTextBounds(); |
| 113 OnPaintBorder(canvas); | 129 rect.Offset(0, -rect.y()); |
| 114 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); | 130 if (rect.width() > 0) |
| 115 | 131 rect.set_x(GetMirroredXForRect(rect)); |
| 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 | 132 |
| 123 canvas->DrawStringRectWithHalo( | 133 canvas->DrawStringRectWithHalo( |
| 124 text(), | 134 text(), |
| 125 gfx::FontList(), | 135 gfx::FontList(), |
| 126 SK_ColorWHITE, | 136 SK_ColorWHITE, |
| 127 SK_ColorDKGRAY, | 137 SK_ColorDKGRAY, |
| 128 rect, | 138 rect, |
| 129 gfx::Canvas::NO_SUBPIXEL_RENDERING); | 139 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 130 | |
| 131 // From MenuButton::PaintButton, paint the marker | |
| 132 PaintMenuMarker(canvas); | |
| 133 } | 140 } |
| 134 | 141 |
| 135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | 142 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 136 UpdateAvatarButtonAndRelayoutParent(); | 143 UpdateAvatarButtonAndRelayoutParent(); |
| 137 } | 144 } |
| 138 | 145 |
| 139 void NewAvatarButton::OnProfileWasRemoved( | 146 void NewAvatarButton::OnProfileWasRemoved( |
| 140 const base::FilePath& profile_path, | 147 const base::FilePath& profile_path, |
| 141 const base::string16& profile_name) { | 148 const base::string16& profile_name) { |
| 142 UpdateAvatarButtonAndRelayoutParent(); | 149 UpdateAvatarButtonAndRelayoutParent(); |
| 143 } | 150 } |
| 144 | 151 |
| 145 void NewAvatarButton::OnProfileNameChanged( | 152 void NewAvatarButton::OnProfileNameChanged( |
| 146 const base::FilePath& profile_path, | 153 const base::FilePath& profile_path, |
| 147 const base::string16& old_profile_name) { | 154 const base::string16& old_profile_name) { |
| 148 UpdateAvatarButtonAndRelayoutParent(); | 155 UpdateAvatarButtonAndRelayoutParent(); |
| 149 } | 156 } |
| 150 | 157 |
| 158 void NewAvatarButton::OnErrorChanged() { |
| 159 gfx::ImageSkia icon; |
| 160 |
| 161 // If there is an error, show an warning icon. |
| 162 SigninErrorController* error = |
| 163 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 164 signin_error_controller(); |
| 165 if (error->HasError()) { |
| 166 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 167 icon = *rb->GetImageNamed(IDR_WARNING).ToImageSkia(); |
| 168 } |
| 169 |
| 170 SetIcon(icon); |
| 171 UpdateAvatarButtonAndRelayoutParent(); |
| 172 } |
| 173 |
| 151 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 174 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
| 152 // We want the button to resize if the new text is shorter. | 175 // We want the button to resize if the new text is shorter. |
| 153 ClearMaxTextSize(); | |
| 154 SetText(GetElidedText( | 176 SetText(GetElidedText( |
| 155 profiles::GetAvatarNameForProfile(browser_->profile()))); | 177 profiles::GetAvatarNameForProfile(browser_->profile()))); |
| 178 ClearMaxTextSize(); |
| 156 | 179 |
| 157 // Because the width of the button might have changed, the parent browser | 180 // Because the width of the button might have changed, the parent browser |
| 158 // frame needs to recalculate the button bounds and redraw it. | 181 // frame needs to recalculate the button bounds and redraw it. |
| 159 parent()->Layout(); | 182 if (parent()) |
| 183 parent()->Layout(); |
| 160 } | 184 } |
| OLD | NEW |