Index: chrome/browser/ui/views/payments/payment_request_sheet_controller.cc |
diff --git a/chrome/browser/ui/views/payments/payment_request_sheet_controller.cc b/chrome/browser/ui/views/payments/payment_request_sheet_controller.cc |
index de6934cf3e1cf43edb88e29da3c840a81bf8f68c..658c877950630036856f78945f5efe72294ea784 100644 |
--- a/chrome/browser/ui/views/payments/payment_request_sheet_controller.cc |
+++ b/chrome/browser/ui/views/payments/payment_request_sheet_controller.cc |
@@ -22,6 +22,13 @@ namespace payments { |
namespace { |
+// This event is used to pass to ButtonPressed when its event parameter doesn't |
+// matter, only the sender. |
+class DummyEvent : public ui::Event { |
+ public: |
+ DummyEvent() : ui::Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} |
+}; |
+ |
// This class is the actual sheet that gets pushed on the view_stack_. It |
// implements views::FocusTraversable to trap focus within its hierarchy. This |
// way, focus doesn't leave the topmost sheet on the view stack to go on views |
@@ -32,9 +39,13 @@ namespace { |
// this SheetView's RequestFocus() is called. |
class SheetView : public views::View, public views::FocusTraversable { |
public: |
- SheetView() |
+ explicit SheetView(const base::Callback<bool()>& confirm_accelerator_callback) |
: first_focusable_(nullptr), |
- focus_search_(base::MakeUnique<views::FocusSearch>(this, true, false)) { |
+ focus_search_(base::MakeUnique<views::FocusSearch>(this, true, false)), |
+ confirm_accelerator_(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)), |
Mathieu
2017/05/04 17:12:43
nit: I would prefer |enter_key_accelerator|, becau
anthonyvd
2017/05/04 18:56:46
Done.
|
+ confirm_accelerator_callback_(confirm_accelerator_callback) { |
+ if (confirm_accelerator_callback_) |
+ AddAccelerator(confirm_accelerator_); |
} |
// Sets |view| as the first focusable view in this pane. If it's nullptr, the |
@@ -69,8 +80,17 @@ class SheetView : public views::View, public views::FocusTraversable { |
first_focusable->RequestFocus(); |
} |
+ bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
+ if (accelerator == confirm_accelerator_ && confirm_accelerator_callback_) { |
Mathieu
2017/05/04 17:12:43
no curlies?
anthonyvd
2017/05/04 18:56:46
Since enter_key_accelerator is longer, the conditi
|
+ return confirm_accelerator_callback_.Run(); |
+ } |
+ return views::View::AcceleratorPressed(accelerator); |
+ } |
+ |
views::View* first_focusable_; |
std::unique_ptr<views::FocusSearch> focus_search_; |
+ ui::Accelerator confirm_accelerator_; |
+ base::Callback<bool()> confirm_accelerator_callback_; |
DISALLOW_COPY_AND_ASSIGN(SheetView); |
}; |
@@ -139,7 +159,16 @@ void PaymentRequestSheetController::ButtonPressed( |
std::unique_ptr<views::View> |
PaymentRequestSheetController::CreatePaymentView() { |
- std::unique_ptr<SheetView> view = base::MakeUnique<SheetView>(); |
+ // Create the footer now so that it's known if there's a primary button or not |
+ // before creating the sheet view. This way, it's possible to determine |
+ // whether there's something to do when the user hits enter. |
+ std::unique_ptr<views::View> footer = CreateFooterView(); |
+ std::unique_ptr<SheetView> view = base::MakeUnique<SheetView>( |
+ primary_button_ |
+ ? base::Bind( |
+ &PaymentRequestSheetController::PerformPrimaryButtonAction, |
+ base::Unretained(this)) |
+ : base::Callback<bool()>()); |
view->set_background(views::Background::CreateSolidBackground(SK_ColorWHITE)); |
// Paint the sheets to layers, otherwise the MD buttons (which do paint to a |
@@ -186,7 +215,7 @@ PaymentRequestSheetController::CreatePaymentView() { |
layout->AddView(scroll_.get()); |
layout->StartRow(0, 0); |
- layout->AddView(CreateFooterView().release()); |
+ layout->AddView(footer.release()); |
view->SetFirstFocusableView(GetFirstFocusedView()); |
@@ -256,4 +285,12 @@ views::View* PaymentRequestSheetController::GetFirstFocusedView() { |
return secondary_button_.get(); |
} |
+bool PaymentRequestSheetController::PerformPrimaryButtonAction() { |
+ if (primary_button_ && primary_button_->enabled()) { |
+ ButtonPressed(primary_button_.get(), DummyEvent()); |
Mathieu
2017/05/04 17:12:43
could we inline ui::Event(ui::ET_UNKNOWN, base::Ti
anthonyvd
2017/05/04 18:56:46
That constructor is protected.
|
+ return true; |
+ } |
+ return false; |
+} |
+ |
} // namespace payments |