File: tooltip_view_aura.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (134 lines) | stat: -rw-r--r-- 4,107 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/corewm/tooltip_view_aura.h"

#include <utility>

#include "base/strings/string_util.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/text_elider.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/painter.h"

namespace views::corewm {

namespace {

constexpr int kTooltipBorderThickness = 1;
constexpr gfx::Insets kBorderInset = gfx::Insets::TLBR(4, 8, 5, 8);

}  // namespace

TooltipViewAura::TooltipViewAura()
    : render_text_(gfx::RenderText::CreateRenderText()) {
  render_text_->SetWordWrapBehavior(gfx::WRAP_LONG_WORDS);
  render_text_->SetMultiline(true);

  SetBackground(views::CreateSolidBackground(ui::kColorTooltipBackground));
  SetBorder(views::CreatePaddedBorder(
      views::CreateSolidBorder(kTooltipBorderThickness,
                               ui::kColorTooltipForeground),
      kBorderInset - gfx::Insets(kTooltipBorderThickness)));

  GetViewAccessibility().SetRole(ax::mojom::Role::kTooltip);
  UpdateAccessibleName();

  ResetDisplayRect();
}

TooltipViewAura::~TooltipViewAura() = default;

void TooltipViewAura::SetText(const std::u16string& text) {
  render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);

  // Replace tabs with whitespace to avoid placeholder character rendering
  // where previously it did not. crbug.com/993100
  std::u16string new_text(text);
  base::ReplaceChars(new_text, u"\t", u"        ", &new_text);
  render_text_->SetText(std::move(new_text));
  UpdateAccessibleName();
  SchedulePaint();
}

void TooltipViewAura::SetFontList(const gfx::FontList& font_list) {
  render_text_->SetFontList(font_list);
}

void TooltipViewAura::SetMinLineHeight(int line_height) {
  render_text_->SetMinLineHeight(line_height);
}

void TooltipViewAura::SetMaxWidth(int width) {
  max_width_ = width;
  ResetDisplayRect();
}

void TooltipViewAura::SetMaxLines(size_t max_lines) {
  render_text_->SetMaxLines(max_lines);
}

void TooltipViewAura::SetElideBehavior(gfx::ElideBehavior elide_behavior) {
  render_text_->SetElideBehavior(elide_behavior);
}

void TooltipViewAura::OnPaint(gfx::Canvas* canvas) {
  OnPaintBackground(canvas);
  gfx::Size text_size = size();
  gfx::Insets insets = GetInsets();
  text_size.Enlarge(-insets.width(), -insets.height());
  render_text_->SetDisplayRect(gfx::Rect(text_size));
  canvas->Save();
  canvas->Translate(gfx::Vector2d(insets.left(), insets.top()));
  render_text_->Draw(canvas);
  canvas->Restore();
  OnPaintBorder(canvas);
}

gfx::Size TooltipViewAura::CalculatePreferredSize(
    const SizeBounds& /*available_size*/) const {
  gfx::Size view_size = render_text_->GetStringSize();
  gfx::Insets insets = GetInsets();
  view_size.Enlarge(insets.width(), insets.height());
  return view_size;
}

void TooltipViewAura::OnThemeChanged() {
  views::View::OnThemeChanged();
  // Force the text color to be readable when |background_color| is not
  // opaque.
  const SkColor background_color =
      background()->color().ResolveToSkColor(GetColorProvider());
  render_text_->set_subpixel_rendering_suppressed(
      SkColorGetA(background_color) != SK_AlphaOPAQUE);
  render_text_->SetColor(
      GetColorProvider()->GetColor(ui::kColorTooltipForeground));
}

void TooltipViewAura::UpdateAccessibleName() {
  if (render_text_->GetDisplayText().empty()) {
    GetViewAccessibility().SetName(
        std::u16string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
    return;
  }

  GetViewAccessibility().SetName(
      std::u16string(render_text_->GetDisplayText()));
}

void TooltipViewAura::ResetDisplayRect() {
  render_text_->SetDisplayRect(gfx::Rect(0, 0, max_width_, 100000));
  UpdateAccessibleName();
}

BEGIN_METADATA(TooltipViewAura)
END_METADATA

}  // namespace views::corewm