Commit 0a9ed932 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Change ButtonPressed overrides to callbacks: ash/system/power/

Bug: 772945
Change-Id: I310fd814ff948e30d433de28fe83df206d7ecbd9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2508177
Commit-Queue: Jenny Zhang <jennyz@chromium.org>
Reviewed-by: default avatarJenny Zhang <jennyz@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823368}
parent ac822cd6
...@@ -38,10 +38,10 @@ constexpr int kLineHeight = 20; ...@@ -38,10 +38,10 @@ constexpr int kLineHeight = 20;
} // namespace } // namespace
PowerButtonMenuItemView::PowerButtonMenuItemView( PowerButtonMenuItemView::PowerButtonMenuItemView(
views::ButtonListener* listener, PressedCallback callback,
const gfx::VectorIcon& icon, const gfx::VectorIcon& icon,
const base::string16& title_text) const base::string16& title_text)
: views::ImageButton(listener), icon_(icon) { : views::ImageButton(std::move(callback)), icon_(icon) {
SetFocusBehavior(FocusBehavior::ALWAYS); SetFocusBehavior(FocusBehavior::ALWAYS);
SetFocusPainter(nullptr); SetFocusPainter(nullptr);
......
...@@ -33,7 +33,7 @@ class ASH_EXPORT PowerButtonMenuItemView : public views::ImageButton { ...@@ -33,7 +33,7 @@ class ASH_EXPORT PowerButtonMenuItemView : public views::ImageButton {
// Thickness of the menu item's border in pixels. // Thickness of the menu item's border in pixels.
static constexpr int kItemBorderThickness = 2; static constexpr int kItemBorderThickness = 2;
PowerButtonMenuItemView(views::ButtonListener* listener, PowerButtonMenuItemView(PressedCallback callback,
const gfx::VectorIcon& icon, const gfx::VectorIcon& icon,
const base::string16& title_text); const base::string16& title_text);
PowerButtonMenuItemView(const PowerButtonMenuItemView&) = delete; PowerButtonMenuItemView(const PowerButtonMenuItemView&) = delete;
......
...@@ -144,8 +144,9 @@ PowerButtonMenuView::GetTransformDisplacement() const { ...@@ -144,8 +144,9 @@ PowerButtonMenuView::GetTransformDisplacement() const {
void PowerButtonMenuView::RecreateItems() { void PowerButtonMenuView::RecreateItems() {
// Helper to add or remove a menu item from |this|. Stores weak pointer to // Helper to add or remove a menu item from |this|. Stores weak pointer to
// |out_item_ptr|. // |out_item_ptr|.
auto add_remove_item = [this]( auto add_remove_item =
bool create, const gfx::VectorIcon& icon, [this](bool create, PowerButtonMenuActionType action,
base::RepeatingClosure callback, const gfx::VectorIcon& icon,
const base::string16& string, const base::string16& string,
PowerButtonMenuItemView** out_item_ptr) -> void { PowerButtonMenuItemView** out_item_ptr) -> void {
// If an item needs to be created and exists, or needs to be destroyed but // If an item needs to be created and exists, or needs to be destroyed but
...@@ -156,8 +157,11 @@ void PowerButtonMenuView::RecreateItems() { ...@@ -156,8 +157,11 @@ void PowerButtonMenuView::RecreateItems() {
return; return;
if (create) { if (create) {
*out_item_ptr = AddChildView( *out_item_ptr = AddChildView(std::make_unique<PowerButtonMenuItemView>(
std::make_unique<PowerButtonMenuItemView>(this, icon, string)); base::BindRepeating(&PowerButtonMenuView::ButtonPressed,
base::Unretained(this), action,
std::move(callback)),
icon, string));
} else { } else {
std::unique_ptr<PowerButtonMenuItemView> to_delete = std::unique_ptr<PowerButtonMenuItemView> to_delete =
RemoveChildViewT(*out_item_ptr); RemoveChildViewT(*out_item_ptr);
...@@ -175,18 +179,46 @@ void PowerButtonMenuView::RecreateItems() { ...@@ -175,18 +179,46 @@ void PowerButtonMenuView::RecreateItems() {
login_status != LoginStatus::KIOSK_APP; login_status != LoginStatus::KIOSK_APP;
add_remove_item( add_remove_item(
true, kSystemPowerButtonMenuPowerOffIcon, true, PowerButtonMenuActionType::kPowerOff,
base::BindRepeating(
&LockStateController::StartShutdownAnimation,
base::Unretained(Shell::Get()->lock_state_controller()),
ShutdownReason::POWER_BUTTON),
kSystemPowerButtonMenuPowerOffIcon,
l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_POWER_OFF_BUTTON), l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_POWER_OFF_BUTTON),
&power_off_item_); &power_off_item_);
add_remove_item(create_sign_out, kSystemPowerButtonMenuSignOutIcon, add_remove_item(
create_sign_out, PowerButtonMenuActionType::kSignOut,
base::BindRepeating(&SessionControllerImpl::RequestSignOut,
base::Unretained(Shell::Get()->session_controller())),
kSystemPowerButtonMenuSignOutIcon,
user::GetLocalizedSignOutStringForStatus(login_status, false), user::GetLocalizedSignOutStringForStatus(login_status, false),
&sign_out_item_); &sign_out_item_);
add_remove_item( add_remove_item(
create_lock_screen, kSystemPowerButtonMenuLockScreenIcon, create_lock_screen, PowerButtonMenuActionType::kLockScreen,
base::BindRepeating(&SessionControllerImpl::LockScreen,
base::Unretained(Shell::Get()->session_controller())),
kSystemPowerButtonMenuLockScreenIcon,
l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_LOCK_SCREEN_BUTTON), l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_LOCK_SCREEN_BUTTON),
&lock_screen_item_); &lock_screen_item_);
add_remove_item( add_remove_item(
create_feedback, kSystemPowerButtonMenuFeedbackIcon, create_feedback, PowerButtonMenuActionType::kFeedback,
base::BindRepeating(
[](Shell* shell) {
if (shell->session_controller()->login_status() ==
LoginStatus::NOT_LOGGED_IN) {
// There is a special flow for feedback while in login screen,
// therefore we trigger the same handler associated with the
// feedback accelerator from the login screen to bring up the
// feedback dialog.
shell->login_screen_controller()->HandleAccelerator(
LoginAcceleratorAction::kShowFeedback);
} else {
NewWindowDelegate::GetInstance()->OpenFeedbackPage();
}
},
Shell::Get()),
kSystemPowerButtonMenuFeedbackIcon,
l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_FEEDBACK_BUTTON), l10n_util::GetStringUTF16(IDS_ASH_POWER_BUTTON_MENU_FEEDBACK_BUTTON),
&feedback_item_); &feedback_item_);
} }
...@@ -254,38 +286,6 @@ void PowerButtonMenuView::OnThemeChanged() { ...@@ -254,38 +286,6 @@ void PowerButtonMenuView::OnThemeChanged() {
AshColorProvider::BaseLayerType::kTransparent80))); AshColorProvider::BaseLayerType::kTransparent80)));
} }
void PowerButtonMenuView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
DCHECK(sender);
Shell* shell = Shell::Get();
if (sender == power_off_item_) {
RecordMenuActionHistogram(PowerButtonMenuActionType::kPowerOff);
shell->lock_state_controller()->StartShutdownAnimation(
ShutdownReason::POWER_BUTTON);
} else if (sender == sign_out_item_) {
RecordMenuActionHistogram(PowerButtonMenuActionType::kSignOut);
shell->session_controller()->RequestSignOut();
} else if (sender == lock_screen_item_) {
RecordMenuActionHistogram(PowerButtonMenuActionType::kLockScreen);
shell->session_controller()->LockScreen();
} else if (sender == feedback_item_) {
RecordMenuActionHistogram(PowerButtonMenuActionType::kFeedback);
if (shell->session_controller()->login_status() ==
LoginStatus::NOT_LOGGED_IN) {
// There is a special flow for feedback while in login screen, therefore
// we trigger the same handler associated with the feedback accelerator
// from the login screen to bring up the feedback dialog.
shell->login_screen_controller()->HandleAccelerator(
LoginAcceleratorAction::kShowFeedback);
} else {
NewWindowDelegate::GetInstance()->OpenFeedbackPage();
}
} else {
NOTREACHED() << "Invalid sender";
}
shell->power_button_controller()->DismissMenu();
}
void PowerButtonMenuView::OnImplicitAnimationsCompleted() { void PowerButtonMenuView::OnImplicitAnimationsCompleted() {
if (layer()->opacity() == 0.f) if (layer()->opacity() == 0.f)
SetVisible(false); SetVisible(false);
...@@ -294,4 +294,11 @@ void PowerButtonMenuView::OnImplicitAnimationsCompleted() { ...@@ -294,4 +294,11 @@ void PowerButtonMenuView::OnImplicitAnimationsCompleted() {
RequestFocus(); RequestFocus();
} }
void PowerButtonMenuView::ButtonPressed(PowerButtonMenuActionType action,
base::RepeatingClosure callback) {
RecordMenuActionHistogram(action);
std::move(callback).Run();
Shell::Get()->power_button_controller()->DismissMenu();
}
} // namespace ash } // namespace ash
...@@ -9,16 +9,15 @@ ...@@ -9,16 +9,15 @@
#include "ash/system/power/power_button_controller.h" #include "ash/system/power/power_button_controller.h"
#include "base/macros.h" #include "base/macros.h"
#include "ui/compositor/layer_animation_observer.h" #include "ui/compositor/layer_animation_observer.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace ash { namespace ash {
enum class PowerButtonMenuActionType;
class PowerButtonMenuItemView; class PowerButtonMenuItemView;
// PowerButtonMenuView displays the menu items of the power button menu. It // PowerButtonMenuView displays the menu items of the power button menu. It
// includes power off and sign out items currently. // includes power off and sign out items currently.
class ASH_EXPORT PowerButtonMenuView : public views::View, class ASH_EXPORT PowerButtonMenuView : public views::View,
public views::ButtonListener,
public ui::ImplicitAnimationObserver { public ui::ImplicitAnimationObserver {
public: public:
// The duration of showing or dismissing power button menu animation. // The duration of showing or dismissing power button menu animation.
...@@ -79,12 +78,12 @@ class ASH_EXPORT PowerButtonMenuView : public views::View, ...@@ -79,12 +78,12 @@ class ASH_EXPORT PowerButtonMenuView : public views::View,
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
void OnThemeChanged() override; void OnThemeChanged() override;
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
// ui::ImplicitAnimationObserver: // ui::ImplicitAnimationObserver:
void OnImplicitAnimationsCompleted() override; void OnImplicitAnimationsCompleted() override;
void ButtonPressed(PowerButtonMenuActionType action,
base::RepeatingClosure callback);
// Items in the menu. Owned by views hierarchy. // Items in the menu. Owned by views hierarchy.
PowerButtonMenuItemView* power_off_item_ = nullptr; PowerButtonMenuItemView* power_off_item_ = nullptr;
PowerButtonMenuItemView* sign_out_item_ = nullptr; PowerButtonMenuItemView* sign_out_item_ = 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