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
 1846
 1847
 1848
 1849
 1850
 1851
 1852
 1853
 1854
 1855
 1856
 1857
 1858
 1859
 1860
 1861
 1862
 1863
 1864
 1865
 1866
 1867
 1868
 1869
 1870
 1871
 1872
 1873
 1874
 1875
 1876
 1877
 1878
 1879
 1880
 1881
 1882
 1883
 1884
 1885
 1886
 1887
 1888
 1889
 1890
 1891
 1892
 1893
 1894
 1895
 1896
 1897
 1898
 1899
 1900
 1901
 1902
 1903
 1904
 1905
 1906
 1907
 1908
 1909
 1910
 1911
 1912
 1913
 1914
 1915
 1916
 1917
 1918
 1919
 1920
 1921
 1922
 1923
 1924
 1925
 1926
 1927
 1928
 1929
 1930
 1931
 1932
 1933
 1934
 1935
 1936
 1937
 1938
 1939
 1940
 1941
 1942
 1943
 1944
 1945
 1946
 1947
 1948
 1949
 1950
 1951
 1952
 1953
 1954
 1955
 1956
 1957
 1958
 1959
 1960
 1961
 1962
 1963
 1964
 1965
 1966
 1967
 1968
 1969
 1970
 1971
 1972
 1973
 1974
 1975
 1976
 1977
 1978
 1979
 1980
 1981
 1982
 1983
 1984
 1985
 1986
 1987
 1988
 1989
 1990
 1991
 1992
 1993
 1994
 1995
 1996
 1997
 1998
 1999
 2000
 2001
 2002
 2003
 2004
 2005
 2006
 2007
 2008
 2009
 2010
 2011
 2012
 2013
 2014
 2015
 2016
 2017
 2018
 2019
 2020
 2021
 2022
 2023
 2024
 2025
 2026
 2027
 2028
 2029
 2030
 2031
 2032
 2033
 2034
 2035
 2036
 2037
 2038
 2039
 2040
 2041
 2042
 2043
 2044
 2045
 2046
 2047
 2048
 2049
 2050
 2051
 2052
 2053
 2054
 2055
 2056
 2057
 2058
 2059
 2060
 2061
 2062
 2063
 2064
 2065
 2066
 2067
 2068
 2069
 2070
 2071
 2072
 2073
 2074
 2075
 2076
 2077
 2078
 2079
 2080
 2081
 2082
 2083
 2084
 2085
 2086
 2087
 2088
 2089
 2090
 2091
 2092
 2093
 2094
 2095
 2096
 2097
 2098
 2099
 2100
 2101
 2102
 2103
 2104
 2105
 2106
 2107
 2108
 2109
 2110
 2111
 2112
 2113
 2114
 2115
 2116
 2117
 2118
 2119
 2120
 2121
 2122
 2123
 2124
 2125
 2126
 2127
 2128

ash / system / notification_center / views / ash_notification_view.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/notification_center/views/ash_notification_view.h"

#include <memory>
#include <utility>

#include "ash/constants/ash_features.h"
#include "ash/drag_drop/drag_drop_util.h"
#include "ash/public/cpp/metrics_util.h"
#include "ash/public/cpp/rounded_image_view.h"
#include "ash/public/cpp/style/color_provider.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/style/icon_button.h"
#include "ash/style/pill_button.h"
#include "ash/style/typography.h"
#include "ash/system/notification_center/ash_notification_control_button_factory.h"
#include "ash/system/notification_center/ash_notification_drag_controller.h"
#include "ash/system/notification_center/message_center_constants.h"
#include "ash/system/notification_center/message_center_controller.h"
#include "ash/system/notification_center/message_center_style.h"
#include "ash/system/notification_center/message_center_utils.h"
#include "ash/system/notification_center/message_view_factory.h"
#include "ash/system/notification_center/metrics_utils.h"
#include "ash/system/notification_center/notification_grouping_controller.h"
#include "ash/system/notification_center/notification_style_utils.h"
#include "ash/system/notification_center/views/ash_notification_expand_button.h"
#include "ash/system/notification_center/views/ash_notification_input_container.h"
#include "ash/system/notification_center/views/timestamp_view.h"
#include "ash/wm/work_area_insets.h"
#include "base/base64.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/animation_throughput_reporter.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/font.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/shadow_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/gfx/text_elider.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/notification_view_controller.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/vector_icons.h"
#include "ui/message_center/views/large_image_view.h"
#include "ui/message_center/views/notification_control_buttons_view.h"
#include "ui/message_center/views/notification_header_view.h"
#include "ui/message_center/views/notification_view_base.h"
#include "ui/message_center/views/notification_view_util.h"
#include "ui/message_center/views/proportional_image_view.h"
#include "ui/message_center/views/relative_time_formatter.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/progress_bar.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/drag_utils.h"
#include "ui/views/highlight_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/metadata/view_factory_internal.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"

namespace {

// Used when encoding a notification drop image into binary data. The drop image
// should be resized if its binary size exceeds this limit.
// Use 1 MB as the size limit. On a 256 color image, each pixel takes four bytes
// (RGB + Alpha). The size limit of 1 MB means the maximum pixel count being
// 250K, which should be enough for most notification images without file
// backing.
constexpr size_t kMaxImageSizeInByte = 1000000;

constexpr int kMainRightViewVerticalSpacing = 4;

// This padding is applied to all the children of `main_right_view_` except the
// action buttons.
constexpr auto kMainRightViewChildPadding = gfx::Insets::TLBR(0, 14, 0, 0);

constexpr auto kImageContainerPadding = gfx::Insets::TLBR(12, 14, 12, 12);

constexpr auto kActionButtonsRowPadding = gfx::Insets::TLBR(4, 38, 12, 4);
constexpr int kActionsRowHorizontalSpacing = 8;

constexpr auto kContentRowPadding = gfx::Insets::TLBR(14, 0, 0, 0);

constexpr int kLeftContentVerticalSpacing = 4;
constexpr int kTitleRowMinimumWidthWithIcon = 186;
constexpr int kTitleRowMinimumWidth = 266;
constexpr int kTitleRowSpacing = 6;

// This padding is applied in collapsed mode when there is no message in order
// to vertically center the title.
constexpr auto kTitleRowNoMessageCollapsedPadding =
    gfx::Insets::TLBR(12, 0, 0, 0);

constexpr auto kHeaderRowExpandedPadding = gfx::Insets::TLBR(6, 0, 8, 0);
constexpr auto kHeaderRowCollapsedPadding = gfx::Insets::TLBR(0, 0, 8, 0);
constexpr auto kRightContentCollapsedPadding = gfx::Insets::TLBR(12, 16, 0, 0);
constexpr auto kRightContentExpandedPadding = gfx::Insets::TLBR(20, 16, 0, 0);
constexpr auto kTimeStampInCollapsedStatePadding =
    gfx::Insets::TLBR(0, 0, 0, 16);

constexpr char kGoogleSansFont[] = "Google Sans";

constexpr int kTitleLabelExpandedMaxLines = 2;
constexpr int kTitleLabelCollapsedMaxLines = 1;

// The size for `icon_view_`, which is the icon within right content (between
// title/message view and expand button).
constexpr int kIconViewSize = 48;

// The size for `icon_view_` with RenderArcNotificationsInChrome enabled. UX
// requires reducing the size of the icons but changing it without this feature
// would require updating it for both ash and arc notifications.
constexpr int kIconViewSizeRenderArcInChromeEnabled = 36;

// If the image displayed in `icon_view()` is smaller in either width or height
// than this value, we draw a background around the image.
constexpr int kSmallImageBackgroundThreshold = 6;

// The size of an icon within a control button. Note that this is not the size
// of a control button itself.
constexpr int kControlButtonsIconSize = 14;

int GetTitleCharacterLimit() {
  return message_center::GetNotificationWidth() *
         message_center::kMaxTitleLines /
         message_center::kMinPixelsPerTitleCharacter;
}

// Helpers ---------------------------------------------------------------------

// Create a view that will contain the `content_row`,
// `message_label_in_expanded_state_`, inline settings and the large image.
views::Builder<views::View> CreateMainRightViewBuilder() {
  auto layout_manager = std::make_unique<views::FlexLayout>();
  layout_manager
      ->SetDefault(views::kMarginsKey,
                   gfx::Insets::TLBR(0, 0, kMainRightViewVerticalSpacing, 0))
      .SetOrientation(views::LayoutOrientation::kVertical);

  return views::Builder<views::View>()
      .SetID(message_center::NotificationViewBase::ViewId::kMainRightView)
      .SetLayoutManager(std::move(layout_manager))
      .SetProperty(
          views::kFlexBehaviorKey,
          views::FlexSpecification(views::LayoutOrientation::kHorizontal,
                                   views::MinimumFlexSizeRule::kScaleToZero,
                                   views::MaximumFlexSizeRule::kUnbounded));
}

// Create a view containing the title and message for the notification in a
// single line. This is used when a grouped child notification is in a
// collapsed parent notification.
views::Builder<views::BoxLayoutView> CreateCollapsedSummaryBuilder(
    const message_center::Notification& notification) {
  return views::Builder<views::BoxLayoutView>()
      .SetID(
          message_center::NotificationViewBase::ViewId::kCollapsedSummaryView)
      .SetInsideBorderInsets(ash::kGroupedCollapsedSummaryInsets)
      .SetBetweenChildSpacing(ash::kGroupedCollapsedSummaryLabelSpacing)
      .SetOrientation(views::BoxLayout::Orientation::kHorizontal)
      .SetVisible(false)
      .AddChild(
          views::Builder<views::Label>()
              .SetText(notification.title())
              .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
              .SetFontList(gfx::FontList({kGoogleSansFont}, gfx::Font::NORMAL,
                                         ash::kNotificationTitleLabelSize,
                                         gfx::Font::Weight::MEDIUM)))
      .AddChild(
          views::Builder<views::Label>()
              .SetText(notification.message())
              .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
              .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
              .SetTextStyle(views::style::STYLE_SECONDARY)
              .SetFontList(gfx::FontList({kGoogleSansFont}, gfx::Font::NORMAL,
                                         ash::kNotificationMessageLabelSize,
                                         gfx::Font::Weight::NORMAL)));
}

views::Builder<ash::AshNotificationView::GroupedNotificationsContainer>
CreateGroupedNotificationsContainerBuilder(
    ash::AshNotificationView* parent_notification_view) {
  return views::Builder<
             ash::AshNotificationView::GroupedNotificationsContainer>()
      .SetParentNotificationView(parent_notification_view)
      .SetOrientation(views::BoxLayout::Orientation::kVertical);
}

// Perform a scale and translate animation by scale from (scale_value_x,
// scalue_value_y) and translate from (translate_value_x, translate_value_y) to
// its correct scale and position.
void ScaleAndTranslateView(views::View* view,
                           SkScalar scale_value_x,
                           SkScalar scale_value_y,
                           SkScalar translate_value_x,
                           SkScalar translate_value_y,
                           const std::string& animation_histogram_name) {
  gfx::Transform transform;
  transform.Translate(translate_value_x, translate_value_y);
  transform.Scale(scale_value_x, scale_value_y);

  ui::AnimationThroughputReporter reporter(
      view->layer()->GetAnimator(),
      ash::metrics_util::ForSmoothnessV3(base::BindRepeating(
          [](const std::string& animation_histogram_name, int smoothness) {
            base::UmaHistogramPercentage(animation_histogram_name, smoothness);
          },
          animation_histogram_name)));

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .SetDuration(base::TimeDelta())
      .SetTransform(view, transform)
      .Then()
      .SetDuration(
          base::Milliseconds(ash::kLargeImageScaleAndTranslateDurationMs))
      .SetTransform(view, gfx::Transform(), gfx::Tween::ACCEL_0_100_DECEL_80);
}

// Returns the HTML snippet that contains the binary data of `bitmap`. Returns
// `std::nullopt` if having any error.
std::optional<std::u16string> GetHtmlForBitmap(const SkBitmap& bitmap) {
  std::optional<std::vector<uint8_t>> image_data =
      gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, /*discard_transparency=*/false);
  if (image_data) {
    std::string encoded_data = base::Base64Encode(image_data.value());
    const std::string html = base::StrCat(
        {"<img src=\"data:image/png;base64,", encoded_data, "\"/>"});
    std::u16string html_in_u16;
    if (base::UTF8ToUTF16(html.c_str(), html.size(), &html_in_u16)) {
      return html_in_u16;
    }
  }
  return std::nullopt;
}

}  // namespace

namespace ash {

using CrossAxisAlignment = views::BoxLayout::CrossAxisAlignment;
using MainAxisAlignment = views::BoxLayout::MainAxisAlignment;
using Orientation = views::BoxLayout::Orientation;

BEGIN_METADATA(AshNotificationView, NotificationTitleRow)
END_METADATA

void AshNotificationView::AddedToWidget() {
  MessageView::AddedToWidget();

  // crbug/1337661: We need to abort animations in a grouped parent view when
  // it's widget is being destroyed. By default when a widget is destroyed, all
  // current animations are forced to finish. The grouped notification removal
  // animation triggers an additional resize animation when it is finished. This
  // needs to be aborted explicitly to prevent a crash. We do not need to this
  // observation for grouped notification views.
  if (!is_grouped_child_view_) {
    widget_observation_.Observe(GetWidget());
  }
}

void AshNotificationView::Layout(PassKey) {
  if (is_animating_) {
    return;
  }

  LayoutSuperclass<message_center::NotificationViewBase>(this);
}

void AshNotificationView::GroupedNotificationsContainer::
    ChildPreferredSizeChanged(views::View* view) {
  PreferredSizeChanged();
  parent_notification_view_->GroupedNotificationsPreferredSizeChanged();
}

void AshNotificationView::GroupedNotificationsContainer::
    SetParentNotificationView(AshNotificationView* parent_notification_view) {
  parent_notification_view_ = parent_notification_view;
}

BEGIN_METADATA(AshNotificationView, GroupedNotificationsContainer)
END_METADATA

AshNotificationView::NotificationTitleRow::NotificationTitleRow(
    const std::u16string& title)
    : title_view_(AddChildView(GenerateTitleView(title))),
      title_row_divider_(AddChildView(std::make_unique<views::Label>(
          kNotificationTitleRowDivider,
          views::style::CONTEXT_DIALOG_BODY_TEXT))),
      timestamp_in_collapsed_view_(
          AddChildView(std::make_unique<TimestampView>())) {
  SetLayoutManager(std::make_unique<views::TableLayout>())
      ->AddColumn(views::LayoutAlignment::kStart,
                  views::LayoutAlignment::kCenter, 1.0,
                  views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
      .AddPaddingColumn(views::TableLayout::kFixedSize, kTitleRowSpacing)
      .AddColumn(views::LayoutAlignment::kStart,
                 views::LayoutAlignment::kCenter,
                 views::TableLayout::kFixedSize,
                 views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
      .AddPaddingColumn(views::TableLayout::kFixedSize, kTitleRowSpacing)
      .AddColumn(views::LayoutAlignment::kStart,
                 views::LayoutAlignment::kCenter, 100.0,
                 views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
      .AddRows(1, views::TableLayout::kFixedSize);

  timestamp_in_collapsed_view_->SetProperty(views::kMarginsKey,
                                            kTimeStampInCollapsedStatePadding);
  timestamp_in_collapsed_view_->SetElideBehavior(gfx::ElideBehavior::NO_ELIDE);

  title_view_->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
                               views::MaximumFlexSizeRule::kPreferred));
  title_view_->SetMultiLine(true);
  title_view_->SetAllowCharacterBreak(true);
  title_view_->SetMaxLines(kTitleLabelExpandedMaxLines);

  notification_style_utils::ConfigureLabelStyle(title_row_divider_,
                                                kNotificationSecondaryLabelSize,
                                                /*is_color_primary=*/false);
  message_center_utils::InitLayerForAnimations(title_row_divider_);
  notification_style_utils::ConfigureLabelStyle(timestamp_in_collapsed_view_,
                                                kNotificationSecondaryLabelSize,
                                                /*is_color_primary=*/false);
  message_center_utils::InitLayerForAnimations(timestamp_in_collapsed_view_);
  notification_style_utils::ConfigureLabelStyle(
      title_view_, kNotificationTitleLabelSize,
      /*is_color_primary=*/true, gfx::Font::Weight::MEDIUM);

  ash::TypographyProvider::Get()->StyleLabel(ash::TypographyToken::kCrosButton2,
                                             *title_view_);
  title_view_->SetEnabledColorId(cros_tokens::kCrosSysOnSurface);

  timestamp_in_collapsed_view_->SetEnabledColorId(
      cros_tokens::kCrosSysOnSurfaceVariant);
  ash::TypographyProvider::Get()->StyleLabel(
      ash::TypographyToken::kCrosAnnotation1, *timestamp_in_collapsed_view_);
}

AshNotificationView::NotificationTitleRow::~NotificationTitleRow() = default;

void AshNotificationView::NotificationTitleRow::UpdateTitle(
    const std::u16string& title) {
  title_view_->SetText(title);
}

void AshNotificationView::NotificationTitleRow::UpdateTimestamp(
    base::Time timestamp) {
  timestamp_in_collapsed_view_->SetTimestamp(timestamp);
}

void AshNotificationView::NotificationTitleRow::UpdateVisibility(
    bool in_collapsed_mode) {
  timestamp_in_collapsed_view_->SetVisible(in_collapsed_mode);
  title_row_divider_->SetVisible(in_collapsed_mode);
}

void AshNotificationView::NotificationTitleRow::
    PerformExpandCollapseAnimation() {
  if (title_row_divider_->GetVisible()) {
    message_center_utils::FadeInView(
        title_row_divider_, kTitleRowTimestampFadeInAnimationDelayMs,
        kTitleRowTimestampFadeInAnimationDurationMs,
        gfx::Tween::ACCEL_20_DECEL_100,
        "Ash.NotificationView.TitleRowDivider.FadeIn.AnimationSmoothness");
    DCHECK(timestamp_in_collapsed_view_->GetVisible());
    message_center_utils::FadeInView(
        timestamp_in_collapsed_view_, kTitleRowTimestampFadeInAnimationDelayMs,
        kTitleRowTimestampFadeInAnimationDurationMs,
        gfx::Tween::ACCEL_20_DECEL_100,
        "Ash.NotificationView.TimestampInTitle.FadeIn.AnimationSmoothness");
  }
}

void AshNotificationView::NotificationTitleRow::SetMaxAvailableWidth(
    int max_available_width) {
  max_available_width_ = max_available_width;
}

gfx::Size AshNotificationView::NotificationTitleRow::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  // TODO(crbug.com/40233803): The size constraint is not passed down from the
  // views tree in the first round of layout, so setting a fixed width to bound
  // the view. The layout manager can size the view beyond this width if there
  // is available space. This works similar to applying a max width on the
  // internal labels.
  return gfx::Size(max_available_width_,
                   GetLayoutManager()->GetPreferredHeightForWidth(
                       this, max_available_width_));
}

void AshNotificationView::NotificationTitleRow::OnThemeChanged() {
  views::View::OnThemeChanged();

  title_view_->SetEnabledColorId(cros_tokens::kCrosSysOnSurface);
  title_row_divider_->SetEnabledColorId(cros_tokens::kCrosSysOnSurfaceVariant);
  timestamp_in_collapsed_view_->SetEnabledColorId(
      cros_tokens::kCrosSysOnSurfaceVariant);
}

AshNotificationView::AshNotificationView(
    const message_center::Notification& notification,
    bool shown_in_popup)
    : NotificationViewBase(notification),
      is_grouped_parent_view_(notification.group_parent()),
      is_grouped_child_view_(notification.group_child()),
      shown_in_popup_(shown_in_popup) {
  if (features::IsNotificationImageDragEnabled()) {
    set_drag_controller(
        Shell::Get()->message_center_controller()->drag_controller());
  }

  message_center_observer_.Observe(message_center::MessageCenter::Get());
  // TODO(crbug.com/40780100): fix views and layout to match spec.
  // Instantiate view instances and define layout and view hierarchy.
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical,
      notification.group_child() ? gfx::Insets() : kNotificationViewPadding));

  auto content_row_layout = std::make_unique<views::FlexLayout>();
  content_row_layout->SetInteriorMargin(kMainRightViewChildPadding);

  auto content_row_builder =
      CreateContentRowBuilder()
          .SetLayoutManager(std::move(content_row_layout))
          .AddChild(
              views::Builder<views::FlexLayoutView>()
                  .SetID(kHeaderLeftContent)
                  .SetOrientation(views::LayoutOrientation::kVertical)
                  .SetInteriorMargin(kContentRowPadding)
                  .SetProperty(views::kFlexBehaviorKey,
                               views::FlexSpecification(
                                   views::MinimumFlexSizeRule::kScaleToZero,
                                   views::MaximumFlexSizeRule::kUnbounded))
                  .AddChild(
                      CreateHeaderRowBuilder()
                          .SetIsInAshNotificationView(true)
                          .SetColor(
                              AshColorProvider::Get()->GetContentLayerColor(
                                  AshColorProvider::ContentLayerType::
                                      kTextColorSecondary)))
                  .AddChild(
                      CreateLeftContentBuilder()
                          .CopyAddressTo(&left_content_)
                          .SetBetweenChildSpacing(kLeftContentVerticalSpacing)))
          .AddChild(
              views::Builder<views::FlexLayoutView>()
                  .SetMainAxisAlignment(views::LayoutAlignment::kEnd)
                  .SetProperty(views::kFlexBehaviorKey,
                               views::FlexSpecification(
                                   views::MinimumFlexSizeRule::kPreferred,
                                   views::MaximumFlexSizeRule::kUnbounded))
                  .AddChild(CreateRightContentBuilder().SetProperty(
                      views::kCrossAxisAlignmentKey,
                      views::LayoutAlignment::kStart))
                  .AddChild(
                      views::Builder<views::FlexLayoutView>()
                          .SetOrientation(views::LayoutOrientation::kVertical)
                          .SetCrossAxisAlignment(views::LayoutAlignment::kEnd)
                          .SetMinimumCrossAxisSize(
                              kExpandAndControlButtonsContainerMinimumWidth)
                          .AddChild(
                              views::Builder<views::BoxLayoutView>()
                                  .SetMainAxisAlignment(MainAxisAlignment::kEnd)
                                  .SetMinimumCrossAxisSize(
                                      kControlButtonsContainerMinimumHeight)
                                  .AddChild(
                                      CreateControlButtonsBuilder()
                                          .CopyAddressTo(&control_buttons_view_)
                                          .SetBetweenButtonSpacing(
                                              kNotificationControlButtonsHorizontalSpacing)
                                          .SetCloseButtonIcon(
                                              kNotificationCloseControlButtonIcon)
                                          .SetSettingsButtonIcon(
                                              kNotificationSettingsControlButtonIcon)
                                          .SetButtonIconSize(
                                              kControlButtonsIconSize)
                                          .SetButtonIconColors(
                                              AshColorProvider::Get()
                                                  ->GetContentLayerColor(
                                                      AshColorProvider::
                                                          ContentLayerType::
                                                              kIconColorPrimary))
                                          .SetNotificationControlButtonFactory(
                                              std::make_unique<
                                                  AshNotificationControlButtonFactory>())))
                          .AddChild(
                              views::Builder<AshNotificationExpandButton>()
                                  .SetID(message_center::NotificationViewBase::
                                             kExpandButton)
                                  .CopyAddressTo(&expand_button_)
                                  .SetCallback(base::BindRepeating(
                                      &AshNotificationView::ToggleExpand,
                                      base::Unretained(this))))));
  // TODO(crbug.com/40232718): See View::SetLayoutManagerUseConstrainedSpace.
  content_row()->SetLayoutManagerUseConstrainedSpace(false);

  // Main right view contains all the views besides control buttons, app icon,
  // grouped container and action buttons.
  auto main_right_view_builder =
      CreateMainRightViewBuilder()
          .CopyAddressTo(&main_right_view_)
          .AddChild(content_row_builder)
          .AddChild(
              views::Builder<views::Label>()
                  .CopyAddressTo(&message_label_in_expanded_state_)
                  .SetHorizontalAlignment(gfx::ALIGN_TO_HEAD)
                  .SetMultiLine(true)
                  .SetMaxLines(message_center::kMaxLinesForExpandedMessageLabel)
                  .SetAllowCharacterBreak(true)
                  .SetBorder(
                      views::CreateEmptyBorder(kMainRightViewChildPadding))
                  // TODO(crbug.com/41295639): This is a workaround to that bug
                  // by explicitly setting the width. Ideally, we should fix the
                  // original bug, but it seems there's no obvious solution for
                  // the bug according to https://crbug.com/678337#c7. We will
                  // consider making changes to this code when the bug is fixed.
                  .SetMaximumWidth(GetExpandedMessageLabelWidth()))
          .AddChild(CreateInlineSettingsBuilder())
          .AddChild(CreateSnoozeSettingsBuilder())
          .AddChild(CreateImageContainerBuilder().SetProperty(
              views::kMarginsKey, kImageContainerPadding));

  notification_style_utils::ConfigureLabelStyle(
      message_label_in_expanded_state_, kNotificationMessageLabelSize,
      /*is_color_primary=*/false);

  message_label_in_expanded_state_->SetEnabledColorId(
      cros_tokens::kCrosSysOnSurfaceVariant);
  ash::TypographyProvider::Get()->StyleLabel(
      ash::TypographyToken::kCrosAnnotation1,
      *message_label_in_expanded_state_);

  AddChildView(
      views::Builder<views::FlexLayoutView>()
          .SetID(message_center::NotificationViewBase::ViewId::kMainView)
          .CopyAddressTo(&main_view_)
          .SetOrientation(views::LayoutOrientation::kHorizontal)
          .AddChild(views::Builder<views::BoxLayoutView>()
                        .SetID(kAppIconViewContainer)
                        .SetOrientation(Orientation::kVertical)
                        .SetMainAxisAlignment(MainAxisAlignment::kStart)
                        .SetCrossAxisAlignment(CrossAxisAlignment::kStart)
                        .AddChild(views::Builder<RoundedImageView>()
                                      .CopyAddressTo(&app_icon_view_)
                                      .SetCornerRadius(
                                          kNotificationAppIconViewSize / 2)))
          .AddChild(main_right_view_builder)
          .Build());

  AddChildView(CreateCollapsedSummaryBuilder(notification)
                   .CopyAddressTo(&collapsed_summary_view_)
                   .Build());

  // We only need a scroll view if the notification is being shown in its own
  // popup. Scrolling is handled by `UnifiedMessageCenterView` otherwise. Having
  // a nested scroll view results in crbug/1302756.
  if (shown_in_popup) {
    AddChildView(
        views::Builder<views::ScrollView>()
            .CopyAddressTo(&grouped_notifications_scroll_view_)
            .SetBackgroundColor(std::nullopt)
            .SetDrawOverflowIndicator(false)
            .ClipHeightTo(0, std::numeric_limits<int>::max())
            .SetContents(
                CreateGroupedNotificationsContainerBuilder(this).CopyAddressTo(
                    &grouped_notifications_container_))
            .Build());
    static_cast<views::BoxLayout*>(GetLayoutManager())
        ->SetFlexForView(grouped_notifications_scroll_view_, 1);
  } else {
    AddChildView(CreateGroupedNotificationsContainerBuilder(this)
                     .CopyAddressTo(&grouped_notifications_container_)
                     .Build());
  }

  AddChildView(CreateActionsRow(std::make_unique<views::FlexLayout>()));

  // Custom layout and paddings for views in `AshNotificationView`.
  // Note that we also change the padding for some particular views in
  // UpdateViewForExpandedState().
  action_buttons_row()
      ->SetLayoutManager(std::make_unique<views::FlexLayout>())
      ->SetDefault(views::kMarginsKey,
                   gfx::Insets::TLBR(0, 0, 0, kActionsRowHorizontalSpacing))
      .SetOrientation(views::LayoutOrientation::kHorizontal)
      .SetInteriorMargin(kActionButtonsRowPadding);
  action_buttons_row()->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
                               views::MaximumFlexSizeRule::kUnbounded));
  inline_reply()->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
                               views::MaximumFlexSizeRule::kUnbounded));

  static_cast<views::FlexLayout*>(header_row()->GetLayoutManager())
      ->SetDefault(views::kMarginsKey, gfx::Insets())
      .SetInteriorMargin(gfx::Insets());
  header_row()->ConfigureLabelsStyle(
      gfx::FontList({kGoogleSansFont}, gfx::Font::NORMAL, kHeaderViewLabelSize,
                    gfx::Font::Weight::NORMAL),
      /*text_view_padding=*/gfx::Insets(), /*auto_color_readability=*/false);

  // This view should not be focusable since it does not act as a button.
  header_row()->SetFocusBehavior(views::View::FocusBehavior::NEVER);

  views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);

  // Create layer in some views for animations.
  message_center_utils::InitLayerForAnimations(header_row());
  message_center_utils::InitLayerForAnimations(
      message_label_in_expanded_state_);
  message_center_utils::InitLayerForAnimations(actions_row());

  UpdateWithNotification(notification);
}

AshNotificationView::~AshNotificationView() {
  // b/330585555: We need to abort any in progress animations before we destroy
  // the views hierarchy to make sure there are no dangling pointers associated
  // with an animations' OnAborted callback.
  layer()->GetAnimator()->AbortAllAnimations();
}

void AshNotificationView::SetGroupedChildExpanded(bool expanded) {
  collapsed_summary_view_->SetVisible(!expanded);
  main_view_->SetVisible(expanded);
}

void AshNotificationView::GroupedNotificationsPreferredSizeChanged() {
  PreferredSizeChanged();
}

std::optional<gfx::Rect> AshNotificationView::GetDragAreaBounds() const {
  DCHECK(features::IsNotificationImageDragEnabled());
  if (!IsDraggable()) {
    return std::nullopt;
  }

  const views::View* large_image_view =
      GetViewByID(message_center::NotificationViewBase::kLargeImageView);
  gfx::RectF larget_image_bounds(large_image_view->GetLocalBounds());
  views::View::ConvertRectToTarget(large_image_view, /*target=*/this,
                                   &larget_image_bounds);
  return gfx::ToEnclosedRect(larget_image_bounds);
}

std::optional<gfx::ImageSkia> AshNotificationView::GetDragImage() {
  DCHECK(features::IsNotificationImageDragEnabled());
  if (!IsDraggable()) {
    return std::nullopt;
  }

  // Assume that an Ash notification has at most one large image view. Fetch the
  // image shown in the large image view.
  const gfx::ImageSkia& original_image =
      static_cast<message_center::LargeImageView*>(
          GetViewByID(message_center::NotificationViewBase::kLargeImageView))
          ->drawn_image();

  // Add the background color.
  const std::optional<size_t> radius =
      message_center::notification_view_util::GetLargeImageCornerRadius();
  const gfx::ImageSkia drag_image_with_background =
      gfx::ImageSkiaOperations::CreateImageWithRoundRectBackground(
          gfx::SizeF{original_image.size()}, radius.value_or(0),
          GetColorProvider()->GetColor(drag_drop::kDragImageBackgroundColor),
          original_image);

  // Add the drop shadow.
  return gfx::ImageSkiaOperations::CreateImageWithDropShadow(
      drag_image_with_background,
      drag_drop::GetDragImageShadowDetails(radius).values);
}

void AshNotificationView::AttachDropData(ui::OSExchangeData* data) {
  DCHECK(IsDraggable());

  // If the notification large image is file-backed, attach the image file path
  // to `data`; otherwise, attach the large image's binary data.
  if (const std::optional<base::FilePath>& image_path =
          message_center::MessageCenter::Get()
              ->FindNotificationById(notification_id())
              ->rich_notification_data()
              .image_path) {
    data->SetFilename(*image_path);
  } else {
    AttachBinaryImageAsDropData(data);
  }
}

bool AshNotificationView::IsDraggable() const {
  // A notification view is draggable only when it contains a large image.
  DCHECK(features::IsNotificationImageDragEnabled());
  return GetViewByID(message_center::NotificationViewBase::kLargeImageView);
}

base::TimeDelta AshNotificationView::GetBoundsAnimationDuration(
    const message_center::Notification& notification) const {
  // This is called after the parent gets notified of
  // `ChildPreferredSizeChanged()`, so the current expanded state is the target
  // state.
  if (!notification.image().IsEmpty()) {
    return base::Milliseconds(kLargeImageExpandAndCollapseAnimationDuration);
  }

  if (HasInlineReply(notification) || is_grouped_parent_view_) {
    if (IsExpanded()) {
      return base::Milliseconds(
          kInlineReplyAndGroupedParentExpandAnimationDuration);
    }
    return base::Milliseconds(
        kInlineReplyAndGroupedParentCollapseAnimationDuration);
  }

  if (inline_settings_row() && inline_settings_row()->GetVisible()) {
    return base::Milliseconds(
        kInlineSettingsExpandAndCollapseAnimationDuration);
  }

  if (IsExpanded()) {
    return base::Milliseconds(kGeneralExpandAnimationDuration);
  }
  return base::Milliseconds(kGeneralCollapseAnimationDuration);
}

void AshNotificationView::AnimateGroupedChildExpandedCollapse(bool expanded) {
  message_center_utils::InitLayerForAnimations(collapsed_summary_view_);
  message_center_utils::InitLayerForAnimations(main_view_);
  // Fade out `collapsed_summary_view_`, then fade in `main_view_` in expanded
  // state and vice versa in collapsed state.
  if (expanded) {
    message_center_utils::FadeOutView(
        collapsed_summary_view_,
        OnFadeOutAnimationEndedClosure(
            message_center::NotificationViewBase::kCollapsedSummaryView),
        0, kCollapsedSummaryViewAnimationDurationMs, gfx::Tween::LINEAR,
        "Ash.NotificationView.CollapsedSummaryView.FadeOut."
        "AnimationSmoothness");
    message_center_utils::FadeInView(
        main_view_, kCollapsedSummaryViewAnimationDurationMs,
        kChildMainViewFadeInAnimationDurationMs, gfx::Tween::LINEAR,
        "Ash.NotificationView.MainView.FadeIn.AnimationSmoothness");
    return;
  }

  message_center_utils::FadeOutView(
      main_view_,
      OnFadeOutAnimationEndedClosure(
          message_center::NotificationViewBase::ViewId::kMainView),
      0, kChildMainViewFadeOutAnimationDurationMs, gfx::Tween::LINEAR,
      "Ash.NotificationView.MainView.FadeOut.AnimationSmoothness");
  message_center_utils::FadeInView(
      collapsed_summary_view_, kChildMainViewFadeOutAnimationDurationMs,
      kCollapsedSummaryViewAnimationDurationMs, gfx::Tween::LINEAR,
      "Ash.NotificationView.CollapsedSummaryView.FadeIn.AnimationSmoothness");
}

void AshNotificationView::AnimateSingleToGroup(
    const std::string& notification_id,
    std::string parent_id) {
  ash::message_center_utils::InitLayerForAnimations(left_content());
  ash::message_center_utils::InitLayerForAnimations(right_content());
  ash::message_center_utils::InitLayerForAnimations(
      message_label_in_expanded_state_);
  ash::message_center_utils::InitLayerForAnimations(image_container_view());
  ash::message_center_utils::InitLayerForAnimations(action_buttons_row());

  std::pair<base::OnceClosure, base::OnceClosure> split =
      base::SplitOnceCallback(OnGroupedAnimationEndedClosure(
          left_content_, right_content(), message_label_in_expanded_state_,
          image_container_view(), action_buttons_row(), expand_button_,
          notification_id, parent_id));

  ui::AnimationThroughputReporter reporter(
      left_content()->layer()->GetAnimator(),
      metrics_util::ForSmoothnessV3(base::BindRepeating([](int smoothness) {
        base::UmaHistogramPercentage(
            "Ash.NotificationView.ConvertSingleToGroup.FadeOut."
            "AnimationSmoothness",
            smoothness);
      })));

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .OnEnded(std::move(split.first))
      .OnAborted(std::move(split.second))
      .Once()
      .SetDuration(
          base::Milliseconds(kConvertFromSingleToGroupFadeOutDurationMs))
      .SetOpacity(left_content(), 0.0f, gfx::Tween::LINEAR)
      .SetOpacity(right_content(), 0.0f, gfx::Tween::LINEAR)
      .SetOpacity(message_label_in_expanded_state_, 0.0f, gfx::Tween::LINEAR)
      .SetOpacity(image_container_view(), 0.0f, gfx::Tween::LINEAR)
      .SetOpacity(action_buttons_row(), 0.0f, gfx::Tween::LINEAR);
}

void AshNotificationView::ToggleExpand() {
  const bool target_expanded_state = !IsExpanded();

  SetManuallyExpandedOrCollapsed(
      target_expanded_state ? message_center::ExpandState::USER_EXPANDED
                            : message_center::ExpandState::USER_COLLAPSED);

  if (inline_reply() && inline_reply()->GetVisible()) {
    message_center_utils::FadeOutView(
        inline_reply(),
        OnFadeOutAnimationEndedClosure(
            message_center::NotificationViewBase::ViewId::kInlineReply),
        /*delay_in_ms=*/0, kInlineReplyFadeOutAnimationDurationMs,
        gfx::Tween::LINEAR,
        "Ash.NotificationView.InlineReply.FadeOut.AnimationSmoothness");
  }

  SetExpanded(target_expanded_state);

  PerformExpandCollapseAnimation();

  // Log expand button click action.
  if (IsExpanded()) {
    is_grouped_parent_view_
        ? metrics_utils::LogExpandButtonClickAction(
              metrics_utils::ExpandButtonClickAction::EXPAND_GROUP)
        : metrics_utils::LogExpandButtonClickAction(
              metrics_utils::ExpandButtonClickAction::EXPAND_INDIVIDUAL);
  } else {
    is_grouped_parent_view_
        ? metrics_utils::LogExpandButtonClickAction(
              metrics_utils::ExpandButtonClickAction::COLLAPSE_GROUP)
        : metrics_utils::LogExpandButtonClickAction(
              metrics_utils::ExpandButtonClickAction::COLLAPSE_INDIVIDUAL);
  }
}

void AshNotificationView::AddGroupNotification(
    const message_center::Notification& notification) {
  DCHECK(is_grouped_parent_view_);
  // Do not add a grouped notification if a view for it already exists.
  if (FindGroupNotificationView(notification.id())) {
    return;
  }

  auto notification_view =
      std::make_unique<AshNotificationView>(notification,
                                            /*shown_in_popup=*/false);
  notification_view->SetGroupedChildExpanded(IsExpanded());
  notification_view->set_parent_message_view(this);
  notification_view->set_scroller(
      scroller() ? scroller() : grouped_notifications_scroll_view_.get());

  header_row()->SetTimestamp(notification.timestamp());

  grouped_notifications_container_->AddChildViewAt(std::move(notification_view),
                                                   0);

  total_grouped_notifications_++;
  left_content_->SetVisible(false);
  UpdateGroupedNotificationsVisibility();
  expand_button_->UpdateCounter(total_grouped_notifications_);
  PreferredSizeChanged();
}

void AshNotificationView::PopulateGroupNotifications(
    const std::vector<const message_center::Notification*>& notifications) {
  DCHECK(is_grouped_parent_view_);
  // Clear all grouped notifications since we will add all grouped notifications
  // from scratch.
  total_grouped_notifications_ = 0;
  grouped_notifications_container_->RemoveAllChildViews();

  for (auto* notification : notifications) {
    auto notification_view =
        MessageViewFactory::Create(*notification, /*shown_in_popup=*/false);

    auto* child_notification_view =
        static_cast<message_center::MessageView*>(notification_view.get());
    child_notification_view->SetGroupedChildExpanded(IsExpanded());

    if (!total_grouped_notifications_) {
      header_row()->SetTimestamp(notification->timestamp());
    }

    notification_view->SetVisible(
        total_grouped_notifications_ <
            message_center_style::kMaxGroupedNotificationsInCollapsedState ||
        IsExpanded());

    notification_view->set_parent_message_view(this);
    notification_view->set_scroller(
        scroller() ? scroller() : grouped_notifications_scroll_view_.get());

    grouped_notifications_container_->AddChildView(
        std::move(notification_view));

    total_grouped_notifications_++;
  }
  left_content_->SetVisible(total_grouped_notifications_ == 0);
  expand_button_->UpdateCounter(total_grouped_notifications_);
}

void AshNotificationView::RemoveGroupNotification(
    const std::string& notification_id) {
  auto* child_view = FindGroupNotificationView(notification_id);
  if (!child_view) {
    return;
  }

  base::WeakPtr<message_center::MessageView> to_be_removed =
      static_cast<message_center::MessageView*>(child_view)
          ->weak_factory_.GetWeakPtr();
  if (to_be_removed) {
    // Abort any previously queued animations, if a remove animation was in
    // progress this will cause `to_be_removed` to be deleted. Because of this
    // we need to use a weakptr to ensure we do not try to animate an already
    // deleted view.
    to_be_removed->layer()->GetAnimator()->AbortAllAnimations();
  }

  if (!to_be_removed) {
    return;
  }

  auto on_notification_slid_out = base::BindOnce(
      [](base::WeakPtr<AshNotificationView> self,
         const std::string& notification_id) {
        if (!self) {
          return;
        }

        views::View* to_be_removed =
            self->FindGroupNotificationView(notification_id);
        if (!to_be_removed) {
          return;
        }

        self->total_grouped_notifications_--;
        self->expand_button_->UpdateCounter(self->total_grouped_notifications_);

        self->AnimateResizeAfterRemoval(to_be_removed);
      },
      weak_factory_.GetWeakPtr(), notification_id);

  auto on_animation_aborted = base::BindOnce(
      [](base::WeakPtr<AshNotificationView> self,
         const std::string& notification_id) {
        if (!self) {
          return;
        }

        views::View* to_be_removed =
            self->FindGroupNotificationView(notification_id);
        if (!to_be_removed) {
          return;
        }

        self->total_grouped_notifications_--;
        self->expand_button_->UpdateCounter(self->total_grouped_notifications_);

        self->grouped_notifications_container_->RemoveChildViewT(to_be_removed);
        self->PreferredSizeChanged();
      },
      weak_factory_.GetWeakPtr(), notification_id);

  // If the removed notification has a layer transform it has already been slid
  // out (For example user swiped it by dragging). We only need to animate a
  // slide out if there is no transform.
  if (to_be_removed && to_be_removed->layer()->transform().IsIdentity()) {
    message_center_utils::SlideOutView(
        to_be_removed.get(), std::move(on_notification_slid_out),
        std::move(on_animation_aborted),
        /*delay_in_ms=*/0,
        /*duration_in_ms=*/kSlideOutGroupedNotificationAnimationDurationMs,
        gfx::Tween::LINEAR,
        "Ash.Notification.GroupNotification.SlideOut.AnimationSmoothness");
  } else {
    std::move(on_notification_slid_out).Run();
  }
}

void AshNotificationView::UpdateViewForExpandedState(bool expanded) {
  // Grouped parent views should always use the expanded paddings, even if they
  // are collapsed.
  bool use_expanded_padding = expanded || is_grouped_parent_view_;

  header_row()->SetVisible(is_grouped_parent_view_ || expanded);
  header_row()->SetTimestampVisible(!is_grouped_parent_view_ || !expanded);

  if (title_row_) {
    title_row_->UpdateVisibility(IsExpandable() && !expanded);
    title_row_->title_view()->SetMaxLines(
        expanded ? kTitleLabelExpandedMaxLines : kTitleLabelCollapsedMaxLines);
    // Add extra padding to center the title in collapsed mode when there is no
    // message. The exception is when this is a progress notification, as
    // progress notifications always show a progress bar and thus don't need the
    // title vertically centered.
    title_row_->SetProperty(
        views::kMarginsKey,
        !progress_bar_view() && !message_label() && !use_expanded_padding
            ? kTitleRowNoMessageCollapsedPadding
            : gfx::Insets());
  }

  if (message_label()) {
    // `message_label()` is shown only in collapsed mode.
    message_label()->SetVisible(!expanded);
    message_label_in_expanded_state_->SetVisible(expanded &&
                                                 !is_grouped_parent_view_);
  }

  if (progress_bar_view()) {
    int progress_bar_bottom_padding;
    if (!action_buttons().empty()) {
      progress_bar_bottom_padding = kProgressBarWithActionButtonsBottomPadding;
    } else if (use_expanded_padding) {
      progress_bar_bottom_padding = kProgressBarExpandedBottomPadding;
    } else {
      progress_bar_bottom_padding = kProgressBarCollapsedBottomPadding;
    }
    progress_bar_view()->SetBorder(views::CreateEmptyBorder(
        gfx::Insets::TLBR(message_center::kProgressBarTopPadding, 0,
                          progress_bar_bottom_padding, 0)));
  }

  // Custom padding for app icon and expand button. These 2 views should always
  // use the same padding value so that they are vertical aligned.
  app_icon_view_->SetProperty(views::kMarginsKey,
                              use_expanded_padding ? kAppIconExpandedPadding
                                                   : kAppIconCollapsedPadding);

  if (features::IsRenderArcNotificationsByChromeEnabled()) {
    right_content()->SetProperty(views::kCrossAxisAlignmentKey,
                                 use_expanded_padding
                                     ? views::LayoutAlignment::kStart
                                     : views::LayoutAlignment::kCenter);
  }

  right_content()->SetProperty(
      views::kMarginsKey, use_expanded_padding ? kRightContentExpandedPadding
                                               : kRightContentCollapsedPadding);

  expand_button_->SetProperty(
      views::kMarginsKey, use_expanded_padding ? kExpandButtonExpandedPadding
                                               : kExpandButtonCollapsedPadding);
  header_row()->SetProperty(views::kMarginsKey,
                            use_expanded_padding ? kHeaderRowExpandedPadding
                                                 : kHeaderRowCollapsedPadding);

  expand_button_->SetExpanded(expanded);

  if (is_grouped_parent_view_) {
    if (grouped_notifications_scroll_view_) {
      grouped_notifications_scroll_view_->ClipHeightTo(
          0, CalculateMaxHeightForGroupedNotifications());
    }

    auto* grouped_notifications_container_layout_manager =
        static_cast<views::BoxLayout*>(
            grouped_notifications_container_->GetLayoutManager());
    grouped_notifications_container_layout_manager->set_inside_border_insets(
        expanded ? kGroupedNotificationContainerExpandedInsets
                 : kGroupedNotificationContainerCollapsedInsets);
    grouped_notifications_container_layout_manager->set_between_child_spacing(
        expanded ? kGroupedNotificationsExpandedSpacing
                 : kGroupedNotificationsCollapsedSpacing);

    int notification_count = 0;
    for (views::View* child : grouped_notifications_container_->children()) {
      auto* notification_view =
          static_cast<message_center::MessageView*>(child);
      notification_view->AnimateGroupedChildExpandedCollapse(expanded);
      notification_view->SetGroupedChildExpanded(expanded);

      notification_count++;
      if (notification_count >
          message_center_style::kMaxGroupedNotificationsInCollapsedState) {
        child->SetVisible(expanded);
      }
    }
  }

  NotificationViewBase::UpdateViewForExpandedState(expanded);

  message_label_in_expanded_state_->SetProperty(
      views::kMarginsKey,
      (actions_row()->GetVisible() || image_container_view()->GetVisible() ||
               is_grouped_child_view_
           ? kMessageLabelInExpandedStatePadding
           : kMessageLabelInExpandedStateExtendedPadding));
}

void AshNotificationView::UpdateWithNotification(
    const message_center::Notification& notification) {
  is_grouped_child_view_ = notification.group_child();
  is_grouped_parent_view_ = notification.group_parent();

  if (grouped_notifications_scroll_view_) {
    grouped_notifications_scroll_view_->SetVisible(is_grouped_parent_view_);
  }
  grouped_notifications_container_->SetVisible(is_grouped_parent_view_);

  if (is_grouped_child_view_ && !is_nested()) {
    SetIsNested();
  }

  header_row()->SetIsInGroupChildNotification(is_grouped_child_view_);
  UpdateMessageLabelInExpandedState(notification);

  NotificationViewBase::UpdateWithNotification(notification);

  CreateOrUpdateSnoozeButton(notification);

  // Configure views style.
  UpdateIconAndButtonsColor(¬ification);
  if (message_label()) {
    notification_style_utils::ConfigureLabelStyle(message_label(),
                                                  kNotificationMessageLabelSize,
                                                  /*is_color_primary=*/false);
    message_label()->SetEnabledColorId(cros_tokens::kCrosSysOnSurfaceVariant);
    ash::TypographyProvider::Get()->StyleLabel(
        ash::TypographyToken::kCrosAnnotation1, *message_label());
  }
}

void AshNotificationView::CreateOrUpdateHeaderView(
    const message_center::Notification& notification) {
  switch (notification.system_notification_warning_level()) {
    case message_center::SystemNotificationWarningLevel::WARNING:
      header_row()->SetSummaryText(
          l10n_util::GetStringUTF16(IDS_ASH_NOTIFICATION_WARNING_LABEL));
      break;
    case message_center::SystemNotificationWarningLevel::CRITICAL_WARNING:
      header_row()->SetSummaryText(l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_CRITICAL_WARNING_LABEL));
      break;
    case message_center::SystemNotificationWarningLevel::NORMAL:
      header_row()->SetSummaryText(std::u16string());
      break;
  }

  NotificationViewBase::CreateOrUpdateHeaderView(notification);
}

void AshNotificationView::CreateOrUpdateTitleView(
    const message_center::Notification& notification) {
  if (notification.title().empty()) {
    if (title_row_) {
      DCHECK(left_content()->Contains(title_row_));
      left_content()->RemoveChildViewT(title_row_.get());
      title_row_ = nullptr;
    }
    return;
  }

  const std::u16string& title = gfx::TruncateString(
      notification.title(), GetTitleCharacterLimit(), gfx::WORD_BREAK);

  if (!title_row_) {
    title_row_ =
        AddViewToLeftContent(std::make_unique<NotificationTitleRow>(title));
  } else {
    title_row_->UpdateTitle(title);
    ReorderViewInLeftContent(title_row_);
  }

  expand_button_->SetNotificationTitleForButtonTooltip(title);

  int max_available_width = notification.icon().IsEmpty()
                                ? kTitleRowMinimumWidth
                                : kTitleRowMinimumWidthWithIcon;
  if (shown_in_popup_) {
    max_available_width -= message_center::GetNotificationWidth() -
                           GetNotificationInMessageCenterWidth();
  }
  title_row_->SetMaxAvailableWidth(max_available_width);

  title_row_->UpdateTimestamp(notification.timestamp());
}

void AshNotificationView::CreateOrUpdateSmallIconView(
    const message_center::Notification& notification) {
  if (is_grouped_child_view_ && !notification.icon().IsEmpty()) {
    app_icon_view_->SetImage(
        notification.icon().Rasterize(GetColorProvider()),
        gfx::Size(kNotificationAppIconViewSize, kNotificationAppIconViewSize));
    return;
  }

  UpdateAppIconView(¬ification);
}

void AshNotificationView::CreateOrUpdateInlineSettingsViews(
    const message_center::Notification& notification) {
  if (inline_settings_enabled()) {
    DCHECK(message_center::SettingsButtonHandler::INLINE ==
           notification.rich_notification_data().settings_button_handler);
    return;
  }

  set_inline_settings_enabled(
      !is_grouped_child_view_ &&
      notification.rich_notification_data().settings_button_handler ==
          message_center::SettingsButtonHandler::INLINE);

  if (!inline_settings_enabled()) {
    return;
  }

  inline_settings_row()->AddChildView(
      notification_style_utils::CreateInlineSettingsViewForMessageView(this));
}

void AshNotificationView::CreateOrUpdateSnoozeSettingsViews(
    const message_center::Notification& notification) {
  // TODO(b/298216201): Enable snooze settings after adding mojo callbacks in
  // the snooze settings layout.

  if (!snooze_settings_enabled()) {
    return;
  }

  auto snooze_notification_1_hour_button = GenerateNotificationLabelButton(
      base::BindRepeating(&MessageView::DisableNotification,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_SNOOZE_SETTINGS_SNOOZE_1_HOUR_TEXT));
  snooze_settings_row()->AddChildView(
      std::move(snooze_notification_1_hour_button));

  auto snooze_notification_15_minutes_button = GenerateNotificationLabelButton(
      base::BindRepeating(&MessageView::DisableNotification,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_SNOOZE_SETTINGS_SNOOZE_15_MINUTES_TEXT));
  snooze_settings_row()->AddChildView(
      std::move(snooze_notification_15_minutes_button));

  auto snooze_notification_30_minutes_button = GenerateNotificationLabelButton(
      base::BindRepeating(&MessageView::DisableNotification,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_SNOOZE_SETTINGS_SNOOZE_30_MINUTES_TEXT));
  snooze_settings_row()->AddChildView(
      std::move(snooze_notification_30_minutes_button));

  auto snooze_notification_2_hours_button = GenerateNotificationLabelButton(
      base::BindRepeating(&MessageView::DisableNotification,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_SNOOZE_SETTINGS_SNOOZE_2_HOURS_TEXT));
  snooze_settings_row()->AddChildView(
      std::move(snooze_notification_2_hours_button));

  auto undo_snooze_notification_button = GenerateNotificationLabelButton(
      base::BindRepeating(&AshNotificationView::ToggleSnoozeSettings,
                          base::Unretained(this)),
      l10n_util::GetStringUTF16(
          IDS_ASH_NOTIFICATION_SNOOZE_SETTINGS_UNDO_SNOOZE_TEXT));
  snooze_settings_row()->AddChildView(
      std::move(undo_snooze_notification_button));
}

void AshNotificationView::CreateOrUpdateCompactTitleMessageView(
    const message_center::Notification& notification) {
  // No CompactTitleMessageView required. It is only used for progress
  // notifications when the notification is collapsed, and Ash progress
  // notifications only show a progress bar when collapsed.
}

void AshNotificationView::CreateOrUpdateProgressViews(
    const message_center::Notification& notification) {
  // AshNotificationView should have the status view, followed by the progress
  // bar. This is the opposite of what is required of the chrome notification.
  CreateOrUpdateProgressStatusView(notification);
  CreateOrUpdateProgressBarView(notification);
  if (progress_bar_view()) {
    progress_bar_view()->SetForegroundColorId(cros_tokens::kCrosSysPrimary);
    progress_bar_view()->SetBackgroundColorId(
        cros_tokens::kCrosSysHighlightShape);
  }

  if (status_view()) {
    status_view()->SetMultiLine(true);
    status_view()->SetMaxLines(message_center::kMaxLinesForStatusView);
    status_view()->SetEnabledColorId(cros_tokens::kCrosSysOnSurfaceVariant);
    TypographyProvider::Get()->StyleLabel(TypographyToken::kCrosAnnotation1,
                                          *status_view());
  }
}

void AshNotificationView::UpdateControlButtonsVisibility() {
  NotificationViewBase::UpdateControlButtonsVisibility();

  // Always hide snooze button in control buttons since we show this snooze
  // button in actions button view.
  control_buttons_view()->ShowSnoozeButton(false);

  // Hide settings button for grouped child notifications.
  if (is_grouped_child_view_) {
    control_buttons_view()->ShowSettingsButton(false);
  }
}

bool AshNotificationView::IsIconViewShown() const {
  return NotificationViewBase::IsIconViewShown() && !is_grouped_child_view_;
}

void AshNotificationView::SetExpandButtonVisibility(bool visible) {
  expand_button_->SetVisible(visible);
}

bool AshNotificationView::IsExpandable() const {
  // Inline settings can not be expanded.
  if (GetMode() == Mode::SETTING) {
    return false;
  }

  // Notification should always be expandable since we hide `header_row()` in
  // collapsed state.
  return true;
}

void AshNotificationView::UpdateCornerRadius(int top_radius,
                                             int bottom_radius) {
  // Call parent's SetCornerRadius to update radius used for highlight path.
  NotificationViewBase::SetCornerRadius(top_radius, bottom_radius);
}

void AshNotificationView::OnThemeChanged() {
  views::View::OnThemeChanged();

  if (message_label()) {
    message_label()->SetEnabledColorId(cros_tokens::kCrosSysOnSurfaceVariant);
  }

  if (control_buttons_view_) {
    control_buttons_view_->SetButtonIconColors(
        AshColorProvider::Get()->GetContentLayerColor(
            AshColorProvider::ContentLayerType::kIconColorPrimary));
  }

  if (message_label_in_expanded_state_) {
    message_label_in_expanded_state_->SetEnabledColorId(
        cros_tokens::kCrosSysOnSurfaceVariant);
  }

  UpdateIconAndButtonsColor(
      message_center::MessageCenter::Get()->FindVisibleNotificationById(
          notification_id()));

  // For unittests, `GetColorProvider()` could be nullptr.
  if (inline_reply() && GetColorProvider()) {
    inline_reply()->textfield()->SetTextColor(
        GetColorProvider()->GetColor(cros_tokens::kCrosSysOnSurface));
    inline_reply()->textfield()->set_placeholder_text_color(
        GetColorProvider()->GetColor(cros_tokens::kCrosSysOnSurfaceVariant));
  }

  if (icon_view() &&
      (right_content()->width() - icon_view()->GetImageDrawingSize().width() >
           kSmallImageBackgroundThreshold ||
       right_content()->height() - icon_view()->GetImageDrawingSize().height() >
           kSmallImageBackgroundThreshold)) {
    icon_view()->set_apply_rounded_corners(false);
    right_content()->SetBackground(views::CreateThemedRoundedRectBackground(
        kColorAshControlBackgroundColorInactive,
        message_center::kImageCornerRadius));
  }
}

std::unique_ptr<message_center::NotificationInputContainer>
AshNotificationView::GenerateNotificationInputContainer() {
  return std::make_unique<AshNotificationInputContainer>(this);
}

std::unique_ptr<views::LabelButton>
AshNotificationView::GenerateNotificationLabelButton(
    views::Button::PressedCallback callback,
    const std::u16string& label) {
  std::unique_ptr<PillButton> actions_button = std::make_unique<PillButton>(
      std::move(callback), label, PillButton::Type::kFloatingWithoutIcon,
      /*icon=*/nullptr, kNotificationPillButtonHorizontalSpacing);
  actions_button->SetButtonTextColorId(cros_tokens::kCrosSysOnSurface);

  return actions_button;
}

gfx::Size AshNotificationView::GetIconViewSize() const {
  int icon_size = features::IsRenderArcNotificationsByChromeEnabled()
                      ? kIconViewSizeRenderArcInChromeEnabled
                      : kIconViewSize;
  return gfx::Size(icon_size, icon_size);
}

int AshNotificationView::GetLargeImageViewMaxWidth() const {
  return message_center::GetNotificationWidth() -
         kNotificationViewPadding.width() - kNotificationAppIconViewSize -
         kMainRightViewChildPadding.width();
}

void AshNotificationView::ToggleInlineSettings(const ui::Event& event) {
  if (!inline_settings_enabled()) {
    return;
  }

  bool should_show_inline_settings = !inline_settings_row()->GetVisible();
  PerformToggleInlineSettingsAnimation(should_show_inline_settings);

  NotificationViewBase::ToggleInlineSettings(event);

  if (is_grouped_parent_view_) {
    if (shown_in_popup_) {
      grouped_notifications_scroll_view_->SetVisible(
          !should_show_inline_settings);
    } else {
      grouped_notifications_container_->SetVisible(
          !should_show_inline_settings);
    }
  } else {
    // In settings UI, we only show the app icon and header row along with the
    // inline settings UI.
    header_row()->SetVisible(true);
    left_content()->SetVisible(!should_show_inline_settings);
    right_content()->SetVisible(!should_show_inline_settings);
  }
  expand_button_->SetVisible(!should_show_inline_settings);

  PreferredSizeChanged();
}

void AshNotificationView::ToggleSnoozeSettings(const ui::Event& event) {
  if (!snooze_settings_enabled()) {
    return;
  }

  bool should_show_snooze_settings = !snooze_settings_row()->GetVisible();

  NotificationViewBase::ToggleSnoozeSettings(event);

  left_content()->SetVisible(!should_show_snooze_settings);
  right_content()->SetVisible(!should_show_snooze_settings);

  PreferredSizeChanged();
}

void AshNotificationView::OnInlineReplyUpdated() {
  DCHECK(inline_reply() && inline_reply()->GetVisible());
  // Fade out actions button and then fade in inline reply.
  message_center_utils::InitLayerForAnimations(action_buttons_row());
  message_center_utils::FadeOutView(
      action_buttons_row(),
      OnFadeOutAnimationEndedClosure(
          message_center::NotificationViewBase::ViewId::kActionButtonsRow),
      /*delay_in_ms=*/0, kActionButtonsFadeOutAnimationDurationMs,
      gfx::Tween::LINEAR,
      "Ash.NotificationView.ActionButtonsRow.FadeOut.AnimationSmoothness");

  // Delay for the action buttons to fade out, then fade in inline reply.
  message_center_utils::InitLayerForAnimations(inline_reply());
  message_center_utils::FadeInView(
      inline_reply(), kActionButtonsFadeOutAnimationDurationMs,
      kInlineReplyFadeInAnimationDurationMs, gfx::Tween::LINEAR,
      "Ash.NotificationView.InlineReply.FadeIn.AnimationSmoothness");
}

views::View* AshNotificationView::FindGroupNotificationView(
    const std::string& notification_id) {
  auto notification = base::ranges::find(
      grouped_notifications_container_->children(), notification_id,
      [](views::View* notification_view) {
        return static_cast<message_center::MessageView*>(notification_view)
            ->notification_id();
      });
  return notification == grouped_notifications_container_->children().end()
             ? nullptr
             : *notification;
}

std::vector<raw_ptr<views::LabelButton, VectorExperimental>>
AshNotificationView::GetActionButtonsForTest() {
  return action_buttons();
}

views::Label* AshNotificationView::GetTitleRowLabelForTest() {
  return title_row_->title_view();
}

message_center::NotificationInputContainer*
AshNotificationView::GetInlineReplyForTest() {
  return inline_reply();
}

void AshNotificationView::OnNotificationRemoved(
    const std::string& notification_id,
    bool by_user) {
  if (!is_grouped_parent_view_) {
    return;
  }

  RemoveGroupNotification(notification_id);
}

void AshNotificationView::OnWidgetClosing(views::Widget* widget) {
  widget_observation_.Reset();
  AbortAllAnimations();
}

void AshNotificationView::OnWidgetDestroying(views::Widget* widget) {
  OnWidgetClosing(widget);
}

void AshNotificationView::AbortAllAnimations() {
  std::vector<scoped_refptr<ui::LayerAnimator>> animators;
  animators.push_back(layer()->GetAnimator());
  for (views::View* child_notification :
       grouped_notifications_container_->children()) {
    animators.push_back(child_notification->layer()->GetAnimator());
  }

  for (auto animator : animators) {
    animator->AbortAllAnimations();
  }
}

void AshNotificationView::CreateOrUpdateSnoozeButton(
    const message_center::Notification& notification) {
  if (!notification.should_show_snooze_button()) {
    if (action_buttons_row()->Contains(snooze_button_)) {
      action_buttons_row()->RemoveChildViewT(snooze_button_.get());
      snooze_button_ = nullptr;
      DCHECK(action_buttons_row()->Contains(snooze_button_spacer_));
      action_buttons_row()->RemoveChildViewT(snooze_button_spacer_);
      snooze_button_spacer_ = nullptr;
    }
    return;
  }

  if (snooze_button_) {
    DCHECK(snooze_button_spacer_);
    // Spacer and snooze button should be at the end of action buttons row.
    action_buttons_row()->ReorderChildView(
        snooze_button_spacer_, action_buttons_row()->children().size());
    action_buttons_row()->ReorderChildView(
        snooze_button_, action_buttons_row()->children().size());
    return;
  }

  action_buttons_row()->AddChildView(
      views::Builder<views::BoxLayoutView>()
          .CopyAddressTo(&snooze_button_spacer_)
          .SetMainAxisAlignment(MainAxisAlignment::kEnd)
          .SetProperty(views::kBoxLayoutFlexKey,
                       views::BoxLayoutFlexSpecification())
          .Build());

  auto snooze_button = std::make_unique<IconButton>(
      base::BindRepeating(&AshNotificationView::OnSnoozeButtonPressed,
                          base::Unretained(this)),
      IconButton::Type::kMediumFloating, &kNotificationSnoozeButtonIcon,
      IDS_MESSAGE_CENTER_NOTIFICATION_SNOOZE_BUTTON_TOOLTIP);
  snooze_button_ = action_buttons_row()->AddChildView(std::move(snooze_button));
}

void AshNotificationView::UpdateGroupedNotificationsVisibility() {
  for (size_t i = 0; i < grouped_notifications_container_->children().size();
       i++) {
    auto* view = grouped_notifications_container_->children()[i].get();
    bool show_notification_view =
        IsExpanded() ||
        i < message_center_style::kMaxGroupedNotificationsInCollapsedState;

    if (view->GetVisible() == show_notification_view) {
      continue;
    }

    view->SetVisible(show_notification_view);
  }
}

void AshNotificationView::UpdateMessageLabelInExpandedState(
    const message_center::Notification& notification) {
  if (notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS ||
      notification.message().empty()) {
    message_label_in_expanded_state_->SetVisible(false);
    return;
  }
  message_label_in_expanded_state_->SetText(gfx::TruncateString(
      notification.message(), message_center::GetMessageCharacterLimit(),
      gfx::WORD_BREAK));

  message_label_in_expanded_state_->SetVisible(true);
}

int AshNotificationView::GetExpandedMessageLabelWidth() {
  int notification_width = shown_in_popup_
                               ? message_center::GetNotificationWidth()
                               : GetNotificationInMessageCenterWidth();

  return notification_width - kNotificationViewPadding.width() -
         kNotificationAppIconViewSize - kMainRightViewChildPadding.width() -
         kMessageLabelInExpandedStatePadding.width();
}

void AshNotificationView::UpdateAppIconView(
    const message_center::Notification* notification) {
  // Grouped child notification use notification's icon for the app icon view,
  // so we don't need further update here.
  if (!notification ||
      (is_grouped_child_view_ && !notification->icon().IsEmpty())) {
    return;
  }

  app_icon_view_->SetImage(
      notification_style_utils::CreateNotificationAppIcon(notification));
}

void AshNotificationView::UpdateIconAndButtonsColor(
    const message_center::Notification* notification) {
  UpdateAppIconView(notification);

  SkColor button_color =
      notification_style_utils::CalculateIconBackgroundColor(notification);
  bool use_default_button_color =
      !notification ||
      notification->rich_notification_data().ignore_accent_color_for_text;
  if (use_default_button_color) {
    button_color = AshColorProvider::Get()->GetControlsLayerColor(
        AshColorProvider::ControlsLayerType::kControlBackgroundColorActive);
  }

  for (views::LabelButton* action_button : action_buttons()) {
    static_cast<PillButton*>(action_button)->SetButtonTextColor(button_color);
  }

  if (snooze_button_) {
    snooze_button_->SetIconColor(button_color);
  }
}

void AshNotificationView::AnimateResizeAfterRemoval(
    views::View* to_be_removed) {
  auto on_resize_complete = base::BindOnce(
      [](base::WeakPtr<AshNotificationView> self) {
        if (!self) {
          return;
        }

        self->set_is_animating(false);

        if (self->shown_in_popup_) {
          self->grouped_notifications_scroll_view_
              ->DeprecatedLayoutImmediately();
        }
      },
      weak_factory_.GetWeakPtr());

  int group_container_previous_height =
      grouped_notifications_container_->height();
  size_t removed_index =
      grouped_notifications_container_->GetIndexOf(to_be_removed).value();

  grouped_notifications_container_->RemoveChildViewT(to_be_removed).reset();

  auto* notification_view_controller = message_center_utils::
      GetActiveNotificationViewControllerForNotificationView(this);
  if (notification_view_controller) {
    notification_view_controller->AnimateResize();
  }

  if (shown_in_popup_) {
    grouped_notifications_scroll_view_->DeprecatedLayoutImmediately();
  } else {
    DeprecatedLayoutImmediately();
    PreferredSizeChanged();
  }

  int grouped_container_height_reduction =
      group_container_previous_height -
      grouped_notifications_container_->height();

  views::AnimationBuilder animation_builder;

  if (grouped_notifications_container_->children().begin() + removed_index >=
      grouped_notifications_container_->children().end()) {
    return;
  }
  set_is_animating(true);
  animation_builder.OnEnded(std::move(on_resize_complete));
  for (auto it =
           grouped_notifications_container_->children().begin() + removed_index;
       it != grouped_notifications_container_->children().end(); it++) {
    gfx::Rect child_bounds = (*it)->layer()->GetTargetBounds();
    (*it)->layer()->SetBounds(gfx::Rect(
        child_bounds.x(), child_bounds.y() + grouped_container_height_reduction,
        child_bounds.width(), child_bounds.height()));

    animation_builder.Once()
        .SetDuration(base::Milliseconds(
            message_center::kNotificationResizeAnimationDurationMs))
        .SetBounds((*it), child_bounds, gfx::Tween::EASE_OUT);
  }
}

void AshNotificationView::PerformExpandCollapseAnimation() {
  if (title_row_) {
    title_row_->PerformExpandCollapseAnimation();
  }

  // Fade in `header row()` if this is not a grouped parent view.
  if (header_row() && header_row()->GetVisible() && !is_grouped_parent_view_) {
    message_center_utils::FadeInView(
        header_row(), kHeaderRowFadeInAnimationDelayMs,
        kHeaderRowFadeInAnimationDurationMs, gfx::Tween::LINEAR,
        "Ash.NotificationView.HeaderRow.FadeIn.AnimationSmoothness");
  }

  // Fade in `message_label()`. We only do fade in for both message view in
  // expanded and collapsed mode if there's a difference between them (a.k.a
  // when `message_label()` is truncated).
  if (message_label() && message_label()->GetVisible() &&
      IsMessageLabelTruncated()) {
    message_center_utils::InitLayerForAnimations(message_label());
    message_center_utils::FadeInView(
        message_label(), kMessageLabelFadeInAnimationDelayMs,
        kMessageLabelFadeInAnimationDurationMs, gfx::Tween::LINEAR,
        "Ash.NotificationView.MessageLabel.FadeIn.AnimationSmoothness");
  }

  // Fade in `message_label_in_expanded_state_`.
  if (message_label_in_expanded_state_ &&
      message_label_in_expanded_state_->GetVisible() && message_label() &&
      IsMessageLabelTruncated()) {
    message_center_utils::FadeInView(
        message_label_in_expanded_state_,
        kMessageLabelInExpandedStateFadeInAnimationDelayMs,
        kMessageLabelInExpandedStateFadeInAnimationDurationMs,
        gfx::Tween::LINEAR,
        "Ash.NotificationView.ExpandedMessageLabel.FadeIn.AnimationSmoothness");
  }

  if (!image_container_view()->children().empty() && icon_view()) {
    PerformLargeImageAnimation();
  }

  if (actions_row() && actions_row()->GetVisible()) {
    message_center_utils::FadeInView(
        actions_row(), kActionsRowFadeInAnimationDelayMs,
        kActionsRowFadeInAnimationDurationMs, gfx::Tween::LINEAR,
        "Ash.NotificationView.ActionsRow.FadeIn.AnimationSmoothness");
  }

  if (total_grouped_notifications_) {
    // Ensure layout is up-to-date before animating expand button. This is used
    // for its bounds animation.
    if (needs_layout()) {
      DeprecatedLayoutImmediately();
    }
    auto* notification =
        message_center::MessageCenter::Get()->FindNotificationById(
            notification_id());

    // When the notification is ArcNotification and not group parent, the view
    // is rendered in Android then attached to message center. Ash does not
    // directly control the layout so we should not check `needs_layout()`.
    if (message_center_utils::IsAshNotification(notification) &&
        !notification->group_parent()) {
      DCHECK(!needs_layout());
    }

    expand_button_->AnimateExpandCollapse();
  }
}

