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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/view.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/containers/adapters.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/ime/input_method.h"
#include "ui/compositor/clip_recorder.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/dip_util.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/paint_context.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/transform_recorder.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event_target_iterator.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/angle_conversions.h"
#include "ui/gfx/geometry/point3_f.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/interpolated_transform.h"
#include "ui/gfx/path.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/transform.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/drag_controller.h"
#include "ui/views/layout/layout_manager.h"
#include "ui/views/view_observer.h"
#include "ui/views/view_tracker.h"
#include "ui/views/views_delegate.h"
#include "ui/views/views_switches.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/tooltip_manager.h"
#include "ui/views/widget/widget.h"
#if defined(OS_WIN)
#include "base/win/scoped_gdi_object.h"
#include "ui/native_theme/native_theme_win.h"
#endif
namespace views {
namespace {
#if defined(OS_WIN)
const bool kContextMenuOnMousePress = false;
#else
const bool kContextMenuOnMousePress = true;
#endif
// Default horizontal drag threshold in pixels.
// Same as what gtk uses.
const int kDefaultHorizontalDragThreshold = 8;
// Default vertical drag threshold in pixels.
// Same as what gtk uses.
const int kDefaultVerticalDragThreshold = 8;
// Returns the top view in |view|'s hierarchy.
const View* GetHierarchyRoot(const View* view) {
const View* root = view;
while (root && root->parent())
root = root->parent();
return root;
}
} // namespace
namespace internal {
#if DCHECK_IS_ON()
class ScopedChildrenLock {
public:
explicit ScopedChildrenLock(const View* view)
: reset_(&view->iterating_, true) {}
~ScopedChildrenLock() {}
private:
base::AutoReset<bool> reset_;
DISALLOW_COPY_AND_ASSIGN(ScopedChildrenLock);
};
#else
class ScopedChildrenLock {
public:
explicit ScopedChildrenLock(const View* view) {}
~ScopedChildrenLock() {}
};
#endif
} // namespace internal
// static
const char View::kViewClassName[] = "View";
////////////////////////////////////////////////////////////////////////////////
// View, public:
// Creation and lifetime -------------------------------------------------------
View::View()
: owned_by_client_(false),
id_(0),
group_(-1),
parent_(nullptr),
#if DCHECK_IS_ON()
iterating_(false),
#endif
can_process_events_within_subtree_(true),
visible_(true),
enabled_(true),
notify_enter_exit_on_child_(false),
registered_for_visible_bounds_notification_(false),
needs_layout_(true),
snap_layer_to_pixel_boundary_(false),
flip_canvas_on_paint_for_rtl_ui_(false),
paint_to_layer_(false),
accelerator_focus_manager_(nullptr),
registered_accelerator_count_(0),
next_focusable_view_(nullptr),
previous_focusable_view_(nullptr),
focus_behavior_(FocusBehavior::NEVER),
context_menu_controller_(nullptr),
drag_controller_(nullptr) {
SetTargetHandler(this);
}
View::~View() {
if (parent_)
parent_->RemoveChildView(this);
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
child->parent_ = nullptr;
if (!child->owned_by_client_)
delete child;
}
}
for (ViewObserver& observer : observers_)
observer.OnViewIsDeleting(this);
}
// Tree operations -------------------------------------------------------------
const Widget* View::GetWidget() const {
// The root view holds a reference to this view hierarchy's Widget.
return parent_ ? parent_->GetWidget() : nullptr;
}
Widget* View::GetWidget() {
return const_cast<Widget*>(const_cast<const View*>(this)->GetWidget());
}
void View::AddChildView(View* view) {
if (view->parent_ == this)
return;
AddChildViewAt(view, child_count());
}
void View::AddChildViewAt(View* view, int index) {
CHECK_NE(view, this) << "You cannot add a view as its own child";
DCHECK_GE(index, 0);
DCHECK_LE(index, child_count());
// If |view| has a parent, remove it from its parent.
View* parent = view->parent_;
ui::NativeTheme* old_theme = nullptr;
Widget* old_widget = nullptr;
if (parent) {
old_theme = view->GetNativeTheme();
old_widget = view->GetWidget();
if (parent == this) {
ReorderChildView(view, index);
return;
}
parent->DoRemoveChildView(view, true, true, false, this);
}
// Sets the prev/next focus views.
InitFocusSiblings(view, index);
view->parent_ = this;
#if DCHECK_IS_ON()
DCHECK(!iterating_);
#endif
children_.insert(children_.begin() + index, view);
// Ensure the layer tree matches the view tree before calling to any client
// code. This way if client code further modifies the view tree we are in a
// sane state.
const bool did_reparent_any_layers = view->UpdateParentLayers();
Widget* widget = GetWidget();
if (did_reparent_any_layers && widget)
widget->LayerTreeChanged();
ReorderLayers();
// Make sure the visibility of the child layers are correct.
// If any of the parent View is hidden, then the layers of the subtree
// rooted at |this| should be hidden. Otherwise, all the child layers should
// inherit the visibility of the owner View.
view->UpdateLayerVisibility();
if (widget) {
const ui::NativeTheme* new_theme = view->GetNativeTheme();
if (new_theme != old_theme)
view->PropagateNativeThemeChanged(new_theme);
}
ViewHierarchyChangedDetails details(true, this, view, parent);
for (View* v = this; v; v = v->parent_)
v->ViewHierarchyChangedImpl(false, details);
view->PropagateAddNotifications(details, widget && widget != old_widget);
UpdateTooltip();
if (widget) {
RegisterChildrenForVisibleBoundsNotification(view);
if (view->visible())
view->SchedulePaint();
}
if (layout_manager_.get())
layout_manager_->ViewAdded(this, view);
for (ViewObserver& observer : observers_)
observer.OnChildViewAdded(this, view);
}
void View::ReorderChildView(View* view, int index) {
DCHECK_EQ(view->parent_, this);
if (index < 0)
index = child_count() - 1;
else if (index >= child_count())
return;
if (children_[index] == view)
return;
const Views::iterator i(std::find(children_.begin(), children_.end(), view));
DCHECK(i != children_.end());
#if DCHECK_IS_ON()
DCHECK(!iterating_);
#endif
children_.erase(i);
// Unlink the view first
View* next_focusable = view->next_focusable_view_;
View* prev_focusable = view->previous_focusable_view_;
if (prev_focusable)
prev_focusable->next_focusable_view_ = next_focusable;
if (next_focusable)
next_focusable->previous_focusable_view_ = prev_focusable;
// Add it in the specified index now.
InitFocusSiblings(view, index);
children_.insert(children_.begin() + index, view);
for (ViewObserver& observer : observers_)
observer.OnChildViewReordered(this, view);
ReorderLayers();
}
void View::RemoveChildView(View* view) {
DoRemoveChildView(view, true, true, false, nullptr);
}
void View::RemoveAllChildViews(bool delete_children) {
while (!children_.empty())
DoRemoveChildView(children_.front(), false, false, delete_children,
nullptr);
UpdateTooltip();
}
bool View::Contains(const View* view) const {
for (const View* v = view; v; v = v->parent_) {
if (v == this)
return true;
}
return false;
}
int View::GetIndexOf(const View* view) const {
Views::const_iterator i(std::find(children_.begin(), children_.end(), view));
return i != children_.end() ? static_cast<int>(i - children_.begin()) : -1;
}
// Size and disposition --------------------------------------------------------
void View::SetBounds(int x, int y, int width, int height) {
SetBoundsRect(gfx::Rect(x, y, std::max(0, width), std::max(0, height)));
}
void View::SetBoundsRect(const gfx::Rect& bounds) {
if (bounds == bounds_) {
if (needs_layout_) {
needs_layout_ = false;
Layout();
}
return;
}
if (visible_) {
// Paint where the view is currently.
SchedulePaintBoundsChanged(
bounds_.size() == bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
SCHEDULE_PAINT_SIZE_CHANGED);
}
gfx::Rect prev = bounds_;
bounds_ = bounds;
BoundsChanged(prev);
for (ViewObserver& observer : observers_)
observer.OnViewBoundsChanged(this);
}
void View::SetSize(const gfx::Size& size) {
SetBounds(x(), y(), size.width(), size.height());
}
void View::SetPosition(const gfx::Point& position) {
SetBounds(position.x(), position.y(), width(), height());
}
void View::SetX(int x) {
SetBounds(x, y(), width(), height());
}
void View::SetY(int y) {
SetBounds(x(), y, width(), height());
}
gfx::Rect View::GetContentsBounds() const {
gfx::Rect contents_bounds(GetLocalBounds());
if (border_.get())
contents_bounds.Inset(border_->GetInsets());
return contents_bounds;
}
gfx::Rect View::GetLocalBounds() const {
return gfx::Rect(size());
}
gfx::Insets View::GetInsets() const {
return border_.get() ? border_->GetInsets() : gfx::Insets();
}
gfx::Rect View::GetVisibleBounds() const {
if (!IsDrawn())
return gfx::Rect();
gfx::Rect vis_bounds(GetLocalBounds());
gfx::Rect ancestor_bounds;
const View* view = this;
gfx::Transform transform;
while (view != nullptr && !vis_bounds.IsEmpty()) {
transform.ConcatTransform(view->GetTransform());
gfx::Transform translation;
translation.Translate(static_cast<float>(view->GetMirroredX()),
static_cast<float>(view->y()));
transform.ConcatTransform(translation);
vis_bounds = view->ConvertRectToParent(vis_bounds);
const View* ancestor = view->parent_;
if (ancestor != nullptr) {
ancestor_bounds.SetRect(0, 0, ancestor->width(), ancestor->height());
vis_bounds.Intersect(ancestor_bounds);
} else if (!view->GetWidget()) {
// If the view has no Widget, we're not visible. Return an empty rect.
return gfx::Rect();
}
view = ancestor;
}
if (vis_bounds.IsEmpty())
return vis_bounds;
// Convert back to this views coordinate system.
gfx::RectF views_vis_bounds(vis_bounds);
transform.TransformRectReverse(&views_vis_bounds);
// Partially visible pixels should be considered visible.
return gfx::ToEnclosingRect(views_vis_bounds);
}
gfx::Rect View::GetBoundsInScreen() const {
gfx::Point origin;
View::ConvertPointToScreen(this, &origin);
return gfx::Rect(origin, size());
}
gfx::Size View::GetPreferredSize() const {
if (preferred_size_)
return *preferred_size_;
return CalculatePreferredSize();
}
int View::GetBaseline() const {
return -1;
}
void View::SetPreferredSize(const gfx::Size& size) {
if (preferred_size_ && *preferred_size_ == size)
return;
preferred_size_ = size;
PreferredSizeChanged();
}
void View::SizeToPreferredSize() {
SetSize(GetPreferredSize());
}
gfx::Size View::GetMinimumSize() const {
return GetPreferredSize();
}
gfx::Size View::GetMaximumSize() const {
return gfx::Size();
}
int View::GetHeightForWidth(int w) const {
if (layout_manager_.get())
return layout_manager_->GetPreferredHeightForWidth(this, w);
return GetPreferredSize().height();
}
void View::SetVisible(bool visible) {
if (visible != visible_) {
// If the View is currently visible, schedule paint to refresh parent.
// TODO(beng): not sure we should be doing this if we have a layer.
if (visible_)
SchedulePaint();
visible_ = visible;
AdvanceFocusIfNecessary();
// Notify the parent.
if (parent_) {
parent_->ChildVisibilityChanged(this);
parent_->NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
false);
}
for (ViewObserver& observer : observers_)
observer.OnViewVisibilityChanged(this);
// This notifies all sub-views recursively.
PropagateVisibilityNotifications(this, visible_);
UpdateLayerVisibility();
// If we are newly visible, schedule paint.
if (visible_)
SchedulePaint();
}
}
bool View::IsDrawn() const {
return visible_ && parent_ ? parent_->IsDrawn() : false;
}
void View::SetEnabled(bool enabled) {
if (enabled != enabled_) {
enabled_ = enabled;
AdvanceFocusIfNecessary();
OnEnabledChanged();
for (ViewObserver& observer : observers_)
observer.OnViewEnabledChanged(this);
}
}
void View::OnEnabledChanged() {
SchedulePaint();
}
View::Views View::GetChildrenInZOrder() {
return children_;
}
// Transformations -------------------------------------------------------------
gfx::Transform View::GetTransform() const {
if (!layer())
return gfx::Transform();
gfx::Transform transform = layer()->transform();
gfx::ScrollOffset scroll_offset = layer()->CurrentScrollOffset();
// Offsets for layer-based scrolling are never negative, but the horizontal
// scroll direction is reversed in RTL via canvas flipping.
transform.Translate((base::i18n::IsRTL() ? 1 : -1) * scroll_offset.x(),
-scroll_offset.y());
return transform;
}
void View::SetTransform(const gfx::Transform& transform) {
if (transform.IsIdentity()) {
if (layer()) {
layer()->SetTransform(transform);
if (!paint_to_layer_)
DestroyLayer();
} else {
// Nothing.
}
} else {
if (!layer())
CreateLayer(ui::LAYER_TEXTURED);
layer()->SetTransform(transform);
layer()->ScheduleDraw();
}
}
void View::SetPaintToLayer(ui::LayerType layer_type) {
if (paint_to_layer_ && (layer()->type() == layer_type))
return;
DestroyLayerImpl(LayerChangeNotifyBehavior::DONT_NOTIFY);
CreateLayer(layer_type);
paint_to_layer_ = true;
// Notify the parent chain about the layer change.
NotifyParentsOfLayerChange();
}
void View::DestroyLayer() {
DestroyLayerImpl(LayerChangeNotifyBehavior::NOTIFY);
}
std::unique_ptr<ui::Layer> View::RecreateLayer() {
std::unique_ptr<ui::Layer> old_layer = LayerOwner::RecreateLayer();
Widget* widget = GetWidget();
if (widget)
widget->LayerTreeChanged();
return old_layer;
}
// RTL positioning -------------------------------------------------------------
gfx::Rect View::GetMirroredBounds() const {
gfx::Rect bounds(bounds_);
bounds.set_x(GetMirroredX());
return bounds;
}
gfx::Point View::GetMirroredPosition() const {
return gfx::Point(GetMirroredX(), y());
}
int View::GetMirroredX() const {
return parent_ ? parent_->GetMirroredXForRect(bounds_) : x();
}
int View::GetMirroredXForRect(const gfx::Rect& rect) const {
return base::i18n::IsRTL() ? (width() - rect.x() - rect.width()) : rect.x();
}
gfx::Rect View::GetMirroredRect(const gfx::Rect& rect) const {
gfx::Rect mirrored_rect = rect;
mirrored_rect.set_x(GetMirroredXForRect(rect));
return mirrored_rect;
}
int View::GetMirroredXInView(int x) const {
return base::i18n::IsRTL() ? width() - x : x;
}
int View::GetMirroredXWithWidthInView(int x, int w) const {
return base::i18n::IsRTL() ? width() - x - w : x;
}
// Layout ----------------------------------------------------------------------
void View::Layout() {
needs_layout_ = false;
// If we have a layout manager, let it handle the layout for us.
if (layout_manager_.get())
layout_manager_->Layout(this);
// Make sure to propagate the Layout() call to any children that haven't
// received it yet through the layout manager and need to be laid out. This
// is needed for the case when the child requires a layout but its bounds
// weren't changed by the layout manager. If there is no layout manager, we
// just propagate the Layout() call down the hierarchy, so whoever receives
// the call can take appropriate action.
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
if (child->needs_layout_ || !layout_manager_.get()) {
TRACE_EVENT1("views", "View::Layout", "class", child->GetClassName());
child->needs_layout_ = false;
child->Layout();
}
}
}
void View::InvalidateLayout() {
// Always invalidate up. This is needed to handle the case of us already being
// valid, but not our parent.
needs_layout_ = true;
if (parent_)
parent_->InvalidateLayout();
}
LayoutManager* View::GetLayoutManager() const {
return layout_manager_.get();
}
void View::SetLayoutManager(std::nullptr_t) {
SetLayoutManagerImpl(nullptr);
}
// Attributes ------------------------------------------------------------------
const char* View::GetClassName() const {
return kViewClassName;
}
const View* View::GetAncestorWithClassName(const std::string& name) const {
for (const View* view = this; view; view = view->parent_) {
if (!strcmp(view->GetClassName(), name.c_str()))
return view;
}
return nullptr;
}
View* View::GetAncestorWithClassName(const std::string& name) {
return const_cast<View*>(const_cast<const View*>(this)->
GetAncestorWithClassName(name));
}
const View* View::GetViewByID(int id) const {
if (id == id_)
return const_cast<View*>(this);
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
const View* view = child->GetViewByID(id);
if (view)
return view;
}
return nullptr;
}
View* View::GetViewByID(int id) {
return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id));
}
void View::SetGroup(int gid) {
// Don't change the group id once it's set.
DCHECK(group_ == -1 || group_ == gid);
group_ = gid;
}
int View::GetGroup() const {
return group_;
}
bool View::IsGroupFocusTraversable() const {
return true;
}
void View::GetViewsInGroup(int group, Views* views) {
if (group_ == group)
views->push_back(this);
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->GetViewsInGroup(group, views);
}
View* View::GetSelectedViewForGroup(int group) {
Views views;
GetWidget()->GetRootView()->GetViewsInGroup(group, &views);
return views.empty() ? nullptr : views[0];
}
// Coordinate conversion -------------------------------------------------------
// static
void View::ConvertPointToTarget(const View* source,
const View* target,
gfx::Point* point) {
DCHECK(source);
DCHECK(target);
if (source == target)
return;
const View* root = GetHierarchyRoot(target);
CHECK_EQ(GetHierarchyRoot(source), root);
if (source != root)
source->ConvertPointForAncestor(root, point);
if (target != root)
target->ConvertPointFromAncestor(root, point);
}
// static
void View::ConvertRectToTarget(const View* source,
const View* target,
gfx::RectF* rect) {
DCHECK(source);
DCHECK(target);
if (source == target)
return;
const View* root = GetHierarchyRoot(target);
CHECK_EQ(GetHierarchyRoot(source), root);
if (source != root)
source->ConvertRectForAncestor(root, rect);
if (target != root)
target->ConvertRectFromAncestor(root, rect);
}
// static
void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
src->ConvertPointForAncestor(nullptr, p);
}
// static
void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
DCHECK(dest);
DCHECK(p);
dest->ConvertPointFromAncestor(nullptr, p);
}
// static
void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
// If the view is not connected to a tree, there's nothing we can do.
const Widget* widget = src->GetWidget();
if (widget) {
ConvertPointToWidget(src, p);
*p += widget->GetClientAreaBoundsInScreen().OffsetFromOrigin();
}
}
// static
void View::ConvertPointFromScreen(const View* dst, gfx::Point* p) {
DCHECK(dst);
DCHECK(p);
const views::Widget* widget = dst->GetWidget();
if (!widget)
return;
*p -= widget->GetClientAreaBoundsInScreen().OffsetFromOrigin();
ConvertPointFromWidget(dst, p);
}
// static
void View::ConvertRectToScreen(const View* src, gfx::Rect* rect) {
DCHECK(src);
DCHECK(rect);
gfx::Point new_origin = rect->origin();
views::View::ConvertPointToScreen(src, &new_origin);
rect->set_origin(new_origin);
}
gfx::Rect View::ConvertRectToParent(const gfx::Rect& rect) const {
gfx::RectF x_rect = gfx::RectF(rect);
GetTransform().TransformRect(&x_rect);
x_rect.Offset(GetMirroredPosition().OffsetFromOrigin());
// Pixels we partially occupy in the parent should be included.
return gfx::ToEnclosingRect(x_rect);
}
gfx::Rect View::ConvertRectToWidget(const gfx::Rect& rect) const {
gfx::Rect x_rect = rect;
for (const View* v = this; v; v = v->parent_)
x_rect = v->ConvertRectToParent(x_rect);
return x_rect;
}
// Painting --------------------------------------------------------------------
void View::SchedulePaint() {
SchedulePaintInRect(GetLocalBounds());
}
void View::SchedulePaintInRect(const gfx::Rect& rect) {
if (!visible_)
return;
if (layer()) {
layer()->SchedulePaint(rect);
} else if (parent_) {
// Translate the requested paint rect to the parent's coordinate system
// then pass this notification up to the parent.
parent_->SchedulePaintInRect(ConvertRectToParent(rect));
}
}
void View::Paint(const PaintInfo& parent_paint_info) {
if (!ShouldPaint())
return;
const gfx::Rect& parent_bounds =
!parent() ? GetMirroredBounds() : parent()->GetMirroredBounds();
PaintInfo paint_info = PaintInfo::CreateChildPaintInfo(
parent_paint_info, GetMirroredBounds(), parent_bounds.size(),
GetPaintScaleType(), !!layer());
const ui::PaintContext& context = paint_info.context();
bool is_invalidated = true;
if (paint_info.context().CanCheckInvalid()) {
#if DCHECK_IS_ON()
if (!context.is_pixel_canvas()) {
gfx::Vector2d offset;
context.Visited(this);
View* view = this;
while (view->parent() && !view->layer()) {
DCHECK(view->GetTransform().IsIdentity());
offset += view->GetMirroredPosition().OffsetFromOrigin();
view = view->parent();
}
// The offset in the PaintContext should be the offset up to the paint
// root, which we compute and verify here.
DCHECK_EQ(context.PaintOffset().x(), offset.x());
DCHECK_EQ(context.PaintOffset().y(), offset.y());
// The above loop will stop when |view| is the paint root, which should be
// the root of the current paint walk, as verified by storing the root in
// the PaintContext.
DCHECK_EQ(context.RootVisited(), view);
}
#endif
// If the View wasn't invalidated, don't waste time painting it, the output
// would be culled.
is_invalidated =
context.IsRectInvalid(gfx::Rect(paint_info.paint_recording_size()));
}
TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
// If the view is backed by a layer, it should paint with itself as the origin
// rather than relative to its parent.
// TODO(danakj): Rework clip and transform recorder usage here to use
// std::optional once we can do so.
ui::ClipRecorder clip_recorder(parent_paint_info.context());
if (!layer()) {
// Set the clip rect to the bounds of this View, or |clip_path_| if it's
// been set. Note that the X (or left) position we pass to ClipRect takes
// into consideration whether or not the View uses a right-to-left layout so
// that we paint the View in its mirrored position if need be.
if (clip_path_.isEmpty()) {
clip_recorder.ClipRect(gfx::Rect(paint_info.paint_recording_size()) +
paint_info.offset_from_parent());
} else {
gfx::Path clip_path_in_parent = clip_path_;
// Transform |clip_path_| from local space to parent recording space.
gfx::Transform to_parent_recording_space;
to_parent_recording_space.Translate(paint_info.offset_from_parent());
to_parent_recording_space.Scale(
SkFloatToScalar(paint_info.paint_recording_scale_x()),
SkFloatToScalar(paint_info.paint_recording_scale_y()));
clip_path_in_parent.transform(to_parent_recording_space.matrix());
clip_recorder.ClipPathWithAntiAliasing(clip_path_in_parent);
}
}
ui::TransformRecorder transform_recorder(context);
SetUpTransformRecorderForPainting(paint_info.offset_from_parent(),
&transform_recorder);
// Note that the cache is not aware of the offset of the view
// relative to the parent since painting is always done relative to
// the top left of the individual view.
if (is_invalidated ||
!paint_cache_.UseCache(context, paint_info.paint_recording_size())) {
ui::PaintRecorder recorder(context, paint_info.paint_recording_size(),
paint_info.paint_recording_scale_x(),
paint_info.paint_recording_scale_y(),
&paint_cache_);
gfx::Canvas* canvas = recorder.canvas();
gfx::ScopedRTLFlipCanvas scoped_canvas(canvas, width(),
flip_canvas_on_paint_for_rtl_ui_);
// Delegate painting the contents of the View to the virtual OnPaint method.
OnPaint(canvas);
}
// View::Paint() recursion over the subtree.
PaintChildren(paint_info);
}
void View::SetBackground(std::unique_ptr<Background> b) {
background_ = std::move(b);
}
void View::SetBorder(std::unique_ptr<Border> b) {
border_ = std::move(b);
}
const ui::ThemeProvider* View::GetThemeProvider() const {
const Widget* widget = GetWidget();
return widget ? widget->GetThemeProvider() : nullptr;
}
const ui::NativeTheme* View::GetNativeTheme() const {
if (native_theme_)
return native_theme_;
if (parent())
return parent()->GetNativeTheme();
const Widget* widget = GetWidget();
if (widget)
return widget->GetNativeTheme();
return ui::NativeTheme::GetInstanceForNativeUi();
}
void View::SetNativeTheme(ui::NativeTheme* theme) {
ui::NativeTheme* original_native_theme = GetNativeTheme();
native_theme_ = theme;
if (native_theme_ != original_native_theme)
PropagateNativeThemeChanged(theme);
}
// RTL painting ----------------------------------------------------------------
void View::EnableCanvasFlippingForRTLUI(bool enable) {
flip_canvas_on_paint_for_rtl_ui_ = enable;
}
// Input -----------------------------------------------------------------------
View* View::GetEventHandlerForPoint(const gfx::Point& point) {
return GetEventHandlerForRect(gfx::Rect(point, gfx::Size(1, 1)));
}
View* View::GetEventHandlerForRect(const gfx::Rect& rect) {
return GetEffectiveViewTargeter()->TargetForRect(this, rect);
}
bool View::CanProcessEventsWithinSubtree() const {
return can_process_events_within_subtree_;
}
View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
// TODO(tdanderson): Move this implementation into ViewTargetDelegate.
if (!HitTestPoint(point) || !CanProcessEventsWithinSubtree())
return nullptr;
// Walk the child Views recursively looking for the View that most
// tightly encloses the specified point.
View::Views children = GetChildrenInZOrder();
DCHECK_EQ(child_count(), static_cast<int>(children.size()));
for (auto* child : base::Reversed(children)) {
if (!child->visible())
continue;
gfx::Point point_in_child_coords(point);
ConvertPointToTarget(this, child, &point_in_child_coords);
View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords);
if (handler)
return handler;
}
return this;
}
gfx::NativeCursor View::GetCursor(const ui::MouseEvent& event) {
#if defined(OS_WIN)
static ui::Cursor arrow;
if (!arrow.platform())
arrow.SetPlatformCursor(LoadCursor(nullptr, IDC_ARROW));
return arrow;
#else
return gfx::kNullCursor;
#endif
}
bool View::HitTestPoint(const gfx::Point& point) const {
return HitTestRect(gfx::Rect(point, gfx::Size(1, 1)));
}
bool View::HitTestRect(const gfx::Rect& rect) const {
return GetEffectiveViewTargeter()->DoesIntersectRect(this, rect);
}
bool View::IsMouseHovered() const {
// If we haven't yet been placed in an onscreen view hierarchy, we can't be
// hovered.
if (!GetWidget())
return false;
// If mouse events are disabled, then the mouse cursor is invisible and
// is therefore not hovering over this button.
if (!GetWidget()->IsMouseEventsEnabled())
return false;
gfx::Point cursor_pos(display::Screen::GetScreen()->GetCursorScreenPoint());
ConvertPointFromScreen(this, &cursor_pos);
return HitTestPoint(cursor_pos);
}
bool View::OnMousePressed(const ui::MouseEvent& event) {
return false;
}
bool View::OnMouseDragged(const ui::MouseEvent& event) {
return false;
}
void View::OnMouseReleased(const ui::MouseEvent& event) {
}
void View::OnMouseCaptureLost() {
}
void View::OnMouseMoved(const ui::MouseEvent& event) {
}
void View::OnMouseEntered(const ui::MouseEvent& event) {
}
void View::OnMouseExited(const ui::MouseEvent& event) {
}
void View::SetMouseHandler(View* new_mouse_handler) {
// |new_mouse_handler| may be nullptr.
if (parent_)
parent_->SetMouseHandler(new_mouse_handler);
}
bool View::OnKeyPressed(const ui::KeyEvent& event) {
return false;
}
bool View::OnKeyReleased(const ui::KeyEvent& event) {
return false;
}
bool View::OnMouseWheel(const ui::MouseWheelEvent& event) {
return false;
}
void View::OnKeyEvent(ui::KeyEvent* event) {
bool consumed = (event->type() == ui::ET_KEY_PRESSED) ? OnKeyPressed(*event) :
OnKeyReleased(*event);
if (consumed)
event->StopPropagation();
}
void View::OnMouseEvent(ui::MouseEvent* event) {
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
if (ProcessMousePressed(*event))
event->SetHandled();
return;
case ui::ET_MOUSE_MOVED:
if ((event->flags() & (ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_RIGHT_MOUSE_BUTTON |
ui::EF_MIDDLE_MOUSE_BUTTON)) == 0) {
OnMouseMoved(*event);
return;
}
FALLTHROUGH;
case ui::ET_MOUSE_DRAGGED:
if (ProcessMouseDragged(*event))
event->SetHandled();
return;
case ui::ET_MOUSE_RELEASED:
ProcessMouseReleased(*event);
return;
case ui::ET_MOUSEWHEEL:
if (OnMouseWheel(*event->AsMouseWheelEvent()))
event->SetHandled();
break;
case ui::ET_MOUSE_ENTERED:
if (event->flags() & ui::EF_TOUCH_ACCESSIBILITY)
NotifyAccessibilityEvent(ax::mojom::Event::kHover, true);
OnMouseEntered(*event);
break;
case ui::ET_MOUSE_EXITED:
OnMouseExited(*event);
break;
default:
return;
}
}
void View::OnScrollEvent(ui::ScrollEvent* event) {
}
void View::OnTouchEvent(ui::TouchEvent* event) {
NOTREACHED() << "Views should not receive touch events.";
}
void View::OnGestureEvent(ui::GestureEvent* event) {
}
const ui::InputMethod* View::GetInputMethod() const {
Widget* widget = const_cast<Widget*>(GetWidget());
return widget ? const_cast<const ui::InputMethod*>(widget->GetInputMethod())
: nullptr;
}
std::unique_ptr<ViewTargeter> View::SetEventTargeter(
std::unique_ptr<ViewTargeter> targeter) {
std::unique_ptr<ViewTargeter> old_targeter = std::move(targeter_);
targeter_ = std::move(targeter);
return old_targeter;
}
ViewTargeter* View::GetEffectiveViewTargeter() const {
DCHECK(GetWidget());
ViewTargeter* view_targeter = targeter();
if (!view_targeter)
view_targeter = GetWidget()->GetRootView()->targeter();
CHECK(view_targeter);
return view_targeter;
}
WordLookupClient* View::GetWordLookupClient() {
return nullptr;
}
bool View::CanAcceptEvent(const ui::Event& event) {
return IsDrawn();
}
ui::EventTarget* View::GetParentTarget() {
return parent_;
}
std::unique_ptr<ui::EventTargetIterator> View::GetChildIterator() const {
return std::make_unique<ui::EventTargetIteratorPtrImpl<View>>(children_);
}
ui::EventTargeter* View::GetEventTargeter() {
return targeter_.get();
}
void View::ConvertEventToTarget(ui::EventTarget* target,
ui::LocatedEvent* event) {
event->ConvertLocationToTarget(this, static_cast<View*>(target));
}
// Accelerators ----------------------------------------------------------------
void View::AddAccelerator(const ui::Accelerator& accelerator) {
if (!accelerators_.get())
accelerators_.reset(new std::vector<ui::Accelerator>());
if (!base::ContainsValue(*accelerators_.get(), accelerator))
accelerators_->push_back(accelerator);
RegisterPendingAccelerators();
}
void View::RemoveAccelerator(const ui::Accelerator& accelerator) {
if (!accelerators_.get()) {
NOTREACHED() << "Removing non-existing accelerator";
return;
}
std::vector<ui::Accelerator>::iterator i(
std::find(accelerators_->begin(), accelerators_->end(), accelerator));
if (i == accelerators_->end()) {
NOTREACHED() << "Removing non-existing accelerator";
return;
}
size_t index = i - accelerators_->begin();
accelerators_->erase(i);
if (index >= registered_accelerator_count_) {
// The accelerator is not registered to FocusManager.
return;
}
--registered_accelerator_count_;
// Providing we are attached to a Widget and registered with a focus manager,
// we should de-register from that focus manager now.
if (GetWidget() && accelerator_focus_manager_)
accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
}
void View::ResetAccelerators() {
if (accelerators_.get())
UnregisterAccelerators(false);
}
bool View::AcceleratorPressed(const ui::Accelerator& accelerator) {
return false;
}
bool View::CanHandleAccelerators() const {
const Widget* widget = GetWidget();
if (!enabled() || !IsDrawn() || !widget || !widget->IsVisible())
return false;
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
// Non-ChromeOS aura windows have an associated FocusManagerEventHandler which
// adds currently focused view as an event PreTarget (see
// DesktopNativeWidgetAura::InitNativeWidget). However, the focused view isn't
// always the right view to handle accelerators: It should only handle them
// when active. Only top level widgets can be active, so for child widgets
// check if they are focused instead. ChromeOS also behaves different than
// Linux when an extension popup is about to handle the accelerator.
bool child = widget && widget->GetTopLevelWidget() != widget;
bool focus_in_child =
widget &&
widget->GetRootView()->Contains(GetFocusManager()->GetFocusedView());
if ((child && !focus_in_child) || (!child && !widget->IsActive()))
return false;
#endif
return true;
}
// Focus -----------------------------------------------------------------------
bool View::HasFocus() const {
const FocusManager* focus_manager = GetFocusManager();
return focus_manager && (focus_manager->GetFocusedView() == this);
}
View* View::GetNextFocusableView() {
return next_focusable_view_;
}
const View* View::GetNextFocusableView() const {
return next_focusable_view_;
}
View* View::GetPreviousFocusableView() {
return previous_focusable_view_;
}
void View::SetNextFocusableView(View* view) {
if (view)
view->previous_focusable_view_ = this;
next_focusable_view_ = view;
}
void View::SetFocusBehavior(FocusBehavior focus_behavior) {
if (focus_behavior_ == focus_behavior)
return;
focus_behavior_ = focus_behavior;
AdvanceFocusIfNecessary();
}
bool View::IsFocusable() const {
return focus_behavior_ == FocusBehavior::ALWAYS && enabled_ && IsDrawn();
}
bool View::IsAccessibilityFocusable() const {
return focus_behavior_ != FocusBehavior::NEVER && enabled_ && IsDrawn();
}
FocusManager* View::GetFocusManager() {
Widget* widget = GetWidget();
return widget ? widget->GetFocusManager() : nullptr;
}
const FocusManager* View::GetFocusManager() const {
const Widget* widget = GetWidget();
return widget ? widget->GetFocusManager() : nullptr;
}
void View::RequestFocus() {
FocusManager* focus_manager = GetFocusManager();
if (focus_manager) {
bool focusable = focus_manager->keyboard_accessible()
? IsAccessibilityFocusable()
: IsFocusable();
if (focusable)
focus_manager->SetFocusedView(this);
}
}
bool View::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
return false;
}
FocusTraversable* View::GetFocusTraversable() {
return nullptr;
}
FocusTraversable* View::GetPaneFocusTraversable() {
return nullptr;
}
// Tooltips --------------------------------------------------------------------
bool View::GetTooltipText(const gfx::Point& p, base::string16* tooltip) const {
return false;
}
bool View::GetTooltipTextOrigin(const gfx::Point& p, gfx::Point* loc) const {
return false;
}
// Context menus ---------------------------------------------------------------
void View::ShowContextMenu(const gfx::Point& p,
ui::MenuSourceType source_type) {
if (!context_menu_controller_)
return;
context_menu_controller_->ShowContextMenuForView(this, p, source_type);
}
// static
bool View::ShouldShowContextMenuOnMousePress() {
return kContextMenuOnMousePress;
}
gfx::Point View::GetKeyboardContextMenuLocation() {
gfx::Rect vis_bounds = GetVisibleBounds();
gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
vis_bounds.y() + vis_bounds.height() / 2);
ConvertPointToScreen(this, &screen_point);
return screen_point;
}
// Drag and drop ---------------------------------------------------------------
bool View::GetDropFormats(int* formats,
std::set<ui::Clipboard::FormatType>* format_types) {
return false;
}
bool View::AreDropTypesRequired() {
return false;
}
bool View::CanDrop(const OSExchangeData& data) {
// TODO(sky): when I finish up migration, this should default to true.
return false;
}
void View::OnDragEntered(const ui::DropTargetEvent& event) {
}
int View::OnDragUpdated(const ui::DropTargetEvent& event) {
return ui::DragDropTypes::DRAG_NONE;
}
void View::OnDragExited() {
}
int View::OnPerformDrop(const ui::DropTargetEvent& event) {
return ui::DragDropTypes::DRAG_NONE;
}
void View::OnDragDone() {
}
// static
bool View::ExceededDragThreshold(const gfx::Vector2d& delta) {
return (abs(delta.x()) > GetHorizontalDragThreshold() ||
abs(delta.y()) > GetVerticalDragThreshold());
}
// Accessibility----------------------------------------------------------------
ViewAccessibility& View::GetViewAccessibility() {
if (!view_accessibility_)
view_accessibility_ = ViewAccessibility::Create(this);
return *view_accessibility_;
}
bool View::HandleAccessibleAction(const ui::AXActionData& action_data) {
switch (action_data.action) {
case ax::mojom::Action::kBlur:
if (HasFocus()) {
GetFocusManager()->ClearFocus();
return true;
}
break;
case ax::mojom::Action::kDoDefault: {
const gfx::Point center = GetLocalBounds().CenterPoint();
OnMousePressed(ui::MouseEvent(
ui::ET_MOUSE_PRESSED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
OnMouseReleased(ui::MouseEvent(
ui::ET_MOUSE_RELEASED, center, center, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
return true;
}
case ax::mojom::Action::kFocus:
if (IsAccessibilityFocusable()) {
RequestFocus();
return true;
}
break;
case ax::mojom::Action::kScrollToMakeVisible:
ScrollRectToVisible(GetLocalBounds());
return true;
case ax::mojom::Action::kShowContextMenu:
ShowContextMenu(GetBoundsInScreen().CenterPoint(),
ui::MENU_SOURCE_KEYBOARD);
return true;
default:
// Some actions are handled by subclasses of View.
break;
}
return false;
}
gfx::NativeViewAccessible View::GetNativeViewAccessible() {
return GetViewAccessibility().GetNativeObject();
}
void View::NotifyAccessibilityEvent(ax::mojom::Event event_type,
bool send_native_event) {
if (ViewsDelegate::GetInstance())
ViewsDelegate::GetInstance()->NotifyAccessibilityEvent(this, event_type);
if (send_native_event && GetWidget())
GetViewAccessibility().NotifyAccessibilityEvent(event_type);
OnAccessibilityEvent(event_type);
}
void View::OnAccessibilityEvent(ax::mojom::Event event_type) {}
// Scrolling -------------------------------------------------------------------
void View::ScrollRectToVisible(const gfx::Rect& rect) {
if (parent_)
parent_->ScrollRectToVisible(rect + bounds().OffsetFromOrigin());
}
void View::ScrollViewToVisible() {
ScrollRectToVisible(GetLocalBounds());
}
int View::GetPageScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
int View::GetLineScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
void View::AddObserver(ViewObserver* observer) {
CHECK(observer);
observers_.AddObserver(observer);
}
void View::RemoveObserver(ViewObserver* observer) {
observers_.RemoveObserver(observer);
}
bool View::HasObserver(const ViewObserver* observer) const {
return observers_.HasObserver(observer);
}
////////////////////////////////////////////////////////////////////////////////
// View, protected:
// Size and disposition --------------------------------------------------------
gfx::Size View::CalculatePreferredSize() const {
if (layout_manager_.get())
return layout_manager_->GetPreferredSize(this);
return gfx::Size();
}
void View::OnBoundsChanged(const gfx::Rect& previous_bounds) {
}
void View::PreferredSizeChanged() {
InvalidateLayout();
if (parent_)
parent_->ChildPreferredSizeChanged(this);
for (ViewObserver& observer : observers_)
observer.OnViewPreferredSizeChanged(this);
}
bool View::GetNeedsNotificationWhenVisibleBoundsChange() const {
return false;
}
void View::OnVisibleBoundsChanged() {
}
// Tree operations -------------------------------------------------------------
void View::ViewHierarchyChanged(const ViewHierarchyChangedDetails& details) {
}
void View::VisibilityChanged(View* starting_from, bool is_visible) {
}
void View::NativeViewHierarchyChanged() {
FocusManager* focus_manager = GetFocusManager();
if (accelerator_focus_manager_ != focus_manager) {
UnregisterAccelerators(true);
if (focus_manager)
RegisterPendingAccelerators();
}
}
void View::AddedToWidget() {}
void View::RemovedFromWidget() {}
// Painting --------------------------------------------------------------------
void View::PaintChildren(const PaintInfo& paint_info) {
TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName());
RecursivePaintHelper(&View::Paint, paint_info);
}
void View::OnPaint(gfx::Canvas* canvas) {
TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName());
OnPaintBackground(canvas);
OnPaintBorder(canvas);
}
void View::OnPaintBackground(gfx::Canvas* canvas) {
if (background_) {
TRACE_EVENT0("views", "View::OnPaintBackground");
background_->Paint(canvas, this);
}
}
void View::OnPaintBorder(gfx::Canvas* canvas) {
if (border_) {
TRACE_EVENT0("views", "View::OnPaintBorder");
border_->Paint(*this, canvas);
}
}
// Accelerated Painting --------------------------------------------------------
View::LayerOffsetData View::CalculateOffsetToAncestorWithLayer(
ui::Layer** layer_parent) {
if (layer()) {
if (layer_parent)
*layer_parent = layer();
return LayerOffsetData(layer()->device_scale_factor());
}
if (!parent_)
return LayerOffsetData();
LayerOffsetData offset_data =
parent_->CalculateOffsetToAncestorWithLayer(layer_parent);
return offset_data + GetMirroredPosition().OffsetFromOrigin();
}
void View::UpdateParentLayer() {
if (!layer())
return;
ui::Layer* parent_layer = nullptr;
gfx::Vector2d offset(GetMirroredX(), y());
if (parent_) {
offset +=
parent_->CalculateOffsetToAncestorWithLayer(&parent_layer).offset();
}
ReparentLayer(offset, parent_layer);
}
void View::MoveLayerToParent(ui::Layer* parent_layer,
const LayerOffsetData& offset_data) {
LayerOffsetData local_offset_data(offset_data);
if (parent_layer != layer())
local_offset_data += GetMirroredPosition().OffsetFromOrigin();
if (layer() && parent_layer != layer()) {
parent_layer->Add(layer());
SetLayerBounds(size(), local_offset_data);
} else {
internal::ScopedChildrenLock lock(this);
for (auto* child : GetChildrenInZOrder())
child->MoveLayerToParent(parent_layer, local_offset_data);
}
}
void View::UpdateLayerVisibility() {
bool visible = visible_;
for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_)
visible = v->visible();
UpdateChildLayerVisibility(visible);
}
void View::UpdateChildLayerVisibility(bool ancestor_visible) {
if (layer()) {
layer()->SetVisible(ancestor_visible && visible_);
} else {
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->UpdateChildLayerVisibility(ancestor_visible && visible_);
}
}
void View::DestroyLayerImpl(LayerChangeNotifyBehavior notify_parents) {
if (!paint_to_layer_)
return;
paint_to_layer_ = false;
if (!layer())
return;
ui::Layer* new_parent = layer()->parent();
std::vector<ui::Layer*> children = layer()->children();
for (size_t i = 0; i < children.size(); ++i) {
layer()->Remove(children[i]);
if (new_parent)
new_parent->Add(children[i]);
}
LayerOwner::DestroyLayer();
if (new_parent)
ReorderLayers();
UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(nullptr));
SchedulePaint();
// Notify the parent chain about the layer change.
if (notify_parents == LayerChangeNotifyBehavior::NOTIFY)
NotifyParentsOfLayerChange();
Widget* widget = GetWidget();
if (widget)
widget->LayerTreeChanged();
}
void View::NotifyParentsOfLayerChange() {
// Notify the parent chain about the layer change.
View* view_parent = parent();
while (view_parent) {
view_parent->OnChildLayerChanged(this);
view_parent = view_parent->parent();
}
}
void View::UpdateChildLayerBounds(const LayerOffsetData& offset_data) {
if (layer()) {
SetLayerBounds(size(), offset_data);
} else {
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
child->UpdateChildLayerBounds(
offset_data + child->GetMirroredPosition().OffsetFromOrigin());
}
}
}
void View::OnPaintLayer(const ui::PaintContext& context) {
PaintFromPaintRoot(context);
}
void View::OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) {
snap_layer_to_pixel_boundary_ =
(new_device_scale_factor - std::floor(new_device_scale_factor)) != 0.0f;
if (!layer())
return;
// There can be no subpixel offset if the layer has no parent.
if (!parent() || !layer()->parent())
return;
if (layer()->parent() && layer()->GetCompositor() &&
layer()->GetCompositor()->is_pixel_canvas()) {
LayerOffsetData offset_data(
parent()->CalculateOffsetToAncestorWithLayer(nullptr));
offset_data += GetMirroredPosition().OffsetFromOrigin();
SnapLayerToPixelBoundary(offset_data);
} else {
SnapLayerToPixelBoundary(LayerOffsetData());
}
}
void View::ReorderLayers() {
View* v = this;
while (v && !v->layer())
v = v->parent();
Widget* widget = GetWidget();
if (!v) {
if (widget) {
ui::Layer* layer = widget->GetLayer();
if (layer)
widget->GetRootView()->ReorderChildLayers(layer);
}
} else {
v->ReorderChildLayers(v->layer());
}
if (widget) {
// Reorder the widget's child NativeViews in case a child NativeView is
// associated with a view (e.g. via a NativeViewHost). Always do the
// reordering because the associated NativeView's layer (if it has one)
// is parented to the widget's layer regardless of whether the host view has
// an ancestor with a layer.
widget->ReorderNativeViews();
}
}
void View::ReorderChildLayers(ui::Layer* parent_layer) {
if (layer() && layer() != parent_layer) {
DCHECK_EQ(parent_layer, layer()->parent());
parent_layer->StackAtBottom(layer());
} else {
// Iterate backwards through the children so that a child with a layer
// which is further to the back is stacked above one which is further to
// the front.
View::Views children = GetChildrenInZOrder();
DCHECK_EQ(child_count(), static_cast<int>(children.size()));
for (auto* child : base::Reversed(children))
child->ReorderChildLayers(parent_layer);
}
}
void View::OnChildLayerChanged(View* child) {}
// Input -----------------------------------------------------------------------
View::DragInfo* View::GetDragInfo() {
return parent_ ? parent_->GetDragInfo() : nullptr;
}
// Focus -----------------------------------------------------------------------
void View::OnFocus() {
// TODO(beng): Investigate whether it's possible for us to move this to
// Focus().
// By default, we clear the native focus. This ensures that no visible native
// view as the focus and that we still receive keyboard inputs.
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->ClearNativeFocus();
// TODO(beng): Investigate whether it's possible for us to move this to
// Focus().
// Notify assistive technologies of the focus change.
NotifyAccessibilityEvent(ax::mojom::Event::kFocus, true);
}
void View::OnBlur() {
}
void View::Focus() {
OnFocus();
ScrollViewToVisible();
for (ViewObserver& observer : observers_)
observer.OnViewFocused(this);
}
void View::Blur() {
ViewTracker tracker(this);
OnBlur();
if (tracker.view()) {
for (ViewObserver& observer : observers_)
observer.OnViewBlurred(this);
}
}
// Tooltips --------------------------------------------------------------------
void View::TooltipTextChanged() {
Widget* widget = GetWidget();
// TooltipManager may be null if there is a problem creating it.
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->TooltipTextChanged(this);
}
// Drag and drop ---------------------------------------------------------------
int View::GetDragOperations(const gfx::Point& press_pt) {
return drag_controller_ ?
drag_controller_->GetDragOperationsForView(this, press_pt) :
ui::DragDropTypes::DRAG_NONE;
}
void View::WriteDragData(const gfx::Point& press_pt, OSExchangeData* data) {
DCHECK(drag_controller_);
drag_controller_->WriteDragDataForView(this, press_pt, data);
}
bool View::InDrag() const {
const Widget* widget = GetWidget();
return widget ? widget->dragged_view() == this : false;
}
int View::GetHorizontalDragThreshold() {
// TODO(jennyz): This value may need to be adjusted for different platforms
// and for different display density.
return kDefaultHorizontalDragThreshold;
}
int View::GetVerticalDragThreshold() {
// TODO(jennyz): This value may need to be adjusted for different platforms
// and for different display density.
return kDefaultVerticalDragThreshold;
}
PaintInfo::ScaleType View::GetPaintScaleType() const {
return PaintInfo::ScaleType::kScaleWithEdgeSnapping;
}
////////////////////////////////////////////////////////////////////////////////
// View, private:
// DropInfo --------------------------------------------------------------------
void View::DragInfo::Reset() {
possible_drag = false;
start_pt = gfx::Point();
}
void View::DragInfo::PossibleDrag(const gfx::Point& p) {
possible_drag = true;
start_pt = p;
}
// Painting --------------------------------------------------------------------
void View::SchedulePaintBoundsChanged(SchedulePaintType type) {
// If we have a layer and the View's size did not change, we do not need to
// schedule any paints since the layer will be redrawn at its new location
// during the next Draw() cycle in the compositor.
if (!layer() || type == SCHEDULE_PAINT_SIZE_CHANGED) {
// Otherwise, if the size changes or we don't have a layer then we need to
// use SchedulePaint to invalidate the area occupied by the View.
SchedulePaint();
} else if (parent_ && type == SCHEDULE_PAINT_SIZE_SAME) {
// The compositor doesn't Draw() until something on screen changes, so
// if our position changes but nothing is being animated on screen, then
// tell the compositor to redraw the scene. We know layer() exists due to
// the above if clause.
layer()->ScheduleDraw();
}
}
void View::SchedulePaintOnParent() {
if (parent_) {
// Translate the requested paint rect to the parent's coordinate system
// then pass this notification up to the parent.
parent_->SchedulePaintInRect(ConvertRectToParent(GetLocalBounds()));
}
}
bool View::ShouldPaint() const {
return visible_ && !size().IsEmpty();
}
void View::SetUpTransformRecorderForPainting(
const gfx::Vector2d& offset_from_parent,
ui::TransformRecorder* recorder) const {
// If the view is backed by a layer, it should paint with itself as the origin
// rather than relative to its parent.
if (layer())
return;
// Translate the graphics such that 0,0 corresponds to where this View is
// located relative to its parent.
gfx::Transform transform_from_parent;
transform_from_parent.Translate(offset_from_parent.x(),
offset_from_parent.y());
recorder->Transform(transform_from_parent);
}
void View::RecursivePaintHelper(void (View::*func)(const PaintInfo&),
const PaintInfo& info) {
View::Views children = GetChildrenInZOrder();
DCHECK_EQ(child_count(), static_cast<int>(children.size()));
for (auto* child : children) {
if (!child->layer())
(child->*func)(info);
}
}
void View::PaintFromPaintRoot(const ui::PaintContext& parent_context) {
PaintInfo paint_info = PaintInfo::CreateRootPaintInfo(
parent_context, layer() ? layer()->size() : size());
Paint(paint_info);
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDrawViewBoundsRects))
PaintDebugRects(paint_info);
}
void View::PaintDebugRects(const PaintInfo& parent_paint_info) {
if (!ShouldPaint())
return;
const gfx::Rect& parent_bounds = (layer() || !parent())
? GetMirroredBounds()
: parent()->GetMirroredBounds();
PaintInfo paint_info = PaintInfo::CreateChildPaintInfo(
parent_paint_info, GetMirroredBounds(), parent_bounds.size(),
GetPaintScaleType(), !!layer());
const ui::PaintContext& context = paint_info.context();
ui::TransformRecorder transform_recorder(context);
SetUpTransformRecorderForPainting(paint_info.offset_from_parent(),
&transform_recorder);
RecursivePaintHelper(&View::PaintDebugRects, paint_info);
// Draw outline rects for debugging.
ui::PaintRecorder recorder(context, paint_info.paint_recording_size(),
paint_info.paint_recording_scale_x(),
paint_info.paint_recording_scale_y(),
&paint_cache_);
gfx::Canvas* canvas = recorder.canvas();
const float scale = canvas->UndoDeviceScaleFactor();
gfx::RectF outline_rect(ScaleToEnclosedRect(GetLocalBounds(), scale));
gfx::RectF content_outline_rect(
ScaleToEnclosedRect(GetContentsBounds(), scale));
if (content_outline_rect != outline_rect) {
content_outline_rect.Inset(0.5f, 0.5f);
const SkColor content_color = SkColorSetARGB(0x30, 0, 0, 0xff);
canvas->DrawRect(content_outline_rect, content_color);
}
outline_rect.Inset(0.5f, 0.5f);
const SkColor color = SkColorSetARGB(0x30, 0xff, 0, 0);
canvas->DrawRect(outline_rect, color);
}
// Tree operations -------------------------------------------------------------
void View::DoRemoveChildView(View* view,
bool update_focus_cycle,
bool update_tool_tip,
bool delete_removed_view,
View* new_parent) {
DCHECK(view);
const Views::iterator i(std::find(children_.begin(), children_.end(), view));
if (i == children_.end())
return;
std::unique_ptr<View> view_to_be_deleted;
if (update_focus_cycle) {
View* next_focusable = view->next_focusable_view_;
View* prev_focusable = view->previous_focusable_view_;
if (prev_focusable)
prev_focusable->next_focusable_view_ = next_focusable;
if (next_focusable)
next_focusable->previous_focusable_view_ = prev_focusable;
}
Widget* widget = GetWidget();
bool is_removed_from_widget = false;
if (widget) {
UnregisterChildrenForVisibleBoundsNotification(view);
if (view->visible())
view->SchedulePaint();
is_removed_from_widget = !new_parent || new_parent->GetWidget() != widget;
if (is_removed_from_widget)
widget->NotifyWillRemoveView(view);
}
// Make sure the layers belonging to the subtree rooted at |view| get
// removed.
view->OrphanLayers();
if (widget)
widget->LayerTreeChanged();
view->PropagateRemoveNotifications(this, new_parent, is_removed_from_widget);
view->parent_ = nullptr;
if (delete_removed_view && !view->owned_by_client_)
view_to_be_deleted.reset(view);
#if DCHECK_IS_ON()
DCHECK(!iterating_);
#endif
children_.erase(i);
if (update_tool_tip)
UpdateTooltip();
if (layout_manager_)
layout_manager_->ViewRemoved(this, view);
for (ViewObserver& observer : observers_)
observer.OnChildViewRemoved(this, view);
}
void View::PropagateRemoveNotifications(View* old_parent,
View* new_parent,
bool is_removed_from_widget) {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
child->PropagateRemoveNotifications(old_parent, new_parent,
is_removed_from_widget);
}
}
ViewHierarchyChangedDetails details(false, old_parent, this, new_parent);
for (View* v = this; v; v = v->parent_)
v->ViewHierarchyChangedImpl(true, details);
if (is_removed_from_widget)
RemovedFromWidget();
}
void View::PropagateAddNotifications(const ViewHierarchyChangedDetails& details,
bool is_added_to_widget) {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->PropagateAddNotifications(details, is_added_to_widget);
}
ViewHierarchyChangedImpl(true, details);
if (is_added_to_widget)
AddedToWidget();
}
void View::PropagateNativeViewHierarchyChanged() {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->PropagateNativeViewHierarchyChanged();
}
NativeViewHierarchyChanged();
}
void View::ViewHierarchyChangedImpl(
bool register_accelerators,
const ViewHierarchyChangedDetails& details) {
if (register_accelerators) {
if (details.is_add) {
// If you get this registration, you are part of a subtree that has been
// added to the view hierarchy.
if (GetFocusManager())
RegisterPendingAccelerators();
} else {
if (details.child == this)
UnregisterAccelerators(true);
}
}
ViewHierarchyChanged(details);
details.parent->needs_layout_ = true;
}
void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) {
if (native_theme_ && native_theme_ != theme)
return;
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->PropagateNativeThemeChanged(theme);
}
OnNativeThemeChanged(theme);
for (ViewObserver& observer : observers_)
observer.OnViewNativeThemeChanged(this);
}
// Size and disposition --------------------------------------------------------
void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->PropagateVisibilityNotifications(start, is_visible);
}
VisibilityChangedImpl(start, is_visible);
}
void View::VisibilityChangedImpl(View* starting_from, bool is_visible) {
VisibilityChanged(starting_from, is_visible);
}
void View::SnapLayerToPixelBoundary(const LayerOffsetData& offset_data) {
if (!layer())
return;
if (snap_layer_to_pixel_boundary_ && layer()->parent() &&
layer()->GetCompositor()) {
if (layer()->GetCompositor()->is_pixel_canvas()) {
layer()->SetSubpixelPositionOffset(offset_data.GetSubpixelOffset());
} else {
ui::SnapLayerToPhysicalPixelBoundary(layer()->parent(), layer());
}
} else {
// Reset the offset.
layer()->SetSubpixelPositionOffset(gfx::Vector2dF());
}
}
void View::BoundsChanged(const gfx::Rect& previous_bounds) {
if (visible_) {
// Paint the new bounds.
SchedulePaintBoundsChanged(
bounds_.size() == previous_bounds.size() ? SCHEDULE_PAINT_SIZE_SAME :
SCHEDULE_PAINT_SIZE_CHANGED);
}
if (layer()) {
if (parent_) {
LayerOffsetData offset_data(
parent_->CalculateOffsetToAncestorWithLayer(nullptr));
offset_data += GetMirroredPosition().OffsetFromOrigin();
SetLayerBounds(size(), offset_data);
} else {
SetLayerBounds(bounds_.size(),
LayerOffsetData() + bounds_.OffsetFromOrigin());
}
// In RTL mode, if our width has changed, our children's mirrored bounds
// will have changed. Update the child's layer bounds, or if it is not a
// layer, the bounds of any layers inside the child.
if (base::i18n::IsRTL() && bounds_.width() != previous_bounds.width()) {
for (int i = 0; i < child_count(); ++i) {
View* child = child_at(i);
child->UpdateChildLayerBounds(
LayerOffsetData(layer()->device_scale_factor(),
child->GetMirroredPosition().OffsetFromOrigin()));
}
}
} else {
// If our bounds have changed, then any descendant layer bounds may have
// changed. Update them accordingly.
UpdateChildLayerBounds(CalculateOffsetToAncestorWithLayer(nullptr));
}
OnBoundsChanged(previous_bounds);
if (bounds_ != previous_bounds)
NotifyAccessibilityEvent(ax::mojom::Event::kLocationChanged, false);
if (needs_layout_ || previous_bounds.size() != size()) {
needs_layout_ = false;
Layout();
}
if (GetNeedsNotificationWhenVisibleBoundsChange())
OnVisibleBoundsChanged();
// Notify interested Views that visible bounds within the root view may have
// changed.
if (descendants_to_notify_.get()) {
for (Views::iterator i(descendants_to_notify_->begin());
i != descendants_to_notify_->end(); ++i) {
(*i)->OnVisibleBoundsChanged();
}
}
}
// static
void View::RegisterChildrenForVisibleBoundsNotification(View* view) {
if (view->GetNeedsNotificationWhenVisibleBoundsChange())
view->RegisterForVisibleBoundsNotification();
for (int i = 0; i < view->child_count(); ++i)
RegisterChildrenForVisibleBoundsNotification(view->child_at(i));
}
// static
void View::UnregisterChildrenForVisibleBoundsNotification(View* view) {
if (view->GetNeedsNotificationWhenVisibleBoundsChange())
view->UnregisterForVisibleBoundsNotification();
for (int i = 0; i < view->child_count(); ++i)
UnregisterChildrenForVisibleBoundsNotification(view->child_at(i));
}
void View::RegisterForVisibleBoundsNotification() {
if (registered_for_visible_bounds_notification_)
return;
registered_for_visible_bounds_notification_ = true;
for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
ancestor->AddDescendantToNotify(this);
}
void View::UnregisterForVisibleBoundsNotification() {
if (!registered_for_visible_bounds_notification_)
return;
registered_for_visible_bounds_notification_ = false;
for (View* ancestor = parent_; ancestor; ancestor = ancestor->parent_)
ancestor->RemoveDescendantToNotify(this);
}
void View::AddDescendantToNotify(View* view) {
DCHECK(view);
if (!descendants_to_notify_.get())
descendants_to_notify_.reset(new Views);
descendants_to_notify_->push_back(view);
}
void View::RemoveDescendantToNotify(View* view) {
DCHECK(view && descendants_to_notify_.get());
Views::iterator i(std::find(
descendants_to_notify_->begin(), descendants_to_notify_->end(), view));
DCHECK(i != descendants_to_notify_->end());
descendants_to_notify_->erase(i);
if (descendants_to_notify_->empty())
descendants_to_notify_.reset();
}
void View::SetLayoutManagerImpl(std::unique_ptr<LayoutManager> layout_manager) {
// Some code keeps a bare pointer to the layout manager for calling
// derived-class-specific-functions. It's an easy mistake to create a new
// unique_ptr and re-set the layout manager with a new unique_ptr, which
// will cause a crash. Re-setting to nullptr is OK.
CHECK(!layout_manager.get() || layout_manager_.get() != layout_manager.get());
layout_manager_ = std::move(layout_manager);
if (layout_manager_)
layout_manager_->Installed(this);
}
void View::SetLayerBounds(const gfx::Size& size,
const LayerOffsetData& offset_data) {
layer()->SetBounds(gfx::Rect(size) + offset_data.offset());
SnapLayerToPixelBoundary(offset_data);
}
// Transformations -------------------------------------------------------------
bool View::GetTransformRelativeTo(const View* ancestor,
gfx::Transform* transform) const {
const View* p = this;
while (p && p != ancestor) {
transform->ConcatTransform(p->GetTransform());
gfx::Transform translation;
translation.Translate(static_cast<float>(p->GetMirroredX()),
static_cast<float>(p->y()));
transform->ConcatTransform(translation);
p = p->parent_;
}
return p == ancestor;
}
// Coordinate conversion -------------------------------------------------------
bool View::ConvertPointForAncestor(const View* ancestor,
gfx::Point* point) const {
gfx::Transform trans;
// TODO(sad): Have some way of caching the transformation results.
bool result = GetTransformRelativeTo(ancestor, &trans);
auto p = gfx::Point3F(gfx::PointF(*point));
trans.TransformPoint(&p);
*point = gfx::ToFlooredPoint(p.AsPointF());
return result;
}
bool View::ConvertPointFromAncestor(const View* ancestor,
gfx::Point* point) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
auto p = gfx::Point3F(gfx::PointF(*point));
trans.TransformPointReverse(&p);
*point = gfx::ToFlooredPoint(p.AsPointF());
return result;
}
bool View::ConvertRectForAncestor(const View* ancestor,
gfx::RectF* rect) const {
gfx::Transform trans;
// TODO(sad): Have some way of caching the transformation results.
bool result = GetTransformRelativeTo(ancestor, &trans);
trans.TransformRect(rect);
return result;
}
bool View::ConvertRectFromAncestor(const View* ancestor,
gfx::RectF* rect) const {
gfx::Transform trans;
bool result = GetTransformRelativeTo(ancestor, &trans);
trans.TransformRectReverse(rect);
return result;
}
// Accelerated painting --------------------------------------------------------
void View::CreateLayer(ui::LayerType layer_type) {
// A new layer is being created for the view. So all the layers of the
// sub-tree can inherit the visibility of the corresponding view.
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->UpdateChildLayerVisibility(true);
}
SetLayer(std::make_unique<ui::Layer>(layer_type));
layer()->set_delegate(this);
layer()->set_name(GetClassName());
UpdateParentLayers();
UpdateLayerVisibility();
// The new layer needs to be ordered in the layer tree according
// to the view tree. Children of this layer were added in order
// in UpdateParentLayers().
if (parent())
parent()->ReorderLayers();
Widget* widget = GetWidget();
if (widget)
widget->LayerTreeChanged();
// Before having its own Layer, this View may have painted in to a Layer owned
// by an ancestor View. Scheduling a paint on the parent View will erase this
// View's painting effects on the ancestor View's Layer.
// (See crbug.com/551492)
SchedulePaintOnParent();
}
bool View::UpdateParentLayers() {
// Attach all top-level un-parented layers.
if (layer()) {
if (!layer()->parent()) {
UpdateParentLayer();
return true;
}
// The layers of any child views are already in place, so we can stop
// iterating here.
return false;
}
bool result = false;
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
if (child->UpdateParentLayers())
result = true;
}
return result;
}
void View::OrphanLayers() {
if (layer()) {
if (layer()->parent())
layer()->parent()->Remove(layer());
// The layer belonging to this View has already been orphaned. It is not
// necessary to orphan the child layers.
return;
}
internal::ScopedChildrenLock lock(this);
for (auto* child : children_)
child->OrphanLayers();
}
void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) {
layer()->SetBounds(GetLocalBounds() + offset);
DCHECK_NE(layer(), parent_layer);
if (parent_layer)
parent_layer->Add(layer());
layer()->SchedulePaint(GetLocalBounds());
MoveLayerToParent(layer(), LayerOffsetData(layer()->device_scale_factor()));
}
// Input -----------------------------------------------------------------------
bool View::ProcessMousePressed(const ui::MouseEvent& event) {
int drag_operations =
(enabled_ && event.IsOnlyLeftMouseButton() &&
HitTestPoint(event.location())) ?
GetDragOperations(event.location()) : 0;
ContextMenuController* context_menu_controller = event.IsRightMouseButton() ?
context_menu_controller_ : 0;
View::DragInfo* drag_info = GetDragInfo();
const bool enabled = enabled_;
const bool result = OnMousePressed(event);
if (!enabled)
return result;
if (event.IsOnlyRightMouseButton() && context_menu_controller &&
kContextMenuOnMousePress) {
// Assume that if there is a context menu controller we won't be deleted
// from mouse pressed.
if (HitTestPoint(event.location())) {
gfx::Point location(event.location());
ConvertPointToScreen(this, &location);
ShowContextMenu(location, ui::MENU_SOURCE_MOUSE);
return true;
}
}
// WARNING: we may have been deleted, don't use any View variables.
if (drag_operations != ui::DragDropTypes::DRAG_NONE) {
drag_info->PossibleDrag(event.location());
return true;
}
return !!context_menu_controller || result;
}
bool View::ProcessMouseDragged(const ui::MouseEvent& event) {
// Copy the field, that way if we're deleted after drag and drop no harm is
// done.
ContextMenuController* context_menu_controller = context_menu_controller_;
const bool possible_drag = GetDragInfo()->possible_drag;
if (possible_drag &&
ExceededDragThreshold(GetDragInfo()->start_pt - event.location()) &&
(!drag_controller_ ||
drag_controller_->CanStartDragForView(
this, GetDragInfo()->start_pt, event.location()))) {
DoDrag(event, GetDragInfo()->start_pt,
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
} else {
if (OnMouseDragged(event))
return true;
// Fall through to return value based on context menu controller.
}
// WARNING: we may have been deleted.
return (context_menu_controller != nullptr) || possible_drag;
}
void View::ProcessMouseReleased(const ui::MouseEvent& event) {
if (!kContextMenuOnMousePress && context_menu_controller_ &&
event.IsOnlyRightMouseButton()) {
// Assume that if there is a context menu controller we won't be deleted
// from mouse released.
gfx::Point location(event.location());
OnMouseReleased(event);
if (HitTestPoint(location)) {
ConvertPointToScreen(this, &location);
ShowContextMenu(location, ui::MENU_SOURCE_MOUSE);
}
} else {
OnMouseReleased(event);
}
// WARNING: we may have been deleted.
}
// Accelerators ----------------------------------------------------------------
void View::RegisterPendingAccelerators() {
if (!accelerators_.get() ||
registered_accelerator_count_ == accelerators_->size()) {
// No accelerators are waiting for registration.
return;
}
if (!GetWidget()) {
// The view is not yet attached to a widget, defer registration until then.
return;
}
accelerator_focus_manager_ = GetFocusManager();
if (!accelerator_focus_manager_) {
// Some crash reports seem to show that we may get cases where we have no
// focus manager (see bug #1291225). This should never be the case, just
// making sure we don't crash.
NOTREACHED();
return;
}
for (std::vector<ui::Accelerator>::const_iterator i(
accelerators_->begin() + registered_accelerator_count_);
i != accelerators_->end(); ++i) {
accelerator_focus_manager_->RegisterAccelerator(
*i, ui::AcceleratorManager::kNormalPriority, this);
}
registered_accelerator_count_ = accelerators_->size();
}
void View::UnregisterAccelerators(bool leave_data_intact) {
if (!accelerators_.get())
return;
if (GetWidget()) {
if (accelerator_focus_manager_) {
accelerator_focus_manager_->UnregisterAccelerators(this);
accelerator_focus_manager_ = nullptr;
}
if (!leave_data_intact) {
accelerators_->clear();
accelerators_.reset();
}
registered_accelerator_count_ = 0;
}
}
// Focus -----------------------------------------------------------------------
void View::InitFocusSiblings(View* v, int index) {
int count = child_count();
if (count == 0) {
v->next_focusable_view_ = nullptr;
v->previous_focusable_view_ = nullptr;
} else {
if (index == count) {
// We are inserting at the end, but the end of the child list may not be
// the last focusable element. Let's try to find an element with no next
// focusable element to link to.
View* last_focusable_view = nullptr;
{
internal::ScopedChildrenLock lock(this);
for (auto* child : children_) {
if (!child->next_focusable_view_) {
last_focusable_view = child;
break;
}
}
}
if (last_focusable_view == nullptr) {
// Hum... there is a cycle in the focus list. Let's just insert ourself
// after the last child.
View* prev = children_[index - 1];
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = prev->next_focusable_view_;
prev->next_focusable_view_->previous_focusable_view_ = v;
prev->next_focusable_view_ = v;
} else {
last_focusable_view->next_focusable_view_ = v;
v->next_focusable_view_ = nullptr;
v->previous_focusable_view_ = last_focusable_view;
}
} else {
View* prev = children_[index]->GetPreviousFocusableView();
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = children_[index];
if (prev)
prev->next_focusable_view_ = v;
children_[index]->previous_focusable_view_ = v;
}
}
}
void View::AdvanceFocusIfNecessary() {
// Focus should only be advanced if this is the focused view and has become
// unfocusable. If the view is still focusable or is not focused, we can
// return early avoiding further unnecessary checks. Focusability check is
// performed first as it tends to be faster.
if (IsAccessibilityFocusable() || !HasFocus())
return;
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->AdvanceFocusIfNecessary();
}
// System events ---------------------------------------------------------------
void View::PropagateThemeChanged() {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : base::Reversed(children_))
child->PropagateThemeChanged();
}
OnThemeChanged();
}
void View::PropagateDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) {
{
internal::ScopedChildrenLock lock(this);
for (auto* child : base::Reversed(children_)) {
child->PropagateDeviceScaleFactorChanged(old_device_scale_factor,
new_device_scale_factor);
}
}
// If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called
// through LayerDelegate callback.
if (!layer())
OnDeviceScaleFactorChanged(old_device_scale_factor,
new_device_scale_factor);
}
// Tooltips --------------------------------------------------------------------
void View::UpdateTooltip() {
Widget* widget = GetWidget();
// TODO(beng): The TooltipManager nullptr check can be removed when we
// consolidate Init() methods and make views_unittests Init() all
// Widgets that it uses.
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->UpdateTooltip();
}
// Drag and drop ---------------------------------------------------------------
bool View::DoDrag(const ui::LocatedEvent& event,
const gfx::Point& press_pt,
ui::DragDropTypes::DragEventSource source) {
int drag_operations = GetDragOperations(press_pt);
if (drag_operations == ui::DragDropTypes::DRAG_NONE)
return false;
Widget* widget = GetWidget();
// We should only start a drag from an event, implying we have a widget.
DCHECK(widget);
// Don't attempt to start a drag while in the process of dragging. This is
// especially important on X where we can get multiple mouse move events when
// we start the drag.
if (widget->dragged_view())
return false;
OSExchangeData data;
WriteDragData(press_pt, &data);
// Message the RootView to do the drag and drop. That way if we're removed
// the RootView can detect it and avoid calling us back.
gfx::Point widget_location(event.location());
ConvertPointToWidget(this, &widget_location);
widget->RunShellDrag(this, data, widget_location, drag_operations, source);
// WARNING: we may have been deleted.
return true;
}
} // namespace views
|