Commit c50c83b1 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

views: recompute omnibox text colors

Resetting the text to the default color is incorrect - OmniboxTextView::SetText
contains logic to apply special colors depending on the type of result. This
change adds a method to OmniboxTextView called ReapplyStyling() which causes
OmniboxTextView to recompute and reapply this text styling.  This is necessary
in high contrast modes because these use different text styling for the selected
or hovered result.

Bug: 865918
Change-Id: Ia2c68e6cb3329b1d58d86dc9f6999420810a9d6d
Reviewed-on: https://chromium-review.googlesource.com/1145136Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577519}
parent 9539b9eb
...@@ -178,12 +178,14 @@ void OmniboxResultView::Invalidate() { ...@@ -178,12 +178,14 @@ void OmniboxResultView::Invalidate() {
match_.contents_class); match_.contents_class);
suggestion_view_->description()->SetText(match_.description, suggestion_view_->description()->SetText(match_.description,
match_.description_class); match_.description_class);
// Explicitly re-apply default styling - high contrast modes use different
// text colors depending on selection state. // Normally, OmniboxTextView caches its appearance, but in high contrast
suggestion_view_->content()->ApplyTextColor( // selected-ness changes the text colors, so the styling of the text part of
OmniboxPart::RESULTS_TEXT_DEFAULT); // the results needs to be recomputed.
suggestion_view_->description()->ApplyTextColor( if (high_contrast) {
OmniboxPart::RESULTS_TEXT_DEFAULT); suggestion_view_->content()->ReapplyStyling();
suggestion_view_->description()->ReapplyStyling();
}
} }
AutocompleteMatch* keyword_match = match_.associated_keyword.get(); AutocompleteMatch* keyword_match = match_.associated_keyword.get();
......
...@@ -224,36 +224,7 @@ void OmniboxTextView::SetText(const base::string16& text, ...@@ -224,36 +224,7 @@ void OmniboxTextView::SetText(const base::string16& text,
std::make_unique<ACMatchClassifications>(classifications); std::make_unique<ACMatchClassifications>(classifications);
render_text_ = CreateRenderText(text); render_text_ = CreateRenderText(text);
const size_t text_length = render_text_->text().length(); ReapplyStyling();
for (size_t i = 0; i < classifications.size(); ++i) {
const size_t text_start = classifications[i].offset;
if (text_start >= text_length)
break;
const size_t text_end =
(i < (classifications.size() - 1))
? std::min(classifications[i + 1].offset, text_length)
: text_length;
const gfx::Range current_range(text_start, text_end);
// Calculate style-related data.
if (classifications[i].style & ACMatchClassification::MATCH)
render_text_->ApplyWeight(gfx::Font::Weight::BOLD, current_range);
OmniboxPart part = OmniboxPart::RESULTS_TEXT_DEFAULT;
if (classifications[i].style & ACMatchClassification::URL) {
part = OmniboxPart::RESULTS_TEXT_URL;
render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_AS_URL);
} else if (classifications[i].style & ACMatchClassification::DIM) {
part = OmniboxPart::RESULTS_TEXT_DIMMED;
} else if (classifications[i].style & ACMatchClassification::INVISIBLE) {
part = OmniboxPart::RESULTS_TEXT_INVISIBLE;
}
render_text_->ApplyColor(result_view_->GetColor(part), current_range);
}
UpdateLineHeight();
SetPreferredSize(CalculatePreferredSize());
} }
void OmniboxTextView::SetText(const SuggestionAnswer::ImageLine& line, void OmniboxTextView::SetText(const SuggestionAnswer::ImageLine& line,
...@@ -309,6 +280,40 @@ int OmniboxTextView::GetLineHeight() const { ...@@ -309,6 +280,40 @@ int OmniboxTextView::GetLineHeight() const {
return font_height_; return font_height_;
} }
void OmniboxTextView::ReapplyStyling() {
const ACMatchClassifications& classifications = *cached_classifications_;
const size_t text_length = render_text_->text().length();
for (size_t i = 0; i < classifications.size(); ++i) {
const size_t text_start = classifications[i].offset;
if (text_start >= text_length)
break;
const size_t text_end =
(i < (classifications.size() - 1))
? std::min(classifications[i + 1].offset, text_length)
: text_length;
const gfx::Range current_range(text_start, text_end);
// Calculate style-related data.
if (classifications[i].style & ACMatchClassification::MATCH)
render_text_->ApplyWeight(gfx::Font::Weight::BOLD, current_range);
OmniboxPart part = OmniboxPart::RESULTS_TEXT_DEFAULT;
if (classifications[i].style & ACMatchClassification::URL) {
part = OmniboxPart::RESULTS_TEXT_URL;
render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_AS_URL);
} else if (classifications[i].style & ACMatchClassification::DIM) {
part = OmniboxPart::RESULTS_TEXT_DIMMED;
} else if (classifications[i].style & ACMatchClassification::INVISIBLE) {
part = OmniboxPart::RESULTS_TEXT_INVISIBLE;
}
render_text_->ApplyColor(result_view_->GetColor(part), current_range);
}
UpdateLineHeight();
SetPreferredSize(CalculatePreferredSize());
}
std::unique_ptr<gfx::RenderText> OmniboxTextView::CreateRenderText( std::unique_ptr<gfx::RenderText> OmniboxTextView::CreateRenderText(
const base::string16& text) const { const base::string16& text) const {
auto render_text = gfx::RenderText::CreateHarfBuzzInstance(); auto render_text = gfx::RenderText::CreateHarfBuzzInstance();
......
...@@ -63,6 +63,10 @@ class OmniboxTextView : public views::View { ...@@ -63,6 +63,10 @@ class OmniboxTextView : public views::View {
// multiple lines. // multiple lines.
int GetLineHeight() const; int GetLineHeight() const;
// Reapplies text styling to the results text, based on the types of the match
// parts.
void ReapplyStyling();
private: private:
std::unique_ptr<gfx::RenderText> CreateRenderText( std::unique_ptr<gfx::RenderText> CreateRenderText(
const base::string16& text) const; const base::string16& text) const;
......
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