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

Side by Side Diff: ui/views/controls/button/label_button.cc

Issue 1569113002: MacViews: Style BUTTON_STYLE buttons using the "modern" UI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: respond to comments, desktop linux Created 4 years, 8 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/controls/button/label_button.h" 5 #include "ui/views/controls/button/label_button.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 12 matching lines...) Expand all
23 #include "ui/views/controls/button/label_button_border.h" 23 #include "ui/views/controls/button/label_button_border.h"
24 #include "ui/views/painter.h" 24 #include "ui/views/painter.h"
25 #include "ui/views/style/platform_style.h" 25 #include "ui/views/style/platform_style.h"
26 #include "ui/views/window/dialog_delegate.h" 26 #include "ui/views/window/dialog_delegate.h"
27 27
28 namespace { 28 namespace {
29 29
30 // The default spacing between the icon and text. 30 // The default spacing between the icon and text.
31 const int kSpacing = 5; 31 const int kSpacing = 5;
32 32
33 #if !(defined(OS_LINUX) && !defined(OS_CHROMEOS))
34 // Default text and shadow colors for STYLE_BUTTON.
35 const SkColor kStyleButtonTextColor = SK_ColorBLACK;
36 const SkColor kStyleButtonShadowColor = SK_ColorWHITE;
37 #endif
38
39 const gfx::FontList& GetDefaultNormalFontList() { 33 const gfx::FontList& GetDefaultNormalFontList() {
40 static base::LazyInstance<gfx::FontList>::Leaky font_list = 34 static base::LazyInstance<gfx::FontList>::Leaky font_list =
41 LAZY_INSTANCE_INITIALIZER; 35 LAZY_INSTANCE_INITIALIZER;
42 return font_list.Get(); 36 return font_list.Get();
43 } 37 }
44 38
45 const gfx::FontList& GetDefaultBoldFontList() { 39 const gfx::FontList& GetDefaultBoldFontList() {
46 static base::LazyInstance<gfx::FontList>::Leaky font_list = 40 static base::LazyInstance<gfx::FontList>::Leaky font_list =
47 LAZY_INSTANCE_INITIALIZER; 41 LAZY_INSTANCE_INITIALIZER;
48 if ((font_list.Get().GetFontStyle() & gfx::Font::BOLD) == 0) { 42 if ((font_list.Get().GetFontStyle() & gfx::Font::BOLD) == 0) {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 152 }
159 153
160 const gfx::FontList& LabelButton::GetFontList() const { 154 const gfx::FontList& LabelButton::GetFontList() const {
161 return label_->font_list(); 155 return label_->font_list();
162 } 156 }
163 157
164 void LabelButton::SetFontList(const gfx::FontList& font_list) { 158 void LabelButton::SetFontList(const gfx::FontList& font_list) {
165 cached_normal_font_list_ = font_list; 159 cached_normal_font_list_ = font_list;
166 cached_bold_font_list_ = font_list.DeriveWithStyle( 160 cached_bold_font_list_ = font_list.DeriveWithStyle(
167 font_list.GetFontStyle() | gfx::Font::BOLD); 161 font_list.GetFontStyle() | gfx::Font::BOLD);
168 label_->SetFontList(is_default_ ? 162 bool bold = PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_;
sky 2016/04/07 17:16:31 Seems like you can skip cached_bold_font_list_ ent
tapted 2016/04/08 08:35:33 gfx::FontList is itself just a scoped_refptr, and
sky 2016/04/08 16:24:22 I think you're right and am fine to nuke the cachi
169 cached_bold_font_list_ : cached_normal_font_list_); 163 label_->SetFontList(bold ? cached_bold_font_list_ : cached_normal_font_list_);
170 } 164 }
171 165
172 void LabelButton::SetElideBehavior(gfx::ElideBehavior elide_behavior) { 166 void LabelButton::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
173 label_->SetElideBehavior(elide_behavior); 167 label_->SetElideBehavior(elide_behavior);
174 } 168 }
175 169
176 void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) { 170 void LabelButton::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
177 DCHECK_NE(gfx::ALIGN_TO_HEAD, alignment); 171 DCHECK_NE(gfx::ALIGN_TO_HEAD, alignment);
178 horizontal_alignment_ = alignment; 172 horizontal_alignment_ = alignment;
179 InvalidateLayout(); 173 InvalidateLayout();
(...skipping 11 matching lines...) Expand all
191 185
192 void LabelButton::SetIsDefault(bool is_default) { 186 void LabelButton::SetIsDefault(bool is_default) {
193 DCHECK_EQ(STYLE_BUTTON, style_); 187 DCHECK_EQ(STYLE_BUTTON, style_);
194 if (is_default == is_default_) 188 if (is_default == is_default_)
195 return; 189 return;
196 190
197 is_default_ = is_default; 191 is_default_ = is_default;
198 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); 192 ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
199 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); 193 is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel);
200 194
201 label_->SetFontList( 195 bool bold = PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_;
202 is_default ? cached_bold_font_list_ : cached_normal_font_list_); 196 label_->SetFontList(bold ? cached_bold_font_list_ : cached_normal_font_list_);
203 InvalidateLayout(); 197 InvalidateLayout();
198 HighlightStateChanged();
204 } 199 }
205 200
206 void LabelButton::SetStyle(ButtonStyle style) { 201 void LabelButton::SetStyle(ButtonStyle style) {
207 // All callers currently pass STYLE_BUTTON, and should only call this once, to 202 // All callers currently pass STYLE_BUTTON, and should only call this once, to
208 // change from the default style. 203 // change from the default style.
209 DCHECK_EQ(style, STYLE_BUTTON); 204 DCHECK_EQ(style, STYLE_BUTTON);
210 DCHECK_EQ(style_, STYLE_TEXTBUTTON); 205 DCHECK_EQ(style_, STYLE_TEXTBUTTON);
211 DCHECK(!GetWidget()) << "Can't change button style after adding to a Widget."; 206 DCHECK(!GetWidget()) << "Can't change button style after adding to a Widget.";
212 207
213 style_ = style; 208 style_ = style;
214 209
215 SetFocusPainter(nullptr); 210 SetFocusPainter(nullptr);
216 SetHorizontalAlignment(gfx::ALIGN_CENTER); 211 SetHorizontalAlignment(gfx::ALIGN_CENTER);
217 SetFocusable(true); 212 SetFocusable(true);
218 SetMinSize(gfx::Size(70, 33)); 213 SetMinSize(gfx::Size(PlatformStyle::kMinLabelButtonWidth,
214 PlatformStyle::kMinLabelButtonHeight));
219 215
220 // Themed borders will be set once the button is added to a Widget, since that 216 // Themed borders will be set once the button is added to a Widget, since that
221 // provides the value of GetNativeTheme(). 217 // provides the value of GetNativeTheme().
222 } 218 }
223 219
224 void LabelButton::SetImageLabelSpacing(int spacing) { 220 void LabelButton::SetImageLabelSpacing(int spacing) {
225 if (spacing == image_label_spacing_) 221 if (spacing == image_label_spacing_)
226 return; 222 return;
227 image_label_spacing_ = spacing; 223 image_label_spacing_ = spacing;
228 ResetCachedPreferredSize(); 224 ResetCachedPreferredSize();
229 InvalidateLayout(); 225 InvalidateLayout();
230 } 226 }
231 227
232 void LabelButton::SetFocusPainter(scoped_ptr<Painter> focus_painter) { 228 void LabelButton::SetFocusPainter(scoped_ptr<Painter> focus_painter) {
233 focus_painter_ = std::move(focus_painter); 229 focus_painter_ = std::move(focus_painter);
234 } 230 }
235 231
236 gfx::Size LabelButton::GetPreferredSize() const { 232 gfx::Size LabelButton::GetPreferredSize() const {
237 if (cached_preferred_size_valid_) 233 if (cached_preferred_size_valid_)
238 return cached_preferred_size_; 234 return cached_preferred_size_;
239 235
240 // Use a temporary label copy for sizing to avoid calculation side-effects. 236 // Use a temporary label copy for sizing to avoid calculation side-effects.
241 Label label(GetText(), cached_normal_font_list_); 237 Label label(GetText(), cached_normal_font_list_);
242 label.SetShadows(label_->shadows()); 238 label.SetShadows(label_->shadows());
243 239
244 if (style() == STYLE_BUTTON) { 240 if (style_ == STYLE_BUTTON && PlatformStyle::kDefaultLabelButtonHasBoldFont) {
245 // Some text appears wider when rendered normally than when rendered bold. 241 // Some text appears wider when rendered normally than when rendered bold.
246 // Accommodate the widest, as buttons may show bold and shouldn't resize. 242 // Accommodate the widest, as buttons may show bold and shouldn't resize.
247 const int current_width = label.GetPreferredSize().width(); 243 const int current_width = label.GetPreferredSize().width();
248 label.SetFontList(cached_bold_font_list_); 244 label.SetFontList(cached_bold_font_list_);
249 if (label.GetPreferredSize().width() < current_width) 245 if (label.GetPreferredSize().width() < current_width)
250 label.SetFontList(cached_normal_font_list_); 246 label.SetFontList(cached_normal_font_list_);
251 } 247 }
252 248
253 // Calculate the required size. 249 // Calculate the required size.
254 const gfx::Size image_size(image_->GetPreferredSize()); 250 const gfx::Size image_size(image_->GetPreferredSize());
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 385
390 void LabelButton::OnBlur() { 386 void LabelButton::OnBlur() {
391 CustomButton::OnBlur(); 387 CustomButton::OnBlur();
392 // Typically the border renders differently when focused. 388 // Typically the border renders differently when focused.
393 SchedulePaint(); 389 SchedulePaint();
394 } 390 }
395 391
396 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { 392 void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
397 ResetColorsFromNativeTheme(); 393 ResetColorsFromNativeTheme();
398 UpdateThemedBorder(); 394 UpdateThemedBorder();
395 HighlightStateChanged();
399 // Invalidate the layout to pickup the new insets from the border. 396 // Invalidate the layout to pickup the new insets from the border.
400 InvalidateLayout(); 397 InvalidateLayout();
401 } 398 }
402 399
403 void LabelButton::AddInkDropLayer(ui::Layer* ink_drop_layer) { 400 void LabelButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
404 image()->SetPaintToLayer(true); 401 image()->SetPaintToLayer(true);
405 image()->layer()->SetFillsBoundsOpaquely(false); 402 image()->layer()->SetFillsBoundsOpaquely(false);
406 ink_drop_container_->SetVisible(true); 403 ink_drop_container_->SetVisible(true);
407 ink_drop_container_->layer()->Add(ink_drop_layer); 404 ink_drop_container_->layer()->Add(ink_drop_layer);
408 } 405 }
(...skipping 24 matching lines...) Expand all
433 gfx::Point LabelButton::GetInkDropCenter() const { 430 gfx::Point LabelButton::GetInkDropCenter() const {
434 // TODO(bruthig): Make the flood fill ink drops centered on the LocatedEvent 431 // TODO(bruthig): Make the flood fill ink drops centered on the LocatedEvent
435 // that triggered them. 432 // that triggered them.
436 return GetText().empty() ? image()->GetMirroredBounds().CenterPoint() 433 return GetText().empty() ? image()->GetMirroredBounds().CenterPoint()
437 : CustomButton::GetInkDropCenter(); 434 : CustomButton::GetInkDropCenter();
438 } 435 }
439 436
440 void LabelButton::StateChanged() { 437 void LabelButton::StateChanged() {
441 const gfx::Size previous_image_size(image_->GetPreferredSize()); 438 const gfx::Size previous_image_size(image_->GetPreferredSize());
442 UpdateImage(); 439 UpdateImage();
443 const SkColor color = button_state_colors_[state()]; 440 HighlightStateChanged();
444 if (state() != STATE_DISABLED && label_->enabled_color() != color)
445 label_->SetEnabledColor(color);
446 label_->SetEnabled(state() != STATE_DISABLED); 441 label_->SetEnabled(state() != STATE_DISABLED);
447 if (image_->GetPreferredSize() != previous_image_size) 442 if (image_->GetPreferredSize() != previous_image_size)
448 Layout(); 443 Layout();
449 } 444 }
450 445
451 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { 446 void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
452 params->button.checked = false; 447 params->button.checked = false;
453 params->button.indeterminate = false; 448 params->button.indeterminate = false;
454 params->button.is_default = is_default_; 449 params->button.is_default = is_default_;
455 params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); 450 params->button.is_focused = HasFocus() && IsAccessibilityFocusable();
456 params->button.has_border = false; 451 params->button.has_border = false;
457 params->button.classic_state = 0; 452 params->button.classic_state = 0;
458 params->button.background_color = label_->background_color(); 453 params->button.background_color = label_->background_color();
459 } 454 }
460 455
461 void LabelButton::ResetColorsFromNativeTheme() { 456 void LabelButton::ResetColorsFromNativeTheme() {
462 const ui::NativeTheme* theme = GetNativeTheme(); 457 const ui::NativeTheme* theme = GetNativeTheme();
463 SkColor colors[STATE_COUNT] = { 458 SkColor colors[STATE_COUNT] = {
464 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor), 459 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonEnabledColor),
465 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), 460 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor),
466 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor), 461 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonHoverColor),
467 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor), 462 theme->GetSystemColor(ui::NativeTheme::kColorId_ButtonDisabledColor),
468 }; 463 };
469 464
470 // Certain styles do not change text color when hovered or pressed.
471 bool constant_text_color = false;
472 // Use hardcoded colors for inverted color scheme support and STYLE_BUTTON. 465 // Use hardcoded colors for inverted color scheme support and STYLE_BUTTON.
473 if (color_utils::IsInvertedColorScheme()) { 466 if (color_utils::IsInvertedColorScheme()) {
474 constant_text_color = true;
475 colors[STATE_NORMAL] = SK_ColorWHITE; 467 colors[STATE_NORMAL] = SK_ColorWHITE;
468 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL];
sky 2016/04/07 17:16:31 nit: a single assignment setting all to WHITE is m
tapted 2016/04/08 08:35:33 Done.
476 label_->SetBackgroundColor(SK_ColorBLACK); 469 label_->SetBackgroundColor(SK_ColorBLACK);
477 label_->set_background(Background::CreateSolidBackground(SK_ColorBLACK)); 470 label_->set_background(Background::CreateSolidBackground(SK_ColorBLACK));
478 label_->SetAutoColorReadabilityEnabled(true); 471 label_->SetAutoColorReadabilityEnabled(true);
479 label_->SetShadows(gfx::ShadowValues()); 472 label_->SetShadows(gfx::ShadowValues());
480 } else if (style() == STYLE_BUTTON) { 473 } else if (style() == STYLE_BUTTON) {
481 // TODO(erg): This is disabled on desktop linux because of the binary asset 474 PlatformStyle::ApplyLabelButtonTextStyle(label_, &colors);
482 // confusion. These details should either be pushed into ui::NativeThemeWin 475 label_->set_background(nullptr);
483 // or should be obsoleted by rendering buttons with paint calls instead of
484 // with static assets. http://crbug.com/350498
485 #if !(defined(OS_LINUX) && !defined(OS_CHROMEOS))
486 constant_text_color = true;
487 colors[STATE_NORMAL] = kStyleButtonTextColor;
488 label_->SetBackgroundColor(theme->GetSystemColor(
489 ui::NativeTheme::kColorId_ButtonBackgroundColor));
490 label_->SetAutoColorReadabilityEnabled(false);
491 label_->SetShadows(gfx::ShadowValues(
492 1, gfx::ShadowValue(gfx::Vector2d(0, 1), 0, kStyleButtonShadowColor)));
493 #endif
494 label_->set_background(NULL);
495 } else { 476 } else {
496 label_->set_background(NULL); 477 label_->set_background(nullptr);
497 } 478 }
498 479
499 if (constant_text_color)
500 colors[STATE_HOVERED] = colors[STATE_PRESSED] = colors[STATE_NORMAL];
501
502 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) { 480 for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) {
503 if (!explicitly_set_colors_[state]) { 481 if (!explicitly_set_colors_[state]) {
504 SetTextColor(static_cast<ButtonState>(state), colors[state]); 482 SetTextColor(static_cast<ButtonState>(state), colors[state]);
505 explicitly_set_colors_[state] = false; 483 explicitly_set_colors_[state] = false;
506 } 484 }
507 } 485 }
508 } 486 }
509 487
510 void LabelButton::UpdateImage() { 488 void LabelButton::UpdateImage() {
511 image_->SetImage(GetImage(state())); 489 image_->SetImage(GetImage(state()));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 ui::NativeTheme::ExtraParams* params) const { 544 ui::NativeTheme::ExtraParams* params) const {
567 GetExtraParams(params); 545 GetExtraParams(params);
568 return ui::NativeTheme::kHovered; 546 return ui::NativeTheme::kHovered;
569 } 547 }
570 548
571 void LabelButton::ResetCachedPreferredSize() { 549 void LabelButton::ResetCachedPreferredSize() {
572 cached_preferred_size_valid_ = false; 550 cached_preferred_size_valid_ = false;
573 cached_preferred_size_ = gfx::Size(); 551 cached_preferred_size_ = gfx::Size();
574 } 552 }
575 553
554 void LabelButton::HighlightStateChanged() {
sky 2016/04/07 17:16:31 This function is confusingly named as there is no
tapted 2016/04/08 08:35:33 Done.
555 const SkColor color =
sky 2016/04/07 17:16:31 If you're going to use const (which is good), be c
tapted 2016/04/08 08:35:33 Done.
556 explicitly_set_colors_[state()]
557 ? button_state_colors_[state()]
558 : PlatformStyle::TextColorForButton(button_state_colors_, *this);
559 if (state() != STATE_DISABLED && label_->enabled_color() != color)
560 label_->SetEnabledColor(color);
561 }
562
576 } // namespace views 563 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698