Commit effb7f69 authored by Wei Li's avatar Wei Li Committed by Commit Bot

Use callbacks instead of ButtonListener overrides

Bug: 772945
Change-Id: I8b5af781812a7baaae83cd59371d67528a3c1a61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2458812
Commit-Queue: Wei Li <weili@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815297}
parent 178c9837
......@@ -94,21 +94,38 @@ BubbleExample::~BubbleExample() = default;
void BubbleExample::CreateExampleView(View* container) {
container->SetLayoutManager(std::make_unique<BoxLayout>(
BoxLayout::Orientation::kHorizontal, gfx::Insets(), 10));
no_shadow_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("No Shadow")));
no_shadow_opaque_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("Opaque Border")));
big_shadow_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("Big Shadow")));
small_shadow_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("Small Shadow")));
no_assets_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("No Assets")));
persistent_ = container->AddChildView(
std::make_unique<LabelButton>(this, ASCIIToUTF16("Persistent")));
no_shadow_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&no_shadow_, BubbleBorder::NO_SHADOW, false),
ASCIIToUTF16("No Shadow")));
no_shadow_opaque_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&no_shadow_opaque_,
BubbleBorder::NO_SHADOW_OPAQUE_BORDER, false),
ASCIIToUTF16("Opaque Border")));
big_shadow_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&big_shadow_, BubbleBorder::BIG_SHADOW, false),
ASCIIToUTF16("Big Shadow")));
small_shadow_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&small_shadow_, BubbleBorder::SMALL_SHADOW, false),
ASCIIToUTF16("Small Shadow")));
no_assets_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&no_assets_, BubbleBorder::NO_ASSETS, false),
ASCIIToUTF16("No Assets")));
persistent_ = container->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&BubbleExample::ShowBubble, base::Unretained(this),
&persistent_, BubbleBorder::NO_SHADOW, true),
ASCIIToUTF16("Persistent")));
}
void BubbleExample::ButtonPressed(Button* sender, const ui::Event& event) {
void BubbleExample::ShowBubble(Button** button,
BubbleBorder::Shadow shadow,
bool persistent,
const ui::Event& event) {
static int arrow_index = 0, color_index = 0;
static const int count = base::size(arrows);
arrow_index = (arrow_index + count + (event.IsShiftDown() ? -1 : 1)) % count;
......@@ -119,21 +136,10 @@ void BubbleExample::ButtonPressed(Button* sender, const ui::Event& event) {
arrow = BubbleBorder::FLOAT;
// |bubble| will be destroyed by its widget when the widget is destroyed.
ExampleBubble* bubble = new ExampleBubble(sender, arrow);
ExampleBubble* bubble = new ExampleBubble(*button, arrow);
bubble->set_color(colors[(color_index++) % base::size(colors)]);
if (sender == no_shadow_)
bubble->set_shadow(BubbleBorder::NO_SHADOW);
else if (sender == no_shadow_opaque_)
bubble->set_shadow(BubbleBorder::NO_SHADOW_OPAQUE_BORDER);
else if (sender == big_shadow_)
bubble->set_shadow(BubbleBorder::BIG_SHADOW);
else if (sender == small_shadow_)
bubble->set_shadow(BubbleBorder::SMALL_SHADOW);
else if (sender == no_assets_)
bubble->set_shadow(BubbleBorder::NO_ASSETS);
if (sender == persistent_)
bubble->set_shadow(shadow);
if (persistent)
bubble->set_close_on_deactivate(false);
BubbleDialogDelegateView::CreateBubble(bubble)->Show();
......
......@@ -6,15 +6,19 @@
#define UI_VIEWS_EXAMPLES_BUBBLE_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/events/event.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/examples/views_examples_export.h"
namespace views {
class Button;
namespace examples {
// A Bubble example.
class VIEWS_EXAMPLES_EXPORT BubbleExample : public ExampleBase,
public ButtonListener {
class VIEWS_EXAMPLES_EXPORT BubbleExample : public ExampleBase {
public:
BubbleExample();
~BubbleExample() override;
......@@ -23,8 +27,10 @@ class VIEWS_EXAMPLES_EXPORT BubbleExample : public ExampleBase,
void CreateExampleView(View* container) override;
private:
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
void ShowBubble(Button** button,
BubbleBorder::Shadow shadow,
bool persistent,
const ui::Event& event);
Button* no_shadow_;
Button* no_shadow_opaque_;
......
......@@ -6,6 +6,7 @@
#include <memory>
#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/views/controls/button/checkbox.h"
......@@ -22,12 +23,11 @@ CheckboxExample::~CheckboxExample() = default;
void CheckboxExample::CreateExampleView(View* container) {
container->SetLayoutManager(std::make_unique<FillLayout>());
button_ = container->AddChildView(
std::make_unique<Checkbox>(base::ASCIIToUTF16("Checkbox"), this));
}
void CheckboxExample::ButtonPressed(Button* sender, const ui::Event& event) {
PrintStatus("Pressed! count: %d", ++count_);
button_ = container->AddChildView(std::make_unique<Checkbox>(
base::ASCIIToUTF16("Checkbox"),
base::BindRepeating(
[](int* count) { PrintStatus("Pressed! count: %d", ++(*count)); },
&count_)));
}
} // namespace examples
......
......@@ -6,8 +6,8 @@
#define UI_VIEWS_EXAMPLES_CHECKBOX_EXAMPLE_H_
#include "base/macros.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/examples/views_examples_export.h"
namespace views {
class Checkbox;
......@@ -15,8 +15,7 @@ class Checkbox;
namespace examples {
// CheckboxExample exercises a Checkbox control.
class VIEWS_EXAMPLES_EXPORT CheckboxExample : public ExampleBase,
public ButtonListener {
class VIEWS_EXAMPLES_EXPORT CheckboxExample : public ExampleBase {
public:
CheckboxExample();
~CheckboxExample() override;
......@@ -25,9 +24,6 @@ class VIEWS_EXAMPLES_EXPORT CheckboxExample : public ExampleBase,
void CreateExampleView(View* container) override;
private:
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
// The only control in this test.
Checkbox* button_ = nullptr;
......
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