File: textfield.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (3382 lines) | stat: -rw-r--r-- 118,368 bytes parent folder | download | duplicates (2)
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
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
// 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 "ui/views/controls/textfield/textfield.h"

#include <algorithm>
#include <set>
#include <string>
#include <string_view>
#include <utility>

#include "base/auto_reset.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/platform/ax_platform.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/data_transfer_policy/data_transfer_endpoint.h"
#include "ui/base/default_style.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/base/ime/constants.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/compositor/canvas_painter.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/gesture_event_details.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/selection_bound.h"
#include "ui/native_theme/native_theme.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/touch_selection/touch_selection_metrics.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/accessibility/views_utilities_aura.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/background.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/focusable_border.h"
#include "ui/views/controls/highlight_path_generator.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/controls/views_text_services_context_menu.h"
#include "ui/views/drag_utils.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/painter.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/style/typography.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/touchui/touch_selection_controller.h"
#include "ui/views/views_delegate.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/win_util.h"
#endif

#if BUILDFLAG(IS_LINUX)
#include "ui/base/ime/linux/text_edit_command_auralinux.h"
#include "ui/base/ime/text_input_flags.h"
#include "ui/linux/linux_ui.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "ui/base/cocoa/secure_password_input.h"
#endif

#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#include "ui/ozone/public/platform_gl_egl_utility.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "ui/aura/window.h"
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/wm/core/ime_util_chromeos.h"
#endif

#if defined(USE_AURA)
#include "ui/views/touchui/touch_selection_controller_impl.h"
#endif

namespace views {
namespace {

using ::ui::mojom::DragOperation;

// An enum giving different model properties unique keys for the
// OnPropertyChanged call.
enum TextfieldPropertyKey {
  kTextfieldText = 1,
  kTextfieldTextColor,
  kTextfieldSelectionTextColor,
  kTextfieldBackgroundColor,
  kTextfieldSelectionBackgroundColor,
  kTextfieldCursorEnabled,
  kTextfieldHorizontalAlignment,
  kTextfieldSelectedRange,
};

// Returns the ui::TextEditCommand corresponding to the |command_id| menu
// action. |has_selection| is true if the textfield has an active selection.
// Keep in sync with UpdateContextMenu.
ui::TextEditCommand GetTextEditCommandFromMenuCommand(int command_id,
                                                      bool has_selection) {
  switch (command_id) {
    case Textfield::kUndo:
      return ui::TextEditCommand::UNDO;
    case Textfield::kCut:
      return ui::TextEditCommand::CUT;
    case Textfield::kCopy:
      return ui::TextEditCommand::COPY;
    case Textfield::kPaste:
      return ui::TextEditCommand::PASTE;
    case Textfield::kDelete:
      // The DELETE menu action only works in case of an active selection.
      if (has_selection) {
        return ui::TextEditCommand::DELETE_FORWARD;
      }
      break;
    case Textfield::kSelectAll:
      return ui::TextEditCommand::SELECT_ALL;
    case Textfield::kSelectWord:
      return ui::TextEditCommand::SELECT_WORD;
  }
  return ui::TextEditCommand::INVALID_COMMAND;
}

base::TimeDelta GetPasswordRevealDuration(const ui::KeyEvent& event) {
  // The key event may carries the property that indicates it was from the
  // virtual keyboard and mirroring is not occurring
  // In that case, reveal the password characters for 1 second.
  auto* properties = event.properties();
  bool from_vk =
      properties && properties->find(ui::kPropertyFromVK) != properties->end();
  if (from_vk) {
    std::vector<uint8_t> fromVKPropertyArray =
        properties->find(ui::kPropertyFromVK)->second;
    DCHECK_GT(fromVKPropertyArray.size(), ui::kPropertyFromVKIsMirroringIndex);
    uint8_t is_mirroring =
        fromVKPropertyArray[ui::kPropertyFromVKIsMirroringIndex];
    if (!is_mirroring) {
      return base::Seconds(1);
    }
  }
  return base::TimeDelta();
}

bool IsControlKeyModifier(int flags) {
// XKB layout doesn't natively generate printable characters from a
// Control-modified key combination, but we cannot extend it to other platforms
// as Control has different meanings and behaviors.
// https://crrev.com/2580483002/#msg46
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  return flags & ui::EF_CONTROL_DOWN;
#else
  return false;
#endif
}

bool IsValidCharToInsert(const char16_t& ch) {
  // Filter out all control characters, including tab and new line characters.
  return (ch >= 0x20 && ch < 0x7F) || ch > 0x9F;
}

#if BUILDFLAG(IS_MAC)
const float kAlmostTransparent = 1.0 / 255.0;
const float kOpaque = 1.0;
#endif

}  // namespace

// static
base::TimeDelta Textfield::GetCaretBlinkInterval() {
  return ui::NativeTheme::GetInstanceForNativeUi()->GetCaretBlinkInterval();
}

// static
const gfx::FontList& Textfield::GetDefaultFontList() {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  return rb.GetFontListWithDelta(ui::kLabelFontSizeDelta);
}

Textfield::Textfield()
    : model_(new TextfieldModel(this)),
      placeholder_text_draw_flags_(gfx::Canvas::DefaultCanvasTextAlignment()),
      selection_controller_(this) {
  set_context_menu_controller(this);
  set_drag_controller(this);
  GetViewAccessibility().set_needs_ax_tree_manager(true);
  auto cursor_view = std::make_unique<View>();
  cursor_view->SetPaintToLayer(ui::LAYER_SOLID_COLOR);
  cursor_view->layer()->SetName("Textfield::CursorView");
  cursor_view->GetViewAccessibility().SetIsIgnored(true);
  cursor_view_ = AddChildView(std::move(cursor_view));
  GetRenderText()->SetFontList(GetDefaultFontList());
  UpdateDefaultBorder();
  SetFocusBehavior(FocusBehavior::ALWAYS);
  views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
                                                GetCornerRadius());
  FocusRing::Install(this);
  FocusRing::Get(this)->SetOutsetFocusRingDisabled(true);
  InkDropHost* ink_drop_host =
      InkDrop::Install(this, std::make_unique<views::InkDropHost>(this));
  ink_drop_host->SetMode(InkDropHost::InkDropMode::ON);
  ink_drop_host->SetLayerRegion(LayerRegion::kAbove);
  ink_drop_host->SetHighlightOpacity(1.0f);
  ink_drop_host->SetBaseColorCallback(base::BindRepeating(
      [](Textfield* host) {
        return host->HasFocus() ? SK_ColorTRANSPARENT
                                : host->GetColorProvider()->GetColor(
                                      ui::kColorTextfieldHover);
      },
      this));

#if !BUILDFLAG(IS_MAC)
  // Do not map accelerators on Mac. E.g. They might not reflect custom
  // keybindings that a user has set. But also on Mac, these commands dispatch
  // via the "responder chain" when the OS searches through menu items in the
  // menu bar. The menu then sends e.g., a "cut:" command to NativeWidgetMac,
  // which will pass it to Textfield via OnKeyEvent() after associating the
  // correct edit command.

  // These allow BrowserView to pass edit commands from the Chrome menu to us
  // when we're focused by simply asking the FocusManager to
  // ProcessAccelerator() with the relevant accelerators.
  AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN));
  AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN));
  AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN));
#endif

  GetViewAccessibility().SetRole(ax::mojom::Role::kTextField);

  // Sometimes there are additional ignored views, such as the View representing
  // the cursor, inside the text field. These should always be ignored by
  // accessibility since a plain text field should always be a leaf node in the
  // accessibility trees of all the platforms we support.
  GetViewAccessibility().SetIsLeaf(true);
  UpdateAccessibleDefaultActionVerb();
  // Editable state indicates support of editable interface, and is always set
  // for a textfield, even if disabled or readonly.
  GetViewAccessibility().SetIsEditable(true);
}

Textfield::~Textfield() {
  if (HasObserver(this)) {
    RemoveObserver(this);
  }

  if (GetInputMethod()) {
    // The textfield should have been blurred before destroy.
    DCHECK(this != GetInputMethod()->GetTextInputClient());
  }
}

void Textfield::SetController(TextfieldController* controller) {
  controller_ = controller;
}

void Textfield::AddedToWidget() {
  UpdateAccessibilityTextDirection();
  UpdateAccessibleValue();
}

bool Textfield::GetReadOnly() const {
  return read_only_;
}

void Textfield::SetReadOnly(bool read_only) {
  if (read_only_ == read_only) {
    return;
  }

  // Update read-only without changing the focusable state (or active, etc.).
  read_only_ = read_only;
  if (GetEnabled()) {
    GetViewAccessibility().SetReadOnly(read_only_);
  }
  if (GetInputMethod()) {
    GetInputMethod()->OnTextInputTypeChanged(this);
  }
  if (GetWidget()) {
    SetColor(GetTextColor());
    UpdateBackgroundColor();
  }

  UpdateDefaultBorder();
  OnPropertyChanged(&read_only_, kPropertyEffectsPaint);
}

void Textfield::SetTextInputType(ui::TextInputType type) {
  if (text_input_type_ == type) {
    return;
  }

  GetRenderText()->SetObscured(type == ui::TEXT_INPUT_TYPE_PASSWORD);
  text_input_type_ = type;
  GetViewAccessibility().SetIsProtected(type == ui::TEXT_INPUT_TYPE_PASSWORD);
  if (GetInputMethod()) {
    GetInputMethod()->OnTextInputTypeChanged(this);
  }
  OnCaretBoundsChanged();
  OnPropertyChanged(&text_input_type_, kPropertyEffectsPaint);
  UpdateAfterChange(TextChangeType::kInternal, false);
}

void Textfield::SetTextInputFlags(int flags) {
  if (text_input_flags_ == flags) {
    return;
  }

  text_input_flags_ = flags;
  OnPropertyChanged(&text_input_flags_, kPropertyEffectsNone);
}

std::u16string_view Textfield::GetText() const {
  return model_->text();
}

void Textfield::SetText(std::u16string_view new_text) {
  SetTextWithoutCaretBoundsChangeNotification(new_text, new_text.length());
  // The above call already notified for the text change; fire notifications
  // etc. for the cursor changes as well.
  UpdateAfterChange(TextChangeType::kNone, true);
}

void Textfield::SetTextWithoutCaretBoundsChangeNotification(
    std::u16string_view text,
    size_t cursor_position) {
  model_->SetText(text, cursor_position);
  UpdateAfterChange(TextChangeType::kInternal, false, false);
}

void Textfield::Scroll(const std::vector<size_t>& positions) {
  for (const auto position : positions) {
    model_->MoveCursorTo(position);
    GetRenderText()->GetUpdatedCursorBounds();
  }
}

void Textfield::AppendText(const std::u16string& new_text) {
  if (new_text.empty()) {
    return;
  }
  model_->Append(new_text);
  UpdateAfterChange(TextChangeType::kInternal, false);
}

void Textfield::InsertOrReplaceText(const std::u16string& new_text) {
  if (new_text.empty()) {
    return;
  }
  model_->InsertText(new_text);
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
}

std::u16string_view Textfield::GetSelectedText() const {
  return model_->GetSelectedText();
}

void Textfield::SelectAll(bool reversed) {
  model_->SelectAll(reversed);
  if (HasSelection() && performing_user_action_) {
    UpdateSelectionClipboard();
  }
  UpdateAfterChange(TextChangeType::kNone, true);
}

void Textfield::SelectWord() {
  model_->SelectWord();
  if (HasSelection() && performing_user_action_) {
    UpdateSelectionClipboard();
  }
  UpdateAfterChange(TextChangeType::kNone, true);
}

void Textfield::SelectWordAt(const gfx::Point& point) {
  model_->MoveCursorTo(point, false);
  model_->SelectWord();
  UpdateAfterChange(TextChangeType::kNone, true);
}

void Textfield::ClearSelection() {
  model_->ClearSelection();
  UpdateAfterChange(TextChangeType::kNone, true);
}

bool Textfield::HasSelection(bool primary_only) const {
  return model_->HasSelection(primary_only);
}

SkColor Textfield::GetTextColor() const {
  return text_color_.value_or(
      GetColorProvider()->GetColor(TypographyProvider::Get().GetColorId(
          style::CONTEXT_TEXTFIELD, GetTextStyle())));
}

void Textfield::SetTextColor(SkColor color) {
  text_color_ = color;
  if (GetWidget()) {
    SetColor(color);
  }
}

SkColor Textfield::GetBackgroundColor() const {
  return background_color_.value_or(GetColorProvider()->GetColor(
      GetReadOnly() || !GetEnabled() ? ui::kColorTextfieldBackgroundDisabled
                                     : ui::kColorTextfieldBackground));
}

void Textfield::SetBackgroundColor(SkColor color) {
  background_color_ = color;
  if (GetWidget()) {
    UpdateBackgroundColor();
  }
}

bool Textfield::GetBackgroundEnabled() const {
  return is_background_enabled_;
}

void Textfield::SetBackgroundEnabled(bool enabled) {
  is_background_enabled_ = enabled;
}

SkColor Textfield::GetSelectionTextColor() const {
  return selection_text_color_.value_or(
      GetColorProvider()->GetColor(ui::kColorTextfieldSelectionForeground));
}

void Textfield::SetSelectionTextColor(SkColor color) {
  selection_text_color_ = color;
  UpdateSelectionTextColor();
}

SkColor Textfield::GetSelectionBackgroundColor() const {
  return selection_background_color_.value_or(
      GetColorProvider()->GetColor(ui::kColorTextfieldSelectionBackground));
}

void Textfield::SetSelectionBackgroundColor(SkColor color) {
  selection_background_color_ = color;
  UpdateSelectionBackgroundColor();
}

bool Textfield::GetCursorEnabled() const {
  return GetRenderText()->cursor_enabled();
}

void Textfield::SetCursorEnabled(bool enabled) {
  if (GetRenderText()->cursor_enabled() == enabled) {
    return;
  }

  GetRenderText()->SetCursorEnabled(enabled);
  UpdateAfterChange(TextChangeType::kNone, true, false);
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldCursorEnabled),
      kPropertyEffectsPaint);
}

const gfx::FontList& Textfield::GetFontList() const {
  return GetRenderText()->font_list();
}

void Textfield::SetFontList(const gfx::FontList& font_list) {
  GetRenderText()->SetFontList(font_list);
  OnCaretBoundsChanged();
  PreferredSizeChanged();
}

void Textfield::SetDefaultWidthInChars(int default_width) {
  DCHECK_GE(default_width, 0);
  default_width_in_chars_ = default_width;
}

