File: omnibox_text_view.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (319 lines) | stat: -rw-r--r-- 11,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <limits.h>

#include <algorithm>
#include <memory>

#include "chrome/browser/ui/views/omnibox/omnibox_text_view.h"

#include "base/feature_list.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/omnibox/common/omnibox_features.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/render_text.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"

namespace {

// Use the primary style for everything. TextStyle sometimes controls color, but
// we use OmniboxTheme for that.
constexpr int kTextStyle = views::style::STYLE_PRIMARY;

// Indicates to use CONTEXT_OMNIBOX_PRIMARY when picking a font size in legacy
// code paths.
constexpr int kInherit = INT_MIN;

// The vertical padding to provide each RenderText in addition to the height
// of the font. Where possible, RenderText uses this additional space to
// vertically center the cap height of the font instead of centering the
// entire font.
static constexpr int kVerticalPadding = 3;

struct TextStyle {
  OmniboxPart part;

  // The legacy size delta, relative to the ui::ResourceBundle BaseFont, or
  // kInherit to use CONTEXT_OMNIBOX_PRIMARY, to match the omnibox font.
  // Note: the actual font size may differ due to |baseline| altering the size.
  int legacy_size_delta = kInherit;

  // The size delta from the Touchable chrome spec. This is always relative to
  // CONTEXT_OMNIBOX_PRIMARY, which defaults to 15pt under touch. Only negative
  // deltas are supported correctly (the line height will not increase to fit).
  int touchable_size_delta = 0;

  // The baseline shift. Ignored under touch (text is always baseline-aligned).
  gfx::BaselineStyle baseline = gfx::BaselineStyle::kNormalBaseline;
};

// The new answer layout has separate and different treatment of text styles,
// and as of writing both styling approaches need to be supported.  When old
// answer styles are deprecated, the above TextStyle structure and related
// logic can be removed, and this used exclusively.  This utility function
// applies new answer text styling for given text_type over range on render_text
// using result_view as a source for omnibox part colors.
void ApplyTextStyleForType(SuggestionAnswer::TextStyle text_style,
                           OmniboxResultView* result_view,
                           gfx::RenderText* render_text,
                           const gfx::Range& range) {
  const gfx::Font::Weight weight =
      (text_style == SuggestionAnswer::TextStyle::BOLD)
          ? gfx::Font::Weight::BOLD
          : gfx::Font::Weight::NORMAL;
  render_text->ApplyWeight(weight, range);

  const gfx::BaselineStyle baseline =
      (text_style == SuggestionAnswer::TextStyle::SUPERIOR)
          ? gfx::BaselineStyle::kSuperior
          : gfx::BaselineStyle::kNormalBaseline;
  render_text->ApplyBaselineStyle(baseline, range);

  const bool selected =
      result_view->GetThemeState() == OmniboxPartState::SELECTED;
  ui::ColorId id;
  if (text_style == SuggestionAnswer::TextStyle::NORMAL_DIM) {
    id = selected ? kColorOmniboxResultsTextDimmedSelected
                  : kColorOmniboxResultsTextDimmed;
  } else if (text_style == SuggestionAnswer::TextStyle::SECONDARY) {
    id = selected ? kColorOmniboxResultsTextSecondarySelected
                  : kColorOmniboxResultsTextSecondary;
  } else if (text_style == SuggestionAnswer::TextStyle::POSITIVE) {
    id = selected ? kColorOmniboxResultsTextPositiveSelected
                  : kColorOmniboxResultsTextPositive;
  } else if (text_style == SuggestionAnswer::TextStyle::NEGATIVE) {
    id = selected ? kColorOmniboxResultsTextNegativeSelected
                  : kColorOmniboxResultsTextNegative;
  } else {
    id = selected ? kColorOmniboxResultsTextSelected : kColorOmniboxText;
  }
  render_text->ApplyColor(result_view->GetColorProvider()->GetColor(id), range);
}

}  // namespace

