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

Extend PressedCallback support to MdTextButton

This removes ButtonListener from BubbleDialogModelHost and adds
Button::PressedCallback versions of MdTextButton and LabelButton
constructors to support it.

Bug: 772945, 1106422
Change-Id: I5a6fc50a714e6ac6701af588d5a428ea291bd008
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2398321
Commit-Queue: Peter Boström <pbos@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Auto-Submit: Peter Boström <pbos@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804920}
parent 4984f1de
...@@ -89,9 +89,12 @@ BubbleDialogModelHost::BubbleDialogModelHost( ...@@ -89,9 +89,12 @@ BubbleDialogModelHost::BubbleDialogModelHost(
// OnDialogInitialized() will not work until then. // OnDialogInitialized() will not work until then.
auto* extra_button = model_->extra_button(GetPassKey()); auto* extra_button = model_->extra_button(GetPassKey());
if (extra_button) { if (extra_button) {
OnViewCreatedForField(SetExtraView(std::make_unique<MdTextButton>( OnViewCreatedForField(
this, extra_button->label(GetPassKey()))), SetExtraView(std::make_unique<MdTextButton>(
extra_button); base::BindRepeating(&ui::DialogModelButton::OnPressed,
base::Unretained(extra_button), GetPassKey()),
extra_button->label(GetPassKey()))),
extra_button);
} }
SetButtons(button_mask); SetButtons(button_mask);
...@@ -329,13 +332,6 @@ void BubbleDialogModelHost::NotifyComboboxSelectedIndexChanged( ...@@ -329,13 +332,6 @@ void BubbleDialogModelHost::NotifyComboboxSelectedIndexChanged(
->OnSelectedIndexChanged(GetPassKey(), combobox->GetSelectedIndex()); ->OnSelectedIndexChanged(GetPassKey(), combobox->GetSelectedIndex());
} }
void BubbleDialogModelHost::ButtonPressed(Button* sender,
const ui::Event& event) {
view_to_field_[sender]
->AsButton(GetPassKey())
->OnPressed(GetPassKey(), event);
}
void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) { void BubbleDialogModelHost::OnPerformAction(Combobox* combobox) {
// TODO(pbos): This should be a subscription through the Combobox directly, // TODO(pbos): This should be a subscription through the Combobox directly,
// but Combobox right now doesn't support listening to selected-index changes. // but Combobox right now doesn't support listening to selected-index changes.
......
...@@ -31,7 +31,6 @@ class Textfield; ...@@ -31,7 +31,6 @@ class Textfield;
// platform-agnostic model-delegate code. // platform-agnostic model-delegate code.
class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView, class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView,
public ui::DialogModelHost, public ui::DialogModelHost,
public ButtonListener,
public ComboboxListener { public ComboboxListener {
public: public:
// Constructs a BubbleDialogModelHost, which for most purposes is to used as a // Constructs a BubbleDialogModelHost, which for most purposes is to used as a
...@@ -53,9 +52,6 @@ class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView, ...@@ -53,9 +52,6 @@ class VIEWS_EXPORT BubbleDialogModelHost : public BubbleDialogDelegateView,
void SelectAllText(int unique_id) override; void SelectAllText(int unique_id) override;
void OnFieldAdded(ui::DialogModelField* field) override; void OnFieldAdded(ui::DialogModelField* field) override;
// ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
// ComboboxListener: // ComboboxListener:
void OnPerformAction(views::Combobox* combobox) override; void OnPerformAction(views::Combobox* combobox) override;
......
...@@ -480,9 +480,8 @@ void Button::AnimationProgressed(const gfx::Animation* animation) { ...@@ -480,9 +480,8 @@ void Button::AnimationProgressed(const gfx::Animation* animation) {
SchedulePaint(); SchedulePaint();
} }
Button::Button(ButtonListener* listener) : Button(PressedCallback()) { Button::Button(ButtonListener* listener)
set_listener(listener); : Button(ListenerToPressedCallback(this, listener)) {}
}
Button::Button(PressedCallback callback) Button::Button(PressedCallback callback)
: AnimationDelegateViews(this), : AnimationDelegateViews(this),
...@@ -496,6 +495,18 @@ Button::Button(PressedCallback callback) ...@@ -496,6 +495,18 @@ Button::Button(PressedCallback callback)
this, std::make_unique<DefaultButtonControllerDelegate>(this)); this, std::make_unique<DefaultButtonControllerDelegate>(this));
} }
Button::PressedCallback Button::ListenerToPressedCallback(
Button* button,
ButtonListener* listener) {
if (!listener)
return base::DoNothing();
return base::BindRepeating(
[](ButtonListener* listener, Button* button, const ui::Event& event) {
listener->ButtonPressed(button, event);
},
listener, button);
}
void Button::RequestFocusFromEvent() { void Button::RequestFocusFromEvent() {
if (request_focus_on_press_) if (request_focus_on_press_)
RequestFocus(); RequestFocus();
......
...@@ -112,16 +112,7 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -112,16 +112,7 @@ class VIEWS_EXPORT Button : public InkDropHostView,
// TODO(pbos): Replace uses of this with set_callback(). // TODO(pbos): Replace uses of this with set_callback().
void set_listener(ButtonListener* listener) { void set_listener(ButtonListener* listener) {
if (!listener) { set_callback(ListenerToPressedCallback(this, 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) { void set_callback(PressedCallback callback) {
...@@ -256,6 +247,12 @@ class VIEWS_EXPORT Button : public InkDropHostView, ...@@ -256,6 +247,12 @@ class VIEWS_EXPORT Button : public InkDropHostView,
explicit Button(ButtonListener* listener = nullptr); explicit Button(ButtonListener* listener = nullptr);
explicit Button(PressedCallback callback); explicit Button(PressedCallback callback);
// Wraps a ButtonListener for |button| into a PressedCallback, used during
// migration from ButtonListener to PressedCallback to let child classes use
// PressedCallback versions of parent constructors.
static PressedCallback ListenerToPressedCallback(Button* button,
ButtonListener* listener);
// 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.
virtual void RequestFocusFromEvent(); virtual void RequestFocusFromEvent();
......
...@@ -35,7 +35,14 @@ namespace views { ...@@ -35,7 +35,14 @@ namespace views {
LabelButton::LabelButton(ButtonListener* listener, LabelButton::LabelButton(ButtonListener* listener,
const base::string16& text, const base::string16& text,
int button_context) int button_context)
: Button(listener), : LabelButton(ListenerToPressedCallback(this, listener),
text,
button_context) {}
LabelButton::LabelButton(PressedCallback callback,
const base::string16& text,
int button_context)
: Button(std::move(callback)),
cached_normal_font_list_( cached_normal_font_list_(
style::GetFont(button_context, style::STYLE_PRIMARY)), style::GetFont(button_context, style::STYLE_PRIMARY)),
cached_default_button_font_list_( cached_default_button_font_list_(
......
...@@ -33,12 +33,17 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -33,12 +33,17 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
public: public:
METADATA_HEADER(LabelButton); METADATA_HEADER(LabelButton);
// Creates a LabelButton with ButtonPressed() events sent to |listener| and // TODO(pbos): Replace with uses of PressedCallback version below.
// label |text|. |button_context| is a value from views::style::TextContext
// and determines the appearance of |text|.
explicit LabelButton(ButtonListener* listener = nullptr, explicit LabelButton(ButtonListener* listener = nullptr,
const base::string16& text = base::string16(), const base::string16& text = base::string16(),
int button_context = style::CONTEXT_BUTTON); int button_context = style::CONTEXT_BUTTON);
// Creates a LabelButton with pressed events sent to |callback| and label
// |text|. |button_context| is a value from views::style::TextContext and
// determines the appearance of |text|.
explicit LabelButton(PressedCallback callback,
const base::string16& text = base::string16(),
int button_context = style::CONTEXT_BUTTON);
~LabelButton() override; ~LabelButton() override;
// Gets or sets the image shown for the specified button state. // Gets or sets the image shown for the specified button state.
......
...@@ -33,7 +33,14 @@ namespace views { ...@@ -33,7 +33,14 @@ namespace views {
MdTextButton::MdTextButton(ButtonListener* listener, MdTextButton::MdTextButton(ButtonListener* listener,
const base::string16& text, const base::string16& text,
int button_context) int button_context)
: LabelButton(listener, text, button_context) { : MdTextButton(ListenerToPressedCallback(this, listener),
text,
button_context) {}
MdTextButton::MdTextButton(PressedCallback callback,
const base::string16& text,
int button_context)
: LabelButton(std::move(callback), text, button_context) {
SetInkDropMode(InkDropMode::ON); SetInkDropMode(InkDropMode::ON);
set_has_ink_drop_action_on_click(true); set_has_ink_drop_action_on_click(true);
set_show_ink_drop_when_hot_tracked(true); set_show_ink_drop_when_hot_tracked(true);
......
...@@ -19,10 +19,13 @@ class VIEWS_EXPORT MdTextButton : public LabelButton { ...@@ -19,10 +19,13 @@ class VIEWS_EXPORT MdTextButton : public LabelButton {
public: public:
METADATA_HEADER(MdTextButton); METADATA_HEADER(MdTextButton);
// TODO(pbos): Replace uses of this with PressedCallback version below.
explicit MdTextButton(ButtonListener* listener = nullptr, explicit MdTextButton(ButtonListener* listener = nullptr,
const base::string16& text = base::string16(), const base::string16& text = base::string16(),
int button_context = style::CONTEXT_BUTTON_MD); int button_context = style::CONTEXT_BUTTON_MD);
explicit MdTextButton(PressedCallback callback,
const base::string16& text = base::string16(),
int button_context = style::CONTEXT_BUTTON_MD);
~MdTextButton() override; ~MdTextButton() override;
// See |is_prominent_|. // See |is_prominent_|.
......
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