Commit 2b8e70ef authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Change ButtonPressed overrides to callbacks: ui/views/controls/

Bug: 772945
Change-Id: I58e8a596aeeca4a78b981600170c134b52c3a214
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2488909
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819446}
parent 75e3376c
...@@ -68,7 +68,8 @@ gfx::ImageSkia GetImageSkiaFromImageModel(const ui::ImageModel* model, ...@@ -68,7 +68,8 @@ gfx::ImageSkia GetImageSkiaFromImageModel(const ui::ImageModel* model,
// The transparent button which holds a button state but is not rendered. // The transparent button which holds a button state but is not rendered.
class TransparentButton : public Button { class TransparentButton : public Button {
public: public:
explicit TransparentButton(ButtonListener* listener) : Button(listener) { explicit TransparentButton(PressedCallback callback)
: Button(std::move(callback)) {
SetFocusBehavior(FocusBehavior::NEVER); SetFocusBehavior(FocusBehavior::NEVER);
button_controller()->set_notify_action( button_controller()->set_notify_action(
ButtonController::NotifyAction::kOnPress); ButtonController::NotifyAction::kOnPress);
...@@ -243,7 +244,9 @@ Combobox::Combobox(std::unique_ptr<ui::ComboboxModel> model, ...@@ -243,7 +244,9 @@ Combobox::Combobox(std::unique_ptr<ui::ComboboxModel> model,
Combobox::Combobox(ui::ComboboxModel* model, int text_context, int text_style) Combobox::Combobox(ui::ComboboxModel* model, int text_context, int text_style)
: text_context_(text_context), : text_context_(text_context),
text_style_(text_style), text_style_(text_style),
arrow_button_(new TransparentButton(this)) { arrow_button_(new TransparentButton(
base::BindRepeating(&Combobox::ArrowButtonPressed,
base::Unretained(this)))) {
SetModel(model); SetModel(model);
#if defined(OS_APPLE) #if defined(OS_APPLE)
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
...@@ -551,16 +554,6 @@ bool Combobox::HandleAccessibleAction(const ui::AXActionData& action_data) { ...@@ -551,16 +554,6 @@ bool Combobox::HandleAccessibleAction(const ui::AXActionData& action_data) {
return View::HandleAccessibleAction(action_data); return View::HandleAccessibleAction(action_data);
} }
void Combobox::ButtonPressed(Button* sender, const ui::Event& event) {
if (!GetEnabled())
return;
// TODO(hajimehoshi): Fix the problem that the arrow button blinks when
// cliking this while the dropdown menu is opened.
if ((base::TimeTicks::Now() - closed_time_) > kMinimumTimeBetweenButtonClicks)
ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
}
void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) { void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) {
DCHECK_EQ(model_, model); DCHECK_EQ(model_, model);
...@@ -648,6 +641,16 @@ void Combobox::PaintIconAndText(gfx::Canvas* canvas) { ...@@ -648,6 +641,16 @@ void Combobox::PaintIconAndText(gfx::Canvas* canvas) {
PaintComboboxArrow(text_color, arrow_bounds, canvas); PaintComboboxArrow(text_color, arrow_bounds, canvas);
} }
void Combobox::ArrowButtonPressed(const ui::Event& event) {
if (!GetEnabled())
return;
// TODO(hajimehoshi): Fix the problem that the arrow button blinks when
// cliking this while the dropdown menu is opened.
if ((base::TimeTicks::Now() - closed_time_) > kMinimumTimeBetweenButtonClicks)
ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
}
void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
constexpr int kMenuBorderWidthTop = 1; constexpr int kMenuBorderWidthTop = 1;
// Menu's requested position's width should be the same as local bounds so the // Menu's requested position's width should be the same as local bounds so the
......
...@@ -40,7 +40,6 @@ class PrefixSelector; ...@@ -40,7 +40,6 @@ class PrefixSelector;
// Combobox has two distinct parts, the drop down arrow and the text. // Combobox has two distinct parts, the drop down arrow and the text.
class VIEWS_EXPORT Combobox : public View, class VIEWS_EXPORT Combobox : public View,
public PrefixDelegate, public PrefixDelegate,
public ButtonListener,
public ui::ComboboxModelObserver { public ui::ComboboxModelObserver {
public: public:
METADATA_HEADER(Combobox); METADATA_HEADER(Combobox);
...@@ -119,9 +118,6 @@ class VIEWS_EXPORT Combobox : public View, ...@@ -119,9 +118,6 @@ class VIEWS_EXPORT Combobox : public View,
void SetSelectedRow(int row) override; void SetSelectedRow(int row) override;
base::string16 GetTextForRow(int row) override; base::string16 GetTextForRow(int row) override;
// Overridden from ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
protected: protected:
// Overridden from ComboboxModelObserver: // Overridden from ComboboxModelObserver:
void OnComboboxModelChanged(ui::ComboboxModel* model) override; void OnComboboxModelChanged(ui::ComboboxModel* model) override;
...@@ -144,6 +140,9 @@ class VIEWS_EXPORT Combobox : public View, ...@@ -144,6 +140,9 @@ class VIEWS_EXPORT Combobox : public View,
// Draws the selected value of the drop down list // Draws the selected value of the drop down list
void PaintIconAndText(gfx::Canvas* canvas); void PaintIconAndText(gfx::Canvas* canvas);
// Opens the dropdown menu in response to |event|.
void ArrowButtonPressed(const ui::Event& event);
// Show the drop down list // Show the drop down list
void ShowDropDownMenu(ui::MenuSourceType source_type); void ShowDropDownMenu(ui::MenuSourceType source_type);
......
...@@ -64,7 +64,7 @@ namespace { ...@@ -64,7 +64,7 @@ namespace {
class Arrow : public Button { class Arrow : public Button {
public: public:
explicit Arrow(ButtonListener* listener) : Button(listener) { explicit Arrow(PressedCallback callback) : Button(std::move(callback)) {
// Similar to Combobox's TransparentButton. // Similar to Combobox's TransparentButton.
SetFocusBehavior(FocusBehavior::NEVER); SetFocusBehavior(FocusBehavior::NEVER);
button_controller()->set_notify_action( button_controller()->set_notify_action(
...@@ -333,7 +333,8 @@ EditableCombobox::EditableCombobox( ...@@ -333,7 +333,8 @@ EditableCombobox::EditableCombobox(
textfield_->SetExtraInsets(gfx::Insets( textfield_->SetExtraInsets(gfx::Insets(
/*top=*/0, /*left=*/0, /*bottom=*/0, /*top=*/0, /*left=*/0, /*bottom=*/0,
/*right=*/kComboboxArrowContainerWidth - kComboboxArrowPaddingWidth)); /*right=*/kComboboxArrowContainerWidth - kComboboxArrowPaddingWidth));
arrow_ = AddChildView(std::make_unique<Arrow>(this)); arrow_ = AddChildView(std::make_unique<Arrow>(base::BindRepeating(
&EditableCombobox::ArrowButtonPressed, base::Unretained(this))));
} }
SetLayoutManager(std::make_unique<views::FillLayout>()); SetLayoutManager(std::make_unique<views::FillLayout>());
} }
...@@ -446,14 +447,6 @@ void EditableCombobox::OnViewBlurred(View* observed_view) { ...@@ -446,14 +447,6 @@ void EditableCombobox::OnViewBlurred(View* observed_view) {
CloseMenu(); CloseMenu();
} }
void EditableCombobox::ButtonPressed(Button* sender, const ui::Event& event) {
textfield_->RequestFocus();
if (menu_runner_ && menu_runner_->IsRunning())
CloseMenu();
else
ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
}
void EditableCombobox::OnLayoutIsAnimatingChanged( void EditableCombobox::OnLayoutIsAnimatingChanged(
views::AnimatingLayoutManager* source, views::AnimatingLayoutManager* source,
bool is_animating) { bool is_animating) {
...@@ -496,6 +489,14 @@ void EditableCombobox::HandleNewContent(const base::string16& new_content) { ...@@ -496,6 +489,14 @@ void EditableCombobox::HandleNewContent(const base::string16& new_content) {
menu_model_->UpdateItemsShown(); menu_model_->UpdateItemsShown();
} }
void EditableCombobox::ArrowButtonPressed(const ui::Event& event) {
textfield_->RequestFocus();
if (menu_runner_ && menu_runner_->IsRunning())
CloseMenu();
else
ShowDropDownMenu(ui::GetMenuSourceTypeForEvent(event));
}
void EditableCombobox::ShowDropDownMenu(ui::MenuSourceType source_type) { void EditableCombobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
constexpr int kMenuBorderWidthTop = 1; constexpr int kMenuBorderWidthTop = 1;
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "ui/base/ui_base_types.h" #include "ui/base/ui_base_types.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/textfield/textfield_controller.h" #include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/layout/animating_layout_manager.h" #include "ui/views/layout/animating_layout_manager.h"
#include "ui/views/style/typography.h" #include "ui/views/style/typography.h"
...@@ -33,6 +32,7 @@ class Event; ...@@ -33,6 +32,7 @@ class Event;
} // namespace ui } // namespace ui
namespace views { namespace views {
class Button;
class EditableComboboxMenuModel; class EditableComboboxMenuModel;
class EditableComboboxPreTargetHandler; class EditableComboboxPreTargetHandler;
class MenuRunner; class MenuRunner;
...@@ -43,7 +43,6 @@ class VIEWS_EXPORT EditableCombobox ...@@ -43,7 +43,6 @@ class VIEWS_EXPORT EditableCombobox
: public View, : public View,
public TextfieldController, public TextfieldController,
public ViewObserver, public ViewObserver,
public ButtonListener,
public views::AnimatingLayoutManager::Observer { public views::AnimatingLayoutManager::Observer {
public: public:
METADATA_HEADER(EditableCombobox); METADATA_HEADER(EditableCombobox);
...@@ -123,6 +122,9 @@ class VIEWS_EXPORT EditableCombobox ...@@ -123,6 +122,9 @@ class VIEWS_EXPORT EditableCombobox
// Notifies listener of new content and updates the menu items to show. // Notifies listener of new content and updates the menu items to show.
void HandleNewContent(const base::string16& new_content); void HandleNewContent(const base::string16& new_content);
// Toggles the dropdown menu in response to |event|.
void ArrowButtonPressed(const ui::Event& event);
// Shows the drop-down menu. // Shows the drop-down menu.
void ShowDropDownMenu(ui::MenuSourceType source_type = ui::MENU_SOURCE_NONE); void ShowDropDownMenu(ui::MenuSourceType source_type = ui::MENU_SOURCE_NONE);
...@@ -142,9 +144,6 @@ class VIEWS_EXPORT EditableCombobox ...@@ -142,9 +144,6 @@ class VIEWS_EXPORT EditableCombobox
// Overridden from ViewObserver: // Overridden from ViewObserver:
void OnViewBlurred(View* observed_view) override; void OnViewBlurred(View* observed_view) override;
// Overridden from ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
// Overridden from views::AnimatingLayoutManager::Observer: // Overridden from views::AnimatingLayoutManager::Observer:
void OnLayoutIsAnimatingChanged(views::AnimatingLayoutManager* source, void OnLayoutIsAnimatingChanged(views::AnimatingLayoutManager* source,
bool is_animating) override; bool is_animating) override;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "ui/views/controls/scrollbar/base_scroll_bar_button.h" #include "ui/views/controls/scrollbar/base_scroll_bar_button.h"
#include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "ui/display/screen.h" #include "ui/display/screen.h"
...@@ -12,9 +14,9 @@ ...@@ -12,9 +14,9 @@
namespace views { namespace views {
BaseScrollBarButton::BaseScrollBarButton(ButtonListener* listener, BaseScrollBarButton::BaseScrollBarButton(PressedCallback callback,
const base::TickClock* tick_clock) const base::TickClock* tick_clock)
: Button(listener), : Button(std::move(callback)),
repeater_(base::BindRepeating(&BaseScrollBarButton::RepeaterNotifyClick, repeater_(base::BindRepeating(&BaseScrollBarButton::RepeaterNotifyClick,
base::Unretained(this)), base::Unretained(this)),
tick_clock) { tick_clock) {
......
...@@ -30,7 +30,7 @@ class VIEWS_EXPORT BaseScrollBarButton : public Button { ...@@ -30,7 +30,7 @@ class VIEWS_EXPORT BaseScrollBarButton : public Button {
public: public:
METADATA_HEADER(BaseScrollBarButton); METADATA_HEADER(BaseScrollBarButton);
explicit BaseScrollBarButton(ButtonListener* listener, explicit BaseScrollBarButton(PressedCallback callback,
const base::TickClock* tick_clock = nullptr); const base::TickClock* tick_clock = nullptr);
~BaseScrollBarButton() override; ~BaseScrollBarButton() override;
......
...@@ -24,31 +24,28 @@ using testing::_; ...@@ -24,31 +24,28 @@ using testing::_;
using testing::AtLeast; using testing::AtLeast;
using testing::AtMost; using testing::AtMost;
class MockButtonListener : public ButtonListener { class MockButtonCallback {
public: public:
MockButtonListener() = default; MockButtonCallback() = default;
MockButtonListener(const MockButtonListener&) = delete; MockButtonCallback(const MockButtonCallback&) = delete;
MockButtonListener& operator=(const MockButtonListener&) = delete; MockButtonCallback& operator=(const MockButtonCallback&) = delete;
~MockButtonListener() override = default; ~MockButtonCallback() = default;
// ButtonListener: MOCK_METHOD(void, ButtonPressed, ());
MOCK_METHOD(void,
ButtonPressed,
(Button * sender, const ui::Event& event),
(override));
}; };
class BaseScrollBarButtonTest : public testing::Test { class BaseScrollBarButtonTest : public testing::Test {
public: public:
BaseScrollBarButtonTest() BaseScrollBarButtonTest()
: button_(std::make_unique<BaseScrollBarButton>( : button_(std::make_unique<BaseScrollBarButton>(
&listener_, base::BindRepeating(&MockButtonCallback::ButtonPressed,
base::Unretained(&callback_)),
task_environment_.GetMockTickClock())) {} task_environment_.GetMockTickClock())) {}
~BaseScrollBarButtonTest() override = default; ~BaseScrollBarButtonTest() override = default;
protected: protected:
testing::StrictMock<MockButtonListener>& listener() { return listener_; } testing::StrictMock<MockButtonCallback>& callback() { return callback_; }
Button* button() { return button_.get(); } Button* button() { return button_.get(); }
void AdvanceTime(base::TimeDelta time_delta) { void AdvanceTime(base::TimeDelta time_delta) {
...@@ -61,7 +58,7 @@ class BaseScrollBarButtonTest : public testing::Test { ...@@ -61,7 +58,7 @@ class BaseScrollBarButtonTest : public testing::Test {
display::test::TestScreen test_screen_; display::test::TestScreen test_screen_;
display::test::ScopedScreenOverride screen_override{&test_screen_}; display::test::ScopedScreenOverride screen_override{&test_screen_};
testing::StrictMock<MockButtonListener> listener_; testing::StrictMock<MockButtonCallback> callback_;
const std::unique_ptr<Button> button_; const std::unique_ptr<Button> button_;
}; };
...@@ -76,18 +73,18 @@ TEST_F(BaseScrollBarButtonTest, FocusBehavior) { ...@@ -76,18 +73,18 @@ TEST_F(BaseScrollBarButtonTest, FocusBehavior) {
} }
TEST_F(BaseScrollBarButtonTest, CallbackFiresOnMouseDown) { TEST_F(BaseScrollBarButtonTest, CallbackFiresOnMouseDown) {
EXPECT_CALL(listener(), ButtonPressed(_, _)); EXPECT_CALL(callback(), ButtonPressed());
// By default the button should notify its listener on mouse release. // By default the button should notify its callback on mouse release.
button()->OnMousePressed(ui::MouseEvent( button()->OnMousePressed(ui::MouseEvent(
ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
} }
TEST_F(BaseScrollBarButtonTest, CallbackFilesMultipleTimesMouseHeldDown) { TEST_F(BaseScrollBarButtonTest, CallbackFiresMultipleTimesMouseHeldDown) {
EXPECT_CALL(listener(), ButtonPressed(_, _)).Times(AtLeast(2)); EXPECT_CALL(callback(), ButtonPressed()).Times(AtLeast(2));
// By default the button should notify its listener on mouse release. // By default the button should notify its callback on mouse release.
button()->OnMousePressed(ui::MouseEvent( button()->OnMousePressed(ui::MouseEvent(
ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
...@@ -96,16 +93,16 @@ TEST_F(BaseScrollBarButtonTest, CallbackFilesMultipleTimesMouseHeldDown) { ...@@ -96,16 +93,16 @@ TEST_F(BaseScrollBarButtonTest, CallbackFilesMultipleTimesMouseHeldDown) {
} }
TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseReleased) { TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseReleased) {
EXPECT_CALL(listener(), ButtonPressed(_, _)).Times(AtLeast(2)); EXPECT_CALL(callback(), ButtonPressed()).Times(AtLeast(2));
// By default the button should notify its listener on mouse release. // By default the button should notify its callback on mouse release.
button()->OnMousePressed(ui::MouseEvent( button()->OnMousePressed(ui::MouseEvent(
ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10); AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10);
testing::Mock::VerifyAndClearExpectations(&listener()); testing::Mock::VerifyAndClearExpectations(&callback());
button()->OnMouseReleased(ui::MouseEvent( button()->OnMouseReleased(ui::MouseEvent(
ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), ui::ET_MOUSE_RELEASED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
...@@ -113,26 +110,26 @@ TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseReleased) { ...@@ -113,26 +110,26 @@ TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseReleased) {
AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10); AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10);
EXPECT_CALL(listener(), ButtonPressed(_, _)).Times(AtMost(0)); EXPECT_CALL(callback(), ButtonPressed()).Times(AtMost(0));
} }
TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseCaptureReleased) { TEST_F(BaseScrollBarButtonTest, CallbackStopsFiringAfterMouseCaptureReleased) {
EXPECT_CALL(listener(), ButtonPressed(_, _)).Times(AtLeast(2)); EXPECT_CALL(callback(), ButtonPressed()).Times(AtLeast(2));
// By default the button should notify its listener on mouse release. // By default the button should notify its callback on mouse release.
button()->OnMousePressed(ui::MouseEvent( button()->OnMousePressed(ui::MouseEvent(
ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(), ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10); AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10);
testing::Mock::VerifyAndClearExpectations(&listener()); testing::Mock::VerifyAndClearExpectations(&callback());
button()->OnMouseCaptureLost(); button()->OnMouseCaptureLost();
AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10); AdvanceTime(RepeatController::GetInitialWaitForTesting() * 10);
EXPECT_CALL(listener(), ButtonPressed(_, _)).Times(AtMost(0)); EXPECT_CALL(callback(), ButtonPressed()).Times(AtMost(0));
} }
} // namespace views } // namespace views
...@@ -35,7 +35,7 @@ class ScrollBarButton : public BaseScrollBarButton { ...@@ -35,7 +35,7 @@ class ScrollBarButton : public BaseScrollBarButton {
kRight, kRight,
}; };
ScrollBarButton(ButtonListener* listener, Type type); ScrollBarButton(PressedCallback callback, Type type);
~ScrollBarButton() override; ~ScrollBarButton() override;
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
...@@ -70,11 +70,8 @@ class ScrollBarThumb : public BaseScrollBarThumb { ...@@ -70,11 +70,8 @@ class ScrollBarThumb : public BaseScrollBarThumb {
ScrollBar* scroll_bar_; ScrollBar* scroll_bar_;
}; };
///////////////////////////////////////////////////////////////////////////// ScrollBarButton::ScrollBarButton(PressedCallback callback, Type type)
// ScrollBarButton : BaseScrollBarButton(std::move(callback)), type_(type) {
ScrollBarButton::ScrollBarButton(ButtonListener* listener, Type type)
: BaseScrollBarButton(listener), type_(type) {
EnableCanvasFlippingForRTLUI(true); EnableCanvasFlippingForRTLUI(true);
DCHECK_EQ(FocusBehavior::NEVER, GetFocusBehavior()); DCHECK_EQ(FocusBehavior::NEVER, GetFocusBehavior());
} }
...@@ -142,9 +139,6 @@ ui::NativeTheme::State ScrollBarButton::GetNativeThemeState() const { ...@@ -142,9 +139,6 @@ ui::NativeTheme::State ScrollBarButton::GetNativeThemeState() const {
return ui::NativeTheme::kNormal; return ui::NativeTheme::kNormal;
} }
/////////////////////////////////////////////////////////////////////////////
// ScrollBarThumb
ScrollBarThumb::ScrollBarThumb(ScrollBar* scroll_bar) ScrollBarThumb::ScrollBarThumb(ScrollBar* scroll_bar)
: BaseScrollBarThumb(scroll_bar), scroll_bar_(scroll_bar) {} : BaseScrollBarThumb(scroll_bar), scroll_bar_(scroll_bar) {}
...@@ -201,34 +195,24 @@ ui::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const { ...@@ -201,34 +195,24 @@ ui::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const {
} // namespace } // namespace
////////////////////////////////////////////////////////////////////////////////
// ScrollBarViews, public:
ScrollBarViews::ScrollBarViews(bool horizontal) : ScrollBar(horizontal) { ScrollBarViews::ScrollBarViews(bool horizontal) : ScrollBar(horizontal) {
EnableCanvasFlippingForRTLUI(true); EnableCanvasFlippingForRTLUI(true);
state_ = ui::NativeTheme::kNormal; state_ = ui::NativeTheme::kNormal;
auto* layout = SetLayoutManager(std::make_unique<views::FlexLayout>()); auto* layout = SetLayoutManager(std::make_unique<views::FlexLayout>());
if (!horizontal)
std::unique_ptr<ScrollBarButton> prev_button, next_button;
using Type = ScrollBarButton::Type;
if (horizontal) {
prev_button = std::make_unique<ScrollBarButton>(this, Type::kLeft);
next_button = std::make_unique<ScrollBarButton>(this, Type::kRight);
part_ = ui::NativeTheme::kScrollbarHorizontalTrack;
} else {
layout->SetOrientation(views::LayoutOrientation::kVertical); layout->SetOrientation(views::LayoutOrientation::kVertical);
prev_button = std::make_unique<ScrollBarButton>(this, Type::kUp); const auto scroll_func = [](ScrollBarViews* scrollbar, ScrollAmount amount) {
next_button = std::make_unique<ScrollBarButton>(this, Type::kDown); scrollbar->ScrollByAmount(amount);
};
part_ = ui::NativeTheme::kScrollbarVerticalTrack; using Type = ScrollBarButton::Type;
} prev_button_ = AddChildView(std::make_unique<ScrollBarButton>(
prev_button->set_context_menu_controller(this); base::BindRepeating(scroll_func, base::Unretained(this),
next_button->set_context_menu_controller(this); ScrollAmount::kPrevLine),
horizontal ? Type::kLeft : Type::kUp));
prev_button_->set_context_menu_controller(this);
prev_button_ = AddChildView(std::move(prev_button));
SetThumb(new ScrollBarThumb(this)); SetThumb(new ScrollBarThumb(this));
// Allow the thumb to take up the whole size of the scrollbar, save for the // Allow the thumb to take up the whole size of the scrollbar, save for the
// prev/next buttons. Layout need only set the thumb cross-axis coordinate; // prev/next buttons. Layout need only set the thumb cross-axis coordinate;
...@@ -237,7 +221,14 @@ ScrollBarViews::ScrollBarViews(bool horizontal) : ScrollBar(horizontal) { ...@@ -237,7 +221,14 @@ ScrollBarViews::ScrollBarViews(bool horizontal) : ScrollBar(horizontal) {
views::kFlexBehaviorKey, views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
views::MaximumFlexSizeRule::kUnbounded)); views::MaximumFlexSizeRule::kUnbounded));
next_button_ = AddChildView(std::move(next_button));
next_button_ = AddChildView(std::make_unique<ScrollBarButton>(
base::BindRepeating(scroll_func, base::Unretained(this),
ScrollBar::ScrollAmount::kNextLine),
horizontal ? Type::kRight : Type::kDown));
next_button_->set_context_menu_controller(this);
part_ = horizontal ? ui::NativeTheme::kScrollbarHorizontalTrack
: ui::NativeTheme::kScrollbarVerticalTrack;
} }
ScrollBarViews::~ScrollBarViews() = default; ScrollBarViews::~ScrollBarViews() = default;
...@@ -259,9 +250,6 @@ int ScrollBarViews::GetVerticalScrollBarWidth(const ui::NativeTheme* theme) { ...@@ -259,9 +250,6 @@ int ScrollBarViews::GetVerticalScrollBarWidth(const ui::NativeTheme* theme) {
return std::max(track_size.width(), button_size.width()); return std::max(track_size.width(), button_size.width());
} }
////////////////////////////////////////////////////////////////////////////////
// ScrollBarViews, View overrides:
void ScrollBarViews::OnPaint(gfx::Canvas* canvas) { void ScrollBarViews::OnPaint(gfx::Canvas* canvas) {
gfx::Rect bounds = GetTrackBounds(); gfx::Rect bounds = GetTrackBounds();
if (bounds.IsEmpty()) if (bounds.IsEmpty())
...@@ -301,19 +289,6 @@ int ScrollBarViews::GetThickness() const { ...@@ -301,19 +289,6 @@ int ScrollBarViews::GetThickness() const {
return IsHorizontal() ? size.height() : size.width(); return IsHorizontal() ? size.height() : size.width();
} }
//////////////////////////////////////////////////////////////////////////////
// BaseButton::ButtonListener overrides:
void ScrollBarViews::ButtonPressed(Button* sender, const ui::Event& event) {
const bool is_prev = sender == prev_button_;
DCHECK(is_prev || sender == next_button_);
ScrollByAmount(is_prev ? ScrollBar::ScrollAmount::kPrevLine
: ScrollBar::ScrollAmount::kNextLine);
}
////////////////////////////////////////////////////////////////////////////////
// ScrollBarViews, private:
gfx::Rect ScrollBarViews::GetTrackBounds() const { gfx::Rect ScrollBarViews::GetTrackBounds() const {
gfx::Rect bounds = GetLocalBounds(); gfx::Rect bounds = GetLocalBounds();
gfx::Size size = prev_button_->GetPreferredSize(); gfx::Size size = prev_button_->GetPreferredSize();
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/point.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/controls/button/button.h" #include "ui/views/controls/button/button.h"
#include "ui/views/controls/scrollbar/scroll_bar.h" #include "ui/views/controls/scrollbar/scroll_bar.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -20,7 +19,7 @@ class Canvas; ...@@ -20,7 +19,7 @@ class Canvas;
namespace views { namespace views {
// Views implementation for the scrollbar. // Views implementation for the scrollbar.
class VIEWS_EXPORT ScrollBarViews : public ScrollBar, public ButtonListener { class VIEWS_EXPORT ScrollBarViews : public ScrollBar {
public: public:
METADATA_HEADER(ScrollBarViews); METADATA_HEADER(ScrollBarViews);
...@@ -37,9 +36,6 @@ class VIEWS_EXPORT ScrollBarViews : public ScrollBar, public ButtonListener { ...@@ -37,9 +36,6 @@ class VIEWS_EXPORT ScrollBarViews : public ScrollBar, public ButtonListener {
// ScrollBar overrides: // ScrollBar overrides:
int GetThickness() const override; int GetThickness() const override;
// BaseButton::ButtonListener overrides:
void ButtonPressed(Button* sender, const ui::Event& event) override;
// Returns the area for the track. This is the area of the scrollbar minus // Returns the area for the track. This is the area of the scrollbar minus
// the size of the arrow buttons. // the size of the arrow buttons.
gfx::Rect GetTrackBounds() const override; gfx::Rect GetTrackBounds() const override;
......
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