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
 2129
 2130
 2131
 2132
 2133
 2134
 2135
 2136
 2137
 2138
 2139
 2140
 2141
 2142
 2143
 2144
 2145
 2146
 2147
 2148
 2149
 2150
 2151
 2152
 2153
 2154
 2155
 2156
 2157
 2158
 2159
 2160
 2161
 2162
 2163
 2164
 2165
 2166
 2167
 2168
 2169
 2170
 2171
 2172
 2173
 2174
 2175
 2176
 2177
 2178
 2179
 2180
 2181
 2182
 2183
 2184
 2185
 2186
 2187
 2188
 2189
 2190
 2191
 2192
 2193
 2194
 2195
 2196
 2197
 2198
 2199
 2200
 2201
 2202
 2203
 2204
 2205
 2206
 2207
 2208
 2209
 2210
 2211
 2212
 2213
 2214
 2215
 2216
 2217
 2218
 2219
 2220
 2221
 2222
 2223
 2224
 2225
 2226
 2227
 2228
 2229
 2230
 2231
 2232
 2233
 2234
 2235
 2236
 2237
 2238
 2239
 2240
 2241
 2242
 2243
 2244
 2245
 2246
 2247
 2248
 2249
 2250
 2251
 2252
 2253
 2254
 2255
 2256
 2257
 2258
 2259
 2260
 2261
 2262
 2263
 2264
 2265
 2266
 2267
 2268
 2269
 2270
 2271
 2272
 2273
 2274
 2275
 2276
 2277
 2278
 2279
 2280
 2281
 2282
 2283
 2284
 2285
 2286
 2287
 2288
 2289
 2290
 2291
 2292
 2293
 2294
 2295
 2296
 2297
 2298
 2299
 2300
 2301
 2302
 2303
 2304
 2305
 2306
 2307
 2308
 2309
 2310
 2311
 2312
 2313
 2314
 2315
 2316
 2317
 2318
 2319
 2320
 2321
 2322
 2323
 2324
 2325
 2326
 2327
 2328
 2329
 2330
 2331
 2332
 2333
 2334
 2335
 2336
 2337
 2338
 2339
 2340
 2341
 2342
 2343
 2344
 2345
 2346
 2347
 2348
 2349
 2350
 2351
 2352
 2353
 2354
 2355
 2356
 2357
 2358
 2359
 2360
 2361
 2362
 2363
 2364
 2365
 2366
 2367
 2368
 2369
 2370
 2371
 2372
 2373
 2374
 2375
 2376
 2377
 2378
 2379
 2380
 2381
 2382
 2383
 2384
 2385
 2386
 2387
 2388
 2389
 2390
 2391
 2392
 2393
 2394
 2395
 2396
 2397
 2398
 2399
 2400
 2401
 2402
 2403
 2404
 2405
 2406
 2407
 2408
 2409
 2410
 2411
 2412
 2413
 2414
 2415
 2416
 2417
 2418
 2419
 2420
 2421
 2422
 2423
 2424
 2425
 2426
 2427
 2428
 2429
 2430
 2431
 2432
 2433
 2434
 2435
 2436
 2437
 2438
 2439
 2440
 2441
 2442
 2443
 2444
 2445
 2446
 2447
 2448
 2449
 2450
 2451
 2452
 2453
 2454
 2455
 2456
 2457
 2458
 2459
 2460
 2461
 2462
 2463
 2464
 2465
 2466
 2467
 2468
 2469
 2470
 2471
 2472
 2473
 2474
 2475
 2476
 2477
 2478
 2479
 2480
 2481
 2482
 2483
 2484
 2485
 2486
 2487
 2488
 2489
 2490
 2491
 2492
 2493
 2494
 2495
 2496
 2497
 2498
 2499
 2500
 2501
 2502
 2503
 2504
 2505
 2506
 2507
 2508
 2509
 2510
 2511
 2512
 2513
 2514
 2515
 2516
 2517
 2518
 2519
 2520
 2521
 2522
 2523
 2524
 2525
 2526
 2527
 2528
 2529
 2530
 2531
 2532
 2533
 2534
 2535
 2536
 2537
 2538
 2539
 2540
 2541
 2542
 2543
 2544
 2545
 2546
 2547
 2548
 2549
 2550
 2551
 2552
 2553
 2554
 2555
 2556
 2557
 2558
 2559
 2560
 2561
 2562
 2563
 2564
 2565
 2566
 2567
 2568
 2569
 2570
 2571
 2572
 2573
 2574
 2575
 2576
 2577
 2578
 2579
 2580
 2581
 2582
 2583
 2584
 2585
 2586
 2587
 2588
 2589
 2590
 2591
 2592
 2593
 2594
 2595
 2596
 2597
 2598
 2599
 2600
 2601
 2602
 2603
 2604
 2605
 2606
 2607
 2608
 2609
 2610
 2611
 2612
 2613
 2614
 2615
 2616
 2617
 2618
 2619
 2620
 2621
 2622
 2623
 2624
 2625
 2626
 2627
 2628
 2629
 2630
 2631
 2632
 2633
 2634
 2635
 2636
 2637
 2638
 2639
 2640
 2641
 2642
 2643
 2644
 2645
 2646
 2647
 2648
 2649
 2650
 2651
 2652
 2653
 2654
 2655
 2656
 2657
 2658
 2659
 2660
 2661
 2662
 2663
 2664
 2665
 2666
 2667
 2668
 2669
 2670
 2671
 2672
 2673
 2674
 2675
 2676
 2677
 2678
 2679
 2680
 2681
 2682
 2683
 2684
 2685
 2686
 2687
 2688
 2689
 2690
 2691
 2692
 2693
 2694
 2695
 2696
 2697
 2698
 2699
 2700
 2701
 2702
 2703
 2704
 2705
 2706
 2707
 2708
 2709
 2710
 2711
 2712
 2713
 2714
 2715
 2716
 2717
 2718
 2719
 2720
 2721
 2722
 2723
 2724
 2725
 2726
 2727
 2728
 2729
 2730
 2731
 2732
 2733
 2734
 2735
 2736
 2737
 2738
 2739
 2740
 2741
 2742
 2743
 2744
 2745
 2746
 2747
 2748
 2749
 2750
 2751
 2752
 2753
 2754
 2755
 2756
 2757
 2758
 2759
 2760
 2761
 2762
 2763
 2764
 2765
 2766
 2767
 2768
 2769
 2770
 2771
 2772
 2773
 2774
 2775
 2776
 2777
 2778
 2779
 2780
 2781
 2782
 2783
 2784
 2785
 2786
 2787
 2788
 2789
 2790
 2791
 2792
 2793
 2794
 2795
 2796
 2797
 2798
 2799
 2800
 2801
 2802
 2803
 2804
 2805
 2806
 2807
 2808
 2809
 2810
 2811
 2812
 2813
 2814
 2815
 2816
 2817
 2818
 2819
 2820
 2821
 2822
 2823
 2824
 2825
 2826
 2827
 2828
 2829
 2830
 2831
 2832
 2833
 2834
 2835
 2836
 2837
 2838
 2839
 2840
 2841
 2842
 2843
 2844
 2845
 2846
 2847
 2848
 2849
 2850
 2851
 2852
 2853
 2854
 2855
 2856
 2857
 2858
 2859
 2860
 2861
 2862
 2863
 2864
 2865
 2866
 2867
 2868
 2869
 2870
 2871
 2872
 2873
 2874
 2875
 2876
 2877
 2878
 2879
 2880
 2881
 2882
 2883
 2884
 2885
 2886
 2887
 2888
 2889
 2890
 2891
 2892
 2893
 2894
 2895
 2896
 2897

ash / shelf / shelf_view.cc [blame]

// Copyright 2012 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/shelf/shelf_view.h"

#include <algorithm>
#include <memory>
#include <utility>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/app_list/app_list_item_util.h"
#include "ash/app_list/views/app_drag_icon_proxy.h"
#include "ash/app_list/views/ghost_image_view.h"
#include "ash/ash_element_identifiers.h"
#include "ash/constants/ash_features.h"
#include "ash/keyboard/keyboard_util.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/public/cpp/app_list/app_list_features.h"
#include "ash/public/cpp/metrics_util.h"
#include "ash/public/cpp/shelf_item.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/screen_util.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/hotseat_widget.h"
#include "ash/shelf/scrollable_shelf_view.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_app_button.h"
#include "ash/shelf/shelf_application_menu_model.h"
#include "ash/shelf/shelf_button.h"
#include "ash/shelf/shelf_context_menu_model.h"
#include "ash/shelf/shelf_controller.h"
#include "ash/shelf/shelf_focus_cycler.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_menu_model_adapter.h"
#include "ash/shelf/shelf_tooltip_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/status_area_widget.h"
#include "ash/user_education/user_education_class_properties.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/window_util.h"
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/scoped_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/timer/timer.h"
#include "chromeos/utils/haptics_util.h"
#include "components/account_id/account_id.h"
#include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/user_education/common/user_education_events.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/aura/window.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/menu_source_utils.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/compositor/animation_throughput_reporter.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/scoped_display_for_new_windows.h"
#include "ui/events/devices/haptic_touchpad_effects.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/bounds_animator.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/separator.h"
#include "ui/views/focus/focus_search.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_model.h"
#include "ui/views/view_model_utils.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"

using gfx::Animation;
using views::View;

namespace ash {

// The rip off distance, where the shelf icon gets unpinned if dragged over this
// distance from the outer edge of the shelf, depends on the shelf size. The
// distance is calculated by multiplying the shelf size by
// `kRipOffDistanceFactor`.
constexpr float kRipOffDistanceFactor = 0.75f;

// The rip off drag and drop proxy image should get scaled by this factor.
constexpr float kDragAndDropProxyScale = 1.2f;

// The opacity represents that this partially disappeared item will get removed.
constexpr float kDraggedImageOpacity = 0.5f;

namespace {

// The dimensions, in pixels, of the separator between pinned and unpinned
// items.
constexpr int kSeparatorSize = 20;
constexpr int kSeparatorThickness = 1;

constexpr char kShelfIconMoveAnimationHistogram[] =
    "Ash.ShelfIcon.AnimationSmoothness.Move";
constexpr char kShelfIconFadeInAnimationHistogram[] =
    "Ash.ShelfIcon.AnimationSmoothness.FadeIn";
constexpr char kShelfIconFadeOutAnimationHistogram[] =
    "Ash.ShelfIcon.AnimationSmoothness.FadeOut";

// A class to temporarily disable a given bounds animator.
class BoundsAnimatorDisabler {
 public:
  explicit BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
      : old_duration_(bounds_animator->GetAnimationDuration()),
        bounds_animator_(bounds_animator) {
    bounds_animator_->SetAnimationDuration(base::Milliseconds(1));
  }

  BoundsAnimatorDisabler(const BoundsAnimatorDisabler&) = delete;
  BoundsAnimatorDisabler& operator=(const BoundsAnimatorDisabler&) = delete;

  ~BoundsAnimatorDisabler() {
    bounds_animator_->SetAnimationDuration(old_duration_);
  }

 private:
  // The previous animation duration.
  base::TimeDelta old_duration_;
  // The bounds animator which gets used.
  raw_ptr<views::BoundsAnimator> bounds_animator_;
};

void ReportMoveAnimationSmoothness(int smoothness) {
  base::UmaHistogramPercentage(kShelfIconMoveAnimationHistogram, smoothness);
}

void ReportFadeInAnimationSmoothness(int smoothness) {
  base::UmaHistogramPercentage(kShelfIconFadeInAnimationHistogram, smoothness);
}

void ReportFadeOutAnimationSmoothness(int smoothness) {
  base::UmaHistogramPercentage(kShelfIconFadeOutAnimationHistogram, smoothness);
}

// Returns the id of the display on which |view| is shown.
int64_t GetDisplayIdForView(const View* view) {
  aura::Window* window = view->GetWidget()->GetNativeWindow();
  return display::Screen::GetScreen()->GetDisplayNearestWindow(window).id();
}

// Whether |item_view| is a ShelfAppButton and its state is STATE_DRAGGING.
bool ShelfButtonIsInDrag(const ShelfItemType item_type,
                         const views::View* item_view) {
  switch (item_type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
      return static_cast<const ShelfAppButton*>(item_view)->state() &
             ShelfAppButton::STATE_DRAGGING;
    case TYPE_DIALOG:
    case TYPE_UNDEFINED:
      return false;
  }
}

// Called back by the shelf item delegates to determine whether an app menu item
// should be included in the shelf app menu given its corresponding window. This
// is used to filter out items whose windows are on inactive desks when the per-
// desk shelf feature is enabled.
bool ShouldIncludeMenuItem(aura::Window* window) {
  if (!features::IsPerDeskShelfEnabled())
    return true;
  return desks_util::BelongsToActiveDesk(window);
}

// Returns true if the app associated with |app_id| is a Remote App.
bool IsRemoteApp(const std::string& app_id) {
  AccountId account_id =
      Shell::Get()->session_controller()->GetActiveAccountId();
  apps::AppRegistryCache* cache =
      apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);
  return cache && cache->GetAppType(app_id) == apps::AppType::kRemote;
}

// Records the user metric action for whenever a shelf item is pinned or
// unpinned.
void RecordPinUnpinUserAction(bool pinned) {
  if (pinned) {
    base::RecordAction(base::UserMetricsAction("Shelf_ItemPinned"));
  } else {
    base::RecordAction(base::UserMetricsAction("Shelf_ItemUnpinned"));
  }
}

}  // namespace

// Helper class that resets a view opacity when called. Used to reset drag view
// opacity after animation to drop the drag icon proxy to its final bounds ends.
// The resetter is no-op if the target view gets destroyed before the resetter
// get run.
class ShelfView::ViewOpacityResetter : public views::ViewObserver {
 public:
  explicit ViewOpacityResetter(views::View* view) : view_(view) {
    view_observer_.Observe(view);
  }
  ViewOpacityResetter(const ViewOpacityResetter&) = delete;
  ViewOpacityResetter& operator=(const ViewOpacityResetter&) = delete;

  ~ViewOpacityResetter() override { Run(); }