void Textfield::SetMinimumWidthInChars(int minimum_width) {
  DCHECK_GE(minimum_width, -1);
  minimum_width_in_chars_ = minimum_width;
}

std::u16string_view Textfield::GetPlaceholderText() const {
  return placeholder_text_;
}

void Textfield::SetPlaceholderText(std::u16string_view text) {
  if (placeholder_text_ == text) {
    return;
  }

  placeholder_text_ = std::u16string(text);
  GetViewAccessibility().SetPlaceholder(base::UTF16ToUTF8(text));
  OnPropertyChanged(&placeholder_text_, kPropertyEffectsPaint);
}

gfx::HorizontalAlignment Textfield::GetHorizontalAlignment() const {
  return GetRenderText()->horizontal_alignment();
}

void Textfield::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
  GetRenderText()->SetHorizontalAlignment(alignment);

  OnPropertyChanged(ui::metadata::MakeUniquePropertyKey(
                        &model_, kTextfieldHorizontalAlignment),
                    kPropertyEffectsNone);
}

void Textfield::ShowVirtualKeyboardIfEnabled() {
  // GetInputMethod() may return nullptr in tests.
  if (GetEnabled() && !GetReadOnly() && GetInputMethod()) {
    GetInputMethod()->SetVirtualKeyboardVisibilityIfEnabled(true);
  }
}

bool Textfield::IsIMEComposing() const {
  return model_->HasCompositionText();
}

const gfx::Range& Textfield::GetSelectedRange() const {
  return GetRenderText()->selection();
}

void Textfield::SetSelectedRange(const gfx::Range& range) {
  model_->SelectRange(range);
  UpdateAfterChange(TextChangeType::kNone, true);
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldSelectedRange),
      kPropertyEffectsPaint);
  UpdateAccessibleTextSelection();
}

void Textfield::AddSecondarySelectedRange(const gfx::Range& range) {
  model_->SelectRange(range, false);
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldSelectedRange),
      kPropertyEffectsPaint);
  UpdateAccessibleTextSelection();
}

const gfx::SelectionModel& Textfield::GetSelectionModel() const {
  return GetRenderText()->selection_model();
}

void Textfield::SelectSelectionModel(const gfx::SelectionModel& sel) {
  model_->SelectSelectionModel(sel);
  UpdateAfterChange(TextChangeType::kNone, true);
}

size_t Textfield::GetCursorPosition() const {
  return model_->GetCursorPosition();
}

void Textfield::SetColor(SkColor value) {
  GetRenderText()->SetColor(value);
  cursor_view_->layer()->SetColor(value);
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldTextColor),
      kPropertyEffectsPaint);
}

void Textfield::ApplyColor(SkColor value, const gfx::Range& range) {
  GetRenderText()->ApplyColor(value, range);
  SchedulePaint();
}

void Textfield::SetStyle(gfx::TextStyle style, bool value) {
  GetRenderText()->SetStyle(style, value);
  SchedulePaint();
}

void Textfield::ApplyStyle(gfx::TextStyle style,
                           bool value,
                           const gfx::Range& range) {
  GetRenderText()->ApplyStyle(style, value, range);
  SchedulePaint();
}

bool Textfield::GetInvalid() const {
  return invalid_;
}

void Textfield::SetInvalid(bool invalid) {
  if (invalid == invalid_) {
    return;
  }
  invalid_ = invalid;
  UpdateDefaultBorder();
  if (FocusRing::Get(this)) {
    FocusRing::Get(this)->SetInvalid(invalid);
  }
  OnPropertyChanged(&invalid_, kPropertyEffectsNone);
}

void Textfield::ClearEditHistory() {
  model_->ClearEditHistory();
}

void Textfield::SetObscuredGlyphSpacing(int spacing) {
  GetRenderText()->SetObscuredGlyphSpacing(spacing);
}

void Textfield::SetExtraInsets(const gfx::Insets& insets) {
  extra_insets_ = insets;
  UpdateDefaultBorder();
}

void Textfield::FitToLocalBounds() {
  // Textfield insets include a reasonable amount of whitespace on all sides of
  // the default font list. Fallback fonts with larger heights may paint over
  // the vertical whitespace as needed. Alternate solutions involve undesirable
  // behavior like changing the default font size, shrinking some fallback fonts
  // beyond their legibility, or enlarging controls dynamically with content.
  gfx::Rect bounds = GetLocalBounds();
  const gfx::Insets insets = GetInsets();

  if (GetRenderText()->multiline()) {
    bounds.Inset(insets);
  } else {
    // The text will draw with the correct vertical alignment if we don't apply
    // the vertical insets.
    bounds.Inset(gfx::Insets::TLBR(0, insets.left(), 0, insets.right()));
  }

  bounds.set_x(GetMirroredXForRect(bounds));
  GetRenderText()->SetDisplayRect(bounds);
  UpdateAfterChange(TextChangeType::kNone, true);
}

bool Textfield::GetUseDefaultBorder() const {
  return use_default_border_;
}
void Textfield::SetUseDefaultBorder(bool use_default_border) {
  use_default_border_ = use_default_border;
}

void Textfield::RemoveHoverEffect() {
  // If no Inkdrop has been installed, this will no-op.
  InkDrop::Remove(this);
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, View overrides:

int Textfield::GetBaseline() const {
  return GetInsets().top() + GetRenderText()->GetBaseline();
}

gfx::Size Textfield::CalculatePreferredSize(
    const SizeBounds& available_size) const {
  DCHECK_GE(default_width_in_chars_, minimum_width_in_chars_);
  return gfx::Size(
      CharsToDips(default_width_in_chars_),
      LayoutProvider::GetControlHeightForFont(style::CONTEXT_TEXTFIELD,
                                              GetTextStyle(), GetFontList()));
}

gfx::Size Textfield::GetMinimumSize() const {
  DCHECK_LE(minimum_width_in_chars_, default_width_in_chars_);
  gfx::Size minimum_size = View::GetMinimumSize();
  if (minimum_width_in_chars_ >= 0) {
    minimum_size.set_width(CharsToDips(minimum_width_in_chars_));
  }
  return minimum_size;
}

void Textfield::SetBorder(std::unique_ptr<Border> b) {
  FocusRing::Remove(this);
  View::SetBorder(std::move(b));
  use_default_border_ = false;
}

ui::Cursor Textfield::GetCursor(const ui::MouseEvent& event) {
  constexpr bool platform_arrow =
      PlatformStyle::kTextfieldUsesDragCursorWhenDraggable;
  bool in_selection = GetRenderText()->IsPointInSelection(event.location());
  bool drag_event = event.type() == ui::EventType::kMouseDragged;
  bool text_cursor =
      !initiating_drag_ && (drag_event || !in_selection || !platform_arrow);
  return text_cursor ? ui::mojom::CursorType::kIBeam : ui::Cursor();
}

bool Textfield::OnMousePressed(const ui::MouseEvent& event) {
  const bool had_focus = HasFocus();
  bool handled = controller_ && controller_->HandleMouseEvent(this, event);
  if (InkDrop::Get(this)) {
    // When a textfield is pressed, the hover state should be off and the
    // background color should no longer have a mask.
    InkDrop::Get(this)->GetInkDrop()->SetHovered(false);
  }
  // If the controller triggered the focus, then record the focus reason as
  // other.
  if (!had_focus && HasFocus()) {
    focus_reason_ = ui::TextInputClient::FOCUS_REASON_OTHER;
  }

  if (!handled &&
      (event.IsOnlyLeftMouseButton() || event.IsOnlyRightMouseButton())) {
    if (!had_focus) {
      RequestFocusWithPointer(ui::EventPointerType::kMouse);
    }
#if !BUILDFLAG(IS_WIN)
    ShowVirtualKeyboardIfEnabled();
#endif
  }

  if (ui::Clipboard::IsSupportedClipboardBuffer(
          ui::ClipboardBuffer::kSelection)) {
    if (!handled && !had_focus && event.IsOnlyMiddleMouseButton()) {
      RequestFocusWithPointer(ui::EventPointerType::kMouse);
    }
  }

  return selection_controller_.OnMousePressed(
      event, handled,
      had_focus
          ? SelectionController::InitialFocusStateOnMousePress::kFocused
          : SelectionController::InitialFocusStateOnMousePress::kUnFocused);
}

bool Textfield::OnMouseDragged(const ui::MouseEvent& event) {
  return selection_controller_.OnMouseDragged(event);
}

void Textfield::OnMouseReleased(const ui::MouseEvent& event) {
  if (controller_) {
    controller_->HandleMouseEvent(this, event);
  }
  selection_controller_.OnMouseReleased(event);
}

void Textfield::OnMouseCaptureLost() {
  selection_controller_.OnMouseCaptureLost();
}

bool Textfield::OnMouseWheel(const ui::MouseWheelEvent& event) {
  return controller_ && controller_->HandleMouseEvent(this, event);
}

WordLookupClient* Textfield::GetWordLookupClient() {
  return this;
}

bool Textfield::OnKeyPressed(const ui::KeyEvent& event) {
  if (PreHandleKeyPressed(event)) {
    return true;
  }

  ui::TextEditCommand edit_command = scheduled_text_edit_command_;
  scheduled_text_edit_command_ = ui::TextEditCommand::INVALID_COMMAND;

  // Since HandleKeyEvent() might destroy |this|, get a weak pointer and verify
  // it isn't null before proceeding.
  base::WeakPtr<Textfield> textfield(weak_ptr_factory_.GetWeakPtr());

  bool handled = controller_ && controller_->HandleKeyEvent(this, event);

  if (!textfield) {
    return handled;
  }

#if BUILDFLAG(IS_LINUX)
  if (!handled) {
    if (auto* linux_ui = ui::LinuxUi::instance()) {
      const auto command =
          linux_ui->GetTextEditCommandForEvent(event, ui::TEXT_INPUT_FLAG_NONE);
      if (command != ui::TextEditCommand::INVALID_COMMAND) {
        if (IsTextEditCommandEnabled(command)) {
          ExecuteTextEditCommand(command);
          return true;
        }
        return false;
      }
    }
  }
#endif

  base::AutoReset<bool> show_rejection_ui(&show_rejection_ui_if_any_, true);

  if (edit_command == ui::TextEditCommand::INVALID_COMMAND) {
    edit_command = GetCommandForKeyEvent(event);
  }

  if (!handled && IsTextEditCommandEnabled(edit_command)) {
    ExecuteTextEditCommand(edit_command);
    handled = true;
  }
  return handled;
}

bool Textfield::OnKeyReleased(const ui::KeyEvent& event) {
  return controller_ && controller_->HandleKeyEvent(this, event);
}

void Textfield::OnGestureEvent(ui::GestureEvent* event) {
  switch (event->type()) {
    case ui::EventType::kGestureTap: {
      RequestFocusForGesture(event->details());
      if (controller_ && controller_->HandleGestureEvent(this, *event)) {
        StopSelectionDragging();
        event->SetHandled();
        return;
      }
      if (HandleGestureForSelectionDragging(event)) {
        return;
      }
      const size_t tap_pos =
          GetRenderText()->FindCursorPosition(event->location()).caret_pos();
      const bool should_toggle_menu = event->details().tap_count() == 1 &&
                                      GetSelectedRange() == gfx::Range(tap_pos);
      if (event->details().tap_count() == 1) {
        // If tap is on the selection and touch handles are not present,
        // handles should be shown without changing selection. Otherwise,
        // cursor should be moved to the tap location.
        if (touch_selection_controller_ ||
            !GetRenderText()->IsPointInSelection(event->location())) {
          OnBeforeUserAction();
          MoveCursorTo(event->location(), false);
          OnAfterUserAction();
        }
      } else if (event->details().tap_count() == 2) {
        OnBeforeUserAction();
        SelectWordAt(event->location());
        OnAfterUserAction();
      } else {
        OnBeforeUserAction();
        SelectAll(false);
        OnAfterUserAction();
      }
      CreateTouchSelectionControllerAndNotifyIt();
      if (touch_selection_controller_ && should_toggle_menu) {
        touch_selection_controller_->ToggleQuickMenu();
      }
      event->SetHandled();
      break;
    }
    case ui::EventType::kGestureTapDown: {
      if (HasFocus()) {
        if (HandleGestureForSelectionDragging(event)) {
          return;
        }
      }
      break;
    }
    case ui::EventType::kGestureLongPress:
      if (GetRenderText()->IsPointInSelection(event->location())) {
        // If long-press happens on the selection, deactivate touch selection
        // and try to initiate drag-drop. If drag-drop is not enabled, context
        // menu will be shown. Event is not marked as handled to let Views
        // handle drag-drop or context menu.
        DestroyTouchSelection();
        StopSelectionDragging();
        initiating_drag_ = ::features::IsTouchDragAndDropEnabled();
        break;
      } else {
        // If long-press happens outside selection, select word and try to
        // activate touch selection.
        OnBeforeUserAction();
        SelectWordAt(event->location());
        OnAfterUserAction();
        CreateTouchSelectionControllerAndNotifyIt();

        if (HandleGestureForSelectionDragging(event)) {
          return;
        }

        // If touch selection is activated, mark the event as handled so that
        // the regular context menu is not shown.
        if (touch_selection_controller_) {
          event->SetHandled();
          return;
        }
      }
      break;
    case ui::EventType::kGestureLongTap:
      if (HandleGestureForSelectionDragging(event)) {
        return;
      }
      // If touch selection is enabled, the context menu on long tap will be
      // shown by the |touch_selection_controller_|, hence we mark the event
      // handled so Views does not try to show context menu on it.
      if (touch_selection_controller_) {
        event->SetHandled();
      }
      break;
    case ui::EventType::kGestureScrollBegin:
      if (HasFocus()) {
        if (HandleGestureForSelectionDragging(event)) {
          return;
        }
        OnGestureScrollBegin(event->location().x());
        event->SetHandled();
      }
      break;
    case ui::EventType::kGestureScrollUpdate:
      if (HasFocus()) {
        if (HandleGestureForSelectionDragging(event)) {
          return;
        }
        GestureScroll(event->location().x());
        event->SetHandled();
      }
      break;
    case ui::EventType::kGestureScrollEnd:
    case ui::EventType::kScrollFlingStart: {
      const bool gesture_handled = HandleGestureForSelectionDragging(event);
      CHECK(!gesture_handled);
      if (HasFocus()) {
        if (show_touch_handles_after_scroll_) {
          CreateTouchSelectionControllerAndNotifyIt();
          show_touch_handles_after_scroll_ = false;
        }
        event->SetHandled();
      }
      break;
    }
    case ui::EventType::kGestureEnd: {
      const bool gesture_handled = HandleGestureForSelectionDragging(event);
      CHECK(!gesture_handled);
      break;
    }
    default:
      return;
  }
}

// This function is called by BrowserView to execute clipboard commands.
bool Textfield::AcceleratorPressed(const ui::Accelerator& accelerator) {
  ui::KeyEvent event(
      accelerator.key_state() == ui::Accelerator::KeyState::PRESSED
          ? ui::EventType::kKeyPressed
          : ui::EventType::kKeyReleased,
      accelerator.key_code(), accelerator.modifiers());
  ExecuteTextEditCommand(GetCommandForKeyEvent(event));
  return true;
}

bool Textfield::CanHandleAccelerators() const {
  return GetRenderText()->focused() && View::CanHandleAccelerators();
}

void Textfield::AboutToRequestFocusFromTabTraversal(bool reverse) {
  SelectAll(false);
}

bool Textfield::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
#if BUILDFLAG(IS_LINUX)
  // Skip any accelerator handling that conflicts with custom keybindings.
  if (auto* linux_ui = ui::LinuxUi::instance()) {
    if (IsTextEditCommandEnabled(linux_ui->GetTextEditCommandForEvent(
            event, ui::TEXT_INPUT_FLAG_NONE))) {
      return true;
    }
  }
#endif

  // Skip backspace accelerator handling; editable textfields handle this key.
  // Also skip processing Windows [Alt]+<num-pad digit> Unicode alt-codes.
  const bool is_backspace = event.key_code() == ui::VKEY_BACK;
  return (is_backspace && !GetReadOnly()) || event.IsUnicodeKeyCode();
}

