OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_sheet_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 19 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
19 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | 20 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
20 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" | 21 #include "chrome/browser/ui/views/payments/payment_request_row_view.h" |
21 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 22 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
22 #include "chrome/grit/chromium_strings.h" | 23 #include "chrome/grit/chromium_strings.h" |
23 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
24 #include "chrome/grit/theme_resources.h" | 25 #include "chrome/grit/theme_resources.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 views::ImageView* chevron = new views::ImageView(); | 158 views::ImageView* chevron = new views::ImageView(); |
158 chevron->set_can_process_events_within_subtree(false); | 159 chevron->set_can_process_events_within_subtree(false); |
159 chevron->SetImage(gfx::CreateVectorIcon( | 160 chevron->SetImage(gfx::CreateVectorIcon( |
160 views::kSubmenuArrowIcon, | 161 views::kSubmenuArrowIcon, |
161 color_utils::DeriveDefaultIconColor(name_label->enabled_color()))); | 162 color_utils::DeriveDefaultIconColor(name_label->enabled_color()))); |
162 layout->AddView(chevron); | 163 layout->AddView(chevron); |
163 | 164 |
164 return std::move(row); | 165 return std::move(row); |
165 } | 166 } |
166 | 167 |
| 168 // Creates a GridLayout object to be used in the Order Summary section's list of |
| 169 // items and the list of prices. |host| is the view that will be assigned the |
| 170 // returned Layout Manager and |trailing| indicates whether the elements added |
| 171 // to the manager should have trailing horizontal alignment. If trailing is |
| 172 // |false|, their horizontal alignment is leading. |
| 173 std::unique_ptr<views::GridLayout> CreateOrderSummarySectionContainerLayout( |
| 174 views::View* host, |
| 175 bool trailing) { |
| 176 std::unique_ptr<views::GridLayout> layout = |
| 177 base::MakeUnique<views::GridLayout>(host); |
| 178 |
| 179 views::ColumnSet* columns = layout->AddColumnSet(0); |
| 180 columns->AddColumn( |
| 181 trailing ? views::GridLayout::TRAILING : views::GridLayout::LEADING, |
| 182 views::GridLayout::LEADING, 1, views::GridLayout::USE_PREF, 0, 0); |
| 183 |
| 184 return layout; |
| 185 } |
| 186 |
167 } // namespace | 187 } // namespace |
168 | 188 |
169 PaymentSheetViewController::PaymentSheetViewController( | 189 PaymentSheetViewController::PaymentSheetViewController( |
170 PaymentRequest* request, | 190 PaymentRequest* request, |
171 PaymentRequestDialogView* dialog) | 191 PaymentRequestDialogView* dialog) |
172 : PaymentRequestSheetController(request, dialog), | 192 : PaymentRequestSheetController(request, dialog), |
173 pay_button_(nullptr), | 193 pay_button_(nullptr), |
174 widest_name_column_view_width_(ComputeWidestNameColumnViewWidth()) { | 194 widest_name_column_view_width_(ComputeWidestNameColumnViewWidth()) { |
175 request->AddObserver(this); | 195 request->AddObserver(this); |
176 } | 196 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 default: | 301 default: |
282 PaymentRequestSheetController::ButtonPressed(sender, event); | 302 PaymentRequestSheetController::ButtonPressed(sender, event); |
283 break; | 303 break; |
284 } | 304 } |
285 } | 305 } |
286 | 306 |
287 void PaymentSheetViewController::UpdatePayButtonState(bool enabled) { | 307 void PaymentSheetViewController::UpdatePayButtonState(bool enabled) { |
288 pay_button_->SetEnabled(enabled); | 308 pay_button_->SetEnabled(enabled); |
289 } | 309 } |
290 | 310 |
291 std::unique_ptr<views::View> | |
292 PaymentSheetViewController::CreateOrderSummarySectionContent() { | |
293 CurrencyFormatter* formatter = request()->GetOrCreateCurrencyFormatter( | |
294 request()->details()->total->amount->currency, | |
295 request()->details()->total->amount->currency_system, | |
296 g_browser_process->GetApplicationLocale()); | |
297 base::string16 label_value = l10n_util::GetStringFUTF16( | |
298 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_TOTAL_FORMAT, | |
299 base::UTF8ToUTF16(request()->details()->total->label), | |
300 base::UTF8ToUTF16(formatter->formatted_currency_code()), | |
301 formatter->Format(request()->details()->total->amount->value)); | |
302 | |
303 return base::MakeUnique<views::Label>(label_value); | |
304 } | |
305 | |
306 // Creates the Order Summary row, which contains an "Order Summary" label, | 311 // Creates the Order Summary row, which contains an "Order Summary" label, |
307 // a Total Amount label, and a Chevron. | 312 // an inline list of display items, a Total Amount label, and a Chevron. |
308 // +----------------------------------------------+ | 313 // +----------------------------------------------+ |
309 // | Order Summary Total USD $12.34 > | | 314 // | Order Summary Item 1 $ 1.34 | |
| 315 // | Item 2 $ 2.00 > | |
| 316 // | 2 more items... | |
| 317 // | Total USD $12.34 | |
310 // +----------------------------------------------+ | 318 // +----------------------------------------------+ |
311 std::unique_ptr<views::Button> | 319 std::unique_ptr<views::Button> |
312 PaymentSheetViewController::CreatePaymentSheetSummaryRow() { | 320 PaymentSheetViewController::CreatePaymentSheetSummaryRow() { |
| 321 std::unique_ptr<views::View> item_summaries = base::MakeUnique<views::View>(); |
| 322 std::unique_ptr<views::GridLayout> item_summaries_layout = |
| 323 CreateOrderSummarySectionContainerLayout(item_summaries.get(), |
| 324 /* trailing =*/false); |
| 325 |
| 326 std::unique_ptr<views::View> item_amounts = base::MakeUnique<views::View>(); |
| 327 std::unique_ptr<views::GridLayout> item_amounts_layout = |
| 328 CreateOrderSummarySectionContainerLayout(item_amounts.get(), |
| 329 /* trailing =*/true); |
| 330 |
| 331 const std::vector<mojom::PaymentItemPtr>& items = |
| 332 request()->details()->display_items; |
| 333 // The inline items section contains the first 2 display items of the |
| 334 // request's details, followed by a label indicating "N more items..." if |
| 335 // there are more than 2 items in the details. The total label and amount |
| 336 // always follow. |
| 337 constexpr int kMaxNumberOfItemsShown = 2; |
| 338 for (size_t i = 0; i < items.size() && i < kMaxNumberOfItemsShown; ++i) { |
| 339 item_summaries_layout->StartRow(0, 0); |
| 340 item_summaries_layout->AddView( |
| 341 new views::Label(base::ASCIIToUTF16(items[i]->label))); |
| 342 |
| 343 item_amounts_layout->StartRow(0, 0); |
| 344 item_amounts_layout->AddView(new views::Label( |
| 345 request()->GetFormattedCurrencyAmount(items[i]->amount->value))); |
| 346 } |
| 347 |
| 348 int hidden_item_count = items.size() - kMaxNumberOfItemsShown; |
| 349 if (hidden_item_count > 0) { |
| 350 item_summaries_layout->StartRow(0, 0); |
| 351 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( |
| 352 l10n_util::GetStringFUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_MORE_ITEMS, |
| 353 base::IntToString16(hidden_item_count))); |
| 354 label->SetDisabledColor(label->GetNativeTheme()->GetSystemColor( |
| 355 ui::NativeTheme::kColorId_LabelDisabledColor)); |
| 356 label->SetEnabled(false); |
| 357 item_summaries_layout->AddView(label.release()); |
| 358 |
| 359 item_amounts_layout->StartRow(0, 0); |
| 360 item_amounts_layout->AddView(new views::Label(base::ASCIIToUTF16(""))); |
| 361 } |
| 362 |
| 363 item_summaries_layout->StartRow(0, 0); |
| 364 item_summaries_layout->AddView( |
| 365 CreateBoldLabel(base::ASCIIToUTF16(request()->details()->total->label)) |
| 366 .release()); |
| 367 |
| 368 item_amounts_layout->StartRow(0, 0); |
| 369 item_amounts_layout->AddView( |
| 370 CreateBoldLabel( |
| 371 l10n_util::GetStringFUTF16( |
| 372 IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SHEET_TOTAL_FORMAT, |
| 373 base::UTF8ToUTF16(request()->GetFormattedCurrencyCode()), |
| 374 request()->GetFormattedCurrencyAmount( |
| 375 request()->details()->total->amount->value))) |
| 376 .release()); |
| 377 |
| 378 item_summaries->SetLayoutManager(item_summaries_layout.release()); |
| 379 item_amounts->SetLayoutManager(item_amounts_layout.release()); |
| 380 |
313 std::unique_ptr<views::Button> section = CreatePaymentSheetRow( | 381 std::unique_ptr<views::Button> section = CreatePaymentSheetRow( |
314 this, | 382 this, |
315 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_NAME), | 383 l10n_util::GetStringUTF16(IDS_PAYMENT_REQUEST_ORDER_SUMMARY_SECTION_NAME), |
316 std::unique_ptr<views::View>(nullptr), | 384 std::move(item_summaries), std::move(item_amounts), |
317 CreateOrderSummarySectionContent(), | |
318 widest_name_column_view_width_); | 385 widest_name_column_view_width_); |
319 section->set_tag(static_cast<int>( | 386 section->set_tag(static_cast<int>( |
320 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON)); | 387 PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON)); |
321 section->set_id( | 388 section->set_id( |
322 static_cast<int>(DialogViewID::PAYMENT_SHEET_SUMMARY_SECTION)); | 389 static_cast<int>(DialogViewID::PAYMENT_SHEET_SUMMARY_SECTION)); |
323 return section; | 390 return section; |
324 } | 391 } |
325 | 392 |
326 std::unique_ptr<views::View> | 393 std::unique_ptr<views::View> |
327 PaymentSheetViewController::CreateShippingSectionContent() { | 394 PaymentSheetViewController::CreateShippingSectionContent() { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 CreateContactInfoSectionContent(), std::unique_ptr<views::View>(nullptr), | 494 CreateContactInfoSectionContent(), std::unique_ptr<views::View>(nullptr), |
428 widest_name_column_view_width_); | 495 widest_name_column_view_width_); |
429 section->set_tag(static_cast<int>( | 496 section->set_tag(static_cast<int>( |
430 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON)); | 497 PaymentSheetViewControllerTags::SHOW_CONTACT_INFO_BUTTON)); |
431 section->set_id( | 498 section->set_id( |
432 static_cast<int>(DialogViewID::PAYMENT_SHEET_CONTACT_INFO_SECTION)); | 499 static_cast<int>(DialogViewID::PAYMENT_SHEET_CONTACT_INFO_SECTION)); |
433 return section; | 500 return section; |
434 } | 501 } |
435 | 502 |
436 } // namespace payments | 503 } // namespace payments |
OLD | NEW |