  // views::ViewObserver:
  void OnViewIsDeleting(View* observed_view) override {
    view_ = nullptr;
    view_observer_.Reset();
  }

  void Run() {
    if (view_ && view_->layer())
      view_->layer()->SetOpacity(1.0f);
  }

 private:
  raw_ptr<views::View> view_;
  base::ScopedObservation<views::View, views::ViewObserver> view_observer_{
      this};
};

// ImplicitAnimationObserver used when adding an item.
class ShelfView::FadeInAnimationDelegate
    : public ui::ImplicitAnimationObserver {
 public:
  explicit FadeInAnimationDelegate(ShelfView* shelf_view)
      : shelf_view_(shelf_view) {}
  ~FadeInAnimationDelegate() override { StopObservingImplicitAnimations(); }

 private:
  // ui::ImplicitAnimationObserver:
  void OnImplicitAnimationsCompleted() override {
    shelf_view_->OnFadeInAnimationEnded();
  }

  raw_ptr<ShelfView> shelf_view_ = nullptr;
};

// AnimationDelegate used when deleting an item. This steadily decreased the
// opacity of the layer as the animation progress.
class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
 public:
  FadeOutAnimationDelegate(ShelfView* host, std::unique_ptr<views::View> view)
      : shelf_view_(host), view_(std::move(view)) {}

  FadeOutAnimationDelegate(const FadeOutAnimationDelegate&) = delete;
  FadeOutAnimationDelegate& operator=(const FadeOutAnimationDelegate&) = delete;

  ~FadeOutAnimationDelegate() override = default;

  // AnimationDelegate overrides:
  void AnimationProgressed(const Animation* animation) override {
    view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
  }
  void AnimationEnded(const Animation* animation) override {
    // Ensures that |view| is not used after destruction.
    shelf_view_->StopAnimatingViewIfAny(view_.get());

    // Remove the view which has been faded away.
    view_.reset();

    shelf_view_->OnFadeOutAnimationEnded();
  }
  void AnimationCanceled(const Animation* animation) override {}

 private:
  raw_ptr<ShelfView> shelf_view_;
  std::unique_ptr<views::View> view_;
};

// AnimationDelegate used to trigger fading an element in. When an item is
// inserted this delegate is attached to the animation that expands the size of
// the item.  When done it kicks off another animation to fade the item in.
class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
 public:
  StartFadeAnimationDelegate(ShelfView* host, views::View* view)
      : shelf_view_(host), view_(view) {}

  StartFadeAnimationDelegate(const StartFadeAnimationDelegate&) = delete;
  StartFadeAnimationDelegate& operator=(const StartFadeAnimationDelegate&) =
      delete;

  ~StartFadeAnimationDelegate() override = default;

  // AnimationDelegate overrides:
  void AnimationEnded(const Animation* animation) override {
    shelf_view_->FadeIn(view_);
  }
  void AnimationCanceled(const Animation* animation) override {
    view_->layer()->SetOpacity(1.0f);
  }

 private:
  raw_ptr<ShelfView> shelf_view_;
  raw_ptr<views::View> view_;
};

// static
const int ShelfView::kMinimumDragDistance = 8;

ShelfView::ShelfView(ShelfModel* model,
                     Shelf* shelf,
                     Delegate* delegate,
                     ShelfButtonDelegate* shelf_button_delegate)
    : model_(model),
      shelf_(shelf),
      view_model_(std::make_unique<views::ViewModel>()),
      delegate_(delegate),
      bounds_animator_(
          std::make_unique<views::BoundsAnimator>(this,
                                                  /*use_transforms=*/true)),
      shelf_button_delegate_(shelf_button_delegate) {
  DCHECK(model_);
  DCHECK(shelf_);
  Shell::Get()->AddShellObserver(this);
  shelf_->AddObserver(this);
  bounds_animator_->AddObserver(this);
  bounds_animator_->SetAnimationDuration(
      ui::ScopedAnimationDurationScaleMode::duration_multiplier() *
      ShelfConfig::Get()->shelf_animation_duration());
  set_context_menu_controller(this);
  set_allow_deactivate_on_esc(true);

  if (features::IsUserEducationEnabled()) {
    // NOTE: Set `kHelpBubbleContextKey` before `views::kElementIdentifierKey`
    // in case registration causes a help bubble to be created synchronously.
    SetProperty(kHelpBubbleContextKey, HelpBubbleContext::kAsh);
  }
  SetProperty(views::kElementIdentifierKey, kShelfViewElementId);

  announcement_view_ = new views::View();
  AddChildView(announcement_view_.get());

  GetViewAccessibility().SetRole(ax::mojom::Role::kToolbar);
  GetViewAccessibility().SetName(
      l10n_util::GetStringUTF8(IDS_ASH_SHELF_ACCESSIBLE_NAME));
}

ShelfView::~ShelfView() {
  shelf_->RemoveObserver(this);
  Shell::Get()->RemoveShellObserver(this);
  bounds_animator_->RemoveObserver(this);
  model_->RemoveObserver(this);
}

int ShelfView::GetSizeOfAppButtons(int count, int button_size) {
  const int button_spacing = ShelfConfig::Get()->button_spacing();
  return button_size * count + button_spacing * std::max(0, count - 1);
}

void ShelfView::Init(views::FocusSearch* focus_search) {
  auto separator = std::make_unique<views::Separator>();
  separator->SetColorId(ui::kColorAshSystemUIMenuSeparator);
  separator->SetPreferredLength(kSeparatorSize);
  separator->SetVisible(false);
  ConfigureChildView(separator.get(), ui::LAYER_TEXTURED);
  separator_ = AddChildView(std::move(separator));

  model()->AddObserver(this);

  const ShelfItems& items(model_->items());

  for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
    views::View* child = CreateViewForItem(*i);
    child->SetPaintToLayer();
    int index = static_cast<int>(i - items.begin());
    view_model_->Add(child, index);
    // Add child view so it has the same ordering as in the |view_model_|.
    AddChildViewAt(child, index);
  }

  fade_in_animation_delegate_ = std::make_unique<FadeInAnimationDelegate>(this);

  focus_search_ = focus_search;

  // We'll layout when our bounds change.
}

bool ShelfView::IsShowingMenu() const {
  return shelf_menu_model_adapter_ &&
         shelf_menu_model_adapter_->IsShowingMenu();
}

void ShelfView::UpdateVisibleShelfItemBoundsUnion() {
  visible_shelf_item_bounds_union_.SetRect(0, 0, 0, 0);
  for (const auto i : visible_views_indices_) {
    const views::View* child = view_model_->view_at(i);
    if (ShouldShowTooltipForChildView(child)) {
      visible_shelf_item_bounds_union_.Union(
          GetChildViewTargetMirroredBounds(child));
    }
  }
}

bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
  if (!view || !view->parent())
    return false;

  if (view->parent() == this)
    return ShouldShowTooltipForChildView(view);

  return false;
}

ShelfAppButton* ShelfView::GetShelfAppButton(const ShelfID& id) {
  const int index = model_->ItemIndexByID(id);
  if (index < 0)
    return nullptr;

  views::View* const view = view_model_->view_at(index);
  DCHECK(views::IsViewClass<ShelfAppButton>(view));
  return static_cast<ShelfAppButton*>(view);
}

void ShelfView::StopAnimatingViewIfAny(views::View* view) {
  if (bounds_animator_->IsAnimating(view))
    bounds_animator_->StopAnimatingView(view);
}

int ShelfView::GetButtonSize() const {
  return ShelfConfig::Get()->GetShelfButtonSize(
      shelf_->hotseat_widget()->target_hotseat_density());
}

int ShelfView::GetButtonIconSize() const {
  return ShelfConfig::Get()->GetShelfButtonIconSize(
      shelf_->hotseat_widget()->target_hotseat_density());
}

int ShelfView::GetShortcutIconSize() const {
  return ShelfConfig::Get()->GetShelfShortcutIconSize();
}

int ShelfView::GetShelfShortcutIconContainerSize() const {
  return GetShortcutIconSize() +
         ShelfConfig::Get()->GetShelfShortcutIconBorderSize() * 2;
}

int ShelfView::GetShelfShortcutHostBadgeIconSize() const {
  return ShelfConfig::Get()->GetShelfShortcutHostBadgeIconSize();
}

int ShelfView::GetShelfShortcutHostBadgeContainerSize() const {
  return GetShelfShortcutHostBadgeIconSize() +
         ShelfConfig::Get()->GetShelfShortcutHostBadgeBorderSize() * 2;
}

int ShelfView::GetShelfShortcutTeardropCornerRadiusSize() const {
  return ShelfConfig::Get()->GetShelfShortcutTeardropCornerRadiusSize();
}

int ShelfView::GetShelfItemRippleSize() const {
  return GetButtonSize() +
         2 * ShelfConfig::Get()->scrollable_shelf_ripple_padding();
}

void ShelfView::LayoutIfAppIconsOffsetUpdates() {
  if (app_icons_layout_offset_ != CalculateAppIconsLayoutOffset())
    LayoutToIdealBounds();
}

ShelfAppButton* ShelfView::GetShelfItemViewWithContextMenu() {
  if (context_menu_id_.IsNull())
    return nullptr;
  const int item_index = model_->ItemIndexByID(context_menu_id_);
  if (item_index < 0)
    return nullptr;
  return static_cast<ShelfAppButton*>(view_model_->view_at(item_index));
}

void ShelfView::AnnounceShelfItemNotificationBadge(views::View* button) {
  announcement_view_->GetViewAccessibility().AnnounceText(
      l10n_util::GetStringFUTF16(IDS_SHELF_ITEM_HAS_NOTIFICATION_BADGE,
                                 GetTitleForView(button)));
}

bool ShelfView::LocationInsideVisibleShelfItemBounds(
    const gfx::Point& location) const {
  return visible_shelf_item_bounds_union_.Contains(location);
}

bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location,
                                  views::View* delegate_view) const {
  // There are thin gaps between launcher buttons but the tooltip shouldn't hide
  // in the gaps, but the tooltip should hide if the mouse moved totally outside
  // of the buttons area.
  return !LocationInsideVisibleShelfItemBounds(cursor_location);
}

const std::vector<aura::Window*> ShelfView::GetOpenWindowsForView(
    views::View* view) {
  std::vector<raw_ptr<aura::Window, VectorExperimental>> window_list =
      Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kActiveDesk);
  std::vector<aura::Window*> open_windows;
  const ShelfItem* item = ShelfItemForView(view);

  // The concept of a list of open windows doesn't make sense for something
  // that isn't an app shortcut: return an empty list.
  if (!item)
    return open_windows;

  for (aura::Window* window : window_list) {
    const std::string window_app_id =
        ShelfID::Deserialize(window->GetProperty(kShelfIDKey)).app_id;
    if (window_app_id == item->id.app_id) {
      // TODO: In the very first version we only show one window. Add the proper
      // UI to show all windows for a given open app.
      open_windows.push_back(window);
    }
  }
  return open_windows;
}

std::u16string ShelfView::GetTitleForView(const views::View* view) const {
  if (view->parent() == this)
    return GetTitleForChildView(view);

  return std::u16string();
}

views::View* ShelfView::GetViewForEvent(const ui::Event& event) {
  if (event.target() == GetWidget()->GetNativeWindow())
    return this;

  return nullptr;
}

gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
  gfx::Size preferred_size = GetPreferredSize();
  gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
  ConvertPointToScreen(this, &origin);
  return gfx::Rect(origin, preferred_size);
}

gfx::Size ShelfView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  const int hotseat_size = shelf_->hotseat_widget()->GetHotseatSize();
  if (visible_views_indices_.empty()) {
    // There are no visible shelf items.
    return shelf_->IsHorizontalAlignment() ? gfx::Size(0, hotseat_size)
                                           : gfx::Size(hotseat_size, 0);
  }

  const gfx::Rect last_button_bounds =
      view_model_->ideal_bounds(visible_views_indices_.back());

  if (shelf_->IsHorizontalAlignment())
    return gfx::Size(last_button_bounds.right(), hotseat_size);

  return gfx::Size(hotseat_size, last_button_bounds.bottom());
}

gfx::Rect ShelfView::GetAnchorBoundsInScreen() const {
  // Anchor bounds is the smallest rect containing all visible items.
  gfx::Rect anchor_bounds_in_screen;
  for (const size_t i : visible_views_indices_) {
    const views::View* const child = view_model_->view_at(i);
    anchor_bounds_in_screen.Union(child->GetBoundsInScreen());
  }
  // When the shelf is in overflow, visible items may exist outside `parent()`
  // bounds but they are clipped. Since they are not visible to the user, do
  // not consider them as part of anchor bounds.
  if (parent()) {
    anchor_bounds_in_screen.Intersect(parent()->GetBoundsInScreen());
  }
  // Fall back to default anchor bounds if there are no visible items.
  return anchor_bounds_in_screen.IsEmpty()
             ? views::AccessiblePaneView::GetAnchorBoundsInScreen()
             : anchor_bounds_in_screen;
}

void ShelfView::OnThemeChanged() {
  views::AccessiblePaneView::OnThemeChanged();
  if (separator_)
    separator_->SchedulePaint();
}

void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  // This bounds change is produced by the shelf movement (rotation, alignment
  // change, etc.) and all content has to follow. Using an animation at that
  // time would produce a time lag since the animation of the BoundsAnimator has
  // itself a delay before it arrives at the required location. As such we tell
  // the animator to go there immediately. We still want to use an animation
  // when the bounds change is caused by entering or exiting tablet mode, with
  // an exception of usage within the scrollable shelf. With scrollable shelf
  // (and hotseat), tablet mode transition causes hotseat bounds changes, so
  // animating shelf items as well would introduce a lag.

  BoundsAnimatorDisabler disabler(bounds_animator_.get());

  LayoutToIdealBounds();
  shelf_->NotifyShelfIconPositionsChanged();
}

