File: md_text_button_with_spinner.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 5,644 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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/controls/button/md_text_button_with_spinner.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "base/notreached.h"
#include "base/time/time.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_types.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_variant.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/label_button_label.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/style/typography.h"

namespace views {
constexpr int kSpinnerDiameter = 20;
constexpr int kSpinnerLabelSpacing = 8;

MdTextButtonWithSpinner::MdTextButtonWithSpinner(
    Button::PressedCallback callback,
    std::u16string_view text)
    : MdTextButton(std::move(callback), text) {
  spinner_ = AddChildView(std::make_unique<Throbber>(kSpinnerDiameter));
  SetText(text);
}

MdTextButtonWithSpinner::~MdTextButtonWithSpinner() {
  SetSpinnerVisible(false);
}

void MdTextButtonWithSpinner::SetSpinnerVisible(bool visible) {
  spinner_visible_ = visible;
  if (spinner_visible_) {
    spinner_->Start();
  } else {
    spinner_->Stop();
  }
  PreferredSizeChanged();
}

bool MdTextButtonWithSpinner::GetSpinnerVisible() const {
  return spinner_visible_;
}

gfx::Size MdTextButtonWithSpinner::CalculatePreferredSize(
    const SizeBounds& available_size) const {
  auto size = views::MdTextButton::CalculatePreferredSize(available_size);
  if (GetSpinnerVisible()) {
    size.set_width(size.width() + kSpinnerDiameter + kSpinnerLabelSpacing);
  }
  return size;
}

views::ProposedLayout MdTextButtonWithSpinner::CalculateProposedLayout(
    const views::SizeBounds& size_bounds) const {
  views::ProposedLayout layouts =
      views::MdTextButton::CalculateProposedLayout(size_bounds);
  if (!GetSpinnerVisible()) {
    return layouts;
  }

  views::ChildLayout* label_layout = nullptr;
  for (auto& child_layout : layouts.child_layouts) {
    if (child_layout.child_view == label()) {
      label_layout = &child_layout;
      break;
    }
  }
  DCHECK(label_layout);

  const int button_content_width =
      layouts.host_size.width() - GetInsets().left() - GetInsets().right();

  const int label_preferred_width = label_layout->bounds.width();

  // Calculate the total width of the spinner and label.
  const int preferred_spinner_label_width =
      kSpinnerDiameter + kSpinnerLabelSpacing + label_layout->bounds.width();

  int label_width;
  int spinner_label_width;

  // Adjust width if the available space is smaller than the preferred space.
  if (preferred_spinner_label_width > button_content_width) {
    // Calculate the maximum width available for the label after accounting
    // for the fixed spinner and spacing.
    int max_available_label_width =
        button_content_width - kSpinnerDiameter - kSpinnerLabelSpacing;

    // Ensure the available width for the label is not negative.
    label_width = std::max(0, max_available_label_width);

    // Update combined width to reflect the shrunk label width.
    spinner_label_width = kSpinnerDiameter + kSpinnerLabelSpacing + label_width;
  } else {
    label_width = label_preferred_width;
    spinner_label_width = preferred_spinner_label_width;
  }

  // Account for different horizontal alignments.
  int combo_start_x;
  auto horizontal_alignment = GetHorizontalAlignment();
  DCHECK_NE(gfx::ALIGN_TO_HEAD, horizontal_alignment);
  switch (horizontal_alignment) {
    case gfx::HorizontalAlignment::ALIGN_LEFT:
      combo_start_x = GetInsets().left();
      break;
    case gfx::HorizontalAlignment::ALIGN_CENTER:
      combo_start_x =
          GetInsets().left() + (button_content_width - spinner_label_width) / 2;
      break;
    case gfx::HorizontalAlignment::ALIGN_RIGHT:
      combo_start_x =
          GetInsets().left() + button_content_width - spinner_label_width;
      break;
    case gfx::HorizontalAlignment::ALIGN_TO_HEAD:
      NOTREACHED();
  }

  const int spinner_x = combo_start_x;
  const int spinner_y =
      label_layout->bounds.CenterPoint().y() - kSpinnerDiameter / 2;
  const gfx::Rect spinner_bounds(spinner_x, spinner_y, kSpinnerDiameter,
                                 kSpinnerDiameter);

  // Adjust label position relative to spinner.
  const int label_x = spinner_x + kSpinnerDiameter + kSpinnerLabelSpacing;
  label_layout->bounds.set_x(label_x);
  label_layout->bounds.set_width(label_width);

  layouts.child_layouts.emplace_back(
      const_cast<views::Throbber*>(spinner_.get()), GetSpinnerVisible(),
      spinner_bounds, views::SizeBounds());

  return layouts;
}

void MdTextButtonWithSpinner::UpdateSpinnerColor() {
  style::TextStyle text_style = style::STYLE_PRIMARY;
  if (GetStyle() == ui::ButtonStyle::kProminent) {
    text_style = style::STYLE_DIALOG_BUTTON_DEFAULT;
  } else if (GetStyle() == ui::ButtonStyle::kTonal) {
    text_style = style::STYLE_DIALOG_BUTTON_TONAL;
  }
  if (GetState() == STATE_DISABLED) {
    text_style = style::STYLE_DISABLED;
  }
  const auto& typography_provider = TypographyProvider::Get();
  spinner_->SetColorId(
      typography_provider.GetColorId(label()->GetTextContext(), text_style));
}

void MdTextButtonWithSpinner::UpdateColors() {
  MdTextButton::UpdateColors();
  UpdateSpinnerColor();
}

BEGIN_METADATA(MdTextButtonWithSpinner)
ADD_PROPERTY_METADATA(bool, SpinnerVisible);
END_METADATA

}  // namespace views