File: colored_dialog_example.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 (196 lines) | stat: -rw-r--r-- 7,011 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
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
// Copyright 2020 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/examples/colored_dialog_example.h"

#include <memory>
#include <string_view>
#include <utility>

#include "base/containers/adapters.h"
#include "base/memory/raw_ref.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/examples/grit/views_examples_resources.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/vector_icons.h"
#include "ui/views/widget/widget.h"

namespace views::examples {

class ThemeTrackingCheckbox : public views::Checkbox {
  METADATA_HEADER(ThemeTrackingCheckbox, views::Checkbox)

 public:
  explicit ThemeTrackingCheckbox(const std::u16string& label)
      : Checkbox(label,
                 base::BindRepeating(&ThemeTrackingCheckbox::ButtonPressed,
                                     base::Unretained(this))) {}
  ThemeTrackingCheckbox(const ThemeTrackingCheckbox&) = delete;
  ThemeTrackingCheckbox& operator=(const ThemeTrackingCheckbox&) = delete;
  ~ThemeTrackingCheckbox() override = default;

  // views::Checkbox
  void OnThemeChanged() override {
    views::Checkbox::OnThemeChanged();
    SetChecked(GetNativeTheme()->ShouldUseDarkColors());
  }

  void ButtonPressed() {
    GetNativeTheme()->set_use_dark_colors(GetChecked());
    GetWidget()->ThemeChanged();
  }
};

BEGIN_METADATA(ThemeTrackingCheckbox)
END_METADATA

class TextVectorImageButton : public views::MdTextButton {
  METADATA_HEADER(TextVectorImageButton, views::MdTextButton)

 public:
  TextVectorImageButton(PressedCallback callback,
                        const std::u16string& text,
                        const gfx::VectorIcon& icon)
      : MdTextButton(std::move(callback), text), icon_(icon) {}
  TextVectorImageButton(const TextVectorImageButton&) = delete;
  TextVectorImageButton& operator=(const TextVectorImageButton&) = delete;
  ~TextVectorImageButton() override = default;

  void OnThemeChanged() override {
    views::MdTextButton::OnThemeChanged();

    // Use the text color for the associated vector image.
    SetImageModel(
        views::Button::ButtonState::STATE_NORMAL,
        ui::ImageModel::FromVectorIcon(*icon_, label()->GetEnabledColor()));
  }

 private:
  const raw_ref<const gfx::VectorIcon> icon_;
};

BEGIN_METADATA(TextVectorImageButton)
END_METADATA

ColoredDialog::ColoredDialog(AcceptCallback accept_callback) {
  SetAcceptCallback(base::BindOnce(
      [](ColoredDialog* dialog, AcceptCallback callback) {
        std::move(callback).Run(dialog->textfield_->GetText());
      },
      base::Unretained(this), std::move(accept_callback)));

  SetModalType(ui::mojom::ModalType::kWindow);
  SetTitle(l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_TITLE));

  SetLayoutManager(std::make_unique<views::FillLayout>());
  set_margins(views::LayoutProvider::Get()->GetDialogInsetsForContentType(
      views::DialogContentType::kControl, views::DialogContentType::kControl));

  textfield_ = AddChildView(std::make_unique<views::Textfield>());
  textfield_->SetPlaceholderText(
      l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_TEXTFIELD_PLACEHOLDER));
  textfield_->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_TEXTFIELD_AX_LABEL));
  textfield_->set_controller(this);

  SetButtonLabel(ui::mojom::DialogButton::kOk,
                 l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_SUBMIT_BUTTON));
  SetButtonEnabled(ui::mojom::DialogButton::kOk, false);
}

ColoredDialog::~ColoredDialog() {
  if (textfield_) {
    textfield_->set_controller(nullptr);
  }
}

bool ColoredDialog::ShouldShowCloseButton() const {
  return false;
}

void ColoredDialog::ContentsChanged(Textfield* sender,
                                    const std::u16string& new_contents) {
  SetButtonEnabled(ui::mojom::DialogButton::kOk,
                   !textfield_->GetText().empty());
  DialogModelChanged();
}

BEGIN_METADATA(ColoredDialog)
END_METADATA

ColoredDialogChooser::ColoredDialogChooser() {
  views::LayoutProvider* provider = views::LayoutProvider::Get();
  const int vertical_spacing =
      provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL);
  auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      vertical_spacing));
  layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kStart);

  AddChildView(std::make_unique<ThemeTrackingCheckbox>(
      l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_CHOOSER_CHECKBOX)));

  AddChildView(std::make_unique<TextVectorImageButton>(
      base::BindRepeating(&ColoredDialogChooser::ButtonPressed,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_CHOOSER_BUTTON),
      views::kInfoIcon));

  confirmation_label_ = AddChildView(
      std::make_unique<views::Label>(std::u16string(), style::CONTEXT_LABEL));
  confirmation_label_->SetVisible(false);
}

ColoredDialogChooser::~ColoredDialogChooser() = default;

void ColoredDialogChooser::ButtonPressed() {
  // Create the colored dialog.
  views::Widget* widget = DialogDelegate::CreateDialogWidget(
      new ColoredDialog(base::BindOnce(&ColoredDialogChooser::OnFeedbackSubmit,
                                       base::Unretained(this))),
      gfx::NativeWindow(), GetWidget()->GetNativeView());
  widget->Show();
}

void ColoredDialogChooser::OnFeedbackSubmit(std::u16string_view text) {
  constexpr base::TimeDelta kConfirmationDuration = base::Seconds(3);

  confirmation_label_->SetText(l10n_util::GetStringFUTF16(
      IDS_COLORED_DIALOG_CHOOSER_CONFIRM_LABEL, std::u16string(text)));
  confirmation_label_->SetVisible(true);

  confirmation_timer_.Start(
      FROM_HERE, kConfirmationDuration,
      base::BindOnce([](views::View* view) { view->SetVisible(false); },
                     confirmation_label_));
}

BEGIN_METADATA(ColoredDialogChooser)
END_METADATA

ColoredDialogExample::ColoredDialogExample() : ExampleBase("Colored Dialog") {}

ColoredDialogExample::~ColoredDialogExample() = default;

void ColoredDialogExample::CreateExampleView(views::View* container) {
  container->SetLayoutManager(std::make_unique<views::FillLayout>());
  container->AddChildView(std::make_unique<ColoredDialogChooser>());
}

}  // namespace views::examples