bool Textfield::GetDropFormats(
    int* formats,
    std::set<ui::ClipboardFormatType>* format_types) {
  if (!GetEnabled() || GetReadOnly()) {
    return false;
  }
  // TODO(msw): Can we support URL, FILENAME, etc.?
  *formats = ui::OSExchangeData::STRING;
  if (controller_) {
    controller_->AppendDropFormats(formats, format_types);
  }
  return true;
}

bool Textfield::CanDrop(const OSExchangeData& data) {
  int formats;
  std::set<ui::ClipboardFormatType> format_types;
  GetDropFormats(&formats, &format_types);
  return GetEnabled() && !GetReadOnly() &&
         data.HasAnyFormat(formats, format_types);
}

int Textfield::OnDragUpdated(const ui::DropTargetEvent& event) {
  DCHECK(CanDrop(event.data()));
  gfx::RenderText* render_text = GetRenderText();
  const gfx::Range& selection = render_text->selection();
  drop_cursor_position_ = render_text->FindCursorPosition(event.location());
  bool in_selection =
      !selection.is_empty() &&
      selection.Contains(gfx::Range(drop_cursor_position_.caret_pos()));
  drop_cursor_visible_ = !in_selection;
  // TODO(msw): Pan over text when the user drags to the visible text edge.
  OnCaretBoundsChanged();
  SchedulePaint();

  StopBlinkingCursor();

  if (initiating_drag_) {
    if (in_selection) {
      return ui::DragDropTypes::DRAG_NONE;
    }
    return event.IsControlDown() ? ui::DragDropTypes::DRAG_COPY
                                 : ui::DragDropTypes::DRAG_MOVE;
  }
  return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_MOVE;
}

void Textfield::OnDragExited() {
  drop_cursor_visible_ = false;
  if (ShouldBlinkCursor()) {
    StartBlinkingCursor();
  }
  SchedulePaint();
}

views::View::DropCallback Textfield::GetDropCallback(
    const ui::DropTargetEvent& event) {
  DCHECK(CanDrop(event.data()));

  drop_cursor_visible_ = false;

  if (controller_) {
    auto cb = controller_->CreateDropCallback(event);
    if (!cb.is_null()) {
      return cb;
    }
  }

  DCHECK(!initiating_drag_ ||
         !GetRenderText()->IsPointInSelection(event.location()));

  return base::BindOnce(&Textfield::DropDraggedText,
                        drop_weak_ptr_factory_.GetWeakPtr());
}

void Textfield::OnDragDone() {
  initiating_drag_ = false;
  drop_cursor_visible_ = false;
}

void Textfield::OnAccessibilityInitializing(ui::AXNodeData* data) {
#if BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)
  // TODO(crbug.com/40933356): Add support for multiline textfields.
  if (::ui::AXPlatform::GetInstance().IsUiaProviderEnabled() &&
      !GetRenderText()->multiline()) {
    ax_value_used_to_compute_offsets_ = GetRenderText()->GetDisplayText();

    data->AddIntListAttribute(ax::mojom::IntListAttribute::kCharacterOffsets,
                              ComputeTextOffsets(GetRenderText()));
    WordBoundaries boundaries = ComputeWordBoundaries(GetText());
    data->AddIntListAttribute(ax::mojom::IntListAttribute::kWordStarts,
                              boundaries.starts);
    data->AddIntListAttribute(ax::mojom::IntListAttribute::kWordEnds,
                              boundaries.ends);
  }
#endif  // BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)
}

#if BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)
void Textfield::UpdateAccessibleTextOffsetsIfNeeded() {
  bool should_update =
      ax_value_used_to_compute_offsets_ != GetRenderText()->GetDisplayText();
  if (::ui::AXPlatform::GetInstance().IsUiaProviderEnabled() &&
      GetViewAccessibility().is_initialized() && should_update) {
    GetViewAccessibility().ClearTextOffsets();

    RefreshAccessibleTextOffsets();
    ax_value_used_to_compute_offsets_ = GetRenderText()->GetDisplayText();
  }
}

void Textfield::RefreshAccessibleTextOffsets() {
  DCHECK(::ui::AXPlatform::GetInstance().IsUiaProviderEnabled());
  DCHECK(GetViewAccessibility().is_initialized());
  // TODO(crbug.com/40933356): Add support for multiline textfields.
  if (GetRenderText()->multiline()) {
    return;
  }

  GetViewAccessibility().SetCharacterOffsets(
      ComputeTextOffsets(GetRenderText()));

  WordBoundaries boundaries = ComputeWordBoundaries(GetText());
  GetViewAccessibility().SetWordStarts(boundaries.starts);
  GetViewAccessibility().SetWordEnds(boundaries.ends);
}
#endif  // BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)

bool Textfield::HandleAccessibleAction(const ui::AXActionData& action_data) {
  if (action_data.action == ax::mojom::Action::kSetSelection) {
    DCHECK_EQ(action_data.anchor_node_id, action_data.focus_node_id);
    const gfx::Range range(static_cast<size_t>(action_data.anchor_offset),
                           static_cast<size_t>(action_data.focus_offset));
    return SetEditableSelectionRange(range);
  }

  // Remaining actions cannot be performed on readonly fields.
  if (GetReadOnly()) {
    return View::HandleAccessibleAction(action_data);
  }

  if (action_data.action == ax::mojom::Action::kSetValue) {
    SetText(base::UTF8ToUTF16(action_data.value));
    ClearSelection();
    return true;
  } else if (action_data.action == ax::mojom::Action::kReplaceSelectedText) {
    InsertOrReplaceText(base::UTF8ToUTF16(action_data.value));
    ClearSelection();
    return true;
  }

  return View::HandleAccessibleAction(action_data);
}

void Textfield::OnBoundsChanged(const gfx::Rect& previous_bounds) {
  FitToLocalBounds();
}

bool Textfield::GetNeedsNotificationWhenVisibleBoundsChange() const {
  return true;
}

void Textfield::OnVisibleBoundsChanged() {
  if (touch_selection_controller_) {
    touch_selection_controller_->SelectionChanged();
  }
}

void Textfield::OnPaint(gfx::Canvas* canvas) {
  OnPaintBackground(canvas);
  PaintTextAndCursor(canvas);
  OnPaintBorder(canvas);
}

void Textfield::OnFocus() {
  is_processing_focus_ = true;

  // Set focus reason if focused was gained without mouse or touch input.
  if (focus_reason_ == ui::TextInputClient::FOCUS_REASON_NONE) {
    focus_reason_ = ui::TextInputClient::FOCUS_REASON_OTHER;
  }

#if BUILDFLAG(IS_MAC)
  if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD) {
    password_input_enabler_ =
        std::make_unique<ui::ScopedPasswordInputEnabler>();
  }
#endif  // BUILDFLAG(IS_MAC)

  GetRenderText()->set_focused(true);
  if (GetInputMethod()) {
    GetInputMethod()->SetFocusedTextInputClient(this);
  }
  UpdateAfterChange(TextChangeType::kNone, true);
  View::OnFocus();

  is_processing_focus_ = false;
}

void Textfield::OnBlur() {
  focus_reason_ = ui::TextInputClient::FOCUS_REASON_NONE;

  gfx::RenderText* render_text = GetRenderText();
  render_text->set_focused(false);

  if (GetInputMethod()) {
    GetInputMethod()->DetachTextInputClient(this);
#if BUILDFLAG(IS_CHROMEOS)
    wm::RestoreWindowBoundsOnClientFocusLost(
        GetNativeView()->GetToplevelWindow());
#endif  // BUILDFLAG(IS_CHROMEOS)
  }
  StopBlinkingCursor();
  cursor_view_->SetVisible(false);

  DestroyTouchSelection();

  SchedulePaint();
  View::OnBlur();

#if BUILDFLAG(IS_MAC)
  password_input_enabler_.reset();
#endif  // BUILDFLAG(IS_MAC)
}

gfx::Point Textfield::GetKeyboardContextMenuLocation() {
  return GetCaretBounds().bottom_right();
}

void Textfield::OnThemeChanged() {
  View::OnThemeChanged();
  gfx::RenderText* render_text = GetRenderText();
  SetColor(GetTextColor());
  UpdateBackgroundColor();
  render_text->set_selection_color(GetSelectionTextColor());
  render_text->set_selection_background_focused_color(
      GetSelectionBackgroundColor());
  cursor_view_->layer()->SetColor(GetTextColor());
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, TextfieldModel::Delegate overrides:

void Textfield::OnCompositionTextConfirmedOrCleared() {
  if (!skip_input_method_cancel_composition_) {
    GetInputMethod()->CancelComposition(this);
  }
}

void Textfield::OnTextChanged() {
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldText),
      kPropertyEffectsPaint);
  drop_weak_ptr_factory_.InvalidateWeakPtrs();
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, ContextMenuController overrides:

