File: tab_strip_nudge_button.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 (171 lines) | stat: -rw-r--r-- 6,433 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
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
// Copyright 2024 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/tab_strip_nudge_button.h"

#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/tabs/organization/tab_organization_service.h"
#include "chrome/browser/ui/views/tabs/tab_strip_controller.h"
#include "chrome/grit/generated_resources.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view_class_properties.h"

namespace {
constexpr int kTabStripNudgeCornerRadius = 10;
constexpr int kTabStripNudgeFlatCornerRadius = 4;
constexpr int kTabStripNudgeIconMargin = 6;
constexpr int kTabStripNudgeLabelMargin = 4;
constexpr int kTabStripNudgeCloseButtonMargin = 8;
constexpr int kTabStripNudgeCloseButtonSize = 16;
}  // namespace

TabStripNudgeButton::TabStripNudgeButton(
    TabStripController* tab_strip_controller,
    PressedCallback pressed_callback,
    PressedCallback close_pressed_callback,
    const std::u16string& label_text,
    const ui::ElementIdentifier& element_identifier,
    Edge flat_edge,
    const gfx::VectorIcon& icon)
    : TabStripControlButton(tab_strip_controller,
                            std::move(pressed_callback),
                            icon,
                            label_text,
                            Edge::kNone,
                            flat_edge) {
  auto* const layout_manager =
      SetLayoutManager(std::make_unique<views::BoxLayout>());
  layout_manager->set_main_axis_alignment(
      views::BoxLayout::MainAxisAlignment::kEnd);

  SetProperty(views::kElementIdentifierKey, element_identifier);

  if (!icon.is_empty()) {
    const gfx::Insets icon_margin =
        gfx::Insets().set_left(kTabStripNudgeIconMargin);
    image_container_view()->SetProperty(views::kMarginsKey, icon_margin);
  }

  SetLabelStyle(views::style::STYLE_BODY_3_EMPHASIS);
  label()->SetElideBehavior(gfx::ElideBehavior::NO_ELIDE);

  const gfx::Insets label_margin =
      gfx::Insets().set_left(kTabStripNudgeLabelMargin);
  label()->SetProperty(views::kMarginsKey, label_margin);

  SetForegroundFrameActiveColorId(kColorTabSearchButtonCRForegroundFrameActive);
  SetForegroundFrameInactiveColorId(
      kColorTabSearchButtonCRForegroundFrameInactive);
  SetBackgroundFrameActiveColorId(kColorNewTabButtonCRBackgroundFrameActive);
  SetBackgroundFrameInactiveColorId(
      kColorNewTabButtonCRBackgroundFrameInactive);

  set_paint_transparent_for_custom_image_theme(false);

  SetCloseButton(std::move(close_pressed_callback));

  UpdateColors();
}

TabStripNudgeButton::~TabStripNudgeButton() = default;

void TabStripNudgeButton::SetOpacity(float factor) {
  label()->layer()->SetOpacity(factor);
  close_button_->layer()->SetOpacity(factor);
}

void TabStripNudgeButton::SetWidthFactor(float factor) {
  width_factor_ = factor;
  PreferredSizeChanged();
}

gfx::Size TabStripNudgeButton::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  const int full_width =
      GetLayoutManager()->GetPreferredSize(this, available_size).width();
  const int width = full_width * width_factor_;
  const int height = TabStripControlButton::CalculatePreferredSize(
                         views::SizeBounds(width, available_size.height()))
                         .height();
  return gfx::Size(width, height);
}

int TabStripNudgeButton::GetCornerRadius() const {
  return kTabStripNudgeCornerRadius;
}

int TabStripNudgeButton::GetFlatCornerRadius() const {
  return kTabStripNudgeFlatCornerRadius;
}

void TabStripNudgeButton::SetCloseButton(PressedCallback pressed_callback) {
  auto close_button =
      std::make_unique<views::LabelButton>(std::move(pressed_callback));
  close_button->SetTooltipText(
      l10n_util::GetStringUTF16(IDS_TOOLTIP_TAB_ORGANIZE_CLOSE));

  const ui::ImageModel icon_image_model = ui::ImageModel::FromVectorIcon(
      vector_icons::kCloseChromeRefreshIcon,
      kColorTabSearchButtonCRForegroundFrameActive,
      kTabStripNudgeCloseButtonSize);

  close_button->SetImageModel(views::Button::STATE_NORMAL, icon_image_model);
  close_button->SetImageModel(views::Button::STATE_HOVERED, icon_image_model);
  close_button->SetImageModel(views::Button::STATE_PRESSED, icon_image_model);

  close_button->SetPaintToLayer();
  close_button->layer()->SetFillsBoundsOpaquely(false);

  views::InkDrop::Get(close_button.get())
      ->SetMode(views::InkDropHost::InkDropMode::ON);
  views::InkDrop::Get(close_button.get())->SetHighlightOpacity(0.16f);
  views::InkDrop::Get(close_button.get())->SetVisibleOpacity(0.14f);
  views::InkDrop::Get(close_button.get())
      ->SetBaseColorId(kColorTabSearchButtonCRForegroundFrameActive);

  auto ink_drop_highlight_path =
      std::make_unique<views::CircleHighlightPathGenerator>(gfx::Insets());
  views::HighlightPathGenerator::Install(close_button.get(),
                                         std::move(ink_drop_highlight_path));

  close_button->SetPreferredSize(
      gfx::Size(kTabStripNudgeCloseButtonSize, kTabStripNudgeCloseButtonSize));
  close_button->SetBorder(nullptr);

  const gfx::Insets margin = gfx::Insets().set_left_right(
      kTabStripNudgeCloseButtonMargin, kTabStripNudgeCloseButtonMargin);
  close_button->SetProperty(views::kMarginsKey, margin);

  close_button_ = AddChildView(std::move(close_button));
  SetIsShowingNudge(false);
}

void TabStripNudgeButton::SetIsShowingNudge(bool is_showing) {
  is_showing_nudge_ = is_showing;
  if (is_showing) {
    SetFocusBehavior(FocusBehavior::ALWAYS);
    SetCloseButtonFocusBehavior(FocusBehavior::ALWAYS);
  } else {
    SetFocusBehavior(FocusBehavior::NEVER);
    SetCloseButtonFocusBehavior(FocusBehavior::NEVER);
  }
}

void TabStripNudgeButton::SetCloseButtonFocusBehavior(
    views::View::FocusBehavior focus_behavior) {
  close_button_->SetFocusBehavior(focus_behavior);
}

BEGIN_METADATA(TabStripNudgeButton)
END_METADATA