OmniboxTextView::OmniboxTextView(OmniboxResultView* result_view)
    : result_view_(result_view) {}

OmniboxTextView::~OmniboxTextView() = default;

gfx::Size OmniboxTextView::CalculatePreferredSize() const {
  return render_text_ ? render_text_->GetStringSize() : gfx::Size();
}

bool OmniboxTextView::GetCanProcessEventsWithinSubtree() const {
  return false;
}

int OmniboxTextView::GetHeightForWidth(int width) const {
  if (!render_text_)
    return 0;
  // If text wrapping is not called for we can simply return the font height.
  if (!wrap_text_lines_)
    return GetLineHeight();
  render_text_->SetDisplayRect(gfx::Rect(width, 0));
  gfx::Size string_size = render_text_->GetStringSize();
  return string_size.height() + kVerticalPadding;
}

void OmniboxTextView::OnPaint(gfx::Canvas* canvas) {
  View::OnPaint(canvas);

  if (!render_text_)
    return;
  render_text_->SetDisplayRect(GetContentsBounds());
  render_text_->Draw(canvas);
}

void OmniboxTextView::ApplyTextColor(ui::ColorId id) {
  if (GetText().empty())
    return;
  render_text_->SetColor(GetColorProvider()->GetColor(id));
  SchedulePaint();
}

const std::u16string& OmniboxTextView::GetText() const {
  return render_text_ ? render_text_->text() : base::EmptyString16();
}

void OmniboxTextView::SetText(const std::u16string& new_text) {
  if (cached_classifications_) {
    cached_classifications_.reset();
  } else if (GetText() == new_text && !use_deemphasized_font_) {
    // Only exit early if |cached_classifications_| was empty,
    // i.e. the last time text was set was through this method.
    return;
  }

  use_deemphasized_font_ = false;
  render_text_ = CreateRenderText(new_text);

  OnStyleChanged();
}

void OmniboxTextView::SetTextWithStyling(
    const std::u16string& new_text,
    const ACMatchClassifications& classifications,
    bool deemphasize) {
  if (GetText() == new_text && cached_classifications_ &&
      classifications == *cached_classifications_ &&
      deemphasize == use_deemphasized_font_)
    return;

  use_deemphasized_font_ = deemphasize;

  cached_classifications_ =
      std::make_unique<ACMatchClassifications>(classifications);
  render_text_ = CreateRenderText(new_text);

  // ReapplyStyling will update the preferred size and request a repaint.
  ReapplyStyling();
}

void OmniboxTextView::SetTextWithStyling(
    const SuggestionAnswer::ImageLine& line,
    bool deemphasize) {
  use_deemphasized_font_ = deemphasize;
  cached_classifications_.reset();
  wrap_text_lines_ = line.num_text_lines() > 1;
  render_text_ = CreateRenderText(std::u16string());

  for (const SuggestionAnswer::TextField& text_field : line.text_fields())
    AppendText(text_field, std::u16string());
  if (!line.text_fields().empty()) {
    const int kMaxDisplayLines =
        OmniboxFieldTrial::IsUniformRowHeightEnabled() ? 1 : 3;
    const SuggestionAnswer::TextField& first_field = line.text_fields().front();
    if (first_field.has_num_lines() && first_field.num_lines() > 1) {
      render_text_->SetMultiline(true);
      render_text_->SetMaxLines(
          std::min(kMaxDisplayLines, first_field.num_lines()));
    }
  }

  // Add the "additional" and "status" text from |line|, if any.
  AppendExtraText(line);

  OnStyleChanged();
}

void OmniboxTextView::AppendExtraText(const SuggestionAnswer::ImageLine& line) {
  const std::u16string space = u" ";
  const auto* text_field = line.additional_text();
  if (text_field) {
    AppendText(*text_field, space);
  }
  text_field = line.status_text();
  if (text_field) {
    AppendText(*text_field, space);
  }
  SetPreferredSize(CalculatePreferredSize());
}

int OmniboxTextView::GetLineHeight() const {
  return font_height_;
}

