Commit 270f6dd6 authored by Keren Zhu's avatar Keren Zhu Committed by Commit Bot

Change some ButtonPressed overrides to callbacks: ui/views/examples

Bug: 772945
Change-Id: I972c061fcc008f3c37308c5186e6432d766de1bd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2470237
Commit-Queue: Keren Zhu <kerenzhu@chromium.org>
Reviewed-by: default avatarWei Li <weili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#817292}
parent c5056f82
......@@ -29,13 +29,14 @@ void AxExample::CreateExampleView(View* container) {
layout->SetMainAxisAlignment(LayoutAlignment::kStart);
layout->SetCrossAxisAlignment(LayoutAlignment::kStart);
announce_button_ = container->AddChildView(
std::make_unique<MdTextButton>(this, base::ASCIIToUTF16("AnnounceText")));
}
void AxExample::ButtonPressed(Button* sender, const ui::Event& event) {
sender->GetViewAccessibility().AnnounceText(
auto announce_text = [](AxExample* example) {
example->announce_button_->GetViewAccessibility().AnnounceText(
base::ASCIIToUTF16("Button pressed."));
};
announce_button_ = container->AddChildView(std::make_unique<MdTextButton>(
base::BindRepeating(announce_text, base::Unretained(this)),
base::ASCIIToUTF16("AnnounceText")));
}
} // namespace examples
......
......@@ -6,16 +6,16 @@
#define UI_VIEWS_EXAMPLES_AX_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/examples/example_base.h"
namespace views {
class Button;
namespace examples {
// ButtonExample simply counts the number of clicks.
class VIEWS_EXAMPLES_EXPORT AxExample : public ExampleBase,
public ButtonListener {
class VIEWS_EXAMPLES_EXPORT AxExample : public ExampleBase {
public:
AxExample();
~AxExample() override;
......@@ -24,9 +24,6 @@ class VIEWS_EXAMPLES_EXPORT AxExample : public ExampleBase,
void CreateExampleView(View* container) override;
private:
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
Button* announce_button_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(AxExample);
......
......@@ -76,16 +76,17 @@ void AddLabeledRowToGridLayout(GridLayout* layout,
// button in |*primary| is a call-to-action button, and the button in
// |*secondary| is a regular button.
std::vector<std::unique_ptr<MdTextButton>> MakeButtonsInState(
ButtonListener* listener,
Button::ButtonState state) {
std::vector<std::unique_ptr<MdTextButton>> buttons;
const base::string16 button_text = base::ASCIIToUTF16("Button");
auto primary = std::make_unique<views::MdTextButton>(listener, button_text);
auto primary = std::make_unique<views::MdTextButton>(
Button::PressedCallback(), button_text);
primary->SetProminent(true);
primary->SetState(state);
buttons.push_back(std::move(primary));
auto secondary = std::make_unique<views::MdTextButton>(listener, button_text);
auto secondary = std::make_unique<views::MdTextButton>(
Button::PressedCallback(), button_text);
secondary->SetState(state);
buttons.push_back(std::move(secondary));
return buttons;
......@@ -108,19 +109,15 @@ void ButtonStickerSheet::CreateExampleView(View* container) {
AddLabeledRowToGridLayout(layout, std::string(), std::move(plainLabel));
AddLabeledRowToGridLayout(layout, "Default",
MakeButtonsInState(this, Button::STATE_NORMAL));
MakeButtonsInState(Button::STATE_NORMAL));
AddLabeledRowToGridLayout(layout, "Normal",
MakeButtonsInState(this, Button::STATE_NORMAL));
MakeButtonsInState(Button::STATE_NORMAL));
AddLabeledRowToGridLayout(layout, "Hovered",
MakeButtonsInState(this, Button::STATE_HOVERED));
MakeButtonsInState(Button::STATE_HOVERED));
AddLabeledRowToGridLayout(layout, "Pressed",
MakeButtonsInState(this, Button::STATE_PRESSED));
MakeButtonsInState(Button::STATE_PRESSED));
AddLabeledRowToGridLayout(layout, "Disabled",
MakeButtonsInState(this, Button::STATE_DISABLED));
}
void ButtonStickerSheet::ButtonPressed(Button* button, const ui::Event& event) {
// Ignore button presses.
MakeButtonsInState(Button::STATE_DISABLED));
}
} // namespace examples
......
......@@ -6,7 +6,6 @@
#define UI_VIEWS_EXAMPLES_BUTTON_STICKER_SHEET_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/examples/example_base.h"
namespace views {
......@@ -16,8 +15,7 @@ namespace examples {
// design button styles. This example only looks right with `--secondary-ui-md`.
// It is designed to be as visually similar to the UI Harmony spec's sticker
// sheet for buttons as possible.
class VIEWS_EXAMPLES_EXPORT ButtonStickerSheet : public ExampleBase,
public ButtonListener {
class VIEWS_EXAMPLES_EXPORT ButtonStickerSheet : public ExampleBase {
public:
ButtonStickerSheet();
~ButtonStickerSheet() override;
......@@ -25,9 +23,6 @@ class VIEWS_EXAMPLES_EXPORT ButtonStickerSheet : public ExampleBase,
// ExampleBase:
void CreateExampleView(View* container) override;
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
private:
DISALLOW_COPY_AND_ASSIGN(ButtonStickerSheet);
};
......
......@@ -27,11 +27,12 @@
namespace views {
namespace examples {
class ThemeTrackingCheckbox : public views::Checkbox,
public views::ButtonListener {
class ThemeTrackingCheckbox : public views::Checkbox {
public:
explicit ThemeTrackingCheckbox(const base::string16& label)
: Checkbox(label, this) {}
: Checkbox(label,
base::BindRepeating(&ThemeTrackingCheckbox::ButtonPressed,
base::Unretained(this))) {}
ThemeTrackingCheckbox(const ThemeTrackingCheckbox&) = delete;
ThemeTrackingCheckbox& operator=(const ThemeTrackingCheckbox&) = delete;
~ThemeTrackingCheckbox() override = default;
......@@ -39,12 +40,10 @@ class ThemeTrackingCheckbox : public views::Checkbox,
// views::Checkbox
void OnThemeChanged() override {
views::Checkbox::OnThemeChanged();
SetChecked(GetNativeTheme()->ShouldUseDarkColors());
}
// ButtonListener
void ButtonPressed(views::Button* sender, const ui::Event& event) override {
void ButtonPressed() {
GetNativeTheme()->set_use_dark_colors(GetChecked());
GetWidget()->ThemeChanged();
}
......@@ -52,10 +51,10 @@ class ThemeTrackingCheckbox : public views::Checkbox,
class TextVectorImageButton : public views::MdTextButton {
public:
TextVectorImageButton(ButtonListener* listener,
TextVectorImageButton(PressedCallback callback,
const base::string16& text,
const gfx::VectorIcon& icon)
: MdTextButton(listener, text), icon_(icon) {}
: MdTextButton(callback, text), icon_(icon) {}
TextVectorImageButton(const TextVectorImageButton&) = delete;
TextVectorImageButton& operator=(const TextVectorImageButton&) = delete;
~TextVectorImageButton() override = default;
......@@ -125,7 +124,9 @@ ColoredDialogChooser::ColoredDialogChooser() {
l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_CHOOSER_CHECKBOX)));
AddChildView(std::make_unique<TextVectorImageButton>(
this, l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_CHOOSER_BUTTON),
base::BindRepeating(&ColoredDialogChooser::ButtonPressed,
base::Unretained(this)),
l10n_util::GetStringUTF16(IDS_COLORED_DIALOG_CHOOSER_BUTTON),
views::kInfoIcon));
confirmation_label_ = AddChildView(
......@@ -135,8 +136,7 @@ ColoredDialogChooser::ColoredDialogChooser() {
ColoredDialogChooser::~ColoredDialogChooser() = default;
void ColoredDialogChooser::ButtonPressed(Button* sender,
const ui::Event& event) {
void ColoredDialogChooser::ButtonPressed() {
// Create the colored dialog.
views::Widget* widget = DialogDelegate::CreateDialogWidget(
new ColoredDialog(base::BindOnce(&ColoredDialogChooser::OnFeedbackSubmit,
......
......@@ -6,7 +6,6 @@
#define UI_VIEWS_EXAMPLES_COLORED_DIALOG_EXAMPLE_H_
#include "base/timer/timer.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/view.h"
......@@ -14,7 +13,6 @@
namespace views {
class Button;
class Label;
namespace examples {
......@@ -41,15 +39,14 @@ class ColoredDialog : public views::DialogDelegateView,
views::Textfield* textfield_;
};
class ColoredDialogChooser : public views::View, public views::ButtonListener {
class ColoredDialogChooser : public views::View {
public:
ColoredDialogChooser();
ColoredDialogChooser(const ColoredDialogChooser&) = delete;
ColoredDialogChooser& operator=(const ColoredDialogChooser&) = delete;
~ColoredDialogChooser() override;
// ButtonListener
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
void ButtonPressed();
private:
void OnFeedbackSubmit(base::string16 text);
......
......@@ -169,8 +169,10 @@ void DialogExample::CreateExampleView(View* container) {
kFixed, kButtonsColumnId, kFixed,
provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL));
show_ = layout->AddView(
std::make_unique<views::MdTextButton>(this, base::ASCIIToUTF16("Show")));
show_ = layout->AddView(std::make_unique<views::MdTextButton>(
base::BindRepeating(&DialogExample::ShowButtonPressed,
base::Unretained(this)),
base::ASCIIToUTF16("Show")));
}
void DialogExample::StartRowWithLabel(GridLayout* layout, const char* label) {
......@@ -194,7 +196,10 @@ void DialogExample::StartTextfieldRow(GridLayout* layout,
}
void DialogExample::AddCheckbox(GridLayout* layout, Checkbox** member) {
auto checkbox = std::make_unique<Checkbox>(base::string16(), this);
auto callback = member == &bubble_ ? &DialogExample::BubbleCheckboxPressed
: &DialogExample::OtherCheckboxPressed;
auto checkbox = std::make_unique<Checkbox>(
base::string16(), base::BindRepeating(callback, base::Unretained(this)));
checkbox->SetChecked(true);
*member = layout->AddView(std::move(checkbox));
}
......@@ -243,11 +248,10 @@ void DialogExample::ResizeDialog() {
widget->OnSizeConstraintsChanged();
}
void DialogExample::ButtonPressed(Button* sender, const ui::Event& event) {
if (sender == show_) {
void DialogExample::ShowButtonPressed() {
if (bubble_->GetChecked()) {
// |bubble| will be destroyed by its widget when the widget is destroyed.
Bubble* bubble = new Bubble(this, sender);
Bubble* bubble = new Bubble(this, show_);
last_dialog_ = bubble;
BubbleDialogDelegateView::CreateBubble(bubble);
} else {
......@@ -266,10 +270,9 @@ void DialogExample::ButtonPressed(Button* sender, const ui::Event& event) {
dialog, example_view()->GetWidget()->GetNativeWindow(), parent);
}
last_dialog_->GetWidget()->Show();
return;
}
}
if (sender == bubble_) {
void DialogExample::BubbleCheckboxPressed() {
if (bubble_->GetChecked() && GetModalType() != ui::MODAL_TYPE_CHILD) {
mode_->SetSelectedIndex(ui::MODAL_TYPE_CHILD);
LogStatus("You nearly always want Child Modal for bubbles.");
......@@ -282,10 +285,11 @@ void DialogExample::ButtonPressed(Button* sender, const ui::Event& event) {
mode_->SetSelectedIndex(ui::MODAL_TYPE_WINDOW);
OnPerformAction();
}
return;
}
}
// Other buttons are all checkboxes. Update the dialog if there is one.
void DialogExample::OtherCheckboxPressed() {
// Buttons other than show and bubble are pressed. They are all checkboxes.
// Update the dialog if there is one.
if (last_dialog_) {
last_dialog_->DialogModelChanged();
ResizeDialog();
......
......@@ -7,7 +7,7 @@
#include "base/macros.h"
#include "ui/base/models/simple_combobox_model.h"
#include "ui/views/controls/button/button.h"
#include "ui/base/ui_base_types.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/examples/example_base.h"
......@@ -25,7 +25,6 @@ namespace examples {
// An example that exercises BubbleDialogDelegateView or DialogDelegateView.
class VIEWS_EXAMPLES_EXPORT DialogExample : public ExampleBase,
public ButtonListener,
public TextfieldController {
public:
DialogExample();
......@@ -61,8 +60,9 @@ class VIEWS_EXAMPLES_EXPORT DialogExample : public ExampleBase,
// Resize the dialog Widget to match the preferred size. Triggers Layout().
void ResizeDialog();
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
void ShowButtonPressed();
void BubbleCheckboxPressed();
void OtherCheckboxPressed();
// TextfieldController:
void ContentsChanged(Textfield* sender,
......
......@@ -125,21 +125,21 @@ void LabelExample::CreateExampleView(View* container) {
AddCustomLabel(container);
}
void LabelExample::ButtonPressed(Button* button, const ui::Event& event) {
if (button == multiline_) {
void LabelExample::MultilineCheckboxPressed() {
custom_label_->SetMultiLine(multiline_->GetChecked());
} else if (button == shadows_) {
}
void LabelExample::ShadowsCheckboxPressed() {
gfx::ShadowValues shadows;
if (shadows_->GetChecked()) {
shadows.push_back(gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED));
shadows.push_back(gfx::ShadowValue(gfx::Vector2d(2, 2), 0, SK_ColorGRAY));
}
custom_label_->SetShadows(shadows);
} else if (button == selectable_) {
}
void LabelExample::SelectableCheckboxPressed() {
custom_label_->SetSelectable(selectable_->GetChecked());
}
custom_label_->parent()->parent()->InvalidateLayout();
custom_label_->SchedulePaint();
}
void LabelExample::ContentsChanged(Textfield* sender,
......@@ -187,12 +187,18 @@ void LabelExample::AddCustomLabel(View* container) {
column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
GridLayout::ColumnSize::kUsePreferred, 0, 0);
layout->StartRow(0, 1);
multiline_ = layout->AddView(
std::make_unique<Checkbox>(base::ASCIIToUTF16("Multiline"), this));
shadows_ = layout->AddView(
std::make_unique<Checkbox>(base::ASCIIToUTF16("Shadows"), this));
selectable_ = layout->AddView(
std::make_unique<Checkbox>(base::ASCIIToUTF16("Selectable"), this));
multiline_ = layout->AddView(std::make_unique<Checkbox>(
base::ASCIIToUTF16("Multiline"),
base::BindRepeating(&LabelExample::MultilineCheckboxPressed,
base::Unretained(this))));
shadows_ = layout->AddView(std::make_unique<Checkbox>(
base::ASCIIToUTF16("Shadows"),
base::BindRepeating(&LabelExample::ShadowsCheckboxPressed,
base::Unretained(this))));
selectable_ = layout->AddView(std::make_unique<Checkbox>(
base::ASCIIToUTF16("Selectable"),
base::BindRepeating(&LabelExample::SelectableCheckboxPressed,
base::Unretained(this))));
layout->AddPaddingRow(0, 8);
column_set = layout->AddColumnSet(2);
......
......@@ -6,7 +6,6 @@
#define UI_VIEWS_EXAMPLES_LABEL_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/examples/example_base.h"
......@@ -20,7 +19,6 @@ class Label;
namespace examples {
class VIEWS_EXAMPLES_EXPORT LabelExample : public ExampleBase,
public ButtonListener,
public TextfieldController {
public:
LabelExample();
......@@ -29,8 +27,9 @@ class VIEWS_EXAMPLES_EXPORT LabelExample : public ExampleBase,
// ExampleBase:
void CreateExampleView(View* container) override;
// ButtonListener:
void ButtonPressed(Button* button, const ui::Event& event) override;
void MultilineCheckboxPressed();
void ShadowsCheckboxPressed();
void SelectableCheckboxPressed();
// TextfieldController:
void ContentsChanged(Textfield* sender,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment