1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/autofill/autofill_dialog_views.h"
#include <utility>
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
#include "chrome/browser/ui/autofill/autofill_dialog_sign_in_delegate.h"
#include "chrome/browser/ui/views/constrained_window_views.h"
#include "components/autofill/browser/autofill_type.h"
#include "components/autofill/content/browser/wallet/wallet_service_url.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/models/menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/separator.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_client_view.h"
using web_modal::WebContentsModalDialogManager;
namespace autofill {
namespace {
// The minimum useful height of the contents area of the dialog.
const int kMinimumContentsHeight = 100;
// Horizontal padding between text and other elements (in pixels).
const int kAroundTextPadding = 4;
// Size of the triangular mark that indicates an invalid textfield (in pixels).
const size_t kDogEarSize = 10;
// The space between the edges of a notification bar and the text within (in
// pixels).
const size_t kNotificationPadding = 14;
// Vertical padding above and below each detail section (in pixels).
const size_t kDetailSectionInset = 10;
const size_t kAutocheckoutProgressBarWidth = 300;
const size_t kAutocheckoutProgressBarHeight = 11;
const size_t kArrowHeight = 7;
const size_t kArrowWidth = 2 * kArrowHeight;
// The padding around the edges of the legal documents text, in pixels.
const size_t kLegalDocPadding = 20;
// Slight shading for mouse hover and legal document background.
SkColor kShadingColor = SkColorSetARGB(7, 0, 0, 0);
// A border color for the legal document view.
SkColor kSubtleBorderColor = SkColorSetARGB(10, 0, 0, 0);
// The top padding, in pixels, for the suggestions menu dropdown arrows.
const size_t kMenuButtonTopOffset = 5;
// The side padding, in pixels, for the suggestions menu dropdown arrows.
const size_t kMenuButtonHorizontalPadding = 20;
const char kDecoratedTextfieldClassName[] = "autofill/DecoratedTextfield";
const char kNotificationAreaClassName[] = "autofill/NotificationArea";
views::Border* CreateLabelAlignmentBorder() {
// TODO(estade): this should be made to match the native textfield top
// inset. It's hard to get at, so for now it's hard-coded.
return views::Border::CreateEmptyBorder(4, 0, 0, 0);
}
// Returns a label that describes a details section.
views::Label* CreateDetailsSectionLabel(const string16& text) {
views::Label* label = new views::Label(text);
label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
label->SetFont(label->font().DeriveFont(0, gfx::Font::BOLD));
label->set_border(CreateLabelAlignmentBorder());
return label;
}
// Draws an arrow at the top of |canvas| pointing to |tip_x|.
void DrawArrow(gfx::Canvas* canvas, int tip_x, const SkColor& color) {
const int arrow_half_width = kArrowWidth / 2.0f;
const int arrow_middle = tip_x - arrow_half_width;
SkPath arrow;
arrow.moveTo(arrow_middle - arrow_half_width, kArrowHeight);
arrow.lineTo(arrow_middle + arrow_half_width, kArrowHeight);
arrow.lineTo(arrow_middle, 0);
arrow.close();
canvas->ClipPath(arrow);
canvas->DrawColor(color);
}
// This class handles layout for the first row of a SuggestionView.
// It exists to circumvent shortcomings of GridLayout and BoxLayout (namely that
// the former doesn't fully respect child visibility, and that the latter won't
// expand a single child).
class SectionRowView : public views::View {
public:
SectionRowView() {}
virtual ~SectionRowView() {}
// views::View implementation:
virtual gfx::Size GetPreferredSize() OVERRIDE {
// Only the height matters anyway.
return child_at(2)->GetPreferredSize();
}
virtual void Layout() OVERRIDE {
const gfx::Rect bounds = GetContentsBounds();
// Icon is left aligned.
int start_x = bounds.x();
views::View* icon = child_at(0);
if (icon->visible()) {
icon->SizeToPreferredSize();
icon->SetX(start_x);
icon->SetY(bounds.y() +
(bounds.height() - icon->bounds().height()) / 2);
start_x += icon->bounds().width() + kAroundTextPadding;
}
// Textfield is right aligned.
int end_x = bounds.width();
views::View* decorated = child_at(2);
if (decorated->visible()) {
decorated->SizeToPreferredSize();
decorated->SetX(bounds.width() - decorated->bounds().width());
decorated->SetY(bounds.y());
end_x = decorated->bounds().x() - kAroundTextPadding;
}
// Label takes up all the space in between.
views::View* label = child_at(1);
if (label->visible())
label->SetBounds(start_x, bounds.y(), end_x - start_x, bounds.height());
views::View::Layout();
}
private:
DISALLOW_COPY_AND_ASSIGN(SectionRowView);
};
// This view is used for the contents of the error bubble widget.
class ErrorBubbleContents : public views::View {
public:
explicit ErrorBubbleContents(const string16& message)
: color_(kWarningColor) {
set_border(views::Border::CreateEmptyBorder(kArrowHeight, 0, 0, 0));
views::Label* label = new views::Label();
label->SetText(message);
label->SetAutoColorReadabilityEnabled(false);
label->SetEnabledColor(SK_ColorWHITE);
label->set_border(
views::Border::CreateSolidSidedBorder(5, 10, 5, 10, color_));
label->set_background(
views::Background::CreateSolidBackground(color_));
SetLayoutManager(new views::FillLayout());
AddChildView(label);
}
virtual ~ErrorBubbleContents() {}
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
views::View::OnPaint(canvas);
DrawArrow(canvas, width() / 2.0f, color_);
}
private:
SkColor color_;
DISALLOW_COPY_AND_ASSIGN(ErrorBubbleContents);
};
// A view that runs a callback whenever its bounds change.
class DetailsContainerView : public views::View {
public:
explicit DetailsContainerView(const base::Closure& callback)
: bounds_changed_callback_(callback) {}
virtual ~DetailsContainerView() {}
// views::View implementation.
virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE {
bounds_changed_callback_.Run();
}
private:
base::Closure bounds_changed_callback_;
DISALLOW_COPY_AND_ASSIGN(DetailsContainerView);
};
// ButtonStripView wraps the Autocheckout progress bar and the "[X] Save details
// in Chrome" checkbox and listens for visibility changes.
class ButtonStripView : public views::View {
public:
ButtonStripView() {}
virtual ~ButtonStripView() {}
protected:
virtual void ChildVisibilityChanged(views::View* child) OVERRIDE {
PreferredSizeChanged();
}
private:
DISALLOW_COPY_AND_ASSIGN(ButtonStripView);
};
} // namespace
// AutofillDialogViews::SizeLimitedScrollView ----------------------------------
AutofillDialogViews::SizeLimitedScrollView::SizeLimitedScrollView(
views::View* scroll_contents)
: max_height_(-1) {
set_hide_horizontal_scrollbar(true);
SetContents(scroll_contents);
}
AutofillDialogViews::SizeLimitedScrollView::~SizeLimitedScrollView() {}
void AutofillDialogViews::SizeLimitedScrollView::Layout() {
contents()->SizeToPreferredSize();
ScrollView::Layout();
}
gfx::Size AutofillDialogViews::SizeLimitedScrollView::GetPreferredSize() {
gfx::Size size = contents()->GetPreferredSize();
if (max_height_ >= 0 && max_height_ < size.height())
size.set_height(max_height_);
return size;
}
void AutofillDialogViews::SizeLimitedScrollView::SetMaximumHeight(
int max_height) {
int old_max = max_height_;
max_height_ = max_height;
if (max_height_ < height() || old_max <= height())
PreferredSizeChanged();
}
// AutofillDialogViews::ErrorBubble --------------------------------------------
AutofillDialogViews::ErrorBubble::ErrorBubble(views::View* anchor,
const string16& message)
: anchor_(anchor),
contents_(new ErrorBubbleContents(message)),
observer_(this) {
widget_ = new views::Widget;
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.transparent = true;
views::Widget* anchor_widget = anchor->GetWidget();
DCHECK(anchor_widget);
params.parent = anchor_widget->GetNativeView();
widget_->Init(params);
widget_->SetContentsView(contents_);
UpdatePosition();
observer_.Add(widget_);
}
AutofillDialogViews::ErrorBubble::~ErrorBubble() {
if (widget_)
widget_->Close();
}
bool AutofillDialogViews::ErrorBubble::IsShowing() {
return widget_ && widget_->IsVisible();
}
void AutofillDialogViews::ErrorBubble::UpdatePosition() {
if (!widget_)
return;
if (!anchor_->GetVisibleBounds().IsEmpty()) {
widget_->SetBounds(GetBoundsForWidget());
widget_->SetVisibilityChangedAnimationsEnabled(true);
widget_->Show();
} else {
widget_->SetVisibilityChangedAnimationsEnabled(false);
widget_->Hide();
}
}
void AutofillDialogViews::ErrorBubble::OnWidgetClosing(views::Widget* widget) {
DCHECK_EQ(widget_, widget);
observer_.Remove(widget_);
widget_ = NULL;
}
gfx::Rect AutofillDialogViews::ErrorBubble::GetBoundsForWidget() {
gfx::Rect anchor_bounds = anchor_->GetBoundsInScreen();
gfx::Rect bubble_bounds;
bubble_bounds.set_size(contents_->GetPreferredSize());
bubble_bounds.set_x(anchor_bounds.right() -
(anchor_bounds.width() + bubble_bounds.width()) / 2);
const int kErrorBubbleOverlap = 3;
bubble_bounds.set_y(anchor_bounds.bottom() - kErrorBubbleOverlap);
return bubble_bounds;
}
// AutofillDialogViews::DecoratedTextfield -------------------------------------
AutofillDialogViews::DecoratedTextfield::DecoratedTextfield(
const string16& default_value,
const string16& placeholder,
views::TextfieldController* controller)
: textfield_(new views::Textfield()),
invalid_(false) {
textfield_->set_placeholder_text(placeholder);
textfield_->SetText(default_value);
textfield_->SetController(controller);
SetLayoutManager(new views::FillLayout());
AddChildView(textfield_);
}
AutofillDialogViews::DecoratedTextfield::~DecoratedTextfield() {}
void AutofillDialogViews::DecoratedTextfield::SetInvalid(bool invalid) {
invalid_ = invalid;
if (invalid)
textfield_->SetBorderColor(kWarningColor);
else
textfield_->UseDefaultBorderColor();
SchedulePaint();
}
const char* AutofillDialogViews::DecoratedTextfield::GetClassName() const {
return kDecoratedTextfieldClassName;
}
void AutofillDialogViews::DecoratedTextfield::PaintChildren(
gfx::Canvas* canvas) {}
void AutofillDialogViews::DecoratedTextfield::OnPaint(gfx::Canvas* canvas) {
// Draw the textfield first.
canvas->Save();
if (FlipCanvasOnPaintForRTLUI()) {
canvas->Translate(gfx::Vector2d(width(), 0));
canvas->Scale(-1, 1);
}
views::View::PaintChildren(canvas);
canvas->Restore();
// Then draw extra stuff on top.
if (invalid_) {
SkPath dog_ear;
dog_ear.moveTo(width() - kDogEarSize, 0);
dog_ear.lineTo(width(), 0);
dog_ear.lineTo(width(), kDogEarSize);
dog_ear.close();
canvas->ClipPath(dog_ear);
canvas->DrawColor(kWarningColor);
}
}
void AutofillDialogViews::DecoratedTextfield::RequestFocus() {
textfield()->RequestFocus();
}
// AutofillDialogViews::AccountChooser -----------------------------------------
AutofillDialogViews::AccountChooser::AccountChooser(
AutofillDialogController* controller)
: image_(new views::ImageView()),
label_(new views::Label()),
arrow_(new views::ImageView()),
link_(new views::Link()),
controller_(controller) {
SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
kAroundTextPadding));
AddChildView(image_);
AddChildView(label_);
arrow_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_MENU_DROPARROW).ToImageSkia());
AddChildView(arrow_);
link_->set_listener(this);
AddChildView(link_);
}
AutofillDialogViews::AccountChooser::~AccountChooser() {}
void AutofillDialogViews::AccountChooser::Update() {
gfx::Image icon = controller_->AccountChooserImage();
image_->SetImage(icon.AsImageSkia());
label_->SetText(controller_->AccountChooserText());
bool show_link = !controller_->MenuModelForAccountChooser();
label_->SetVisible(!show_link);
arrow_->SetVisible(!show_link);
link_->SetText(controller_->SignInLinkText());
link_->SetVisible(show_link);
menu_runner_.reset();
PreferredSizeChanged();
}
bool AutofillDialogViews::AccountChooser::OnMousePressed(
const ui::MouseEvent& event) {
// Return true so we get the release event.
if (controller_->MenuModelForAccountChooser())
return event.IsOnlyLeftMouseButton();
return false;
}
void AutofillDialogViews::AccountChooser::OnMouseReleased(
const ui::MouseEvent& event) {
if (!HitTestPoint(event.location()))
return;
ui::MenuModel* model = controller_->MenuModelForAccountChooser();
if (!model)
return;
menu_runner_.reset(new views::MenuRunner(model));
ignore_result(
menu_runner_->RunMenuAt(GetWidget(),
NULL,
GetBoundsInScreen(),
views::MenuItemView::TOPRIGHT,
0));
}
void AutofillDialogViews::AccountChooser::LinkClicked(views::Link* source,
int event_flags) {
controller_->SignInLinkClicked();
}
// AutofillDialogViews::NotificationArea ---------------------------------------
AutofillDialogViews::NotificationArea::NotificationArea(
AutofillDialogController* controller)
: controller_(controller),
checkbox_(NULL) {
// Reserve vertical space for the arrow (regardless of whether one exists).
set_border(views::Border::CreateEmptyBorder(kArrowHeight, 0, 0, 0));
views::BoxLayout* box_layout =
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
SetLayoutManager(box_layout);
}
AutofillDialogViews::NotificationArea::~NotificationArea() {}
void AutofillDialogViews::NotificationArea::SetNotifications(
const std::vector<DialogNotification>& notifications) {
notifications_ = notifications;
RemoveAllChildViews(true);
checkbox_ = NULL;
if (notifications_.empty())
return;
for (size_t i = 0; i < notifications_.size(); ++i) {
const DialogNotification& notification = notifications_[i];
scoped_ptr<views::View> view;
if (notification.HasCheckbox()) {
scoped_ptr<views::Checkbox> checkbox(new views::Checkbox(string16()));
checkbox_ = checkbox.get();
// We have to do this instead of using set_border() because a border
// is being used to draw the check square.
static_cast<views::LabelButtonBorder*>(checkbox->border())->
set_insets(gfx::Insets(kNotificationPadding,
kNotificationPadding,
kNotificationPadding,
kNotificationPadding));
if (!notification.interactive())
checkbox->SetState(views::Button::STATE_DISABLED);
checkbox->SetText(notification.display_text());
checkbox->SetTextMultiLine(true);
checkbox->SetHorizontalAlignment(gfx::ALIGN_LEFT);
checkbox->SetTextColor(views::Button::STATE_NORMAL,
notification.GetTextColor());
checkbox->SetTextColor(views::Button::STATE_HOVERED,
notification.GetTextColor());
checkbox->SetChecked(notification.checked());
checkbox->set_listener(this);
view.reset(checkbox.release());
} else {
scoped_ptr<views::Label> label(new views::Label());
label->SetText(notification.display_text());
label->SetMultiLine(true);
label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label->SetAutoColorReadabilityEnabled(false);
label->SetEnabledColor(notification.GetTextColor());
label->set_border(views::Border::CreateSolidBorder(
kNotificationPadding, notification.GetBackgroundColor()));
view.reset(label.release());
}
view->set_background(views::Background::CreateSolidBackground(
notification.GetBackgroundColor()));
AddChildView(view.release());
}
PreferredSizeChanged();
}
gfx::Size AutofillDialogViews::NotificationArea::GetPreferredSize() {
gfx::Size size = views::View::GetPreferredSize();
// Ensure that long notifications wrap and don't enlarge the dialog.
size.set_width(1);
return size;
}
const char* AutofillDialogViews::NotificationArea::GetClassName() const {
return kNotificationAreaClassName;
}
void AutofillDialogViews::NotificationArea::OnPaint(gfx::Canvas* canvas) {
views::View::OnPaint(canvas);
if (HasArrow()) {
DrawArrow(
canvas,
GetMirroredXInView(width() - arrow_centering_anchor_->width() / 2.0f),
notifications_[0].GetBackgroundColor());
}
}
void AutofillDialogViews::OnWidgetClosing(views::Widget* widget) {
observer_.Remove(widget);
}
void AutofillDialogViews::OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) {
int non_scrollable_height = window_->GetContentsView()->bounds().height() -
scrollable_area_->bounds().height();
int browser_window_height = widget->GetContentsView()->bounds().height();
scrollable_area_->SetMaximumHeight(
std::max(kMinimumContentsHeight,
(browser_window_height - non_scrollable_height) * 8 / 10));
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::NotificationArea::ButtonPressed(
views::Button* sender, const ui::Event& event) {
DCHECK_EQ(sender, checkbox_);
controller_->NotificationCheckboxStateChanged(notifications_.front().type(),
checkbox_->checked());
}
bool AutofillDialogViews::NotificationArea::HasArrow() {
return !notifications_.empty() && notifications_[0].HasArrow() &&
arrow_centering_anchor_.get();
}
// AutofillDialogViews::SectionContainer ---------------------------------------
AutofillDialogViews::SectionContainer::SectionContainer(
const string16& label,
views::View* controls,
views::Button* proxy_button)
: proxy_button_(proxy_button),
forward_mouse_events_(false) {
set_notify_enter_exit_on_child(true);
views::GridLayout* layout = new views::GridLayout(this);
layout->SetInsets(kDetailSectionInset, 0, kDetailSectionInset, 0);
SetLayoutManager(layout);
const int kColumnSetId = 0;
views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId);
// TODO(estade): pull out these constants, and figure out better values
// for them.
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::LEADING,
0,
views::GridLayout::FIXED,
180,
0);
column_set->AddPaddingColumn(0, 30);
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::LEADING,
0,
views::GridLayout::FIXED,
300,
0);
layout->StartRow(0, kColumnSetId);
layout->AddView(CreateDetailsSectionLabel(label));
layout->AddView(controls);
}
AutofillDialogViews::SectionContainer::~SectionContainer() {}
void AutofillDialogViews::SectionContainer::SetActive(bool active) {
bool is_active = active && proxy_button_->visible();
if (is_active == !!background())
return;
set_background(is_active ?
views::Background::CreateSolidBackground(kShadingColor) :
NULL);
SchedulePaint();
}
void AutofillDialogViews::SectionContainer::SetForwardMouseEvents(
bool forward) {
forward_mouse_events_ = forward;
if (!forward)
set_background(NULL);
}
void AutofillDialogViews::SectionContainer::OnMouseMoved(
const ui::MouseEvent& event) {
if (!forward_mouse_events_)
return;
SetActive(true);
}
void AutofillDialogViews::SectionContainer::OnMouseEntered(
const ui::MouseEvent& event) {
if (!forward_mouse_events_)
return;
SetActive(true);
proxy_button_->OnMouseEntered(ProxyEvent(event));
SchedulePaint();
}
void AutofillDialogViews::SectionContainer::OnMouseExited(
const ui::MouseEvent& event) {
if (!forward_mouse_events_)
return;
SetActive(false);
proxy_button_->OnMouseExited(ProxyEvent(event));
SchedulePaint();
}
bool AutofillDialogViews::SectionContainer::OnMousePressed(
const ui::MouseEvent& event) {
if (!forward_mouse_events_)
return false;
return proxy_button_->OnMousePressed(ProxyEvent(event));
}
void AutofillDialogViews::SectionContainer::OnMouseReleased(
const ui::MouseEvent& event) {
if (!forward_mouse_events_)
return;
proxy_button_->OnMouseReleased(ProxyEvent(event));
}
// static
ui::MouseEvent AutofillDialogViews::SectionContainer::ProxyEvent(
const ui::MouseEvent& event) {
ui::MouseEvent event_copy = event;
event_copy.set_location(gfx::Point());
return event_copy;
}
// AutofilDialogViews::SuggestionView ------------------------------------------
AutofillDialogViews::SuggestionView::SuggestionView(
const string16& edit_label,
AutofillDialogViews* autofill_dialog)
: label_(new views::Label()),
label_line_2_(new views::Label()),
icon_(new views::ImageView()),
label_container_(new SectionRowView()),
decorated_(
new DecoratedTextfield(string16(), string16(), autofill_dialog)),
edit_link_(new views::Link(edit_label)) {
// Label and icon.
label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label_->set_border(CreateLabelAlignmentBorder());
label_container_->AddChildView(icon_);
label_container_->AddChildView(label_);
label_container_->AddChildView(decorated_);
decorated_->SetVisible(false);
// TODO(estade): get the sizing and spacing right on this textfield.
decorated_->textfield()->set_default_width_in_chars(10);
AddChildView(label_container_);
label_line_2_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
label_line_2_->SetVisible(false);
AddChildView(label_line_2_);
// TODO(estade): The link needs to have a different color when hovered.
edit_link_->set_listener(autofill_dialog);
edit_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
edit_link_->SetUnderline(false);
// This container prevents the edit link from being horizontally stretched.
views::View* link_container = new views::View();
link_container->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
link_container->AddChildView(edit_link_);
AddChildView(link_container);
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
}
AutofillDialogViews::SuggestionView::~SuggestionView() {}
void AutofillDialogViews::SuggestionView::SetEditable(bool editable) {
edit_link_->SetVisible(editable);
}
void AutofillDialogViews::SuggestionView::SetSuggestionText(
const string16& text,
gfx::Font::FontStyle text_style) {
label_->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont(
ui::ResourceBundle::BaseFont).DeriveFont(0, text_style));
// TODO(estade): does this localize well?
string16 line_return(ASCIIToUTF16("\n"));
size_t position = text.find(line_return);
if (position == string16::npos) {
label_->SetText(text);
label_line_2_->SetVisible(false);
} else {
label_->SetText(text.substr(0, position));
label_line_2_->SetText(text.substr(position + line_return.length()));
label_line_2_->SetVisible(true);
}
}
void AutofillDialogViews::SuggestionView::SetSuggestionIcon(
const gfx::Image& image) {
icon_->SetVisible(!image.IsEmpty());
icon_->SetImage(image.AsImageSkia());
}
void AutofillDialogViews::SuggestionView::ShowTextfield(
const string16& placeholder_text,
const gfx::ImageSkia& icon) {
decorated_->textfield()->set_placeholder_text(placeholder_text);
decorated_->textfield()->SetIcon(icon);
decorated_->SetVisible(true);
// The textfield will increase the height of the first row and cause the
// label to be aligned properly, so the border is not necessary.
label_->set_border(NULL);
}
// AutofilDialogViews::AutocheckoutProgressBar ---------------------------------
AutofillDialogViews::AutocheckoutProgressBar::AutocheckoutProgressBar() {}
AutofillDialogViews::AutocheckoutProgressBar::~AutocheckoutProgressBar() {}
gfx::Size AutofillDialogViews::AutocheckoutProgressBar::GetPreferredSize() {
return gfx::Size(kAutocheckoutProgressBarWidth,
kAutocheckoutProgressBarHeight);
}
// AutofillDialogView ----------------------------------------------------------
// static
AutofillDialogView* AutofillDialogView::Create(
AutofillDialogController* controller) {
return new AutofillDialogViews(controller);
}
// AutofillDialogViews ---------------------------------------------------------
AutofillDialogViews::AutofillDialogViews(AutofillDialogController* controller)
: controller_(controller),
window_(NULL),
contents_(NULL),
notification_area_(NULL),
account_chooser_(NULL),
sign_in_webview_(NULL),
main_container_(NULL),
scrollable_area_(NULL),
details_container_(NULL),
button_strip_extra_view_(NULL),
save_in_chrome_checkbox_(NULL),
autocheckout_progress_bar_view_(NULL),
autocheckout_progress_bar_(NULL),
footnote_view_(NULL),
legal_document_view_(NULL),
focus_manager_(NULL),
observer_(this) {
DCHECK(controller);
detail_groups_.insert(std::make_pair(SECTION_EMAIL,
DetailsGroup(SECTION_EMAIL)));
detail_groups_.insert(std::make_pair(SECTION_CC,
DetailsGroup(SECTION_CC)));
detail_groups_.insert(std::make_pair(SECTION_BILLING,
DetailsGroup(SECTION_BILLING)));
detail_groups_.insert(std::make_pair(SECTION_CC_BILLING,
DetailsGroup(SECTION_CC_BILLING)));
detail_groups_.insert(std::make_pair(SECTION_SHIPPING,
DetailsGroup(SECTION_SHIPPING)));
}
AutofillDialogViews::~AutofillDialogViews() {
DCHECK(!window_);
}
void AutofillDialogViews::Show() {
InitChildViews();
UpdateAccountChooser();
UpdateNotificationArea();
UpdateSaveInChromeCheckbox();
// Ownership of |contents_| is handed off by this call. The widget will take
// care of deleting itself after calling DeleteDelegate().
WebContentsModalDialogManager* web_contents_modal_dialog_manager =
WebContentsModalDialogManager::FromWebContents(
controller_->web_contents());
window_ = CreateWebContentsModalDialogViews(
this,
controller_->web_contents()->GetView()->GetNativeView(),
web_contents_modal_dialog_manager->delegate()->
GetWebContentsModalDialogHost());
web_contents_modal_dialog_manager->ShowDialog(window_->GetNativeView());
focus_manager_ = window_->GetFocusManager();
focus_manager_->AddFocusChangeListener(this);
#if defined(OS_WIN) && !defined(USE_AURA)
// On non-Aura Windows a standard accelerator gets registered that will
// navigate on a backspace. Override that here.
// TODO(abodenha): Remove this when no longer needed. See
// http://crbug.com/242584.
ui::Accelerator backspace(ui::VKEY_BACK, ui::EF_NONE);
focus_manager_->RegisterAccelerator(
backspace, ui::AcceleratorManager::kNormalPriority, this);
#endif
// Listen for size changes on the browser.
views::Widget* browser_widget =
views::Widget::GetTopLevelWidgetForNativeView(
controller_->web_contents()->GetView()->GetNativeView());
observer_.Add(browser_widget);
OnWidgetBoundsChanged(browser_widget, gfx::Rect());
}
void AutofillDialogViews::Hide() {
if (window_)
window_->Close();
}
void AutofillDialogViews::UpdateAccountChooser() {
account_chooser_->Update();
// Update legal documents for the account.
if (footnote_view_) {
const string16 text = controller_->LegalDocumentsText();
legal_document_view_->SetText(text);
if (!text.empty()) {
const std::vector<ui::Range>& link_ranges =
controller_->LegalDocumentLinks();
for (size_t i = 0; i < link_ranges.size(); ++i) {
legal_document_view_->AddStyleRange(
link_ranges[i],
views::StyledLabel::RangeStyleInfo::CreateForLink());
}
}
footnote_view_->SetVisible(!text.empty());
ContentsPreferredSizeChanged();
}
}
void AutofillDialogViews::UpdateButtonStrip() {
button_strip_extra_view_->SetVisible(
GetDialogButtons() != ui::DIALOG_BUTTON_NONE);
UpdateSaveInChromeCheckbox();
autocheckout_progress_bar_view_->SetVisible(
controller_->ShouldShowProgressBar());
GetDialogClientView()->UpdateDialogButtons();
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::UpdateDetailArea() {
details_container_->SetVisible(controller_->ShouldShowDetailArea());
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::UpdateNotificationArea() {
DCHECK(notification_area_);
notification_area_->SetNotifications(controller_->CurrentNotifications());
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::UpdateSection(DialogSection section) {
UpdateSectionImpl(section, true);
}
void AutofillDialogViews::FillSection(DialogSection section,
const DetailInput& originating_input) {
DetailsGroup* group = GroupForSection(section);
// Make sure to overwrite the originating input.
TextfieldMap::iterator text_mapping =
group->textfields.find(&originating_input);
if (text_mapping != group->textfields.end())
text_mapping->second->textfield()->SetText(string16());
// If the Autofill data comes from a credit card, make sure to overwrite the
// CC comboboxes (even if they already have something in them). If the
// Autofill data comes from an AutofillProfile, leave the comboboxes alone.
if ((section == SECTION_CC || section == SECTION_CC_BILLING) &&
AutofillType(originating_input.type).group() ==
AutofillType::CREDIT_CARD) {
for (ComboboxMap::const_iterator it = group->comboboxes.begin();
it != group->comboboxes.end(); ++it) {
if (AutofillType(it->first->type).group() == AutofillType::CREDIT_CARD)
it->second->SetSelectedIndex(it->second->model()->GetDefaultIndex());
}
}
UpdateSectionImpl(section, false);
}
void AutofillDialogViews::GetUserInput(DialogSection section,
DetailOutputMap* output) {
DetailsGroup* group = GroupForSection(section);
for (TextfieldMap::const_iterator it = group->textfields.begin();
it != group->textfields.end(); ++it) {
output->insert(std::make_pair(it->first, it->second->textfield()->text()));
}
for (ComboboxMap::const_iterator it = group->comboboxes.begin();
it != group->comboboxes.end(); ++it) {
output->insert(std::make_pair(it->first,
it->second->model()->GetItemAt(it->second->selected_index())));
}
}
string16 AutofillDialogViews::GetCvc() {
DialogSection billing_section = controller_->SectionIsActive(SECTION_CC) ?
SECTION_CC : SECTION_CC_BILLING;
return GroupForSection(billing_section)->suggested_info->
decorated_textfield()->textfield()->text();
}
bool AutofillDialogViews::SaveDetailsLocally() {
DCHECK(save_in_chrome_checkbox_->visible());
return save_in_chrome_checkbox_->checked();
}
const content::NavigationController* AutofillDialogViews::ShowSignIn() {
// TODO(abodenha) We should be able to use the WebContents of the WebView
// to navigate instead of LoadInitialURL. Figure out why it doesn't work.
sign_in_webview_->LoadInitialURL(wallet::GetSignInUrl());
main_container_->SetVisible(false);
sign_in_webview_->SetVisible(true);
UpdateButtonStrip();
ContentsPreferredSizeChanged();
return &sign_in_webview_->web_contents()->GetController();
}
void AutofillDialogViews::HideSignIn() {
sign_in_webview_->SetVisible(false);
main_container_->SetVisible(true);
UpdateButtonStrip();
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::UpdateProgressBar(double value) {
autocheckout_progress_bar_->SetValue(value);
}
void AutofillDialogViews::ModelChanged() {
menu_runner_.reset();
for (DetailGroupMap::const_iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
UpdateDetailsGroupState(iter->second);
}
}
TestableAutofillDialogView* AutofillDialogViews::GetTestableView() {
return this;
}
void AutofillDialogViews::OnSignInResize(const gfx::Size& pref_size) {
sign_in_webview_->SetPreferredSize(pref_size);
ContentsPreferredSizeChanged();
}
void AutofillDialogViews::SubmitForTesting() {
Accept();
}
void AutofillDialogViews::CancelForTesting() {
if (Cancel())
Hide();
}
string16 AutofillDialogViews::GetTextContentsOfInput(const DetailInput& input) {
views::Textfield* textfield = TextfieldForInput(input);
if (textfield)
return textfield->text();
views::Combobox* combobox = ComboboxForInput(input);
if (combobox)
return combobox->model()->GetItemAt(combobox->selected_index());
NOTREACHED();
return string16();
}
void AutofillDialogViews::SetTextContentsOfInput(const DetailInput& input,
const string16& contents) {
views::Textfield* textfield = TextfieldForInput(input);
if (textfield) {
TextfieldForInput(input)->SetText(contents);
return;
}
views::Combobox* combobox = ComboboxForInput(input);
if (combobox) {
for (int i = 0; i < combobox->model()->GetItemCount(); ++i) {
if (contents == combobox->model()->GetItemAt(i)) {
combobox->SetSelectedIndex(i);
return;
}
}
// If we don't find a match, return the combobox to its default state.
combobox->SetSelectedIndex(combobox->model()->GetDefaultIndex());
return;
}
NOTREACHED();
}
void AutofillDialogViews::ActivateInput(const DetailInput& input) {
TextfieldEditedOrActivated(TextfieldForInput(input), false);
}
gfx::Size AutofillDialogViews::GetSize() const {
return GetWidget() ? GetWidget()->GetRootView()->size() : gfx::Size();
}
bool AutofillDialogViews::AcceleratorPressed(
const ui::Accelerator& accelerator) {
ui::KeyboardCode key = accelerator.key_code();
if (key == ui::VKEY_BACK)
return true;
return false;
}
bool AutofillDialogViews::CanHandleAccelerators() const {
return true;
}
string16 AutofillDialogViews::GetWindowTitle() const {
return controller_->DialogTitle();
}
void AutofillDialogViews::WindowClosing() {
focus_manager_->RemoveFocusChangeListener(this);
}
void AutofillDialogViews::DeleteDelegate() {
window_ = NULL;
// |this| belongs to |controller_|.
controller_->ViewClosed();
}
views::Widget* AutofillDialogViews::GetWidget() {
return contents_->GetWidget();
}
const views::Widget* AutofillDialogViews::GetWidget() const {
return contents_->GetWidget();
}
views::View* AutofillDialogViews::GetContentsView() {
return contents_;
}
int AutofillDialogViews::GetDialogButtons() const {
if (sign_in_webview_->visible())
return ui::DIALOG_BUTTON_NONE;
return controller_->GetDialogButtons();
}
string16 AutofillDialogViews::GetDialogButtonLabel(ui::DialogButton button)
const {
return button == ui::DIALOG_BUTTON_OK ?
controller_->ConfirmButtonText() : controller_->CancelButtonText();
}
bool AutofillDialogViews::IsDialogButtonEnabled(ui::DialogButton button) const {
return controller_->IsDialogButtonEnabled(button);
}
views::View* AutofillDialogViews::CreateExtraView() {
return button_strip_extra_view_;
}
views::View* AutofillDialogViews::CreateTitlebarExtraView() {
return account_chooser_;
}
views::View* AutofillDialogViews::CreateFootnoteView() {
footnote_view_ = new views::View();
footnote_view_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical,
kLegalDocPadding,
kLegalDocPadding,
0));
footnote_view_->set_border(
views::Border::CreateSolidSidedBorder(1, 0, 0, 0, kSubtleBorderColor));
footnote_view_->set_background(
views::Background::CreateSolidBackground(kShadingColor));
legal_document_view_ = new views::StyledLabel(string16(), this);
footnote_view_->AddChildView(legal_document_view_);
footnote_view_->SetVisible(false);
return footnote_view_;
}
bool AutofillDialogViews::Cancel() {
controller_->OnCancel();
return true;
}
bool AutofillDialogViews::Accept() {
if (ValidateForm())
controller_->OnAccept();
else if (!validity_map_.empty())
validity_map_.begin()->first->RequestFocus();
// |controller_| decides when to hide the dialog.
return false;
}
// TODO(wittman): Remove this override once we move to the new style frame view
// on all dialogs.
views::NonClientFrameView* AutofillDialogViews::CreateNonClientFrameView(
views::Widget* widget) {
return CreateConstrainedStyleNonClientFrameView(
widget,
controller_->web_contents()->GetBrowserContext());
}
void AutofillDialogViews::ButtonPressed(views::Button* sender,
const ui::Event& event) {
// TODO(estade): Should the menu be shown on mouse down?
DetailsGroup* group = NULL;
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
if (sender == iter->second.suggested_button) {
group = &iter->second;
break;
}
}
DCHECK(group);
if (!group->suggested_button->visible())
return;
menu_runner_.reset(new views::MenuRunner(
controller_->MenuModelForSection(group->section)));
group->container->SetActive(true);
views::Button::ButtonState state = group->suggested_button->state();
group->suggested_button->SetState(views::Button::STATE_PRESSED);
// Ignore the result since we don't need to handle a deleted menu specially.
gfx::Rect bounds = group->suggested_button->GetBoundsInScreen();
bounds.Inset(group->suggested_button->GetInsets());
ignore_result(
menu_runner_->RunMenuAt(sender->GetWidget(),
NULL,
bounds,
views::MenuItemView::TOPRIGHT,
0));
group->container->SetActive(false);
group->suggested_button->SetState(state);
}
void AutofillDialogViews::ContentsChanged(views::Textfield* sender,
const string16& new_contents) {
TextfieldEditedOrActivated(sender, true);
}
bool AutofillDialogViews::HandleKeyEvent(views::Textfield* sender,
const ui::KeyEvent& key_event) {
scoped_ptr<ui::KeyEvent> copy(key_event.Copy());
#if defined(OS_WIN) && !defined(USE_AURA)
content::NativeWebKeyboardEvent event(copy->native_event());
#else
content::NativeWebKeyboardEvent event(copy.get());
#endif
return controller_->HandleKeyPressEventInInput(event);
}
bool AutofillDialogViews::HandleMouseEvent(views::Textfield* sender,
const ui::MouseEvent& mouse_event) {
if (mouse_event.IsLeftMouseButton() && sender->HasFocus()) {
TextfieldEditedOrActivated(sender, false);
// Show an error bubble if a user clicks on an input that's already focused
// (and invalid).
ShowErrorBubbleForViewIfNecessary(sender);
}
return false;
}
void AutofillDialogViews::OnWillChangeFocus(
views::View* focused_before,
views::View* focused_now) {
controller_->FocusMoved();
error_bubble_.reset();
}
void AutofillDialogViews::OnDidChangeFocus(
views::View* focused_before,
views::View* focused_now) {
// If user leaves an edit-field, revalidate the group it belongs to.
if (focused_before) {
DetailsGroup* group = GroupForView(focused_before);
if (group && group->container->visible())
ValidateGroup(*group, VALIDATE_EDIT);
}
// Show an error bubble when the user focuses the input.
if (focused_now)
ShowErrorBubbleForViewIfNecessary(focused_now);
}
void AutofillDialogViews::LinkClicked(views::Link* source, int event_flags) {
// Edit links.
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
if (iter->second.suggested_info->Contains(source)) {
controller_->EditClickedForSection(iter->first);
return;
}
}
}
void AutofillDialogViews::OnSelectedIndexChanged(views::Combobox* combobox) {
DetailsGroup* group = GroupForView(combobox);
ValidateGroup(*group, VALIDATE_EDIT);
}
void AutofillDialogViews::StyledLabelLinkClicked(const ui::Range& range,
int event_flags) {
controller_->LegalDocumentLinkClicked(range);
}
void AutofillDialogViews::InitChildViews() {
button_strip_extra_view_ = new ButtonStripView();
button_strip_extra_view_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
save_in_chrome_checkbox_ =
new views::Checkbox(controller_->SaveLocallyText());
save_in_chrome_checkbox_->SetChecked(true);
button_strip_extra_view_->AddChildView(save_in_chrome_checkbox_);
autocheckout_progress_bar_view_ = new views::View();
autocheckout_progress_bar_view_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
views::Label* progress_bar_label = new views::Label();
progress_bar_label->SetText(controller_->ProgressBarText());
progress_bar_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
autocheckout_progress_bar_view_->AddChildView(progress_bar_label);
autocheckout_progress_bar_ = new AutocheckoutProgressBar();
autocheckout_progress_bar_view_->AddChildView(autocheckout_progress_bar_);
button_strip_extra_view_->AddChildView(autocheckout_progress_bar_view_);
autocheckout_progress_bar_view_->SetVisible(false);
contents_ = new views::View();
contents_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
contents_->AddChildView(CreateMainContainer());
sign_in_webview_ = new views::WebView(controller_->profile());
sign_in_webview_->SetVisible(false);
contents_->AddChildView(sign_in_webview_);
sign_in_delegate_.reset(
new AutofillDialogSignInDelegate(this,
sign_in_webview_->GetWebContents()));
}
views::View* AutofillDialogViews::CreateMainContainer() {
main_container_ = new views::View();
main_container_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0,
views::kRelatedControlVerticalSpacing));
account_chooser_ = new AccountChooser(controller_);
if (!views::DialogDelegate::UseNewStyle())
main_container_->AddChildView(account_chooser_);
notification_area_ = new NotificationArea(controller_);
notification_area_->set_arrow_centering_anchor(account_chooser_->AsWeakPtr());
main_container_->AddChildView(notification_area_);
scrollable_area_ = new SizeLimitedScrollView(CreateDetailsContainer());
main_container_->AddChildView(scrollable_area_);
return main_container_;
}
views::View* AutofillDialogViews::CreateDetailsContainer() {
details_container_ = new DetailsContainerView(
base::Bind(&AutofillDialogViews::DetailsContainerBoundsChanged,
base::Unretained(this)));
// A box layout is used because it respects widget visibility.
details_container_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
CreateDetailsSection(iter->second.section);
details_container_->AddChildView(iter->second.container);
}
return details_container_;
}
void AutofillDialogViews::CreateDetailsSection(DialogSection section) {
// Inputs container (manual inputs + combobox).
views::View* inputs_container = CreateInputsContainer(section);
DetailsGroup* group = GroupForSection(section);
// Container (holds label + inputs).
group->container = new SectionContainer(
controller_->LabelForSection(section),
inputs_container,
group->suggested_button);
UpdateDetailsGroupState(*group);
}
views::View* AutofillDialogViews::CreateInputsContainer(DialogSection section) {
views::View* inputs_container = new views::View();
views::GridLayout* layout = new views::GridLayout(inputs_container);
inputs_container->SetLayoutManager(layout);
int kColumnSetId = 0;
views::ColumnSet* column_set = layout->AddColumnSet(kColumnSetId);
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::LEADING,
1,
views::GridLayout::USE_PREF,
0,
0);
// A column for the menu button.
column_set->AddColumn(views::GridLayout::CENTER,
views::GridLayout::LEADING,
0,
views::GridLayout::USE_PREF,
0,
0);
layout->StartRow(0, kColumnSetId);
// The |info_view| holds |manual_inputs| and |suggested_info|, allowing the
// dialog to toggle which is shown.
views::View* info_view = new views::View();
info_view->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
views::View* manual_inputs = InitInputsView(section);
info_view->AddChildView(manual_inputs);
SuggestionView* suggested_info =
new SuggestionView(controller_->EditSuggestionText(), this);
info_view->AddChildView(suggested_info);
layout->AddView(info_view);
views::ImageButton* menu_button = new views::ImageButton(this);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
menu_button->SetImage(views::Button::STATE_NORMAL,
rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON));
menu_button->SetImage(views::Button::STATE_PRESSED,
rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON_P));
menu_button->SetImage(views::Button::STATE_HOVERED,
rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON_H));
menu_button->SetImage(views::Button::STATE_DISABLED,
rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON_D));
menu_button->set_border(views::Border::CreateEmptyBorder(
kMenuButtonTopOffset,
kMenuButtonHorizontalPadding,
0,
kMenuButtonHorizontalPadding));
layout->AddView(menu_button);
DetailsGroup* group = GroupForSection(section);
group->suggested_button = menu_button;
group->manual_input = manual_inputs;
group->suggested_info = suggested_info;
return inputs_container;
}
// TODO(estade): we should be using Chrome-style constrained window padding
// values.
views::View* AutofillDialogViews::InitInputsView(DialogSection section) {
const DetailInputs& inputs = controller_->RequestedFieldsForSection(section);
TextfieldMap* textfields = &GroupForSection(section)->textfields;
ComboboxMap* comboboxes = &GroupForSection(section)->comboboxes;
views::View* view = new views::View();
views::GridLayout* layout = new views::GridLayout(view);
view->SetLayoutManager(layout);
for (DetailInputs::const_iterator it = inputs.begin();
it != inputs.end(); ++it) {
const DetailInput& input = *it;
int kColumnSetId = input.row_id;
views::ColumnSet* column_set = layout->GetColumnSet(kColumnSetId);
if (!column_set) {
// Create a new column set and row.
column_set = layout->AddColumnSet(kColumnSetId);
if (it != inputs.begin())
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
layout->StartRow(0, kColumnSetId);
} else {
// Add a new column to existing row.
column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
// Must explicitly skip the padding column since we've already started
// adding views.
layout->SkipColumns(1);
}
float expand = input.expand_weight;
column_set->AddColumn(views::GridLayout::FILL,
views::GridLayout::BASELINE,
expand ? expand : 1,
views::GridLayout::USE_PREF,
0,
0);
ui::ComboboxModel* input_model =
controller_->ComboboxModelForAutofillType(input.type);
if (input_model) {
views::Combobox* combobox = new views::Combobox(input_model);
combobox->set_listener(this);
comboboxes->insert(std::make_pair(&input, combobox));
layout->AddView(combobox);
for (int i = 0; i < input_model->GetItemCount(); ++i) {
if (input.initial_value == input_model->GetItemAt(i)) {
combobox->SetSelectedIndex(i);
break;
}
}
} else {
DecoratedTextfield* field = new DecoratedTextfield(
input.initial_value,
l10n_util::GetStringUTF16(input.placeholder_text_rid),
this);
gfx::Image icon =
controller_->IconForField(input.type, input.initial_value);
field->textfield()->SetIcon(icon.AsImageSkia());
textfields->insert(std::make_pair(&input, field));
layout->AddView(field);
}
}
return view;
}
void AutofillDialogViews::UpdateSectionImpl(
DialogSection section,
bool clobber_inputs) {
const DetailInputs& updated_inputs =
controller_->RequestedFieldsForSection(section);
DetailsGroup* group = GroupForSection(section);
for (DetailInputs::const_iterator iter = updated_inputs.begin();
iter != updated_inputs.end(); ++iter) {
const DetailInput& input = *iter;
TextfieldMap::iterator text_mapping = group->textfields.find(&input);
if (text_mapping != group->textfields.end()) {
views::Textfield* textfield = text_mapping->second->textfield();
textfield->SetEnabled(input.editable);
if (textfield->text().empty() || clobber_inputs) {
textfield->SetText(iter->initial_value);
textfield->SetIcon(controller_->IconForField(
input.type, textfield->text()).AsImageSkia());
}
}
ComboboxMap::iterator combo_mapping = group->comboboxes.find(&input);
if (combo_mapping != group->comboboxes.end()) {
views::Combobox* combobox = combo_mapping->second;
combobox->SetEnabled(input.editable);
if (combobox->selected_index() == combobox->model()->GetDefaultIndex() ||
clobber_inputs) {
for (int i = 0; i < combobox->model()->GetItemCount(); ++i) {
if (input.initial_value == combobox->model()->GetItemAt(i)) {
combobox->SetSelectedIndex(i);
break;
}
}
}
}
}
UpdateDetailsGroupState(*group);
}
void AutofillDialogViews::UpdateDetailsGroupState(const DetailsGroup& group) {
const SuggestionState& suggestion_state =
controller_->SuggestionStateForSection(group.section);
bool show_suggestions = !suggestion_state.text.empty();
group.suggested_info->SetVisible(show_suggestions);
group.suggested_info->SetSuggestionText(suggestion_state.text,
suggestion_state.text_style);
group.suggested_info->SetSuggestionIcon(suggestion_state.icon);
group.suggested_info->SetEditable(suggestion_state.editable);
if (!suggestion_state.extra_text.empty()) {
group.suggested_info->ShowTextfield(
suggestion_state.extra_text,
suggestion_state.extra_icon.AsImageSkia());
}
group.manual_input->SetVisible(!show_suggestions);
// Show or hide the "Save in chrome" checkbox. If nothing is in editing mode,
// hide. If the controller tells us not to show it, likewise hide.
UpdateSaveInChromeCheckbox();
const bool has_menu = !!controller_->MenuModelForSection(group.section);
if (group.suggested_button)
group.suggested_button->SetVisible(has_menu);
if (group.container) {
group.container->SetForwardMouseEvents(has_menu && show_suggestions);
group.container->SetVisible(controller_->SectionIsActive(group.section));
if (group.container->visible())
ValidateGroup(group, VALIDATE_EDIT);
}
ContentsPreferredSizeChanged();
}
template<class T>
void AutofillDialogViews::SetValidityForInput(
T* input,
const string16& message) {
bool invalid = !message.empty();
input->SetInvalid(invalid);
if (invalid) {
validity_map_[input] = message;
} else {
validity_map_.erase(input);
if (error_bubble_ && error_bubble_->anchor() == input) {
validity_map_.erase(input);
error_bubble_.reset();
}
}
}
void AutofillDialogViews::ShowErrorBubbleForViewIfNecessary(views::View* view) {
if (!view->GetWidget())
return;
views::View* input =
view->GetAncestorWithClassName(kDecoratedTextfieldClassName);
if (!input)
input = view;
if (error_bubble_ && error_bubble_->anchor() == input)
return;
std::map<views::View*, string16>::iterator error_message =
validity_map_.find(input);
if (error_message != validity_map_.end())
error_bubble_.reset(new ErrorBubble(input, error_message->second));
}
bool AutofillDialogViews::ValidateGroup(const DetailsGroup& group,
ValidationType validation_type) {
DCHECK(group.container->visible());
scoped_ptr<DetailInput> cvc_input;
DetailOutputMap detail_outputs;
typedef std::map<AutofillFieldType, base::Callback<void(const string16&)> >
FieldMap;
FieldMap field_map;
if (group.manual_input->visible()) {
for (TextfieldMap::const_iterator iter = group.textfields.begin();
iter != group.textfields.end(); ++iter) {
if (!iter->first->editable)
continue;
detail_outputs[iter->first] = iter->second->textfield()->text();
field_map[iter->first->type] = base::Bind(
&AutofillDialogViews::SetValidityForInput<DecoratedTextfield>,
base::Unretained(this),
iter->second);
}
for (ComboboxMap::const_iterator iter = group.comboboxes.begin();
iter != group.comboboxes.end(); ++iter) {
if (!iter->first->editable)
continue;
views::Combobox* combobox = iter->second;
string16 item =
combobox->model()->GetItemAt(combobox->selected_index());
detail_outputs[iter->first] = item;
field_map[iter->first->type] = base::Bind(
&AutofillDialogViews::SetValidityForInput<views::Combobox>,
base::Unretained(this),
iter->second);
}
} else if (group.section == SECTION_CC) {
DecoratedTextfield* decorated_cvc =
group.suggested_info->decorated_textfield();
cvc_input.reset(new DetailInput);
cvc_input->type = CREDIT_CARD_VERIFICATION_CODE;
detail_outputs[cvc_input.get()] = decorated_cvc->textfield()->text();
field_map[cvc_input->type] = base::Bind(
&AutofillDialogViews::SetValidityForInput<DecoratedTextfield>,
base::Unretained(this),
decorated_cvc);
}
ValidityData invalid_inputs;
invalid_inputs = controller_->InputsAreValid(detail_outputs, validation_type);
// Flag invalid fields, removing them from |field_map|.
for (ValidityData::const_iterator iter =
invalid_inputs.begin(); iter != invalid_inputs.end(); ++iter) {
const string16& message = iter->second;
field_map[iter->first].Run(message);
field_map.erase(iter->first);
}
// The remaining fields in |field_map| are valid. Mark them as such.
for (FieldMap::iterator iter = field_map.begin(); iter != field_map.end();
++iter) {
iter->second.Run(string16());
}
return invalid_inputs.empty();
}
bool AutofillDialogViews::ValidateForm() {
bool all_valid = true;
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
const DetailsGroup& group = iter->second;
if (!group.container->visible())
continue;
if (!ValidateGroup(group, VALIDATE_FINAL))
all_valid = false;
}
return all_valid;
}
void AutofillDialogViews::TextfieldEditedOrActivated(
views::Textfield* textfield,
bool was_edit) {
DetailsGroup* group = GroupForView(textfield);
DCHECK(group);
// Figure out the AutofillFieldType this textfield represents.
AutofillFieldType type = UNKNOWN_TYPE;
DecoratedTextfield* decorated = NULL;
// Look for the input in the manual inputs.
views::View* ancestor =
textfield->GetAncestorWithClassName(kDecoratedTextfieldClassName);
for (TextfieldMap::const_iterator iter = group->textfields.begin();
iter != group->textfields.end();
++iter) {
decorated = iter->second;
if (decorated == ancestor) {
controller_->UserEditedOrActivatedInput(iter->first,
GetWidget()->GetNativeView(),
textfield->GetBoundsInScreen(),
textfield->text(),
was_edit);
type = iter->first->type;
break;
}
}
if (ancestor == group->suggested_info->decorated_textfield()) {
decorated = group->suggested_info->decorated_textfield();
type = CREDIT_CARD_VERIFICATION_CODE;
}
DCHECK_NE(UNKNOWN_TYPE, type);
// If the field is marked as invalid, check if the text is now valid.
// Many fields (i.e. CC#) are invalid for most of the duration of editing,
// so flagging them as invalid prematurely is not helpful. However,
// correcting a minor mistake (i.e. a wrong CC digit) should immediately
// result in validation - positive user feedback.
if (decorated->invalid() && was_edit) {
SetValidityForInput<DecoratedTextfield>(
decorated,
controller_->InputValidityMessage(type, textfield->text()));
// If the field transitioned from invalid to valid, re-validate the group,
// since inter-field checks become meaningful with valid fields.
if (!decorated->invalid())
ValidateGroup(*group, VALIDATE_EDIT);
}
gfx::Image icon = controller_->IconForField(type, textfield->text());
textfield->SetIcon(icon.AsImageSkia());
}
void AutofillDialogViews::UpdateSaveInChromeCheckbox() {
save_in_chrome_checkbox_->SetVisible(
controller_->ShouldOfferToSaveInChrome());
}
void AutofillDialogViews::ContentsPreferredSizeChanged() {
if (GetWidget()) {
GetWidget()->SetSize(GetWidget()->non_client_view()->GetPreferredSize());
// If the above line does not cause the dialog's size to change, |contents_|
// may not be laid out. This will trigger a layout only if it's needed.
contents_->SetBoundsRect(contents_->bounds());
if (error_bubble_)
error_bubble_->UpdatePosition();
}
}
AutofillDialogViews::DetailsGroup* AutofillDialogViews::GroupForSection(
DialogSection section) {
return &detail_groups_.find(section)->second;
}
AutofillDialogViews::DetailsGroup* AutofillDialogViews::GroupForView(
views::View* view) {
DCHECK(view);
// Textfields are treated slightly differently. For them, inspect
// the DecoratedTextfield ancestor, not the actual control.
views::View* ancestor =
view->GetAncestorWithClassName(kDecoratedTextfieldClassName);
views::View* control = ancestor ? ancestor : view;
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
DetailsGroup* group = &iter->second;
if (control->parent() == group->manual_input)
return group;
// Textfields need to check a second case, since they can be
// suggested inputs instead of directly editable inputs. Those are
// accessed via |suggested_info|.
if (ancestor &&
ancestor == group->suggested_info->decorated_textfield()) {
return group;
}
}
return NULL;
}
views::Textfield* AutofillDialogViews::TextfieldForInput(
const DetailInput& input) {
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
const DetailsGroup& group = iter->second;
TextfieldMap::const_iterator text_mapping = group.textfields.find(&input);
if (text_mapping != group.textfields.end())
return text_mapping->second->textfield();
}
return NULL;
}
views::Combobox* AutofillDialogViews::ComboboxForInput(
const DetailInput& input) {
for (DetailGroupMap::iterator iter = detail_groups_.begin();
iter != detail_groups_.end(); ++iter) {
const DetailsGroup& group = iter->second;
ComboboxMap::const_iterator combo_mapping = group.comboboxes.find(&input);
if (combo_mapping != group.comboboxes.end())
return combo_mapping->second;
}
return NULL;
}
void AutofillDialogViews::DetailsContainerBoundsChanged() {
if (error_bubble_)
error_bubble_->UpdatePosition();
}
AutofillDialogViews::DetailsGroup::DetailsGroup(DialogSection section)
: section(section),
container(NULL),
manual_input(NULL),
suggested_info(NULL),
suggested_button(NULL) {}
AutofillDialogViews::DetailsGroup::~DetailsGroup() {}
} // namespace autofill
|