bool ShelfView::OnKeyPressed(const ui::KeyEvent& event) {
  if (event.IsControlDown() &&
      keyboard_util::IsArrowKeyCode(event.key_code())) {
    bool swap_with_next = (event.key_code() == ui::VKEY_DOWN ||
                           event.key_code() == ui::VKEY_RIGHT);
    SwapButtons(GetFocusManager()->GetFocusedView(), swap_with_next);
    return true;
  }
  return views::View::OnKeyPressed(event);
}

void ShelfView::OnMouseEvent(ui::MouseEvent* event) {
  gfx::Point location_in_screen(event->location());
  View::ConvertPointToScreen(this, &location_in_screen);

  switch (event->type()) {
    case ui::EventType::kMousewheel:
      // The mousewheel event is handled by the ScrollableShelfView.
      break;
    case ui::EventType::kMousePressed:
      if (!event->IsOnlyLeftMouseButton()) {
        if (event->IsOnlyRightMouseButton()) {
          ShowContextMenuForViewImpl(this, location_in_screen,
                                     ui::mojom::MenuSourceType::kMouse);
          event->SetHandled();
        }
        return;
      }

      [[fallthrough]];
    case ui::EventType::kMouseDragged:
    case ui::EventType::kMouseReleased:
      // Convert the event location from current view to screen, since dragging
      // the shelf by mouse can open the fullscreen app list. Updating the
      // bounds of the app list during dragging is based on screen coordinate
      // space.
      event->set_location(location_in_screen);

      event->SetHandled();
      shelf_->ProcessMouseEvent(*event->AsMouseEvent());
      break;
    default:
      break;
  }
}

views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
  // ScrollableShelfView should handles the focus traversal.
  return nullptr;
}

View* ShelfView::GetTooltipHandlerForPoint(const gfx::Point& point) {
  // Similar implementation as views::View, but without going into each
  // child's subviews.
  View::Views children = GetChildrenInZOrder();
  for (views::View* child : base::Reversed(children)) {
    if (!child->GetVisible())
      continue;

    gfx::Point point_in_child_coords(point);
    ConvertPointToTarget(this, child, &point_in_child_coords);
    if (child->HitTestPoint(point_in_child_coords) &&
        ShouldShowTooltipForChildView(child)) {
      return child;
    }
  }
  // If none of our children qualifies, just return the shelf view itself.
  return this;
}

void ShelfView::ViewHierarchyChanged(
    const views::ViewHierarchyChangedDetails& details) {
  if (!details.is_add) {
    if (details.child == current_ghost_view_.get()) {
      current_ghost_view_ = nullptr;
      current_ghost_view_index_ = std::nullopt;
    }
    if (details.child == last_ghost_view_.get()) {
      last_ghost_view_ = nullptr;
      current_ghost_view_index_ = std::nullopt;
    }
  }
}

void ShelfView::OnShelfButtonAboutToRequestFocusFromTabTraversal(
    ShelfButton* button,
    bool reverse) {
  if (ShouldFocusOut(reverse, button)) {
    shelf_->shelf_focus_cycler()->FocusOut(reverse, SourceView::kShelfView);
  }
}

void ShelfView::ButtonPressed(views::Button* sender,
                              const ui::Event& event,
                              views::InkDrop* ink_drop) {
  if (!ShouldEventActivateButton(sender, event)) {
    ink_drop->SnapToHidden();
    return;
  }

  // Prevent concurrent requests that may show application or context menus.
  if (!item_awaiting_response_.IsNull()) {
    const ShelfItem* item = ShelfItemForView(sender);
    if (item && item->id != item_awaiting_response_)
      ink_drop->AnimateToState(views::InkDropState::DEACTIVATED);
    return;
  }

  // Ensure the keyboard is hidden and stays hidden (as long as it isn't locked)
  if (keyboard::KeyboardUIController::Get()->IsEnabled())
    keyboard::KeyboardUIController::Get()->HideKeyboardExplicitlyBySystem();

  // Record the index for the last pressed shelf item.
  last_pressed_index_ = view_model_->GetIndexOfView(sender);
  DCHECK(last_pressed_index_.has_value());

  // Place new windows on the same display as the button. Opening windows is
  // usually an async operation so we wait until window activation changes
  // (ShelfItemStatusChanged) before destroying the scoped object. Post a task
  // to destroy the scoped object just in case the window activation event does
  // not get fired.
  aura::Window* window = sender->GetWidget()->GetNativeWindow();
  scoped_display_for_new_windows_ =
      std::make_unique<display::ScopedDisplayForNewWindows>(
          window->GetRootWindow());
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&ShelfView::DestroyScopedDisplay,
                     weak_factory_.GetWeakPtr()),
      base::Milliseconds(100));

  // Slow down activation animations if Control key is pressed.
  std::unique_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
  if (event.IsControlDown()) {
    slowing_animations = std::make_unique<ui::ScopedAnimationDurationScaleMode>(
        ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  }

  // Collect usage statistics before we decide what to do with the click.
  switch (model_->items()[last_pressed_index_.value()].type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
      base::RecordAction(base::UserMetricsAction("Launcher_ClickOnApp"));
      break;

    case TYPE_DIALOG:
      break;

    case TYPE_UNDEFINED:
      NOTREACHED() << "ShelfItemType must be set.";
  }

  // Run AfterItemSelected directly if the item has no delegate (ie. in tests).
  const ShelfItem& item = model_->items()[last_pressed_index_.value()];
  if (!model_->GetShelfItemDelegate(item.id)) {
    AfterItemSelected(item, sender, event.Clone(), ink_drop, SHELF_ACTION_NONE,
                      {});
    return;
  }

  // Notify the item of its selection; handle the result in AfterItemSelected.
  item_awaiting_response_ = item.id;
  model_->GetShelfItemDelegate(item.id)->ItemSelected(
      event.Clone(), GetDisplayIdForView(this), LAUNCH_FROM_SHELF,
      base::BindOnce(&ShelfView::AfterItemSelected, weak_factory_.GetWeakPtr(),
                     item, sender, event.Clone(), ink_drop),
      base::BindRepeating(&ShouldIncludeMenuItem));
}

bool ShelfView::IsShowingMenuForView(const views::View* view) const {
  return IsShowingMenu() &&
         shelf_menu_model_adapter_->IsShowingMenuForView(*view);
}

////////////////////////////////////////////////////////////////////////////////
// ShelfView, FocusTraversable implementation:

views::FocusSearch* ShelfView::GetFocusSearch() {
  return focus_search_;
}

////////////////////////////////////////////////////////////////////////////////
// ShelfView, AccessiblePaneView implementation:

views::View* ShelfView::GetDefaultFocusableChild() {
  return default_last_focusable_child_ ? FindLastFocusableChild()
                                       : FindFirstFocusableChild();
}

void ShelfView::ShowContextMenuForViewImpl(
    views::View* source,
    const gfx::Point& point,
    ui::mojom::MenuSourceType source_type) {
  // Prevent concurrent requests that may show application or context menus.
  const ShelfItem* item = ShelfItemForView(source);
  if (!item_awaiting_response_.IsNull()) {
    if (item && item->id != item_awaiting_response_) {
      views::InkDrop::Get(source)->AnimateToState(
          views::InkDropState::DEACTIVATED, nullptr);
    }
    return;
  }
  last_pressed_index_ = std::nullopt;
  if (!item || !model_->GetShelfItemDelegate(item->id)) {
    ShowShelfContextMenu(ShelfID(), point, source, source_type, nullptr);
    return;
  }

  item_awaiting_response_ = item->id;
  context_menu_callback_.Reset(base::BindOnce(
      &ShelfView::ShowShelfContextMenu, weak_factory_.GetWeakPtr(), item->id,
      point, source, source_type));

  const int64_t display_id = GetDisplayIdForView(this);
  model_->GetShelfItemDelegate(item->id)->GetContextMenu(
      display_id, context_menu_callback_.callback());
}

void ShelfView::OnShelfConfigUpdated() {
  // Ensure the shelf app buttons have an icon which is up to date with the
  // current ShelfConfig sizing.
  for (size_t i = 0; i < view_model_->view_size(); i++) {
    ShelfAppButton* button =
        static_cast<ShelfAppButton*>(view_model_->view_at(i));
    if (!button->IsIconSizeCurrent())
      ShelfItemChanged(i, model_->items()[i]);
  }
}

bool ShelfView::ShouldEventActivateButton(View* view, const ui::Event& event) {
  // This only applies to app buttons.
  DCHECK(views::IsViewClass<ShelfAppButton>(view));
  if (dragging())
    return false;

  // Ignore if we are already in a pointer event sequence started with a repost
  // event on the same shelf item. See crbug.com/343005 for more detail.
  if (is_repost_event_on_same_item_)
    return false;

  // Don't activate the item twice on double-click. Otherwise the window starts
  // animating open due to the first click, then immediately minimizes due to
  // the second click. The user most likely intended to open or minimize the
  // item once, not do both.
  if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
    return false;

  const bool repost = IsRepostEvent(event);

  // Ignore if this is a repost event on the last pressed shelf item.
  auto index = view_model_->GetIndexOfView(view);
  if (!index.has_value())
    return false;
  return !repost || last_pressed_index_ != index;
}

bool ShelfView::IsDraggedView(const views::View* view) const {
  return drag_view_ == view;
}

views::View* ShelfView::FindFirstFocusableChild() {
  if (visible_views_indices_.empty())
    return nullptr;
  return view_model_->view_at(visible_views_indices_.front());
}

views::View* ShelfView::FindLastFocusableChild() {
  if (visible_views_indices_.empty())
    return nullptr;
  return view_model_->view_at(visible_views_indices_.back());
}

views::View* ShelfView::FindFirstOrLastFocusableChild(bool last) {
  return last ? FindLastFocusableChild() : FindFirstFocusableChild();
}

bool ShelfView::HandleGestureEvent(const ui::GestureEvent* event) {
  // Avoid changing |event|'s location since |event| may be received by post
  // event handlers.
  ui::GestureEvent copy_event(*event);

  // Convert the event location from current view to screen, since swiping up on
  // the shelf can open the fullscreen app list. Updating the bounds of the app
  // list during dragging is based on screen coordinate space.
  gfx::Point location_in_screen(copy_event.location());
  View::ConvertPointToScreen(this, &location_in_screen);
  copy_event.set_location(location_in_screen);

  if (shelf_->ProcessGestureEvent(copy_event))
    return true;

  return false;
}

bool ShelfView::ShouldShowTooltipForChildView(
    const views::View* child_view) const {
  DCHECK_EQ(this, child_view->parent());

  // Don't show a tooltip for a view that's currently being dragged.
  if (child_view == drag_view_)
    return false;

  return ShelfItemForView(child_view) && !IsShowingMenuForView(child_view);
}

// static
void ShelfView::ConfigureChildView(views::View* view,
                                   ui::LayerType layer_type) {
  if (view->layer()) {
    return;
  }
  view->SetPaintToLayer(layer_type);
  view->layer()->SetFillsBoundsOpaquely(false);
}

void ShelfView::CalculateIdealBounds() {
  DCHECK(static_cast<size_t>(model()->item_count()) ==
         view_model_->view_size());

  const int button_spacing = ShelfConfig::Get()->button_spacing();
  UpdateSeparatorIndex();

  // Don't show the separator if it isn't needed, or would appear after all
  // visible items.
  separator_->SetVisible(separator_index_.has_value() &&
                         separator_index_ < visible_views_indices_.back());
  // Set |separator_index_| to nullopt if it is not visible.
  if (!separator_->GetVisible())
    separator_index_ = std::nullopt;

  app_icons_layout_offset_ = CalculateAppIconsLayoutOffset();
  int x = shelf()->PrimaryAxisValue(app_icons_layout_offset_, 0);
  int y = shelf()->PrimaryAxisValue(0, app_icons_layout_offset_);

  // The padding is handled in ScrollableShelfView.

  const int button_size = GetButtonSize();
  for (size_t i = 0; i < view_model_->view_size(); ++i) {
    if (view_model_->view_at(i)->GetVisible()) {
      gfx::Rect ideal_view_bounds(x, y, button_size, button_size);
      view_model_->set_ideal_bounds(i, ideal_view_bounds);
      if (view_model_->view_at(i) == drag_view_ &&
          current_ghost_view_index_ != i && !dragged_off_shelf_) {
        if (current_ghost_view_)
          current_ghost_view_->FadeOut();

        last_ghost_view_ = current_ghost_view_;

        auto current_ghost_view = std::make_unique<GhostImageView>(GridIndex());
        gfx::Size icon_size = drag_view_
                                  ->GetIdealIconBounds(ideal_view_bounds.size(),
                                                       /*icon_scale=*/1.0f)
                                  .size();
        gfx::Rect ghost_view_bounds = ideal_view_bounds;

        // Ensure that the ghost_view_bounds are a square that encloses the
        // icon_size with the same center. The ghost view should draw as a
        // circle.
        const int icon_width = std::min(icon_size.width(), icon_size.height());
        ghost_view_bounds.ClampToCenteredSize(
            gfx::Size(icon_width, icon_width));

        current_ghost_view->Init(ghost_view_bounds,
                                 ghost_view_bounds.width() / 2);

        current_ghost_view_ = AddChildView(std::move(current_ghost_view));
        current_ghost_view_->FadeIn();
        current_ghost_view_index_ = i;
      }
      x = shelf()->PrimaryAxisValue(x + button_size + button_spacing, x);
      y = shelf()->PrimaryAxisValue(y, y + button_size + button_spacing);
    } else {
      view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
    }
  }
}

views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
  views::View* view = nullptr;
  switch (item.type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
    case TYPE_DIALOG: {
      ShelfAppButton* button = new ShelfAppButton(
          this, shelf_button_delegate_ ? shelf_button_delegate_.get() : this);
      UpdateButton(button, item);
      view = button;
      break;
    }

    case TYPE_UNDEFINED:
      return nullptr;
  }

  view->set_context_menu_controller(this);

  ConfigureChildView(view, ui::LAYER_NOT_DRAWN);
  return view;
}

