Commit d0c7052e authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

Support typography styles in views::StyledLabel.

Adds initial support to views::Link as well, by allowing it to pass
TextContext to its parent views::Label constructor.

The entire StyledLabel has a single TextContext. A default TextStyle
can be optionally overridden on a RangeStyleInfo.

Audited the StyledLabel consumers: One consumer (EchoDialogView)
uses underlined text outside of a Link. The dialog is under review
(http://crbug.com/768663), so don't add a style for it.

The PageInfoBubble was the only consumer that wanted an underline on
its links. Remove underlines from all links in the page info bubble for
consistency with the rest of Chrome's UI.

All the (slow) FontList::Derive(..) calls disappear from StyledLabel.
Most fonts just come from the ResourceBundle cache now, unless they
need a custom_font (which is currently just EchoDialogView).

Bug: 691891
Change-Id: I6cca4480d366d2578ac01da4d604a414415ffbc4
Reviewed-on: https://chromium-review.googlesource.com/649930
Commit-Queue: Trent Apted <tapted@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarBret Sepulveda <bsep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505246}
parent e8819a83
...@@ -57,17 +57,15 @@ void EchoDialogView::InitForEnabledEcho(const base::string16& service_name, ...@@ -57,17 +57,15 @@ void EchoDialogView::InitForEnabledEcho(const base::string16& service_name,
label_ = new views::StyledLabel(text, this); label_ = new views::StyledLabel(text, this);
views::StyledLabel::RangeStyleInfo service_name_style; views::StyledLabel::RangeStyleInfo service_name_style;
service_name_style.font_style = gfx::Font::UNDERLINE; service_name_style.custom_font =
label_->GetDefaultFontList().DeriveWithStyle(gfx::Font::UNDERLINE);
service_name_style.tooltip = origin; service_name_style.tooltip = origin;
label_->AddStyleRange( label_->AddStyleRange(
gfx::Range(offsets[0], offsets[0] + service_name.length()), gfx::Range(offsets[0], offsets[0] + service_name.length()),
service_name_style); service_name_style);
views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink();
link_style.font_style = gfx::Font::NORMAL;
label_->AddStyleRange(gfx::Range(offsets[1], offsets[1] + link.length()), label_->AddStyleRange(gfx::Range(offsets[1], offsets[1] + link.length()),
link_style); views::StyledLabel::RangeStyleInfo::CreateForLink());
SetLabelBorderAndBounds(); SetLabelBorderAndBounds();
...@@ -86,11 +84,8 @@ void EchoDialogView::InitForDisabledEcho() { ...@@ -86,11 +84,8 @@ void EchoDialogView::InitForDisabledEcho() {
IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset); IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset);
label_ = new views::StyledLabel(text, this); label_ = new views::StyledLabel(text, this);
label_->AddStyleRange(gfx::Range(offset, offset + link.length()),
views::StyledLabel::RangeStyleInfo link_style = views::StyledLabel::RangeStyleInfo::CreateForLink());
views::StyledLabel::RangeStyleInfo::CreateForLink();
link_style.font_style = gfx::Font::NORMAL;
label_->AddStyleRange(gfx::Range(offset, offset + link.length()), link_style);
SetLabelBorderAndBounds(); SetLabelBorderAndBounds();
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
#include "chrome/app/vector_icons/vector_icons.h" #include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/autofill/password_generation_popup_controller.h" #include "chrome/browser/ui/autofill/password_generation_popup_controller.h"
#include "chrome/browser/ui/autofill/popup_constants.h" #include "chrome/browser/ui/autofill/popup_constants.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "ui/accessibility/ax_node_data.h" #include "ui/accessibility/ax_node_data.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h" #include "ui/gfx/color_palette.h"
...@@ -40,11 +42,9 @@ class PasswordTextBox : public views::View { ...@@ -40,11 +42,9 @@ class PasswordTextBox : public views::View {
~PasswordTextBox() override {} ~PasswordTextBox() override {}
// |suggestion_text| prompts the user to select the password, // |suggestion_text| prompts the user to select the password,
// |generated_password| is the generated password, and |font_list| is the font // |generated_password| is the generated password.
// used for all text in this class.
void Init(const base::string16& suggestion_text, void Init(const base::string16& suggestion_text,
const base::string16& generated_password, const base::string16& generated_password) {
const gfx::FontList& font_list) {
views::BoxLayout* box_layout = new views::BoxLayout( views::BoxLayout* box_layout = new views::BoxLayout(
views::BoxLayout::kVertical, gfx::Insets(12, 0), 5); views::BoxLayout::kVertical, gfx::Insets(12, 0), 5);
box_layout->set_main_axis_alignment( box_layout->set_main_axis_alignment(
...@@ -52,15 +52,14 @@ class PasswordTextBox : public views::View { ...@@ -52,15 +52,14 @@ class PasswordTextBox : public views::View {
SetLayoutManager(box_layout); SetLayoutManager(box_layout);
views::Label* suggestion_label = new views::Label( views::Label* suggestion_label = new views::Label(
suggestion_text, views::Label::CustomFont{font_list.DeriveWithWeight( suggestion_text, CONTEXT_DEPRECATED_SMALL, STYLE_EMPHASIZED);
gfx::Font::Weight::BOLD)});
suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); suggestion_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
suggestion_label->SetEnabledColor( suggestion_label->SetEnabledColor(
PasswordGenerationPopupView::kPasswordTextColor); PasswordGenerationPopupView::kPasswordTextColor);
AddChildView(suggestion_label); AddChildView(suggestion_label);
views::Label* password_label = views::Label* password_label =
new views::Label(generated_password, {font_list}); new views::Label(generated_password, CONTEXT_DEPRECATED_SMALL);
password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); password_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
password_label->SetEnabledColor( password_label->SetEnabledColor(
PasswordGenerationPopupView::kPasswordTextColor); PasswordGenerationPopupView::kPasswordTextColor);
...@@ -87,11 +86,8 @@ class PasswordGenerationPopupViewViews::PasswordBox : public views::View { ...@@ -87,11 +86,8 @@ class PasswordGenerationPopupViewViews::PasswordBox : public views::View {
~PasswordBox() override {} ~PasswordBox() override {}
// |password| is the generated password, |suggestion| is the text prompting // |password| is the generated password, |suggestion| is the text prompting
// the user to select the password, and |font_list| is the font used for all // the user to select the password.
// the text. void Init(const base::string16& password, const base::string16& suggestion) {
void Init(const base::string16& password,
const base::string16& suggestion,
const gfx::FontList& font_list) {
views::BoxLayout* box_layout = new views::BoxLayout( views::BoxLayout* box_layout = new views::BoxLayout(
views::BoxLayout::kHorizontal, views::BoxLayout::kHorizontal,
gfx::Insets(0, PasswordGenerationPopupController::kHorizontalPadding), gfx::Insets(0, PasswordGenerationPopupController::kHorizontalPadding),
...@@ -106,7 +102,7 @@ class PasswordGenerationPopupViewViews::PasswordBox : public views::View { ...@@ -106,7 +102,7 @@ class PasswordGenerationPopupViewViews::PasswordBox : public views::View {
AddChildView(key_image); AddChildView(key_image);
PasswordTextBox* password_text_box = new PasswordTextBox(); PasswordTextBox* password_text_box = new PasswordTextBox();
password_text_box->Init(suggestion, password, font_list); password_text_box->Init(suggestion, password);
AddChildView(password_text_box); AddChildView(password_text_box);
} }
...@@ -126,18 +122,19 @@ PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews( ...@@ -126,18 +122,19 @@ PasswordGenerationPopupViewViews::PasswordGenerationPopupViewViews(
views::Widget* parent_widget) views::Widget* parent_widget)
: AutofillPopupBaseView(controller, parent_widget), : AutofillPopupBaseView(controller, parent_widget),
password_view_(NULL), password_view_(NULL),
font_list_(ResourceBundle::GetSharedInstance().GetFontList(
ResourceBundle::SmallFont)),
controller_(controller) { controller_(controller) {
if (controller_->display_password()) if (controller_->display_password())
CreatePasswordView(); CreatePasswordView();
help_label_ = new views::StyledLabel(controller_->HelpText(), this); help_label_ = new views::StyledLabel(controller_->HelpText(), this);
help_label_->SetBaseFontList(font_list_);
help_label_->SetLineHeight(20); // With MD, the line spacing comes from the TextContext instead and this is
views::StyledLabel::RangeStyleInfo default_style; // unnecessary.
default_style.color = kExplanatoryTextColor; if (!ui::MaterialDesignController::IsSecondaryUiMaterial())
help_label_->SetDefaultStyle(default_style); help_label_->SetLineHeight(20);
help_label_->SetTextContext(CONTEXT_DEPRECATED_SMALL);
help_label_->SetDefaultTextStyle(STYLE_HINT);
views::StyledLabel::RangeStyleInfo link_style = views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink();
...@@ -166,9 +163,7 @@ void PasswordGenerationPopupViewViews::CreatePasswordView() { ...@@ -166,9 +163,7 @@ void PasswordGenerationPopupViewViews::CreatePasswordView() {
return; return;
password_view_ = new PasswordBox(); password_view_ = new PasswordBox();
password_view_->Init(controller_->password(), password_view_->Init(controller_->password(), controller_->SuggestedText());
controller_->SuggestedText(),
font_list_);
password_view_->SetPosition(gfx::Point()); password_view_->SetPosition(gfx::Point());
password_view_->SizeToPreferredSize(); password_view_->SizeToPreferredSize();
AddChildView(password_view_); AddChildView(password_view_);
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/ui/autofill/password_generation_popup_view.h" #include "chrome/browser/ui/autofill/password_generation_popup_view.h"
#include "chrome/browser/ui/views/autofill/autofill_popup_base_view.h" #include "chrome/browser/ui/views/autofill/autofill_popup_base_view.h"
#include "ui/gfx/font_list.h"
#include "ui/views/controls/styled_label_listener.h" #include "ui/views/controls/styled_label_listener.h"
namespace views { namespace views {
...@@ -58,9 +57,6 @@ class PasswordGenerationPopupViewViews : public AutofillPopupBaseView, ...@@ -58,9 +57,6 @@ class PasswordGenerationPopupViewViews : public AutofillPopupBaseView,
PasswordBox* password_view_; PasswordBox* password_view_;
views::StyledLabel* help_label_; views::StyledLabel* help_label_;
// Fonts to use.
gfx::FontList font_list_;
// Size of the divider between the password and the help text. // Size of the divider between the password and the help text.
gfx::Rect divider_bounds_; gfx::Rect divider_bounds_;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_controller.h" #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_controller.h"
#include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_footnote_delegate.h" #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_footnote_delegate.h"
#include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_util.h" #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_util.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
...@@ -34,13 +35,11 @@ DesktopIOSPromotionFootnoteView::DesktopIOSPromotionFootnoteView( ...@@ -34,13 +35,11 @@ DesktopIOSPromotionFootnoteView::DesktopIOSPromotionFootnoteView(
IDS_BOOKMARK_FOOTNOTE_DESKTOP_TO_IOS_PROMO_MESSAGE, link_text, &offset); IDS_BOOKMARK_FOOTNOTE_DESKTOP_TO_IOS_PROMO_MESSAGE, link_text, &offset);
SetText(promo_text); SetText(promo_text);
views::StyledLabel::RangeStyleInfo link_style = AddStyleRange(gfx::Range(offset, offset + link_text.length()),
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink());
link_style.font_style = gfx::Font::NORMAL;
AddStyleRange(gfx::Range(offset, offset + link_text.length()), link_style);
views::StyledLabel::RangeStyleInfo promo_style; views::StyledLabel::RangeStyleInfo promo_style;
promo_style.color = SkColorSetRGB(102, 102, 102); promo_style.text_style = STYLE_SECONDARY;
gfx::Range before_link_range(0, offset); gfx::Range before_link_range(0, offset);
if (!before_link_range.is_empty()) if (!before_link_range.is_empty())
AddStyleRange(before_link_range, promo_style); AddStyleRange(before_link_range, promo_style);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h" #include "components/constrained_window/constrained_window_views.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
...@@ -108,7 +109,7 @@ RequestFileSystemDialogView::RequestFileSystemDialogView( ...@@ -108,7 +109,7 @@ RequestFileSystemDialogView::RequestFileSystemDialogView(
views::StyledLabel* const label = new views::StyledLabel(message, nullptr); views::StyledLabel* const label = new views::StyledLabel(message, nullptr);
views::StyledLabel::RangeStyleInfo bold_style; views::StyledLabel::RangeStyleInfo bold_style;
bold_style.weight = gfx::Font::Weight::BOLD; bold_style.text_style = STYLE_EMPHASIZED;
DCHECK_EQ(2u, placeholder_offsets.size()); DCHECK_EQ(2u, placeholder_offsets.size());
label->AddStyleRange(gfx::Range(placeholder_offsets[0], label->AddStyleRange(gfx::Range(placeholder_offsets[0],
......
...@@ -306,9 +306,6 @@ void BubbleHeaderView::SetDetails(const base::string16& details_text) { ...@@ -306,9 +306,6 @@ void BubbleHeaderView::SetDetails(const base::string16& details_text) {
views::StyledLabel::RangeStyleInfo link_style = views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink();
if (!ui::MaterialDesignController::IsSecondaryUiMaterial()) {
link_style.font_style |= gfx::Font::FontStyle::UNDERLINE;
}
link_style.disable_line_wrapping = false; link_style.disable_line_wrapping = false;
security_details_label_->AddStyleRange(details_range, link_style); security_details_label_->AddStyleRange(details_range, link_style);
...@@ -333,9 +330,6 @@ void BubbleHeaderView::AddResetDecisionsLabel() { ...@@ -333,9 +330,6 @@ void BubbleHeaderView::AddResetDecisionsLabel() {
views::StyledLabel::RangeStyleInfo link_style = views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink();
if (!ui::MaterialDesignController::IsSecondaryUiMaterial()) {
link_style.font_style |= gfx::Font::FontStyle::UNDERLINE;
}
link_style.disable_line_wrapping = false; link_style.disable_line_wrapping = false;
reset_cert_decisions_label_->AddStyleRange(link_range, link_style); reset_cert_decisions_label_->AddStyleRange(link_range, link_style);
...@@ -745,6 +739,7 @@ void PageInfoBubbleView::SetPermissionInfo( ...@@ -745,6 +739,7 @@ void PageInfoBubbleView::SetPermissionInfo(
site_settings_link->set_id( site_settings_link->set_id(
PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_SITE_SETTINGS); PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_SITE_SETTINGS);
site_settings_link->set_listener(this); site_settings_link->set_listener(this);
site_settings_link->SetUnderline(false);
views::View* link_section = new views::View(); views::View* link_section = new views::View();
const int kLinkMarginTop = 4; const int kLinkMarginTop = 4;
link_section->SetLayoutManager(new views::BoxLayout( link_section->SetLayoutManager(new views::BoxLayout(
...@@ -784,6 +779,7 @@ void PageInfoBubbleView::SetIdentityInfo(const IdentityInfo& identity_info) { ...@@ -784,6 +779,7 @@ void PageInfoBubbleView::SetIdentityInfo(const IdentityInfo& identity_info) {
certificate_viewer_link->set_id( certificate_viewer_link->set_id(
PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_CERTIFICATE_VIEWER); PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_CERTIFICATE_VIEWER);
certificate_viewer_link->set_listener(this); certificate_viewer_link->set_listener(this);
certificate_viewer_link->SetUnderline(false);
if (valid_identity) { if (valid_identity) {
certificate_viewer_link->SetTooltipText(l10n_util::GetStringFUTF16( certificate_viewer_link->SetTooltipText(l10n_util::GetStringFUTF16(
IDS_PAGE_INFO_CERTIFICATE_VALID_LINK_TOOLTIP, IDS_PAGE_INFO_CERTIFICATE_VALID_LINK_TOOLTIP,
...@@ -823,6 +819,7 @@ views::View* PageInfoBubbleView::CreateSiteSettingsView(int side_margin) { ...@@ -823,6 +819,7 @@ views::View* PageInfoBubbleView::CreateSiteSettingsView(int side_margin) {
cookie_dialog_link_->set_id( cookie_dialog_link_->set_id(
PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_COOKIE_DIALOG); PageInfoBubbleView::VIEW_ID_PAGE_INFO_LINK_COOKIE_DIALOG);
cookie_dialog_link_->set_listener(this); cookie_dialog_link_->set_listener(this);
cookie_dialog_link_->SetUnderline(false);
PageInfoUI::PermissionInfo info; PageInfoUI::PermissionInfo info;
info.type = CONTENT_SETTINGS_TYPE_COOKIES; info.type = CONTENT_SETTINGS_TYPE_COOKIES;
......
...@@ -125,8 +125,7 @@ void AccountChooserDialogView::AddedToWidget() { ...@@ -125,8 +125,7 @@ void AccountChooserDialogView::AddedToWidget() {
controller_->GetAccoutChooserTitle(); controller_->GetAccoutChooserTitle();
std::unique_ptr<views::StyledLabel> title_label = std::unique_ptr<views::StyledLabel> title_label =
base::MakeUnique<views::StyledLabel>(title_content.first, this); base::MakeUnique<views::StyledLabel>(title_content.first, this);
title_label->SetBaseFontList(views::style::GetFont( title_label->SetTextContext(views::style::CONTEXT_DIALOG_TITLE);
views::style::CONTEXT_DIALOG_TITLE, views::style::STYLE_PRIMARY));
if (!title_content.second.is_empty()) if (!title_content.second.is_empty())
title_label->AddStyleRange(title_content.second, GetLinkStyle()); title_label->AddStyleRange(title_content.second, GetLinkStyle());
GetBubbleFrameView()->SetTitleView(std::move(title_label)); GetBubbleFrameView()->SetTitleView(std::move(title_label));
......
...@@ -53,14 +53,6 @@ void AutoSigninFirstRunDialogView::WindowClosing() { ...@@ -53,14 +53,6 @@ void AutoSigninFirstRunDialogView::WindowClosing() {
controller_->OnCloseDialog(); controller_->OnCloseDialog();
} }
void AutoSigninFirstRunDialogView::OnNativeThemeChanged(
const ui::NativeTheme* theme) {
views::StyledLabel::RangeStyleInfo default_style;
default_style.color =
views::style::GetColor(CONTEXT_BODY_TEXT_LARGE, STYLE_SECONDARY, theme);
text_->SetDefaultStyle(default_style);
}
bool AutoSigninFirstRunDialogView::Cancel() { bool AutoSigninFirstRunDialogView::Cancel() {
controller_->OnAutoSigninTurnOff(); controller_->OnAutoSigninTurnOff();
return true; return true;
...@@ -99,9 +91,8 @@ void AutoSigninFirstRunDialogView::InitWindow() { ...@@ -99,9 +91,8 @@ void AutoSigninFirstRunDialogView::InitWindow() {
std::pair<base::string16, gfx::Range> text_content = std::pair<base::string16, gfx::Range> text_content =
controller_->GetAutoSigninText(); controller_->GetAutoSigninText();
text_ = new views::StyledLabel(text_content.first, this); text_ = new views::StyledLabel(text_content.first, this);
text_->SetBaseFontList( text_->SetTextContext(CONTEXT_BODY_TEXT_LARGE);
views::style::GetFont(CONTEXT_BODY_TEXT_LARGE, STYLE_SECONDARY)); text_->SetDefaultTextStyle(STYLE_SECONDARY);
OnNativeThemeChanged(GetNativeTheme());
if (!text_content.second.is_empty()) { if (!text_content.second.is_empty()) {
text_->AddStyleRange(text_content.second, text_->AddStyleRange(text_content.second,
views::StyledLabel::RangeStyleInfo::CreateForLink()); views::StyledLabel::RangeStyleInfo::CreateForLink());
......
...@@ -32,7 +32,6 @@ class AutoSigninFirstRunDialogView : public views::DialogDelegateView, ...@@ -32,7 +32,6 @@ class AutoSigninFirstRunDialogView : public views::DialogDelegateView,
base::string16 GetWindowTitle() const override; base::string16 GetWindowTitle() const override;
bool ShouldShowCloseButton() const override; bool ShouldShowCloseButton() const override;
void WindowClosing() override; void WindowClosing() override;
void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
bool Cancel() override; bool Cancel() override;
bool Accept() override; bool Accept() override;
bool Close() override; bool Close() override;
......
...@@ -712,8 +712,7 @@ ManagePasswordsBubbleView::SaveConfirmationView::SaveConfirmationView( ...@@ -712,8 +712,7 @@ ManagePasswordsBubbleView::SaveConfirmationView::SaveConfirmationView(
views::StyledLabel* confirmation = views::StyledLabel* confirmation =
new views::StyledLabel(parent_->model()->save_confirmation_text(), this); new views::StyledLabel(parent_->model()->save_confirmation_text(), this);
confirmation->SetBaseFontList(views::style::GetFont( confirmation->SetTextContext(CONTEXT_DEPRECATED_SMALL);
CONTEXT_DEPRECATED_SMALL, views::style::STYLE_PRIMARY));
confirmation->AddStyleRange(parent_->model()->save_confirmation_link_range(), confirmation->AddStyleRange(parent_->model()->save_confirmation_link_range(),
GetLinkStyle()); GetLinkStyle());
...@@ -1019,8 +1018,7 @@ void ManagePasswordsBubbleView::CloseBubble() { ...@@ -1019,8 +1018,7 @@ void ManagePasswordsBubbleView::CloseBubble() {
void ManagePasswordsBubbleView::AddedToWidget() { void ManagePasswordsBubbleView::AddedToWidget() {
auto title_view = auto title_view =
base::MakeUnique<views::StyledLabel>(base::string16(), this); base::MakeUnique<views::StyledLabel>(base::string16(), this);
title_view->SetBaseFontList(views::style::GetFont( title_view->SetTextContext(views::style::CONTEXT_DIALOG_TITLE);
views::style::CONTEXT_DIALOG_TITLE, views::style::STYLE_PRIMARY));
UpdateTitleText(title_view.get()); UpdateTitleText(title_view.get());
GetBubbleFrameView()->SetTitleView(std::move(title_view)); GetBubbleFrameView()->SetTitleView(std::move(title_view));
} }
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h" #include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h" #include "ui/gfx/color_utils.h"
...@@ -959,15 +960,15 @@ std::unique_ptr<views::View> PaymentSheetViewController::CreateDataSourceRow() { ...@@ -959,15 +960,15 @@ std::unique_ptr<views::View> PaymentSheetViewController::CreateDataSourceRow() {
base::MakeUnique<views::StyledLabel>(data_source, this); base::MakeUnique<views::StyledLabel>(data_source, this);
data_source_label->SetBorder(views::CreateEmptyBorder(22, 0, 0, 0)); data_source_label->SetBorder(views::CreateEmptyBorder(22, 0, 0, 0));
data_source_label->set_id(static_cast<int>(DialogViewID::DATA_SOURCE_LABEL)); data_source_label->set_id(static_cast<int>(DialogViewID::DATA_SOURCE_LABEL));
data_source_label->SetDefaultTextStyle(views::style::STYLE_DISABLED);
views::StyledLabel::RangeStyleInfo default_style;
default_style.color = data_source_label->GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LabelDisabledColor);
data_source_label->SetDefaultStyle(default_style);
views::StyledLabel::RangeStyleInfo link_style = views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink();
link_style.color = gfx::kGoogleBlue700;
// This is the harmony color, so not needed when MD is default.
if (ui::MaterialDesignController::IsSecondaryUiMaterial())
link_style.override_color = gfx::kGoogleBlue700;
data_source_label->AddStyleRange( data_source_label->AddStyleRange(
gfx::Range(link_begin, link_begin + link_length), link_style); gfx::Range(link_begin, link_begin + link_length), link_style);
data_source_label->SizeToFit(0); data_source_label->SizeToFit(0);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "net/ssl/client_cert_identity.h" #include "net/ssl/client_cert_identity.h"
#include "net/ssl/ssl_private_key.h" #include "net/ssl/ssl_private_key.h"
...@@ -85,7 +86,7 @@ void PlatformKeysCertificateSelector::Init() { ...@@ -85,7 +86,7 @@ void PlatformKeysCertificateSelector::Init() {
new views::StyledLabel(text, nullptr /* no listener */)); new views::StyledLabel(text, nullptr /* no listener */));
views::StyledLabel::RangeStyleInfo bold_style; views::StyledLabel::RangeStyleInfo bold_style;
bold_style.weight = gfx::Font::Weight::BOLD; bold_style.text_style = STYLE_EMPHASIZED;
label->AddStyleRange(gfx::Range(offset, offset + name.size()), bold_style); label->AddStyleRange(gfx::Range(offset, offset + name.size()), bold_style);
CertificateSelector::InitWithText(std::move(label)); CertificateSelector::InitWithText(std::move(label));
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h" #include "components/constrained_window/constrained_window_views.h"
...@@ -174,7 +175,7 @@ void ForcedReauthenticationDialogView::AddedToWidget() { ...@@ -174,7 +175,7 @@ void ForcedReauthenticationDialogView::AddedToWidget() {
prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color); prompt_label->SetDisplayedOnBackgroundColor(prompt_bar_background_color);
views::StyledLabel::RangeStyleInfo bold_style; views::StyledLabel::RangeStyleInfo bold_style;
bold_style.weight = gfx::Font::Weight::BOLD; bold_style.text_style = STYLE_EMPHASIZED;
prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()), prompt_label->AddStyleRange(gfx::Range(offset, offset + domain.size()),
bold_style); bold_style);
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "chrome/browser/ui/singleton_tabs.h" #include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/browser/ui/user_manager.h" #include "chrome/browser/ui/user_manager.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/browser/ui/views/profiles/signin_view_controller_delegate_views.h" #include "chrome/browser/ui/views/profiles/signin_view_controller_delegate_views.h"
#include "chrome/browser/ui/views/profiles/user_manager_view.h" #include "chrome/browser/ui/views/profiles/user_manager_view.h"
#include "chrome/browser/ui/webui/signin/login_ui_service.h" #include "chrome/browser/ui/webui/signin/login_ui_service.h"
...@@ -433,12 +434,9 @@ class TitleCard : public views::View { ...@@ -433,12 +434,9 @@ class TitleCard : public views::View {
back_button_ = CreateBackButton(listener); back_button_ = CreateBackButton(listener);
*back_button = back_button_; *back_button = back_button_;
title_label_ = new views::Label(message); title_label_ =
new views::Label(message, views::style::CONTEXT_DIALOG_TITLE);
title_label_->SetHorizontalAlignment(gfx::ALIGN_CENTER); title_label_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
const gfx::FontList& medium_font_list =
rb->GetFontList(ui::ResourceBundle::MediumFont);
title_label_->SetFontList(medium_font_list);
AddChildView(back_button_); AddChildView(back_button_);
AddChildView(title_label_); AddChildView(title_label_);
...@@ -1349,12 +1347,10 @@ views::View* ProfileChooserView::CreateSupervisedUserDisclaimerView() { ...@@ -1349,12 +1347,10 @@ views::View* ProfileChooserView::CreateSupervisedUserDisclaimerView() {
kMenuEdgeMargin, horizontal_margin)); kMenuEdgeMargin, horizontal_margin));
views::Label* disclaimer = new views::Label( views::Label* disclaimer = new views::Label(
avatar_menu_->GetSupervisedUserInformation()); avatar_menu_->GetSupervisedUserInformation(), CONTEXT_DEPRECATED_SMALL);
disclaimer->SetMultiLine(true); disclaimer->SetMultiLine(true);
disclaimer->SetAllowCharacterBreak(true); disclaimer->SetAllowCharacterBreak(true);
disclaimer->SetHorizontalAlignment(gfx::ALIGN_LEFT); disclaimer->SetHorizontalAlignment(gfx::ALIGN_LEFT);
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
disclaimer->SetFontList(rb->GetFontList(ui::ResourceBundle::SmallFont));
layout->StartRow(1, 0); layout->StartRow(1, 0);
layout->AddView(disclaimer); layout->AddView(disclaimer);
...@@ -1494,9 +1490,6 @@ views::View* ProfileChooserView::CreateAccountRemovalView() { ...@@ -1494,9 +1490,6 @@ views::View* ProfileChooserView::CreateAccountRemovalView() {
// Adds main text. // Adds main text.
layout->StartRowWithPadding(1, 0, 0, unrelated_vertical_spacing); layout->StartRowWithPadding(1, 0, 0, unrelated_vertical_spacing);
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
const gfx::FontList& small_font_list =
rb->GetFontList(ui::ResourceBundle::SmallFont);
if (is_primary_account) { if (is_primary_account) {
std::string email = signin_ui_util::GetDisplayEmail(browser_->profile(), std::string email = signin_ui_util::GetDisplayEmail(browser_->profile(),
...@@ -1512,14 +1505,14 @@ views::View* ProfileChooserView::CreateAccountRemovalView() { ...@@ -1512,14 +1505,14 @@ views::View* ProfileChooserView::CreateAccountRemovalView() {
primary_account_removal_label->AddStyleRange( primary_account_removal_label->AddStyleRange(
gfx::Range(offsets[1], offsets[1] + settings_text.size()), gfx::Range(offsets[1], offsets[1] + settings_text.size()),
views::StyledLabel::RangeStyleInfo::CreateForLink()); views::StyledLabel::RangeStyleInfo::CreateForLink());
primary_account_removal_label->SetBaseFontList(small_font_list); primary_account_removal_label->SetTextContext(CONTEXT_DEPRECATED_SMALL);
layout->AddView(primary_account_removal_label); layout->AddView(primary_account_removal_label);
} else { } else {
views::Label* content_label = new views::Label( views::Label* content_label = new views::Label(
l10n_util::GetStringUTF16(IDS_PROFILES_ACCOUNT_REMOVAL_TEXT)); l10n_util::GetStringUTF16(IDS_PROFILES_ACCOUNT_REMOVAL_TEXT),
CONTEXT_DEPRECATED_SMALL);
content_label->SetMultiLine(true); content_label->SetMultiLine(true);
content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
content_label->SetFontList(small_font_list);
layout->AddView(content_label); layout->AddView(content_label);
} }
...@@ -1561,17 +1554,14 @@ views::View* ProfileChooserView::CreateSwitchUserView() { ...@@ -1561,17 +1554,14 @@ views::View* ProfileChooserView::CreateSwitchUserView() {
// Adds main text. // Adds main text.
layout->StartRowWithPadding(1, 1, 0, unrelated_vertical_spacing); layout->StartRowWithPadding(1, 1, 0, unrelated_vertical_spacing);
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
const gfx::FontList& small_font_list =
rb->GetFontList(ui::ResourceBundle::SmallFont);
const AvatarMenu::Item& avatar_item = const AvatarMenu::Item& avatar_item =
avatar_menu_->GetItemAt(avatar_menu_->GetActiveProfileIndex()); avatar_menu_->GetItemAt(avatar_menu_->GetActiveProfileIndex());
views::Label* content_label = new views::Label( views::Label* content_label =
l10n_util::GetStringFUTF16( new views::Label(l10n_util::GetStringFUTF16(
IDS_PROFILES_NOT_YOU_CONTENT_TEXT, avatar_item.name)); IDS_PROFILES_NOT_YOU_CONTENT_TEXT, avatar_item.name),
CONTEXT_DEPRECATED_SMALL);
content_label->SetMultiLine(true); content_label->SetMultiLine(true);
content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); content_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
content_label->SetFontList(small_font_list);
layout->AddView(content_label); layout->AddView(content_label);
// Adds "Add person" button. // Adds "Add person" button.
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "chrome/browser/ui/browser_list_observer.h" #include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/browser/ui/views/toolbar/app_menu_button.h" #include "chrome/browser/ui/views/toolbar/app_menu_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h" #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
...@@ -51,9 +52,6 @@ const int kWidthOfDescriptionText = 320; ...@@ -51,9 +52,6 @@ const int kWidthOfDescriptionText = 320;
// Distance between checkbox and the text to the right of it. // Distance between checkbox and the text to the right of it.
const int kCheckboxTextDistance = 4; const int kCheckboxTextDistance = 4;
// The color of the text of the sub panel to offer UMA opt-in.
const SkColor kTextColor = SkColorSetRGB(102, 102, 102);
enum SessionCrashedBubbleHistogramValue { enum SessionCrashedBubbleHistogramValue {
SESSION_CRASHED_BUBBLE_SHOWN, SESSION_CRASHED_BUBBLE_SHOWN,
SESSION_CRASHED_BUBBLE_ERROR, SESSION_CRASHED_BUBBLE_ERROR,
...@@ -229,13 +227,10 @@ views::View* SessionCrashedBubbleView::CreateFootnoteView() { ...@@ -229,13 +227,10 @@ views::View* SessionCrashedBubbleView::CreateFootnoteView() {
link_text, link_text,
&offset); &offset);
views::StyledLabel* uma_label = new views::StyledLabel(uma_text, this); views::StyledLabel* uma_label = new views::StyledLabel(uma_text, this);
views::StyledLabel::RangeStyleInfo link_style =
views::StyledLabel::RangeStyleInfo::CreateForLink();
link_style.font_style = gfx::Font::NORMAL;
uma_label->AddStyleRange(gfx::Range(offset, offset + link_text.length()), uma_label->AddStyleRange(gfx::Range(offset, offset + link_text.length()),
link_style); views::StyledLabel::RangeStyleInfo::CreateForLink());
views::StyledLabel::RangeStyleInfo uma_style; views::StyledLabel::RangeStyleInfo uma_style;
uma_style.color = kTextColor; uma_style.text_style = STYLE_SECONDARY;
gfx::Range before_link_range(0, offset); gfx::Range before_link_range(0, offset);
if (!before_link_range.is_empty()) if (!before_link_range.is_empty())
uma_label->AddStyleRange(before_link_range, uma_style); uma_label->AddStyleRange(before_link_range, uma_style);
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "components/constrained_window/constrained_window_views.h" #include "components/constrained_window/constrained_window_views.h"
#include "ui/gfx/font.h" #include "ui/gfx/font.h"
#include "ui/gfx/geometry/insets.h" #include "ui/gfx/geometry/insets.h"
...@@ -47,7 +48,7 @@ SettingsResetPromptDialog::SettingsResetPromptDialog( ...@@ -47,7 +48,7 @@ SettingsResetPromptDialog::SettingsResetPromptDialog(
views::StyledLabel* dialog_label = views::StyledLabel* dialog_label =
new views::StyledLabel(controller_->GetMainText(), /*listener=*/nullptr); new views::StyledLabel(controller_->GetMainText(), /*listener=*/nullptr);
views::StyledLabel::RangeStyleInfo url_style; views::StyledLabel::RangeStyleInfo url_style;
url_style.weight = gfx::Font::Weight::BOLD; url_style.text_style = STYLE_EMPHASIZED;
dialog_label->AddStyleRange(controller_->GetMainTextUrlRange(), url_style); dialog_label->AddStyleRange(controller_->GetMainTextUrlRange(), url_style);
AddChildView(dialog_label); AddChildView(dialog_label);
} }
......
...@@ -152,11 +152,9 @@ SubtleNotificationView::SubtleNotificationView( ...@@ -152,11 +152,9 @@ SubtleNotificationView::SubtleNotificationView(
instruction_view_ = instruction_view_ =
new InstructionView(base::string16(), kForegroundColor, kBackgroundColor); new InstructionView(base::string16(), kForegroundColor, kBackgroundColor);
link_ = new views::Link(); link_ = new views::Link(base::string16(), kInstructionTextContext);
link_->SetFocusBehavior(FocusBehavior::NEVER); link_->SetFocusBehavior(FocusBehavior::NEVER);
link_->set_listener(link_listener); link_->set_listener(link_listener);
link_->SetFontList(
views::style::GetFont(kInstructionTextContext, views::style::STYLE_LINK));
link_->SetEnabledColor(kForegroundColor); // Override STYLE_LINK. link_->SetEnabledColor(kForegroundColor); // Override STYLE_LINK.
link_->SetBackgroundColor(kBackgroundColor); link_->SetBackgroundColor(kBackgroundColor);
link_->SetVisible(false); link_->SetVisible(false);
......
...@@ -8,19 +8,13 @@ ...@@ -8,19 +8,13 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/ui/sync/bubble_sync_promo_delegate.h" #include "chrome/browser/ui/sync/bubble_sync_promo_delegate.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/font.h" #include "ui/gfx/font.h"
#include "ui/views/controls/styled_label.h" #include "ui/views/controls/styled_label.h"
#include "ui/views/layout/fill_layout.h" #include "ui/views/layout/fill_layout.h"
namespace {
// Color of the text of the promo.
const SkColor kTextColor = SkColorSetRGB(102, 102, 102);
} // namespace
BubbleSyncPromoView::BubbleSyncPromoView(BubbleSyncPromoDelegate* delegate, BubbleSyncPromoView::BubbleSyncPromoView(BubbleSyncPromoDelegate* delegate,
int link_text_resource_id, int link_text_resource_id,
int message_text_resource_id) int message_text_resource_id)
...@@ -31,13 +25,11 @@ BubbleSyncPromoView::BubbleSyncPromoView(BubbleSyncPromoDelegate* delegate, ...@@ -31,13 +25,11 @@ BubbleSyncPromoView::BubbleSyncPromoView(BubbleSyncPromoDelegate* delegate,
l10n_util::GetStringFUTF16(message_text_resource_id, link_text, &offset); l10n_util::GetStringFUTF16(message_text_resource_id, link_text, &offset);
SetText(promo_text); SetText(promo_text);
views::StyledLabel::RangeStyleInfo link_style = AddStyleRange(gfx::Range(offset, offset + link_text.length()),
views::StyledLabel::RangeStyleInfo::CreateForLink(); views::StyledLabel::RangeStyleInfo::CreateForLink());
link_style.font_style = gfx::Font::NORMAL;
AddStyleRange(gfx::Range(offset, offset + link_text.length()), link_style);
views::StyledLabel::RangeStyleInfo promo_style; views::StyledLabel::RangeStyleInfo promo_style;
promo_style.color = kTextColor; promo_style.text_style = STYLE_SECONDARY;
gfx::Range before_link_range(0, offset); gfx::Range before_link_range(0, offset);
if (!before_link_range.is_empty()) if (!before_link_range.is_empty())
AddStyleRange(before_link_range, promo_style); AddStyleRange(before_link_range, promo_style);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "chrome/browser/ui/browser_navigator_params.h" #include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h"
#include "chrome/browser/ui/views/harmony/chrome_typography.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h" #include "components/constrained_window/constrained_window_views.h"
...@@ -169,7 +170,7 @@ void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged( ...@@ -169,7 +170,7 @@ void ProfileSigninConfirmationDialogViews::ViewHierarchyChanged(
prompt_label->SetDisplayedOnBackgroundColor(kPromptBarBackgroundColor); prompt_label->SetDisplayedOnBackgroundColor(kPromptBarBackgroundColor);
views::StyledLabel::RangeStyleInfo bold_style; views::StyledLabel::RangeStyleInfo bold_style;
bold_style.weight = gfx::Font::Weight::BOLD; bold_style.text_style = STYLE_EMPHASIZED;
prompt_label->AddStyleRange( prompt_label->AddStyleRange(
gfx::Range(offset, offset + domain.size()), bold_style); gfx::Range(offset, offset + domain.size()), bold_style);
......
...@@ -27,10 +27,8 @@ namespace views { ...@@ -27,10 +27,8 @@ namespace views {
const char Link::kViewClassName[] = "Link"; const char Link::kViewClassName[] = "Link";
constexpr int Link::kFocusBorderPadding; constexpr int Link::kFocusBorderPadding;
Link::Link() : Link(base::string16()) {} Link::Link(const base::string16& title, int text_context, int text_style)
: Label(title, text_context, text_style),
Link::Link(const base::string16& title)
: Label(title),
requested_enabled_color_(gfx::kPlaceholderColor), requested_enabled_color_(gfx::kPlaceholderColor),
requested_enabled_color_set_(false) { requested_enabled_color_set_(false) {
Init(); Init();
...@@ -268,6 +266,7 @@ void Link::ConfigureFocus() { ...@@ -268,6 +266,7 @@ void Link::ConfigureFocus() {
} }
SkColor Link::GetColor() { SkColor Link::GetColor() {
// TODO(tapted): Use style::GetColor().
const ui::NativeTheme* theme = GetNativeTheme(); const ui::NativeTheme* theme = GetNativeTheme();
DCHECK(theme); DCHECK(theme);
if (!enabled()) if (!enabled())
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
#include "ui/views/style/typography.h"
namespace views { namespace views {
...@@ -37,8 +38,9 @@ class VIEWS_EXPORT Link : public Label { ...@@ -37,8 +38,9 @@ class VIEWS_EXPORT Link : public Label {
RING, // A focus ring is drawn around the View. RING, // A focus ring is drawn around the View.
}; };
Link(); explicit Link(const base::string16& title,
explicit Link(const base::string16& title); int text_context = style::CONTEXT_LABEL,
int text_style = style::STYLE_LINK);
~Link() override; ~Link() override;
// Returns the default FocusStyle for a views::Link. Calling SetUnderline() // Returns the default FocusStyle for a views::Link. Calling SetUnderline()
......
...@@ -124,10 +124,11 @@ void MessageBoxView::SetLink(const base::string16& text, ...@@ -124,10 +124,11 @@ void MessageBoxView::SetLink(const base::string16& text,
} else { } else {
DCHECK(listener); DCHECK(listener);
if (!link_) { if (!link_) {
link_ = new Link(); link_ = new Link(text);
link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
} else {
link_->SetText(text);
} }
link_->SetText(text);
link_->set_listener(listener); link_->set_listener(listener);
} }
ResetLayoutManager(); ResetLayoutManager();
......
...@@ -30,52 +30,37 @@ gfx::Insets FocusBorderInsets(const Label& label) { ...@@ -30,52 +30,37 @@ gfx::Insets FocusBorderInsets(const Label& label) {
return label.GetInsets(); return label.GetInsets();
} }
// Calculates the height of a line of text. Currently returns the height of
// a label.
int CalculateLineHeight(const gfx::FontList& font_list) {
Label label;
label.SetFontList(font_list);
return label.GetPreferredSize().height();
}
std::unique_ptr<Label> CreateLabelRange( std::unique_ptr<Label> CreateLabelRange(
const base::string16& text, const base::string16& text,
const gfx::FontList& font_list, int text_context,
int default_style,
const StyledLabel::RangeStyleInfo& style_info, const StyledLabel::RangeStyleInfo& style_info,
views::LinkListener* link_listener) { views::LinkListener* link_listener) {
std::unique_ptr<Label> result; std::unique_ptr<Label> result;
if (style_info.is_link) { if (style_info.IsLink()) {
Link* link = new Link(text); // Nothing should (and nothing does) use a custom font for links.
DCHECK(!style_info.custom_font);
// Note this ignores |default_style|, in favor of style::STYLE_LINK.
Link* link = new Link(text, text_context);
link->set_listener(link_listener); link->set_listener(link_listener);
// StyledLabel makes assumptions about how to inset the entire View based on // Links in a StyledLabel do not get underlines.
// the default focus style: If focus rings are not the default, nothing is link->SetUnderline(false);
// inset. So an individual range can't deviate from that.
if (Link::GetDefaultFocusStyle() == Link::FocusStyle::UNDERLINE) {
// Nothing should (and nothing does) request underlines for links with MD.
DCHECK_EQ(0, style_info.font_style & gfx::Font::UNDERLINE);
link->SetUnderline(false); // Override what Link::Init() does.
} else {
link->SetUnderline((style_info.font_style & gfx::Font::UNDERLINE) != 0);
}
result.reset(link); result.reset(link);
} else if (style_info.custom_font) {
result.reset(new Label(text, {style_info.custom_font.value()}));
} else { } else {
result.reset(new Label(text)); result.reset(new Label(text, text_context,
style_info.text_style.value_or(default_style)));
} }
if (style_info.override_color != SK_ColorTRANSPARENT)
if (style_info.color != SK_ColorTRANSPARENT) result->SetEnabledColor(style_info.override_color);
result->SetEnabledColor(style_info.color);
result->SetFontList(font_list);
if (!style_info.tooltip.empty()) if (!style_info.tooltip.empty())
result->SetTooltipText(style_info.tooltip); result->SetTooltipText(style_info.tooltip);
if (style_info.font_style != gfx::Font::NORMAL ||
style_info.weight != gfx::Font::Weight::NORMAL) {
result->SetFontList(result->font_list().Derive(0, style_info.font_style,
style_info.weight));
}
return result; return result;
} }
...@@ -84,12 +69,9 @@ std::unique_ptr<Label> CreateLabelRange( ...@@ -84,12 +69,9 @@ std::unique_ptr<Label> CreateLabelRange(
// StyledLabel::RangeStyleInfo ------------------------------------------------ // StyledLabel::RangeStyleInfo ------------------------------------------------
StyledLabel::RangeStyleInfo::RangeStyleInfo() StyledLabel::RangeStyleInfo::RangeStyleInfo() = default;
: font_style(gfx::Font::NORMAL), StyledLabel::RangeStyleInfo::RangeStyleInfo(const RangeStyleInfo& copy) =
weight(gfx::Font::Weight::NORMAL), default;
color(SK_ColorTRANSPARENT),
disable_line_wrapping(false),
is_link(false) {}
StyledLabel::RangeStyleInfo::~RangeStyleInfo() {} StyledLabel::RangeStyleInfo::~RangeStyleInfo() {}
...@@ -97,10 +79,13 @@ StyledLabel::RangeStyleInfo::~RangeStyleInfo() {} ...@@ -97,10 +79,13 @@ StyledLabel::RangeStyleInfo::~RangeStyleInfo() {}
StyledLabel::RangeStyleInfo StyledLabel::RangeStyleInfo::CreateForLink() { StyledLabel::RangeStyleInfo StyledLabel::RangeStyleInfo::CreateForLink() {
RangeStyleInfo result; RangeStyleInfo result;
result.disable_line_wrapping = true; result.disable_line_wrapping = true;
result.is_link = true; result.text_style = style::STYLE_LINK;
return result; return result;
} }
bool StyledLabel::RangeStyleInfo::IsLink() const {
return text_style && text_style.value() == style::STYLE_LINK;
}
// StyledLabel::StyleRange ---------------------------------------------------- // StyledLabel::StyleRange ----------------------------------------------------
...@@ -117,8 +102,7 @@ const char StyledLabel::kViewClassName[] = "StyledLabel"; ...@@ -117,8 +102,7 @@ const char StyledLabel::kViewClassName[] = "StyledLabel";
StyledLabel::StyledLabel(const base::string16& text, StyledLabel::StyledLabel(const base::string16& text,
StyledLabelListener* listener) StyledLabelListener* listener)
: font_list_(Label().font_list()), : specified_line_height_(0),
specified_line_height_(0),
listener_(listener), listener_(listener),
width_at_last_size_calculation_(0), width_at_last_size_calculation_(0),
width_at_last_layout_(0), width_at_last_layout_(0),
...@@ -137,9 +121,8 @@ void StyledLabel::SetText(const base::string16& text) { ...@@ -137,9 +121,8 @@ void StyledLabel::SetText(const base::string16& text) {
PreferredSizeChanged(); PreferredSizeChanged();
} }
void StyledLabel::SetBaseFontList(const gfx::FontList& font_list) { gfx::FontList StyledLabel::GetDefaultFontList() const {
font_list_ = font_list; return style::GetFont(text_context_, default_text_style_);
PreferredSizeChanged();
} }
void StyledLabel::AddStyleRange(const gfx::Range& range, void StyledLabel::AddStyleRange(const gfx::Range& range,
...@@ -156,8 +139,19 @@ void StyledLabel::AddStyleRange(const gfx::Range& range, ...@@ -156,8 +139,19 @@ void StyledLabel::AddStyleRange(const gfx::Range& range,
PreferredSizeChanged(); PreferredSizeChanged();
} }
void StyledLabel::SetDefaultStyle(const RangeStyleInfo& style_info) { void StyledLabel::SetTextContext(int text_context) {
default_style_info_ = style_info; if (text_context_ == text_context)
return;
text_context_ = text_context;
PreferredSizeChanged();
}
void StyledLabel::SetDefaultTextStyle(int text_style) {
if (default_text_style_ == text_style)
return;
default_text_style_ = text_style;
PreferredSizeChanged(); PreferredSizeChanged();
} }
...@@ -201,7 +195,7 @@ gfx::Insets StyledLabel::GetInsets() const { ...@@ -201,7 +195,7 @@ gfx::Insets StyledLabel::GetInsets() const {
// That in turn will be true only if the link is non-empty. // That in turn will be true only if the link is non-empty.
for (StyleRanges::const_iterator i(style_ranges_.begin()); for (StyleRanges::const_iterator i(style_ranges_.begin());
i != style_ranges_.end(); ++i) { i != style_ranges_.end(); ++i) {
if (i->style_info.is_link && !i->range.is_empty()) { if (i->style_info.IsLink() && !i->range.is_empty()) {
insets += gfx::Insets(Link::kFocusBorderPadding); insets += gfx::Insets(Link::kFocusBorderPadding);
break; break;
} }
...@@ -238,6 +232,26 @@ void StyledLabel::LinkClicked(Link* source, int event_flags) { ...@@ -238,6 +232,26 @@ void StyledLabel::LinkClicked(Link* source, int event_flags) {
listener_->StyledLabelLinkClicked(this, link_targets_[source], event_flags); listener_->StyledLabelLinkClicked(this, link_targets_[source], event_flags);
} }
int StyledLabel::GetDefaultLineHeight() const {
return specified_line_height_ > 0
? specified_line_height_
: std::max(
style::GetLineHeight(text_context_, default_text_style_),
GetDefaultFontList().GetHeight());
}
gfx::FontList StyledLabel::GetFontListForRange(
const StyleRanges::const_iterator& range) const {
if (range == style_ranges_.end())
return GetDefaultFontList();
return range->style_info.custom_font
? range->style_info.custom_font.value()
: style::GetFont(
text_context_,
range->style_info.text_style.value_or(default_text_style_));
}
gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
if (width == width_at_last_size_calculation_ && if (width == width_at_last_size_calculation_ &&
(dry_run || width == width_at_last_layout_)) (dry_run || width == width_at_last_layout_))
...@@ -257,8 +271,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { ...@@ -257,8 +271,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
if (width <= 0 || text_.empty()) if (width <= 0 || text_.empty())
return gfx::Size(); return gfx::Size();
const int line_height = specified_line_height_ > 0 ? specified_line_height_ const int line_height = GetDefaultLineHeight();
: CalculateLineHeight(font_list_);
// The index of the line we're on. // The index of the line we're on.
int line = 0; int line = 0;
// The x position (in pixels) of the line we're on, relative to content // The x position (in pixels) of the line we're on, relative to content
...@@ -268,6 +282,9 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { ...@@ -268,6 +282,9 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
// The width that was actually used. Guaranteed to be no larger than |width|. // The width that was actually used. Guaranteed to be no larger than |width|.
int used_width = 0; int used_width = 0;
RangeStyleInfo default_style;
default_style.text_style = default_text_style_;
base::string16 remaining_string = text_; base::string16 remaining_string = text_;
StyleRanges::const_iterator current_range = style_ranges_.begin(); StyleRanges::const_iterator current_range = style_ranges_.begin();
...@@ -298,15 +315,12 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { ...@@ -298,15 +315,12 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height);
std::vector<base::string16> substrings; std::vector<base::string16> substrings;
gfx::FontList text_font_list = font_list_;
// If the start of the remaining text is inside a styled range, the font // If the start of the remaining text is inside a styled range, the font
// style may differ from the base font. The font specified by the range // style may differ from the base font. The font specified by the range
// should be used when eliding text. // should be used when eliding text.
if (position >= range.start()) { gfx::FontList text_font_list = position >= range.start()
text_font_list = ? GetFontListForRange(current_range)
text_font_list.Derive(0, current_range->style_info.font_style, : GetDefaultFontList();
current_range->style_info.weight);
}
gfx::ElideRectangleText(remaining_string, gfx::ElideRectangleText(remaining_string,
text_font_list, text_font_list,
chunk_bounds.width(), chunk_bounds.width(),
...@@ -347,9 +361,10 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { ...@@ -347,9 +361,10 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
if (chunk.size() > range.end() - position) if (chunk.size() > range.end() - position)
chunk = chunk.substr(0, range.end() - position); chunk = chunk.substr(0, range.end() - position);
label = CreateLabelRange(chunk, font_list_, style_info, this); label = CreateLabelRange(chunk, text_context_, default_text_style_,
style_info, this);
if (style_info.is_link && !dry_run) if (style_info.IsLink() && !dry_run)
link_targets_[label.get()] = range; link_targets_[label.get()] = range;
if (position + chunk.size() >= range.end()) if (position + chunk.size() >= range.end())
...@@ -358,7 +373,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { ...@@ -358,7 +373,8 @@ gfx::Size StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
// This chunk is normal text. // This chunk is normal text.
if (position + chunk.size() > range.start()) if (position + chunk.size() > range.start())
chunk = chunk.substr(0, range.start() - position); chunk = chunk.substr(0, range.start() - position);
label = CreateLabelRange(chunk, font_list_, default_style_info_, this); label = CreateLabelRange(chunk, text_context_, default_text_style_,
default_style, this);
} }
if (displayed_on_background_color_set_) if (displayed_on_background_color_set_)
......
...@@ -9,12 +9,14 @@ ...@@ -9,12 +9,14 @@
#include <map> #include <map>
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gfx/range/range.h" #include "ui/gfx/range/range.h"
#include "ui/views/controls/link_listener.h" #include "ui/views/controls/link_listener.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace views { namespace views {
...@@ -35,30 +37,31 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener { ...@@ -35,30 +37,31 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
// Parameters that define label style for a styled label's text range. // Parameters that define label style for a styled label's text range.
struct VIEWS_EXPORT RangeStyleInfo { struct VIEWS_EXPORT RangeStyleInfo {
RangeStyleInfo(); RangeStyleInfo();
RangeStyleInfo(const RangeStyleInfo& copy);
~RangeStyleInfo(); ~RangeStyleInfo();
// Creates a range style info with default values for link. // Creates a range style info with default values for link.
static RangeStyleInfo CreateForLink(); static RangeStyleInfo CreateForLink();
// The font style that will be applied to the range. Should be a bitmask of bool IsLink() const;
// values defined in gfx::Font::FontStyle (ITALIC, UNDERLINE).
int font_style;
// The font weight to be applied to the range. Default is Weight::NORMAL. // Allows full customization of the font used in the range. Ignores the
gfx::Font::Weight weight; // StyledLabel's default text context and |text_style|.
base::Optional<gfx::FontList> custom_font;
// The text color for the range. Default is SK_ColorTRANSPARENT, indicating // The style::TextStyle for this range.
// the theme's default color should be used. base::Optional<int> text_style;
SkColor color;
// Overrides the text color given by |text_style| for this range. Default is
// SK_ColorTRANSPARENT, indicating not to override.
// DEPRECATED: Use TextStyle.
SkColor override_color = SK_ColorTRANSPARENT;
// Tooltip for the range. // Tooltip for the range.
base::string16 tooltip; base::string16 tooltip;
// If set, the whole range will be put on a single line. // If set, the whole range will be put on a single line.
bool disable_line_wrapping; bool disable_line_wrapping = false;
// If set, the range will be created as a link.
bool is_link;
}; };
// Note that any trailing whitespace in |text| will be trimmed. // Note that any trailing whitespace in |text| will be trimmed.
...@@ -70,17 +73,21 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener { ...@@ -70,17 +73,21 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
const base::string16& text() const { return text_; } const base::string16& text() const { return text_; }
// Sets the fonts used by all labels. Can be augemented by styling set by // Returns the font list that results from the default text context and style
// AddStyleRange and SetDefaultStyle. // for ranges. This can be used as the basis for a range |custom_font|.
void SetBaseFontList(const gfx::FontList& font_list); gfx::FontList GetDefaultFontList() const;
// Marks the given range within |text_| with style defined by |style_info|. // Marks the given range within |text_| with style defined by |style_info|.
// |range| must be contained in |text_|. // |range| must be contained in |text_|.
void AddStyleRange(const gfx::Range& range, const RangeStyleInfo& style_info); void AddStyleRange(const gfx::Range& range, const RangeStyleInfo& style_info);
// Sets the default style to use for any part of the text that isn't within // Set the context of this text. All ranges have the same context.
// a range set by AddStyleRange. // |text_context| must be a value from views::style::TextContext.
void SetDefaultStyle(const RangeStyleInfo& style_info); void SetTextContext(int text_context);
// Set the default text style.
// |text_style| must be a value from views::style::TextStyle.
void SetDefaultTextStyle(int text_style);
// Get or set the distance in pixels between baselines of multi-line text. // Get or set the distance in pixels between baselines of multi-line text.
// Default is 0, indicating the distance between lines should be the standard // Default is 0, indicating the distance between lines should be the standard
...@@ -134,6 +141,13 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener { ...@@ -134,6 +141,13 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
}; };
typedef std::list<StyleRange> StyleRanges; typedef std::list<StyleRange> StyleRanges;
// Returns the default line height, based on the default style.
int GetDefaultLineHeight() const;
// Returns the FontList that should be used for |range|.
gfx::FontList GetFontListForRange(
const StyleRanges::const_iterator& range) const;
// Calculates how to layout child views, creates them and sets their size and // Calculates how to layout child views, creates them and sets their size and
// position. |width| is the horizontal space, in pixels, that the view has to // position. |width| is the horizontal space, in pixels, that the view has to
// work with. If |dry_run| is true, the view hierarchy is not touched. Caches // work with. If |dry_run| is true, the view hierarchy is not touched. Caches
...@@ -144,16 +158,12 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener { ...@@ -144,16 +158,12 @@ class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
// The text to display. // The text to display.
base::string16 text_; base::string16 text_;
// Fonts used to display text. Can be augmented by RangeStyleInfo. int text_context_ = style::CONTEXT_LABEL;
gfx::FontList font_list_; int default_text_style_ = style::STYLE_PRIMARY;
// Line height. // Line height. If zero, style::GetLineHeight() is used.
int specified_line_height_; int specified_line_height_;
// The default style to use for any part of the text that isn't within
// a range in |style_ranges_|.
RangeStyleInfo default_style_info_;
// The listener that will be informed of link clicks. // The listener that will be informed of link clicks.
StyledLabelListener* listener_; StyledLabelListener* listener_;
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#include "ui/views/border.h" #include "ui/views/border.h"
#include "ui/views/controls/link.h" #include "ui/views/controls/link.h"
#include "ui/views/controls/styled_label_listener.h" #include "ui/views/controls/styled_label_listener.h"
#include "ui/views/style/typography.h"
#include "ui/views/test/test_layout_provider.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
...@@ -328,18 +330,14 @@ TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) { ...@@ -328,18 +330,14 @@ TEST_F(StyledLabelTest, StyledRangeWithDisabledLineWrapping) {
EXPECT_EQ(0, styled()->child_at(1)->x()); EXPECT_EQ(0, styled()->child_at(1)->x());
} }
// TODO(mboc): Linux has never supported UNDERLINE, only virtually. Fix this. TEST_F(StyledLabelTest, StyledRangeCustomFontUnderlined) {
#if defined(OS_LINUX)
#define MAYBE_StyledRangeUnderlined DISABLED_StyledRangeUnderlined
#else
#define MAYBE_StyledRangeUnderlined StyledRangeUnderlined
#endif
TEST_F(StyledLabelTest, MAYBE_StyledRangeUnderlined) {
const std::string text("This is a test block of text, "); const std::string text("This is a test block of text, ");
const std::string underlined_text("and this should be undelined"); const std::string underlined_text("and this should be undelined");
InitStyledLabel(text + underlined_text); InitStyledLabel(text + underlined_text);
StyledLabel::RangeStyleInfo style_info; StyledLabel::RangeStyleInfo style_info;
style_info.font_style = gfx::Font::UNDERLINE; style_info.tooltip = ASCIIToUTF16("tooltip");
style_info.custom_font =
styled()->GetDefaultFontList().DeriveWithStyle(gfx::Font::UNDERLINE);
styled()->AddStyleRange( styled()->AddStyleRange(
gfx::Range(static_cast<uint32_t>(text.size()), gfx::Range(static_cast<uint32_t>(text.size()),
static_cast<uint32_t>(text.size() + underlined_text.size())), static_cast<uint32_t>(text.size() + underlined_text.size())),
...@@ -356,14 +354,20 @@ TEST_F(StyledLabelTest, MAYBE_StyledRangeUnderlined) { ...@@ -356,14 +354,20 @@ TEST_F(StyledLabelTest, MAYBE_StyledRangeUnderlined) {
static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle()); static_cast<Label*>(styled()->child_at(1))->font_list().GetFontStyle());
} }
TEST_F(StyledLabelTest, StyledRangeBold) { TEST_F(StyledLabelTest, StyledRangeTextStyleBold) {
test::TestLayoutProvider bold_provider;
const std::string bold_text( const std::string bold_text(
"This is a block of text whose style will be set to BOLD in the test"); "This is a block of text whose style will be set to BOLD in the test");
const std::string text(" normal text"); const std::string text(" normal text");
InitStyledLabel(bold_text + text); InitStyledLabel(bold_text + text);
// Pretend disabled text becomes bold for testing.
bold_provider.SetFont(
style::CONTEXT_LABEL, style::STYLE_DISABLED,
styled()->GetDefaultFontList().DeriveWithWeight(gfx::Font::Weight::BOLD));
StyledLabel::RangeStyleInfo style_info; StyledLabel::RangeStyleInfo style_info;
style_info.weight = gfx::Font::Weight::BOLD; style_info.text_style = style::STYLE_DISABLED;
styled()->AddStyleRange( styled()->AddStyleRange(
gfx::Range(0u, static_cast<uint32_t>(bold_text.size())), style_info); gfx::Range(0u, static_cast<uint32_t>(bold_text.size())), style_info);
...@@ -425,7 +429,7 @@ TEST_F(StyledLabelTest, Color) { ...@@ -425,7 +429,7 @@ TEST_F(StyledLabelTest, Color) {
InitStyledLabel(text_red + text_link + text); InitStyledLabel(text_red + text_link + text);
StyledLabel::RangeStyleInfo style_info_red; StyledLabel::RangeStyleInfo style_info_red;
style_info_red.color = SK_ColorRED; style_info_red.override_color = SK_ColorRED;
styled()->AddStyleRange( styled()->AddStyleRange(
gfx::Range(0u, static_cast<uint32_t>(text_red.size())), style_info_red); gfx::Range(0u, static_cast<uint32_t>(text_red.size())), style_info_red);
...@@ -540,22 +544,30 @@ TEST_P(MDStyledLabelTest, StyledRangeWithTooltip) { ...@@ -540,22 +544,30 @@ TEST_P(MDStyledLabelTest, StyledRangeWithTooltip) {
EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip); EXPECT_EQ(ASCIIToUTF16("tooltip"), tooltip);
} }
TEST_F(StyledLabelTest, SetBaseFontList) { TEST_F(StyledLabelTest, SetTextContextAndDefaultStyle) {
const std::string text("This is a test block of text."); const std::string text("This is a test block of text.");
InitStyledLabel(text); InitStyledLabel(text);
std::string font_name("arial"); styled()->SetTextContext(style::CONTEXT_DIALOG_TITLE);
gfx::Font font(font_name, 30); styled()->SetDefaultTextStyle(style::STYLE_DISABLED);
styled()->SetBaseFontList(gfx::FontList(font)); Label label(ASCIIToUTF16(text), style::CONTEXT_DIALOG_TITLE,
Label label(ASCIIToUTF16(text), Label::CustomFont{gfx::FontList(font)}); style::STYLE_DISABLED);
styled()->SetBounds(0, styled()->SetBounds(0,
0, 0,
label.GetPreferredSize().width(), label.GetPreferredSize().width(),
label.GetPreferredSize().height()); label.GetPreferredSize().height());
// Make sure we have the same sizing as a label. // Make sure we have the same sizing as a label with the same style.
EXPECT_EQ(label.GetPreferredSize().height(), styled()->height()); EXPECT_EQ(label.GetPreferredSize().height(), styled()->height());
EXPECT_EQ(label.GetPreferredSize().width(), styled()->width()); EXPECT_EQ(label.GetPreferredSize().width(), styled()->width());
styled()->Layout();
ASSERT_EQ(1, styled()->child_count());
Label* sublabel = static_cast<Label*>(styled()->child_at(0));
EXPECT_EQ(style::CONTEXT_DIALOG_TITLE, sublabel->text_context());
EXPECT_NE(SK_ColorBLACK, label.enabled_color()); // Sanity check,
EXPECT_EQ(label.enabled_color(), sublabel->enabled_color());
} }
TEST_F(StyledLabelTest, LineHeight) { TEST_F(StyledLabelTest, LineHeight) {
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "ui/views/test/test_layout_provider.h" #include "ui/views/test/test_layout_provider.h"
#include "ui/gfx/font_list.h"
namespace views { namespace views {
namespace test { namespace test {
...@@ -13,19 +15,37 @@ TestLayoutProvider::~TestLayoutProvider() {} ...@@ -13,19 +15,37 @@ TestLayoutProvider::~TestLayoutProvider() {}
void TestLayoutProvider::SetDistanceMetric(int metric, int value) { void TestLayoutProvider::SetDistanceMetric(int metric, int value) {
distance_metrics_[metric] = value; distance_metrics_[metric] = value;
} }
void TestLayoutProvider::SetSnappedDialogWidth(int width) { void TestLayoutProvider::SetSnappedDialogWidth(int width) {
snapped_dialog_width_ = width; snapped_dialog_width_ = width;
} }
void TestLayoutProvider::SetFont(int context,
int style,
const gfx::FontList& font) {
fonts_[{context, style}] = font;
}
int TestLayoutProvider::GetDistanceMetric(int metric) const { int TestLayoutProvider::GetDistanceMetric(int metric) const {
if (distance_metrics_.count(metric)) if (distance_metrics_.count(metric))
return distance_metrics_.find(metric)->second; return distance_metrics_.find(metric)->second;
return LayoutProvider::GetDistanceMetric(metric); return LayoutProvider::GetDistanceMetric(metric);
} }
const TypographyProvider& TestLayoutProvider::GetTypographyProvider() const {
return *this;
}
int TestLayoutProvider::GetSnappedDialogWidth(int min_width) const { int TestLayoutProvider::GetSnappedDialogWidth(int min_width) const {
return snapped_dialog_width_ ? snapped_dialog_width_ : min_width; return snapped_dialog_width_ ? snapped_dialog_width_ : min_width;
} }
const gfx::FontList& TestLayoutProvider::GetFont(int context, int style) const {
auto it = fonts_.find({context, style});
return it != fonts_.end()
? it->second
: DefaultTypographyProvider::GetFont(context, style);
}
} // namespace test } // namespace test
} // namespace views } // namespace views
...@@ -9,12 +9,18 @@ ...@@ -9,12 +9,18 @@
#include "base/macros.h" #include "base/macros.h"
#include "ui/views/layout/layout_provider.h" #include "ui/views/layout/layout_provider.h"
#include "ui/views/style/typography_provider.h"
namespace gfx {
class FontList;
}
namespace views { namespace views {
namespace test { namespace test {
// Helper to test LayoutProvider overrides. // Helper to test LayoutProvider overrides.
class TestLayoutProvider : public LayoutProvider { class TestLayoutProvider : public LayoutProvider,
public DefaultTypographyProvider {
public: public:
TestLayoutProvider(); TestLayoutProvider();
~TestLayoutProvider() override; ~TestLayoutProvider() override;
...@@ -26,12 +32,20 @@ class TestLayoutProvider : public LayoutProvider { ...@@ -26,12 +32,20 @@ class TestLayoutProvider : public LayoutProvider {
// Override the return value of GetSnappedDialogWidth(). // Override the return value of GetSnappedDialogWidth().
void SetSnappedDialogWidth(int width); void SetSnappedDialogWidth(int width);
// Override the font provided by style::GetFont().
void SetFont(int context, int style, const gfx::FontList& font);
// LayoutProvider: // LayoutProvider:
int GetDistanceMetric(int metric) const override; int GetDistanceMetric(int metric) const override;
const TypographyProvider& GetTypographyProvider() const override;
int GetSnappedDialogWidth(int min_width) const override; int GetSnappedDialogWidth(int min_width) const override;
// TypographyProvider:
const gfx::FontList& GetFont(int context, int style) const override;
private: private:
std::map<int, int> distance_metrics_; std::map<int, int> distance_metrics_;
std::map<std::pair<int, int>, gfx::FontList> fonts_;
int snapped_dialog_width_ = 0; int snapped_dialog_width_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider); DISALLOW_COPY_AND_ASSIGN(TestLayoutProvider);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment