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/image/image_skia_operations.h" | |
21 #include "ui/gfx/text_elider.h" | 22 #include "ui/gfx/text_elider.h" |
22 #include "ui/views/border.h" | 23 #include "ui/views/border.h" |
23 #include "ui/views/painter.h" | 24 #include "ui/views/painter.h" |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Text padding within the button border. | 28 // Text padding within the button border. |
28 const int kInset = 10; | 29 const int kInset = 10; |
30 const int kBottomInset = 3; | |
msw
2014/05/30 18:24:13
My biggest concern for this CL is that it's regres
noms (inactive)
2014/06/09 20:34:19
Done.
| |
31 // Target size of the warning icon. | |
32 const int kAuthErrorIconWidth = 13; | |
33 const int kAuthErrorIconHeight = 11; | |
29 | 34 |
30 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | 35 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], |
31 const int hot_image_set[], | 36 const int hot_image_set[], |
32 const int pushed_image_set[]) { | 37 const int pushed_image_set[], |
38 bool is_themed_window) { | |
33 scoped_ptr<views::TextButtonDefaultBorder> border( | 39 scoped_ptr<views::TextButtonDefaultBorder> border( |
34 new views::TextButtonDefaultBorder()); | 40 new views::TextButtonDefaultBorder()); |
35 | 41 int bottom_inset = is_themed_window ? kBottomInset : 0; |
36 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); | 42 border->SetInsets(gfx::Insets(0, kInset, bottom_inset, kInset)); |
37 border->set_normal_painter( | 43 border->set_normal_painter( |
38 views::Painter::CreateImageGridPainter(normal_image_set)); | 44 views::Painter::CreateImageGridPainter(normal_image_set)); |
39 border->set_hot_painter( | 45 border->set_hot_painter( |
40 views::Painter::CreateImageGridPainter(hot_image_set)); | 46 views::Painter::CreateImageGridPainter(hot_image_set)); |
41 border->set_pushed_painter( | 47 border->set_pushed_painter( |
42 views::Painter::CreateImageGridPainter(pushed_image_set)); | 48 views::Painter::CreateImageGridPainter(pushed_image_set)); |
43 | 49 |
44 return border.PassAs<views::Border>(); | 50 return border.PassAs<views::Border>(); |
45 } | 51 } |
46 | 52 |
(...skipping 26 matching lines...) Expand all Loading... | |
73 bool is_win8 = false; | 79 bool is_win8 = false; |
74 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
75 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | 81 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; |
76 #endif | 82 #endif |
77 | 83 |
78 if (button_style == THEMED_BUTTON) { | 84 if (button_style == THEMED_BUTTON) { |
79 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 85 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
80 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); | 86 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
81 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); | 87 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
82 | 88 |
83 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 89 SetBorder( |
90 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, true)); | |
84 set_menu_marker( | 91 set_menu_marker( |
85 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); | 92 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); |
86 } else if (is_win8) { | 93 } else if (is_win8) { |
87 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); | 94 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); |
88 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); | 95 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); |
89 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); | 96 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); |
90 | 97 |
91 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 98 SetBorder( |
99 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, false)); | |
92 set_menu_marker( | 100 set_menu_marker( |
93 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); | 101 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); |
94 } else { | 102 } else { |
95 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 103 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
96 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 104 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
97 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 105 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
98 | 106 |
99 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 107 SetBorder( |
108 CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet, false)); | |
100 set_menu_marker( | 109 set_menu_marker( |
101 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 110 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
102 } | 111 } |
103 | 112 |
104 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); | 113 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
105 | 114 |
106 // Subscribe to authentication error changes so that the avatar button | 115 // Subscribe to authentication error changes so that the avatar button |
107 // can update itself. Note that guest mode profiles won't have a token | 116 // can update itself. Note that guest mode profiles won't have a token |
108 // service. | 117 // service. |
109 SigninErrorController* error = | 118 SigninErrorController* error = |
(...skipping 11 matching lines...) Expand all Loading... | |
121 GetProfileInfoCache().RemoveObserver(this); | 130 GetProfileInfoCache().RemoveObserver(this); |
122 SigninErrorController* error = | 131 SigninErrorController* error = |
123 profiles::GetSigninErrorController(browser_->profile()); | 132 profiles::GetSigninErrorController(browser_->profile()); |
124 if (error) | 133 if (error) |
125 error->RemoveObserver(this); | 134 error->RemoveObserver(this); |
126 } | 135 } |
127 | 136 |
128 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { | 137 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
129 // Get text bounds, and then adjust for the top and RTL languages. | 138 // Get text bounds, and then adjust for the top and RTL languages. |
130 gfx::Rect rect = GetTextBounds(); | 139 gfx::Rect rect = GetTextBounds(); |
131 rect.Offset(0, -rect.y()); | 140 |
132 if (rect.width() > 0) | 141 if (rect.width() > 0) |
133 rect.set_x(GetMirroredXForRect(rect)); | 142 rect.set_x(GetMirroredXForRect(rect)); |
134 | 143 |
135 canvas->DrawStringRectWithHalo( | 144 canvas->DrawStringRectWithHalo( |
136 text(), | 145 text(), |
137 gfx::FontList(), | 146 gfx::FontList(), |
138 SK_ColorWHITE, | 147 SK_ColorWHITE, |
139 SK_ColorDKGRAY, | 148 SK_ColorDKGRAY, |
140 rect, | 149 rect, |
141 gfx::Canvas::NO_SUBPIXEL_RENDERING); | 150 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
(...skipping 16 matching lines...) Expand all Loading... | |
158 } | 167 } |
159 | 168 |
160 void NewAvatarButton::OnErrorChanged() { | 169 void NewAvatarButton::OnErrorChanged() { |
161 gfx::ImageSkia icon; | 170 gfx::ImageSkia icon; |
162 | 171 |
163 // If there is an error, show an warning icon. | 172 // If there is an error, show an warning icon. |
164 const SigninErrorController* error = | 173 const SigninErrorController* error = |
165 profiles::GetSigninErrorController(browser_->profile()); | 174 profiles::GetSigninErrorController(browser_->profile()); |
166 if (error && error->HasError()) { | 175 if (error && error->HasError()) { |
167 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 176 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
168 icon = *rb->GetImageNamed(IDR_WARNING).ToImageSkia(); | 177 icon = gfx::ImageSkiaOperations::CreateResizedImage( |
178 *rb->GetImageNamed(IDR_ICON_PROFILES_ACCOUNT_AUTH_ERROR).ToImageSkia(), | |
179 skia::ImageOperations::RESIZE_BEST, | |
180 gfx::Size(kAuthErrorIconWidth, kAuthErrorIconHeight)); | |
169 } | 181 } |
170 | 182 |
171 SetIcon(icon); | 183 SetIcon(icon); |
172 UpdateAvatarButtonAndRelayoutParent(); | 184 UpdateAvatarButtonAndRelayoutParent(); |
173 } | 185 } |
174 | 186 |
175 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 187 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
176 // We want the button to resize if the new text is shorter. | 188 // We want the button to resize if the new text is shorter. |
177 SetText(GetElidedText( | 189 SetText(GetElidedText( |
178 profiles::GetAvatarNameForProfile(browser_->profile()))); | 190 profiles::GetAvatarNameForProfile(browser_->profile()))); |
179 ClearMaxTextSize(); | 191 ClearMaxTextSize(); |
180 | 192 |
181 // Because the width of the button might have changed, the parent browser | 193 // Because the width of the button might have changed, the parent browser |
182 // frame needs to recalculate the button bounds and redraw it. | 194 // frame needs to recalculate the button bounds and redraw it. |
183 if (parent()) | 195 if (parent()) |
184 parent()->Layout(); | 196 parent()->Layout(); |
185 } | 197 } |
OLD | NEW |