void ShelfView::UpdateButton(ShelfAppButton* button, const ShelfItem& item) {
  button->ReflectItemStatus(item);
  button->SetMainAndMaybeHostBadgeImage(item.image, item.has_placeholder_icon,
                                        item.badge_image);
  button->SetNotificationBadgeColor(item.notification_badge_color);
  button->GetViewAccessibility().SetName(item.accessible_name);
  // If an empty accessible name of the item is provided, in such a case we want
  // the cache to have the name corresponding to the implementation in
  // `UpdateAccessibleName`
  button->UpdateAccessibleName();
  button->SchedulePaint();
}

int ShelfView::GetAvailableSpaceForAppIcons() const {
  return shelf()->PrimaryAxisValue(width(), height());
}

void ShelfView::UpdateSeparatorIndex() {
  // A separator is shown after the last pinned item only if it's followed by a
  // visible app item.
  std::optional<size_t> first_unpinned_index = std::nullopt;
  std::optional<size_t> last_pinned_index = std::nullopt;

  std::optional<size_t> dragged_item_index = std::nullopt;
  if (drag_view_)
    dragged_item_index = view_model_->GetIndexOfView(drag_view_);

  const bool can_drag_view_across_separator =
      drag_view_ && CanDragAcrossSeparator(drag_view_);

  for (size_t i : base::Reversed(visible_views_indices_)) {
    const auto& item = model()->items()[i];
    if (IsItemPinned(item)) {
      // The dragged item is temporarily moved to the end of the shelf if it is
      // ripped off by dragging. Ignore this case and continue to find the
      // correct separator index.
      if (dragged_off_shelf_ &&
          i == static_cast<size_t>(model()->item_count() - 1)) {
        continue;
      }

      last_pinned_index = i;
      break;
    }

    if (!IsPinnedShelfItemType(item.type) && item.is_on_active_desk) {
      first_unpinned_index = i;
    }
  }

  // If there is no unpinned item in shelf, return -1 as the separator should be
  // hidden.
  if (!first_unpinned_index.has_value()) {
    separator_index_ = std::nullopt;
    return;
  }

  // If the dragged item is between the pinned apps and unpinned apps, move it
  // to the pinned app side if it is closer to the pinned section compared to
  // its ideal bounds.
  if (can_drag_view_across_separator &&
      last_pinned_index < dragged_item_index &&
      dragged_item_index <= first_unpinned_index &&
      drag_view_relative_to_ideal_bounds_ == RelativePosition::kLeft) {
    separator_index_ = dragged_item_index;
    return;
  }

  separator_index_ = last_pinned_index;
}

bool ShelfView::ShouldHandleDrag(const std::string& app_id,
                                 const gfx::Point& location_in_screen) const {
  // Remote Apps are not pinnable.
  if (IsRemoteApp(app_id))
    return false;

  // Do not handle drag if an operation for another app is already in progress,
  // or the cursor is not inside the shelf bounds. This could happen if mouse /
  // touch operations overlap.
  return !app_id.empty() &&
         (drag_and_drop_shelf_id_.IsNull() ||
          drag_and_drop_shelf_id_.app_id == app_id) &&
         GetBoundsInScreen().Contains(location_in_screen);
}

bool ShelfView::StartDrag(const std::string& app_id,
                          const gfx::Point& location_in_screen,
                          const gfx::Rect& drag_icon_bounds_in_screen) {
  // Don't start a drag if another one is in progress.
  if (!drag_and_drop_shelf_id_.IsNull())
    return false;

  if (!ShouldHandleDrag(app_id, location_in_screen))
    return false;

  DCHECK(!is_active_drag_and_drop_host_);
  is_active_drag_and_drop_host_ = true;

  // If the AppsGridView (which was dispatching this event) was opened by our
  // button, ShelfView dragging operations are locked and we have to unlock.
  CancelDrag(std::nullopt);
  drag_and_drop_item_pinned_ = false;
  drag_and_drop_shelf_id_ = ShelfID(app_id);
  // Check if the application is pinned - if not, we have to pin it so
  // that we can re-arrange the shelf order accordingly. Note that items have
  // to be pinned to give them the same (order) possibilities as a shortcut.
  if (!model_->IsAppPinned(app_id)) {
    ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);

    if (model_->ItemIndexByAppID(app_id) >= 0) {
      model_->PinExistingItemWithID(app_id);
    } else {
      model_->AddAndPinAppWithFactoryConstructedDelegate(app_id);
      drag_and_drop_item_pinned_ = true;
    }
  }

  views::View* drag_and_drop_view =
      view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
  DCHECK(drag_and_drop_view);

  // Since there is already an icon presented by the caller, we hide this item
  // for now. That has to be done by reducing the size since the visibility will
  // change once a regrouping animation is performed.
  pre_drag_and_drop_size_ = drag_and_drop_view->size();
  drag_and_drop_view->SetSize(gfx::Size());

  // First we have to center the mouse cursor over the item.
  const gfx::Point start_point_in_screen =
      drag_and_drop_view->GetBoundsInScreen().CenterPoint();
  gfx::Point pt = start_point_in_screen;
  views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
  gfx::Point point_in_root = start_point_in_screen;
  wm::ConvertPointFromScreen(window_util::GetRootWindowAt(location_in_screen),
                             &point_in_root);
  ui::MouseEvent event(ui::EventType::kMousePressed, pt, point_in_root,
                       ui::EventTimeForNow(), 0, 0);
  PointerPressedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);

  // Drag the item where it really belongs.
  Drag(location_in_screen, drag_icon_bounds_in_screen);
  return true;
}

bool ShelfView::Drag(const gfx::Point& location_in_screen,
                     const gfx::Rect& drag_icon_bounds_in_screen) {
  if (drag_and_drop_shelf_id_.IsNull() ||
      !GetBoundsInScreen().Contains(location_in_screen))
    return false;

  drag_icon_bounds_in_screen_ = drag_icon_bounds_in_screen;
  gfx::Point pt = location_in_screen;
  views::View* drag_and_drop_view =
      view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
  ConvertPointFromScreen(drag_and_drop_view, &pt);
  gfx::Point point_in_root = location_in_screen;
  wm::ConvertPointFromScreen(window_util::GetRootWindowAt(location_in_screen),
                             &point_in_root);
  ui::MouseEvent event(ui::EventType::kMouseDragged, pt, point_in_root,
                       ui::EventTimeForNow(), 0, 0);
  PointerDraggedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);
  return true;
}

void ShelfView::EndDrag(bool cancel) {
  drag_scroll_dir_ = 0;
  scrolling_timer_.Stop();
  speed_up_drag_scrolling_.Stop();

  if (drag_and_drop_shelf_id_.IsNull()) {
    is_active_drag_and_drop_host_ = false;
    return;
  }

  views::View* drag_and_drop_view =
      view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
  PointerReleasedOnButton(drag_and_drop_view, DRAG_AND_DROP, cancel);

  // Either destroy the temporarily created item - or - make the item visible.
  if (drag_and_drop_item_pinned_ && cancel) {
    ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
    model_->UnpinAppWithID(drag_and_drop_shelf_id_.app_id);
  } else if (drag_and_drop_view) {
    std::unique_ptr<gfx::AnimationDelegate> animation_delegate;

    // Resets the dragged view's opacity at the end of drag. Otherwise, if
    // the app is already pinned on shelf before drag starts, the dragged view
    // will be invisible when drag ends.
    animation_delegate =
        std::make_unique<StartFadeAnimationDelegate>(this, drag_and_drop_view);

    if (cancel) {
      // When a hosted drag gets canceled, the item can remain in the same slot
      // and it might have moved within the bounds. In that case the item need
      // to animate back to its correct location.
      AnimateToIdealBounds();
      // TODO(crbug.com/40266934): Remove the check below once the bounds
      // animator works better with zero animation duration.
      if (!bounds_animator_->GetAnimationDuration().is_zero()) {
        bounds_animator_->SetAnimationDelegate(drag_and_drop_view,
                                               std::move(animation_delegate));
      }

    } else {
      drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
    }
  }
  drag_icon_bounds_in_screen_ = gfx::Rect();
  drag_and_drop_shelf_id_ = ShelfID();
  is_active_drag_and_drop_host_ = false;
}

void ShelfView::SwapButtons(views::View* button_to_swap, bool with_next) {
  if (!button_to_swap)
    return;

  // Find the index of the button to swap in the view model.
  size_t src_index = static_cast<size_t>(-1);
  for (size_t i = 0; i < view_model_->view_size(); ++i) {
    View* view = view_model_->view_at(i);
    if (view == button_to_swap) {
      src_index = i;
      break;
    }
  }

  // Swapping items in the model is sufficient, everything will then be
  // reflected in the views.
  if (model_->Swap(src_index, with_next)) {
    AnimateToIdealBounds();
    const ShelfItem src_item = model_->items()[src_index];
    const ShelfItem dst_item =
        model_->items()[src_index + (with_next ? 1 : -1)];
    AnnounceSwapEvent(src_item, dst_item);
  }
}

void ShelfView::PointerPressedOnButton(views::View* view,
                                       Pointer pointer,
                                       const ui::LocatedEvent& event) {
  if (drag_view_)
    return;

  if (IsShowingMenu())
    shelf_menu_model_adapter_->Cancel();

  auto index = view_model_->GetIndexOfView(view);
  if (!index.has_value() || view_model_->view_size() < 1)
    return;  // View is being deleted, ignore request.

  // Reset drag icon proxy from previous drag (which could be set if the drop
  // animation is still in progress), as drag icon proxy is not expected to be
  // reused after it starts animating out.
  drag_icon_proxy_.reset();
  drag_image_layer_.reset();

  // Only when the repost event occurs on the same shelf item, we should ignore
  // the call in ShelfView::ButtonPressed(...).
  is_repost_event_on_same_item_ =
      IsRepostEvent(event) && (last_pressed_index_ == index);

  CHECK(views::IsViewClass<ShelfAppButton>(view));
  drag_view_ = static_cast<ShelfAppButton*>(view);
  drag_origin_ = gfx::Point(event.x(), event.y());
  UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
                            static_cast<ShelfAlignmentUmaEnumValue>(
                                shelf_->SelectValueForShelfAlignment(
                                    SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
                                    SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
                                    SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT)),
                            SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
}

void ShelfView::PointerDraggedOnButton(const views::View* view,
                                       Pointer pointer,
                                       const ui::LocatedEvent& event) {
  if (CanPrepareForDrag(pointer, event))
    PrepareForDrag(pointer, event);

  if (drag_pointer_ == pointer)
    ContinueDrag(event);
}

void ShelfView::PointerReleasedOnButton(const views::View* view,
                                        Pointer pointer,
                                        bool canceled) {
  drag_scroll_dir_ = 0;
  scrolling_timer_.Stop();
  speed_up_drag_scrolling_.Stop();

  is_repost_event_on_same_item_ = false;

  if (canceled) {
    CancelDrag(std::nullopt);
  } else if (drag_pointer_ == pointer) {
    FinalizeRipOffDrag(false);
    drag_pointer_ = NONE;

    // Check if the pin status of |drag_view_| should be changed when
    // |drag_view_| is dragged over the separator. Do nothing if |drag_view_| is
    // already handled in FinalizedRipOffDrag.
    if (drag_view_) {
      if (ShouldUpdateDraggedViewPinStatus(
              view_model_->GetIndexOfView(view).value())) {
        const std::string drag_app_id = ShelfItemForView(drag_view_)->id.app_id;
        ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
        if (model_->IsAppPinned(drag_app_id)) {
          model_->UnpinAppWithID(drag_app_id);
        } else {
          model_->PinExistingItemWithID(drag_app_id);
        }
      }
    }
    AnimateToIdealBounds();
  }

  if (drag_pointer_ != NONE)
    return;

  delegate_->CancelScrollForItemDrag();

  if (!drag_view_ || dragged_off_shelf_) {
    drag_icon_proxy_.reset();
    drag_image_layer_.reset();
  }

  const gfx::Rect target_bounds_in_screen =
      CalculateDropTargetBoundsForDragViewInScreen();

  if (drag_icon_proxy_) {
    AnimateDragIconProxy(target_bounds_in_screen);
  } else if (drag_image_layer_) {
    AnimateDragImageLayer(target_bounds_in_screen);
  } else if (drag_view_) {
    drag_view_->layer()->SetOpacity(1.0f);
  }

  // If the drag pointer is NONE, no drag operation is going on and the
  // |drag_view_| can be released.
  drag_view_ = nullptr;
  drag_view_relative_to_ideal_bounds_ = RelativePosition::kNotAvailable;
  RemoveGhostView();
}

void ShelfView::AnimateDragImageLayer(
    const gfx::Rect& target_bounds_in_screen) {
  DCHECK(drag_image_layer_);

  if (!delegate_->AreBoundsWithinVisibleSpace(target_bounds_in_screen)) {
    drag_image_layer_.reset();
    drag_view_->layer()->SetOpacity(1.0f);
    return;
  }

  ui::Layer* target_layer = drag_image_layer_->root();
  if (target_layer) {
    target_layer->GetAnimator()->AbortAllAnimations();

    gfx::Rect current_bounds = target_layer->bounds();
    if (current_bounds.IsEmpty()) {
      OnDragIconProxyAnimatedOut(
          std::make_unique<ViewOpacityResetter>(drag_view_));
      return;
    }

    // |target_layer| bounds are in display coordinates.
    display::Display display =
        display::Screen::GetScreen()->GetDisplayNearestWindow(
            GetWidget()->GetNativeWindow());
    current_bounds.Offset(display.bounds().OffsetFromOrigin());

    const gfx::Transform transform = gfx::TransformBetweenRects(
        gfx::RectF(current_bounds), gfx::RectF(target_bounds_in_screen));

    views::AnimationBuilder builder;
    builder.SetPreemptionStrategy(ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET)
        .OnEnded(base::BindOnce(
            &ShelfView::OnDragIconProxyAnimatedOut, weak_factory_.GetWeakPtr(),
            std::make_unique<ViewOpacityResetter>(drag_view_)))
        .OnAborted(base::BindOnce(
            &ShelfView::OnDragIconProxyAnimatedOut, weak_factory_.GetWeakPtr(),
            std::make_unique<ViewOpacityResetter>(drag_view_)))
        .Once()
        .SetDuration(base::Milliseconds(200))
        .SetTransform(target_layer, transform, gfx::Tween::FAST_OUT_LINEAR_IN);
  }
}

