File: fade_label_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (103 lines) | stat: -rw-r--r-- 3,179 bytes parent folder | download | duplicates (4)
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
// 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 "chrome/browser/ui/views/tabs/fade_label_view.h"

#include <string_view>

#include "chrome/browser/ui/views/tabs/filename_elider.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"

namespace {
std::unique_ptr<FadeLabel> CreateFadeLabel(int context, int text_style) {
  return std::make_unique<FadeLabel>(std::u16string(), context, text_style);
}
}  // namespace

// FadeLabel
// ----------------------------------------------------------

void FadeLabel::SetData(const FadeLabelViewData& data) {
  data_ = data;
  std::u16string text = data.text;
  const bool is_filename = data.is_filename;
  SetElideBehavior(is_filename ? gfx::NO_ELIDE : data.elide);
  if (is_filename) {
    text = TruncateFilenameToTwoLines(text);
  }
  SetText(text);
}

void FadeLabel::SetFade(double percent) {
  percent = std::min(1.0, percent);
  const SkAlpha alpha = base::saturated_cast<SkAlpha>(
      std::numeric_limits<SkAlpha>::max() * (1.0 - percent));
  SetBackgroundColor(SkColorSetA(GetBackgroundColor(), alpha));
  SetEnabledColor(SkColorSetA(GetEnabledColor(), alpha));
}

void FadeLabel::SetPaintBackground(bool paint_background) {
  paint_background_ = paint_background;
}

void FadeLabel::OnPaintBackground(gfx::Canvas* canvas) {
  if (paint_background_) {
    canvas->DrawColor(GetBackgroundColor());
  } else {
    views::Label::OnPaintBackground(canvas);
  }
}

// Returns a version of the text that's middle-elided on two lines.
std::u16string FadeLabel::TruncateFilenameToTwoLines(
    const std::u16string& text) const {
  FilenameElider elider(CreateRenderText());
  gfx::Rect text_rect = GetContentsBounds();
  text_rect.Inset(-gfx::ShadowValue::GetMargin(GetShadows()));
  return elider.Elide(text, text_rect);
}

BEGIN_METADATA(FadeLabel)
END_METADATA

// FadeLabelView:
// ----------------------------------------------------------

FadeLabelView::FadeLabelView(int num_lines, int context, int text_style)
    : FadeView<FadeLabel, FadeLabel, FadeLabelViewData>(
          CreateFadeLabel(context, text_style),
          CreateFadeLabel(context, text_style)) {
  if (num_lines > 1) {
    primary_view_->SetMultiLine(true);
    fade_out_view_->SetMultiLine(true);
    primary_view_->SetMaxLines(num_lines);
    fade_out_view_->SetMaxLines(num_lines);
  }

  fade_out_view_->SetPaintBackground(true);
}

std::u16string_view FadeLabelView::GetText() const {
  return primary_view_->GetText();
}

void FadeLabelView::SetEnabledColor(ui::ColorId color) {
  primary_view_->SetEnabledColor(color);
  fade_out_view_->SetEnabledColor(color);
}

using FadeWrapper_Label_FadeLabelViewData =
    FadeWrapper<views::Label, FadeLabelViewData>;
BEGIN_TEMPLATE_METADATA(FadeWrapper_Label_FadeLabelViewData, FadeWrapper)
END_METADATA

using FadeView_FadeLabel_FadeLabel_FadeLabelViewData =
    FadeView<FadeLabel, FadeLabel, FadeLabelViewData>;
BEGIN_TEMPLATE_METADATA(FadeView_FadeLabel_FadeLabel_FadeLabelViewData,
                        FadeView)
END_METADATA

BEGIN_METADATA(FadeLabelView)
END_METADATA