Chromium Code Reviews| 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_request_views_util.h" | 5 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "ui/views/controls/button/button.h" | 28 #include "ui/views/controls/button/button.h" |
| 29 #include "ui/views/controls/button/vector_icon_button.h" | 29 #include "ui/views/controls/button/vector_icon_button.h" |
| 30 #include "ui/views/controls/image_view.h" | 30 #include "ui/views/controls/image_view.h" |
| 31 #include "ui/views/controls/label.h" | 31 #include "ui/views/controls/label.h" |
| 32 #include "ui/views/controls/styled_label.h" | 32 #include "ui/views/controls/styled_label.h" |
| 33 #include "ui/views/layout/box_layout.h" | 33 #include "ui/views/layout/box_layout.h" |
| 34 #include "ui/views/layout/grid_layout.h" | 34 #include "ui/views/layout/grid_layout.h" |
| 35 #include "ui/views/painter.h" | 35 #include "ui/views/painter.h" |
| 36 #include "ui/views/view.h" | 36 #include "ui/views/view.h" |
| 37 | 37 |
| 38 namespace payments { | |
| 39 | |
| 38 namespace { | 40 namespace { |
| 39 | 41 |
| 40 // TODO(tmartino): Consider combining this with the Android equivalent in | 42 // TODO(tmartino): Consider combining this with the Android equivalent in |
| 41 // PersonalDataManager.java | 43 // PersonalDataManager.java |
| 42 base::string16 GetAddressFromProfile(const autofill::AutofillProfile& profile, | 44 base::string16 GetAddressFromProfile(const autofill::AutofillProfile& profile, |
| 43 const std::string& locale) { | 45 const std::string& locale) { |
| 44 std::vector<autofill::ServerFieldType> fields; | 46 std::vector<autofill::ServerFieldType> fields; |
| 45 fields.push_back(autofill::COMPANY_NAME); | 47 fields.push_back(autofill::COMPANY_NAME); |
| 46 fields.push_back(autofill::ADDRESS_HOME_LINE1); | 48 fields.push_back(autofill::ADDRESS_HOME_LINE1); |
| 47 fields.push_back(autofill::ADDRESS_HOME_LINE2); | 49 fields.push_back(autofill::ADDRESS_HOME_LINE2); |
| 48 fields.push_back(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY); | 50 fields.push_back(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY); |
| 49 fields.push_back(autofill::ADDRESS_HOME_CITY); | 51 fields.push_back(autofill::ADDRESS_HOME_CITY); |
| 50 fields.push_back(autofill::ADDRESS_HOME_STATE); | 52 fields.push_back(autofill::ADDRESS_HOME_STATE); |
| 51 fields.push_back(autofill::ADDRESS_HOME_ZIP); | 53 fields.push_back(autofill::ADDRESS_HOME_ZIP); |
| 52 fields.push_back(autofill::ADDRESS_HOME_SORTING_CODE); | 54 fields.push_back(autofill::ADDRESS_HOME_SORTING_CODE); |
| 53 | 55 |
| 54 return profile.ConstructInferredLabel(fields, fields.size(), locale); | 56 return profile.ConstructInferredLabel(fields, fields.size(), locale); |
| 55 } | 57 } |
| 56 | 58 |
| 59 std::unique_ptr<views::View> GetThreeLineLabel(AddressStyleType type, | |
| 60 base::string16 s1, | |
|
please use gerrit instead
2017/03/10 20:23:12
const-ref these strings to avoid copies.
tmartino
2017/03/10 23:46:23
Done
| |
| 61 base::string16 s2, | |
| 62 base::string16 s3) { | |
| 63 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); | |
| 64 std::unique_ptr<views::BoxLayout> layout = | |
| 65 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, 0, 0); | |
| 66 layout->set_cross_axis_alignment( | |
| 67 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | |
| 68 container->SetLayoutManager(layout.release()); | |
| 69 | |
| 70 if (!s1.empty()) { | |
| 71 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>(s1); | |
| 72 if (type == AddressStyleType::DETAILED) { | |
| 73 const gfx::FontList& font_list = label->font_list(); | |
| 74 label->SetFontList(font_list.DeriveWithWeight(gfx::Font::Weight::BOLD)); | |
| 75 } | |
| 76 container->AddChildView(label.release()); | |
| 77 } | |
| 78 | |
| 79 if (!s2.empty()) { | |
| 80 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>(s2); | |
| 81 container->AddChildView(label.release()); | |
| 82 } | |
| 83 | |
| 84 if (!s3.empty()) { | |
| 85 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>(s3); | |
| 86 container->AddChildView(label.release()); | |
| 87 } | |
| 88 | |
| 89 // TODO(anthonyvd): add the error label | |
| 90 | |
| 91 return container; | |
| 92 } | |
| 93 | |
| 57 // Paints the gray horizontal line that doesn't span the entire width of the | 94 // Paints the gray horizontal line that doesn't span the entire width of the |
| 58 // dialog at the bottom of the view it borders. | 95 // dialog at the bottom of the view it borders. |
| 59 class PaymentRequestRowBorderPainter : public views::Painter { | 96 class PaymentRequestRowBorderPainter : public views::Painter { |
| 60 public: | 97 public: |
| 61 PaymentRequestRowBorderPainter() {} | 98 PaymentRequestRowBorderPainter() {} |
| 62 ~PaymentRequestRowBorderPainter() override {} | 99 ~PaymentRequestRowBorderPainter() override {} |
| 63 | 100 |
| 64 // views::Painter: | 101 // views::Painter: |
| 65 gfx::Size GetMinimumSize() const override { | 102 gfx::Size GetMinimumSize() const override { |
| 66 return gfx::Size(2 * payments::kPaymentRequestRowHorizontalInsets, 1); | 103 return gfx::Size(2 * payments::kPaymentRequestRowHorizontalInsets, 1); |
| 67 } | 104 } |
| 68 | 105 |
| 69 void Paint(gfx::Canvas* canvas, const gfx::Size& size) override { | 106 void Paint(gfx::Canvas* canvas, const gfx::Size& size) override { |
| 70 int line_height = size.height() - 1; | 107 int line_height = size.height() - 1; |
| 71 canvas->DrawLine( | 108 canvas->DrawLine( |
| 72 gfx::PointF(payments::kPaymentRequestRowHorizontalInsets, line_height), | 109 gfx::PointF(payments::kPaymentRequestRowHorizontalInsets, line_height), |
| 73 gfx::PointF(size.width() - payments::kPaymentRequestRowHorizontalInsets, | 110 gfx::PointF(size.width() - payments::kPaymentRequestRowHorizontalInsets, |
| 74 line_height), | 111 line_height), |
| 75 SK_ColorLTGRAY); | 112 SK_ColorLTGRAY); |
| 76 } | 113 } |
| 77 | 114 |
| 78 private: | 115 private: |
| 79 DISALLOW_COPY_AND_ASSIGN(PaymentRequestRowBorderPainter); | 116 DISALLOW_COPY_AND_ASSIGN(PaymentRequestRowBorderPainter); |
| 80 }; | 117 }; |
| 81 | 118 |
| 82 } // namespace | 119 } // namespace |
| 83 | 120 |
| 84 namespace payments { | |
| 85 | |
| 86 std::unique_ptr<views::View> CreateSheetHeaderView( | 121 std::unique_ptr<views::View> CreateSheetHeaderView( |
| 87 bool show_back_arrow, | 122 bool show_back_arrow, |
| 88 const base::string16& title, | 123 const base::string16& title, |
| 89 views::VectorIconButtonDelegate* delegate) { | 124 views::VectorIconButtonDelegate* delegate) { |
| 90 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); | 125 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); |
| 91 views::GridLayout* layout = new views::GridLayout(container.get()); | 126 views::GridLayout* layout = new views::GridLayout(container.get()); |
| 92 container->SetLayoutManager(layout); | 127 container->SetLayoutManager(layout); |
| 93 | 128 |
| 94 constexpr int kHeaderTopVerticalInset = 14; | 129 constexpr int kHeaderTopVerticalInset = 14; |
| 95 constexpr int kHeaderBottomVerticalInset = 8; | 130 constexpr int kHeaderBottomVerticalInset = 8; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 card_icon_view->SetBorder(views::CreateRoundedRectBorder( | 183 card_icon_view->SetBorder(views::CreateRoundedRectBorder( |
| 149 1, 3, card_icon_view->GetNativeTheme()->GetSystemColor( | 184 1, 3, card_icon_view->GetNativeTheme()->GetSystemColor( |
| 150 ui::NativeTheme::kColorId_UnfocusedBorderColor))); | 185 ui::NativeTheme::kColorId_UnfocusedBorderColor))); |
| 151 return card_icon_view; | 186 return card_icon_view; |
| 152 } | 187 } |
| 153 | 188 |
| 154 std::unique_ptr<views::View> GetShippingAddressLabel( | 189 std::unique_ptr<views::View> GetShippingAddressLabel( |
| 155 AddressStyleType type, | 190 AddressStyleType type, |
| 156 const std::string& locale, | 191 const std::string& locale, |
| 157 const autofill::AutofillProfile& profile) { | 192 const autofill::AutofillProfile& profile) { |
| 158 base::string16 name_value = | 193 base::string16 name = |
| 159 profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale); | 194 profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale); |
| 160 | 195 |
| 161 // TODO(tmartino): Add bold styling for name in DETAILED style. | 196 base::string16 address = GetAddressFromProfile(profile, locale); |
| 162 | 197 |
| 163 base::string16 address_value = GetAddressFromProfile(profile, locale); | 198 base::string16 phone = profile.GetInfo( |
| 164 | |
| 165 base::string16 phone_value = profile.GetInfo( | |
| 166 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), locale); | 199 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), locale); |
| 167 | 200 |
| 168 std::vector<base::string16> values; | 201 return GetThreeLineLabel(type, name, address, phone); |
| 169 if (!name_value.empty()) | |
| 170 values.push_back(name_value); | |
| 171 if (!address_value.empty()) | |
| 172 values.push_back(address_value); | |
| 173 if (!phone_value.empty()) | |
| 174 values.push_back(phone_value); | |
| 175 | |
| 176 return base::MakeUnique<views::StyledLabel>( | |
| 177 base::JoinString(values, base::ASCIIToUTF16("\n")), nullptr); | |
| 178 } | 202 } |
| 179 | 203 |
| 180 // TODO(anthonyvd): unit test the label layout. | 204 // TODO(anthonyvd): unit test the label layout. |
| 181 std::unique_ptr<views::View> GetContactInfoLabel( | 205 std::unique_ptr<views::View> GetContactInfoLabel( |
| 182 AddressStyleType type, | 206 AddressStyleType type, |
| 183 const std::string& locale, | 207 const std::string& locale, |
| 184 const autofill::AutofillProfile& profile, | 208 const autofill::AutofillProfile& profile, |
| 185 bool show_payer_name, | 209 bool show_payer_name, |
| 186 bool show_payer_email, | 210 bool show_payer_email, |
| 187 bool show_payer_phone) { | 211 bool show_payer_phone) { |
| 188 std::unique_ptr<views::View> container = base::MakeUnique<views::View>(); | 212 base::string16 name = |
| 189 std::unique_ptr<views::BoxLayout> layout = | 213 show_payer_name |
| 190 base::MakeUnique<views::BoxLayout>(views::BoxLayout::kVertical, 0, 0, 0); | 214 ? profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale) |
| 191 layout->set_cross_axis_alignment( | 215 : base::string16(); |
| 192 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | |
| 193 container->SetLayoutManager(layout.release()); | |
| 194 | 216 |
| 195 if (show_payer_name) { | 217 base::string16 phone = |
| 196 base::string16 name = | 218 show_payer_phone |
| 197 profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale); | 219 ? profile.GetInfo( |
| 198 if (!name.empty()) { | 220 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 199 std::unique_ptr<views::Label> label = | 221 locale) |
| 200 base::MakeUnique<views::Label>(name); | 222 : base::string16(); |
| 201 if (type == AddressStyleType::DETAILED) { | |
| 202 const gfx::FontList& font_list = label->font_list(); | |
| 203 label->SetFontList(font_list.DeriveWithWeight(gfx::Font::Weight::BOLD)); | |
| 204 } | |
| 205 container->AddChildView(label.release()); | |
| 206 } | |
| 207 } | |
| 208 | 223 |
| 209 if (show_payer_phone) { | 224 base::string16 email = |
| 210 base::string16 phone = profile.GetInfo( | 225 show_payer_email |
| 211 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), locale); | 226 ? profile.GetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), |
| 212 if (!phone.empty()) { | 227 locale) |
| 213 std::unique_ptr<views::Label> label = | 228 : base::string16(); |
| 214 base::MakeUnique<views::Label>(phone); | |
| 215 container->AddChildView(label.release()); | |
| 216 } | |
| 217 } | |
| 218 | 229 |
| 219 if (show_payer_email) { | 230 return GetThreeLineLabel(type, name, phone, email); |
| 220 base::string16 email = profile.GetInfo( | |
| 221 autofill::AutofillType(autofill::EMAIL_ADDRESS), locale); | |
| 222 if (!email.empty()) { | |
| 223 std::unique_ptr<views::Label> label = | |
| 224 base::MakeUnique<views::Label>(email); | |
| 225 container->AddChildView(label.release()); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 // TODO(anthonyvd): add the error label | |
| 230 | |
| 231 return container; | |
| 232 } | 231 } |
| 233 | 232 |
| 234 std::unique_ptr<views::Border> CreatePaymentRequestRowBorder() { | 233 std::unique_ptr<views::Border> CreatePaymentRequestRowBorder() { |
| 235 return views::CreateBorderPainter( | 234 return views::CreateBorderPainter( |
| 236 base::MakeUnique<PaymentRequestRowBorderPainter>(), | 235 base::MakeUnique<PaymentRequestRowBorderPainter>(), |
| 237 gfx::Insets()); | 236 gfx::Insets()); |
| 238 } | 237 } |
| 239 | 238 |
| 240 } // namespace payments | 239 } // namespace payments |
| OLD | NEW |