void ShelfView::AnimateDragIconProxy(const gfx::Rect& target_bounds_in_screen) {
  DCHECK(drag_icon_proxy_);

  if (!delegate_->AreBoundsWithinVisibleSpace(target_bounds_in_screen)) {
    drag_icon_proxy_.reset();
    drag_view_->layer()->SetOpacity(1.0f);
    return;
  }

  drag_icon_proxy_->AnimateToBoundsAndCloseWidget(
      target_bounds_in_screen,
      base::BindOnce(&ShelfView::OnDragIconProxyAnimatedOut,
                     base::Unretained(this),
                     std::make_unique<ViewOpacityResetter>(drag_view_)));
}

gfx::Rect ShelfView::CalculateDropTargetBoundsForDragViewInScreen() {
  if (!drag_view_) {
    return gfx::Rect();
  }

  const gfx::Rect drag_view_ideal_bounds = view_model_->ideal_bounds(
      view_model_->GetIndexOfView(drag_view_).value());
  gfx::Rect target_bounds_in_screen =
      drag_view_->GetIdealIconBounds(drag_view_ideal_bounds.size(),
                                     /*icon_scale=*/1.0f);
  target_bounds_in_screen.Offset(drag_view_ideal_bounds.x(),
                                 drag_view_ideal_bounds.y());
  target_bounds_in_screen = GetMirroredRect(target_bounds_in_screen);
  views::View::ConvertRectToScreen(this, &target_bounds_in_screen);

  return target_bounds_in_screen;
}

void ShelfView::OnDragIconProxyAnimatedOut(
    std::unique_ptr<ViewOpacityResetter> opacity_resetter) {
  opacity_resetter->Run();
  drag_icon_proxy_.reset();
  drag_image_layer_.reset();
}

void ShelfView::LayoutToIdealBounds() {
  if (bounds_animator_->IsAnimating()) {
    AnimateToIdealBounds();
    return;
  }

  CalculateIdealBounds();
  views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
  UpdateSeparatorBounds(/*animate=*/false);
  UpdateVisibleShelfItemBoundsUnion();

  // Notify user education features that anchor bounds have changed.
  if (features::IsUserEducationEnabled()) {
    views::ElementTrackerViews::GetInstance()->NotifyCustomEvent(
        user_education::kHelpBubbleAnchorBoundsChangedEvent, this);
  }
}

bool ShelfView::IsItemPinned(const ShelfItem& item) const {
  return IsPinnedShelfItemType(item.type);
}

bool ShelfView::IsItemVisible(const ShelfItem& item) const {
  return IsItemPinned(item) || item.is_on_active_desk;
}

void ShelfView::OnTabletModeChanged() {
  // The layout change will happen as part of shelf config update.
}

void ShelfView::AnimateToIdealBounds() {
  CalculateIdealBounds();

  move_animation_tracker_.emplace(
      GetWidget()->GetCompositor()->RequestNewCompositorMetricsTracker());
  move_animation_tracker_->Start(metrics_util::ForSmoothnessV3(
      base::BindRepeating(&ReportMoveAnimationSmoothness)));

  for (size_t i = 0; i < view_model_->view_size(); ++i) {
    View* view = view_model_->view_at(i);
    bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
    // Now that the item animation starts, we have to make sure that the
    // padding of the first gets properly transferred to the new first item.
    view->SetBorder(nullptr);
  }

  UpdateSeparatorBounds(/*animate=*/true);
  UpdateVisibleShelfItemBoundsUnion();
}

void ShelfView::UpdateSeparatorBounds(bool animate) {
  // The `- 1` is because we expect at least one item after the separator.
  if (!separator_index_.has_value() ||
      separator_index_ >= view_model_->view_size() - 1) {
    return;
  }

  // The ` + 1` is because we compute the separator position as an offset
  // leftward from the item just after the separator.
  gfx::Rect icon_bounds_beside_separator =
      view_model_->ideal_bounds(separator_index_.value() + 1);

  // Calculate the position value on the secondary axis.
  int secondary_offset =
      (shelf_->hotseat_widget()->GetHotseatSize() - kSeparatorSize) / 2;

  // Because we will be subtracting half the button spacing, round it up to
  // favor leftward or upward.
  const int half_button_spacing_rounded_up =
      (ShelfConfig::Get()->button_spacing() + 1) / 2;
  const int separator_x = shelf()->PrimaryAxisValue(
      icon_bounds_beside_separator.x() - half_button_spacing_rounded_up,
      secondary_offset);
  const int separator_y = shelf()->PrimaryAxisValue(
      secondary_offset,
      icon_bounds_beside_separator.y() - half_button_spacing_rounded_up);
  gfx::Rect separator_bounds(
      separator_x, separator_y,
      shelf()->PrimaryAxisValue(kSeparatorThickness, kSeparatorSize),
      shelf()->PrimaryAxisValue(kSeparatorSize, kSeparatorThickness));

  if (animate)
    bounds_animator_->AnimateViewTo(separator_, separator_bounds);
  else
    separator_->SetBoundsRect(separator_bounds);
}

void ShelfView::FadeIn(views::View* view) {
  view->SetVisible(true);
  view->layer()->SetOpacity(0);

  ui::ScopedLayerAnimationSettings fade_in_animation_settings(
      view->layer()->GetAnimator());
  fade_in_animation_settings.SetTweenType(gfx::Tween::EASE_OUT);
  fade_in_animation_settings.SetPreemptionStrategy(
      ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
  fade_in_animation_settings.AddObserver(fade_in_animation_delegate_.get());

  ui::AnimationThroughputReporter reporter(
      fade_in_animation_settings.GetAnimator(),
      metrics_util::ForSmoothnessV3(
          base::BindRepeating(&ReportFadeInAnimationSmoothness)));

  view->layer()->SetOpacity(1.f);
}

void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
  DCHECK(!dragging());
  DCHECK(drag_view_);
  drag_pointer_ = pointer;
  start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
  drag_scroll_dir_ = 0;

  if (!start_drag_index_.has_value()) {
    CancelDrag(std::nullopt);
    return;
  }

  // Cancel in-flight request for app item context menu model (made when app
  // context menu is requested), to prevent the pending callback from showing
  // a context menu just after drag starts.
  if (!context_menu_callback_.IsCancelled()) {
    GetShelfAppButton(item_awaiting_response_)
        ->OnContextMenuModelRequestCanceled();
    ResetActiveMenuModelRequest();
  }

  // Move the view to the front so that it appears on top of other views.
  ReorderChildView(drag_view_, children().size());
  bounds_animator_->StopAnimatingView(drag_view_);

  drag_view_->OnDragStarted(&event);

  // Drag icon proxy from previous drag may be around if the icon is still
  // animating to the final position. Reset it here to cancel the animation.
  drag_icon_proxy_.reset();
  drag_image_layer_.reset();
  delegate_->CancelScrollForItemDrag();

  drag_view_->layer()->SetOpacity(0.0f);
  if (!is_active_drag_and_drop_host_) {
    aura::Window* root_window = GetWidget()->GetNativeWindow()->GetRootWindow();
    gfx::Point screen_location = event.root_location();
    ::wm::ConvertPointToScreen(root_window, &screen_location);

    const gfx::ImageSkia icon_image =
        drag_view_->GetIconImage(kDragAndDropProxyScale);
    drag_icon_proxy_ = std::make_unique<AppDragIconProxy>(
        root_window, icon_image,
        drag_view_->GetBadgeIconImage(kDragAndDropProxyScale), screen_location,
        gfx::Vector2d(), /*scale_factor=*/1.0f,
        /*is_folder_icon=*/false, icon_image.size());

    if (pointer == MOUSE) {
      chromeos::haptics_util::PlayHapticTouchpadEffect(
          ui::HapticTouchpadEffect::kTick,
          ui::HapticTouchpadEffectStrength::kMedium);
    }
  }
}

void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
  DCHECK(dragging());
  DCHECK(drag_view_);
  const auto index = view_model_->GetIndexOfView(drag_view_);
  DCHECK(index.has_value());

  const bool dragged_off_shelf_before = dragged_off_shelf_;

  // Handle rip off functionality if this is not a drag and drop host operation
  // and not the app list item.
  if (drag_and_drop_shelf_id_.IsNull() &&
      RemovableByRipOff(index.value()) != NOT_REMOVABLE) {
    HandleRipOffDrag(event);
    // Check if the item got ripped off the shelf - if it did we are done.
    if (dragged_off_shelf_) {
      drag_scroll_dir_ = 0;
      scrolling_timer_.Stop();
      speed_up_drag_scrolling_.Stop();
      if (!dragged_off_shelf_before)
        model_->OnItemRippedOff();
      return;
    }
  }

  // Calculates the drag point in screen before MoveDragViewTo is called.
  gfx::Point drag_point_in_screen(event.location());
  ConvertPointToScreen(drag_view_, &drag_point_in_screen);

  gfx::Point drag_point(event.location());
  ConvertPointToTarget(drag_view_, this, &drag_point);
  MoveDragViewTo(shelf_->PrimaryAxisValue(drag_point.x() - drag_origin_.x(),
                                          drag_point.y() - drag_origin_.y()));

  if (drag_icon_proxy_)
    drag_icon_proxy_->UpdatePosition(drag_point_in_screen);

  // If drag_icon_proxy_ is available, get its item bounds, otherwise (in case
  // of an ApplicationDragAndDropHost drag), last drag icon bounds are cached in
  // `drag_icon_bounds_in_screen_`.
  const gfx::Rect drag_icon_bounds_in_screen =
      drag_icon_proxy_ ? drag_icon_proxy_->GetBoundsInScreen()
                       : drag_icon_bounds_in_screen_;
  delegate_->ScheduleScrollForItemDragIfNeeded(drag_icon_bounds_in_screen);

  if (dragged_off_shelf_before) {
    model_->OnItemReturnedFromRipOff(
        static_cast<int>(view_model_->GetIndexOfView(drag_view_).value()));
  }
}

void ShelfView::MoveDragViewTo(int primary_axis_coordinate) {
  const size_t current_item_index =
      view_model_->GetIndexOfView(drag_view_).value();
  const std::pair<size_t, size_t> indices(GetDragRange(current_item_index));
  if (shelf_->IsHorizontalAlignment()) {
    int x = GetMirroredXWithWidthInView(primary_axis_coordinate,
                                        drag_view_->width());
    x = std::max(view_model_->ideal_bounds(indices.first).x(), x);
    x = std::min(view_model_->ideal_bounds(indices.second).right() -
                     view_model_->ideal_bounds(current_item_index).width(),
                 x);
    if (drag_view_->x() != x)
      drag_view_->SetX(x);
  } else {
    int y = std::max(view_model_->ideal_bounds(indices.first).y(),
                     primary_axis_coordinate);
    y = std::min(view_model_->ideal_bounds(indices.second).bottom() -
                     view_model_->ideal_bounds(current_item_index).height(),
                 y);
    if (drag_view_->y() != y)
      drag_view_->SetY(y);
  }

  size_t target_index = views::ViewModelUtils::DetermineMoveIndex(
      *view_model_, drag_view_, shelf_->IsHorizontalAlignment(),
      drag_view_->x(), drag_view_->y());
  target_index = std::clamp(target_index, indices.first, indices.second);

  // Check the relative position of |drag_view_| and its ideal bounds if it can
  // be dragged across the separator to pin.
  if (CanDragAcrossSeparator(drag_view_)) {
    // Compare the center points of |drag_view_| and its ideal bounds to
    // determine whether the separator should be moved to the left or right by
    // using |drag_view_relative_to_ideal_bounds_|. The actual position will
    // be updated in CalculateIdealBounds.
    gfx::Point drag_view_center = drag_view_->bounds().CenterPoint();
    int drag_view_position =
        shelf()->PrimaryAxisValue(drag_view_center.x(), drag_view_center.y());
    gfx::Point ideal_bound_center =
        view_model_->ideal_bounds(target_index).CenterPoint();
    int ideal_bound_position = shelf()->PrimaryAxisValue(
        ideal_bound_center.x(), ideal_bound_center.y());

    RelativePosition old_relative_position =
        drag_view_relative_to_ideal_bounds_;
    drag_view_relative_to_ideal_bounds_ =
        drag_view_position < ideal_bound_position ? RelativePosition::kLeft
                                                  : RelativePosition::kRight;
    if (target_index == current_item_index &&
        old_relative_position != drag_view_relative_to_ideal_bounds_) {
      AnimateToIdealBounds();
      NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
                               true /* send_native_event */);
    }
  }

  if (target_index == current_item_index)
    return;
  // Change the model if the dragged item index is changed, the ShelfItemMoved()
  // callback will handle the |view_model_| update.
  model_->Move(current_item_index, target_index);
  bounds_animator_->StopAnimatingView(drag_view_);
}

void ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
  auto current_index = view_model_->GetIndexOfView(drag_view_);
  DCHECK(current_index.has_value());
  std::string dragged_app_id = model_->items()[current_index.value()].id.app_id;

  aura::Window* root_window = GetWidget()->GetNativeWindow()->GetRootWindow();
  gfx::Point screen_location = event.root_location();
  ::wm::ConvertPointToScreen(root_window, &screen_location);

  // To avoid ugly forwards and backwards flipping we use different constants
  // for ripping off / re-inserting the items.
  if (dragged_off_shelf_) {
    // If the shelf/overflow bubble bounds contains |screen_location| we insert
    // the item back into the shelf.
    if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
      if (!is_active_drag_and_drop_host_) {
        const gfx::ImageSkia icon_image =
            drag_view_->GetIconImage(kDragAndDropProxyScale);
        drag_icon_proxy_ = std::make_unique<AppDragIconProxy>(
            root_window, icon_image,
            drag_view_->GetBadgeIconImage(kDragAndDropProxyScale),
            screen_location,
            /*cursor_offset_from_center=*/gfx::Vector2d(),
            /*scale_factor=*/1.0f,
            /*is_folder_icon=*/false, icon_image.size());
      }

      // Re-insert the item and return simply false since the caller will handle
      // the move as in any normal case.
      dragged_off_shelf_ = false;

      // After re-insertion, trigger an animation to ideal bounds to show the
      // ghost view.
      AnimateToIdealBounds();

      return;
    }
    if (drag_icon_proxy_) {
      drag_icon_proxy_->UpdatePosition(screen_location);
    }
    return;
  }

  // Mark the item as dragged off the shelf if the drag distance exceeds
  // `rip_off_distance`.
  int rip_off_distance =
      ShelfConfig::Get()->shelf_size() * kRipOffDistanceFactor;
  int delta = CalculateShelfDistance(screen_location);
  bool dragged_off_shelf = delta > rip_off_distance;

  if (dragged_off_shelf) {
    if (!is_active_drag_and_drop_host_) {
      // Create a new, scaled up drag icon proxy when the item is dragged off
      // shelf - keep cursor position consistent with the  host provided icon.
      const gfx::Point center = drag_view_->GetLocalBounds().CenterPoint();
      const gfx::Vector2d cursor_offset_from_center = drag_origin_ - center;
      const gfx::ImageSkia icon_image =
          drag_view_->GetIconImage(kDragAndDropProxyScale);
      drag_icon_proxy_ = std::make_unique<AppDragIconProxy>(
          root_window, icon_image,
          drag_view_->GetBadgeIconImage(kDragAndDropProxyScale),
          screen_location, cursor_offset_from_center, /*scale_factor=*/1.0f,
          /*is_folder_icon=*/false, icon_image.size());
      delegate_->CancelScrollForItemDrag();
    }

    dragged_off_shelf_ = true;
    RemoveGhostView();

    if (RemovableByRipOff(current_index.value()) == REMOVABLE) {
      // Move the item to the back and hide it. ShelfItemMoved() callback will
      // handle the |view_model_| update and call AnimateToIdealBounds().
      if (current_index.value() !=
          static_cast<size_t>(model_->item_count() - 1)) {
        model_->Move(current_index.value(), model_->item_count() - 1);
      }
      // Make the item partially disappear to show that it will get removed if
      // dropped.
      if (drag_icon_proxy_) {
        drag_icon_proxy_->SetOpacity(kDraggedImageOpacity);
      }
    }
  }
}

void ShelfView::FinalizeRipOffDrag(bool cancel) {
  if (!dragged_off_shelf_)
    return;
  // Make sure we do not come in here again.
  dragged_off_shelf_ = false;

  // Coming here we should always have a |drag_view_|.
  DCHECK(drag_view_);

  delegate_->CancelScrollForItemDrag();

  auto current_index = view_model_->GetIndexOfView(drag_view_);
  // If the view isn't part of the model anymore, a sync operation must have
  // removed it. In that case we shouldn't change the model and only delete the
  // proxy image.
  if (!current_index.has_value()) {
    drag_icon_proxy_.reset();
    drag_image_layer_.reset();
    return;
  }

  // Set to true when the animation should snap back to where it was before.
  bool snap_back = false;
  // Items which cannot be dragged off will be handled as a cancel.
  if (!cancel) {
    if (RemovableByRipOff(current_index.value()) != REMOVABLE) {
      // Make sure we do not try to remove un-removable items like items which
      // were not pinned or have to be always there.
      cancel = true;
      snap_back = true;
    } else {
      // Make sure the item stays invisible upon removal.
      drag_view_->SetVisible(false);
      ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
      model_->UnpinAppWithID(model_->items()[current_index.value()].id.app_id);
    }
  }
  if (cancel || snap_back) {
    if (!cancelling_drag_model_changed_) {
      // Only do something if the change did not come through a model change.
      gfx::Rect drag_bounds = drag_icon_proxy_->GetBoundsInScreen();
      gfx::Point relative_to = GetBoundsInScreen().origin();
      gfx::Rect target(
          gfx::PointAtOffsetFromOrigin(drag_bounds.origin() - relative_to),
          drag_bounds.size());
      drag_view_->SetBoundsRect(target);
      // Hide the status from the active item since we snap it back now. Upon
      // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
      // is set.
      snap_back_from_rip_off_view_ = drag_view_;
      drag_view_->AddState(ShelfAppButton::STATE_HIDDEN);
      // When a canceling drag model is happening, the view model is diverged
      // from the menu model and movements / animations should not be done.
      model_->Move(current_index.value(), start_drag_index_.value());
      AnimateToIdealBounds();
    }
    drag_view_->layer()->SetOpacity(1.0f);
    model_->OnItemReturnedFromRipOff(model_->item_count() - 1);
  }
  drag_icon_proxy_.reset();
  drag_image_layer_.reset();
}

ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
  DCHECK(index >= 0 && index < model_->item_count());
  ShelfItemType type = model_->items()[index].type;
  if (type == TYPE_DIALOG)
    return NOT_REMOVABLE;

  if (model_->items()[index].pinned_by_policy)
    return NOT_REMOVABLE;

  // Note: Only pinned app shortcuts can be removed!
  const std::string& app_id = model_->items()[index].id.app_id;
  return (type == TYPE_PINNED_APP && model_->IsAppPinned(app_id)) ? REMOVABLE
                                                                  : DRAGGABLE;
}

bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
  if (IsPinnedShelfItemType(typea) && IsPinnedShelfItemType(typeb))
    return true;
  if (typea == TYPE_UNDEFINED || typeb == TYPE_UNDEFINED) {
    NOTREACHED() << "ShelfItemType must be set.";
  }
  // Running app or dialog.
  return typea == typeb;
}

bool ShelfView::ShouldFocusOut(bool reverse, views::View* button) {
  // The logic here seems backwards, but is actually correct. For instance if
  // the ShelfView's internal focus cycling logic attemmpts to focus the first
  // child after hitting Tab, we intercept that and instead, advance through
  // to the status area.
  return (reverse && button == FindLastFocusableChild()) ||
         (!reverse && button == FindFirstFocusableChild());
}

std::pair<size_t, size_t> ShelfView::GetDragRange(size_t index) {
  DCHECK(base::Contains(visible_views_indices_, index));
  const ShelfItem& dragged_item = model_->items()[index];

  // If |drag_view_| is allowed to be dragged across the separator, return the
  // first and the last index of the |visible_views_indices_|.
  if (CanDragAcrossSeparator(drag_view_)) {
    return std::make_pair(visible_views_indices_[0],
                          visible_views_indices_.back());
  }

  std::optional<size_t> first = std::nullopt;
  std::optional<size_t> last = std::nullopt;
  for (size_t i : visible_views_indices_) {
    if (SameDragType(model_->items()[i].type, dragged_item.type)) {
      if (!first.has_value())
        first = i;
      last = i;
    } else if (first.has_value()) {
      break;
    }
  }
  DCHECK(first.has_value());
  DCHECK(last.has_value());

  // TODO(afakhry): Consider changing this when taking into account inactive
  // desks.
  return std::make_pair(first.value(), last.value());
}

bool ShelfView::ShouldUpdateDraggedViewPinStatus(size_t dragged_view_index) {
  DCHECK(base::Contains(visible_views_indices_, dragged_view_index));
  bool is_moved_item_pinned =
      IsPinnedShelfItemType(model_->items()[dragged_view_index].type);
  if (!separator_index_.has_value()) {
    // If there is no |separator_index_|, all the apps in shelf are expected to
    // have the same pinned status.
    for (auto index : visible_views_indices_) {
      if (index != dragged_view_index) {
        // Return true if the pin status of the moved item is different from
        // others.
        return is_moved_item_pinned !=
               IsPinnedShelfItemType(model_->items()[index].type);
      }
    }
    return false;
  }
  // If the separator is shown, check whether the pin status of dragged item
  // matches the pin status implied by the dragged view position relative to the
  // separator.
  bool should_pinned_by_position =
      dragged_view_index <= separator_index_.value();
  return should_pinned_by_position != is_moved_item_pinned;
}

bool ShelfView::CanDragAcrossSeparator(views::View* drag_view) const {
  DCHECK(drag_view);

  // Only unpinned running apps on shelf can be dragged across the separator to
  // pin.
  bool can_change_pin_state = ShelfItemForView(drag_view)->type == TYPE_APP;

  // Note that |drag_and_drop_shelf_id_| is set only when the current drag view
  // is from app list, which can not be dragged to the unpinned app side.
  return !ShelfItemForView(drag_view)->IsPinStateForced() &&
         drag_and_drop_shelf_id_ == ShelfID() && can_change_pin_state;
}

void ShelfView::OnFadeInAnimationEnded() {
  // Call PreferredSizeChanged() to notify container to re-layout at the end
  // of fade-in animation.
  PreferredSizeChanged();
}

void ShelfView::OnFadeOutAnimationEnded() {
  if (fade_out_animation_tracker_) {
    fade_out_animation_tracker_->Stop();
    fade_out_animation_tracker_.reset();
  }

  // Call PreferredSizeChanged() to notify container to re-layout at the end
  // of removal animation.
  PreferredSizeChanged();

  AnimateToIdealBounds();
}

gfx::Rect ShelfView::GetMenuAnchorRect(const views::View& source,
                                       const gfx::Point& location,
                                       bool context_menu) const {
  // Application menus for items are anchored on the icon bounds.
  if (ShelfItemForView(&source) || !context_menu)
    return source.GetBoundsInScreen();

  gfx::Rect shelf_bounds_in_screen;
  if (ShelfConfig::Get()->is_in_app() &&
      display::Screen::GetScreen()->InTabletMode()) {
    // Use the shelf widget background as the menu anchor point in tablet mode
    // and in app.
    ShelfWidget* shelf_widget = shelf_->shelf_widget();
    shelf_bounds_in_screen = shelf_widget->GetOpaqueBackground()->bounds();
    const gfx::Rect widget_bounds =
        shelf_widget->GetRootView()->GetBoundsInScreen();
    shelf_bounds_in_screen.Offset(widget_bounds.x(), widget_bounds.y());
  } else {
    shelf_bounds_in_screen = GetBoundsInScreen();
  }

  gfx::Point origin;
  switch (shelf_->alignment()) {
    case ShelfAlignment::kBottom:
    case ShelfAlignment::kBottomLocked:
      origin = gfx::Point(location.x(), shelf_bounds_in_screen.y());
      break;
    case ShelfAlignment::kLeft:
      origin = gfx::Point(shelf_bounds_in_screen.right(), location.y());
      break;
    case ShelfAlignment::kRight:
      origin = gfx::Point(shelf_bounds_in_screen.x(), location.y());
      break;
  }
  return gfx::Rect(origin, gfx::Size());
}

void ShelfView::AnnounceShelfAlignment() {
  std::u16string announcement;
  switch (shelf_->alignment()) {
    case ShelfAlignment::kBottom:
    case ShelfAlignment::kBottomLocked:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_BOTTOM);
      break;
    case ShelfAlignment::kLeft:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_LEFT);
      break;
    case ShelfAlignment::kRight:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_RIGHT);
      break;
  }
  announcement_view_->GetViewAccessibility().AnnounceText(announcement);
}

bool ShelfView::IsAnimating() const {
  return bounds_animator_->IsAnimating();
}

gfx::Rect ShelfView::GetDragIconBoundsInScreenForTest() const {
  if (!drag_icon_proxy_)
    return drag_view_ ? drag_view_->GetBoundsInScreen() : gfx::Rect();
  return drag_icon_proxy_->GetBoundsInScreen();
}

void ShelfView::AddAnimationObserver(views::BoundsAnimatorObserver* observer) {
  bounds_animator_->AddObserver(observer);
}

void ShelfView::RemoveAnimationObserver(
    views::BoundsAnimatorObserver* observer) {
  bounds_animator_->RemoveObserver(observer);
}

void ShelfView::AnnounceShelfAutohideBehavior() {
  std::u16string announcement;
  switch (shelf_->auto_hide_behavior()) {
    case ShelfAutoHideBehavior::kAlways:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_AUTO_HIDE);
      break;
    case ShelfAutoHideBehavior::kNever:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_SHOWN);
      break;
    case ShelfAutoHideBehavior::kAlwaysHidden:
      announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_HIDDEN);
      break;
  }
  announcement_view_->GetViewAccessibility().AnnounceText(announcement);
}

void ShelfView::AnnouncePinUnpinEvent(const ShelfItem& item, bool pinned) {
  std::u16string item_title =
      item.title.empty()
          ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
          : item.title;
  std::u16string announcement = l10n_util::GetStringFUTF16(
      pinned ? IDS_SHELF_ITEM_WAS_PINNED : IDS_SHELF_ITEM_WAS_UNPINNED,
      item_title);
  announcement_view_->GetViewAccessibility().AnnounceText(announcement);
}

void ShelfView::AnnounceSwapEvent(const ShelfItem& first_item,
                                  const ShelfItem& second_item) {
  std::u16string first_item_title =
      first_item.title.empty()
          ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
          : first_item.title;
  std::u16string second_item_title =
      second_item.title.empty()
          ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
          : second_item.title;
  std::u16string announcement = l10n_util::GetStringFUTF16(
      IDS_SHELF_ITEMS_WERE_SWAPPED, first_item_title, second_item_title);
  announcement_view_->GetViewAccessibility().AnnounceText(announcement);
}

gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
  const ScrollableShelfView* scrollable_shelf_view =
      shelf_->hotseat_widget()->scrollable_shelf_view();
  gfx::Rect bounds = scrollable_shelf_view->visible_space();
  views::View::ConvertRectToScreen(scrollable_shelf_view, &bounds);
  return bounds;
}

