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

Add PerformActionCallback to views::Combobox

This makes views::Combobox use a base::RepeatingCallback internally.
Pre-existing uses of ComboboxListener are wrapped in a callback in
set_listener(). Cleanups removing ComboboxListeners will follow.

Bug: 772945
Change-Id: I2c1b4a7637ba5125fd7774e1f18f4751ced9959c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2401951
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@{#805493}
parent 7cfa2375
......@@ -30,7 +30,6 @@
#include "ui/views/background.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/controls/combobox/combobox_util.h"
#include "ui/views/controls/combobox/empty_combobox_model.h"
#include "ui/views/controls/focus_ring.h"
......@@ -677,10 +676,9 @@ void Combobox::OnPerformAction() {
NotifyAccessibilityEvent(ax::mojom::Event::kValueChanged, true);
SchedulePaint();
if (listener_)
listener_->OnPerformAction(this);
callback_.Run();
// Note |this| may be deleted by |listener_|.
// Note |this| may be deleted by |callback_|.
}
gfx::Size Combobox::GetContentSize() const {
......
......@@ -6,6 +6,7 @@
#define UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_
#include <memory>
#include <utility>
#include "base/macros.h"
#include "base/scoped_observer.h"
......@@ -14,6 +15,7 @@
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/combobox_model_observer.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/controls/prefix_delegate.h"
#include "ui/views/style/typography.h"
......@@ -30,7 +32,6 @@ namespace test {
class ComboboxTestApi;
}
class ComboboxListener;
class FocusRing;
class MenuRunner;
class PrefixSelector;
......@@ -44,6 +45,8 @@ class VIEWS_EXPORT Combobox : public View,
public:
METADATA_HEADER(Combobox);
using PerformActionCallback = base::RepeatingClosure;
static constexpr int kDefaultComboboxTextContext = style::CONTEXT_BUTTON;
static constexpr int kDefaultComboboxTextStyle = style::STYLE_PRIMARY;
......@@ -63,8 +66,24 @@ class VIEWS_EXPORT Combobox : public View,
const gfx::FontList& GetFontList() const;
// Sets the listener which will be called when a selection has been made.
void set_listener(ComboboxListener* listener) { listener_ = listener; }
// TODO(pbos): Migrate users of this to set_callback().
void set_listener(ComboboxListener* listener) {
if (!listener) {
set_callback(base::DoNothing());
return;
}
set_callback(base::BindRepeating(
[](ComboboxListener* listener, Combobox* combobox) {
listener->OnPerformAction(combobox);
},
listener, this));
}
// Sets the callback which will be called when a selection has been made.
void set_callback(PerformActionCallback callback) {
callback_ = std::move(callback);
}
// Gets/Sets the selected index.
int GetSelectedIndex() const { return selected_index_; }
......@@ -170,8 +189,8 @@ class VIEWS_EXPORT Combobox : public View,
// in the drop-down menu.
const int text_style_;
// Our listener. Not owned. Notified when the selected index change.
ComboboxListener* listener_ = nullptr;
// Callback notified when the selected index changes.
PerformActionCallback callback_ = base::DoNothing();
// The current selected index; -1 and means no selection.
int selected_index_ = -1;
......
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