void Textfield::ShowContextMenuForViewImpl(
    View* source,
    const gfx::Point& point,
    ui::mojom::MenuSourceType source_type) {
  UpdateContextMenu();
  context_menu_runner_->RunMenuAt(GetWidget(), nullptr,
                                  gfx::Rect(point, gfx::Size()),
                                  MenuAnchorPosition::kTopLeft, source_type);
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, DragController overrides:

void Textfield::WriteDragDataForView(View* sender,
                                     const gfx::Point& press_pt,
                                     OSExchangeData* data) {
  const std::u16string_view selected_text = GetSelectedText();
  data->SetString(selected_text);
  Label label(selected_text, {GetFontList()});
  label.SetBackgroundColor(GetBackgroundColor());
  label.SetSubpixelRenderingEnabled(false);
  gfx::Size size(label.GetPreferredSize({}));
  gfx::NativeView native_view = GetWidget()->GetNativeView();
  display::Display display =
      display::Screen::GetScreen()->GetDisplayNearestView(native_view);
  size.SetToMin(gfx::Size(display.size().width(), height()));
  label.SetBoundsRect(gfx::Rect(size));
  label.SetEnabledColor(GetTextColor());

  SkBitmap bitmap;
  float raster_scale = ScaleFactorForDragFromWidget(GetWidget());
  SkColor color = views::Widget::IsWindowCompositingSupported()
                      ? SK_ColorTRANSPARENT
                      : GetBackgroundColor();
  label.Paint(PaintInfo::CreateRootPaintInfo(
      ui::CanvasPainter(&bitmap, label.size(), raster_scale, color,
                        GetWidget()->GetCompositor()->is_pixel_canvas())
          .context(),
      label.size()));
  constexpr gfx::Vector2d kOffset(-15, 0);
  gfx::ImageSkia image = gfx::ImageSkia::CreateFromBitmap(bitmap, raster_scale);
  data->provider().SetDragImage(image, kOffset);
  if (controller_) {
    controller_->OnWriteDragData(data);
  }
}

int Textfield::GetDragOperationsForView(View* sender, const gfx::Point& p) {
  int drag_operations = ui::DragDropTypes::DRAG_COPY;
  if (!GetEnabled() || text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD ||
      !GetRenderText()->IsPointInSelection(p)) {
    drag_operations = ui::DragDropTypes::DRAG_NONE;
  } else if (sender == this && !GetReadOnly()) {
    drag_operations =
        ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY;
  }
  if (controller_) {
    controller_->OnGetDragOperationsForTextfield(&drag_operations);
  }
  return drag_operations;
}

bool Textfield::CanStartDragForView(View* sender,
                                    const gfx::Point& press_pt,
                                    const gfx::Point& p) {
  return initiating_drag_ && GetRenderText()->IsPointInSelection(press_pt);
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, WordLookupClient overrides:

bool Textfield::GetWordLookupDataAtPoint(const gfx::Point& point,
                                         gfx::DecoratedText* decorated_word,
                                         gfx::Rect* rect) {
  return GetRenderText()->GetWordLookupDataAtPoint(point, decorated_word, rect);
}

bool Textfield::GetWordLookupDataFromSelection(
    gfx::DecoratedText* decorated_text,
    gfx::Rect* rect) {
  if (GetRenderText()->obscured()) {
    return false;
  }
  return GetRenderText()->GetLookupDataForRange(GetRenderText()->selection(),
                                                decorated_text, rect);
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, SelectionControllerDelegate overrides:

bool Textfield::HasTextBeingDragged() const {
  return initiating_drag_;
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, ui::TouchEditable overrides:

void Textfield::MoveCaret(const gfx::Point& position) {
  SelectBetweenCoordinates(position, position);
}

void Textfield::MoveRangeSelectionExtent(const gfx::Point& extent) {
  if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
    return;
  }

  gfx::RenderText* render_text = GetRenderText();
  if (!::features::IsTouchTextEditingRedesignEnabled()) {
    gfx::SelectionModel base_caret =
        render_text->GetSelectionModelForSelectionStart();
    gfx::SelectionModel extent_caret = render_text->FindCursorPosition(extent);
    gfx::SelectionModel selection_model(
        gfx::Range(base_caret.caret_pos(), extent_caret.caret_pos()),
        extent_caret.caret_affinity());

    OnBeforeUserAction();
    SelectSelectionModel(selection_model);
    OnAfterUserAction();
    return;
  }

  const gfx::Range selection = GetSelectedRange();
  const gfx::SelectionModel cursor_position_at_old_extent =
      render_text->FindCursorPosition(selection_extent_);
  const gfx::SelectionModel cursor_position_at_new_extent =
      render_text->FindCursorPosition(extent);

  if (render_text->GetLineContainingCaret(cursor_position_at_old_extent) !=
      render_text->GetLineContainingCaret(cursor_position_at_new_extent)) {
    // Reset the offset if a line change has occurred.
    extent_offset_x_ = 0;
  } else {
    // Otherwise, if the extent has moved in the direction of the offset, reduce
    // the amount of offset.
    const int dx = extent.x() - selection_extent_.x();
    if (extent_offset_x_ > 0 && dx > 0) {
      extent_offset_x_ = std::max(0, extent_offset_x_ - dx);
    } else if (extent_offset_x_ < 0 && dx < 0) {
      extent_offset_x_ = std::min(0, extent_offset_x_ - dx);
    }
  }

  const gfx::Point old_extent_with_offset =
      selection_extent_ + gfx::Vector2d(extent_offset_x_, 0);
  const size_t caret_pos_at_old_extent_with_offset =
      render_text->FindCursorPosition(old_extent_with_offset).caret_pos();
  gfx::Point new_extent_with_offset =
      extent + gfx::Vector2d(extent_offset_x_, 0);
  size_t caret_pos_at_new_extent_with_offset =
      render_text->FindCursorPosition(new_extent_with_offset).caret_pos();

  // Determine whether we need to switch between character and word
  // granularity and update the offset again if necessary.
  if (break_type_ == gfx::CHARACTER_BREAK) {
    // Switch to word granularity only after the selection has expanded past a
    // word boundary. This ensures that the selection can be adjusted by
    // character within a word after the selection has shrunk.
    const bool selection_expanding =
        selection.is_reversed() ? caret_pos_at_new_extent_with_offset <
                                      caret_pos_at_old_extent_with_offset
                                : caret_pos_at_new_extent_with_offset >
                                      caret_pos_at_old_extent_with_offset;
    const gfx::Range nearest_word_boundaries =
        render_text->ExpandRangeToWordBoundary(selection);
    const bool extent_moved_past_next_word_boundary =
        caret_pos_at_new_extent_with_offset <=
            nearest_word_boundaries.GetMin() ||
        caret_pos_at_new_extent_with_offset >= nearest_word_boundaries.GetMax();
    if (selection_expanding && extent_moved_past_next_word_boundary) {
      break_type_ = gfx::WORD_BREAK;
      extent_offset_x_ = 0;
    }
  } else {
    const bool selection_shrinking =
        selection.is_reversed() ? caret_pos_at_new_extent_with_offset >
                                      caret_pos_at_old_extent_with_offset
                                : caret_pos_at_new_extent_with_offset <
                                      caret_pos_at_old_extent_with_offset;
    if (selection_shrinking) {
      break_type_ = gfx::CHARACTER_BREAK;
      const gfx::Rect cursor_bounds =
          render_text->GetCursorBounds(GetSelectionModel(), true);
      extent_offset_x_ =
          cursor_bounds.CenterPoint().x() - selection_extent_.x();
    }
  }
  selection_extent_ = extent;
  new_extent_with_offset = extent + gfx::Vector2d(extent_offset_x_, 0);
  caret_pos_at_new_extent_with_offset =
      render_text->FindCursorPosition(new_extent_with_offset).caret_pos();

  size_t end = caret_pos_at_new_extent_with_offset;
  if (break_type_ == gfx::WORD_BREAK) {
    // Move the selection end to the nearest word boundary.
    const gfx::Range nearest_word_boundaries =
        render_text->ExpandRangeToWordBoundary(gfx::Range(end));
    DCHECK_GE(end, nearest_word_boundaries.start());
    DCHECK_LE(end, nearest_word_boundaries.end());
    end = end - nearest_word_boundaries.start() <
                  nearest_word_boundaries.end() - end
              ? nearest_word_boundaries.start()
              : nearest_word_boundaries.end();
  }

  // No need to update the selection if the end is still the same.
  if (end == selection.end()) {
    return;
  }

  const size_t start = selection.start();
  // Don't let the selection become empty.
  if (start == end) {
    return;
  }

  const gfx::LogicalCursorDirection affinity =
      start > end ? gfx::CURSOR_FORWARD : gfx::CURSOR_BACKWARD;
  OnBeforeUserAction();
  SelectSelectionModel(gfx::SelectionModel(gfx::Range(start, end), affinity));
  OnAfterUserAction();
}

void Textfield::SelectBetweenCoordinates(const gfx::Point& base,
                                         const gfx::Point& extent) {
  if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
    return;
  }

  gfx::SelectionModel base_caret = GetRenderText()->FindCursorPosition(base);
  gfx::SelectionModel extent_caret =
      GetRenderText()->FindCursorPosition(extent);
  gfx::SelectionModel selection(
      gfx::Range(base_caret.caret_pos(), extent_caret.caret_pos()),
      extent_caret.caret_affinity());

  OnBeforeUserAction();
  SelectSelectionModel(selection);
  OnAfterUserAction();

  selection_extent_ = extent;
  extent_offset_x_ = 0;
  break_type_ = gfx::CHARACTER_BREAK;
}

void Textfield::GetSelectionEndPoints(gfx::SelectionBound* anchor,
                                      gfx::SelectionBound* focus) {
  gfx::RenderText* render_text = GetRenderText();
  const gfx::SelectionModel& sel = render_text->selection_model();
  gfx::SelectionModel start_sel =
      render_text->GetSelectionModelForSelectionStart();
  gfx::Rect r1 = render_text->GetCursorBounds(start_sel, true);
  gfx::Rect r2 = render_text->GetCursorBounds(sel, true);

  anchor->SetEdge(gfx::PointF(r1.origin()), gfx::PointF(r1.bottom_left()));
  focus->SetEdge(gfx::PointF(r2.origin()), gfx::PointF(r2.bottom_left()));

  // Determine the SelectionBound's type for focus and anchor.
  // TODO(mfomitchev): Ideally we should have different logical directions for
  // start and end to support proper handle direction for mixed LTR/RTL text.
  const bool ltr = GetTextDirection() != base::i18n::RIGHT_TO_LEFT;
  size_t anchor_position_index = sel.selection().start();
  size_t focus_position_index = sel.selection().end();

  if (anchor_position_index == focus_position_index) {
    anchor->set_type(gfx::SelectionBound::CENTER);
    focus->set_type(gfx::SelectionBound::CENTER);
  } else if ((ltr && anchor_position_index < focus_position_index) ||
             (!ltr && anchor_position_index > focus_position_index)) {
    anchor->set_type(gfx::SelectionBound::LEFT);
    focus->set_type(gfx::SelectionBound::RIGHT);
  } else {
    anchor->set_type(gfx::SelectionBound::RIGHT);
    focus->set_type(gfx::SelectionBound::LEFT);
  }
}

gfx::Rect Textfield::GetBounds() {
  return GetLocalBounds();
}

gfx::NativeView Textfield::GetNativeView() const {
  return GetWidget()->GetNativeView();
}

bool Textfield::IsSelectionDragging() const {
  return selection_dragging_state_ == SelectionDraggingState::kDraggingCursor ||
         selection_dragging_state_ ==
             SelectionDraggingState::kDraggingSelectionExtent;
}

void Textfield::ConvertPointToScreen(gfx::Point* point) {
  View::ConvertPointToScreen(this, point);
}

void Textfield::ConvertPointFromScreen(gfx::Point* point) {
  View::ConvertPointFromScreen(this, point);
}

void Textfield::OpenContextMenu(const gfx::Point& anchor) {
  DestroyTouchSelection();
  ShowContextMenu(anchor, ui::mojom::MenuSourceType::kTouchEditMenu);
}

void Textfield::DestroyTouchSelection() {
  touch_selection_controller_.reset();
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, ui::SimpleMenuModel::Delegate overrides:

bool Textfield::IsCommandIdChecked(int command_id) const {
  if (text_services_context_menu_ &&
      text_services_context_menu_->SupportsCommand(command_id)) {
    return text_services_context_menu_->IsCommandIdChecked(command_id);
  }

  return true;
}

bool Textfield::IsCommandIdEnabled(int command_id) const {
  if (text_services_context_menu_ &&
      text_services_context_menu_->SupportsCommand(command_id)) {
    return text_services_context_menu_->IsCommandIdEnabled(command_id);
  }

  return IsTextEditCommandEnabled(
      GetTextEditCommandFromMenuCommand(command_id, HasSelection()));
}

bool Textfield::GetAcceleratorForCommandId(int command_id,
                                           ui::Accelerator* accelerator) const {
  switch (command_id) {
    case kUndo:
      *accelerator = ui::Accelerator(ui::VKEY_Z, ui::EF_PLATFORM_ACCELERATOR);
      return true;

    case kCut:
      *accelerator = ui::Accelerator(ui::VKEY_X, ui::EF_PLATFORM_ACCELERATOR);
      return true;

    case kCopy:
      *accelerator = ui::Accelerator(ui::VKEY_C, ui::EF_PLATFORM_ACCELERATOR);
      return true;

    case kPaste:
      *accelerator = ui::Accelerator(ui::VKEY_V, ui::EF_PLATFORM_ACCELERATOR);
      return true;

    case kSelectAll:
      *accelerator = ui::Accelerator(ui::VKEY_A, ui::EF_PLATFORM_ACCELERATOR);
      return true;

    default:
      return text_services_context_menu_->GetAcceleratorForCommandId(
          command_id, accelerator);
  }
}

void Textfield::ExecuteCommand(int command_id, int event_flags) {
  if (text_services_context_menu_ &&
      text_services_context_menu_->SupportsCommand(command_id)) {
    text_services_context_menu_->ExecuteCommand(command_id, event_flags);
    return;
  }

  Textfield::ExecuteTextEditCommand(
      GetTextEditCommandFromMenuCommand(command_id, HasSelection()));

  if (::features::IsTouchTextEditingRedesignEnabled() &&
      (event_flags & ui::EF_FROM_TOUCH) &&
      (command_id == Textfield::kSelectAll ||
       command_id == Textfield::kSelectWord)) {
    CreateTouchSelectionControllerAndNotifyIt();
  }
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, ui::TextInputClient overrides:

base::WeakPtr<ui::TextInputClient> Textfield::AsWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void Textfield::SetCompositionText(const ui::CompositionText& composition) {
  if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
    return;
  }

  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;
  model_->SetCompositionText(composition);
  skip_input_method_cancel_composition_ = false;
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
}

size_t Textfield::ConfirmCompositionText(bool keep_selection) {
  // TODO(b/134473433) Modify this function so that when keep_selection is
  // true, the selection is not changed when text committed
  if (keep_selection) {
    NOTIMPLEMENTED_LOG_ONCE();
  }
  if (!model_->HasCompositionText()) {
    return 0;
  }
  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;
  const size_t confirmed_text_length = model_->ConfirmCompositionText();
  skip_input_method_cancel_composition_ = false;
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
  return confirmed_text_length;
}

void Textfield::ClearCompositionText() {
  if (!model_->HasCompositionText()) {
    return;
  }

  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;
  model_->CancelCompositionText();
  skip_input_method_cancel_composition_ = false;
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
}

void Textfield::InsertText(const std::u16string& new_text,
                           InsertTextCursorBehavior cursor_behavior) {
  std::u16string filtered_new_text;
  std::ranges::copy_if(new_text, std::back_inserter(filtered_new_text),
                       IsValidCharToInsert);

  if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE ||
      filtered_new_text.empty()) {
    return;
  }

  // TODO(crbug.com/1155331): Handle |cursor_behavior| correctly.
  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;
  model_->InsertText(filtered_new_text);
  skip_input_method_cancel_composition_ = false;
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
}

void Textfield::InsertChar(const ui::KeyEvent& event) {
  if (GetReadOnly()) {
    OnEditFailed();
    return;
  }

  // Filter all invalid chars and all characters with Alt modifier (and Search
  // on ChromeOS, Ctrl on Linux). But allow characters with the AltGr modifier.
  // On Windows AltGr is represented by Alt+Ctrl or Right Alt, and on Linux it's
  // a different flag that we don't care about.
  const char16_t ch = event.GetCharacter();
  const bool should_insert_char = IsValidCharToInsert(ch) &&
                                  !ui::IsSystemKeyModifier(event.flags()) &&
                                  !IsControlKeyModifier(event.flags());
  if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || !should_insert_char) {
    return;
  }

  DoInsertChar(ch);

  if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD) {
    password_char_reveal_index_ = std::nullopt;
    base::TimeDelta duration = GetPasswordRevealDuration(event);
    if (!duration.is_zero()) {
      const size_t change_offset = model_->GetCursorPosition();
      DCHECK_GT(change_offset, 0u);
      RevealPasswordChar(change_offset - 1, duration);
    }
  }
  UpdateAccessibleTextSelection();
}

ui::TextInputType Textfield::GetTextInputType() const {
  if (GetReadOnly() || !GetEnabled()) {
    return ui::TEXT_INPUT_TYPE_NONE;
  }
  return text_input_type_;
}

ui::TextInputMode Textfield::GetTextInputMode() const {
  return ui::TEXT_INPUT_MODE_DEFAULT;
}

base::i18n::TextDirection Textfield::GetTextDirection() const {
  return GetRenderText()->GetDisplayTextDirection();
}

int Textfield::GetTextInputFlags() const {
  return text_input_flags_;
}

bool Textfield::CanComposeInline() const {
  return true;
}

// TODO(mbid): GetCaretBounds is const but calls
// RenderText::GetUpdatedCursorBounds, which is not const and in fact mutates
// internal state. (Is it at least logically const?) Violation of const
// correctness?
gfx::Rect Textfield::GetCaretBounds() const {
  gfx::Rect rect = GetRenderText()->GetUpdatedCursorBounds();
  ConvertRectToScreen(this, &rect);
  return rect;
}

gfx::Rect Textfield::GetSelectionBoundingBox() const {
  NOTIMPLEMENTED_LOG_ONCE();
  return gfx::Rect();
}

#if BUILDFLAG(IS_WIN)
std::optional<gfx::Rect> Textfield::GetProximateCharacterBounds(
    const gfx::Range& range) const {
  NOTIMPLEMENTED_LOG_ONCE();
  return std::nullopt;
}

std::optional<size_t> Textfield::GetProximateCharacterIndexFromPoint(
    const gfx::Point& screen_point_in_dips,
    ui::IndexFromPointFlags flags) const {
  NOTIMPLEMENTED_LOG_ONCE();
  return std::nullopt;
}
#endif  // BUILDFLAG(IS_WIN)

bool Textfield::GetCompositionCharacterBounds(size_t index,
                                              gfx::Rect* rect) const {
  DCHECK(rect);
  if (!HasCompositionText()) {
    return false;
  }
  gfx::Range composition_range;
  model_->GetCompositionTextRange(&composition_range);
  DCHECK(!composition_range.is_empty());

  size_t text_index = composition_range.start() + index;
  if (composition_range.end() <= text_index) {
    return false;
  }
  gfx::RenderText* render_text = GetRenderText();
  if (!render_text->IsValidCursorIndex(text_index)) {
    text_index =
        render_text->IndexOfAdjacentGrapheme(text_index, gfx::CURSOR_BACKWARD);
  }
  if (text_index < composition_range.start()) {
    return false;
  }
  const gfx::SelectionModel caret(text_index, gfx::CURSOR_BACKWARD);
  *rect = render_text->GetCursorBounds(caret, false);
  ConvertRectToScreen(this, rect);
  return true;
}

bool Textfield::HasCompositionText() const {
  return model_->HasCompositionText();
}

ui::TextInputClient::FocusReason Textfield::GetFocusReason() const {
  return focus_reason_;
}

bool Textfield::GetTextRange(gfx::Range* range) const {
  if (!ImeEditingAllowed()) {
    return false;
  }

  model_->GetTextRange(range);
  return true;
}

bool Textfield::GetCompositionTextRange(gfx::Range* range) const {
  if (!ImeEditingAllowed()) {
    return false;
  }

  model_->GetCompositionTextRange(range);
  return true;
}

bool Textfield::GetEditableSelectionRange(gfx::Range* range) const {
  if (!ImeEditingAllowed()) {
    return false;
  }
  *range = GetRenderText()->selection();
  return true;
}

bool Textfield::SetEditableSelectionRange(const gfx::Range& range) {
  if (!ImeEditingAllowed() || !range.IsValid()) {
    return false;
  }
  OnBeforeUserAction();
  SetSelectedRange(range);
  OnAfterUserAction();
  return true;
}

bool Textfield::DeleteRange(const gfx::Range& range) {
  if (!ImeEditingAllowed() || range.is_empty()) {
    return false;
  }

  OnBeforeUserAction();
  model_->SelectRange(range);
  if (model_->HasSelection()) {
    model_->DeleteSelection();
    UpdateAfterChange(TextChangeType::kUserTriggered, true);
  }
  OnAfterUserAction();
  return true;
}

bool Textfield::GetTextFromRange(const gfx::Range& range,
                                 std::u16string* range_text) const {
  if (!ImeEditingAllowed() || !range.IsValid()) {
    return false;
  }

  gfx::Range text_range;
  if (!GetTextRange(&text_range) || !range.IsBoundedBy(text_range)) {
    return false;
  }

  *range_text = model_->GetTextFromRange(range);
  return true;
}

void Textfield::OnInputMethodChanged() {}

bool Textfield::ChangeTextDirectionAndLayoutAlignment(
    base::i18n::TextDirection direction) {
  DCHECK_NE(direction, base::i18n::UNKNOWN_DIRECTION);
  // Restore text directionality mode when the indicated direction matches the
  // current forced mode; otherwise, force the mode indicated. This helps users
  // manage BiDi text layout without getting stuck in forced LTR or RTL modes.
  const bool default_rtl = direction == base::i18n::RIGHT_TO_LEFT;
  const auto new_mode = default_rtl ? gfx::DIRECTIONALITY_FORCE_RTL
                                    : gfx::DIRECTIONALITY_FORCE_LTR;
  auto* render_text = GetRenderText();
  const bool modes_match = new_mode == render_text->directionality_mode();
  render_text->SetDirectionalityMode(modes_match ? gfx::DIRECTIONALITY_FROM_TEXT
                                                 : new_mode);
  // Do not reset horizontal alignment to left/right if it has been set to
  // center, or if it depends on the text direction and that direction has not
  // been modified.
  bool dir_from_text =
      modes_match && GetHorizontalAlignment() == gfx::ALIGN_TO_HEAD;
  if (!dir_from_text && GetHorizontalAlignment() != gfx::ALIGN_CENTER) {
    SetHorizontalAlignment(default_rtl ? gfx::ALIGN_RIGHT : gfx::ALIGN_LEFT);
  }
  SchedulePaint();
  UpdateAccessibilityTextDirection();
  return true;
}

void Textfield::ExtendSelectionAndDelete(size_t before, size_t after) {
  gfx::Range range = GetRenderText()->selection();
  // Discard out-of-bound operations.
  // TODO(crbug.com/40852753): this is a temporary fix to prevent the
  // checked_cast failure in gfx::Range. There does not seem to be any
  // observable bad behaviors before checked_cast was added. However, range
  // clipping or dropping should be the last resort because a checkfail
  // indicates that we run into bad states somewhere earlier on the stack.
  if (range.start() < before) {
    return;
  }

  range.set_start(range.start() - before);
  range.set_end(range.end() + after);
  gfx::Range text_range;
  if (GetTextRange(&text_range) && text_range.Contains(range)) {
    DeleteRange(range);
  }
  UpdateAccessibleTextSelection();
}

void Textfield::EnsureCaretNotInRect(const gfx::Rect& rect_in_screen) {
#if BUILDFLAG(IS_CHROMEOS)
  aura::Window* top_level_window = GetNativeView()->GetToplevelWindow();
  wm::EnsureWindowNotInRect(top_level_window, rect_in_screen);
#endif  // BUILDFLAG(IS_CHROMEOS)
}

bool Textfield::IsTextEditCommandEnabled(ui::TextEditCommand command) const {
  bool editable = !GetReadOnly();
  bool readable = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD;
  switch (command) {
    case ui::TextEditCommand::DELETE_BACKWARD:
    case ui::TextEditCommand::DELETE_FORWARD:
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE:
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH:
    case ui::TextEditCommand::DELETE_TO_END_OF_LINE:
    case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH:
    case ui::TextEditCommand::DELETE_WORD_BACKWARD:
    case ui::TextEditCommand::DELETE_WORD_FORWARD:
      return editable;
    case ui::TextEditCommand::MOVE_BACKWARD:
    case ui::TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_FORWARD:
    case ui::TextEditCommand::MOVE_FORWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_LEFT:
    case ui::TextEditCommand::MOVE_LEFT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_RIGHT:
    case ui::TextEditCommand::MOVE_RIGHT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_DOCUMENT:
    case ui::TextEditCommand::
        MOVE_TO_BEGINNING_OF_DOCUMENT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_LINE:
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_PARAGRAPH:
    case ui::TextEditCommand::
        MOVE_TO_BEGINNING_OF_PARAGRAPH_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT:
    case ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_END_OF_LINE:
    case ui::TextEditCommand::MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_END_OF_PARAGRAPH:
    case ui::TextEditCommand::MOVE_TO_END_OF_PARAGRAPH_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_WORD_BACKWARD:
    case ui::TextEditCommand::MOVE_WORD_BACKWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_WORD_FORWARD:
    case ui::TextEditCommand::MOVE_WORD_FORWARD_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_WORD_LEFT:
    case ui::TextEditCommand::MOVE_WORD_LEFT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_WORD_RIGHT:
    case ui::TextEditCommand::MOVE_WORD_RIGHT_AND_MODIFY_SELECTION:
      return true;
    case ui::TextEditCommand::UNDO:
      return editable && model_->CanUndo();
    case ui::TextEditCommand::REDO:
      return editable && model_->CanRedo();
    case ui::TextEditCommand::CUT:
      return editable && readable && HasSelection();
    case ui::TextEditCommand::COPY:
      return readable && HasSelection();
    case ui::TextEditCommand::PASTE: {
      if (!editable) {
        return false;
      }
      ui::DataTransferEndpoint data_dst(
          ui::EndpointType::kDefault,
          {.notify_if_restricted = show_rejection_ui_if_any_});
      return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
          ui::ClipboardFormatType::PlainTextType(),
          ui::ClipboardBuffer::kCopyPaste, &data_dst);
    }
    case ui::TextEditCommand::SELECT_ALL:
      return !GetText().empty() &&
             GetSelectedRange().length() != GetText().length();
    case ui::TextEditCommand::SELECT_WORD:
      return readable && !GetText().empty() && !HasSelection();
    case ui::TextEditCommand::TRANSPOSE:
      return editable && !HasSelection() && !model_->HasCompositionText();
    case ui::TextEditCommand::YANK:
      return editable;
    case ui::TextEditCommand::MOVE_DOWN:
    case ui::TextEditCommand::MOVE_DOWN_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_PAGE_DOWN:
    case ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_PAGE_UP:
    case ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_UP:
    case ui::TextEditCommand::MOVE_UP_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::SCROLL_TO_BEGINNING_OF_DOCUMENT:
    case ui::TextEditCommand::SCROLL_TO_END_OF_DOCUMENT:
    case ui::TextEditCommand::SCROLL_PAGE_DOWN:
    case ui::TextEditCommand::SCROLL_PAGE_UP:
// On Mac, the textfield should respond to Up/Down arrows keys and
// PageUp/PageDown.
#if BUILDFLAG(IS_MAC)
      return true;
#else
      return GetRenderText()->multiline();
#endif
    case ui::TextEditCommand::INSERT_TEXT:
    case ui::TextEditCommand::SET_MARK:
    case ui::TextEditCommand::UNSELECT:
    case ui::TextEditCommand::INVALID_COMMAND:
      return false;
  }
  NOTREACHED();
}

void Textfield::SetTextEditCommandForNextKeyEvent(ui::TextEditCommand command) {
  DCHECK_EQ(ui::TextEditCommand::INVALID_COMMAND, scheduled_text_edit_command_);
  scheduled_text_edit_command_ = command;
}

ukm::SourceId Textfield::GetClientSourceForMetrics() const {
  // TODO(shend): Implement this method.
  NOTIMPLEMENTED_LOG_ONCE();
  return ukm::SourceId();
}