std::optional<size_t> ShelfView::CancelDrag(
    std::optional<size_t> modified_index) {
  drag_scroll_dir_ = 0;
  scrolling_timer_.Stop();
  speed_up_drag_scrolling_.Stop();

  FinalizeRipOffDrag(true);

  delegate_->CancelScrollForItemDrag();
  drag_icon_proxy_.reset();
  drag_image_layer_.reset();

  if (!drag_view_)
    return modified_index;
  bool was_dragging = dragging();
  auto drag_view_index = view_model_->GetIndexOfView(drag_view_);
  drag_pointer_ = NONE;
  drag_view_ = nullptr;
  if (drag_view_index == modified_index) {
    // The view that was being dragged is being modified. Don't do anything.
    return modified_index;
  }
  if (!was_dragging)
    return modified_index;

  // Restore previous position, tracking the position of the modified view.
  bool at_end = modified_index == view_model_->view_size();
  views::View* modified_view =
      (modified_index.has_value() && !at_end)
          ? view_model_->view_at(modified_index.value())
          : nullptr;
  model_->Move(drag_view_index.value(), start_drag_index_.value());

  // If the modified view will be at the end of the list, return the new end of
  // the list.
  if (at_end)
    return view_model_->view_size();
  return modified_view ? view_model_->GetIndexOfView(modified_view)
                       : std::nullopt;
}

void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
  if (!ShouldHandleGestures(*event))
    return;

  if (HandleGestureEvent(event))
    event->StopPropagation();
}

void ShelfView::MaybeDuplicatePromiseAppForRemoval(
    ShelfAppButton* promise_app_view,
    const ShelfItem& item) {
  if (!ash::features::ArePromiseIconsEnabled()) {
    return;
  }

  if (!promise_app_view || !promise_app_view->is_promise_app()) {
    return;
  }

  if (promise_app_view->app_status() != AppStatus::kInstallSuccess ||
      !promise_app_view->IsDrawn()) {
    return;
  }

  // Search along the `view_model_` for an existing app with the same
  // package id as the promise app to be removed.
  for (const auto& entry : view_model_->entries()) {
    if (entry.view == promise_app_view) {
      continue;
    }
    if (static_cast<ShelfAppButton*>(entry.view)->package_id() ==
        item.id.app_id) {
      // PromiseApps don't get animation for removal if an app already existst
      // in the grid.
      return;
    }
  }

  AddPendingPromiseAppRemoval(item.id.app_id,
                              promise_app_view->icon_image_model());
}

void ShelfView::ShelfItemAdded(int model_index) {
  // ShelfView must keep view_model_ in sync with ShelfModel::items_ as its very
  // first response to ShelfModel changes. Failure to do so can result in UaF in
  // methods like ShelfView::ShelfItemForView, which can be implicitly called by
  // other classes. See https://crbug.com/1238959 for more details.
  const ShelfItem& item(model_->items()[model_index]);
  views::View* view = CreateViewForItem(item);

  {
    base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
                                          true);
    model_index = static_cast<int>(CancelDrag(model_index).value());
  }
  view_model_->Add(view, static_cast<size_t>(model_index));

  // If |item| is pinned and the mutation is user-triggered, report the pinning
  // action for accessibility and UMA. Do it now, because if |item| is hidden
  // then we will soon bail out but we still want to report the pinning action.
  if (model_->is_current_mutation_user_triggered() &&
      item.type == TYPE_PINNED_APP) {
    AnnouncePinUnpinEvent(item, /*pinned=*/true);
    RecordPinUnpinUserAction(/*pinned=*/true);
  }

  // Add child view so it has the same ordering as in the |view_model_|.
  // Note: No need to call UpdateShelfItemViewsVisibility() here directly, since
  // it will be called by ScrollableShelfView::ViewHierarchyChanged() as a
  // result of the below call.
  AddChildViewAt(view, model_index);

  if (!IsItemVisible(item))
    return;

  // Hide the view, it'll be made visible when the animation is done. Using
  // opacity 0 here to avoid messing with CalculateIdealBounds which touches
  // the view's visibility.
  view->layer()->SetOpacity(0);

  // Give the button its ideal bounds. That way if we end up animating the
  // button before this animation completes it doesn't appear at some random
  // spot (because it was in the middle of animating from 0,0 0x0 to its
  // target).
  CalculateIdealBounds();
  view->SetBoundsRect(
      view_model_->ideal_bounds(static_cast<size_t>(model_index)));

  if (model_->is_current_mutation_user_triggered() &&
      drag_and_drop_shelf_id_ != item.id) {
    view->ScrollViewToVisible();
  }

  // The first animation moves all the views to their target position. |view|
  // is hidden, so it visually appears as though we are providing space for
  // it. When done we'll fade the view in.
  AnimateToIdealBounds();

  // Attempt to animate the transition from a promise app into an actual app
  std::string package_id = item.package_id;
  auto found = pending_promise_apps_removals_.find(package_id);

  if (item.app_status == AppStatus::kReady &&
      found != pending_promise_apps_removals_.end()) {
    LayoutToIdealBounds();
    static_cast<ShelfAppButton*>(view)->AnimateInFromPromiseApp(
        found->second,
        base::BindRepeating(&ShelfView::FinishAnimationForPromiseApps,
                            weak_factory_.GetWeakPtr(), std::move(package_id)));
    return;
  }
  DCHECK_LE(static_cast<size_t>(model_index), visible_views_indices_.back());
  // TODO(crbug.com/40266934): Remove the check below once the bounds animator
  // works better with zero animation duration.
  if (!bounds_animator_->GetAnimationDuration().is_zero()) {
    bounds_animator_->SetAnimationDelegate(
        view, std::unique_ptr<gfx::AnimationDelegate>(
                  new StartFadeAnimationDelegate(this, view)));
  }
}

void ShelfView::AddPendingPromiseAppRemoval(
    const std::string& id,
    const ui::ImageModel& promise_icon) {
  pending_promise_apps_removals_.emplace(id, promise_icon);
}

void ShelfView::FinishAnimationForPromiseApps(
    const std::string& pending_app_id) {
  auto pending_app_found = pending_promise_apps_removals_.find(pending_app_id);

  // Discard the pending promise app layer.
  if (pending_app_found != pending_promise_apps_removals_.end()) {
    pending_promise_apps_removals_.erase(pending_app_found);
  }
}

void ShelfView::ShelfItemRemoved(int model_index, const ShelfItem& old_item) {
  // ShelfView must keep view_model_ in sync with ShelfModel::items_ as its very
  // first response to ShelfModel changes. Failure to do so can result in UaF in
  // methods like ShelfView::ShelfItemForView, which can be implicitly called by
  // other classes. See https://crbug.com/1238959 for more details.
  //
  // If std::move is not called on |view|, |view| will be deleted once out of
  // scope.
  std::unique_ptr<views::View> view(view_model_->view_at(model_index));

  shelf_button_delegate_->OnButtonWillBeRemoved();

  if (old_item.is_promise_app) {
    MaybeDuplicatePromiseAppForRemoval(static_cast<ShelfAppButton*>(view.get()),
                                       old_item);
  }
  view_model_->Remove(model_index);

  if (old_item.id == context_menu_id_ && shelf_menu_model_adapter_)
    shelf_menu_model_adapter_->Cancel();

  if (old_item.id == item_awaiting_response_)
    ResetActiveMenuModelRequest();

  {
    base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
                                          true);
    CancelDrag(std::nullopt);
  }

  if (view.get() == shelf_->tooltip()->GetCurrentAnchorView())
    shelf_->tooltip()->Close();

  // Disable the view while it's fading out to prevent it from getting events.
  view->SetEnabled(false);

  if (view->GetVisible() && view->layer()->opacity() > 0.0f) {
    UpdateShelfItemViewsVisibility();

    // There could be multiple fade out animations running. Only start
    // tracking for the first one.
    if (!fade_out_animation_tracker_) {
      fade_out_animation_tracker_.emplace(
          GetWidget()->GetCompositor()->RequestNewCompositorMetricsTracker());
      fade_out_animation_tracker_->Start(metrics_util::ForSmoothnessV3(
          base::BindRepeating(&ReportFadeOutAnimationSmoothness)));
    }

    // The first animation fades out the view. When done we'll animate the rest
    // of the views to their target location.
    bounds_animator_->AnimateViewTo(view.get(), view->bounds());
    auto* const view_ptr = view.get();
    // TODO(crbug.com/40266934): Remove the check below once the bounds animator
    // works better with zero animation duration.
    if (!bounds_animator_->GetAnimationDuration().is_zero()) {
      bounds_animator_->SetAnimationDelegate(
          view_ptr,
          std::make_unique<FadeOutAnimationDelegate>(this, std::move(view)));
    }
  } else {
    // Ensures that |view| is not used after destruction.
    StopAnimatingViewIfAny(view.get());

    // Removes |view| to trigger ViewHierarchyChanged function in the parent
    // view if any.
    view.reset();

    // If there is no fade out animation, notify the parent view of the
    // changed size before bounds animations start.
    PreferredSizeChanged();

    // We don't need to show a fade out animation for invisible |view|. When an
    // item is ripped out from the shelf, its |view| is already invisible.
    AnimateToIdealBounds();
  }

  if (model_->is_current_mutation_user_triggered() &&
      old_item.type == TYPE_PINNED_APP) {
    AnnouncePinUnpinEvent(old_item, /*pinned=*/false);
    RecordPinUnpinUserAction(/*pinned=*/false);
  }
}

void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
  // Bail if the view and shelf sizes do not match. ShelfItemChanged may be
  // called here before ShelfItemAdded, due to ChromeShelfController's
  // item initialization, which calls SetItem during ShelfItemAdded.
  if (model_->items().size() != view_model_->view_size())
    return;

  const ShelfItem& item = model_->items()[model_index];

  // If there's a change in the item's active desk, perform the update at the
  // end of this function in order to guarantee that both |model_| and
  // |view_model_| are consistent if there are other changes in the item.
  base::ScopedClosureRunner run_at_scope_exit;
  if (old_item.is_on_active_desk != item.is_on_active_desk) {
    run_at_scope_exit.ReplaceClosure(base::BindOnce(
        &ShelfView::ShelfItemsUpdatedForDeskChange, base::Unretained(this)));
  }

  if (old_item.type != item.type) {
    // Type changed, swap the views.
    model_index = static_cast<int>(CancelDrag(model_index).value());
    std::unique_ptr<views::View> old_view(view_model_->view_at(model_index));
    bounds_animator_->StopAnimatingView(old_view.get());
    // Removing and re-inserting a view in our view model will strip the ideal
    // bounds from the item. To avoid recalculation of everything the bounds
    // get remembered and restored after the insertion to the previous value.
    gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
    view_model_->Remove(model_index);
    views::View* new_view = CreateViewForItem(item);
    // The view must be added to the |view_model_| before it's added as a child
    // so that the model is consistent when UpdateShelfItemViewsVisibility() is
    // called as a result the hierarchy changes caused by AddChildView(). See
    // ScrollableShelfView::ViewHierarchyChanged().
    view_model_->Add(new_view, model_index);
    AddChildView(new_view);
    view_model_->set_ideal_bounds(model_index, old_ideal_bounds);

    bounds_animator_->StopAnimatingView(new_view);
    new_view->SetBoundsRect(old_view->bounds());
    bounds_animator_->AnimateViewTo(new_view, old_ideal_bounds);

    // If an item is being pinned or unpinned, show the new status of the
    // shelf immediately so that the separator gets drawn as needed.
    if (old_item.type == TYPE_PINNED_APP || item.type == TYPE_PINNED_APP) {
      if (model_->is_current_mutation_user_triggered()) {
        AnnouncePinUnpinEvent(old_item, item.type == TYPE_PINNED_APP);
        RecordPinUnpinUserAction(item.type == TYPE_PINNED_APP);
      }
      AnimateToIdealBounds();
    }
    return;
  }

  views::View* view = view_model_->view_at(model_index);
  switch (item.type) {
    case TYPE_PINNED_APP:
    case TYPE_BROWSER_SHORTCUT:
    case TYPE_APP:
    case TYPE_UNPINNED_BROWSER_SHORTCUT:
    case TYPE_DIALOG: {
      CHECK(views::IsViewClass<ShelfAppButton>(view));
      ShelfAppButton* button = static_cast<ShelfAppButton*>(view);
      UpdateButton(button, item);
      break;
    }
    case TYPE_UNDEFINED:
      break;
  }
}

void ShelfView::ShelfItemsUpdatedForDeskChange() {
  DCHECK(features::IsPerDeskShelfEnabled());

  // The order here matters, since switching/removing desks, or moving windows
  // between desks will affect shelf items' visibility, we need to update the
  // visibility of the views first before we layout.
  UpdateShelfItemViewsVisibility();
  // Signal to the parent ScrollableShelfView so that it can recenter the items
  // after their visibility have been updated (via
  // `UpdateAvailableSpaceAndScroll()`).
  PreferredSizeChanged();
  LayoutToIdealBounds();
}

void ShelfView::ShelfItemMoved(int start_index, int target_index) {
  view_model_->Move(start_index, target_index);

  // Reorder the child view to be in the same order as in the |view_model_|.
  ReorderChildView(view_model_->view_at(target_index), target_index);
  NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
                           true /* send_native_event */);

  // When cancelling a drag due to a shelf item being added, the currently
  // dragged item is moved back to its initial position. AnimateToIdealBounds
  // will be called again when the new item is added to the |view_model_| but
  // at this time the |view_model_| is inconsistent with the |model_|.
  if (!cancelling_drag_model_changed_)
    AnimateToIdealBounds();
}

void ShelfView::ShelfItemDelegateChanged(const ShelfID& id,
                                         ShelfItemDelegate* old_delegate,
                                         ShelfItemDelegate* delegate) {
  if (id == context_menu_id_ && shelf_menu_model_adapter_)
    shelf_menu_model_adapter_->Cancel();
}

void ShelfView::ShelfItemStatusChanged(const ShelfID& id) {
  scoped_display_for_new_windows_.reset();

  int index = model_->ItemIndexByID(id);
  if (index < 0)
    return;

  const ShelfItem item = model_->items()[index];
  ShelfAppButton* button = GetShelfAppButton(id);
  button->ReflectItemStatus(item);
  button->SchedulePaint();
}

void ShelfView::ShelfItemRippedOff() {
  // On the display where the drag started, there is nothing to do.
  if (dragging())
    return;
  // When a dragged item has been ripped off the shelf, it is moved to the end.
  // Now we need to hide it.
  view_model_->view_at(model_->item_count() - 1)->layer()->SetOpacity(0.f);
}