void OmniboxTextView::ReapplyStyling() {
  // No work required if there are no preexisting styles.
  if (!cached_classifications_)
    return;

  const size_t text_length = GetText().length();
  for (size_t i = 0; i < cached_classifications_->size(); ++i) {
    const size_t text_start = (*cached_classifications_)[i].offset;
    if (text_start >= text_length)
      break;

    const size_t text_end =
        (i < (cached_classifications_->size() - 1))
            ? std::min((*cached_classifications_)[i + 1].offset, text_length)
            : text_length;
    const gfx::Range current_range(text_start, text_end);

    // Calculate style-related data.
    if ((*cached_classifications_)[i].style & ACMatchClassification::MATCH)
      render_text_->ApplyWeight(gfx::Font::Weight::BOLD, current_range);

    const bool selected =
        result_view_->GetThemeState() == OmniboxPartState::SELECTED;
    ui::ColorId id =
        selected ? kColorOmniboxResultsTextSelected : kColorOmniboxText;
    if ((*cached_classifications_)[i].style & ACMatchClassification::URL) {
      id = selected ? kColorOmniboxResultsUrlSelected : kColorOmniboxResultsUrl;
      render_text_->SetDirectionalityMode(gfx::DIRECTIONALITY_AS_URL);
    } else if ((*cached_classifications_)[i].style &
               ACMatchClassification::DIM) {
      id = selected ? kColorOmniboxResultsTextDimmedSelected
                    : kColorOmniboxResultsTextDimmed;
    }
    render_text_->ApplyColor(GetColorProvider()->GetColor(id), current_range);
  }

  OnStyleChanged();
}

std::unique_ptr<gfx::RenderText> OmniboxTextView::CreateRenderText(
    const std::u16string& text) const {
  std::unique_ptr<gfx::RenderText> render_text =
      gfx::RenderText::CreateRenderText();
  render_text->SetDisplayRect(gfx::Rect(gfx::Size(INT_MAX, 0)));
  render_text->SetCursorEnabled(false);
  render_text->SetElideBehavior(gfx::ELIDE_TAIL);
  const gfx::FontList& font = views::TypographyProvider::Get().GetFont(
      (use_deemphasized_font_ ? CONTEXT_OMNIBOX_DEEMPHASIZED
                              : CONTEXT_OMNIBOX_POPUP),
      kTextStyle);
  render_text->SetFontList(font);
  render_text->SetText(text);
  return render_text;
}

void OmniboxTextView::AppendText(const SuggestionAnswer::TextField& field,
                                 const std::u16string& prefix) {
  const std::u16string& append_text =
      prefix.empty() ? field.text() : (prefix + field.text());
  if (append_text.empty())
    return;
  int offset = GetText().length();
  gfx::Range range(offset, offset + append_text.length());
  render_text_->AppendText(append_text);
  ApplyTextStyleForType(field.style(), result_view_, render_text_.get(), range);
}

void OmniboxTextView::OnStyleChanged() {
  const int height_normal = render_text_->font_list().GetHeight();
  const int size_delta =
      render_text_->font_list().GetFontSize() - gfx::FontList().GetFontSize();
  const int height_bold =
      ui::ResourceBundle::GetSharedInstance()
          .GetFontListForDetails(ui::ResourceBundle::FontDetails(
              std::string(), size_delta, gfx::Font::Weight::BOLD))
          .GetHeight();
  font_height_ = std::max(height_normal, height_bold);
  font_height_ += kVerticalPadding;

  render_text_->SetElideBehavior(gfx::NO_ELIDE);
  SetPreferredSize(CalculatePreferredSize());
  render_text_->SetElideBehavior(gfx::ELIDE_TAIL);
  SchedulePaint();
}

BEGIN_METADATA(OmniboxTextView, views::View)
ADD_PROPERTY_METADATA(std::u16string, Text)
ADD_READONLY_PROPERTY_METADATA(int, LineHeight)
END_METADATA