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/avatar_button.h" | 5 #include "chrome/browser/ui/views/profiles/avatar_button.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "build/build_config.h" | |
10 #include "chrome/app/vector_icons/vector_icons.h" | 9 #include "chrome/app/vector_icons/vector_icons.h" |
11 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/profiles/profile_attributes_entry.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
14 #include "chrome/browser/profiles/profiles_state.h" | 12 #include "chrome/browser/profiles/profiles_state.h" |
15 #include "chrome/browser/ui/views/profiles/avatar_button_delegate.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
16 #include "chrome/browser/ui/views/profiles/profile_chooser_view.h" | 14 #include "chrome/browser/themes/theme_service.h" |
| 15 #include "chrome/browser/themes/theme_service_factory.h" |
17 #include "chrome/grit/theme_resources.h" | 16 #include "chrome/grit/theme_resources.h" |
18 #include "components/signin/core/common/profile_management_switches.h" | 17 #include "components/signin/core/browser/signin_manager.h" |
19 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
20 #include "ui/gfx/canvas.h" | |
21 #include "ui/gfx/color_palette.h" | 19 #include "ui/gfx/color_palette.h" |
22 #include "ui/gfx/geometry/vector2d.h" | |
23 #include "ui/gfx/paint_vector_icon.h" | 20 #include "ui/gfx/paint_vector_icon.h" |
24 #include "ui/vector_icons/vector_icons.h" | 21 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
25 #include "ui/views/border.h" | 22 #include "ui/views/animation/ink_drop_impl.h" |
26 #include "ui/views/controls/button/label_button_border.h" | 23 #include "ui/views/controls/button/label_button_border.h" |
27 #include "ui/views/painter.h" | |
28 | 24 |
29 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
30 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
| 27 #include "chrome/browser/ui/views/frame/minimize_button_metrics_win.h" |
31 #endif | 28 #endif |
32 | 29 |
33 namespace { | 30 namespace { |
34 | 31 |
35 std::unique_ptr<views::Border> CreateBorder(const int normal_image_set[], | 32 constexpr int kLeftRightInset = 8; |
36 const int hot_image_set[], | 33 constexpr int kTopInset = 2; |
37 const int pushed_image_set[]) { | 34 constexpr int kBottomInset = 4; |
| 35 |
| 36 // TODO(emx): Calculate width based on caption button [http://crbug.com/716365] |
| 37 constexpr int kMdButtonMinWidth = 46; |
| 38 |
| 39 std::unique_ptr<views::Border> CreateThemedBorder( |
| 40 const int normal_image_set[], |
| 41 const int hot_image_set[], |
| 42 const int pushed_image_set[]) { |
38 std::unique_ptr<views::LabelButtonAssetBorder> border( | 43 std::unique_ptr<views::LabelButtonAssetBorder> border( |
39 new views::LabelButtonAssetBorder(views::Button::STYLE_TEXTBUTTON)); | 44 new views::LabelButtonAssetBorder(views::Button::STYLE_TEXTBUTTON)); |
| 45 |
40 border->SetPainter(false, views::Button::STATE_NORMAL, | 46 border->SetPainter(false, views::Button::STATE_NORMAL, |
41 views::Painter::CreateImageGridPainter(normal_image_set)); | 47 views::Painter::CreateImageGridPainter(normal_image_set)); |
42 border->SetPainter(false, views::Button::STATE_HOVERED, | 48 border->SetPainter(false, views::Button::STATE_HOVERED, |
43 views::Painter::CreateImageGridPainter(hot_image_set)); | 49 views::Painter::CreateImageGridPainter(hot_image_set)); |
44 border->SetPainter(false, views::Button::STATE_PRESSED, | 50 border->SetPainter(false, views::Button::STATE_PRESSED, |
45 views::Painter::CreateImageGridPainter(pushed_image_set)); | 51 views::Painter::CreateImageGridPainter(pushed_image_set)); |
46 | 52 |
47 const int kLeftRightInset = 8; | 53 border->set_insets( |
48 const int kTopInset = 2; | 54 gfx::Insets(kTopInset, kLeftRightInset, kBottomInset, kLeftRightInset)); |
49 const int kBottomInset = 4; | |
50 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, | |
51 kBottomInset, kLeftRightInset)); | |
52 | 55 |
53 return std::move(border); | 56 return std::move(border); |
54 } | 57 } |
55 | 58 |
| 59 std::unique_ptr<views::Border> CreateWin10NativeBorder() { |
| 60 return views::CreateEmptyBorder(kTopInset, kLeftRightInset, kBottomInset, |
| 61 kLeftRightInset); |
| 62 } |
| 63 |
56 } // namespace | 64 } // namespace |
57 | 65 |
58 NewAvatarButton::NewAvatarButton(AvatarButtonDelegate* delegate, | 66 AvatarButton::AvatarButton(views::ButtonListener* listener, |
59 AvatarButtonStyle button_style, | 67 AvatarButtonStyle button_style, |
60 Profile* profile) | 68 Profile* profile) |
61 : LabelButton(delegate, base::string16()), | 69 : LabelButton(listener, base::string16()), |
62 delegate_(delegate), | |
63 error_controller_(this, profile), | 70 error_controller_(this, profile), |
64 profile_(profile), | 71 profile_(profile), |
65 suppress_mouse_released_action_(false) { | 72 profile_observer_(this), |
66 set_triggerable_event_flags( | 73 use_win10_native_button_(false) { |
67 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON); | 74 set_notify_action(CustomButton::NOTIFY_ON_PRESS); |
| 75 set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON | |
| 76 ui::EF_RIGHT_MOUSE_BUTTON); |
68 set_animate_on_state_change(false); | 77 set_animate_on_state_change(false); |
69 SetEnabledTextColors(SK_ColorWHITE); | 78 SetEnabledTextColors(SK_ColorWHITE); |
70 SetTextSubpixelRenderingEnabled(false); | 79 SetTextSubpixelRenderingEnabled(false); |
71 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 80 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
72 | 81 |
| 82 profile_observer_.Add( |
| 83 &g_browser_process->profile_manager()->GetProfileAttributesStorage()); |
| 84 |
73 // The largest text height that fits in the button. If the font list height | 85 // The largest text height that fits in the button. If the font list height |
74 // is larger than this, it will be shrunk to match it. | 86 // is larger than this, it will be shrunk to match it. |
75 // TODO(noms): Calculate this constant algorithmically from the button's size. | 87 // TODO(noms): Calculate this constant algorithmically from the button's size. |
76 const int kDisplayFontHeight = 16; | 88 const int kDisplayFontHeight = 16; |
77 SetFontList( | 89 SetFontList( |
78 label()->font_list().DeriveWithHeightUpperBound(kDisplayFontHeight)); | 90 label()->font_list().DeriveWithHeightUpperBound(kDisplayFontHeight)); |
79 | 91 |
80 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 92 #if defined(OS_WIN) |
81 if (button_style == AvatarButtonStyle::THEMED) { | 93 // TODO(estade): Use MD button in other cases, too [http://crbug.com/591586] |
82 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 94 if ((base::win::GetVersion() >= base::win::VERSION_WIN10) && |
83 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); | 95 ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme()) { |
84 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); | 96 DCHECK_EQ(AvatarButtonStyle::NATIVE, button_style); |
| 97 use_win10_native_button_ = true; |
| 98 } |
| 99 #endif // defined(OS_WIN) |
85 | 100 |
86 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 101 if (use_win10_native_button_) { |
87 generic_avatar_ = | 102 constexpr int kMdButtonIconHeight = 16; |
88 *rb->GetImageNamed(IDR_AVATAR_THEMED_BUTTON_AVATAR).ToImageSkia(); | 103 constexpr SkColor kMdButtonIconColor = |
| 104 SkColorSetA(SK_ColorBLACK, static_cast<SkAlpha>(0.54 * 0xFF)); |
| 105 generic_avatar_ = gfx::CreateVectorIcon( |
| 106 kAccountCircleIcon, kMdButtonIconHeight, kMdButtonIconColor); |
| 107 SetBorder(CreateWin10NativeBorder()); |
| 108 |
| 109 SetInkDropMode(InkDropMode::ON); |
| 110 set_has_ink_drop_action_on_click(true); |
| 111 SetFocusPainter(nullptr); |
| 112 set_ink_drop_base_color(SK_ColorBLACK); |
| 113 } else { |
| 114 if (button_style == AvatarButtonStyle::THEMED) { |
| 115 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
| 116 const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_HOVER); |
| 117 const int kPressedImageSet[] = |
| 118 IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_PRESSED); |
| 119 SetButtonAvatar(IDR_AVATAR_THEMED_BUTTON_AVATAR); |
| 120 SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
| 121 kPressedImageSet)); |
89 #if defined(OS_WIN) | 122 #if defined(OS_WIN) |
90 } else if (base::win::GetVersion() < base::win::VERSION_WIN8) { | 123 } else if (base::win::GetVersion() < base::win::VERSION_WIN8) { |
91 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 124 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
92 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 125 const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
93 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 126 const int kPressedImageSet[] = |
94 | 127 IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
95 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 128 SetButtonAvatar(IDR_AVATAR_GLASS_BUTTON_AVATAR); |
96 generic_avatar_ = | 129 SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
97 *rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_AVATAR).ToImageSkia(); | 130 kPressedImageSet)); |
98 #endif | 131 #endif |
99 } else { | 132 } else { |
100 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_NORMAL); | 133 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_NORMAL); |
101 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); | 134 const int kHoverImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_HOVER); |
102 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); | 135 const int kPressedImageSet[] = |
103 | 136 IMAGE_GRID(IDR_AVATAR_NATIVE_BUTTON_PRESSED); |
104 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 137 SetButtonAvatar(IDR_AVATAR_NATIVE_BUTTON_AVATAR); |
105 generic_avatar_ = | 138 SetBorder(CreateThemedBorder(kNormalImageSet, kHoverImageSet, |
106 *rb->GetImageNamed(IDR_AVATAR_NATIVE_BUTTON_AVATAR).ToImageSkia(); | 139 kPressedImageSet)); |
| 140 } |
107 } | 141 } |
108 | 142 |
109 g_browser_process->profile_manager()-> | |
110 GetProfileAttributesStorage().AddObserver(this); | |
111 Update(); | 143 Update(); |
112 SchedulePaint(); | 144 SchedulePaint(); |
113 } | 145 } |
114 | 146 |
115 NewAvatarButton::~NewAvatarButton() { | 147 AvatarButton::~AvatarButton() {} |
116 g_browser_process->profile_manager()-> | |
117 GetProfileAttributesStorage().RemoveObserver(this); | |
118 } | |
119 | 148 |
120 bool NewAvatarButton::OnMousePressed(const ui::MouseEvent& event) { | 149 void AvatarButton::OnGestureEvent(ui::GestureEvent* event) { |
121 // Prevent the bubble from being re-shown if it's already showing. | |
122 suppress_mouse_released_action_ = ProfileChooserView::IsShowing(); | |
123 return LabelButton::OnMousePressed(event); | |
124 } | |
125 | |
126 void NewAvatarButton::OnMouseReleased(const ui::MouseEvent& event) { | |
127 if (suppress_mouse_released_action_) | |
128 suppress_mouse_released_action_ = false; | |
129 else | |
130 LabelButton::OnMouseReleased(event); | |
131 } | |
132 | |
133 void NewAvatarButton::OnGestureEvent(ui::GestureEvent* event) { | |
134 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since | 150 // TODO(wjmaclean): The check for ET_GESTURE_LONG_PRESS is done here since |
135 // no other UI button based on CustomButton appears to handle mouse | 151 // no other UI button based on CustomButton appears to handle mouse |
136 // right-click. If other cases are identified, it may make sense to move this | 152 // right-click. If other cases are identified, it may make sense to move this |
137 // check to CustomButton. | 153 // check to CustomButton. |
138 if (event->type() == ui::ET_GESTURE_LONG_PRESS) | 154 if (event->type() == ui::ET_GESTURE_LONG_PRESS) |
139 NotifyClick(*event); | 155 NotifyClick(*event); |
140 else | 156 else |
141 LabelButton::OnGestureEvent(event); | 157 LabelButton::OnGestureEvent(event); |
142 } | 158 } |
143 | 159 |
144 void NewAvatarButton::OnAvatarErrorChanged() { | 160 gfx::Size AvatarButton::GetMinimumSize() const { |
| 161 if (use_win10_native_button_) { |
| 162 // Returns the size of the button when it is atop the tabstrip. Called by |
| 163 // GlassBrowserFrameView::LayoutProfileSwitcher(). |
| 164 // TODO(emx): Calculate the height based on the top of the new tab button. |
| 165 return gfx::Size(kMdButtonMinWidth, 20); |
| 166 } |
| 167 |
| 168 return LabelButton::GetMinimumSize(); |
| 169 } |
| 170 |
| 171 gfx::Size AvatarButton::GetPreferredSize() const { |
| 172 gfx::Size size = LabelButton::GetPreferredSize(); |
| 173 |
| 174 if (use_win10_native_button_) { |
| 175 #if defined(OS_WIN) |
| 176 // Returns the normal size of the button (when it does not overlap the |
| 177 // tabstrip). Its height should match the caption button height. |
| 178 // The minimum width is the caption button width and the maximum is fixed |
| 179 // as per the spec in http://crbug.com/635699. |
| 180 // TODO(emx): Should this be calculated based on average character width? |
| 181 constexpr int kMdButtonMaxWidth = 98; |
| 182 size.set_width( |
| 183 std::min(std::max(size.width(), kMdButtonMinWidth), kMdButtonMaxWidth)); |
| 184 size.set_height(MinimizeButtonMetrics::GetCaptionButtonHeightInDIPs()); |
| 185 #endif |
| 186 } else { |
| 187 size.set_height(20); |
| 188 } |
| 189 |
| 190 return size; |
| 191 } |
| 192 |
| 193 std::unique_ptr<views::InkDropHighlight> AvatarButton::CreateInkDropHighlight() |
| 194 const { |
| 195 auto center = gfx::RectF(GetLocalBounds()).CenterPoint(); |
| 196 auto ink_drop_highlight = base::MakeUnique<views::InkDropHighlight>( |
| 197 size(), 0, center, GetInkDropBaseColor()); |
| 198 constexpr float kInkDropHighlightOpacity = 0.08f; |
| 199 ink_drop_highlight->set_visible_opacity(kInkDropHighlightOpacity); |
| 200 return ink_drop_highlight; |
| 201 } |
| 202 |
| 203 bool AvatarButton::ShouldUseFloodFillInkDrop() const { |
| 204 return true; |
| 205 } |
| 206 |
| 207 void AvatarButton::OnAvatarErrorChanged() { |
145 Update(); | 208 Update(); |
146 } | 209 } |
147 | 210 |
148 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | 211 void AvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
149 Update(); | 212 Update(); |
150 } | 213 } |
151 | 214 |
152 void NewAvatarButton::OnProfileWasRemoved( | 215 void AvatarButton::OnProfileWasRemoved(const base::FilePath& profile_path, |
153 const base::FilePath& profile_path, | 216 const base::string16& profile_name) { |
154 const base::string16& profile_name) { | |
155 // If deleting the active profile, don't bother updating the avatar | 217 // If deleting the active profile, don't bother updating the avatar |
156 // button, as the browser window is being closed anyway. | 218 // button, as the browser window is being closed anyway. |
157 if (profile_->GetPath() != profile_path) | 219 if (profile_->GetPath() != profile_path) |
158 Update(); | 220 Update(); |
159 } | 221 } |
160 | 222 |
161 void NewAvatarButton::OnProfileNameChanged( | 223 void AvatarButton::OnProfileNameChanged( |
162 const base::FilePath& profile_path, | 224 const base::FilePath& profile_path, |
163 const base::string16& old_profile_name) { | 225 const base::string16& old_profile_name) { |
164 if (profile_->GetPath() == profile_path) | 226 if (profile_->GetPath() == profile_path) |
165 Update(); | 227 Update(); |
166 } | 228 } |
167 | 229 |
168 void NewAvatarButton::OnProfileSupervisedUserIdChanged( | 230 void AvatarButton::OnProfileSupervisedUserIdChanged( |
169 const base::FilePath& profile_path) { | 231 const base::FilePath& profile_path) { |
170 if (profile_->GetPath() == profile_path) | 232 if (profile_->GetPath() == profile_path) |
171 Update(); | 233 Update(); |
172 } | 234 } |
173 | 235 |
174 void NewAvatarButton::Update() { | 236 void AvatarButton::Update() { |
175 ProfileAttributesStorage& storage = | 237 ProfileAttributesStorage& storage = |
176 g_browser_process->profile_manager()->GetProfileAttributesStorage(); | 238 g_browser_process->profile_manager()->GetProfileAttributesStorage(); |
177 | 239 |
178 // If we have a single local profile, then use the generic avatar | 240 // If we have a single local profile, then use the generic avatar |
179 // button instead of the profile name. Never use the generic button if | 241 // button instead of the profile name. Never use the generic button if |
180 // the active profile is Guest. | 242 // the active profile is Guest. |
181 const bool use_generic_button = | 243 const bool use_generic_button = |
182 !profile_->IsGuestSession() && | 244 !profile_->IsGuestSession() && storage.GetNumberOfProfiles() == 1 && |
183 storage.GetNumberOfProfiles() == 1 && | 245 !SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated(); |
184 !storage.GetAllProfilesAttributes().front()->IsAuthenticated(); | |
185 | 246 |
186 SetText(use_generic_button | 247 SetText(use_generic_button |
187 ? base::string16() | 248 ? base::string16() |
188 : profiles::GetAvatarButtonTextForProfile(profile_)); | 249 : profiles::GetAvatarButtonTextForProfile(profile_)); |
189 | 250 |
190 // If the button has no text, clear the text shadows to make sure the | 251 // If the button has no text, clear the text shadows to make sure the |
191 // image is centered correctly. | 252 // image is centered correctly. |
192 SetTextShadows( | 253 SetTextShadows( |
193 use_generic_button | 254 use_generic_button |
194 ? gfx::ShadowValues() | 255 ? gfx::ShadowValues() |
(...skipping 11 matching lines...) Expand all Loading... |
206 } else { | 267 } else { |
207 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); | 268 SetImage(views::Button::STATE_NORMAL, gfx::ImageSkia()); |
208 } | 269 } |
209 | 270 |
210 // If we are not using the generic button, then reset the spacing between | 271 // If we are not using the generic button, then reset the spacing between |
211 // the text and the possible authentication error icon. | 272 // the text and the possible authentication error icon. |
212 const int kDefaultImageTextSpacing = 5; | 273 const int kDefaultImageTextSpacing = 5; |
213 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); | 274 SetImageLabelSpacing(use_generic_button ? 0 : kDefaultImageTextSpacing); |
214 | 275 |
215 PreferredSizeChanged(); | 276 PreferredSizeChanged(); |
216 delegate_->ButtonPreferredSizeChanged(); | |
217 } | 277 } |
| 278 |
| 279 void AvatarButton::SetButtonAvatar(int avatar_idr) { |
| 280 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 281 generic_avatar_ = *rb->GetImageNamed(avatar_idr).ToImageSkia(); |
| 282 } |
OLD | NEW |