void AshNotificationView::PerformLargeImageAnimation() {
  message_center_utils::InitLayerForAnimations(image_container_view());
  message_center_utils::InitLayerForAnimations(icon_view());
  auto icon_view_bounds = icon_view()->GetBoundsInScreen();
  auto large_image_bounds = image_container_view()->GetBoundsInScreen();

  if (IsExpanded()) {
    // In expanded state, do a scale and translate from `icon_view()` to
    // `image_container_view()`.
    message_center_utils::InitLayerForAnimations(image_container_view());
    ScaleAndTranslateView(image_container_view(),
                          static_cast<double>(icon_view()->width()) /
                              image_container_view()->width(),
                          static_cast<double>(icon_view()->height()) /
                              image_container_view()->height(),
                          icon_view_bounds.x() - large_image_bounds.x(),
                          icon_view_bounds.y() - large_image_bounds.y(),
                          "Ash.NotificationView.ImageContainerView."
                          "ScaleAndTranslate.AnimationSmoothness");

    // If we use different images for `icon_view()` and `image_container_view()`
    // (a.k.a hide_icon_on_expanded() is false), fade in
    // `image_container_view()`.
    if (!hide_icon_on_expanded()) {
      message_center_utils::FadeInView(
          image_container_view(), kLargeImageFadeInAnimationDelayMs,
          kLargeImageFadeInAnimationDurationMs, gfx::Tween::LINEAR,
          "Ash.NotificationView.ImageContainerView.FadeIn.AnimationSmoothness");
    }
    return;
  }

  if (hide_icon_on_expanded()) {
    // In collapsed state, if we use a same image for `icon_view()` and
    // `image_container_view()`, perform a scale and translate from
    // `image_container_view()` to `icon_view()`.
    ScaleAndTranslateView(
        icon_view(),
        static_cast<double>(image_container_view()->width()) /
            icon_view()->width(),
        static_cast<double>(image_container_view()->height()) /
            icon_view()->height(),
        large_image_bounds.x() - icon_view_bounds.x(),
        large_image_bounds.y() - icon_view_bounds.y(),
        "Ash.NotificationView.IconView.ScaleAndTranslate.AnimationSmoothness");
    return;
  }

  // In collapsed state, if we use a different image for `icon_view()` and
  // `image_container_view()`, fade out and scale down `image_container_view()`.
  message_center_utils::FadeOutView(
      image_container_view(),
      OnFadeOutAnimationEndedClosure(
          message_center::NotificationViewBase::ViewId::kImageContainerView),
      kLargeImageFadeOutAnimationDelayMs, kLargeImageFadeOutAnimationDurationMs,
      gfx::Tween::ACCEL_20_DECEL_100,
      "Ash.NotificationView.ImageContainerView.FadeOut.AnimationSmoothness");

  gfx::Transform transform;
  // Translate y further down so that it would not interfere with the currently
  // shown `icon_view()`.
  transform.Translate((icon_view_bounds.x() - large_image_bounds.x()),
                      (icon_view_bounds.y() - large_image_bounds.y() +
                       large_image_bounds.height()));
  transform.Scale(static_cast<double>(icon_view()->width()) /
                      image_container_view()->width(),
                  static_cast<double>(icon_view()->height()) /
                      image_container_view()->height());

  ui::AnimationThroughputReporter reporter(
      image_container_view()->layer()->GetAnimator(),
      ash::metrics_util::ForSmoothnessV3(
          base::BindRepeating([](int smoothness) {
            base::UmaHistogramPercentage(
                "Ash.NotificationView.ImageContainerView.ScaleDown."
                "AnimationSmoothness",
                smoothness);
          })));

  views::AnimationBuilder()
      .SetPreemptionStrategy(
          ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
      .Once()
      .At(base::TimeDelta())
      .SetTransform(image_container_view(), gfx::Transform())
      .Then()
      .SetDuration(base::Milliseconds(kLargeImageScaleDownDurationMs))
      .SetTransform(image_container_view(), transform,
                    gfx::Tween::ACCEL_20_DECEL_100);
}

void AshNotificationView::PerformToggleInlineSettingsAnimation(
    bool should_show_inline_settings) {
  if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
      ui::ScopedAnimationDurationScaleMode::ZERO_DURATION) {
    return;
  }

  message_center_utils::InitLayerForAnimations(main_right_view_);
  message_center_utils::InitLayerForAnimations(inline_settings_row());

  // Fade out views.
  if (should_show_inline_settings) {
    // Fade out left_content if it's visible.
    if (left_content_->GetVisible()) {
      message_center_utils::InitLayerForAnimations(left_content());
      message_center_utils::FadeOutView(
          left_content(),
          OnFadeOutAnimationEndedClosure(
              message_center::NotificationViewBase::ViewId::kLeftContent),
          /*delay_in_ms=*/0, kToggleInlineSettingsFadeOutDurationMs,
          gfx::Tween::LINEAR,
          "Ash.NotificationView.LeftContent.FadeOut.AnimationSmoothness");
    }
    message_center_utils::FadeOutView(
        expand_button_,
        OnFadeOutAnimationEndedClosure(
            message_center::NotificationViewBase::ViewId::kExpandButton),
        /*delay_in_ms=*/0, kToggleInlineSettingsFadeOutDurationMs,
        gfx::Tween::LINEAR,
        "Ash.NotificationView.ExpandButton.FadeOut.AnimationSmoothness");

    // Fade out icon_view() if it exists.
    if (icon_view()) {
      message_center_utils::InitLayerForAnimations(icon_view());
      message_center_utils::FadeOutView(
          icon_view(),
          OnFadeOutAnimationEndedClosure(
              message_center::NotificationViewBase::ViewId::kIconView),
          /*delay_in_ms=*/0, kToggleInlineSettingsFadeOutDurationMs,
          gfx::Tween::LINEAR,
          "Ash.NotificationView.IconView.FadeOut.AnimationSmoothness");
    }
  } else {
    message_center_utils::FadeOutView(
        inline_settings_row(),
        OnFadeOutAnimationEndedClosure(
            message_center::NotificationViewBase::ViewId::kInlineSettingsRow),
        /*delay_in_ms=*/0, kToggleInlineSettingsFadeOutDurationMs,
        gfx::Tween::LINEAR,
        "Ash.NotificationView.InlineSettingsRow.FadeOut.AnimationSmoothness");
  }

  // Fade in views.
  message_center_utils::FadeInView(
      main_right_view_, kToggleInlineSettingsFadeInDelayMs,
      kToggleInlineSettingsFadeInDurationMs, gfx::Tween::LINEAR,
      "Ash.NotificationView.MainRightView.FadeIn.AnimationSmoothness");
}

void AshNotificationView::AnimateSingleToGroupFadeIn() {
  auto fade_in_view = shown_in_popup_ ? grouped_notifications_scroll_view_
                                      : grouped_notifications_container_;
  message_center_utils::InitLayerForAnimations(fade_in_view);
  message_center_utils::FadeInView(
      fade_in_view, /*delay_in_ms=*/0,
      kConvertFromSingleToGroupFadeInDurationMs, gfx::Tween::LINEAR,
      "Ash.NotificationView.ConvertSingleToGroup.FadeIn.AnimationSmoothness");
}

int AshNotificationView::CalculateMaxHeightForGroupedNotifications() {
  auto* shelf = Shell::GetPrimaryRootWindowController()->shelf();
  const WorkAreaInsets* work_area =
      WorkAreaInsets::ForWindow(shelf->GetWindow()->GetRootWindow());

  const int bottom = shelf->IsHorizontalAlignment()
                         ? shelf->GetShelfBoundsInScreen().y()
                         : work_area->user_work_area_bounds().bottom();

  const int free_space_height_above_anchor =
      bottom - work_area->user_work_area_bounds().y();

  const int vertical_margin = 2 * message_center::kMarginBetweenPopups +
                              kNotificationViewPadding.height();

  return free_space_height_above_anchor - main_view_->bounds().height() -
         vertical_margin;
}

bool AshNotificationView::IsMessageLabelTruncated() {
  // True if the expanded label has more than one line.
  if (message_label_in_expanded_state_->GetRequiredLines() > 1) {
    return true;
  }

  // Get the first row's width of `message_label_in_expanded_state_`'s text,
  // which is also the text width of this label since it has one line. If text
  // width is larger than `left_content()`'s width, which is the space dedicated
  // to `message_label()`, the text is truncated.
  int text_width =
      message_label_in_expanded_state_
          ->GetSubstringBounds(gfx::Range(
              0, message_label_in_expanded_state_->GetText().length()))
          .front()
          .width();
  return text_width > left_content()->width();
}

void AshNotificationView::AttachBinaryImageAsDropData(
    ui::OSExchangeData* data) {
  DCHECK(IsDraggable());

  // Fetch the original image from the large image view.
  const gfx::ImageSkia& image =
      static_cast<message_center::LargeImageView*>(
          GetViewByID(message_center::NotificationViewBase::kLargeImageView))
          ->original_image();
  DCHECK(!image.size().IsEmpty());

  // Resize `image` if necessary.
  std::optional<gfx::ImageSkia> resized_image =
      message_center_utils::ResizeImageIfExceedSizeLimit(image,
                                                         kMaxImageSizeInByte);

  // Add the drop data in the format of HTML.
  if (const std::optional<std::u16string> html_snippet = GetHtmlForBitmap(
          resized_image ? *resized_image->bitmap() : *image.bitmap())) {
    data->SetHtml(*html_snippet, /*base_url=*/GURL());
  }
}

void AshNotificationView::OnFadeOutAnimationEnded(int view_id) {
  auto* view = GetViewByID(view_id);
  if (!view) {
    return;
  }

  auto* layer = view->layer();
  if (layer) {
    layer->SetOpacity(1.0f);
  }
  view->SetVisible(false);

  if (view == image_container_view() && layer) {
    layer->SetTransform(gfx::Transform());
  }
}

base::OnceClosure AshNotificationView::OnFadeOutAnimationEndedClosure(
    int view_id) {
  return base::BindOnce(&AshNotificationView::OnFadeOutAnimationEnded,
                        weak_factory_.GetWeakPtr(), view_id);
}

void AshNotificationView::OnGroupedAnimationEnded(
    views::View* left_content,
    views::View* right_content,
    views::View* message_label_in_expanded_state,
    views::View* image_container_view,
    views::View* action_buttons_row,
    AshNotificationExpandButton* expand_button,
    std::string notification_id,
    std::string parent_id) {
  auto* parent_notification =
      message_center::MessageCenter::Get()->FindNotificationById(parent_id);
  auto* child_notification =
      message_center::MessageCenter::Get()->FindNotificationById(
          notification_id);
  // The child and parent notifications are not guaranteed to exist. If
  // they were deleted avoid the animation cleanup.
  if (!parent_notification || !child_notification) {
    return;
  }

  auto* grouping_controller =
      message_center_utils::GetGroupingControllerForNotificationView(this);
  if (grouping_controller) {
    grouping_controller->ConvertFromSingleToGroupNotificationAfterAnimation(
        notification_id, parent_id, parent_notification);
  }

  left_content->layer()->SetOpacity(1.0f);
  right_content->layer()->SetOpacity(1.0f);
  message_label_in_expanded_state->layer()->SetOpacity(1.0f);
  image_container_view->layer()->SetOpacity(1.0f);
  action_buttons_row->layer()->SetOpacity(1.0f);

  // After fade out single notification and set up a group one, perform
  // a fade in.
  AnimateSingleToGroupFadeIn();

  expand_button->set_previous_bounds(expand_button->GetContentsBounds());
  DeprecatedLayoutImmediately();
  expand_button->AnimateSingleToGroupNotification();
}

base::OnceClosure AshNotificationView::OnGroupedAnimationEndedClosure(
    views::View* left_content,
    views::View* right_content,
    views::View* message_label_in_expanded_state,
    views::View* image_container_view,
    views::View* action_buttons_row,
    AshNotificationExpandButton* expand_button,
    const std::string& notification_id,
    std::string parent_id) {
  return base::BindOnce(&AshNotificationView::OnGroupedAnimationEnded,
                        weak_factory_.GetWeakPtr(), left_content, right_content,
                        message_label_in_expanded_state, image_container_view,
                        action_buttons_row, expand_button, notification_id,
                        parent_id);
}

BEGIN_METADATA(AshNotificationView)
END_METADATA

}  // namespace ash