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

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

Issue 544173007: Avoid repeatedly deriving fonts in the LabelButton ctor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add bold styling DCHECK warning against repeated derive calls. Created 6 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/lazy_instance.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "ui/gfx/animation/throb_animation.h" 9 #include "ui/gfx/animation/throb_animation.h"
9 #include "ui/gfx/canvas.h" 10 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/font_list.h" 11 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/sys_color_change_listener.h" 12 #include "ui/gfx/sys_color_change_listener.h"
12 #include "ui/native_theme/native_theme.h" 13 #include "ui/native_theme/native_theme.h"
13 #include "ui/views/background.h" 14 #include "ui/views/background.h"
14 #include "ui/views/controls/button/label_button_border.h" 15 #include "ui/views/controls/button/label_button_border.h"
15 #include "ui/views/painter.h" 16 #include "ui/views/painter.h"
16 #include "ui/views/window/dialog_delegate.h" 17 #include "ui/views/window/dialog_delegate.h"
17 18
18 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 19 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
19 #include "ui/views/linux_ui/linux_ui.h" 20 #include "ui/views/linux_ui/linux_ui.h"
20 #endif 21 #endif
21 22
22 namespace { 23 namespace {
23 24
24 // The default spacing between the icon and text. 25 // The default spacing between the icon and text.
25 const int kSpacing = 5; 26 const int kSpacing = 5;
26 27
27 #if !(defined(OS_LINUX) && !defined(OS_CHROMEOS)) 28 #if !(defined(OS_LINUX) && !defined(OS_CHROMEOS))
28 // Default text and shadow colors for STYLE_BUTTON. 29 // Default text and shadow colors for STYLE_BUTTON.
29 const SkColor kStyleButtonTextColor = SK_ColorBLACK; 30 const SkColor kStyleButtonTextColor = SK_ColorBLACK;
30 const SkColor kStyleButtonShadowColor = SK_ColorWHITE; 31 const SkColor kStyleButtonShadowColor = SK_ColorWHITE;
31 #endif 32 #endif
32 33
34 const gfx::FontList& GetDefaultNormalFontList() {
35 static base::LazyInstance<gfx::FontList>::Leaky font_list =
36 LAZY_INSTANCE_INITIALIZER;
37 return font_list.Get();
38 }
39
40 const gfx::FontList& GetDefaultBoldFontList() {
41 static base::LazyInstance<gfx::FontList>::Leaky font_list =
42 LAZY_INSTANCE_INITIALIZER;
43 if ((font_list.Get().GetFontStyle() & gfx::Font::BOLD) == 0) {
44 font_list.Get() = font_list.Get().
45 DeriveWithStyle(font_list.Get().GetFontStyle() | gfx::Font::BOLD);
46 DCHECK_NE(font_list.Get().GetFontStyle() & gfx::Font::BOLD, 0);
47 }
48 return font_list.Get();
49 }
50
33 } // namespace 51 } // namespace
34 52
35 namespace views { 53 namespace views {
36 54
37 // static 55 // static
38 const int LabelButton::kHoverAnimationDurationMs = 170; 56 const int LabelButton::kHoverAnimationDurationMs = 170;
39 57
40 // static 58 // static
41 const char LabelButton::kViewClassName[] = "LabelButton"; 59 const char LabelButton::kViewClassName[] = "LabelButton";
42 60
43 LabelButton::LabelButton(ButtonListener* listener, const base::string16& text) 61 LabelButton::LabelButton(ButtonListener* listener, const base::string16& text)
44 : CustomButton(listener), 62 : CustomButton(listener),
45 image_(new ImageView()), 63 image_(new ImageView()),
46 label_(new Label()), 64 label_(new Label()),
65 cached_normal_font_list_(GetDefaultNormalFontList()),
66 cached_bold_font_list_(GetDefaultBoldFontList()),
47 button_state_images_(), 67 button_state_images_(),
48 button_state_colors_(), 68 button_state_colors_(),
49 explicitly_set_colors_(), 69 explicitly_set_colors_(),
50 is_default_(false), 70 is_default_(false),
51 style_(STYLE_TEXTBUTTON), 71 style_(STYLE_TEXTBUTTON),
52 border_is_themed_border_(true), 72 border_is_themed_border_(true),
53 image_label_spacing_(kSpacing) { 73 image_label_spacing_(kSpacing) {
54 SetAnimationDuration(kHoverAnimationDurationMs); 74 SetAnimationDuration(kHoverAnimationDurationMs);
55 SetText(text); 75 SetText(text);
56 SetFontList(gfx::FontList());
57 76
58 AddChildView(image_); 77 AddChildView(image_);
59 image_->set_interactive(false); 78 image_->set_interactive(false);
60 79
61 AddChildView(label_); 80 AddChildView(label_);
81 label_->SetFontList(cached_normal_font_list_);
62 label_->SetAutoColorReadabilityEnabled(false); 82 label_->SetAutoColorReadabilityEnabled(false);
63 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 83 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
64 84
65 // Initialize the colors, border, and layout. 85 // Initialize the colors, border, and layout.
66 SetStyle(style_); 86 SetStyle(style_);
67 87
68 SetAccessibleName(text); 88 SetAccessibleName(text);
69 } 89 }
70 90
71 LabelButton::~LabelButton() {} 91 LabelButton::~LabelButton() {}
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 GetExtraParams(params); 509 GetExtraParams(params);
490 return ui::NativeTheme::kHovered; 510 return ui::NativeTheme::kHovered;
491 } 511 }
492 512
493 void LabelButton::ResetCachedPreferredSize() { 513 void LabelButton::ResetCachedPreferredSize() {
494 cached_preferred_size_valid_ = false; 514 cached_preferred_size_valid_ = false;
495 cached_preferred_size_= gfx::Size(); 515 cached_preferred_size_= gfx::Size();
496 } 516 }
497 517
498 } // namespace views 518 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698