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