Use views::style for buttons, bootstrap ash_typography to do so.
This makes LabelButton::AdjustFontSize() obsolete.
LabelButton::SetFontSize() is also mostly obsolete. Remove it. Two
remaining cases are button subclasses that are never "default" buttons
so they can just set the font list on the button->label() directly.
BUG=691891, 623987
TEST=LabelButtonTest.TextSizeFromContext (replaces AdjustFontSize)
Review-Url: https://codereview.chromium.org/2801583002
Cr-Commit-Position: refs/heads/master@{#472019}
diff --git a/ash/public/cpp/BUILD.gn b/ash/public/cpp/BUILD.gn
index e291ff39..4dde322 100644
--- a/ash/public/cpp/BUILD.gn
+++ b/ash/public/cpp/BUILD.gn
@@ -6,6 +6,8 @@
component("ash_public_cpp") {
sources = [
"ash_public_export.h",
+ "ash_typography.cc",
+ "ash_typography.h",
"config.h",
"mus_property_mirror_ash.cc",
"mus_property_mirror_ash.h",
@@ -32,6 +34,7 @@
"//mojo/common:common_custom_types",
"//skia/public/interfaces",
"//ui/aura",
+ "//ui/views",
"//ui/views/mus",
]
diff --git a/ash/public/cpp/ash_typography.cc b/ash/public/cpp/ash_typography.cc
new file mode 100644
index 0000000..ff671025
--- /dev/null
+++ b/ash/public/cpp/ash_typography.cc
@@ -0,0 +1,23 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/public/cpp/ash_typography.h"
+
+namespace ash {
+
+void ApplyAshFontStyles(int context,
+ int style,
+ int* size_delta,
+ gfx::Font::Weight* font_weight) {
+ switch (context) {
+ case CONTEXT_LAUNCHER_BUTTON:
+ *size_delta = 2;
+ break;
+ case CONTEXT_TOAST_OVERLAY:
+ *size_delta = 3;
+ break;
+ }
+}
+
+} // namespace ash
diff --git a/ash/public/cpp/ash_typography.h b/ash/public/cpp/ash_typography.h
new file mode 100644
index 0000000..e56a1922
--- /dev/null
+++ b/ash/public/cpp/ash_typography.h
@@ -0,0 +1,35 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ASH_PUBLIC_CPP_ASH_TYPOGRAPHY_H_
+#define ASH_PUBLIC_CPP_ASH_TYPOGRAPHY_H_
+
+#include "ash/public/cpp/ash_public_export.h"
+#include "ui/gfx/font.h"
+#include "ui/views/style/typography.h"
+
+namespace ash {
+
+enum AshTextContext {
+ ASH_TEXT_CONTEXT_START = views::style::VIEWS_TEXT_CONTEXT_END,
+
+ // A button that appears in the launcher's status area.
+ CONTEXT_LAUNCHER_BUTTON = ASH_TEXT_CONTEXT_START,
+
+ // Buttons and labels that appear in the fullscreen toast overlay UI.
+ CONTEXT_TOAST_OVERLAY,
+
+ ASH_TEXT_CONTEXT_END
+};
+
+// Sets the |size_delta| and |font_weight| for ash-specific text contexts.
+// Values are only set for contexts specific to ash.
+void ASH_PUBLIC_EXPORT ApplyAshFontStyles(int context,
+ int style,
+ int* size_delta,
+ gfx::Font::Weight* font_weight);
+
+} // namespace ash
+
+#endif // ASH_PUBLIC_CPP_ASH_TYPOGRAPHY_H_
diff --git a/ash/system/session/logout_button_tray.cc b/ash/system/session/logout_button_tray.cc
index a2783a9..980cdfe 100644
--- a/ash/system/session/logout_button_tray.cc
+++ b/ash/system/session/logout_button_tray.cc
@@ -4,6 +4,7 @@
#include "ash/system/session/logout_button_tray.h"
+#include "ash/public/cpp/ash_typography.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shelf/wm_shelf.h"
#include "ash/shell.h"
@@ -26,14 +27,15 @@
LogoutButtonTray::LogoutButtonTray(WmShelf* wm_shelf)
: wm_shelf_(wm_shelf),
container_(new TrayContainer(wm_shelf)),
- button_(views::MdTextButton::Create(this, base::string16())),
+ button_(views::MdTextButton::Create(this,
+ base::string16(),
+ CONTEXT_LAUNCHER_BUTTON)),
show_logout_button_in_tray_(false) {
SetLayoutManager(new views::FillLayout);
AddChildView(container_);
button_->SetProminent(true);
button_->SetBgColorOverride(gfx::kGoogleRed700);
- button_->AdjustFontSize(kTrayTextFontSizeIncrease);
container_->AddChildView(button_);
Shell::Get()->system_tray_notifier()->AddLogoutButtonObserver(this);
diff --git a/ash/system/toast/toast_overlay.cc b/ash/system/toast/toast_overlay.cc
index 8c0fcd9..d879521 100644
--- a/ash/system/toast/toast_overlay.cc
+++ b/ash/system/toast/toast_overlay.cc
@@ -4,6 +4,7 @@
#include "ash/system/toast/toast_overlay.h"
+#include "ash/public/cpp/ash_typography.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/wm_shelf.h"
@@ -15,7 +16,6 @@
#include "base/threading/thread_task_runner_handle.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
-#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/views/border.h"
@@ -34,10 +34,6 @@
// Offset of the overlay from the edge of the work area.
const int kOffset = 5;
-// Font style used for modifier key labels.
-const ui::ResourceBundle::FontStyle kTextFontStyle =
- ui::ResourceBundle::MediumFont;
-
// Duration of slide animation when overlay is shown or hidden.
const int kSlideAnimationDurationMs = 100;
@@ -71,12 +67,9 @@
DISALLOW_COPY_AND_ASSIGN(ToastOverlayLabel);
};
-ToastOverlayLabel::ToastOverlayLabel(const base::string16& label) {
- ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
-
- SetText(label);
+ToastOverlayLabel::ToastOverlayLabel(const base::string16& label)
+ : Label(label, CONTEXT_TOAST_OVERLAY) {
SetHorizontalAlignment(gfx::ALIGN_LEFT);
- SetFontList(rb->GetFontList(kTextFontStyle));
SetAutoColorReadabilityEnabled(false);
SetMultiLine(true);
SetEnabledColor(SK_ColorWHITE);
@@ -107,15 +100,12 @@
ToastOverlayButton::ToastOverlayButton(views::ButtonListener* listener,
const base::string16& text)
- : views::LabelButton(listener, text) {
+ : views::LabelButton(listener, text, CONTEXT_TOAST_OVERLAY) {
SetInkDropMode(InkDropMode::ON);
set_has_ink_drop_action_on_click(true);
set_ink_drop_base_color(SK_ColorWHITE);
- ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
-
SetEnabledTextColors(kButtonTextColor);
- SetFontList(rb->GetFontList(kTextFontStyle));
// Treat the space below the baseline as a margin.
int verticalSpacing = kToastVerticalSpacing -
diff --git a/chrome/browser/ui/views/harmony/chrome_typography.cc b/chrome/browser/ui/views/harmony/chrome_typography.cc
index f128f7c..5fc7051 100644
--- a/chrome/browser/ui/views/harmony/chrome_typography.cc
+++ b/chrome/browser/ui/views/harmony/chrome_typography.cc
@@ -16,6 +16,10 @@
gfx::Font::Weight font_weight;
GetDefaultFont(text_context, text_style, &size_delta, &font_weight);
+#if defined(USE_ASH)
+ ash::ApplyAshFontStyles(text_context, text_style, &size_delta, &font_weight);
+#endif
+
switch (text_context) {
case CONTEXT_HEADLINE:
size_delta = kHeadlineDelta;
diff --git a/chrome/browser/ui/views/harmony/chrome_typography.h b/chrome/browser/ui/views/harmony/chrome_typography.h
index 9f89400..3d626d4 100644
--- a/chrome/browser/ui/views/harmony/chrome_typography.h
+++ b/chrome/browser/ui/views/harmony/chrome_typography.h
@@ -9,9 +9,19 @@
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
+#if defined(USE_ASH)
+#include "ash/public/cpp/ash_typography.h"
+#endif
+
enum ChromeTextContext {
+#if defined(USE_ASH)
+ CHROME_TEXT_CONTEXT_START = ash::ASH_TEXT_CONTEXT_END,
+#else
+ CHROME_TEXT_CONTEXT_START = views::style::VIEWS_TEXT_CONTEXT_END,
+#endif
+
// Headline text. Usually 20pt. Never multi-line.
- CONTEXT_HEADLINE = views::style::VIEWS_TEXT_CONTEXT_END,
+ CONTEXT_HEADLINE = CHROME_TEXT_CONTEXT_START,
// "Body 1". Usually 13pt.
CONTEXT_BODY_TEXT_LARGE,
@@ -26,8 +36,10 @@
};
enum ChromeTextStyle {
+ CHROME_TEXT_STYLE_START = views::style::VIEWS_TEXT_STYLE_END,
+
// Secondary text. May be lighter than views::style::PRIMARY.
- STYLE_SECONDARY = views::style::VIEWS_TEXT_STYLE_END,
+ STYLE_SECONDARY = CHROME_TEXT_STYLE_START,
// "Hint" text, usually a line that gives context to something more important.
STYLE_HINT,
diff --git a/chrome/browser/ui/views/harmony/harmony_typography_provider.cc b/chrome/browser/ui/views/harmony/harmony_typography_provider.cc
index 21caf0c..570101fe 100644
--- a/chrome/browser/ui/views/harmony/harmony_typography_provider.cc
+++ b/chrome/browser/ui/views/harmony/harmony_typography_provider.cc
@@ -8,6 +8,10 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/platform_font.h"
+#if defined(USE_ASH)
+#include "ash/public/cpp/ash_typography.h"
+#endif
+
const gfx::FontList& HarmonyTypographyProvider::GetFont(int text_context,
int text_style) const {
// "Target" font size constants from the Harmony spec.
@@ -24,9 +28,14 @@
int size_delta = kDefaultSize - gfx::PlatformFont::kDefaultBaseFontSize;
gfx::Font::Weight font_weight = gfx::Font::Weight::NORMAL;
+
+#if defined(USE_ASH)
+ ash::ApplyAshFontStyles(text_context, text_style, &size_delta, &font_weight);
+#endif
+
switch (text_context) {
- case CONTEXT_HEADLINE:
- size_delta = kHeadlineSize - gfx::PlatformFont::kDefaultBaseFontSize;
+ case views::style::CONTEXT_BUTTON_MD:
+ font_weight = WeightNotLighterThanNormal(kButtonFontWeight);
break;
case views::style::CONTEXT_DIALOG_TITLE:
size_delta = kTitleSize - gfx::PlatformFont::kDefaultBaseFontSize;
@@ -34,14 +43,15 @@
case CONTEXT_BODY_TEXT_LARGE:
size_delta = kBodyTextLargeSize - gfx::PlatformFont::kDefaultBaseFontSize;
break;
- case views::style::CONTEXT_BUTTON:
- font_weight = kButtonFontWeight;
+ case CONTEXT_HEADLINE:
+ size_delta = kHeadlineSize - gfx::PlatformFont::kDefaultBaseFontSize;
break;
default:
break;
}
// Ignore |text_style| since it only affects color in the Harmony spec.
+
return ui::ResourceBundle::GetSharedInstance().GetFontListWithDelta(
size_delta, gfx::Font::NORMAL, font_weight);
}
@@ -107,14 +117,15 @@
kBodyTextSmallPlatformHeight + kBodyHeight;
switch (text_context) {
- case CONTEXT_HEADLINE:
- return headline_height;
+ case views::style::CONTEXT_BUTTON:
+ case views::style::CONTEXT_BUTTON_MD:
+ return kButtonAbsoluteHeight;
case views::style::CONTEXT_DIALOG_TITLE:
return title_height;
case CONTEXT_BODY_TEXT_LARGE:
return body_large_height;
- case views::style::CONTEXT_BUTTON:
- return kButtonAbsoluteHeight;
+ case CONTEXT_HEADLINE:
+ return headline_height;
default:
return default_height;
}
diff --git a/chrome/browser/ui/views/harmony/layout_provider_unittest.cc b/chrome/browser/ui/views/harmony/layout_provider_unittest.cc
index 2e9136a..0945b90 100644
--- a/chrome/browser/ui/views/harmony/layout_provider_unittest.cc
+++ b/chrome/browser/ui/views/harmony/layout_provider_unittest.cc
@@ -279,6 +279,8 @@
// buttons have flexibility to configure their own spacing.
EXPECT_EQ(0,
views::style::GetLineHeight(views::style::CONTEXT_BUTTON, kStyle));
+ EXPECT_EQ(
+ 0, views::style::GetLineHeight(views::style::CONTEXT_BUTTON_MD, kStyle));
}
// Ensure that line heights reported in a default bot configuration match the
diff --git a/chrome/browser/ui/views/profiles/avatar_button.cc b/chrome/browser/ui/views/profiles/avatar_button.cc
index 6b5ac73..a787cc31 100644
--- a/chrome/browser/ui/views/profiles/avatar_button.cc
+++ b/chrome/browser/ui/views/profiles/avatar_button.cc
@@ -86,7 +86,7 @@
// is larger than this, it will be shrunk to match it.
// TODO(noms): Calculate this constant algorithmically from the button's size.
const int kDisplayFontHeight = 16;
- SetFontList(
+ label()->SetFontList(
label()->font_list().DeriveWithHeightUpperBound(kDisplayFontHeight));
#if defined(OS_WIN)
diff --git a/chrome/browser/ui/views/toolbar/app_menu.cc b/chrome/browser/ui/views/toolbar/app_menu.cc
index e1a39e9..b26eea5 100644
--- a/chrome/browser/ui/views/toolbar/app_menu.cc
+++ b/chrome/browser/ui/views/toolbar/app_menu.cc
@@ -257,7 +257,7 @@
set_background(in_menu_background_);
SetBorder(
views::CreateEmptyBorder(0, kHorizontalPadding, 0, kHorizontalPadding));
- SetFontList(MenuConfig::instance().font_list);
+ label()->SetFontList(MenuConfig::instance().font_list);
}
void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
diff --git a/ui/views/controls/button/label_button.cc b/ui/views/controls/button/label_button.cc
index de45e48..aa8dae70 100644
--- a/ui/views/controls/button/label_button.cc
+++ b/ui/views/controls/button/label_button.cc
@@ -31,61 +31,23 @@
#include "ui/views/style/platform_style.h"
#include "ui/views/window/dialog_delegate.h"
-namespace {
-
-gfx::Font::Weight GetValueBolderThan(gfx::Font::Weight weight) {
- if (weight < gfx::Font::Weight::BOLD)
- return gfx::Font::Weight::BOLD;
- switch (weight) {
- case gfx::Font::Weight::BOLD:
- return gfx::Font::Weight::EXTRA_BOLD;
- case gfx::Font::Weight::EXTRA_BOLD:
- case gfx::Font::Weight::BLACK:
- return gfx::Font::Weight::BLACK;
- default:
- NOTREACHED();
- }
- return gfx::Font::Weight::INVALID;
-}
-
-const gfx::FontList& GetDefaultNormalFontList() {
- static base::LazyInstance<gfx::FontList>::Leaky font_list =
- LAZY_INSTANCE_INITIALIZER;
- return font_list.Get();
-}
-
-const gfx::FontList& GetDefaultBoldFontList() {
- if (!views::PlatformStyle::kDefaultLabelButtonHasBoldFont)
- return GetDefaultNormalFontList();
-
- static base::LazyInstance<gfx::FontList>::Leaky font_list =
- LAZY_INSTANCE_INITIALIZER;
-
- static const gfx::Font::Weight default_bold_weight =
- font_list.Get().GetFontWeight();
-
- font_list.Get() = font_list.Get().DeriveWithWeight(
- GetValueBolderThan(default_bold_weight));
- DCHECK_GE(font_list.Get().GetFontWeight(), gfx::Font::Weight::BOLD);
-
- return font_list.Get();
-}
-
-} // namespace
-
namespace views {
// static
const int LabelButton::kHoverAnimationDurationMs = 170;
const char LabelButton::kViewClassName[] = "LabelButton";
-LabelButton::LabelButton(ButtonListener* listener, const base::string16& text)
+LabelButton::LabelButton(ButtonListener* listener,
+ const base::string16& text,
+ int button_context)
: CustomButton(listener),
image_(new ImageView()),
- label_(new Label()),
+ label_(new Label(text, button_context, style::STYLE_PRIMARY)),
ink_drop_container_(new InkDropContainerView()),
- cached_normal_font_list_(GetDefaultNormalFontList()),
- cached_bold_font_list_(GetDefaultBoldFontList()),
+ cached_normal_font_list_(
+ style::GetFont(button_context, style::STYLE_PRIMARY)),
+ cached_default_button_font_list_(
+ style::GetFont(button_context, style::STYLE_DIALOG_BUTTON_DEFAULT)),
button_state_images_(),
button_state_colors_(),
explicitly_set_colors_(),
@@ -107,7 +69,6 @@
image_->set_can_process_events_within_subtree(false);
AddChildView(label_);
- label_->SetFontList(cached_normal_font_list_);
label_->SetAutoColorReadabilityEnabled(false);
label_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
@@ -159,11 +120,6 @@
label_->SetSubpixelRenderingEnabled(enabled);
}
-void LabelButton::AdjustFontSize(int font_size_delta) {
- LabelButton::SetFontList(
- label()->font_list().DeriveWithSizeDelta(font_size_delta));
-}
-
void LabelButton::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
label_->SetElideBehavior(elide_behavior);
}
@@ -241,7 +197,7 @@
// Some text appears wider when rendered normally than when rendered bold.
// Accommodate the widest, as buttons may show bold and shouldn't resize.
const int current_width = label.GetPreferredSize().width();
- label.SetFontList(cached_bold_font_list_);
+ label.SetFontList(cached_default_button_font_list_);
if (label.GetPreferredSize().width() < current_width)
label.SetFontList(label_->font_list());
}
@@ -378,19 +334,6 @@
return GetLocalBounds();
}
-void LabelButton::SetFontList(const gfx::FontList& font_list) {
- cached_normal_font_list_ = font_list;
- if (PlatformStyle::kDefaultLabelButtonHasBoldFont) {
- cached_bold_font_list_ = font_list.DeriveWithWeight(
- GetValueBolderThan(font_list.GetFontWeight()));
- if (is_default_) {
- label_->SetFontList(cached_bold_font_list_);
- return;
- }
- }
- label_->SetFontList(cached_normal_font_list_);
-}
-
bool LabelButton::ShouldUseFloodFillInkDrop() const {
return !GetText().empty();
}
@@ -526,9 +469,17 @@
}
void LabelButton::UpdateStyleToIndicateDefaultStatus() {
+ // Check that a subclass hasn't replaced the Label font. These buttons may
+ // never be given default status.
+ DCHECK_EQ(cached_normal_font_list_.GetFontSize(),
+ label()->font_list().GetFontSize());
const bool bold =
PlatformStyle::kDefaultLabelButtonHasBoldFont && is_default_;
- label_->SetFontList(bold ? cached_bold_font_list_ : cached_normal_font_list_);
+ // TODO(tapted): This should use style::GetFont(), but this part can just be
+ // deleted when default buttons no longer go bold. Colors will need updating
+ // still.
+ label_->SetFontList(bold ? cached_default_button_font_list_
+ : cached_normal_font_list_);
InvalidateLayout();
ResetLabelEnabledColor();
}
diff --git a/ui/views/controls/button/label_button.h b/ui/views/controls/button/label_button.h
index 4661d20..8495c43 100644
--- a/ui/views/controls/button/label_button.h
+++ b/ui/views/controls/button/label_button.h
@@ -17,6 +17,7 @@
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/native_theme_delegate.h"
+#include "ui/views/style/typography.h"
namespace views {
@@ -33,7 +34,12 @@
static const char kViewClassName[];
- LabelButton(ButtonListener* listener, const base::string16& text);
+ // Creates a LabelButton with ButtonPressed() events sent to |listener| and
+ // label |text|. |button_context| is a value from views::style::TextContext
+ // and determines the appearance of |text|.
+ LabelButton(ButtonListener* listener,
+ const base::string16& text,
+ int button_context = style::CONTEXT_BUTTON);
~LabelButton() override;
// Gets or sets the image shown for the specified button state.
@@ -57,9 +63,6 @@
// Sets whether subpixel rendering is used on the label.
void SetTextSubpixelRenderingEnabled(bool enabled);
- // Adjusts the font size up or down by the given amount.
- virtual void AdjustFontSize(int font_size_delta);
-
// Sets the elide behavior of this button.
void SetElideBehavior(gfx::ElideBehavior elide_behavior);
@@ -121,9 +124,6 @@
// these bounds if they need room to do manual painting.
virtual gfx::Rect GetChildAreaBounds();
- // Sets the font list used by this button.
- virtual void SetFontList(const gfx::FontList& font_list);
-
// Returns true if the CreateInkDrop*() methods should create flood fill ink
// drop components.
virtual bool ShouldUseFloodFillInkDrop() const;
@@ -198,9 +198,10 @@
// drawing |label_| on a layer (which can mess with subpixel anti-aliasing).
InkDropContainerView* ink_drop_container_;
- // The cached font lists in the normal and bold style.
+ // The cached font lists in the normal and default button style. The latter
+ // may be bold.
gfx::FontList cached_normal_font_list_;
- gfx::FontList cached_bold_font_list_;
+ gfx::FontList cached_default_button_font_list_;
// The images and colors for each button state.
gfx::ImageSkia button_state_images_[STATE_COUNT];
diff --git a/ui/views/controls/button/label_button_unittest.cc b/ui/views/controls/button/label_button_unittest.cc
index 76fbc3c..9ba1771e 100644
--- a/ui/views/controls/button/label_button_unittest.cc
+++ b/ui/views/controls/button/label_button_unittest.cc
@@ -44,8 +44,9 @@
// Testing button that exposes protected methods.
class TestLabelButton : public LabelButton {
public:
- explicit TestLabelButton(const base::string16& text = base::string16())
- : LabelButton(nullptr, text) {}
+ explicit TestLabelButton(const base::string16& text = base::string16(),
+ int button_context = style::CONTEXT_BUTTON)
+ : LabelButton(nullptr, text, button_context) {}
using LabelButton::label;
using LabelButton::image;
@@ -148,7 +149,7 @@
TEST_F(LabelButtonTest, Label) {
EXPECT_TRUE(button_->GetText().empty());
- const gfx::FontList font_list;
+ const gfx::FontList font_list = button_->label()->font_list();
const base::string16 short_text(ASCIIToUTF16("abcdefghijklm"));
const base::string16 long_text(ASCIIToUTF16("abcdefghijklmnopqrstuvwxyz"));
const int short_text_width = gfx::GetStringWidth(short_text, font_list);
@@ -238,7 +239,7 @@
}
TEST_F(LabelButtonTest, LabelAndImage) {
- const gfx::FontList font_list;
+ const gfx::FontList font_list = button_->label()->font_list();
const base::string16 text(ASCIIToUTF16("abcdefghijklm"));
const int text_width = gfx::GetStringWidth(text, font_list);
@@ -296,23 +297,43 @@
EXPECT_LT(button_->GetPreferredSize().height(), image_size);
}
-TEST_F(LabelButtonTest, AdjustFontSize) {
- button_->SetText(base::ASCIIToUTF16("abc"));
+// Ensure that the text used for button labels correctly adjusts in response
+// to provided style::TextContext values.
+TEST_F(LabelButtonTest, TextSizeFromContext) {
+ constexpr style::TextContext kDefaultContext = style::CONTEXT_BUTTON;
- const int original_width = button_->GetPreferredSize().width();
- const int original_height = button_->GetPreferredSize().height();
+ // Although CONTEXT_DIALOG_TITLE isn't used for buttons, picking a style with
+ // a small delta risks finding a font with a different point-size but with the
+ // same maximum glyph height.
+ constexpr style::TextContext kAlternateContext = style::CONTEXT_DIALOG_TITLE;
+
+ // First sanity that the TextConstants used in the test give different sizes.
+ int default_delta, alternate_delta;
+ gfx::Font::Weight default_weight, alternate_weight;
+ DefaultTypographyProvider::GetDefaultFont(
+ kDefaultContext, style::STYLE_PRIMARY, &default_delta, &default_weight);
+ DefaultTypographyProvider::GetDefaultFont(
+ kAlternateContext, style::STYLE_PRIMARY, &alternate_delta,
+ &alternate_weight);
+ EXPECT_LT(default_delta, alternate_delta);
+
+ const base::string16 text(ASCIIToUTF16("abcdefghijklm"));
+ button_->SetText(text);
+ EXPECT_EQ(default_delta, button_->label()->font_list().GetFontSize() -
+ gfx::FontList().GetFontSize());
+
+ TestLabelButton* alternate_button =
+ new TestLabelButton(text, kAlternateContext);
+ button_->parent()->AddChildView(alternate_button);
+ EXPECT_EQ(alternate_delta,
+ alternate_button->label()->font_list().GetFontSize() -
+ gfx::FontList().GetFontSize());
// The button size increases when the font size is increased.
- button_->AdjustFontSize(100);
- EXPECT_GT(button_->GetPreferredSize().width(), original_width);
- EXPECT_GT(button_->GetPreferredSize().height(), original_height);
-
- // The button returns to its original size when the minimal size is cleared
- // and the original font size is restored.
- button_->SetMinSize(gfx::Size());
- button_->AdjustFontSize(-100);
- EXPECT_EQ(original_width, button_->GetPreferredSize().width());
- EXPECT_EQ(original_height, button_->GetPreferredSize().height());
+ EXPECT_LT(button_->GetPreferredSize().width(),
+ alternate_button->GetPreferredSize().width());
+ EXPECT_LT(button_->GetPreferredSize().height(),
+ alternate_button->GetPreferredSize().height());
}
TEST_F(LabelButtonTest, ChangeTextSize) {
diff --git a/ui/views/controls/button/md_text_button.cc b/ui/views/controls/button/md_text_button.cc
index aaa3f2e..460517a1 100644
--- a/ui/views/controls/button/md_text_button.cc
+++ b/ui/views/controls/button/md_text_button.cc
@@ -22,6 +22,7 @@
#include "ui/views/layout/layout_provider.h"
#include "ui/views/painter.h"
#include "ui/views/style/platform_style.h"
+#include "ui/views/style/typography.h"
namespace views {
@@ -31,22 +32,13 @@
const base::string16& text,
bool md) {
if (md)
- return MdTextButton::Create(listener, text);
+ return MdTextButton::Create(listener, text, style::CONTEXT_BUTTON_MD);
- LabelButton* button = new LabelButton(listener, text);
+ LabelButton* button = new LabelButton(listener, text, style::CONTEXT_BUTTON);
button->SetStyleDeprecated(CustomButton::STYLE_BUTTON);
return button;
}
-const gfx::FontList& GetMdFontList() {
- static base::LazyInstance<gfx::FontList>::Leaky font_list =
- LAZY_INSTANCE_INITIALIZER;
- const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM;
- if (font_list.Get().GetFontWeight() < min_weight)
- font_list.Get() = font_list.Get().DeriveWithWeight(min_weight);
- return font_list.Get();
-}
-
} // namespace
// static
@@ -61,7 +53,8 @@
ButtonListener* listener,
const base::string16& text) {
if (ui::MaterialDesignController::IsSecondaryUiMaterial()) {
- MdTextButton* md_button = MdTextButton::Create(listener, text);
+ MdTextButton* md_button =
+ MdTextButton::Create(listener, text, style::CONTEXT_BUTTON_MD);
md_button->SetProminent(true);
return md_button;
}
@@ -71,8 +64,9 @@
// static
MdTextButton* MdTextButton::Create(ButtonListener* listener,
- const base::string16& text) {
- MdTextButton* button = new MdTextButton(listener);
+ const base::string16& text,
+ int button_context) {
+ MdTextButton* button = new MdTextButton(listener, button_context);
button->SetText(text);
button->SetFocusForPlatform();
return button;
@@ -170,23 +164,13 @@
UpdatePadding();
}
-void MdTextButton::AdjustFontSize(int size_delta) {
- LabelButton::AdjustFontSize(size_delta);
- UpdatePadding();
-}
-
void MdTextButton::UpdateStyleToIndicateDefaultStatus() {
is_prominent_ = is_prominent_ || is_default();
UpdateColors();
}
-void MdTextButton::SetFontList(const gfx::FontList& font_list) {
- NOTREACHED()
- << "Don't call MdTextButton::SetFontList (it will soon be protected)";
-}
-
-MdTextButton::MdTextButton(ButtonListener* listener)
- : LabelButton(listener, base::string16()),
+MdTextButton::MdTextButton(ButtonListener* listener, int button_context)
+ : LabelButton(listener, base::string16(), button_context),
is_prominent_(false) {
SetInkDropMode(InkDropMode::ON);
set_has_ink_drop_action_on_click(true);
@@ -198,7 +182,6 @@
SetFocusPainter(nullptr);
label()->SetAutoColorReadabilityEnabled(false);
set_request_focus_on_press(false);
- LabelButton::SetFontList(GetMdFontList());
set_animate_on_state_change(true);
@@ -227,7 +210,9 @@
// * Linux user sets base system font size to 17dp. For a normal button, the
// |size_delta| will be zero, so to adjust upwards we double 17 to get 34.
int size_delta =
- label()->font_list().GetFontSize() - GetMdFontList().GetFontSize();
+ label()->font_list().GetFontSize() -
+ style::GetFont(style::CONTEXT_BUTTON_MD, style::STYLE_PRIMARY)
+ .GetFontSize();
const int kBaseHeight = 28;
int target_height = std::max(kBaseHeight + size_delta * 2,
label()->font_list().GetFontSize() * 2);
diff --git a/ui/views/controls/button/md_text_button.h b/ui/views/controls/button/md_text_button.h
index 2ca349a..8d0d6e3 100644
--- a/ui/views/controls/button/md_text_button.h
+++ b/ui/views/controls/button/md_text_button.h
@@ -9,6 +9,7 @@
#include "base/optional.h"
#include "ui/views/controls/button/label_button.h"
+#include "ui/views/style/typography.h"
namespace views {
@@ -22,7 +23,8 @@
static LabelButton* CreateSecondaryUiBlueButton(ButtonListener* listener,
const base::string16& text);
static MdTextButton* Create(ButtonListener* listener,
- const base::string16& text);
+ const base::string16& text,
+ int button_context = style::CONTEXT_BUTTON_MD);
~MdTextButton() override;
@@ -46,16 +48,11 @@
SkColor GetInkDropBaseColor() const override;
void SetEnabledTextColors(SkColor color) override;
void SetText(const base::string16& text) override;
- void AdjustFontSize(int size_delta) override;
void UpdateStyleToIndicateDefaultStatus() override;
void StateChanged(ButtonState old_state) override;
- protected:
- // LabelButton:
- void SetFontList(const gfx::FontList& font_list) override;
-
private:
- explicit MdTextButton(ButtonListener* listener);
+ MdTextButton(ButtonListener* listener, int button_context);
void UpdatePadding();
void UpdateColors();
diff --git a/ui/views/style/typography.h b/ui/views/style/typography.h
index 055361a..66a683c7 100644
--- a/ui/views/style/typography.h
+++ b/ui/views/style/typography.h
@@ -24,8 +24,15 @@
// "CONTEXT_" represent the actual TextContexts: the rest are markers.
VIEWS_TEXT_CONTEXT_START = 0,
+ // Text that appears on a button control. Usually 12pt. This includes controls
+ // with button-like behavior, such as Checkbox.
+ CONTEXT_BUTTON = VIEWS_TEXT_CONTEXT_START,
+
+ // Text that appears on an MD-styled dialog button control. Usually 12pt.
+ CONTEXT_BUTTON_MD,
+
// A title for a dialog window. Usually 15pt. Multi-line OK.
- CONTEXT_DIALOG_TITLE = VIEWS_TEXT_CONTEXT_START,
+ CONTEXT_DIALOG_TITLE,
// Text to label a control, usually next to it. "Body 2". Usually 12pt.
CONTEXT_LABEL,
@@ -33,8 +40,8 @@
// An editable text field. Usually matches CONTROL_LABEL.
CONTEXT_TEXTFIELD,
- // Text that appears on a button control. Usually 12pt.
- CONTEXT_BUTTON,
+ // Text for the menu items that appear in the touch-selection context menu.
+ CONTEXT_TOUCH_MENU,
// Embedders must start TextContext enum values from this value.
VIEWS_TEXT_CONTEXT_END,
@@ -55,6 +62,9 @@
// contexts (e.g. BUTTON_TEXT, FIELD).
STYLE_PRIMARY = VIEWS_TEXT_STYLE_START,
+ // Style for the default button on a dialog.
+ STYLE_DIALOG_BUTTON_DEFAULT,
+
// Disabled "greyed out" text.
STYLE_DISABLED,
diff --git a/ui/views/style/typography_provider.cc b/ui/views/style/typography_provider.cc
index 521e2bf..ab46a0a9 100644
--- a/ui/views/style/typography_provider.cc
+++ b/ui/views/style/typography_provider.cc
@@ -9,39 +9,42 @@
#include "ui/base/resource/resource_bundle.h"
#include "ui/views/style/typography.h"
+using gfx::Font;
+
namespace views {
+namespace {
+
+Font::Weight GetValueBolderThan(Font::Weight weight) {
+ switch (weight) {
+ case Font::Weight::BOLD:
+ return Font::Weight::EXTRA_BOLD;
+ case Font::Weight::EXTRA_BOLD:
+ case Font::Weight::BLACK:
+ return Font::Weight::BLACK;
+ default:
+ return Font::Weight::BOLD;
+ }
+}
+
+} // namespace
+
+// static
+Font::Weight TypographyProvider::WeightNotLighterThanNormal(
+ Font::Weight weight) {
+ if (ResourceBundle::GetSharedInstance()
+ .GetFontListWithDelta(0, Font::NORMAL, Font::Weight::NORMAL)
+ .GetFontWeight() < weight)
+ return weight;
+ return Font::Weight::NORMAL;
+}
const gfx::FontList& DefaultTypographyProvider::GetFont(int context,
int style) const {
int size_delta;
- gfx::Font::Weight font_weight;
+ Font::Weight font_weight;
GetDefaultFont(context, style, &size_delta, &font_weight);
return ResourceBundle::GetSharedInstance().GetFontListWithDelta(
- size_delta, gfx::Font::NORMAL, font_weight);
-}
-
-void DefaultTypographyProvider::GetDefaultFont(
- int context,
- int style,
- int* size_delta,
- gfx::Font::Weight* font_weight) const {
- switch (context) {
- case style::CONTEXT_DIALOG_TITLE:
- *size_delta = ui::kTitleFontSizeDelta;
- break;
- default:
- *size_delta = ui::kLabelFontSizeDelta;
- break;
- }
-
- switch (style) {
- case style::STYLE_TAB_ACTIVE:
- *font_weight = gfx::Font::Weight::BOLD;
- break;
- default:
- *font_weight = gfx::Font::Weight::NORMAL;
- break;
- }
+ size_delta, Font::NORMAL, font_weight);
}
SkColor DefaultTypographyProvider::GetColor(int context, int style) const {
@@ -52,4 +55,43 @@
return 0;
}
+// static
+void DefaultTypographyProvider::GetDefaultFont(int context,
+ int style,
+ int* size_delta,
+ Font::Weight* font_weight) {
+ *font_weight = Font::Weight::NORMAL;
+
+ switch (context) {
+ case style::CONTEXT_BUTTON_MD:
+ *size_delta = ui::kLabelFontSizeDelta;
+ *font_weight = WeightNotLighterThanNormal(Font::Weight::MEDIUM);
+ break;
+ case style::CONTEXT_DIALOG_TITLE:
+ *size_delta = ui::kTitleFontSizeDelta;
+ break;
+ case style::CONTEXT_TOUCH_MENU:
+ *size_delta = -1;
+ break;
+ default:
+ *size_delta = ui::kLabelFontSizeDelta;
+ break;
+ }
+
+ switch (style) {
+ case style::STYLE_TAB_ACTIVE:
+ *font_weight = Font::Weight::BOLD;
+ break;
+ case style::STYLE_DIALOG_BUTTON_DEFAULT:
+ // Only non-MD default buttons should "increase" in boldness.
+ if (context == style::CONTEXT_BUTTON) {
+ *font_weight = GetValueBolderThan(
+ ResourceBundle::GetSharedInstance()
+ .GetFontListWithDelta(*size_delta, Font::NORMAL, *font_weight)
+ .GetFontWeight());
+ }
+ break;
+ }
+}
+
} // namespace views
diff --git a/ui/views/style/typography_provider.h b/ui/views/style/typography_provider.h
index aab1c0e9..76d6b68 100644
--- a/ui/views/style/typography_provider.h
+++ b/ui/views/style/typography_provider.h
@@ -31,6 +31,14 @@
// Gets the line spacing, or 0 if it should be provided by gfx::FontList.
virtual int GetLineHeight(int context, int style) const = 0;
+ // The system may indicate a "bold" UI font is preferred (e.g. by selecting
+ // the "Bold" checkbox in Windows under "Change only the text size" in
+ // Control Panel). In this case, a user's gfx::Weight::NORMAL font will
+ // already be bold, and requesting a MEDIUM font will result in a font that is
+ // less bold. So this method returns NORMAL, if the NORMAL font is at least as
+ // bold as |weight|.
+ static gfx::Font::Weight WeightNotLighterThanNormal(gfx::Font::Weight weight);
+
protected:
TypographyProvider() = default;
@@ -52,10 +60,10 @@
// Sets the |size_delta| and |font_weight| that the the default GetFont()
// implementation uses. Always sets values, even for styles it doesn't know
// about.
- void GetDefaultFont(int context,
- int style,
- int* size_delta,
- gfx::Font::Weight* font_weight) const;
+ static void GetDefaultFont(int context,
+ int style,
+ int* size_delta,
+ gfx::Font::Weight* font_weight);
private:
DISALLOW_COPY_AND_ASSIGN(DefaultTypographyProvider);
diff --git a/ui/views/touchui/touch_selection_menu_runner_views.cc b/ui/views/touchui/touch_selection_menu_runner_views.cc
index b208d5ff..f57c39e 100644
--- a/ui/views/touchui/touch_selection_menu_runner_views.cc
+++ b/ui/views/touchui/touch_selection_menu_runner_views.cc
@@ -10,7 +10,6 @@
#include "base/strings/utf_string_conversions.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
-#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
@@ -20,6 +19,7 @@
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/layout/box_layout.h"
+#include "ui/views/style/typography.h"
namespace views {
namespace {
@@ -158,10 +158,9 @@
int tag) {
base::string16 label =
gfx::RemoveAcceleratorChar(title, '&', nullptr, nullptr);
- LabelButton* button = new LabelButton(this, label);
+ LabelButton* button = new LabelButton(this, label, style::CONTEXT_TOUCH_MENU);
button->SetMinSize(gfx::Size(kMenuButtonMinWidth, kMenuButtonMinHeight));
button->SetFocusForPlatform();
- button->AdjustFontSize(ui::ResourceBundle::kSmallFontDelta);
button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
button->set_tag(tag);
return button;