Chromium Code Reviews| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 94 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 93 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 95 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 94 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 96 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 95 | 97 |
| 96 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 98 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
| 97 set_menu_marker( | 99 set_menu_marker( |
| 98 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 100 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 99 } | 101 } |
| 100 | 102 |
| 101 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | 103 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
| 104 | |
| 105 // Subscribe to authentication error changes so that the avatar button | |
| 106 // can update itself. | |
| 107 SigninErrorController* error = | |
| 108 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> | |
| 109 signin_error_controller(); | |
| 110 error->AddObserver(this); | |
| 111 OnErrorChanged(); | |
| 112 | |
| 102 SchedulePaint(); | 113 SchedulePaint(); |
| 103 } | 114 } |
| 104 | 115 |
| 105 NewAvatarButton::~NewAvatarButton() { | 116 NewAvatarButton::~NewAvatarButton() { |
| 106 g_browser_process->profile_manager()-> | 117 g_browser_process->profile_manager()-> |
| 107 GetProfileInfoCache().RemoveObserver(this); | 118 GetProfileInfoCache().RemoveObserver(this); |
| 119 SigninErrorController* error = | |
| 120 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> | |
| 121 signin_error_controller(); | |
| 122 error->RemoveObserver(this); | |
| 108 } | 123 } |
| 109 | 124 |
| 110 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { | 125 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 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; | 126 gfx::Rect rect; |
| 117 // In RTL languages the marker gets drawn leftmost, so account for its offset. | 127 // In RTL languages the marker gets drawn leftmost, so account for its offset. |
| 118 if (base::i18n::IsRTL()) | 128 if (base::i18n::IsRTL()) |
| 119 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); | 129 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); |
| 120 else | 130 else |
| 121 rect = gfx::Rect(kInset, 0, size().width(), size().height()); | 131 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, use a special background. | |
|
noms (inactive)
2014/05/11 19:21:25
nit: I think the comment is wrong. You're only cha
Roger Tawa OOO till Jul 10th
2014/05/13 21:23:54
Done.
| |
| 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 } | |
| 172 | |
| 151 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 173 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
| 152 // We want the button to resize if the new text is shorter. | 174 // We want the button to resize if the new text is shorter. |
| 153 ClearMaxTextSize(); | 175 ClearMaxTextSize(); |
| 154 SetText(GetElidedText( | 176 SetText(GetElidedText( |
| 155 profiles::GetAvatarNameForProfile(browser_->profile()))); | 177 profiles::GetAvatarNameForProfile(browser_->profile()))); |
| 156 | 178 |
| 157 // Because the width of the button might have changed, the parent browser | 179 // Because the width of the button might have changed, the parent browser |
| 158 // frame needs to recalculate the button bounds and redraw it. | 180 // frame needs to recalculate the button bounds and redraw it. |
| 159 parent()->Layout(); | 181 parent()->Layout(); |
| 160 } | 182 } |
| OLD | NEW |