void ShelfView::ShelfItemReturnedFromRipOff(int index) {
  // On the display where the drag started, there is nothing to do.
  if (dragging())
    return;
  // Show the item and prevent it from animating into place from the position
  // where it was sitting with zero opacity.
  views::View* view = view_model_->view_at(index);
  const gfx::Rect bounds = bounds_animator_->GetTargetBounds(view);
  bounds_animator_->StopAnimatingView(view);
  view->SetBoundsRect(bounds);
  view->layer()->SetOpacity(1.f);
}

void ShelfView::OnShelfAlignmentChanged(aura::Window* root_window,
                                        ShelfAlignment old_alignment) {
  LayoutToIdealBounds();
  for (size_t visible_index : visible_views_indices_)
    view_model_->view_at(visible_index)->DeprecatedLayoutImmediately();

  AnnounceShelfAlignment();
}

void ShelfView::OnShelfAutoHideBehaviorChanged() {
  AnnounceShelfAutohideBehavior();
}

void ShelfView::AfterItemSelected(const ShelfItem& item,
                                  views::Button* sender,
                                  std::unique_ptr<ui::Event> event,
                                  views::InkDrop* ink_drop,
                                  ShelfAction action,
                                  ShelfItemDelegate::AppMenuItems menu_items) {
  item_awaiting_response_ = ShelfID();
  shelf_button_pressed_metric_tracker_.ButtonPressed(*event, sender, action);

  // Record AppList metric for any action considered an app launch.
  if (action == SHELF_ACTION_NEW_WINDOW_CREATED ||
      action == SHELF_ACTION_WINDOW_ACTIVATED) {
    Shell::Get()->app_list_controller()->RecordShelfAppLaunched();
  }

  // The app list handles its own ink drop effect state changes.
  if (action == SHELF_ACTION_APP_LIST_DISMISSED) {
    ink_drop->SnapToActivated();
    ink_drop->AnimateToState(views::InkDropState::HIDDEN);
  } else if (action != SHELF_ACTION_APP_LIST_SHOWN && !dragging()) {
    if (action != SHELF_ACTION_NEW_WINDOW_CREATED && menu_items.size() > 1 &&
        !dragging()) {
      // Show the app menu with 2 or more items, if no window was created. The
      // menu is not shown in case item drag started while the selection request
      // was in progress.
      ShowMenu(std::make_unique<ShelfApplicationMenuModel>(
                   item.title, std::move(menu_items),
                   model_->GetShelfItemDelegate(item.id)),
               sender, item.id, gfx::Point(), /*context_menu=*/false,
               ui::GetMenuSourceTypeForEvent(*event));
      shelf_->UpdateVisibilityState();
    } else {
      ink_drop->AnimateToState(views::InkDropState::ACTION_TRIGGERED);
    }
  }
  shelf_->shelf_layout_manager()->OnShelfItemSelected(action);
}

void ShelfView::ShowShelfContextMenu(
    const ShelfID& shelf_id,
    const gfx::Point& point,
    views::View* source,
    ui::mojom::MenuSourceType source_type,
    std::unique_ptr<ui::SimpleMenuModel> model) {
  if (!model) {
    const int64_t display_id = GetDisplayIdForView(this);
    model = std::make_unique<ShelfContextMenuModel>(nullptr, display_id,
                                                    /*menu_in_shelf=*/true);
  }
  ShowMenu(std::move(model), source, shelf_id, point, /*context_menu=*/true,
           source_type);
}

void ShelfView::ShowMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,
                         views::View* source,
                         const ShelfID& shelf_id,
                         const gfx::Point& click_point,
                         bool context_menu,
                         ui::mojom::MenuSourceType source_type) {
  // Delayed callbacks to show context and application menus may conflict; hide
  // the old menu before showing a new menu in that case.
  if (IsShowingMenu())
    shelf_menu_model_adapter_->Cancel();

  item_awaiting_response_ = ShelfID();
  if (menu_model->GetItemCount() == 0)
    return;

  context_menu_id_ = shelf_id;

  closing_event_time_ = base::TimeTicks();

  // NOTE: If you convert to HAS_MNEMONICS be sure to update menu building code.
  int run_types = views::MenuRunner::USE_ASH_SYS_UI_LAYOUT;
  if (context_menu) {
    run_types |=
        views::MenuRunner::CONTEXT_MENU | views::MenuRunner::FIXED_ANCHOR;
  }

  const ShelfItem* item = ShelfItemForView(source);

  if ((source_type == ui::mojom::MenuSourceType::kMouse ||
       source_type == ui::mojom::MenuSourceType::kKeyboard) &&
      item) {
    views::InkDrop::Get(source)->GetInkDrop()->AnimateToState(
        views::InkDropState::ACTIVATED);
  }

  // Only selected shelf items with context menu opened can be dragged.
  if (context_menu && item && ShelfButtonIsInDrag(item->type, source) &&
      source_type == ui::mojom::MenuSourceType::kTouch) {
    run_types |= views::MenuRunner::SEND_GESTURE_EVENTS_TO_OWNER;
  }

  // UnsafeDangling triaged in https://crbug.com/1423849.
  shelf_menu_model_adapter_ = std::make_unique<ShelfMenuModelAdapter>(
      item ? item->id.app_id : std::string(), std::move(menu_model), source,
      source_type,
      base::BindOnce(&ShelfView::OnMenuClosed, base::Unretained(this),
                     base::UnsafeDangling(source)),
      display::Screen::GetScreen()->InTabletMode(),
      /*for_application_menu_items*/ !context_menu);
  shelf_menu_model_adapter_->Run(
      GetMenuAnchorRect(*source, click_point, context_menu),
      shelf_->IsHorizontalAlignment()
          ? views::MenuAnchorPosition::kBubbleTopRight
          : views::MenuAnchorPosition::kBubbleLeft,
      run_types);

  if (!context_menu_shown_callback_.is_null())
    context_menu_shown_callback_.Run();
}

void ShelfView::OnMenuClosed(MayBeDangling<views::View> source) {
  context_menu_id_ = ShelfID();

  closing_event_time_ = shelf_menu_model_adapter_->GetClosingEventTime();

  const ShelfItem* item = ShelfItemForView(source);
  if (item) {
    static_cast<ShelfAppButton*>(source)->OnMenuClosed();
  }

  shelf_menu_model_adapter_.reset();

  const bool is_in_drag = item && ShelfButtonIsInDrag(item->type, source);
  // Update the shelf visibility since auto-hide or alignment might have
  // changes, but don't update if shelf item is being dragged. Since shelf
  // should be kept as visible during shelf item drag even menu is closed.
  if (!is_in_drag)
    shelf_->UpdateVisibilityState();
}

void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
  shelf_->NotifyShelfIconPositionsChanged();

  // Notify user education features that anchor bounds have changed.
  if (features::IsUserEducationEnabled()) {
    views::ElementTrackerViews::GetInstance()->NotifyCustomEvent(
        user_education::kHelpBubbleAnchorBoundsChangedEvent, this);
  }

  // Do not call PreferredSizeChanged() so that container does not re-layout
  // during the bounds animation.
}

void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
  if (move_animation_tracker_) {
    move_animation_tracker_->Stop();
    move_animation_tracker_.reset();
  }

  if (snap_back_from_rip_off_view_ && animator == bounds_animator_.get()) {
    if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
      // Coming here the animation of the ShelfAppButton is finished and the
      // previously hidden status can be shown again. Since the button itself
      // might have gone away or changed locations we check that the button
      // is still in the shelf and show its status again.
      if (base::Contains(view_model_->entries(), snap_back_from_rip_off_view_,
                         &views::ViewModelBase::Entry::view))
        snap_back_from_rip_off_view_->ClearState(ShelfAppButton::STATE_HIDDEN);

      snap_back_from_rip_off_view_ = nullptr;
    }
  }
}

bool ShelfView::IsRepostEvent(const ui::Event& event) {
  if (closing_event_time_.is_null())
    return false;

  // If the current (press down) event is a repost event, the time stamp of
  // these two events should be the same.
  return closing_event_time_ == event.time_stamp();
}

const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
  const auto view_index = view_model_->GetIndexOfView(view);
  return (!view_index.has_value()) ? nullptr
                                   : &(model_->items()[view_index.value()]);
}

int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
  const gfx::Rect bounds = GetBoundsInScreen();
  int distance = shelf_->SelectValueForShelfAlignment(
      bounds.y() - coordinate.y(), coordinate.x() - bounds.right(),
      bounds.x() - coordinate.x());
  return distance > 0 ? distance : 0;
}

bool ShelfView::CanPrepareForDrag(Pointer pointer,
                                  const ui::LocatedEvent& event) {
  // Bail if dragging has already begun, or if no item has been pressed.
  if (dragging() || !drag_view_)
    return false;

  // Dragging only begins once the pointer has travelled a minimum distance.
  if ((std::abs(event.x() - drag_origin_.x()) < kMinimumDragDistance) &&
      (std::abs(event.y() - drag_origin_.y()) < kMinimumDragDistance)) {
    return false;
  }

  return true;
}

bool ShelfView::ShouldHandleGestures(const ui::GestureEvent& event) const {
  if (event.type() == ui::EventType::kGestureScrollBegin) {
    float x_offset = event.details().scroll_x_hint();
    float y_offset = event.details().scroll_y_hint();
    if (!shelf_->IsHorizontalAlignment())
      std::swap(x_offset, y_offset);

    return std::abs(x_offset) < std::abs(y_offset);
  }

  return true;
}

std::u16string ShelfView::GetTitleForChildView(const views::View* view) const {
  const ShelfItem* item = ShelfItemForView(view);
  return item ? item->title : std::u16string();
}

void ShelfView::UpdateShelfItemViewsVisibility() {
  visible_views_indices_.clear();
  for (size_t i = 0; i < view_model_->view_size(); ++i) {
    View* view = view_model_->view_at(i);
    // To receive drag event continuously from |drag_view_| during the dragging
    // off from the shelf, don't make |drag_view_| invisible. It will be
    // eventually invisible and removed from the |view_model_| by
    // FinalizeRipOffDrag().
    const bool has_to_show = dragged_off_shelf_ && view == drag_view();
    const bool is_visible = has_to_show || IsItemVisible(model()->items()[i]);
    view->SetVisible(is_visible);

    if (is_visible)
      visible_views_indices_.push_back(i);
  }
}

void ShelfView::DestroyScopedDisplay() {
  scoped_display_for_new_windows_.reset();
}

int ShelfView::CalculateAppIconsLayoutOffset() const {
  const ScrollableShelfView* scrollable_shelf_view =
      shelf_->hotseat_widget()->scrollable_shelf_view();
  const gfx::Insets& edge_padding_insets =
      scrollable_shelf_view->edge_padding_insets();

  // Note that `edge_padding_insets` fetched from `scrollable_shelf_view` is
  // mirrored under RTL.
  if (scrollable_shelf_view->ShouldAdaptToRTL())
    return edge_padding_insets.right();

  return shelf_->PrimaryAxisValue(edge_padding_insets.left(),
                                  edge_padding_insets.top());
}

gfx::Rect ShelfView::GetChildViewTargetMirroredBounds(
    const views::View* child) const {
  DCHECK_EQ(this, child->parent());
  return GetMirroredRect(bounds_animator_->GetTargetBounds(child));
}

void ShelfView::RemoveGhostView() {
  if (current_ghost_view_) {
    current_ghost_view_index_ = std::nullopt;
    current_ghost_view_->FadeOut();
    current_ghost_view_ = nullptr;
  }

  if (last_ghost_view_) {
    last_ghost_view_->FadeOut();
    last_ghost_view_ = nullptr;
  }
}

void ShelfView::ResetActiveMenuModelRequest() {
  context_menu_callback_.Cancel();
  item_awaiting_response_ = ShelfID();
}

views::View::DropCallback ShelfView::GetDropCallback(
    const ui::DropTargetEvent& event) {
  return base::BindOnce(&ShelfView::EndDragCallback, base::Unretained(this));
}

void ShelfView::EndDragCallback(
    const ui::DropTargetEvent& event,
    ui::mojom::DragOperation& output_drag_op,
    std::unique_ptr<ui::LayerTreeOwner> drag_image_layer_owner) {
  // TODO(b/271601288): Hook up drop animation with the drag image icon.
  output_drag_op = ui::mojom::DragOperation::kMove;
  drag_image_layer_ = std::move(drag_image_layer_owner);
  EndDrag(false);
}

bool ShelfView::GetDropFormats(
    int* formats,
    std::set<ui::ClipboardFormatType>* format_types) {
  format_types->insert(GetAppItemFormatType());
  return true;
}

bool ShelfView::CanDrop(const OSExchangeData& data) {
  auto app_info = GetAppInfoFromDropDataForAppType(data);
  if (!app_info || app_info->IsValid()) {
    return false;
  }

  std::set<ui::ClipboardFormatType> format_types;
  format_types.insert(GetAppItemFormatType());
  return data.HasAnyFormat(0, format_types) &&
         app_info->type == DraggableAppType::kAppGridItem;
}

void ShelfView::OnDragExited() {
  EndDrag(/*cancel=*/true);
}

void ShelfView::OnDragEntered(const ui::DropTargetEvent& event) {
  auto app_info = GetAppInfoFromDropDataForAppType(event.data());
  if (!app_info || app_info->IsValid()) {
    return;
  }

  std::string app_id = app_info->app_id;
  if (app_id.empty() || app_info->type != DraggableAppType::kAppGridItem) {
    views::View::OnDragEntered(event);
    return;
  }

  gfx::Point drag_point_in_screen = event.location();
  views::View::ConvertPointToScreen(this, &drag_point_in_screen);
  StartDrag(app_id, drag_point_in_screen, gfx::Rect());
}

int ShelfView::OnDragUpdated(const ui::DropTargetEvent& event) {
  gfx::Point drag_point_in_screen = event.location();
  views::View::ConvertPointToScreen(this, &drag_point_in_screen);
  Drag(drag_point_in_screen,
       drag_view_ ? drag_view_->GetBoundsInScreen() : gfx::Rect());
  return ui::DragDropTypes::DRAG_MOVE;
}

BEGIN_METADATA(ShelfView)
END_METADATA

}  // namespace ash