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/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
| 20 #include "ui/gfx/font_list.h" | 20 #include "ui/gfx/font_list.h" |
| 21 #include "ui/gfx/text_elider.h" | 21 #include "ui/gfx/text_elider.h" |
| 22 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
| 23 #include "ui/views/painter.h" | 23 #include "ui/views/painter.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Text padding within the button border. | 27 // Text padding within the button border. |
| 28 const int kInset = 10; | 28 const int kInset = 10; |
| 29 const int kBottomInset = 3; | |
|
msw
2014/05/28 19:34:58
I'm not sure this change does the right thing. In
noms (inactive)
2014/05/28 19:59:27
The misalignment of the drop-down arrow and the sh
noms (inactive)
2014/05/30 17:43:34
Actually, I'd rather fix them in a separate CL. Th
| |
| 29 | 30 |
| 30 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | 31 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], |
| 31 const int hot_image_set[], | 32 const int hot_image_set[], |
| 32 const int pushed_image_set[]) { | 33 const int pushed_image_set[], |
| 34 bool is_themed_window) { | |
| 33 scoped_ptr<views::TextButtonDefaultBorder> border( | 35 scoped_ptr<views::TextButtonDefaultBorder> border( |
| 34 new views::TextButtonDefaultBorder()); | 36 new views::TextButtonDefaultBorder()); |
| 35 | 37 int bottom_inset = is_themed_window ? kBottomInset : 0; |
| 36 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); | 38 border->SetInsets(gfx::Insets(0, kInset, bottom_inset, kInset)); |
| 37 border->set_normal_painter( | 39 border->set_normal_painter( |
| 38 views::Painter::CreateImageGridPainter(normal_image_set)); | 40 views::Painter::CreateImageGridPainter(normal_image_set)); |
| 39 border->set_hot_painter( | 41 border->set_hot_painter( |
| 40 views::Painter::CreateImageGridPainter(hot_image_set)); | 42 views::Painter::CreateImageGridPainter(hot_image_set)); |
| 41 border->set_pushed_painter( | 43 border->set_pushed_painter( |
| 42 views::Painter::CreateImageGridPainter(pushed_image_set)); | 44 views::Painter::CreateImageGridPainter(pushed_image_set)); |
| 43 | 45 |
| 44 return border.PassAs<views::Border>(); | 46 return border.PassAs<views::Border>(); |
| 45 } | 47 } |
| 46 | 48 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 73 bool is_win8 = false; | 75 bool is_win8 = false; |
| 74 #if defined(OS_WIN) | 76 #if defined(OS_WIN) |
| 75 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | 77 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 76 #endif | 78 #endif |
| 77 | 79 |
| 78 if (button_style == THEMED_BUTTON) { | 80 if (button_style == THEMED_BUTTON) { |
| 79 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 81 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| 80 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); | 82 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| 81 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); | 83 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| 82 | 84 |
| 83 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 85 SetBorder( |
| 86 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, true)); | |
| 84 set_menu_marker( | 87 set_menu_marker( |
| 85 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); | 88 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); |
| 86 } else if (is_win8) { | 89 } else if (is_win8) { |
| 87 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); | 90 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); |
| 88 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); | 91 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); |
| 89 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); | 92 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); |
| 90 | 93 |
| 91 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 94 SetBorder( |
| 95 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, false)); | |
| 92 set_menu_marker( | 96 set_menu_marker( |
| 93 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); | 97 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); |
| 94 } else { | 98 } else { |
| 95 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 99 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
| 96 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 100 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
| 97 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 101 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
| 98 | 102 |
| 99 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 103 SetBorder( |
| 104 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, false)); | |
| 100 set_menu_marker( | 105 set_menu_marker( |
| 101 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 106 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
| 102 } | 107 } |
| 103 | 108 |
| 104 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | 109 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
| 105 | 110 |
| 106 // Subscribe to authentication error changes so that the avatar button | 111 // Subscribe to authentication error changes so that the avatar button |
| 107 // can update itself. | 112 // can update itself. |
| 108 SigninErrorController* error = | 113 SigninErrorController* error = |
| 109 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> | 114 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 110 signin_error_controller(); | 115 signin_error_controller(); |
| 111 error->AddObserver(this); | 116 error->AddObserver(this); |
| 112 OnErrorChanged(); | 117 OnErrorChanged(); |
| 113 | 118 |
| 114 SchedulePaint(); | 119 SchedulePaint(); |
| 115 } | 120 } |
| 116 | 121 |
| 117 NewAvatarButton::~NewAvatarButton() { | 122 NewAvatarButton::~NewAvatarButton() { |
| 118 g_browser_process->profile_manager()-> | 123 g_browser_process->profile_manager()-> |
| 119 GetProfileInfoCache().RemoveObserver(this); | 124 GetProfileInfoCache().RemoveObserver(this); |
| 120 SigninErrorController* error = | 125 SigninErrorController* error = |
| 121 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> | 126 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 122 signin_error_controller(); | 127 signin_error_controller(); |
| 123 error->RemoveObserver(this); | 128 error->RemoveObserver(this); |
| 124 } | 129 } |
| 125 | 130 |
| 126 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { | 131 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 127 // Get text bounds, and then adjust for the top and RTL languages. | 132 // Get text bounds, and then adjust for the top and RTL languages. |
| 128 gfx::Rect rect = GetTextBounds(); | 133 gfx::Rect rect = GetTextBounds(); |
| 129 rect.Offset(0, -rect.y()); | 134 |
| 130 if (rect.width() > 0) | 135 if (rect.width() > 0) |
| 131 rect.set_x(GetMirroredXForRect(rect)); | 136 rect.set_x(GetMirroredXForRect(rect)); |
| 132 | 137 |
| 133 canvas->DrawStringRectWithHalo( | 138 canvas->DrawStringRectWithHalo( |
| 134 text(), | 139 text(), |
| 135 gfx::FontList(), | 140 gfx::FontList(), |
| 136 SK_ColorWHITE, | 141 SK_ColorWHITE, |
| 137 SK_ColorDKGRAY, | 142 SK_ColorDKGRAY, |
| 138 rect, | 143 rect, |
| 139 gfx::Canvas::NO_SUBPIXEL_RENDERING); | 144 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 157 | 162 |
| 158 void NewAvatarButton::OnErrorChanged() { | 163 void NewAvatarButton::OnErrorChanged() { |
| 159 gfx::ImageSkia icon; | 164 gfx::ImageSkia icon; |
| 160 | 165 |
| 161 // If there is an error, show an warning icon. | 166 // If there is an error, show an warning icon. |
| 162 SigninErrorController* error = | 167 SigninErrorController* error = |
| 163 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> | 168 ProfileOAuth2TokenServiceFactory::GetForProfile(browser_->profile())-> |
| 164 signin_error_controller(); | 169 signin_error_controller(); |
| 165 if (error->HasError()) { | 170 if (error->HasError()) { |
| 166 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 171 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 167 icon = *rb->GetImageNamed(IDR_WARNING).ToImageSkia(); | 172 icon = *rb->GetImageNamed( |
| 173 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia(); | |
|
msw
2014/05/28 19:34:58
nit: We have many similar warning icons:
update_fa
noms (inactive)
2014/05/28 19:59:27
Oh my. So I was just given the new avatar_button_a
msw
2014/05/28 20:56:27
If scaling down the larger icon looks okay (and is
noms (inactive)
2014/05/30 17:43:34
Done.
| |
| 168 } | 174 } |
| 169 | 175 |
| 170 SetIcon(icon); | 176 SetIcon(icon); |
| 171 UpdateAvatarButtonAndRelayoutParent(); | 177 UpdateAvatarButtonAndRelayoutParent(); |
| 172 } | 178 } |
| 173 | 179 |
| 174 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 180 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
| 175 // We want the button to resize if the new text is shorter. | 181 // We want the button to resize if the new text is shorter. |
| 176 SetText(GetElidedText( | 182 SetText(GetElidedText( |
| 177 profiles::GetAvatarNameForProfile(browser_->profile()))); | 183 profiles::GetAvatarNameForProfile(browser_->profile()))); |
| 178 ClearMaxTextSize(); | 184 ClearMaxTextSize(); |
| 179 | 185 |
| 180 // Because the width of the button might have changed, the parent browser | 186 // Because the width of the button might have changed, the parent browser |
| 181 // frame needs to recalculate the button bounds and redraw it. | 187 // frame needs to recalculate the button bounds and redraw it. |
| 182 if (parent()) | 188 if (parent()) |
| 183 parent()->Layout(); | 189 parent()->Layout(); |
| 184 } | 190 } |
| OLD | NEW |