OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" | 5 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" |
6 | 6 |
7 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 7 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
8 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | |
9 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 8 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
10 #include "components/payments/content/payment_request.h" | 9 #include "components/payments/content/payment_request.h" |
11 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
12 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
13 #include "ui/views/background.h" | 12 #include "ui/views/background.h" |
14 #include "ui/views/controls/button/md_text_button.h" | 13 #include "ui/views/controls/button/md_text_button.h" |
15 #include "ui/views/controls/scroll_view.h" | 14 #include "ui/views/controls/scroll_view.h" |
16 #include "ui/views/focus/focus_search.h" | 15 #include "ui/views/focus/focus_search.h" |
17 #include "ui/views/layout/box_layout.h" | 16 #include "ui/views/layout/box_layout.h" |
18 #include "ui/views/layout/fill_layout.h" | 17 #include "ui/views/layout/fill_layout.h" |
19 #include "ui/views/layout/grid_layout.h" | 18 #include "ui/views/layout/grid_layout.h" |
20 | 19 |
21 namespace payments { | 20 namespace payments { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
| 24 // This event is used to pass to ButtonPressed when its event parameter doesn't |
| 25 // matter, only the sender. |
| 26 class DummyEvent : public ui::Event { |
| 27 public: |
| 28 DummyEvent() : ui::Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} |
| 29 }; |
| 30 |
25 // This class is the actual sheet that gets pushed on the view_stack_. It | 31 // This class is the actual sheet that gets pushed on the view_stack_. It |
26 // implements views::FocusTraversable to trap focus within its hierarchy. This | 32 // implements views::FocusTraversable to trap focus within its hierarchy. This |
27 // way, focus doesn't leave the topmost sheet on the view stack to go on views | 33 // way, focus doesn't leave the topmost sheet on the view stack to go on views |
28 // that happen to be underneath. | 34 // that happen to be underneath. |
29 // This class also overrides RequestFocus() to allow consumers to specify which | 35 // This class also overrides RequestFocus() to allow consumers to specify which |
30 // view should be focused first (through SetFirstFocusableView). If no initial | 36 // view should be focused first (through SetFirstFocusableView). If no initial |
31 // view is specified, the first view added to the hierarchy will get focus when | 37 // view is specified, the first view added to the hierarchy will get focus when |
32 // this SheetView's RequestFocus() is called. | 38 // this SheetView's RequestFocus() is called. |
33 class SheetView : public views::View, public views::FocusTraversable { | 39 class SheetView : public views::View, public views::FocusTraversable { |
34 public: | 40 public: |
35 SheetView() | 41 explicit SheetView( |
| 42 const base::Callback<bool()>& enter_key_accelerator_callback) |
36 : first_focusable_(nullptr), | 43 : first_focusable_(nullptr), |
37 focus_search_(base::MakeUnique<views::FocusSearch>(this, true, false)) { | 44 focus_search_(base::MakeUnique<views::FocusSearch>(this, true, false)), |
| 45 enter_key_accelerator_(ui::Accelerator(ui::VKEY_RETURN, ui::EF_NONE)), |
| 46 enter_key_accelerator_callback_(enter_key_accelerator_callback) { |
| 47 if (enter_key_accelerator_callback_) |
| 48 AddAccelerator(enter_key_accelerator_); |
38 } | 49 } |
39 | 50 |
40 // Sets |view| as the first focusable view in this pane. If it's nullptr, the | 51 // Sets |view| as the first focusable view in this pane. If it's nullptr, the |
41 // fallback is to use focus_search_ to find the first focusable view. | 52 // fallback is to use focus_search_ to find the first focusable view. |
42 void SetFirstFocusableView(views::View* view) { first_focusable_ = view; } | 53 void SetFirstFocusableView(views::View* view) { first_focusable_ = view; } |
43 | 54 |
44 private: | 55 private: |
45 // views::FocusTraversable: | 56 // views::FocusTraversable: |
46 views::FocusSearch* GetFocusSearch() override { return focus_search_.get(); } | 57 views::FocusSearch* GetFocusSearch() override { return focus_search_.get(); } |
47 | 58 |
(...skipping 14 matching lines...) Expand all Loading... |
62 views::View* dummy_focus_traversable_view; | 73 views::View* dummy_focus_traversable_view; |
63 first_focusable = focus_search_->FindNextFocusableView( | 74 first_focusable = focus_search_->FindNextFocusableView( |
64 nullptr, false, views::FocusSearch::DOWN, false, | 75 nullptr, false, views::FocusSearch::DOWN, false, |
65 &dummy_focus_traversable, &dummy_focus_traversable_view); | 76 &dummy_focus_traversable, &dummy_focus_traversable_view); |
66 } | 77 } |
67 | 78 |
68 if (first_focusable) | 79 if (first_focusable) |
69 first_focusable->RequestFocus(); | 80 first_focusable->RequestFocus(); |
70 } | 81 } |
71 | 82 |
| 83 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 84 if (accelerator == enter_key_accelerator_ && |
| 85 enter_key_accelerator_callback_) { |
| 86 return enter_key_accelerator_callback_.Run(); |
| 87 } |
| 88 return views::View::AcceleratorPressed(accelerator); |
| 89 } |
| 90 |
72 views::View* first_focusable_; | 91 views::View* first_focusable_; |
73 std::unique_ptr<views::FocusSearch> focus_search_; | 92 std::unique_ptr<views::FocusSearch> focus_search_; |
| 93 ui::Accelerator enter_key_accelerator_; |
| 94 base::Callback<bool()> enter_key_accelerator_callback_; |
74 | 95 |
75 DISALLOW_COPY_AND_ASSIGN(SheetView); | 96 DISALLOW_COPY_AND_ASSIGN(SheetView); |
76 }; | 97 }; |
77 | 98 |
78 } // namespace | 99 } // namespace |
79 | 100 |
80 PaymentRequestSheetController::PaymentRequestSheetController( | 101 PaymentRequestSheetController::PaymentRequestSheetController( |
81 PaymentRequestSpec* spec, | 102 PaymentRequestSpec* spec, |
82 PaymentRequestState* state, | 103 PaymentRequestState* state, |
83 PaymentRequestDialogView* dialog) | 104 PaymentRequestDialogView* dialog) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 dialog()->Pay(); | 153 dialog()->Pay(); |
133 break; | 154 break; |
134 case PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX: | 155 case PaymentRequestCommonTags::PAYMENT_REQUEST_COMMON_TAG_MAX: |
135 NOTREACHED(); | 156 NOTREACHED(); |
136 break; | 157 break; |
137 } | 158 } |
138 } | 159 } |
139 | 160 |
140 std::unique_ptr<views::View> | 161 std::unique_ptr<views::View> |
141 PaymentRequestSheetController::CreatePaymentView() { | 162 PaymentRequestSheetController::CreatePaymentView() { |
142 std::unique_ptr<SheetView> view = base::MakeUnique<SheetView>(); | 163 // Create the footer now so that it's known if there's a primary button or not |
| 164 // before creating the sheet view. This way, it's possible to determine |
| 165 // whether there's something to do when the user hits enter. |
| 166 std::unique_ptr<views::View> footer = CreateFooterView(); |
| 167 std::unique_ptr<SheetView> view = base::MakeUnique<SheetView>( |
| 168 primary_button_ |
| 169 ? base::Bind( |
| 170 &PaymentRequestSheetController::PerformPrimaryButtonAction, |
| 171 base::Unretained(this)) |
| 172 : base::Callback<bool()>()); |
| 173 |
| 174 DialogViewID sheet_id; |
| 175 if (GetSheetId(&sheet_id)) |
| 176 view->set_id(static_cast<int>(sheet_id)); |
| 177 |
143 view->set_background(views::Background::CreateSolidBackground(SK_ColorWHITE)); | 178 view->set_background(views::Background::CreateSolidBackground(SK_ColorWHITE)); |
144 | 179 |
145 // Paint the sheets to layers, otherwise the MD buttons (which do paint to a | 180 // Paint the sheets to layers, otherwise the MD buttons (which do paint to a |
146 // layer) won't do proper clipping. | 181 // layer) won't do proper clipping. |
147 view->SetPaintToLayer(); | 182 view->SetPaintToLayer(); |
148 | 183 |
149 views::GridLayout* layout = new views::GridLayout(view.get()); | 184 views::GridLayout* layout = new views::GridLayout(view.get()); |
150 view->SetLayoutManager(layout); | 185 view->SetLayoutManager(layout); |
151 | 186 |
152 // Note: each view is responsible for its own padding (insets). | 187 // Note: each view is responsible for its own padding (insets). |
(...skipping 26 matching lines...) Expand all Loading... |
179 pane_->SizeToPreferredSize(); | 214 pane_->SizeToPreferredSize(); |
180 | 215 |
181 scroll_ = base::MakeUnique<views::ScrollView>(); | 216 scroll_ = base::MakeUnique<views::ScrollView>(); |
182 scroll_->set_owned_by_client(); | 217 scroll_->set_owned_by_client(); |
183 scroll_->EnableViewPortLayer(); | 218 scroll_->EnableViewPortLayer(); |
184 scroll_->set_hide_horizontal_scrollbar(true); | 219 scroll_->set_hide_horizontal_scrollbar(true); |
185 scroll_->SetContents(pane_); | 220 scroll_->SetContents(pane_); |
186 layout->AddView(scroll_.get()); | 221 layout->AddView(scroll_.get()); |
187 | 222 |
188 layout->StartRow(0, 0); | 223 layout->StartRow(0, 0); |
189 layout->AddView(CreateFooterView().release()); | 224 layout->AddView(footer.release()); |
190 | 225 |
191 view->SetFirstFocusableView(GetFirstFocusedView()); | 226 view->SetFirstFocusableView(GetFirstFocusedView()); |
192 | 227 |
193 return std::move(view); | 228 return std::move(view); |
194 } | 229 } |
195 | 230 |
196 std::unique_ptr<views::View> PaymentRequestSheetController::CreateFooterView() { | 231 std::unique_ptr<views::View> PaymentRequestSheetController::CreateFooterView() { |
197 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); | 232 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); |
198 | 233 |
199 views::GridLayout* layout = new views::GridLayout(container.get()); | 234 views::GridLayout* layout = new views::GridLayout(container.get()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 | 284 |
250 views::View* PaymentRequestSheetController::GetFirstFocusedView() { | 285 views::View* PaymentRequestSheetController::GetFirstFocusedView() { |
251 if (primary_button_ && primary_button_->enabled()) | 286 if (primary_button_ && primary_button_->enabled()) |
252 return primary_button_.get(); | 287 return primary_button_.get(); |
253 | 288 |
254 DCHECK(secondary_button_); | 289 DCHECK(secondary_button_); |
255 | 290 |
256 return secondary_button_.get(); | 291 return secondary_button_.get(); |
257 } | 292 } |
258 | 293 |
| 294 bool PaymentRequestSheetController::GetSheetId(DialogViewID* sheet_id) { |
| 295 return false; |
| 296 } |
| 297 |
| 298 bool PaymentRequestSheetController::PerformPrimaryButtonAction() { |
| 299 if (primary_button_ && primary_button_->enabled()) { |
| 300 ButtonPressed(primary_button_.get(), DummyEvent()); |
| 301 return true; |
| 302 } |
| 303 return false; |
| 304 } |
| 305 |
259 } // namespace payments | 306 } // namespace payments |
OLD | NEW |