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