Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1640)

Side by Side Diff: chrome/browser/ui/views/profiles/new_avatar_button.cc

Issue 598163002: [Win] Redesign the new avatar button. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move static var declaration Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 13 matching lines...) Expand all
24 const int pushed_image_set[]) { 24 const int pushed_image_set[]) {
25 scoped_ptr<views::LabelButtonBorder> border( 25 scoped_ptr<views::LabelButtonBorder> border(
26 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); 26 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON));
27 border->SetPainter(false, views::Button::STATE_NORMAL, 27 border->SetPainter(false, views::Button::STATE_NORMAL,
28 views::Painter::CreateImageGridPainter(normal_image_set)); 28 views::Painter::CreateImageGridPainter(normal_image_set));
29 border->SetPainter(false, views::Button::STATE_HOVERED, 29 border->SetPainter(false, views::Button::STATE_HOVERED,
30 views::Painter::CreateImageGridPainter(hot_image_set)); 30 views::Painter::CreateImageGridPainter(hot_image_set));
31 border->SetPainter(false, views::Button::STATE_PRESSED, 31 border->SetPainter(false, views::Button::STATE_PRESSED,
32 views::Painter::CreateImageGridPainter(pushed_image_set)); 32 views::Painter::CreateImageGridPainter(pushed_image_set));
33 33
34 const int kLeftRightInset = 10; 34 const int kLeftRightInset = 8;
35 const int kTopInset = 0; 35 const int kTopInset = 2;
36 const int kBottomInset = 4; 36 const int kBottomInset = 4;
37 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, 37 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset,
38 kBottomInset, kLeftRightInset)); 38 kBottomInset, kLeftRightInset));
39 39
40 return border.PassAs<views::Border>(); 40 return border.PassAs<views::Border>();
41 } 41 }
42 42
43 } // namespace 43 } // namespace
44 44
45 NewAvatarButton::NewAvatarButton( 45 NewAvatarButton::NewAvatarButton(views::ButtonListener* listener,
46 views::ButtonListener* listener, 46 AvatarButtonStyle button_style,
47 const base::string16& profile_name, 47 Browser* browser)
48 AvatarButtonStyle button_style, 48 : LabelButton(listener, base::string16()),
49 Browser* browser)
50 : MenuButton(listener,
51 profiles::GetAvatarButtonTextForProfile(browser->profile()),
52 NULL,
53 true),
54 browser_(browser), 49 browser_(browser),
50 has_auth_error_(false),
55 suppress_mouse_released_action_(false) { 51 suppress_mouse_released_action_(false) {
56 set_animate_on_state_change(false); 52 set_animate_on_state_change(false);
57 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); 53 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE);
58 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); 54 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE);
59 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); 55 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE);
60 SetTextShadows(gfx::ShadowValues(10, 56 SetTextShadows(gfx::ShadowValues(10,
61 gfx::ShadowValue(gfx::Point(), 1.0f, SK_ColorDKGRAY))); 57 gfx::ShadowValue(gfx::Point(), 1.0f, SK_ColorDKGRAY)));
62 SetTextSubpixelRenderingEnabled(false); 58 SetTextSubpixelRenderingEnabled(false);
59 SetHorizontalAlignment(gfx::ALIGN_CENTER);
63 60
64 // The largest text height that fits in the button. If the font list height 61 // The largest text height that fits in the button. If the font list height
65 // is larger than this, it will be shrunk to match it. 62 // is larger than this, it will be shrunk to match it.
66 // TODO(noms): Calculate this constant algorithmically. 63 // TODO(noms): Calculate this constant algorithmically.
67 const int kDisplayFontHeight = 15; 64 const int kDisplayFontHeight = 15;
68 SetFontList(GetFontList().DeriveWithHeightUpperBound(kDisplayFontHeight)); 65 SetFontList(GetFontList().DeriveWithHeightUpperBound(kDisplayFontHeight));
69 66
70 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); 67 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
71 if (button_style == THEMED_BUTTON) { 68 if (button_style == THEMED_BUTTON) {
72 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); 69 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL);
73 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); 70 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER);
74 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); 71 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED);
75 72
76 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); 73 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
77 set_menu_marker( 74 generic_avatar_ =
78 rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_DROPARROW).ToImageSkia()); 75 *rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_AVATAR).ToImageSkia();
79 #if defined(OS_WIN) 76 #if defined(OS_WIN)
80 } else if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 77 } else if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
81 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL); 78 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_NORMAL);
82 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER); 79 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_HOVER);
83 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED); 80 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_METRO_BUTTON_PRESSED);
84 81
85 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); 82 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
86 set_menu_marker( 83 generic_avatar_ =
87 rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_DROPARROW).ToImageSkia()); 84 *rb->GetImageNamed(IDR_AVATAR_METRO_BUTTON_AVATAR).ToImageSkia();
88 #endif 85 #endif
89 } else { 86 } else {
90 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); 87 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL);
91 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); 88 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER);
92 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); 89 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED);
93 90
94 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); 91 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet));
95 set_menu_marker( 92 generic_avatar_ =
96 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); 93 *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia();
97 } 94 }
98 95
99 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); 96 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this);
100 97
101 // Subscribe to authentication error changes so that the avatar button can 98 // Subscribe to authentication error changes so that the avatar button can
102 // update itself. Note that guest mode profiles won't have a token service. 99 // update itself. Note that guest mode profiles won't have a token service.
103 SigninErrorController* error = 100 SigninErrorController* error =
104 profiles::GetSigninErrorController(browser_->profile()); 101 profiles::GetSigninErrorController(browser_->profile());
105 if (error) { 102 if (error) {
106 error->AddObserver(this); 103 error->AddObserver(this);
104 // This calls UpdateAvatarButtonAndRelayoutParent().
107 OnErrorChanged(); 105 OnErrorChanged();
106 } else {
107 UpdateAvatarButtonAndRelayoutParent();
108 } 108 }
109
110 SchedulePaint(); 109 SchedulePaint();
111 } 110 }
112 111
113 NewAvatarButton::~NewAvatarButton() { 112 NewAvatarButton::~NewAvatarButton() {
114 g_browser_process->profile_manager()-> 113 g_browser_process->profile_manager()->
115 GetProfileInfoCache().RemoveObserver(this); 114 GetProfileInfoCache().RemoveObserver(this);
116 SigninErrorController* error = 115 SigninErrorController* error =
117 profiles::GetSigninErrorController(browser_->profile()); 116 profiles::GetSigninErrorController(browser_->profile());
118 if (error) 117 if (error)
119 error->RemoveObserver(this); 118 error->RemoveObserver(this);
120 } 119 }
121 120
122 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { 121 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) {
123 // Prevent the bubble from being re-shown if it's already showing. 122 // Prevent the bubble from being re-shown if it's already showing.
124 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); 123 suppress_mouse_released_action_ = ProfileChooserView::IsShowing();
125 return MenuButton::OnMousePressed(event); 124 return LabelButton::OnMousePressed(event);
126 } 125 }
127 126
128 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { 127 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) {
129 if (suppress_mouse_released_action_) 128 if (suppress_mouse_released_action_)
130 suppress_mouse_released_action_ = false; 129 suppress_mouse_released_action_ = false;
131 else 130 else
132 MenuButton::OnMouseReleased(event); 131 LabelButton::OnMouseReleased(event);
133 } 132 }
134 133
135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { 134 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) {
136 UpdateAvatarButtonAndRelayoutParent(); 135 UpdateAvatarButtonAndRelayoutParent();
137 } 136 }
138 137
139 void NewAvatarButton::OnProfileWasRemoved( 138 void NewAvatarButton::OnProfileWasRemoved(
140 const base::FilePath& profile_path, 139 const base::FilePath& profile_path,
141 const base::string16& profile_name) { 140 const base::string16& profile_name) {
142 UpdateAvatarButtonAndRelayoutParent(); 141 UpdateAvatarButtonAndRelayoutParent();
143 } 142 }
144 143
145 void NewAvatarButton::OnProfileNameChanged( 144 void NewAvatarButton::OnProfileNameChanged(
146 const base::FilePath& profile_path, 145 const base::FilePath& profile_path,
147 const base::string16& old_profile_name) { 146 const base::string16& old_profile_name) {
148 UpdateAvatarButtonAndRelayoutParent(); 147 UpdateAvatarButtonAndRelayoutParent();
149 } 148 }
150 149
151 void NewAvatarButton::OnProfileAvatarChanged( 150 void NewAvatarButton::OnProfileAvatarChanged(
152 const base::FilePath& profile_path) { 151 const base::FilePath& profile_path) {
153 UpdateAvatarButtonAndRelayoutParent(); 152 UpdateAvatarButtonAndRelayoutParent();
154 } 153 }
155 154
156 void NewAvatarButton::OnProfileSupervisedUserIdChanged( 155 void NewAvatarButton::OnProfileSupervisedUserIdChanged(
157 const base::FilePath& profile_path) { 156 const base::FilePath& profile_path) {
158 UpdateAvatarButtonAndRelayoutParent(); 157 UpdateAvatarButtonAndRelayoutParent();
159 } 158 }
160 159
161 void NewAvatarButton::OnErrorChanged() { 160 void NewAvatarButton::OnErrorChanged() {
162 gfx::ImageSkia icon;
163
164 // If there is an error, show an warning icon. 161 // If there is an error, show an warning icon.
165 const SigninErrorController* error = 162 const SigninErrorController* error =
166 profiles::GetSigninErrorController(browser_->profile()); 163 profiles::GetSigninErrorController(browser_->profile());
167 if (error && error->HasError()) { 164 has_auth_error_ = error && error->HasError();
168 icon = *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
169 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia();
170 }
171 165
172 SetImage(views::Button::STATE_NORMAL, icon);
173 UpdateAvatarButtonAndRelayoutParent(); 166 UpdateAvatarButtonAndRelayoutParent();
174 } 167 }
175 168
176 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { 169 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() {
170 const ProfileInfoCache& cache =
171 g_browser_process->profile_manager()->GetProfileInfoCache();
172
173 // If we have a single local profile, then use the generic avatar
174 // button instead of the profile name. Never use the generic button if
175 // the active profile is Guest.
176 bool use_generic_button = (!browser_->profile()->IsGuestSession() &&
177 cache.GetNumberOfProfiles() == 1 &&
178 cache.GetUserNameOfProfileAtIndex(0).empty());
179
180 SetText(use_generic_button ? base::string16() :
181 profiles::GetAvatarButtonTextForProfile(browser_->profile()));
177 // We want the button to resize if the new text is shorter. 182 // We want the button to resize if the new text is shorter.
178 SetText(profiles::GetAvatarButtonTextForProfile(browser_->profile()));
179 SetMinSize(gfx::Size()); 183 SetMinSize(gfx::Size());
184
185 if (use_generic_button) {
186 SetImage(views::Button::STATE_NORMAL, generic_avatar_);
187 } else if (has_auth_error_) {
188 SetImage(views::Button::STATE_NORMAL,
189 *ui::ResourceBundle::GetSharedInstance().GetImageNamed(
190 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia());
191 } else {
192 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia());
193 }
194
195 // If we are not using the generic button, then reset the spacing between
196 // the text and the possible authentication error icon.
197 const int kDefaultImageTextSpacing = 5;
198 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing);
199
180 InvalidateLayout(); 200 InvalidateLayout();
181 201
182 // Because the width of the button might have changed, the parent browser 202 // Because the width of the button might have changed, the parent browser
183 // frame needs to recalculate the button bounds and redraw it. 203 // frame needs to recalculate the button bounds and redraw it.
184 if (parent()) 204 if (parent())
185 parent()->Layout(); 205 parent()->Layout();
186 } 206 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/profiles/new_avatar_button.h ('k') | chrome/browser/ui/views/tabs/tab_strip.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698