bool Textfield::ShouldDoLearning() {
  if (should_do_learning_.has_value()) {
    return should_do_learning_.value();
  }

  NOTIMPLEMENTED_LOG_ONCE();
  DVLOG(1) << "This Textfield instance does not support ShouldDoLearning";
  return false;
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// TODO(crbug.com/41452689): Implement this method to support Korean IME
// reconversion feature on native text fields (e.g. find bar).
bool Textfield::SetCompositionFromExistingText(
    const gfx::Range& range,
    const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) {
  // TODO(crbug.com/41452689): Support custom text spans.
  DCHECK(!model_->HasCompositionText());
  OnBeforeUserAction();
  model_->SetCompositionFromExistingText(range);
  SchedulePaint();
  OnAfterUserAction();
  return true;
}
#endif

#if BUILDFLAG(IS_CHROMEOS)
gfx::Range Textfield::GetAutocorrectRange() const {
  // TODO(b/316461955): Implement autocorrect UI for native fields.
  NOTIMPLEMENTED_LOG_ONCE();
  return gfx::Range();
}

gfx::Rect Textfield::GetAutocorrectCharacterBounds() const {
  // TODO(b/316461955): Implement autocorrect UI for native fields.
  NOTIMPLEMENTED_LOG_ONCE();
  return gfx::Rect();
}

bool Textfield::SetAutocorrectRange(const gfx::Range& range) {
  if (!range.is_empty()) {
    base::UmaHistogramEnumeration("InputMethod.Assistive.Autocorrect.Count",
                                  TextInputClient::SubClass::kTextField);
  }
  // TODO(b/316461955): Implement autocorrect UI for native fields.
  NOTIMPLEMENTED_LOG_ONCE();
  return false;
}

bool Textfield::AddGrammarFragments(
    const std::vector<ui::GrammarFragment>& fragments) {
  if (!fragments.empty()) {
    base::UmaHistogramEnumeration("InputMethod.Assistive.Grammar.Count",
                                  TextInputClient::SubClass::kTextField);
  }
  // TODO(crbug.com/40178699): Implement this method for CrOS Grammar.
  NOTIMPLEMENTED_LOG_ONCE();
  return false;
}

#endif

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
void Textfield::GetActiveTextInputControlLayoutBounds(
    std::optional<gfx::Rect>* control_bounds,
    std::optional<gfx::Rect>* selection_bounds) {
  gfx::Rect origin = GetContentsBounds();
  ConvertRectToScreen(this, &origin);
  *control_bounds = origin;
}
#endif

#if BUILDFLAG(IS_WIN)
// TODO(crbug.com/41452689): Implement this method once TSF supports
// reconversion features on native text fields.
void Textfield::SetActiveCompositionForAccessibility(
    const gfx::Range& range,
    const std::u16string& active_composition_text,
    bool is_composition_committed) {}
#endif

////////////////////////////////////////////////////////////////////////////////
// Textfield, views::ViewObserver overrides:
void Textfield::OnViewFocused(views::View* observed_view) {
  observed_view->RemoveObserver(this);
  observed_view->NotifyAccessibilityEventDeprecated(
      ax::mojom::Event::kTextSelectionChanged, true);
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, protected:

void Textfield::DoInsertChar(char16_t ch) {
  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;
  model_->InsertChar(ch);
  skip_input_method_cancel_composition_ = false;

  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
}

gfx::RenderText* Textfield::GetRenderText() const {
  return model_->render_text();
}

gfx::Point Textfield::GetLastClickRootLocation() const {
  return selection_controller_.last_click_root_location();
}

std::u16string Textfield::GetSelectionClipboardText() const {
  std::u16string selection_clipboard_text;
  ui::Clipboard::GetForCurrentThread()->ReadText(
      ui::ClipboardBuffer::kSelection, /* data_dst = */ nullptr,
      &selection_clipboard_text);
  return selection_clipboard_text;
}

void Textfield::ExecuteTextEditCommand(ui::TextEditCommand command) {
  DestroyTouchSelection();

  // We only execute the commands enabled in Textfield::IsTextEditCommandEnabled
  // below. Hence don't do a virtual IsTextEditCommandEnabled call.
  if (!IsTextEditCommandEnabled(command)) {
    return;
  }

  OnBeforeUserAction();

  gfx::SelectionModel selection_model = GetSelectionModel();
  auto [text_changed, cursor_changed] = DoExecuteTextEditCommand(command);

  cursor_changed |= (GetSelectionModel() != selection_model);
  if (cursor_changed && HasSelection()) {
    UpdateSelectionClipboard();
  }
  UpdateAfterChange(
      text_changed ? TextChangeType::kUserTriggered : TextChangeType::kNone,
      cursor_changed);
  OnAfterUserAction();
}

Textfield::EditCommandResult Textfield::DoExecuteTextEditCommand(
    ui::TextEditCommand command) {
  bool changed = false;
  bool cursor_changed = false;
  bool add_to_kill_buffer = false;

  base::AutoReset<bool> show_rejection_ui(&show_rejection_ui_if_any_, true);

  // Some codepaths may bypass GetCommandForKeyEvent, so any selection-dependent
  // modifications of the command should happen here.
  switch (command) {
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE:
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH:
    case ui::TextEditCommand::DELETE_TO_END_OF_LINE:
    case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH:
      add_to_kill_buffer = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD;
      [[fallthrough]];
    case ui::TextEditCommand::DELETE_WORD_BACKWARD:
    case ui::TextEditCommand::DELETE_WORD_FORWARD:
      if (HasSelection()) {
        command = ui::TextEditCommand::DELETE_FORWARD;
      }
      break;
    default:
      break;
  }

  bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
  gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT;
  gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT;

  switch (command) {
    case ui::TextEditCommand::DELETE_BACKWARD:
      changed = cursor_changed = model_->Backspace(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_FORWARD:
      changed = cursor_changed = model_->Delete(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE:
      model_->MoveCursor(gfx::LINE_BREAK, begin, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Backspace(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH:
      model_->MoveCursor(gfx::FIELD_BREAK, begin, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Backspace(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_TO_END_OF_LINE:
      model_->MoveCursor(gfx::LINE_BREAK, end, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Delete(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_TO_END_OF_PARAGRAPH:
      model_->MoveCursor(gfx::FIELD_BREAK, end, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Delete(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_WORD_BACKWARD:
      model_->MoveCursor(gfx::WORD_BREAK, begin, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Backspace(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::DELETE_WORD_FORWARD:
      model_->MoveCursor(gfx::WORD_BREAK, end, gfx::SELECTION_RETAIN);
      changed = cursor_changed = model_->Delete(add_to_kill_buffer);
      break;
    case ui::TextEditCommand::MOVE_BACKWARD:
      model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_BACKWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::CHARACTER_BREAK, begin, gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_FORWARD:
      model_->MoveCursor(gfx::CHARACTER_BREAK, end, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_FORWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::CHARACTER_BREAK, end, gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_LEFT:
      model_->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_LEFT,
                         gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_LEFT_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_LEFT,
                         gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_RIGHT:
      model_->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_RIGHT,
                         gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_RIGHT_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_RIGHT,
                         gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_LINE:
      model_->MoveCursor(gfx::LINE_BREAK, begin, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_DOCUMENT:
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_PARAGRAPH:
    case ui::TextEditCommand::MOVE_UP:
    case ui::TextEditCommand::MOVE_PAGE_UP:
    case ui::TextEditCommand::SCROLL_TO_BEGINNING_OF_DOCUMENT:
    case ui::TextEditCommand::SCROLL_PAGE_UP:
      model_->MoveCursor(gfx::FIELD_BREAK, begin, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::LINE_BREAK, begin, kLineSelectionBehavior);
      break;
    case ui::TextEditCommand::
        MOVE_TO_BEGINNING_OF_DOCUMENT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::
        MOVE_TO_BEGINNING_OF_PARAGRAPH_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, begin, kLineSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_UP_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, begin, gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_TO_END_OF_LINE:
      model_->MoveCursor(gfx::LINE_BREAK, end, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT:
    case ui::TextEditCommand::MOVE_TO_END_OF_PARAGRAPH:
    case ui::TextEditCommand::MOVE_DOWN:
    case ui::TextEditCommand::MOVE_PAGE_DOWN:
    case ui::TextEditCommand::SCROLL_TO_END_OF_DOCUMENT:
    case ui::TextEditCommand::SCROLL_PAGE_DOWN:
      model_->MoveCursor(gfx::FIELD_BREAK, end, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::LINE_BREAK, end, kLineSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_TO_END_OF_PARAGRAPH_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, end, kLineSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION:
    case ui::TextEditCommand::MOVE_DOWN_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, end, gfx::SELECTION_RETAIN);
      break;
    case ui::TextEditCommand::MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, begin,
                         kMoveParagraphSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::FIELD_BREAK, end,
                         kMoveParagraphSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_WORD_BACKWARD:
      model_->MoveCursor(gfx::WORD_BREAK, begin, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_WORD_BACKWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::WORD_BREAK, begin, kWordSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_WORD_FORWARD:
      model_->MoveCursor(gfx::WORD_BREAK, end, gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_WORD_FORWARD_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::WORD_BREAK, end, kWordSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_WORD_LEFT:
      model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_LEFT,
                         gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_WORD_LEFT_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_LEFT,
                         kWordSelectionBehavior);
      break;
    case ui::TextEditCommand::MOVE_WORD_RIGHT:
      model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT,
                         gfx::SELECTION_NONE);
      break;
    case ui::TextEditCommand::MOVE_WORD_RIGHT_AND_MODIFY_SELECTION:
      model_->MoveCursor(gfx::WORD_BREAK, gfx::CURSOR_RIGHT,
                         kWordSelectionBehavior);
      break;
    case ui::TextEditCommand::UNDO:
      changed = cursor_changed = model_->Undo();
      break;
    case ui::TextEditCommand::REDO:
      changed = cursor_changed = model_->Redo();
      break;
    case ui::TextEditCommand::CUT:
      changed = cursor_changed = Cut();
      break;
    case ui::TextEditCommand::COPY:
      Copy();
      break;
    case ui::TextEditCommand::PASTE:
      changed = cursor_changed = Paste();
      break;
    case ui::TextEditCommand::SELECT_ALL:
      SelectAll(false);
      break;
    case ui::TextEditCommand::SELECT_WORD:
      SelectWord();
      break;
    case ui::TextEditCommand::TRANSPOSE:
      changed = cursor_changed = model_->Transpose();
      break;
    case ui::TextEditCommand::YANK:
      changed = cursor_changed = model_->Yank();
      break;
    case ui::TextEditCommand::INSERT_TEXT:
    case ui::TextEditCommand::SET_MARK:
    case ui::TextEditCommand::UNSELECT:
    case ui::TextEditCommand::INVALID_COMMAND:
      NOTREACHED();
  }

  return {changed, cursor_changed};
}

void Textfield::OffsetDoubleClickWord(size_t offset) {
  selection_controller_.OffsetDoubleClickWord(offset);
}

bool Textfield::IsDropCursorForInsertion() const {
  return true;
}

bool Textfield::ShouldShowPlaceholderText() const {
  return GetText().empty() && !GetPlaceholderText().empty();
}

void Textfield::RequestFocusWithPointer(ui::EventPointerType pointer_type) {
  if (HasFocus()) {
    return;
  }

  switch (pointer_type) {
    case ui::EventPointerType::kMouse:
      focus_reason_ = ui::TextInputClient::FOCUS_REASON_MOUSE;
      break;
    case ui::EventPointerType::kPen:
      focus_reason_ = ui::TextInputClient::FOCUS_REASON_PEN;
      break;
    case ui::EventPointerType::kTouch:
      focus_reason_ = ui::TextInputClient::FOCUS_REASON_TOUCH;
      break;
    default:
      focus_reason_ = ui::TextInputClient::FOCUS_REASON_OTHER;
      break;
  }

  View::RequestFocus();
}

void Textfield::RequestFocusForGesture(const ui::GestureEventDetails& details) {
  bool show_virtual_keyboard = true;
#if BUILDFLAG(IS_WIN)
  show_virtual_keyboard =
      details.primary_pointer_type() == ui::EventPointerType::kTouch ||
      details.primary_pointer_type() == ui::EventPointerType::kPen;
#endif

  RequestFocusWithPointer(details.primary_pointer_type());
  if (show_virtual_keyboard) {
    ShowVirtualKeyboardIfEnabled();
  }
}

base::CallbackListSubscription Textfield::AddTextChangedCallback(
    views::PropertyChangedCallback callback) {
  return AddPropertyChangedCallback(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldText),
      std::move(callback));
}

bool Textfield::PreHandleKeyPressed(const ui::KeyEvent& event) {
  return false;
}

ui::TextEditCommand Textfield::GetCommandForKeyEvent(
    const ui::KeyEvent& event) {
  if (event.type() != ui::EventType::kKeyPressed || event.IsUnicodeKeyCode()) {
    return ui::TextEditCommand::INVALID_COMMAND;
  }

  const bool shift = event.IsShiftDown();
#if BUILDFLAG(IS_MAC)
  const bool command = event.IsCommandDown();
#endif
  const bool control = event.IsControlDown() || event.IsCommandDown();
  const bool alt = event.IsAltDown() || event.IsAltGrDown();
  switch (event.key_code()) {
    case ui::VKEY_Z:
      if (control && !shift && !alt) {
        return ui::TextEditCommand::UNDO;
      }
      return (control && shift && !alt) ? ui::TextEditCommand::REDO
                                        : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_Y:
      return (control && !alt) ? ui::TextEditCommand::REDO
                               : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_A:
      return (control && !alt) ? ui::TextEditCommand::SELECT_ALL
                               : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_X:
      return (control && !alt) ? ui::TextEditCommand::CUT
                               : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_C:
      return (control && !alt) ? ui::TextEditCommand::COPY
                               : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_V:
      return (control && !alt) ? ui::TextEditCommand::PASTE
                               : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_RIGHT:
      // Ignore alt+right, which may be a browser navigation shortcut.
      if (alt) {
        return ui::TextEditCommand::INVALID_COMMAND;
      }
      if (!shift) {
        return control ? ui::TextEditCommand::MOVE_WORD_RIGHT
                       : ui::TextEditCommand::MOVE_RIGHT;
      }
      return control ? ui::TextEditCommand::MOVE_WORD_RIGHT_AND_MODIFY_SELECTION
                     : ui::TextEditCommand::MOVE_RIGHT_AND_MODIFY_SELECTION;
    case ui::VKEY_LEFT:
      // Ignore alt+left, which may be a browser navigation shortcut.
      if (alt) {
        return ui::TextEditCommand::INVALID_COMMAND;
      }
      if (!shift) {
        return control ? ui::TextEditCommand::MOVE_WORD_LEFT
                       : ui::TextEditCommand::MOVE_LEFT;
      }
      return control ? ui::TextEditCommand::MOVE_WORD_LEFT_AND_MODIFY_SELECTION
                     : ui::TextEditCommand::MOVE_LEFT_AND_MODIFY_SELECTION;
    case ui::VKEY_HOME:
      if (shift) {
        return ui::TextEditCommand::
            MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION;
      }
#if BUILDFLAG(IS_MAC)
      return ui::TextEditCommand::SCROLL_TO_BEGINNING_OF_DOCUMENT;
#else
      return ui::TextEditCommand::MOVE_TO_BEGINNING_OF_LINE;
#endif
    case ui::VKEY_END:
      if (shift) {
        return ui::TextEditCommand::MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION;
      }
#if BUILDFLAG(IS_MAC)
      return ui::TextEditCommand::SCROLL_TO_END_OF_DOCUMENT;
#else
      return ui::TextEditCommand::MOVE_TO_END_OF_LINE;
#endif
    case ui::VKEY_UP:
#if BUILDFLAG(IS_MAC)
      if (control && shift) {
        return ui::TextEditCommand::
            MOVE_PARAGRAPH_BACKWARD_AND_MODIFY_SELECTION;
      }
      if (command) {
        return ui::TextEditCommand::MOVE_TO_BEGINNING_OF_DOCUMENT;
      }
      return shift ? ui::TextEditCommand::
                         MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION
                   : ui::TextEditCommand::MOVE_UP;
#else
      return shift ? ui::TextEditCommand::
                         MOVE_TO_BEGINNING_OF_LINE_AND_MODIFY_SELECTION
                   : ui::TextEditCommand::INVALID_COMMAND;
#endif
    case ui::VKEY_DOWN:
#if BUILDFLAG(IS_MAC)
      if (control && shift) {
        return ui::TextEditCommand::MOVE_PARAGRAPH_FORWARD_AND_MODIFY_SELECTION;
      }
      if (command) {
        return ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT;
      }
      return shift
                 ? ui::TextEditCommand::MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION
                 : ui::TextEditCommand::MOVE_DOWN;
#else
      return shift
                 ? ui::TextEditCommand::MOVE_TO_END_OF_LINE_AND_MODIFY_SELECTION
                 : ui::TextEditCommand::INVALID_COMMAND;
#endif
    case ui::VKEY_BACK:
      if (!control) {
#if BUILDFLAG(IS_WIN)
        if (alt) {
          return shift ? ui::TextEditCommand::REDO : ui::TextEditCommand::UNDO;
        }
#endif
        return ui::TextEditCommand::DELETE_BACKWARD;
      }
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
      // Only erase by line break on Linux and ChromeOS.
      if (shift) {
        return ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE;
      }
#endif
      return ui::TextEditCommand::DELETE_WORD_BACKWARD;
    case ui::VKEY_DELETE:
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
      // Only erase by line break on Linux and ChromeOS.
      if (shift && control) {
        return ui::TextEditCommand::DELETE_TO_END_OF_LINE;
      }
#endif
      if (control) {
        return ui::TextEditCommand::DELETE_WORD_FORWARD;
      }
      return shift ? ui::TextEditCommand::CUT
                   : ui::TextEditCommand::DELETE_FORWARD;
    case ui::VKEY_INSERT:
      if (control && !shift) {
        return ui::TextEditCommand::COPY;
      }
      return (shift && !control) ? ui::TextEditCommand::PASTE
                                 : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_PRIOR:
      return control ? ui::TextEditCommand::SCROLL_PAGE_UP
                     : ui::TextEditCommand::INVALID_COMMAND;
    case ui::VKEY_NEXT:
      return control ? ui::TextEditCommand::SCROLL_PAGE_DOWN
                     : ui::TextEditCommand::INVALID_COMMAND;
    default:
      return ui::TextEditCommand::INVALID_COMMAND;
  }
}

////////////////////////////////////////////////////////////////////////////////
// Textfield, private:

////////////////////////////////////////////////////////////////////////////////
// Textfield, SelectionControllerDelegate overrides:

gfx::RenderText* Textfield::GetRenderTextForSelectionController() {
  return GetRenderText();
}

bool Textfield::IsReadOnly() const {
  return GetReadOnly();
}

bool Textfield::SupportsDrag() const {
  return true;
}

void Textfield::SetTextBeingDragged(bool value) {
  initiating_drag_ = value;
}

int Textfield::GetViewHeight() const {
  return height();
}

int Textfield::GetViewWidth() const {
  return width();
}

int Textfield::GetDragSelectionDelay() const {
  if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION) {
    // NON_ZERO_DURATION is 1/20 by default, but we want 1/100 here.
    return 1;
  }
  return ui::ScopedAnimationDurationScaleMode::duration_multiplier() * 100;
}

void Textfield::OnBeforePointerAction() {
  OnBeforeUserAction();
  if (model_->HasCompositionText()) {
    model_->ConfirmCompositionText();
  }
}

void Textfield::OnAfterPointerAction(bool text_changed,
                                     bool selection_changed) {
  OnAfterUserAction();
  const auto text_change_type =
      text_changed ? TextChangeType::kUserTriggered : TextChangeType::kNone;
  UpdateAfterChange(text_change_type, selection_changed);
}

bool Textfield::PasteSelectionClipboard() {
  DCHECK(performing_user_action_);
  DCHECK(!GetReadOnly());
  const std::u16string selection_clipboard_text = GetSelectionClipboardText();
  if (selection_clipboard_text.empty()) {
    return false;
  }

  model_->InsertText(selection_clipboard_text);
  return true;
}

void Textfield::UpdateSelectionClipboard() {
  if (ui::Clipboard::IsSupportedClipboardBuffer(
          ui::ClipboardBuffer::kSelection)) {
    if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD) {
      ui::ScopedClipboardWriter(ui::ClipboardBuffer::kSelection)
          .WriteText(GetSelectedText());
      if (controller_) {
        controller_->OnAfterCutOrCopy(ui::ClipboardBuffer::kSelection);
      }
    }
  }
}

void Textfield::UpdateBackgroundColor() {
  if (!is_background_enabled_) {
    if (GetBackground()) {
      SetBackground(nullptr);
      // If the parent for this textfield creates a non-opaque background they
      // are responsible for disabling subpixel rendering.
      GetRenderText()->set_subpixel_rendering_suppressed(false);
    }
    return;
  }

  const SkColor color = GetBackgroundColor();
  SetBackground(CreateBackgroundFromPainter(
      Painter::CreateSolidRoundRectPainter(color, GetCornerRadius())));
  // Disable subpixel rendering when the background color is not opaque because
  // it draws incorrect colors around the glyphs in that case.
  // See crbug.com/115198
  GetRenderText()->set_subpixel_rendering_suppressed(SkColorGetA(color) !=
                                                     SK_AlphaOPAQUE);
  OnPropertyChanged(
      ui::metadata::MakeUniquePropertyKey(&model_, kTextfieldBackgroundColor),
      kPropertyEffectsPaint);
}

void Textfield::UpdateDefaultBorder() {
  // Only update the border if SetBorder() has not been called. This is to avoid
  // overriding any custom borders.
  if (!use_default_border_) {
    return;
  }

  auto border = std::make_unique<views::FocusableBorder>();
  const LayoutProvider* provider = LayoutProvider::Get();
  border->SetInsets(gfx::Insets::TLBR(
      extra_insets_.top() +
          provider->GetDistanceMetric(DISTANCE_CONTROL_VERTICAL_TEXT_PADDING),
      extra_insets_.left() + provider->GetDistanceMetric(
                                 DISTANCE_TEXTFIELD_HORIZONTAL_TEXT_PADDING),
      extra_insets_.bottom() +
          provider->GetDistanceMetric(DISTANCE_CONTROL_VERTICAL_TEXT_PADDING),
      extra_insets_.right() + provider->GetDistanceMetric(
                                  DISTANCE_TEXTFIELD_HORIZONTAL_TEXT_PADDING)));

  auto border_color_id = ui::kColorTextfieldOutline;
  if (invalid_) {
    border_color_id = ui::kColorTextfieldOutlineInvalid;
  } else if (!GetEnabled() || GetReadOnly()) {
    border_color_id = ui::kColorTextfieldOutlineDisabled;
  }

  border->SetColor(border_color_id);

  border->SetCornerRadius(GetCornerRadius());
  View::SetBorder(std::move(border));
}

void Textfield::UpdateSelectionTextColor() {
  if (!GetWidget()) {
    return;
  }
  GetRenderText()->set_selection_color(GetSelectionTextColor());
  OnPropertyChanged(ui::metadata::MakeUniquePropertyKey(
                        &model_, kTextfieldSelectionTextColor),
                    kPropertyEffectsPaint);
}

void Textfield::UpdateSelectionBackgroundColor() {
  if (!GetWidget()) {
    return;
  }
  GetRenderText()->set_selection_background_focused_color(
      GetSelectionBackgroundColor());
  OnPropertyChanged(ui::metadata::MakeUniquePropertyKey(
                        &model_, kTextfieldSelectionBackgroundColor),
                    kPropertyEffectsPaint);
}

void Textfield::UpdateAccessibleTextSelection() {
  const gfx::Range range = GetSelectedRange();
  GetViewAccessibility().SetTextSelStart(
      base::checked_cast<int32_t>(range.start()));
  GetViewAccessibility().SetTextSelEnd(
      base::checked_cast<int32_t>(range.end()));
}

void Textfield::UpdateAfterChange(
    TextChangeType text_change_type,
    bool cursor_changed,
    std::optional<bool> notify_caret_bounds_changed) {
  if (text_change_type != TextChangeType::kNone) {
    if ((text_change_type == TextChangeType::kUserTriggered) && controller_) {
      controller_->ContentsChanged(this, std::u16string(GetText()));
    }
    UpdateAccessibleValue();
  }
  UpdateAccessibilityTextDirection();
  if (cursor_changed) {
    UpdateCursorViewPosition();
    UpdateCursorVisibility();
  }
  const bool anything_changed =
      (text_change_type != TextChangeType::kNone) || cursor_changed;
  if (notify_caret_bounds_changed.value_or(anything_changed)) {
    OnCaretBoundsChanged();
  }
  if (anything_changed) {
    SchedulePaint();
  }
  UpdateAccessibleTextSelection();
}

void Textfield::UpdateAccessibilityTextDirection() {
  GetViewAccessibility().SetTextDirection(
      static_cast<int32_t>(GetTextDirection() == base::i18n::RIGHT_TO_LEFT
                               ? ax::mojom::WritingDirection::kRtl
                               : ax::mojom::WritingDirection::kLtr));
}

void Textfield::UpdateAccessibleValue() {
  if (text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD) {
    GetViewAccessibility().SetValue(std::u16string(
        GetText().size(), gfx::RenderText::kPasswordReplacementChar));
  } else {
    GetViewAccessibility().SetValue(GetText());
  }

#if BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)
  UpdateAccessibleTextOffsetsIfNeeded();
#endif  // BUILDFLAG(SUPPORTS_AX_TEXT_OFFSETS)
}

void Textfield::UpdateCursorVisibility() {
  cursor_view_->SetVisible(ShouldShowCursor());
#if BUILDFLAG(IS_MAC)
  // If we called SetVisible(true) above we want the cursor layer to be opaque.
  // If we called SetVisible(false), making the layer opaque has no effect on
  // its visibility.
  cursor_view_->layer()->SetOpacity(kOpaque);
#endif
  if (ShouldBlinkCursor()) {
    StartBlinkingCursor();
  } else {
    StopBlinkingCursor();
  }
}

bool Textfield::IsMenuShowing() const {
  return context_menu_runner_ && context_menu_runner_->IsRunning();
}

gfx::Rect Textfield::CalculateCursorViewBounds() const {
  gfx::Rect location(GetRenderText()->GetUpdatedCursorBounds());
  location.set_x(GetMirroredXForRect(location));
  // Shrink the cursor bounds to fit within the view.
  location.Intersect(GetLocalBounds());
  return location;
}

void Textfield::UpdateCursorViewPosition() {
  cursor_view_->SetBoundsRect(CalculateCursorViewBounds());
  GetViewAccessibility().SetScrollX(
      GetRenderText()->GetUpdatedDisplayOffset().x());
}

int Textfield::GetTextStyle() const {
  if (GetReadOnly() || !GetEnabled()) {
    return style::STYLE_DISABLED;
  } else if (GetInvalid()) {
    return style::STYLE_INVALID;
  } else {
    return style::STYLE_PRIMARY;
  }
}

void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) {
  TRACE_EVENT0("views", "Textfield::PaintTextAndCursor");
  canvas->Save();

  // Draw placeholder text if needed.
  gfx::RenderText* render_text = GetRenderText();
  if (ShouldShowPlaceholderText()) {
    // Disable subpixel rendering when the background color is not opaque
    // because it draws incorrect colors around the glyphs in that case.
    // See crbug.com/786343
    int placeholder_text_draw_flags = placeholder_text_draw_flags_;
    if (SkColorGetA(GetBackgroundColor()) != SK_AlphaOPAQUE) {
      placeholder_text_draw_flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
    }

    canvas->DrawStringRectWithFlags(
        GetPlaceholderText(), placeholder_font_list_.value_or(GetFontList()),
        placeholder_text_color_.value_or(
            GetColorProvider()->GetColor(TypographyProvider::Get().GetColorId(
                style::CONTEXT_TEXTFIELD_PLACEHOLDER,
                GetInvalid() ? style::STYLE_INVALID : style::STYLE_PRIMARY))),
        render_text->display_rect(), placeholder_text_draw_flags);
  }

  // If drop cursor is active, draw |render_text| with its text selected.
  const bool select_all = drop_cursor_visible_ && !IsDropCursorForInsertion();
  render_text->Draw(canvas, select_all);

  if (drop_cursor_visible_ && IsDropCursorForInsertion()) {
    // Draw a drop cursor that marks where the text will be dropped/inserted.
    canvas->FillRect(render_text->GetCursorBounds(drop_cursor_position_, true),
                     GetTextColor());
  }

  canvas->Restore();
}

void Textfield::MoveCursorTo(const gfx::Point& point, bool select) {
  if (model_->MoveCursorTo(point, select)) {
    UpdateAfterChange(TextChangeType::kNone, true);
  }
}

void Textfield::OnCaretBoundsChanged() {
  if (GetInputMethod()) {
    GetInputMethod()->OnCaretBoundsChanged(this);
  }
  if (touch_selection_controller_) {
    touch_selection_controller_->SelectionChanged();
  }

  // Screen reader users don't expect notifications about unfocused textfields.
  if (HasFocus()) {
    // If this control is in the process of receiving focus, even though it
    // 'HasFocus', the accessibility event to announce that it has focus has not
    // been fired yet. The kTextSelectionChanged event needs to be fired *after*
    // the focus event, so we attach our observer which will fire the event
    // after we finish receiving focus (which includes the accessibility focus
    // event being fired).
    if (is_processing_focus_) {
      if (!HasObserver(this)) {
        AddObserver(this);
      }
    } else {
      UpdateAccessibleTextSelection();
      NotifyAccessibilityEventDeprecated(
          ax::mojom::Event::kTextSelectionChanged, true);
    }
  }

  UpdateCursorViewPosition();
}

void Textfield::OnBeforeUserAction() {
  DCHECK(!performing_user_action_);
  performing_user_action_ = true;
  if (controller_) {
    controller_->OnBeforeUserAction(this);
  }
}

void Textfield::OnAfterUserAction() {
  if (controller_) {
    controller_->OnAfterUserAction(this);
  }
  DCHECK(performing_user_action_);
  performing_user_action_ = false;
}

bool Textfield::Cut() {
  if (!GetReadOnly() && text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD &&
      model_->Cut()) {
    if (controller_) {
      controller_->OnAfterCutOrCopy(ui::ClipboardBuffer::kCopyPaste);
    }
    UpdateAccessibleTextSelection();
    return true;
  }
  return false;
}

bool Textfield::Copy() {
  if (text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD && model_->Copy()) {
    if (controller_) {
      controller_->OnAfterCutOrCopy(ui::ClipboardBuffer::kCopyPaste);
    }
    return true;
  }
  return false;
}

bool Textfield::Paste() {
  if (!GetReadOnly() && model_->Paste()) {
    if (controller_) {
      controller_->OnAfterPaste();
    }
    UpdateAccessibleTextSelection();
    return true;
  }
  return false;
}

void Textfield::UpdateContextMenu() {
  // TextfieldController may modify Textfield's menu, so the menu should be
  // recreated each time it's shown. Destroy the existing objects in the reverse
  // order of creation.
  context_menu_runner_.reset();
  context_menu_contents_.reset();

  context_menu_contents_ = std::make_unique<ui::SimpleMenuModel>(this);
  context_menu_contents_->AddItemWithStringId(kUndo, IDS_APP_UNDO);
  context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR);
  context_menu_contents_->AddItemWithStringId(kCut, IDS_APP_CUT);
  context_menu_contents_->AddItemWithStringId(kCopy, IDS_APP_COPY);
  context_menu_contents_->AddItemWithStringId(kPaste, IDS_APP_PASTE);
  context_menu_contents_->AddItemWithStringId(kDelete, IDS_APP_DELETE);
  context_menu_contents_->AddSeparator(ui::NORMAL_SEPARATOR);
  context_menu_contents_->AddItemWithStringId(kSelectAll, IDS_APP_SELECT_ALL);

  // If the controller adds menu commands, also override ExecuteCommand() and
  // IsCommandIdEnabled() as appropriate, for the commands added.
  if (controller_) {
    controller_->UpdateContextMenu(context_menu_contents_.get());
  }

  text_services_context_menu_ =
      ViewsTextServicesContextMenu::Create(context_menu_contents_.get(), this);

  context_menu_runner_ = std::make_unique<MenuRunner>(
      context_menu_contents_.get(),
      MenuRunner::HAS_MNEMONICS | MenuRunner::CONTEXT_MENU);
}

bool Textfield::ImeEditingAllowed() const {
  // Disallow input method editing of password fields.
  ui::TextInputType t = GetTextInputType();
  return (t != ui::TEXT_INPUT_TYPE_NONE && t != ui::TEXT_INPUT_TYPE_PASSWORD);
}

void Textfield::RevealPasswordChar(std::optional<size_t> index,
                                   base::TimeDelta duration) {
  GetRenderText()->SetObscuredRevealIndex(index);
  SchedulePaint();
  password_char_reveal_index_ = index;
  UpdateCursorViewPosition();

  if (index.has_value()) {
    password_reveal_timer_.Start(
        FROM_HERE, duration,
        base::BindOnce(&Textfield::RevealPasswordChar,
                       weak_ptr_factory_.GetWeakPtr(), std::nullopt, duration));
  }
}

void Textfield::CreateTouchSelectionControllerAndNotifyIt() {
  if (!HasFocus()) {
    return;
  }

  if (!touch_selection_controller_) {
#if defined(USE_AURA)
    touch_selection_controller_ =
        std::make_unique<TouchSelectionControllerImpl>(this);
#endif
  }
  if (touch_selection_controller_) {
    touch_selection_controller_->SelectionChanged();
  }
}

void Textfield::OnEditFailed() {
  PlatformStyle::OnTextfieldEditFailed();
}

bool Textfield::ShouldShowCursor() const {
  // Show the cursor when the primary selected range is empty; secondary
  // selections do not affect cursor visibility.
  return HasFocus() && !HasSelection(true) && GetEnabled() && !GetReadOnly() &&
         !drop_cursor_visible_ && GetRenderText()->cursor_enabled() &&
         !cursor_view_->bounds().IsEmpty();
}

int Textfield::CharsToDips(int width_in_chars) const {
  // Use a subset of the conditions in ShouldShowCursor() that are unlikely to
  // change dynamically.  Dynamic changes can result in glitchy-looking visual
  // effects like find boxes on different tabs being 1 DIP different width.
  const int cursor_width =
      (!GetReadOnly() && GetRenderText()->cursor_enabled()) ? 1 : 0;
  return GetFontList().GetExpectedTextWidth(width_in_chars) + cursor_width +
         GetInsets().width();
}

bool Textfield::ShouldBlinkCursor() const {
  return ShouldShowCursor() && !Textfield::GetCaretBlinkInterval().is_zero();
}

void Textfield::StartBlinkingCursor() {
  DCHECK(ShouldBlinkCursor());
  cursor_blink_timer_.Start(FROM_HERE, Textfield::GetCaretBlinkInterval(), this,
                            &Textfield::OnCursorBlinkTimerFired);
}

void Textfield::StopBlinkingCursor() {
  cursor_blink_timer_.Stop();
}

void Textfield::OnCursorBlinkTimerFired() {
  DCHECK(ShouldBlinkCursor());
  // TODO(crbug.com/40820702): The cursor position is not updated appropriately
  // when locale changes from a left-to-right script to a right-to-left script.
  // Thus the cursor is displayed at a wrong position immediately after the
  // locale change. As a band-aid solution, we update the cursor here, so that
  // the cursor can be at the wrong position only up until the next blink. It
  // would be better to detect locale change explicitly (how?) and update
  // there.
  UpdateCursorViewPosition();

#if BUILDFLAG(IS_MAC)
  // https://crbug.com/1311416 . The cursor is a solid color Layer which we make
  // flash by toggling the Layer's visibility. A bug in the CARendererLayerTree
  // causes all Layers that come after the cursor in the Layer tree (the
  // bookmarks bar, for example) to be pushed to the screen with each
  // appearance and disappearance of the cursor, consuming unnecessary CPU in
  // the window server. The same also happens if we leave the cursor visible
  // but make its color or layer 100% transparent (there's an optimization
  // somewhere that removes completely transparent layers from the hierarchy).
  // Until this bug is fixed, flash the cursor by alternating the cursor
  // layer's opacity between opaque and "almost" transparent. Once
  // https://crbug.com/1313999 is fixed, this Mac-specific code and its tests
  // can be removed.
  float new_opacity = cursor_view_->layer()->opacity() != kOpaque
                          ? kOpaque
                          : kAlmostTransparent;
  cursor_view_->layer()->SetOpacity(new_opacity);
#else
  cursor_view_->SetVisible(!cursor_view_->GetVisible());
#endif
}

void Textfield::OnEnabledChanged() {
  if (GetInputMethod()) {
    GetInputMethod()->OnTextInputTypeChanged(this);
  }
  UpdateDefaultBorder();

  // Only expose readonly if enabled. Don't overwrite the disabled restriction.
  // However, if we re-enable a textfield that was already set to readonly,
  // we need to update the readonly state, since the disabled restriction would
  // have overwritten it.
  if (GetEnabled() && GetReadOnly()) {
    GetViewAccessibility().SetReadOnly(true);
  }
  UpdateAccessibleDefaultActionVerb();
}

void Textfield::DropDraggedText(
    const ui::DropTargetEvent& event,
    ui::mojom::DragOperation& output_drag_op,
    std::unique_ptr<ui::LayerTreeOwner> drag_image_layer_owner) {
  DCHECK(CanDrop(event.data()));

  gfx::RenderText* render_text = GetRenderText();

  OnBeforeUserAction();
  skip_input_method_cancel_composition_ = true;

  gfx::SelectionModel drop_destination_model =
      render_text->FindCursorPosition(event.location());
  std::u16string new_text = event.data().GetString().value_or(std::u16string());

  // Delete the current selection for a drag and drop within this view.
  const bool move = initiating_drag_ && !event.IsControlDown() &&
                    event.source_operations() & ui::DragDropTypes::DRAG_MOVE;
  if (move) {
    // Adjust the drop destination if it is on or after the current selection.
    size_t pos = drop_destination_model.caret_pos();
    pos -= render_text->selection().Intersect(gfx::Range(0, pos)).length();
    model_->DeletePrimarySelectionAndInsertTextAt(new_text, pos);
  } else {
    model_->MoveCursorTo(drop_destination_model);
    // Drop always inserts text even if the textfield is not in insert mode.
    model_->InsertText(new_text);
  }
  skip_input_method_cancel_composition_ = false;
  UpdateAfterChange(TextChangeType::kUserTriggered, true);
  OnAfterUserAction();
  output_drag_op = move ? DragOperation::kMove : DragOperation::kCopy;
}

float Textfield::GetCornerRadius() {
  return LayoutProvider::Get()->GetCornerRadiusMetric(
      ShapeContextTokens::kTextfieldRadius, size());
}

void Textfield::OnGestureScrollBegin(int drag_start_location_x) {
  drag_start_location_x_ = drag_start_location_x;
  drag_start_display_offset_ = GetRenderText()->GetUpdatedDisplayOffset().x();
  show_touch_handles_after_scroll_ = touch_selection_controller_ != nullptr;
  DestroyTouchSelection();
}

void Textfield::GestureScroll(int drag_location_x) {
  int new_display_offset =
      drag_start_display_offset_ + drag_location_x - drag_start_location_x_;
  GetRenderText()->SetDisplayOffset(new_display_offset);
  SchedulePaint();
}

bool Textfield::HandleGestureForSelectionDragging(ui::GestureEvent* event) {
  if (!::features::IsTouchTextEditingRedesignEnabled()) {
    return false;
  }

  switch (event->type()) {
    case ui::EventType::kGestureTap:
      if (selection_dragging_state_ != SelectionDraggingState::kNone) {
        // Selection has already been set in preceding events, so we can just
        // cancel selection dragging and show touch handles without changing the
        // selection.
        StopSelectionDragging();
        CreateTouchSelectionControllerAndNotifyIt();
        event->SetHandled();
        return true;
      }
      return false;
    case ui::EventType::kGestureTapDown:
      if (event->details().tap_down_count() == 1) {
        selection_dragging_state_ = SelectionDraggingState::kNone;
        return false;
      } else if (event->details().tap_down_count() == 2) {
        OnBeforeUserAction();
        SelectWordAt(event->location());
        OnAfterUserAction();
        selection_dragging_state_ = SelectionDraggingState::kSelectedWord;
        selection_drag_type_ = ui::TouchSelectionDragType::kDoublePressDrag;
      } else if (event->details().tap_down_count() == 3) {
        OnBeforeUserAction();
        SelectAll(false);
        OnAfterUserAction();
        selection_dragging_state_ = SelectionDraggingState::kSelectedAll;
      }
      DestroyTouchSelection();
      event->SetHandled();
      return true;
    case ui::EventType::kGestureLongPress:
      selection_dragging_state_ = SelectionDraggingState::kSelectedWord;
      selection_drag_type_ = ui::TouchSelectionDragType::kLongPressDrag;
      DestroyTouchSelection();
      event->SetHandled();
      return true;
    case ui::EventType::kGestureLongTap:
      if (selection_dragging_state_ != SelectionDraggingState::kNone) {
        StopSelectionDragging();
        CreateTouchSelectionControllerAndNotifyIt();
        event->SetHandled();
        return true;
      }
      return false;
    case ui::EventType::kGestureScrollBegin:
      // Only start selection dragging if scrolling with one touch point.
      if (event->details().touch_points() == 1 &&
          StartSelectionDragging(*event)) {
        CreateTouchSelectionControllerAndNotifyIt();
        show_touch_handles_after_scroll_ = true;
        event->SetHandled();
        return true;
      }
      StopSelectionDragging();
      return false;
    case ui::EventType::kGestureScrollUpdate:
      // Switch from selection dragging to default scrolling behaviour if scroll
      // update has multiple touch points.
      if (IsSelectionDragging() && event->details().touch_points() > 1) {
        StopSelectionDragging();
        OnGestureScrollBegin(event->location().x());
        return false;
      } else if (selection_dragging_state_ ==
                 SelectionDraggingState::kDraggingSelectionExtent) {
        MoveRangeSelectionExtent(event->location() +
                                 selection_dragging_offset_);
        event->SetHandled();
        return true;
      } else if (selection_dragging_state_ ==
                 SelectionDraggingState::kDraggingCursor) {
        MoveCursorTo(event->location(), false);
        event->SetHandled();
        return true;
      }
      return false;
    case ui::EventType::kGestureScrollEnd:
    case ui::EventType::kScrollFlingStart:
    case ui::EventType::kGestureEnd:
      StopSelectionDragging();
      return false;
    default:
      return false;
  }
}

bool Textfield::StartSelectionDragging(const ui::GestureEvent& event) {
  DCHECK_EQ(event.type(), ui::EventType::kGestureScrollBegin);

  const float delta_x = event.details().scroll_x_hint();
  const float delta_y = event.details().scroll_y_hint();
  if (selection_dragging_state_ == SelectionDraggingState::kSelectedWord) {
    gfx::RenderText* render_text = GetRenderText();
    gfx::SelectionModel start_sel =
        render_text->GetSelectionModelForSelectionStart();
    const gfx::SelectionModel& sel = render_text->selection_model();
    gfx::Point selection_start =
        render_text->GetCursorBounds(start_sel, true).CenterPoint();
    gfx::Point selection_end =
        render_text->GetCursorBounds(sel, true).CenterPoint();

    gfx::LogicalCursorDirection drag_direction = gfx::CURSOR_FORWARD;
    if (std::fabs(delta_y) > std::fabs(delta_x)) {
      // If the initial dragging motion is up/down, extend the selection
      // backwards/forwards.
      drag_direction = delta_y < 0 ? gfx::CURSOR_BACKWARD : gfx::CURSOR_FORWARD;
    } else {
      // Otherwise, extend the selection in the direction of horizontal
      // movement.
      drag_direction = delta_x * (selection_end.x() - selection_start.x()) < 0
                           ? gfx::CURSOR_BACKWARD
                           : gfx::CURSOR_FORWARD;
    }

    gfx::Point base =
        drag_direction == gfx::CURSOR_FORWARD ? selection_start : selection_end;
    gfx::Point extent =
        drag_direction == gfx::CURSOR_FORWARD ? selection_end : selection_start;
    SelectBetweenCoordinates(base, extent);

    selection_dragging_offset_ = extent - event.location();
    selection_dragging_state_ =
        SelectionDraggingState::kDraggingSelectionExtent;
    return true;
  } else if (selection_dragging_state_ == SelectionDraggingState::kNone &&
             std::fabs(delta_x) >= std::fabs(delta_y)) {
    // If a horizontal dragging gesture begins while the cursor is present (i.e.
    // empty selection), use the gesture to move the cursor. Temporarily destroy
    // the touch selection controller so that the touch handles don't appear in
    // the wrong spot before the cursor is moved.
    DestroyTouchSelection();
    MoveCursorTo(event.location(), false);
    selection_dragging_state_ = SelectionDraggingState::kDraggingCursor;
    selection_drag_type_ = ui::TouchSelectionDragType::kCursorDrag;
    return true;
  }
  return false;
}

void Textfield::StopSelectionDragging() {
  if (IsSelectionDragging() && selection_drag_type_.has_value()) {
    ui::RecordTouchSelectionDrag(selection_drag_type_.value());
  }
  selection_dragging_state_ = SelectionDraggingState::kNone;
  selection_drag_type_ = std::nullopt;
}

void Textfield::UpdateAccessibleDefaultActionVerb() {
  if (GetEnabled()) {
    GetViewAccessibility().SetDefaultActionVerb(
        ax::mojom::DefaultActionVerb::kActivate);
  } else {
    GetViewAccessibility().RemoveDefaultActionVerb();
  }
}

BEGIN_METADATA(Textfield)
ADD_PROPERTY_METADATA(bool, ReadOnly)
ADD_PROPERTY_METADATA(std::u16string_view, Text)
ADD_PROPERTY_METADATA(ui::TextInputType, TextInputType)
ADD_PROPERTY_METADATA(int, TextInputFlags)
ADD_PROPERTY_METADATA(SkColor, TextColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(SkColor,
                      SelectionTextColor,
                      ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(SkColor, BackgroundColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(bool, BackgroundEnabled)
ADD_PROPERTY_METADATA(SkColor,
                      SelectionBackgroundColor,
                      ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(bool, CursorEnabled)
ADD_PROPERTY_METADATA(std::u16string_view, PlaceholderText)
ADD_PROPERTY_METADATA(bool, Invalid)
ADD_PROPERTY_METADATA(gfx::HorizontalAlignment, HorizontalAlignment)
ADD_PROPERTY_METADATA(gfx::Range, SelectedRange)
END_METADATA

}  // namespace views