Commit d9a7628f authored by Peter Boström's avatar Peter Boström Committed by Commit Bot

Add PressedCallback to views::Button

Removes |listener_| from the internal implementation, but still
providing set_listener() that wraps a ButtonListener* in a callback.

Removal of ButtonListener will be piecemeal as there's about 500
references to it, though I intend to continue until it's gone.

Bug: 772945
Change-Id: I9ff386b40a75bee58adba5c4ad9a3304a2c69d4e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2392436
Commit-Queue: Peter Boström <pbos@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804463}
parent 56d27510
...@@ -480,9 +480,13 @@ void Button::AnimationProgressed(const gfx::Animation* animation) { ...@@ -480,9 +480,13 @@ void Button::AnimationProgressed(const gfx::Animation* animation) {
SchedulePaint(); SchedulePaint();
} }
Button::Button(ButtonListener* listener) Button::Button(ButtonListener* listener) : Button(PressedCallback()) {
set_listener(listener);
}
Button::Button(PressedCallback callback)
: AnimationDelegateViews(this), : AnimationDelegateViews(this),
listener_(listener), callback_(std::move(callback)),
ink_drop_base_color_(gfx::kPlaceholderColor) { ink_drop_base_color_(gfx::kPlaceholderColor) {
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
SetProperty(kIsButtonProperty, true); SetProperty(kIsButtonProperty, true);
...@@ -505,8 +509,7 @@ void Button::NotifyClick(const ui::Event& event) { ...@@ -505,8 +509,7 @@ void Button::NotifyClick(const ui::Event& event) {
// We can be called when there is no listener, in cases like double clicks on // We can be called when there is no listener, in cases like double clicks on
// menu buttons etc. // menu buttons etc.
if (listener_) callback_.Run(event);
listener_->ButtonPressed(this, event);
} }
void Button::OnClickCanceled(const ui::Event& event) { void Button::OnClickCanceled(const ui::Event& event) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define UI_VIEWS_CONTROLS_BUTTON_BUTTON_H_ #define UI_VIEWS_CONTROLS_BUTTON_BUTTON_H_
#include <memory> #include <memory>
#include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -31,6 +32,8 @@ class Event; ...@@ -31,6 +32,8 @@ class Event;
// An interface implemented by an object to let it know that a button was // An interface implemented by an object to let it know that a button was
// pressed. // pressed.
// Deprecated, please use callback versions instead.
// TODO(pbos): Replace ButtonListener with ClickedCallback methods below.
class VIEWS_EXPORT ButtonListener { class VIEWS_EXPORT ButtonListener {
public: public:
virtual void ButtonPressed(Button* sender, const ui::Event& event) = 0; virtual void ButtonPressed(Button* sender, const ui::Event& event) = 0;
...@@ -97,6 +100,8 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -97,6 +100,8 @@ class VIEWS_EXPORT Button : public InkDropHostView,
static ButtonState GetButtonStateFrom(ui::NativeTheme::State state); static ButtonState GetButtonStateFrom(ui::NativeTheme::State state);
using PressedCallback = base::RepeatingCallback<void(const ui::Event& event)>;
// Make the button focusable as per the platform. // Make the button focusable as per the platform.
void SetFocusForPlatform(); void SetFocusForPlatform();
...@@ -105,7 +110,23 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -105,7 +110,23 @@ class VIEWS_EXPORT Button : public InkDropHostView,
int tag() const { return tag_; } int tag() const { return tag_; }
void set_tag(int tag) { tag_ = tag; } void set_tag(int tag) { tag_ = tag; }
void set_listener(ButtonListener* listener) { listener_ = listener; } // TODO(pbos): Replace uses of this with set_callback().
void set_listener(ButtonListener* listener) {
if (!listener) {
set_callback(base::DoNothing());
return;
}
set_callback(base::BindRepeating(
[](ButtonListener* listener, Button* button, const ui::Event& event) {
listener->ButtonPressed(button, event);
},
listener, this));
}
void set_callback(PressedCallback callback) {
callback_ = std::move(callback);
}
void SetAccessibleName(const base::string16& name); void SetAccessibleName(const base::string16& name);
const base::string16& GetAccessibleName() const; const base::string16& GetAccessibleName() const;
...@@ -233,6 +254,7 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -233,6 +254,7 @@ class VIEWS_EXPORT Button : public InkDropHostView,
// true of buttons that don't have a listener - e.g. menubuttons where there's // true of buttons that don't have a listener - e.g. menubuttons where there's
// no default action and checkboxes. // no default action and checkboxes.
explicit Button(ButtonListener* listener = nullptr); explicit Button(ButtonListener* listener = nullptr);
explicit Button(PressedCallback callback);
// Called when the button has been clicked or tapped and should request focus // Called when the button has been clicked or tapped and should request focus
// if necessary. // if necessary.
...@@ -302,10 +324,12 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -302,10 +324,12 @@ class VIEWS_EXPORT Button : public InkDropHostView,
base::string16 accessible_name_; base::string16 accessible_name_;
// The button's listener. Notified when clicked. // The button's listener. Notified when clicked.
ButtonListener* listener_; PressedCallback callback_;
// The id tag associated with this button. Used to disambiguate buttons in // The id tag associated with this button. Used to disambiguate buttons in
// the ButtonListener implementation. // the ButtonListener implementation.
// TODO(pbos): Remove this after ButtonListener is gone since disambiguation
// shouldn't be needed.
int tag_ = -1; int tag_ = -1;
ButtonState state_ = STATE_NORMAL; ButtonState state_ = STATE_NORMAL;
......
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