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
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
#![allow(clippy::needless_doctest_main)]

/*!
 * Crate for easy integration of the [Dear ImGui][dearimgui] library.
 *
 * This crate is a bind to the Dear ImGui library only. There is also a matching rendering
 * library, [`easy-imgui-renderer`](https://docs.rs/easy-imgui-renderer), that renders the UI using OpenGl, and a matching
 * window-integrated library, [`easy-imgui-window`](https://docs.rs/easy-imgui-window/), that enables to build a full desktop
 * application in just a few lines.
 *
 * If you don't know where to start, then start with the latter. Take a look at the [examples].
 * The simplest `easy-imgui` program would be something like this:
 *
 * ```rust
 * use easy_imgui_window::{
 *     easy_imgui as imgui,
 *     MainWindow,
 *     MainWindowWithRenderer,
 *     winit::event_loop::EventLoopBuilder,
 * };
 *
 * // The App type, this will do the actual app. stuff.
 * struct App;
 *
 * impl imgui::UiBuilder for App {
 *     // There are other function in this trait, but this is the only one
 *     // mandatory and the most important: it build the UI.
 *     fn do_ui(&mut self, ui: &imgui::Ui<Self>) {
 *         ui.show_demo_window(None);
 *     }
 * }
 *
 * fn main() {
 *     // Create a `winit` event loop.
 *     let event_loop = EventLoopBuilder::new().build().unwrap();
 *     // Create a `winit` window.
 *     let main_window = MainWindow::new(&event_loop, "Example").unwrap();
 *     // Create a `easy-imgui` window.
 *     let mut window = MainWindowWithRenderer::new(main_window);
 *
 *     // Create the app object.
 *     let mut app = App;
 *
 *     // Run the loop.
 *     event_loop.run(move |event, w| {
 *         // Do the magic!
 *         let res = window.do_event(&mut app, &event, w);
 *         // If the user requested to close the app, you should consider doing that.
 *         if res.is_break() {
 *             w.exit();
 *         }
 *     }).unwrap();
 * }
 * ```
 *
 * # Alternatives
 * This `crate` is similar to [`imgui-rs`][imguirs], and it is inpired by it, but with a few key
 * differences:
 *  * It doesn't use any C++-to-C api generator, as `rust-bindgen` is able to import simple C++
 * libraries directly.
 *  * It is lower level, there are fewer high-level abstractions over the ImGui API. This means
 * that:
 *      * This API is less Rusty than imgui-rs's.
 *      * If you know how to use Dear ImGui, then you know how to use easy-imgui.
 *      * It is far easier to upgrade to new Dear ImGui versions.
 *
 * # Features
 * These are the available features for this crate:
 *  * `freetype`: Uses an external _freetype_ font loader for Dear ImGui, instead of the embedded
 *  `stb_truetype` library.
 *  * `docking`: Uses the `docking` branch of Dear ImGui. Note that this is considered somewhat
 *  experimental.
 *
 * # Usage
 * It is easier to use one of the higher level crates [`easy-imgui-window`] or [`easy-imgui-renderer`].
 * But if you intend to render the UI yourself, then you can use this directly.
 *
 * These are the main pieces of this crate:
 *  * [`Context`]: It represents the ImGui context. In DearImgui this is a global variable. Here it
 *  is a thread-local variable. Still, since it is implicit in most API calls, most uses of this
 *  type are unsafe. If you use [`easy-imgui-window`] or [`easy-imgui-renderer`] you will rarely
 *  need to touch this type directly.
 *  * [`Ui`]: A frame that is being built. Most ImGui functions are members of `Ui`.
 *  * [`UiBuilder`]: A trait that your application implements do build your user interface.
 *
 * If you want to use this library directly, just create a [`Context`], set up its properties, and
 * when you want to render a frame do [`Context::set_current`] and then [`CurrentContext::do_frame`].
 *
 * If you use one of the helper crates then you will just implement `UiBuilder` and get a `Ui` for
 * free.
 *
 * # Conventions
 * This crate follows a series of naming conventions to make the API more predictable,
 * particularly with the [`Ui`] member functions:
 *  * A [`Pushable`] is any value that can be made active by a _push_ function and inactive by a
 *  corresponding _pop_ function. Examples are styles, colors, fonts...
 *  * A [`Hashable`] is any value that can be used to build an ImGui hash id. Ideally there should
 *  be one of these everywhere, but the Dear ImGui API it not totally othogonal here...
 *  * A function without special prefix or suffix does the same thing as its Dear ImGui
 *  counterpart. For example [`Ui::button`] calls `ImGui_Button`.
 *  * A function name that contains the `with` word takes a function that is called immediately. It
 *  corresponds to a pair of `*Begin` and `*End` functions in Dear ImGui. The function is called
 *  between these two functions. The value returned will be that of the function.
 *      * If the function is called based on some condition, such as with `ImGui_BeginChild`, then there
 *      will be another function with prefix `with_always_` that takes a function with a bool
 *      argument `opened: bool`, that can be used if you need the function to be called even if the
 *      condition is not met.
 *  * A function name that ends as `_config` will create a builder object (with the `must_use`
 *  annotation). This object will have a few properties to be set and a `build` or a
 *  `with` function to create the actual UI element.
 *  * Most builder object have a `push_for_begin` function, that will set up the pushable to be
 *  used only for the `begin` part of the UI. This is useful for example to set up the style for a
 *  window but not for its contents.
 *
 * When a function takes a value of type `String` this crate will usually take a generic `impl IntoCStr`.
 * This is an optimization that allows you to pass either a `String`, a `&str`, a `CString` or a
 * `&CStr`, avoiding an extra allocation if it is not really necessary. If you pass a constant
 * string and have a recent Rust compiler you can pass a literal `CStr` with the new syntax `c"hello"`.
 *
 *
 *
 *
 * [dearimgui]: https://github.com/ocornut/imgui
 * [imguirs]: https://github.com/imgui-rs/imgui-rs
 * [examples]: ../../../easy-imgui/examples
 */

// Too many unsafes ahead
#![allow(clippy::missing_safety_doc, clippy::too_many_arguments)]

pub use cgmath;
use cstr::cstr;
use easy_imgui_sys::*;
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::ffi::{c_char, c_void, CStr, CString};
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ptr::{null, null_mut};

/// A type alias of the `cgmath::Vector2<f32>`.
///
/// Used in this crate to describe a 2D position or size.
/// The equivalent type in Dear ImGui would be [`ImVec2`].
pub type Vector2 = cgmath::Vector2<f32>;

mod enums;
pub mod style;

pub use easy_imgui_sys::{self, ImGuiID};
pub use enums::*;
pub use image;
pub use mint;

use image::GenericImage;

// Here we use a "generation value" to avoid calling stale callbacks. It shouldn't happen, but just
// in case, as it would cause undefined behavior.
// The generation value is taken from the ImGui frame number, that is increased every frame.
// The callback id itself is composed by combining the callback index with the generation value.
// When calling a callback, if the generation value does not match the callback is ignored.
const GEN_BITS: u32 = 8;
const GEN_ID_BITS: u32 = usize::BITS - GEN_BITS;
const GEN_MASK: usize = (1 << GEN_BITS) - 1;
const GEN_ID_MASK: usize = (1 << GEN_ID_BITS) - 1;

fn merge_generation(id: usize, gen: usize) -> usize {
    if (id & GEN_ID_MASK) != id {
        panic!("UI callback overflow")
    }
    (gen << GEN_ID_BITS) | id
}
fn remove_generation(id: usize, gen: usize) -> Option<usize> {
    if (id >> GEN_ID_BITS) != (gen & GEN_MASK) {
        None
    } else {
        Some(id & GEN_ID_MASK)
    }
}

/// Helper function to create a `Vector2`.
pub fn to_v2(v: impl Into<mint::Vector2<f32>>) -> Vector2 {
    let v = v.into();
    Vector2 { x: v.x, y: v.y }
}
/// Helper function to create a `Vector2`.
pub const fn vec2(x: f32, y: f32) -> Vector2 {
    Vector2 { x, y }
}
/// Helper function to create a `ImVec2`.
pub const fn im_vec2(x: f32, y: f32) -> ImVec2 {
    ImVec2 { x, y }
}
/// Helper function to create a `ImVec2`.
pub const fn v2_to_im(v: Vector2) -> ImVec2 {
    ImVec2 { x: v.x, y: v.y }
}
/// Helper function to create a `Vector2`.
pub const fn im_to_v2(v: ImVec2) -> Vector2 {
    Vector2 { x: v.x, y: v.y }
}

/// A color is stored as a `[r, g, b, a]`, each value between 0.0 and 1.0.
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}
impl Color {
    // Primary and secondary colors
    pub const TRANSPARENT: Color = Color::new(0.0, 0.0, 0.0, 0.0);
    pub const WHITE: Color = Color::new(1.0, 1.0, 1.0, 1.0);
    pub const BLACK: Color = Color::new(0.0, 0.0, 0.0, 1.0);
    pub const RED: Color = Color::new(1.0, 0.0, 0.0, 1.0);
    pub const GREEN: Color = Color::new(0.0, 1.0, 0.0, 1.0);
    pub const BLUE: Color = Color::new(0.0, 0.0, 1.0, 1.0);
    pub const YELLOW: Color = Color::new(1.0, 1.0, 0.0, 1.0);
    pub const MAGENTA: Color = Color::new(1.0, 0.0, 1.0, 1.0);
    pub const CYAN: Color = Color::new(0.0, 1.0, 1.0, 1.0);

    pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
        Color { r, g, b, a }
    }
    /// Converts a `Color` into a packed `u32` value, required by some Dear ImGui functions.
    pub fn as_u32(&self) -> u32 {
        unsafe { ImGui_ColorConvertFloat4ToU32(&(*self).into()) }
    }
}
impl AsRef<[f32; 4]> for Color {
    fn as_ref(&self) -> &[f32; 4] {
        // SAFETY: Self is repr(C) so layout compatible with an array
        unsafe { std::mem::transmute::<&Color, &[f32; 4]>(self) }
    }
}
impl AsMut<[f32; 4]> for Color {
    fn as_mut(&mut self) -> &mut [f32; 4] {
        // SAFETY: Self is repr(C) so layout compatible with an array
        unsafe { std::mem::transmute::<&mut Color, &mut [f32; 4]>(self) }
    }
}
impl From<ImVec4> for Color {
    #[inline]
    fn from(c: ImVec4) -> Color {
        Color::new(c.x, c.y, c.z, c.w)
    }
}
impl From<Color> for ImVec4 {
    #[inline]
    fn from(c: Color) -> ImVec4 {
        ImVec4 {
            x: c.r,
            y: c.g,
            z: c.b,
            w: c.a,
        }
    }
}

/// The main ImGui context.
pub struct Context {
    imgui: *mut ImGuiContext,
    pending_atlas: bool,
}

pub struct CurrentContext<'a> {
    ctx: &'a mut Context,
}

impl Context {
    pub unsafe fn new() -> Context {
        let imgui = unsafe {
            let imgui = ImGui_CreateContext(null_mut());
            ImGui_SetCurrentContext(imgui);

            let io = ImGui_GetIO();
            (*io).IniFilename = null();

            // If you eanbled the "docking" feature you will want to use it
            #[cfg(feature = "docking")]
            {
                (*io).ConfigFlags |= ConfigFlags::DockingEnable.bits();
            }

            //ImGui_StyleColorsDark(null_mut());
            imgui
        };
        Context {
            imgui,
            pending_atlas: true,
        }
    }
    /// Makes this context the current one.
    ///
    /// SAFETY: Do not make two different contexts current at the same time
    /// in the same thread.
    pub unsafe fn set_current(&mut self) -> CurrentContext<'_> {
        ImGui_SetCurrentContext(self.imgui);
        CurrentContext { ctx: self }
    }
    /// The next time [`CurrentContext::do_frame()`] is called, it will trigger a call to
    /// [`UiBuilder::build_custom_atlas`].
    pub fn invalidate_font_atlas(&mut self) {
        self.pending_atlas = true;
    }
}

impl CurrentContext<'_> {
    pub fn set_allow_user_scaling(&mut self, val: bool) {
        unsafe {
            let io = ImGui_GetIO();
            (*io).FontAllowUserScaling = val;
        }
    }
    pub fn want_capture_mouse(&self) -> bool {
        unsafe {
            let io = &*ImGui_GetIO();
            io.WantCaptureMouse
        }
    }
    pub fn want_capture_keyboard(&self) -> bool {
        unsafe {
            let io = &*ImGui_GetIO();
            io.WantCaptureKeyboard
        }
    }
    pub fn want_text_input(&self) -> bool {
        unsafe {
            let io = &*ImGui_GetIO();
            io.WantTextInput
        }
    }
    pub fn io(&self) -> &ImGuiIO {
        unsafe { &*ImGui_GetIO() }
    }
    pub fn io_mut(&mut self) -> &mut ImGuiIO {
        unsafe { &mut *ImGui_GetIO() }
    }
    // This is unsafe because you could break thing setting weird flags
    // If possible use the safe wrappers below
    pub unsafe fn add_config_flags(&mut self, flags: ConfigFlags) {
        let io = ImGui_GetIO();
        (*io).ConfigFlags |= flags.bits();
    }
    pub unsafe fn remove_config_flags(&mut self, flags: ConfigFlags) {
        let io = ImGui_GetIO();
        (*io).ConfigFlags &= !flags.bits();
    }
    pub fn nav_enable_keyboard(&mut self) {
        unsafe {
            self.add_config_flags(ConfigFlags::NavEnableKeyboard);
        }
    }
    pub fn nav_enable_gamepad(&mut self) {
        unsafe {
            self.add_config_flags(ConfigFlags::NavEnableGamepad);
        }
    }
    pub unsafe fn set_size(&mut self, size: Vector2, scale: f32) {
        let io = ImGui_GetIO();
        (*io).DisplaySize = v2_to_im(size);
        if self.scale() != scale {
            (*io).DisplayFramebufferScale = ImVec2 { x: scale, y: scale };
            (*io).FontGlobalScale = scale.recip();
            self.invalidate_font_atlas();
        }
    }
    pub unsafe fn size(&self) -> Vector2 {
        let io = ImGui_GetIO();
        im_to_v2((*io).DisplaySize)
    }
    pub unsafe fn scale(&self) -> f32 {
        let io = ImGui_GetIO();
        (*io).DisplayFramebufferScale.x
    }
    /// The next time [`CurrentContext::do_frame()`] is called, it will trigger a call to
    /// [`UiBuilder::build_custom_atlas`].
    pub fn invalidate_font_atlas(&mut self) {
        self.ctx.invalidate_font_atlas();
    }
    // I like to be explicit about this particular lifetime
    #[allow(clippy::needless_lifetimes)]
    pub unsafe fn update_atlas<'ui, A: UiBuilder>(&'ui mut self, app: &mut A) -> bool {
        if !std::mem::take(&mut self.ctx.pending_atlas) {
            return false;
        }
        let io = ImGui_GetIO();
        ImFontAtlas_Clear((*io).Fonts);
        (*(*io).Fonts).TexPixelsUseColors = true;

        let scale = (*io).DisplayFramebufferScale.x;
        let mut atlas = FontAtlasMut {
            ptr: FontAtlasPtr {
                ptr: &mut *(*io).Fonts,
            },
            scale,
            glyph_ranges: Vec::new(),
            custom_rects: Vec::new(),
        };

        app.build_custom_atlas(&mut atlas);
        // If the app did not create any font, create a default one here.
        // ImGui will do it by itself, but that would not be properly scaled if scale != 1.0
        if atlas.ptr.ptr.Fonts.is_empty() {
            atlas.add_font(FontInfo::default_font(13.0));
        }
        atlas.build_custom_rects(app);
        true
    }
    /// Builds and renders a UI frame.
    ///
    /// * `app`: `UiBuilder` to be used to build the frame.
    /// * `re_render`: function to be called after `app.do_ui` but before rendering.
    /// * `render`: function to do the actual render.
    pub unsafe fn do_frame<A: UiBuilder>(
        &mut self,
        app: &mut A,
        pre_render: impl FnOnce(),
        render: impl FnOnce(&ImDrawData),
    ) {
        let mut ui = Ui {
            data: std::ptr::null_mut(),
            generation: ImGui_GetFrameCount() as usize,
            callbacks: RefCell::new(Vec::new()),
            pending_atlas: Cell::new(false),
        };

        let io = ImGui_GetIO();
        (*io).BackendLanguageUserData = &ui as *const Ui<A> as *mut c_void;
        let _guard = UiPtrToNullGuard(self.ctx);
        ImGui_NewFrame();

        app.do_ui(&ui);

        pre_render();
        app.pre_render();

        ImGui_Render();

        ui.data = app;

        // This is the same pointer, but without it, there is something fishy about stacked borrows
        // and the mutable access to `ui` above.
        (*io).BackendLanguageUserData = &ui as *const Ui<A> as *mut c_void;

        let draw_data = ImGui_GetDrawData();
        render(&*draw_data);

        _guard.0.pending_atlas |= ui.pending_atlas.get();
    }
}

impl Drop for Context {
    fn drop(&mut self) {
        unsafe {
            ImGui_DestroyContext(self.imgui);
        }
    }
}

struct UiPtrToNullGuard<'a>(&'a mut Context);

impl Drop for UiPtrToNullGuard<'_> {
    fn drop(&mut self) {
        unsafe {
            let io = ImGui_GetIO();
            (*io).BackendLanguageUserData = null_mut();
        }
    }
}

/// The main trait that the user must implement to create a UI.
pub trait UiBuilder {
    /// This function is run the first time an ImGui context is used to create the font atlas.
    ///
    /// You can force new call by invalidating the current atlas by calling [`Context::invalidate_font_atlas`].
    fn build_custom_atlas(&mut self, _atlas: &mut FontAtlasMut<'_, Self>) {}
    /// This function is run after `do_ui` but before rendering.
    ///
    /// It can be used to clear the framebuffer.
    fn pre_render(&mut self) {}
    /// User the `ui` value to create a UI frame.
    ///
    /// This is equivalent to the Dear ImGui code between `NewFrame` and `EndFrame`.
    fn do_ui(&mut self, ui: &Ui<Self>);
}

enum TtfData {
    Bytes(Cow<'static, [u8]>),
    DefaultFont,
}

/// A font to be fed to the ImGui atlas.
pub struct FontInfo {
    ttf: TtfData,
    size: f32,
    char_ranges: Vec<[ImWchar; 2]>,
}

impl FontInfo {
    /// Creates a new `FontInfo` from a TTF content and a font size.
    pub fn new(ttf: impl Into<Cow<'static, [u8]>>, size: f32) -> FontInfo {
        FontInfo {
            ttf: TtfData::Bytes(ttf.into()),
            size,
            char_ranges: Vec::new(),
        }
    }
    /// Creates a `FontInfo` using the embedded default Dear ImGui font, with the given font size.
    ///
    /// The default size of the default font is 13.0.
    pub fn default_font(size: f32) -> FontInfo {
        FontInfo {
            ttf: TtfData::DefaultFont,
            size,
            char_ranges: Vec::new(),
        }
    }
    /// Adds the given char range to this font info.
    ///
    /// If the range list is empty, it is as if `'\u{20}'..='\u{ff}'`, that is the "ISO-8859-1"
    /// table. But if you call this function for a font, then it will not be added by default, you
    /// should add it yourself.
    pub fn add_char_range(mut self, range: std::ops::RangeInclusive<char>) -> Self {
        self.char_ranges
            .push([ImWchar::from(*range.start()), ImWchar::from(*range.end())]);
        self
    }
}

/// Represents any type that can be converted into something that can be deref'ed to a `&CStr`.
pub trait IntoCStr {
    type Temp: Deref<Target = CStr>;
    fn into(self) -> Self::Temp;
}

impl IntoCStr for &str {
    type Temp = CString;

    fn into(self) -> Self::Temp {
        CString::new(self).unwrap()
    }
}
impl IntoCStr for &String {
    type Temp = CString;

    fn into(self) -> Self::Temp {
        CString::new(self.as_str()).unwrap()
    }
}
impl IntoCStr for String {
    type Temp = CString;

    fn into(self) -> Self::Temp {
        CString::new(self).unwrap()
    }
}
impl IntoCStr for &CStr {
    type Temp = Self;
    fn into(self) -> Self {
        self
    }
}
impl IntoCStr for CString {
    type Temp = Self;

    fn into(self) -> Self {
        self
    }
}
impl<'a> IntoCStr for &'a CString {
    type Temp = &'a CStr;

    fn into(self) -> &'a CStr {
        self.as_c_str()
    }
}

// Helper functions

// Take care to not consume the argument before using the pointer
fn optional_str<S: Deref<Target = CStr>>(t: &Option<S>) -> *const c_char {
    t.as_ref().map(|s| s.as_ptr()).unwrap_or(null())
}

fn optional_mut_bool(b: &mut Option<&mut bool>) -> *mut bool {
    b.as_mut().map(|x| *x as *mut bool).unwrap_or(null_mut())
}

/// Helper function that, given a string, returns the start and end pointer.
unsafe fn text_ptrs(text: &str) -> (*const c_char, *const c_char) {
    let btxt = text.as_bytes();
    let start = btxt.as_ptr() as *const c_char;
    let end = unsafe { start.add(btxt.len()) };
    (start, end)
}

unsafe fn font_ptr(font: FontId) -> *mut ImFont {
    let io = &*ImGui_GetIO();
    let fonts = &*io.Fonts;
    // fonts.Fonts is never empty, at least there is the default font
    fonts.Fonts[font.0]
}

// this is unsafe because it replaces a C binding function that does nothing, and adding `unsafe`
// avoids a warning
unsafe fn no_op() {}

/// `Ui` represents an ImGui frame that is being built.
///
/// Usually you will get a `&mut Ui` when you are expected to build a user interface,
/// as in [`UiBuilder::do_ui`].
pub struct Ui<A>
where
    A: ?Sized,
{
    data: *mut A, // only for callbacks, after `do_ui` has finished, do not use directly
    generation: usize,
    callbacks: RefCell<Vec<UiCallback<A>>>,
    pending_atlas: Cell<bool>,
}

/// Callbacks called during `A::do_ui()` will have the first argument as null, because the app value
/// is already `self`, no need for it.
/// Callbacks called during rendering will not have access to `Ui`, because the frame is finished,
/// but they will get a proper `&mut A` as a first argument.
/// The second is a generic pointer to the real function argument, beware of fat pointers!
/// The second parameter will be consumed by the callback, take care of calling the drop exactly
/// once.
type UiCallback<A> = Box<dyn FnMut(*mut A, *mut c_void)>;

macro_rules! with_begin_end {
    ( $(#[$attr:meta])* $name:ident $begin:ident $end:ident ($($arg:ident ($($type:tt)*) ($pass:expr),)*) ) => {
        paste::paste! {
            $(#[$attr])*
            pub fn [< with_ $name >]<R>(&self, $($arg: $($type)*,)* f: impl FnOnce() -> R) -> R {
                unsafe { $begin( $( $pass, )* ) }
                let r = f();
                unsafe { $end() }
                r
            }
        }
    };
}

macro_rules! with_begin_end_opt {
    ( $(#[$attr:meta])* $name:ident $begin:ident $end:ident ($($arg:ident ($($type:tt)*) ($pass:expr),)*) ) => {
        paste::paste! {
            $(#[$attr])*
            pub fn [< with_ $name >]<R>(&self, $($arg: $($type)*,)* f: impl FnOnce() -> R) -> Option<R> {
                self.[< with_always_ $name >]($($arg,)* move |opened| { opened.then(f) })
            }
            pub fn [< with_always_ $name >]<R>(&self, $($arg: $($type)*,)* f: impl FnOnce(bool) -> R) -> R {
                if !unsafe { $begin( $( $pass, )* ) } {
                    return f(false);
                }
                let r = f(true);
                unsafe { $end() }
                r
            }
        }
    };
}

macro_rules! decl_builder {
    ( $sname:ident -> $tres:ty, $func:ident ($($life:lifetime),*) ( $( $gen_n:ident : $gen_d:tt ),* )
        (
            $(
                $arg:ident ($($ty:tt)*) ($pass:expr),
            )*
        )
        { $($extra:tt)* }
        { $($constructor:tt)* }
    ) => {
        #[must_use]
        pub struct $sname<$($life,)* $($gen_n : $gen_d, )* > {
            $(
                $arg: $($ty)*,
            )*
        }
        impl <$($life,)* $($gen_n : $gen_d, )* > $sname<$($life,)* $($gen_n, )* > {
            pub fn build(self) -> $tres {
                let $sname { $($arg, )* } = self;
                unsafe {
                    $func($($pass,)*)
                }
            }
            $($extra)*
        }

        impl<A> Ui<A> {
            $($constructor)*
        }
    };
}

macro_rules! decl_builder_setter_ex {
    ($name:ident: $ty:ty = $expr:expr) => {
        pub fn $name(mut self, $name: $ty) -> Self {
            self.$name = $expr;
            self
        }
    };
}

macro_rules! decl_builder_setter {
    ($name:ident: $ty:ty) => {
        decl_builder_setter_ex! { $name: $ty = $name.into() }
    };
}

macro_rules! decl_builder_setter_vector2 {
    ($name:ident: Vector2) => {
        decl_builder_setter_ex! { $name: Vector2 = v2_to_im($name) }
    };
}

macro_rules! decl_builder_with_maybe_opt {
    ( $always_run_end:literal
      $sname:ident, $func_beg:ident, $func_end:ident ($($life:lifetime),*) ( $( $gen_n:ident : $gen_d:tt ),* )
        (
            $(
                $arg:ident ($($ty:tt)*) ($pass:expr),
            )*
        )
        { $($extra:tt)* }
        { $($constructor:tt)* }
    ) => {
        #[must_use]
        pub struct $sname< $($life,)* $($gen_n : $gen_d, )* P: Pushable = () > {
            $(
                $arg: $($ty)*,
            )*
            push: P,
        }
        impl <$($life,)* $($gen_n : $gen_d, )* P: Pushable > $sname<$($life,)* $($gen_n,)* P > {
            /// Registers this `Pushable` to be called only for the `begin` part of this UI
            /// element.
            ///
            /// This is useful for example to modify the style of a window without changing the
            /// stily of its content.
            pub fn push_for_begin<P2: Pushable>(self, push: P2) -> $sname< $($life,)* $($gen_n,)* (P, P2) > {
                $sname {
                    $(
                        $arg: self.$arg,
                    )*
                    push: (self.push, push),
                }
            }
            /// Calls `f` inside this UI element, but only if it is visible.
            pub fn with<R>(self, f: impl FnOnce() -> R) -> Option<R> {
                self.with_always(move |opened| { opened.then(f) })
            }
            /// Calls `f` inside this UI element, passing `true` if the elements visible, `false`
            /// if it is not.
            pub fn with_always<R>(self, f: impl FnOnce(bool) -> R) -> R {
                // Some uses will require `mut`, some will not`
                #[allow(unused_mut)]
                let $sname { $(mut $arg, )* push } = self;
                let bres = unsafe {
                    let _guard = push_guard(&push);
                    $func_beg($($pass,)*)
                };
                let r = f(bres);
                unsafe {
                    if $always_run_end || bres {
                        $func_end();
                    }
                }
                r
            }
            $($extra)*
        }

        impl<A> Ui<A> {
            $($constructor)*
        }
    };
}

macro_rules! decl_builder_with {
    ($($args:tt)*) => {
        decl_builder_with_maybe_opt!{ true $($args)* }
    };
}

macro_rules! decl_builder_with_opt {
    ($($args:tt)*) => {
        decl_builder_with_maybe_opt!{ false $($args)* }
    };
}

decl_builder_with! {Child, ImGui_BeginChild, ImGui_EndChild () (S: IntoCStr)
    (
        name (S::Temp) (name.as_ptr()),
        size (ImVec2) (&size),
        child_flags (ChildFlags) (child_flags.bits()),
        window_flags (WindowFlags) (window_flags.bits()),
    )
    {
        decl_builder_setter_vector2!{size: Vector2}
        decl_builder_setter!{child_flags: ChildFlags}
        decl_builder_setter!{window_flags: WindowFlags}
    }
    {
        pub fn child_config<S: IntoCStr>(&self, name: S) -> Child<S> {
            Child {
                name: name.into(),
                size: im_vec2(0.0, 0.0),
                child_flags: ChildFlags::None,
                window_flags: WindowFlags::None,
                push: (),
            }
        }
    }
}

decl_builder_with! {Window, ImGui_Begin, ImGui_End ('v) (S: IntoCStr)
    (
        name (S::Temp) (name.as_ptr()),
        open (Option<&'v mut bool>) (optional_mut_bool(&mut open)),
        flags (WindowFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{open: &'v mut bool}
        decl_builder_setter!{flags: WindowFlags}
    }
    {
        pub fn window_config<S: IntoCStr>(&self, name: S) -> Window<S> {
            Window {
                name: name.into(),
                open: None,
                flags: WindowFlags::None,
                push: (),
            }
        }
    }
}

decl_builder! { MenuItem -> bool, ImGui_MenuItem () (S1: IntoCStr, S2: IntoCStr)
    (
        label (S1::Temp) (label.as_ptr()),
        shortcut (Option<S2::Temp>) (optional_str(&shortcut)),
        selected (bool) (selected),
        enabled (bool) (enabled),
    )
    {
        pub fn shortcut_opt<S3: IntoCStr>(self, shortcut: Option<S3>) -> MenuItem<S1, S3> {
            MenuItem {
                label: self.label,
                shortcut: shortcut.map(|s| s.into()),
                selected: self.selected,
                enabled: self.enabled,
            }
        }
        pub fn shortcut<S3: IntoCStr>(self, shortcut: S3) -> MenuItem<S1, S3> {
            self.shortcut_opt(Some(shortcut))
        }
        decl_builder_setter!{selected: bool}
        decl_builder_setter!{enabled: bool}
    }
    {
        pub fn menu_item_config<S: IntoCStr>(&self, label: S) -> MenuItem<S, &str> {
            MenuItem {
                label: label.into(),
                shortcut: None,
                selected: false,
                enabled: true,
            }
        }
    }
}

decl_builder! { Button -> bool, ImGui_Button () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        size (ImVec2) (&size),
    )
    {
        decl_builder_setter_vector2!{size: Vector2}
    }
    {
        pub fn button_config<S: IntoCStr>(&self, label: S) -> Button<S> {
            Button {
                label: label.into(),
                size: im_vec2(0.0, 0.0),
            }
        }
        pub fn button<S: IntoCStr>(&self, label: S) -> bool {
            self.button_config(label).build()
        }
    }
}

decl_builder! { SmallButton -> bool, ImGui_SmallButton () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
    )
    {}
    {
        pub fn small_button_config<S: IntoCStr>(&self, label: S) -> SmallButton<S> {
            SmallButton {
                label: label.into(),
            }
        }
        pub fn small_button<S: IntoCStr>(&self, label: S) -> bool {
            self.small_button_config(label).build()
        }
    }
}

decl_builder! { InvisibleButton -> bool, ImGui_InvisibleButton () (S: IntoCStr)
    (
        id (S::Temp) (id.as_ptr()),
        size (ImVec2) (&size),
        flags (ButtonFlags) (flags.bits()),
    )
    {
        decl_builder_setter_vector2!{size: Vector2}
        decl_builder_setter!{flags: ButtonFlags}
    }
    {
        pub fn invisible_button_config<S: IntoCStr>(&self, id: S) -> InvisibleButton<S> {
            InvisibleButton {
                id: id.into(),
                size: im_vec2(0.0, 0.0),
                flags: ButtonFlags::MouseButtonLeft,
            }
        }
    }
}

decl_builder! { ArrowButton -> bool, ImGui_ArrowButton () (S: IntoCStr)
    (
        id (S::Temp) (id.as_ptr()),
        dir (Dir) (dir.bits()),
    )
    {}
    {
        pub fn arrow_button_config<S: IntoCStr>(&self, id: S, dir: Dir) -> ArrowButton<S> {
            ArrowButton {
                id: id.into(),
                dir,
            }
        }
        pub fn arrow_button<S: IntoCStr>(&self, id: S, dir: Dir) -> bool {
            self.arrow_button_config(id, dir).build()
        }
    }
}

decl_builder! { Checkbox -> bool, ImGui_Checkbox ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        value (&'v mut bool) (value),
    )
    {}
    {
        pub fn checkbox_config<'v, S: IntoCStr>(&self, label: S, value: &'v mut bool) -> Checkbox<'v, S> {
            Checkbox {
                label: label.into(),
                value,
            }
        }
        pub fn checkbox<S: IntoCStr>(&self, label: S, value: &mut bool) -> bool {
            self.checkbox_config(label, value).build()
        }
    }
}

decl_builder! { RadioButton -> bool, ImGui_RadioButton () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        active (bool) (active),
    )
    {}
    {
        pub fn radio_button_config<S: IntoCStr>(&self, label: S, active: bool) -> RadioButton<S> {
            RadioButton {
                label: label.into(),
                active,
            }
        }
    }
}

decl_builder! { ProgressBar -> (), ImGui_ProgressBar () (S: IntoCStr)
    (
        fraction (f32) (fraction),
        size (ImVec2) (&size),
        overlay (Option<S::Temp>) (optional_str(&overlay)),
    )
    {
        decl_builder_setter_vector2!{size: Vector2}
        pub fn overlay<S2: IntoCStr>(self, overlay: S2) -> ProgressBar<S2> {
            ProgressBar {
                fraction: self.fraction,
                size: self.size,
                overlay: Some(overlay.into()),
            }
        }
    }
    {
        pub fn progress_bar_config<'a>(&self, fraction: f32) -> ProgressBar<&'a str> {
            ProgressBar {
                fraction,
                size: im_vec2(-f32::MIN_POSITIVE, 0.0),
                overlay: None,
            }
        }
    }
}

decl_builder! { Image -> (), ImGui_Image () ()
    (
        user_texture_id (TextureId) (user_texture_id.id()),
        size (ImVec2) (&size),
        uv0 (ImVec2) (&uv0),
        uv1 (ImVec2) (&uv1),
        tint_col (ImVec4) (&tint_col),
        border_col (ImVec4) (&border_col),
    )
    {
        decl_builder_setter_vector2!{uv0: Vector2}
        decl_builder_setter_vector2!{uv1: Vector2}
        decl_builder_setter!{tint_col: Color}
        decl_builder_setter!{border_col: Color}
    }
    {
        pub fn image_config(&self, user_texture_id: TextureId, size: Vector2) -> Image {
            Image {
                user_texture_id,
                size: v2_to_im(size),
                uv0: im_vec2(0.0, 0.0),
                uv1: im_vec2(1.0, 1.0),
                tint_col: Color::WHITE.into(),
                border_col: Color::TRANSPARENT.into(),
            }
        }
        pub fn image_with_custom_rect_config(&self, ridx: CustomRectIndex, scale: f32) -> Image {
            let atlas = self.font_atlas();
            let rect = atlas.get_custom_rect(ridx);
            let tex_id = atlas.texture_id();
            let tex_size = atlas.texture_size();
            let inv_tex_w = 1.0 / tex_size[0] as f32;
            let inv_tex_h = 1.0 / tex_size[1] as f32;
            let uv0 = vec2(rect.X as f32 * inv_tex_w, rect.Y as f32 * inv_tex_h);
            let uv1 = vec2((rect.X + rect.Width) as f32 * inv_tex_w, (rect.Y + rect.Height) as f32 * inv_tex_h);

            self.image_config(tex_id, vec2(scale * rect.Width as f32, scale * rect.Height as f32))
                .uv0(uv0)
                .uv1(uv1)
        }

    }
}
decl_builder! { ImageButton -> bool, ImGui_ImageButton () (S: IntoCStr)
    (
        str_id (S::Temp) (str_id.as_ptr()),
        user_texture_id (TextureId) (user_texture_id.id()),
        size (ImVec2) (&size),
        uv0 (ImVec2) (&uv0),
        uv1 (ImVec2) (&uv1),
        bg_col (ImVec4) (&bg_col),
        tint_col (ImVec4) (&tint_col),
    )
    {
        decl_builder_setter_vector2!{uv0: Vector2}
        decl_builder_setter_vector2!{uv1: Vector2}
        decl_builder_setter!{bg_col: Color}
        decl_builder_setter!{tint_col: Color}
    }
    {
        pub fn image_button_config<S: IntoCStr>(&self, str_id: S, user_texture_id: TextureId, size: Vector2) -> ImageButton<S> {
            ImageButton {
                str_id: str_id.into(),
                user_texture_id,
                size: v2_to_im(size),
                uv0: im_vec2(0.0, 0.0),
                uv1: im_vec2(1.0, 1.0),
                bg_col: Color::TRANSPARENT.into(),
                tint_col: Color::WHITE.into(),
            }
        }
        pub fn image_button_with_custom_rect_config<S: IntoCStr>(&self, str_id: S, ridx: CustomRectIndex, scale: f32) -> ImageButton<S> {
            let atlas = self.font_atlas();
            let rect = atlas.get_custom_rect(ridx);
            let tex_id = atlas.texture_id();
            let tex_size = atlas.texture_size();
            let inv_tex_w = 1.0 / tex_size[0] as f32;
            let inv_tex_h = 1.0 / tex_size[1] as f32;
            let uv0 = vec2(rect.X as f32 * inv_tex_w, rect.Y as f32 * inv_tex_h);
            let uv1 = vec2((rect.X + rect.Width) as f32 * inv_tex_w, (rect.Y + rect.Height) as f32 * inv_tex_h);

            self.image_button_config(str_id, tex_id, vec2(scale * rect.Width as f32, scale * rect.Height as f32))
                .uv0(uv0)
                .uv1(uv1)
        }
    }
}

decl_builder! { Selectable -> bool, ImGui_Selectable () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        selected (bool) (selected),
        flags (SelectableFlags) (flags.bits()),
        size (ImVec2) (&size),
    )
    {
        decl_builder_setter!{selected: bool}
        decl_builder_setter!{flags: SelectableFlags}
        decl_builder_setter_vector2!{size: Vector2}
    }
    {
        pub fn selectable_config<S: IntoCStr>(&self, label: S) -> Selectable<S> {
            Selectable {
                label: label.into(),
                selected: false,
                flags: SelectableFlags::None,
                size: im_vec2(0.0, 0.0),
            }
        }
        pub fn selectable<S: IntoCStr>(&self, label: S) -> bool {
            self.selectable_config(label).build()
        }
    }
}

macro_rules! decl_builder_drag {
    ($name:ident $func:ident $cfunc:ident $life:lifetime ($argty:ty) ($ty:ty) ($expr:expr)) => {
        decl_builder! { $name -> bool, $cfunc ($life) (S: IntoCStr)
            (
                label (S::Temp) (label.as_ptr()),
                value ($ty) ($expr(value)),
                speed (f32) (speed),
                min ($argty) (min),
                max ($argty) (max),
                format (Cow<'static, CStr>) (format.as_ptr()),
                flags (SliderFlags) (flags.bits()),
            )
            {
                decl_builder_setter!{speed: f32}
                pub fn range(mut self, min: $argty, max: $argty) -> Self {
                    self.min = min;
                    self.max = max;
                    self
                }
                decl_builder_setter!{flags: SliderFlags}
            }
            {
                pub fn $func<$life, S: IntoCStr>(&self, label: S, value: $ty) -> $name<$life, S> {
                    $name {
                        label: label.into(),
                        value,
                        speed: 1.0,
                        min: <$argty>::default(),
                        max: <$argty>::default(),
                        format: Cow::Borrowed(cstr!("%.3f")),
                        flags: SliderFlags::None,
                    }
                }
            }
        }
    };
}

macro_rules! impl_float_format {
    ($name:ident) => {
        impl_float_format! {$name "%g" "%.0f" "%.3f" "%.{}f"}
    };
    ($name:ident $g:literal $f0:literal $f3:literal $f_n:literal) => {
        impl<S: IntoCStr> $name<'_, S> {
            pub fn display_format(mut self, format: FloatFormat) -> Self {
                self.format = match format {
                    FloatFormat::G => Cow::Borrowed(cstr!($g)),
                    FloatFormat::F(0) => Cow::Borrowed(cstr!($f0)),
                    FloatFormat::F(3) => Cow::Borrowed(cstr!($f3)),
                    FloatFormat::F(n) => Cow::Owned(CString::new(format!($f_n, n)).unwrap()),
                };
                self
            }
        }
    };
}

decl_builder_drag! { DragFloat drag_float_config ImGui_DragFloat 'v (f32) (&'v mut f32) (std::convert::identity)}
decl_builder_drag! { DragFloat2 drag_float_2_config ImGui_DragFloat2 'v (f32) (&'v mut [f32; 2]) (<[f32]>::as_mut_ptr)}
decl_builder_drag! { DragFloat3 drag_float_3_config ImGui_DragFloat3 'v (f32) (&'v mut [f32; 3]) (<[f32]>::as_mut_ptr)}
decl_builder_drag! { DragFloat4 drag_float_4_config ImGui_DragFloat4 'v (f32) (&'v mut [f32; 4]) (<[f32]>::as_mut_ptr)}

impl_float_format! { DragFloat }
impl_float_format! { DragFloat2 }
impl_float_format! { DragFloat3 }
impl_float_format! { DragFloat4 }

decl_builder_drag! { DragInt drag_int_config ImGui_DragInt 'v (i32) (&'v mut i32) (std::convert::identity)}
decl_builder_drag! { DragInt2 drag_int_2_config ImGui_DragInt2 'v (i32) (&'v mut [i32; 2]) (<[i32]>::as_mut_ptr)}
decl_builder_drag! { DragInt3 drag_int_3_config ImGui_DragInt3 'v (i32) (&'v mut [i32; 3]) (<[i32]>::as_mut_ptr)}
decl_builder_drag! { DragInt4 drag_int_4_config ImGui_DragInt4 'v (i32) (&'v mut [i32; 4]) (<[i32]>::as_mut_ptr)}

macro_rules! decl_builder_slider {
    ($name:ident $func:ident $cfunc:ident $life:lifetime ($argty:ty) ($ty:ty) ($expr:expr)) => {
        decl_builder! { $name -> bool, $cfunc ($life) (S: IntoCStr)
            (
                label (S::Temp) (label.as_ptr()),
                value ($ty) ($expr(value)),
                min ($argty) (min),
                max ($argty) (max),
                format (Cow<'static, CStr>) (format.as_ptr()),
                flags (SliderFlags) (flags.bits()),
            )
            {
                pub fn range(mut self, min: $argty, max: $argty) -> Self {
                    self.min = min;
                    self.max = max;
                    self
                }
                decl_builder_setter!{flags: SliderFlags}
            }
            {
                pub fn $func<$life, S: IntoCStr>(&self, label: S, value: $ty) -> $name<$life, S> {
                    $name {
                        label: label.into(),
                        value,
                        min: <$argty>::default(),
                        max: <$argty>::default(),
                        format: Cow::Borrowed(cstr!("%.3f")),
                        flags: SliderFlags::None,
                    }
                }
            }
        }
    };
}

decl_builder_slider! { SliderFloat slider_float_config ImGui_SliderFloat 'v (f32) (&'v mut f32) (std::convert::identity)}
decl_builder_slider! { SliderFloat2 slider_float_2_config ImGui_SliderFloat2 'v (f32) (&'v mut [f32; 2]) (<[f32]>::as_mut_ptr)}
decl_builder_slider! { SliderFloat3 slider_float_3_config ImGui_SliderFloat3 'v (f32) (&'v mut [f32; 3]) (<[f32]>::as_mut_ptr)}
decl_builder_slider! { SliderFloat4 slider_float_4_config ImGui_SliderFloat4 'v (f32) (&'v mut [f32; 4]) (<[f32]>::as_mut_ptr)}

impl_float_format! { SliderFloat }
impl_float_format! { SliderFloat2 }
impl_float_format! { SliderFloat3 }
impl_float_format! { SliderFloat4 }

decl_builder_slider! { SliderInt slider_int_config ImGui_SliderInt 'v (i32) (&'v mut i32) (std::convert::identity)}
decl_builder_slider! { SliderInt2 slider_int_2_config ImGui_SliderInt2 'v (i32) (&'v mut [i32; 2]) (<[i32]>::as_mut_ptr)}
decl_builder_slider! { SliderInt3 slider_int_3_config ImGui_SliderInt3 'v (i32) (&'v mut [i32; 3]) (<[i32]>::as_mut_ptr)}
decl_builder_slider! { SliderInt4 slider_int_4_config ImGui_SliderInt4 'v (i32) (&'v mut [i32; 4]) (<[i32]>::as_mut_ptr)}

decl_builder! { SliderAngle -> bool, ImGui_SliderAngle ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        v_rad (&'v mut f32) (v_rad),
        v_degrees_min (f32) (v_degrees_min),
        v_degrees_max (f32) (v_degrees_max),
        format (Cow<'static, CStr>) (format.as_ptr()),
        flags (SliderFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{v_degrees_max: f32}
        decl_builder_setter!{v_degrees_min: f32}
        decl_builder_setter!{flags: SliderFlags}
    }
    {
        pub fn slider_angle_config<'v, S: IntoCStr>(&self, label: S, v_rad: &'v mut f32) -> SliderAngle<'v, S> {
            SliderAngle {
                label: label.into(),
                v_rad,
                v_degrees_min: -360.0,
                v_degrees_max: 360.0,
                format: Cow::Borrowed(cstr!("%.0f deg")),
                flags: SliderFlags::None,
            }
        }
    }
}

impl_float_format! { SliderAngle "%g deg" "%.0f deg" "%.3f deg" "%.{}f deg"}

decl_builder! { ColorEdit3 -> bool, ImGui_ColorEdit3 ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        color (&'v mut [f32; 3]) (color.as_mut_ptr()),
        flags (ColorEditFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: ColorEditFlags}
    }
    {
        pub fn color_edit_3_config<'v, S: IntoCStr>(&self, label: S, color: &'v mut [f32; 3]) -> ColorEdit3<'v, S> {
            ColorEdit3 {
                label: label.into(),
                color,
                flags: ColorEditFlags::None,
            }
        }
    }
}

decl_builder! { ColorEdit4 -> bool, ImGui_ColorEdit4 ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        color (&'v mut [f32; 4]) (color.as_mut_ptr()),
        flags (ColorEditFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: ColorEditFlags}
    }
    {
        pub fn color_edit_4_config<'v, S: IntoCStr>(&self, label: S, color: &'v mut Color) -> ColorEdit4<'v, S> {
            ColorEdit4 {
                label: label.into(),
                color: color.as_mut(),
                flags: ColorEditFlags::None,
            }
        }
    }
}

decl_builder! { ColorPicker3 -> bool, ImGui_ColorPicker3 ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        color (&'v mut [f32; 3]) (color.as_mut_ptr()),
        flags (ColorEditFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: ColorEditFlags}
    }
    {
        pub fn color_picker_3_config<'v, S: IntoCStr>(&self, label: S, color: &'v mut [f32; 3]) -> ColorPicker3<'v, S> {
            ColorPicker3 {
                label: label.into(),
                color,
                flags: ColorEditFlags::None,
            }
        }
    }
}

decl_builder! { ColorPicker4 -> bool, ImGui_ColorPicker4 ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        color (&'v mut [f32; 4]) (color.as_mut_ptr()),
        flags (ColorEditFlags) (flags.bits()),
        ref_col (Option<Color>) (ref_col.as_ref().map(|x| x.as_ref().as_ptr()).unwrap_or(null())),
    )
    {
        decl_builder_setter!{flags: ColorEditFlags}
        pub fn ref_color(mut self, ref_color: Color) -> Self {
            self.ref_col = Some(ref_color);
            self
        }
    }
    {
        pub fn color_picker_4_config<'v, S: IntoCStr>(&self, label: S, color: &'v mut Color) -> ColorPicker4<'v, S> {
            ColorPicker4 {
                label: label.into(),
                color: color.as_mut(),
                flags: ColorEditFlags::None,
                ref_col: None,
            }
        }
    }
}

unsafe extern "C" fn input_text_callback(data: *mut ImGuiInputTextCallbackData) -> i32 {
    let data = &mut *data;
    if data.EventFlag == InputTextFlags::CallbackResize.bits() {
        let this = &mut *(data.UserData as *mut String);
        let extra = (data.BufSize as usize).saturating_sub(this.len());
        this.reserve(extra);
        data.Buf = this.as_mut_ptr() as *mut c_char;
    }
    0
}

#[inline]
fn text_pre_edit(text: &mut String) {
    // Ensure a NUL at the end
    text.push('\0');
}

#[inline]
unsafe fn text_post_edit(text: &mut String) {
    let buf = text.as_mut_vec();
    // Look for the ending NUL that must be there, instead of memchr or iter::position, leverage the standard CStr
    let len = CStr::from_ptr(buf.as_ptr() as *const c_char)
        .to_bytes()
        .len();
    buf.set_len(len);
}

unsafe fn input_text_wrapper(
    label: *const c_char,
    text: &mut String,
    flags: InputTextFlags,
) -> bool {
    let flags = flags | InputTextFlags::CallbackResize;

    text_pre_edit(text);
    let r = ImGui_InputText(
        label,
        text.as_mut_ptr() as *mut c_char,
        text.capacity(),
        flags.bits(),
        Some(input_text_callback),
        text as *mut String as *mut c_void,
    );
    text_post_edit(text);
    r
}

decl_builder! { InputText -> bool, input_text_wrapper ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        text (&'v mut String) (text),
        flags (InputTextFlags) (flags),
    )
    {
        decl_builder_setter!{flags: InputTextFlags}
    }
    {
        pub fn input_text_config<'v, S: IntoCStr>(&self, label: S, text: &'v mut String) -> InputText<'v, S> {
            InputText {
                label:label.into(),
                text,
                flags: InputTextFlags::None,
            }
        }
    }
}

unsafe fn input_text_multiline_wrapper(
    label: *const c_char,
    text: &mut String,
    size: &ImVec2,
    flags: InputTextFlags,
) -> bool {
    let flags = flags | InputTextFlags::CallbackResize;
    text_pre_edit(text);
    let r = ImGui_InputTextMultiline(
        label,
        text.as_mut_ptr() as *mut c_char,
        text.capacity(),
        size,
        flags.bits(),
        Some(input_text_callback),
        text as *mut String as *mut c_void,
    );
    text_post_edit(text);
    r
}

decl_builder! { InputTextMultiline -> bool, input_text_multiline_wrapper ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        text (&'v mut String) (text),
        size (ImVec2) (&size),
        flags (InputTextFlags) (flags),
    )
    {
        decl_builder_setter!{flags: InputTextFlags}
        decl_builder_setter_vector2!{size: Vector2}
    }
    {
        pub fn input_text_multiline_config<'v, S: IntoCStr>(&self, label: S, text: &'v mut String) -> InputTextMultiline<'v, S> {
            InputTextMultiline {
                label:label.into(),
                text,
                flags: InputTextFlags::None,
                size: im_vec2(0.0, 0.0),
            }
        }
    }
}

unsafe fn input_text_hint_wrapper(
    label: *const c_char,
    hint: *const c_char,
    text: &mut String,
    flags: InputTextFlags,
) -> bool {
    let flags = flags | InputTextFlags::CallbackResize;
    text_pre_edit(text);
    let r = ImGui_InputTextWithHint(
        label,
        hint,
        text.as_mut_ptr() as *mut c_char,
        text.capacity(),
        flags.bits(),
        Some(input_text_callback),
        text as *mut String as *mut c_void,
    );
    text_post_edit(text);
    r
}

decl_builder! { InputTextHint -> bool, input_text_hint_wrapper ('v) (S1: IntoCStr, S2: IntoCStr)
    (
        label (S1::Temp) (label.as_ptr()),
        hint (S2::Temp) (hint.as_ptr()),
        text (&'v mut String) (text),
        flags (InputTextFlags) (flags),
    )
    {
        decl_builder_setter!{flags: InputTextFlags}
    }
    {
        pub fn input_text_hint_config<'v, S1: IntoCStr, S2: IntoCStr>(&self, label: S1, hint: S2, text: &'v mut String) -> InputTextHint<'v, S1, S2> {
            InputTextHint {
                label:label.into(),
                hint: hint.into(),
                text,
                flags: InputTextFlags::None,
            }
        }
    }
}

/// How to convert a float value to a string.
///
/// It maps to the inner ImGui `sprintf` format parameter.
pub enum FloatFormat {
    /// `F(x)` is like `sprintf("%xf")`
    F(u32),
    /// `G` is like `sprintf("%g")`
    G,
}

decl_builder! { InputFloat -> bool, ImGui_InputFloat ('v) (S: IntoCStr)
    (
        label (S::Temp)  (label.as_ptr()),
        value (&'v mut f32) (value),
        step (f32) (step),
        step_fast (f32) (step_fast),
        format (Cow<'static, CStr>) (format.as_ptr()),
        flags (InputTextFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: InputTextFlags}
        decl_builder_setter!{step: f32}
        decl_builder_setter!{step_fast: f32}
    }
    {
        pub fn input_float_config<'v, S: IntoCStr>(&self, label: S, value: &'v mut f32) -> InputFloat<'v, S> {
            InputFloat {
                label:label.into(),
                value,
                step: 0.0,
                step_fast: 0.0,
                format: Cow::Borrowed(cstr!("%.3f")),
                flags: InputTextFlags::None,
            }
        }
    }
}

decl_builder! { InputInt -> bool, ImGui_InputInt ('v) (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        value (&'v mut i32) (value),
        step (i32) (step),
        step_fast (i32) (step_fast),
        flags (InputTextFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: InputTextFlags}
        decl_builder_setter!{step: i32}
        decl_builder_setter!{step_fast: i32}
    }
    {
        pub fn input_int_config<'v, S: IntoCStr>(&self, label: S, value: &'v mut i32) -> InputInt<'v, S> {
            InputInt {
                label:label.into(),
                value,
                step: 1,
                step_fast: 100,
                flags: InputTextFlags::None,
            }
        }
    }
}

macro_rules! decl_builder_input_f {
    ($name:ident $func:ident $cfunc:ident $len:literal) => {
        decl_builder! { $name -> bool, $cfunc ('v) (S: IntoCStr)
        (
            label (S::Temp) (label.as_ptr()),
            value (&'v mut [f32; $len]) (value.as_mut_ptr()),
            format (Cow<'static, CStr>) (format.as_ptr()),
            flags (InputTextFlags) (flags.bits()),
        )
        {
            decl_builder_setter!{flags: InputTextFlags}
        }
        {
            pub fn $func<'v, S: IntoCStr>(&self, label: S, value: &'v mut [f32; $len]) -> $name<'v, S> {
                $name {
                    label:label.into(),
                    value,
                    format: Cow::Borrowed(cstr!("%.3f")),
                    flags: InputTextFlags::None,
                }
            }
        }
    }

    };
}

decl_builder_input_f! { InputFloat2 input_float_2_config ImGui_InputFloat2 2}
decl_builder_input_f! { InputFloat3 input_float_3_config ImGui_InputFloat3 3}
decl_builder_input_f! { InputFloat4 input_float_4_config ImGui_InputFloat4 4}

impl_float_format! { InputFloat }
impl_float_format! { InputFloat2 }
impl_float_format! { InputFloat3 }
impl_float_format! { InputFloat4 }

macro_rules! decl_builder_input_i {
    ($name:ident $func:ident $cfunc:ident $len:literal) => {
        decl_builder! { $name -> bool, $cfunc ('v) (S: IntoCStr)
        (
            label (S::Temp) (label.as_ptr()),
            value (&'v mut [i32; $len]) (value.as_mut_ptr()),
            flags (InputTextFlags) (flags.bits()),
        )
        {
            decl_builder_setter!{flags: InputTextFlags}
        }
        {
            pub fn $func<'v, S: IntoCStr>(&self, label: S, value: &'v mut [i32; $len]) -> $name<'v, S> {
                $name {
                    label:label.into(),
                    value,
                    flags: InputTextFlags::None,
                }
            }
        }
    }

    };
}

decl_builder_input_i! { InputInt2 input_int_2_config ImGui_InputInt2 2}
decl_builder_input_i! { InputInt3 input_int_3_config ImGui_InputInt3 3}
decl_builder_input_i! { InputInt4 input_int_4_config ImGui_InputInt4 4}

decl_builder_with_opt! {Menu, ImGui_BeginMenu, ImGui_EndMenu () (S: IntoCStr)
    (
        name (S::Temp) (name.as_ptr()),
        enabled (bool) (enabled),
    )
    {
        decl_builder_setter!{enabled: bool}
    }
    {
        pub fn menu_config<S: IntoCStr>(&self, name: S) -> Menu<S> {
            Menu {
                name: name.into(),
                enabled: true,
                push: (),
            }
        }
    }
}

decl_builder_with_opt! {CollapsingHeader, ImGui_CollapsingHeader, no_op () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        flags (TreeNodeFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: TreeNodeFlags}
    }
    {
        pub fn collapsing_header_config<S: IntoCStr>(&self, label: S) -> CollapsingHeader<S> {
            CollapsingHeader {
                label: label.into(),
                flags: TreeNodeFlags::None,
                push: (),
            }
        }
    }
}

enum LabelId<'a, S: IntoCStr, H: Hashable> {
    Label(S),
    LabelId(&'a str, H),
}

unsafe fn tree_node_ex_helper<S: IntoCStr, H: Hashable>(
    label_id: LabelId<'_, S, H>,
    flags: TreeNodeFlags,
) -> bool {
    match label_id {
        LabelId::Label(lbl) => ImGui_TreeNodeEx(lbl.into().as_ptr(), flags.bits()),
        LabelId::LabelId(lbl, id) => {
            let (start, end) = text_ptrs(lbl);
            // Warning! internal imgui API ahead, the alterative would be to call all the TreeNodeEx* functions without the Hashable generics
            ImGui_TreeNodeBehavior(id.get_id(), flags.bits(), start, end)
        }
    }
}

decl_builder_with_opt! {TreeNode, tree_node_ex_helper, ImGui_TreePop ('a) (S: IntoCStr, H: Hashable)
    (
        label (LabelId<'a, S, H>) (label),
        flags (TreeNodeFlags) (flags),
    )
    {
        decl_builder_setter!{flags: TreeNodeFlags}
    }
    {
        pub fn tree_node_config<S: IntoCStr>(&self, label: S) -> TreeNode<'static, S, usize> {
            TreeNode {
                label: LabelId::Label(label),
                flags: TreeNodeFlags::None,
                push: (),
            }
        }
        pub fn tree_node_ex_config<'a, H: Hashable>(&self, id: H, label: &'a str) -> TreeNode<'a, &'a str, H> {
            TreeNode {
                label: LabelId::LabelId(label, id),
                flags: TreeNodeFlags::None,
                push: (),
            }
        }
    }
}

decl_builder_with_opt! {Popup, ImGui_BeginPopup, ImGui_EndPopup () (S: IntoCStr)
    (
        str_id (S::Temp) (str_id.as_ptr()),
        flags (WindowFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: WindowFlags}
    }
    {
        pub fn popup_config<S: IntoCStr>(&self, str_id: S) -> Popup<S> {
            Popup {
                str_id: str_id.into(),
                flags: WindowFlags::None,
                push: (),
            }
        }
    }
}

decl_builder_with_opt! {PopupModal, ImGui_BeginPopupModal, ImGui_EndPopup () (S: IntoCStr)
    (
        name (S::Temp) (name.as_ptr()),
        opened (Option<bool>) (optional_mut_bool(&mut opened.as_mut())),
        flags (WindowFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: WindowFlags}
        pub fn close_button(mut self, close_button: bool) -> Self {
            self.opened = if close_button { Some(true) } else { None };
            self
        }
    }
    {
        pub fn popup_modal_config<S: IntoCStr>(&self, name: S) -> PopupModal<S> {
            PopupModal {
                name: name.into(),
                opened: None,
                flags: WindowFlags::None,
                push: (),
            }
        }
    }
}

macro_rules! decl_builder_popup_context {
    ($struct:ident $begin:ident $do_function:ident) => {
        decl_builder_with_opt! {$struct, $begin, ImGui_EndPopup () (S: IntoCStr)
            (
                str_id (Option<S::Temp>) (optional_str(&str_id)),
                flags (PopupFlags) (flags.bits()),
            )
            {
                decl_builder_setter!{flags: PopupFlags}
                pub fn str_id<S2: IntoCStr>(self, str_id: S2) -> $struct<S2, P> {
                    $struct {
                        str_id: Some(str_id.into()),
                        flags: self.flags,
                        push: self.push,
                    }
                }

            }
            {
                pub fn $do_function<'a>(&self) -> $struct<&'a str> {
                    $struct {
                        str_id: None,
                        flags: PopupFlags::MouseButtonRight,
                        push: (),
                    }
                }
            }
        }
    };
}

decl_builder_popup_context! {PopupContextItem ImGui_BeginPopupContextItem popup_context_item_config}
decl_builder_popup_context! {PopupContextWindow ImGui_BeginPopupContextWindow popup_context_window_config}
decl_builder_popup_context! {PopupContextVoid ImGui_BeginPopupContextVoid popup_context_void_config}

decl_builder_with_opt! {Combo, ImGui_BeginCombo, ImGui_EndCombo () (S1: IntoCStr, S2: IntoCStr)
    (
        label (S1::Temp) (label.as_ptr()),
        preview_value (Option<S2::Temp>) (optional_str(&preview_value)),
        flags (ComboFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: ComboFlags}
        pub fn preview_value_opt<S3: IntoCStr>(self, preview_value: Option<S3>) -> Combo<S1, S3> {
            Combo {
                label: self.label,
                preview_value: preview_value.map(|x| x.into()),
                flags: ComboFlags::None,
                push: (),
            }
        }
        pub fn preview_value<S3: IntoCStr>(self, preview_value: S3) -> Combo<S1, S3> {
            self.preview_value_opt(Some(preview_value))
        }
    }
    {
        pub fn combo_config<'a, S: IntoCStr>(&self, label: S) -> Combo<S, &'a str> {
            Combo {
                label: label.into(),
                preview_value: None,
                flags: ComboFlags::None,
                push: (),
            }
        }
        // Helper function for simple use cases
        pub fn combo<V: Copy + PartialEq, S2: IntoCStr>(
            &self,
            label: impl IntoCStr,
            values: impl IntoIterator<Item=V>,
            f_name: impl Fn(V) -> S2,
            current: &mut V
        ) -> bool
        {
            let mut changed = false;
            self.combo_config(label)
                .preview_value(f_name(*current))
                .with(|| {
                    for val in values {
                        if self.selectable_config(f_name(val))
                            .selected(*current == val)
                            .build()
                        {
                            *current = val;
                            changed = true;
                        }
                    }
                });
            changed
        }
    }
}

decl_builder_with_opt! {ListBox, ImGui_BeginListBox, ImGui_EndListBox () (S: IntoCStr)
    (
        label (S::Temp) (label.as_ptr()),
        size (ImVec2) (&size),
    )
    {
        decl_builder_setter_vector2!{size: Vector2}
    }
    {
        pub fn list_box_config<S: IntoCStr>(&self, label: S) -> ListBox<S> {
            ListBox {
                label: label.into(),
                size: im_vec2(0.0, 0.0),
                push: (),
            }
        }
        // Helper function for simple use cases
        pub fn list_box<V: Copy + PartialEq, S2: IntoCStr>(
            &self,
            label: impl IntoCStr,
            mut height_in_items: i32,
            values: impl IntoIterator<Item=V>,
            f_name: impl Fn(V) -> S2,
            current: &mut V
        ) -> bool
        {
            // Calculate size from "height_in_items"
            if height_in_items < 0 {
                // this should be values.len().min(7) but IntoIterator is lazy evaluated
                height_in_items = 7;
            }
            let height_in_items_f = height_in_items as f32 + 0.25;
            let height_in_pixels = self.get_text_line_height_with_spacing() * height_in_items_f + self.style().frame_padding().y * 2.0;

            let mut changed = false;
            self.list_box_config(label)
                .size(vec2(0.0, height_in_pixels.floor()))
                .with(|| {
                    for val in values {
                        if self.selectable_config(f_name(val))
                            .selected(*current == val)
                            .build()
                        {
                            *current = val;
                            changed = true;
                        }
                    }
                });
            changed
        }
    }
}

decl_builder_with_opt! {TabBar, ImGui_BeginTabBar, ImGui_EndTabBar () (S: IntoCStr)
    (
        std_id (S::Temp) (std_id.as_ptr()),
        flags (TabBarFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: TabBarFlags}
    }
    {
        pub fn tab_bar_config<S: IntoCStr>(&self, std_id: S) -> TabBar<S> {
            TabBar {
                std_id: std_id.into(),
                flags: TabBarFlags::None,
                push: (),
            }
        }
    }
}

decl_builder_with_opt! {TabItem, ImGui_BeginTabItem, ImGui_EndTabItem ('o) (S: IntoCStr)
    (
        std_id (S::Temp) (std_id.as_ptr()),
        opened (Option<&'o mut bool>) (optional_mut_bool(&mut opened)),
        flags (TabItemFlags) (flags.bits()),
    )
    {
        decl_builder_setter!{flags: TabItemFlags}
        decl_builder_setter!{opened: &'o mut bool}
    }
    {
        pub fn tab_item_config<S: IntoCStr>(&self, std_id: S) -> TabItem<S> {
            TabItem {
                std_id: std_id.into(),
                opened: None,
                flags: TabItemFlags::None,
                push: (),
            }
        }
        pub fn tab_item_button(label: impl IntoCStr, flags: TabItemFlags) -> bool {
            unsafe {
                ImGui_TabItemButton(label.into().as_ptr(), flags.bits())
            }
        }
        pub fn set_tab_item_closed(tab_or_docked_window_label: impl IntoCStr) {
            unsafe {
                ImGui_SetTabItemClosed(tab_or_docked_window_label.into().as_ptr());
            }
        }
    }
}

impl<A> Ui<A> {
    // The callback will be callable until the next call to do_frame()
    unsafe fn push_callback<X>(&self, mut cb: impl FnMut(*mut A, X) + 'static) -> usize {
        let cb = Box::new(move |data: *mut A, ptr: *mut c_void| {
            let x = ptr as *mut X;
            cb(data, unsafe { std::ptr::read(x) });
        });
        let mut callbacks = self.callbacks.borrow_mut();
        let id = callbacks.len();

        callbacks.push(cb);
        merge_generation(id, self.generation)
    }
    unsafe fn run_callback<X>(id: usize, x: X) {
        let io = &*ImGui_GetIO();
        if io.BackendLanguageUserData.is_null() {
            return;
        }
        // The lifetime of ui has been erased, but at least the types of A and X should be correct
        let ui = &*(io.BackendLanguageUserData as *const Self);
        let Some(id) = remove_generation(id, ui.generation) else {
            eprintln!("lost generation callback");
            return;
        };

        let mut callbacks = ui.callbacks.borrow_mut();
        let cb = &mut callbacks[id];
        // disable the destructor of x, it will be run inside the callback
        let mut x = MaybeUninit::new(x);
        cb(&mut *ui.data, x.as_mut_ptr() as *mut c_void);
    }
    /// The next time [`CurrentContext::do_frame()`] is called, it will trigger a call to
    /// [`UiBuilder::build_custom_atlas`].
    pub fn invalidate_font_atlas(&self) {
        self.pending_atlas.set(true);
    }

    pub fn display_size(&self) -> Vector2 {
        unsafe {
            let io = ImGui_GetIO();
            im_to_v2((*io).DisplaySize)
        }
    }
    pub fn display_scale(&self) -> f32 {
        unsafe {
            let io = ImGui_GetIO();
            (*io).DisplayFramebufferScale.x
        }
    }
    pub fn get_clipboard_text(&self) -> String {
        unsafe {
            CStr::from_ptr(ImGui_GetClipboardText())
                .to_string_lossy()
                .into_owned()
        }
    }
    pub fn set_clipboard_text(&self, text: impl IntoCStr) {
        let text = text.into();
        unsafe { ImGui_SetClipboardText(text.as_ptr()) }
    }
    pub fn set_next_window_size_constraints_callback(
        &self,
        size_min: Vector2,
        size_max: Vector2,
        mut cb: impl FnMut(SizeCallbackData<'_>) + 'static,
    ) {
        unsafe {
            // Beware! This callback is called while the `do_ui()` is still running, so the argument for the
            // first callback is null!
            let id = self.push_callback(move |_, scd| cb(scd));
            ImGui_SetNextWindowSizeConstraints(
                &v2_to_im(size_min),
                &v2_to_im(size_max),
                Some(call_size_callback::<A>),
                id as *mut c_void,
            );
        }
    }
    pub fn set_next_window_size_constraints(&self, size_min: Vector2, size_max: Vector2) {
        unsafe {
            ImGui_SetNextWindowSizeConstraints(
                &v2_to_im(size_min),
                &v2_to_im(size_max),
                None,
                null_mut(),
            );
        }
    }
    pub fn set_next_item_width(&self, item_width: f32) {
        unsafe {
            ImGui_SetNextItemWidth(item_width);
        }
    }
    pub fn set_next_item_open(&self, is_open: bool, cond: Cond) {
        unsafe {
            ImGui_SetNextItemOpen(is_open, cond.bits());
        }
    }
    pub fn set_keyboard_focus_here(offset: i32) {
        unsafe { ImGui_SetKeyboardFocusHere(offset) }
    }

    with_begin_end! {
        /// See `BeginGroup`, `EndGroup`.
        group ImGui_BeginGroup ImGui_EndGroup ()
    }
    with_begin_end! {
        /// See `BeginDisabled`, `EndDisabled`.
        disabled ImGui_BeginDisabled ImGui_EndDisabled (
            disabled (bool) (disabled),
        )
    }
    with_begin_end! {
        /// See `PushClipRect`, `PopClipRect`.
        clip_rect ImGui_PushClipRect ImGui_PopClipRect (
            clip_rect_min (Vector2) (&v2_to_im(clip_rect_min)),
            clip_rect_max (Vector2) (&v2_to_im(clip_rect_max)),
            intersect_with_current_clip_rect (bool) (intersect_with_current_clip_rect),
        )
    }

    with_begin_end_opt! {
        /// See `BeginMainMenuBar`, `EndMainMenuBar`.
        main_menu_bar ImGui_BeginMainMenuBar ImGui_EndMainMenuBar ()
    }
    with_begin_end_opt! {
        /// See `BeginMenuBar`, `EndMenuBar`.
        menu_bar ImGui_BeginMenuBar ImGui_EndMenuBar ()
    }
    with_begin_end_opt! {
        /// See `BeginTooltip`, `EndTooltip`.
        tooltip ImGui_BeginTooltip ImGui_EndTooltip ()
    }
    with_begin_end_opt! {
        /// See `BeginItemTooltip`, `EndTooltip`. There is not `EndItemTooltip`.
        item_tooltip ImGui_BeginItemTooltip ImGui_EndTooltip ()
    }

    /// Calls the `f` functions with the given `push`
    pub fn with_push<R>(&self, push: impl Pushable, f: impl FnOnce() -> R) -> R {
        unsafe {
            let _guard = push_guard(&push);
            f()
        }
    }
    pub fn show_demo_window(&self, mut show: Option<&mut bool>) {
        unsafe {
            ImGui_ShowDemoWindow(optional_mut_bool(&mut show));
        }
    }
    pub fn set_next_window_pos(&self, pos: Vector2, cond: Cond, pivot: Vector2) {
        unsafe {
            ImGui_SetNextWindowPos(&v2_to_im(pos), cond.bits(), &v2_to_im(pivot));
        }
    }
    pub fn set_next_window_size(&self, size: Vector2, cond: Cond) {
        unsafe {
            ImGui_SetNextWindowSize(&v2_to_im(size), cond.bits());
        }
    }
    pub fn set_next_window_content_size(&self, size: Vector2) {
        unsafe {
            ImGui_SetNextWindowContentSize(&v2_to_im(size));
        }
    }

    pub fn set_next_window_collapsed(&self, collapsed: bool, cond: Cond) {
        unsafe {
            ImGui_SetNextWindowCollapsed(collapsed, cond.bits());
        }
    }

    pub fn set_next_window_focus(&self) {
        unsafe {
            ImGui_SetNextWindowFocus();
        }
    }

    pub fn set_next_window_scroll(&self, scroll: Vector2) {
        unsafe {
            ImGui_SetNextWindowScroll(&v2_to_im(scroll));
        }
    }

    pub fn set_next_window_bg_alpha(&self, alpha: f32) {
        unsafe {
            ImGui_SetNextWindowBgAlpha(alpha);
        }
    }
    pub fn window_draw_list(&self) -> WindowDrawList<'_, A> {
        unsafe {
            let ptr = ImGui_GetWindowDrawList();
            WindowDrawList { ui: self, ptr }
        }
    }
    pub fn foreground_draw_list(&self) -> WindowDrawList<'_, A> {
        unsafe {
            let ptr = ImGui_GetForegroundDrawList();
            WindowDrawList { ui: self, ptr }
        }
    }
    pub fn background_draw_list(&self) -> WindowDrawList<'_, A> {
        unsafe {
            let ptr = ImGui_GetBackgroundDrawList();
            WindowDrawList { ui: self, ptr }
        }
    }
    pub fn text(&self, text: &str) {
        unsafe {
            let (start, end) = text_ptrs(text);
            ImGui_TextUnformatted(start, end);
        }
    }
    pub fn text_colored(&self, color: Color, text: impl IntoCStr) {
        let text = text.into();
        unsafe { ImGui_TextColored(&color.into(), cstr!("%s").as_ptr(), text.as_ptr()) }
    }
    pub fn text_disabled(&self, text: impl IntoCStr) {
        let text = text.into();
        unsafe { ImGui_TextDisabled(cstr!("%s").as_ptr(), text.as_ptr()) }
    }
    pub fn text_wrapped(&self, text: impl IntoCStr) {
        let text = text.into();
        unsafe { ImGui_TextWrapped(cstr!("%s").as_ptr(), text.as_ptr()) }
    }
    pub fn label_text(&self, label: impl IntoCStr, text: impl IntoCStr) {
        let label = label.into();
        let text = text.into();
        unsafe { ImGui_LabelText(label.as_ptr(), cstr!("%s").as_ptr(), text.as_ptr()) }
    }
    pub fn bullet_text(&self, text: impl IntoCStr) {
        let text = text.into();
        unsafe { ImGui_BulletText(cstr!("%s").as_ptr(), text.as_ptr()) }
    }
    pub fn bullet(&self) {
        unsafe {
            ImGui_Bullet();
        }
    }
    pub fn separator_text(&self, text: impl IntoCStr) {
        let text = text.into();
        unsafe {
            ImGui_SeparatorText(text.as_ptr());
        }
    }
    pub fn separator(&self) {
        unsafe {
            ImGui_Separator();
        }
    }

    pub fn set_item_default_focus(&self) {
        unsafe {
            ImGui_SetItemDefaultFocus();
        }
    }
    pub fn is_item_hovered(&self) -> bool {
        self.is_item_hovered_ex(HoveredFlags::None)
    }
    pub fn is_item_hovered_ex(&self, flags: HoveredFlags) -> bool {
        unsafe { ImGui_IsItemHovered(flags.bits()) }
    }
    pub fn is_item_active(&self) -> bool {
        unsafe { ImGui_IsItemActive() }
    }
    pub fn is_item_focused(&self) -> bool {
        unsafe { ImGui_IsItemFocused() }
    }
    pub fn is_item_clicked(&self, flags: MouseButton) -> bool {
        unsafe { ImGui_IsItemClicked(flags.bits()) }
    }
    pub fn is_item_visible(&self) -> bool {
        unsafe { ImGui_IsItemVisible() }
    }
    pub fn is_item_edited(&self) -> bool {
        unsafe { ImGui_IsItemEdited() }
    }
    pub fn is_item_activated(&self) -> bool {
        unsafe { ImGui_IsItemActivated() }
    }
    pub fn is_item_deactivated(&self) -> bool {
        unsafe { ImGui_IsItemDeactivated() }
    }
    pub fn is_item_deactivated_after_edit(&self) -> bool {
        unsafe { ImGui_IsItemDeactivatedAfterEdit() }
    }
    pub fn is_item_toggled_open(&self) -> bool {
        unsafe { ImGui_IsItemToggledOpen() }
    }
    pub fn is_any_item_hovered(&self) -> bool {
        unsafe { ImGui_IsAnyItemHovered() }
    }
    pub fn is_any_item_active(&self) -> bool {
        unsafe { ImGui_IsAnyItemActive() }
    }
    pub fn is_any_item_focused(&self) -> bool {
        unsafe { ImGui_IsAnyItemFocused() }
    }
    pub fn is_window_collapsed(&self) -> bool {
        unsafe { ImGui_IsWindowCollapsed() }
    }
    pub fn is_window_focused(&self, flags: FocusedFlags) -> bool {
        unsafe { ImGui_IsWindowFocused(flags.bits()) }
    }
    pub fn is_window_hovered(&self, flags: FocusedFlags) -> bool {
        unsafe { ImGui_IsWindowHovered(flags.bits()) }
    }
    pub fn get_item_id(&self) -> ImGuiID {
        unsafe { ImGui_GetItemID() }
    }
    pub fn get_id(&self, id: impl Hashable) -> ImGuiID {
        unsafe { id.get_id() }
    }
    pub fn get_item_rect_min(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetItemRectMin()) }
    }
    pub fn get_item_rect_max(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetItemRectMax()) }
    }
    pub fn get_item_rect_size(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetItemRectSize()) }
    }
    pub fn get_main_viewport(&self) -> Viewport<'_> {
        unsafe {
            Viewport {
                ptr: &*ImGui_GetMainViewport(),
            }
        }
    }
    pub fn get_content_region_avail(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetContentRegionAvail()) }
    }
    pub fn get_content_region_max(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetContentRegionMax()) }
    }
    pub fn get_window_content_region_min(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetWindowContentRegionMin()) }
    }
    pub fn get_window_content_region_max(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetWindowContentRegionMax()) }
    }
    pub fn get_window_pos(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetWindowPos()) }
    }
    pub fn get_window_width(&self) -> f32 {
        unsafe { ImGui_GetWindowWidth() }
    }
    pub fn get_window_height(&self) -> f32 {
        unsafe { ImGui_GetWindowHeight() }
    }
    pub fn get_scroll_x(&self) -> f32 {
        unsafe { ImGui_GetScrollX() }
    }
    pub fn get_scroll_y(&self) -> f32 {
        unsafe { ImGui_GetScrollY() }
    }
    pub fn set_scroll_x(&self, scroll_x: f32) {
        unsafe {
            ImGui_SetScrollX(scroll_x);
        }
    }
    pub fn set_scroll_y(&self, scroll_y: f32) {
        unsafe {
            ImGui_SetScrollY(scroll_y);
        }
    }
    pub fn get_scroll_max_x(&self) -> f32 {
        unsafe { ImGui_GetScrollMaxX() }
    }
    pub fn get_scroll_max_y(&self) -> f32 {
        unsafe { ImGui_GetScrollMaxY() }
    }
    pub fn set_scroll_here_x(&self, center_x_ratio: f32) {
        unsafe {
            ImGui_SetScrollHereX(center_x_ratio);
        }
    }
    pub fn set_scroll_here_y(&self, center_y_ratio: f32) {
        unsafe {
            ImGui_SetScrollHereY(center_y_ratio);
        }
    }
    pub fn set_scroll_from_pos_x(&self, local_x: f32, center_x_ratio: f32) {
        unsafe {
            ImGui_SetScrollFromPosX(local_x, center_x_ratio);
        }
    }
    pub fn set_scroll_from_pos_y(&self, local_y: f32, center_y_ratio: f32) {
        unsafe {
            ImGui_SetScrollFromPosY(local_y, center_y_ratio);
        }
    }
    pub fn set_window_pos(&self, pos: Vector2, cond: Cond) {
        unsafe {
            ImGui_SetWindowPos(&v2_to_im(pos), cond.bits());
        }
    }
    pub fn set_window_size(&self, size: Vector2, cond: Cond) {
        unsafe {
            ImGui_SetWindowSize(&v2_to_im(size), cond.bits());
        }
    }
    pub fn set_window_collapsed(&self, collapsed: bool, cond: Cond) {
        unsafe {
            ImGui_SetWindowCollapsed(collapsed, cond.bits());
        }
    }
    pub fn set_window_focus(&self) {
        unsafe {
            ImGui_SetWindowFocus();
        }
    }
    pub fn same_line(&self) {
        unsafe {
            ImGui_SameLine(0.0, -1.0);
        }
    }
    pub fn same_line_ex(&self, offset_from_start_x: f32, spacing: f32) {
        unsafe {
            ImGui_SameLine(offset_from_start_x, spacing);
        }
    }
    pub fn new_line(&self) {
        unsafe {
            ImGui_NewLine();
        }
    }
    pub fn spacing(&self) {
        unsafe {
            ImGui_Spacing();
        }
    }
    pub fn dummy(&self, size: Vector2) {
        unsafe {
            ImGui_Dummy(&v2_to_im(size));
        }
    }
    pub fn indent(&self, indent_w: f32) {
        unsafe {
            ImGui_Indent(indent_w);
        }
    }
    pub fn unindent(&self, indent_w: f32) {
        unsafe {
            ImGui_Unindent(indent_w);
        }
    }
    pub fn get_cursor_pos(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetCursorPos()) }
    }
    pub fn get_cursor_pos_x(&self) -> f32 {
        unsafe { ImGui_GetCursorPosX() }
    }
    pub fn get_cursor_pos_y(&self) -> f32 {
        unsafe { ImGui_GetCursorPosY() }
    }
    pub fn set_cursor_pos(&self, local_pos: Vector2) {
        unsafe {
            ImGui_SetCursorPos(&v2_to_im(local_pos));
        }
    }
    pub fn set_cursor_pos_x(&self, local_x: f32) {
        unsafe {
            ImGui_SetCursorPosX(local_x);
        }
    }
    pub fn set_cursor_pos_y(&self, local_y: f32) {
        unsafe {
            ImGui_SetCursorPosY(local_y);
        }
    }
    pub fn get_cursor_start_pos(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetCursorStartPos()) }
    }
    pub fn get_cursor_screen_pos(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetCursorScreenPos()) }
    }
    pub fn set_cursor_screen_pos(&self, pos: Vector2) {
        unsafe {
            ImGui_SetCursorScreenPos(&v2_to_im(pos));
        }
    }
    pub fn align_text_to_frame_padding(&self) {
        unsafe {
            ImGui_AlignTextToFramePadding();
        }
    }
    pub fn get_text_line_height(&self) -> f32 {
        unsafe { ImGui_GetTextLineHeight() }
    }
    pub fn get_text_line_height_with_spacing(&self) -> f32 {
        unsafe { ImGui_GetTextLineHeightWithSpacing() }
    }
    pub fn get_frame_height(&self) -> f32 {
        unsafe { ImGui_GetFrameHeight() }
    }
    pub fn get_frame_height_with_spacing(&self) -> f32 {
        unsafe { ImGui_GetFrameHeightWithSpacing() }
    }
    pub fn calc_item_width(&self) -> f32 {
        unsafe { ImGui_CalcItemWidth() }
    }
    pub fn calc_text_size(&self, text: &str) -> Vector2 {
        self.calc_text_size_ex(text, false, -1.0)
    }
    pub fn calc_text_size_ex(
        &self,
        text: &str,
        hide_text_after_double_hash: bool,
        wrap_width: f32,
    ) -> Vector2 {
        unsafe {
            let (start, end) = text_ptrs(text);
            im_to_v2(ImGui_CalcTextSize(
                start,
                end,
                hide_text_after_double_hash,
                wrap_width,
            ))
        }
    }
    pub fn set_color_edit_options(&self, flags: ColorEditFlags) {
        unsafe {
            ImGui_SetColorEditOptions(flags.bits());
        }
    }
    pub fn is_key_down(&self, key: Key) -> bool {
        unsafe { ImGui_IsKeyDown(ImGuiKey(key.bits())) }
    }
    pub fn is_key_pressed(&self, key: Key) -> bool {
        unsafe {
            ImGui_IsKeyPressed(ImGuiKey(key.bits()), /*repeat*/ true)
        }
    }
    pub fn is_key_pressed_no_repeat(&self, key: Key) -> bool {
        unsafe {
            ImGui_IsKeyPressed(ImGuiKey(key.bits()), /*repeat*/ false)
        }
    }
    pub fn is_key_released(&self, key: Key) -> bool {
        unsafe { ImGui_IsKeyReleased(ImGuiKey(key.bits())) }
    }
    pub fn get_key_pressed_amount(&self, key: Key, repeat_delay: f32, rate: f32) -> i32 {
        unsafe { ImGui_GetKeyPressedAmount(ImGuiKey(key.bits()), repeat_delay, rate) }
    }
    pub fn get_font_tex_uv_white_pixel(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetFontTexUvWhitePixel()) }
    }
    //GetKeyName
    //SetNextFrameWantCaptureKeyboard
    pub fn get_font_size(&self) -> f32 {
        unsafe { ImGui_GetFontSize() }
    }
    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
        unsafe { ImGui_IsMouseDown(button.bits()) }
    }
    pub fn is_mouse_clicked(&self, button: MouseButton) -> bool {
        unsafe {
            ImGui_IsMouseClicked(button.bits(), /*repeat*/ false)
        }
    }
    pub fn is_mouse_clicked_repeat(&self, button: MouseButton) -> bool {
        unsafe {
            ImGui_IsMouseClicked(button.bits(), /*repeat*/ true)
        }
    }
    pub fn is_mouse_released(&self, button: MouseButton) -> bool {
        unsafe { ImGui_IsMouseReleased(button.bits()) }
    }
    pub fn is_mouse_double_clicked(&self, button: MouseButton) -> bool {
        unsafe { ImGui_IsMouseDoubleClicked(button.bits()) }
    }
    pub fn get_mouse_clicked_count(&self, button: MouseButton) -> i32 {
        unsafe { ImGui_GetMouseClickedCount(button.bits()) }
    }
    pub fn is_rect_visible_size(&self, size: Vector2) -> bool {
        unsafe { ImGui_IsRectVisible(&v2_to_im(size)) }
    }
    pub fn is_rect_visible(&self, rect_min: Vector2, rect_max: Vector2) -> bool {
        unsafe { ImGui_IsRectVisible1(&v2_to_im(rect_min), &v2_to_im(rect_max)) }
    }
    /*
    pub fn is_mouse_hovering_rect(&self) -> bool {
        unsafe {
            ImGui_IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);
        }
    }
    pub fn is_mouse_pos_valid(&self) -> bool {
        unsafe {
            ImGui_IsMousePosValid(const ImVec2* mouse_pos = NULL);
        }
    }*/
    pub fn is_any_mouse_down(&self) -> bool {
        unsafe { ImGui_IsAnyMouseDown() }
    }
    pub fn get_mouse_pos(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetMousePos()) }
    }
    pub fn get_mouse_pos_on_opening_current_popup(&self) -> Vector2 {
        unsafe { im_to_v2(ImGui_GetMousePosOnOpeningCurrentPopup()) }
    }
    pub fn is_mouse_dragging(&self, button: MouseButton) -> bool {
        unsafe {
            ImGui_IsMouseDragging(button.bits(), /*lock_threshold*/ -1.0)
        }
    }
    pub fn get_mouse_drag_delta(&self, button: MouseButton) -> Vector2 {
        unsafe {
            im_to_v2(ImGui_GetMouseDragDelta(
                button.bits(),
                /*lock_threshold*/ -1.0,
            ))
        }
    }
    pub fn reset_mouse_drag_delta(&self, button: MouseButton) {
        unsafe {
            ImGui_ResetMouseDragDelta(button.bits());
        }
    }
    pub fn get_mouse_cursor(&self) -> MouseCursor {
        unsafe { MouseCursor::from_bits(ImGui_GetMouseCursor()).unwrap_or(MouseCursor::None) }
    }
    pub fn set_mouse_cursor(&self, cursor_type: MouseCursor) {
        unsafe {
            ImGui_SetMouseCursor(cursor_type.bits());
        }
    }
    pub fn get_time(&self) -> f64 {
        unsafe { ImGui_GetTime() }
    }
    pub fn get_frame_count(&self) -> i32 {
        unsafe { ImGui_GetFrameCount() }
    }
    pub fn is_popup_open(&self, str_id: Option<&str>) -> bool {
        self.is_popup_open_ex(str_id, PopupFlags::None)
    }
    pub fn is_popup_open_ex(&self, str_id: Option<&str>, flags: PopupFlags) -> bool {
        let temp;
        let str_id = match str_id {
            Some(s) => {
                temp = IntoCStr::into(s);
                temp.as_ptr()
            }
            None => null(),
        };
        unsafe { ImGui_IsPopupOpen(str_id, flags.bits()) }
    }
    pub fn open_popup(&self, str_id: impl IntoCStr) {
        self.open_popup_ex(str_id, PopupFlags::None)
    }
    pub fn open_popup_ex(&self, str_id: impl IntoCStr, flags: PopupFlags) {
        let str_id = str_id.into();
        unsafe {
            ImGui_OpenPopup(str_id.as_ptr(), flags.bits());
        }
    }
    pub fn close_current_popup(&self) {
        unsafe {
            ImGui_CloseCurrentPopup();
        }
    }
    pub fn is_window_appearing(&self) -> bool {
        unsafe { ImGui_IsWindowAppearing() }
    }

    pub fn io(&self) -> &ImGuiIO {
        unsafe { &*ImGui_GetIO() }
    }
    pub fn font_atlas(&self) -> FontAtlas<'_> {
        unsafe {
            let io = &*ImGui_GetIO();
            FontAtlas {
                ptr: FontAtlasPtr {
                    ptr: &mut *io.Fonts,
                },
            }
        }
    }

    pub fn with_always_drag_drop_source<R>(
        &self,
        flags: DragDropSourceFlags,
        f: impl FnOnce(Option<DragDropPayloadSetter<'_>>) -> R,
    ) -> R {
        if !unsafe { ImGui_BeginDragDropSource(flags.bits()) } {
            return f(None);
        }
        let payload = DragDropPayloadSetter {
            _dummy: PhantomData,
        };
        let r = f(Some(payload));
        unsafe { ImGui_EndDragDropSource() }
        r
    }
    pub fn with_drag_drop_source<R>(
        &self,
        flags: DragDropSourceFlags,
        f: impl FnOnce(DragDropPayloadSetter<'_>) -> R,
    ) -> Option<R> {
        self.with_always_drag_drop_source(flags, move |r| r.map(f))
    }
    pub fn with_always_drag_drop_target<R>(
        &self,
        f: impl FnOnce(Option<DragDropPayloadGetter<'_>>) -> R,
    ) -> R {
        if !unsafe { ImGui_BeginDragDropTarget() } {
            return f(None);
        }
        let payload = DragDropPayloadGetter {
            _dummy: PhantomData,
        };
        let r = f(Some(payload));
        unsafe { ImGui_EndDragDropTarget() }
        r
    }
    pub fn with_drag_drop_target<R>(
        &self,
        f: impl FnOnce(DragDropPayloadGetter<'_>) -> R,
    ) -> Option<R> {
        self.with_always_drag_drop_target(move |r| r.map(f))
    }

    pub fn with_list_clipper(
        &self,
        items_count: usize,
        items_height: f32,
        included_ranges: &[std::ops::Range<usize>],
        mut f: impl FnMut(usize),
    ) {
        unsafe {
            let mut clip = ImGuiListClipper::new();
            clip.Begin(items_count as i32, items_height);
            for r in included_ranges {
                clip.IncludeItemsByIndex(r.start as i32, r.end as i32);
            }
            while clip.Step() {
                for i in clip.DisplayStart..clip.DisplayEnd {
                    f(i as usize);
                }
            }
        }
    }

    /// Gets information about a glyph for a font.
    ///
    /// This is a member of `Ui` instead of `FontAtlas` because it requires the atlas to be fully
    /// built, and in `build_custom_atlas` it is not yet. The only way to ensure a fully built
    /// atlas is by requiring a `&Ui`.
    pub fn find_glyph(&self, font_id: FontId, c: char) -> FontGlyph<'_> {
        unsafe {
            let font = font_ptr(font_id);
            FontGlyph(&*ImFont_FindGlyph(font, ImWchar::from(c)))
        }
    }
    /// Just like `find_glyph` but doesn't use the fallback character for unavailable glyphs.
    pub fn find_glyph_no_fallback(&self, font_id: FontId, c: char) -> Option<FontGlyph<'_>> {
        unsafe {
            let font = font_ptr(font_id);
            let p = ImFont_FindGlyphNoFallback(font, ImWchar::from(c));
            p.as_ref().map(FontGlyph)
        }
    }
    /// Gets the font details for a `FontId`.
    ///
    /// TODO: do a proper ImFont wrapper?
    pub fn get_font(&self, font_id: FontId) -> &ImFont {
        unsafe {
            let font = font_ptr(font_id);
            &*font
        }
    }
}

pub struct FontGlyph<'a>(&'a ImFontGlyph);

impl FontGlyph<'_> {
    pub fn p0(&self) -> Vector2 {
        Vector2::new(self.0.X0, self.0.Y0)
    }
    pub fn p1(&self) -> Vector2 {
        Vector2::new(self.0.X1, self.0.Y1)
    }
    pub fn uv0(&self) -> Vector2 {
        Vector2::new(self.0.U0, self.0.V0)
    }
    pub fn uv1(&self) -> Vector2 {
        Vector2::new(self.0.U1, self.0.V1)
    }
    pub fn advance_x(&self) -> f32 {
        self.0.AdvanceX
    }
    pub fn visible(&self) -> bool {
        self.0.Visible() != 0
    }
    pub fn colored(&self) -> bool {
        self.0.Colored() != 0
    }
    pub fn codepoint(&self) -> char {
        char::try_from(self.0.Codepoint()).unwrap()
    }
}

impl std::fmt::Debug for FontGlyph<'_> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_struct("FontGlyph")
            .field("p0", &self.p0())
            .field("p1", &self.p1())
            .field("uv0", &self.uv0())
            .field("uv1", &self.uv1())
            .field("advance_x", &self.advance_x())
            .field("visible", &self.visible())
            .field("colored", &self.colored())
            .field("codepoint", &self.codepoint())
            .finish()
    }
}

#[cfg(feature = "docking")]
impl<A> Ui<A> {
    pub fn dock_space(
        &self,
        id: ImGuiID,
        size: Vector2,
        flags: DockNodeFlags, /*window_class: &WindowClass*/
    ) -> ImGuiID {
        unsafe { ImGui_DockSpace(id, &v2_to_im(size), flags.bits(), std::ptr::null()) }
    }
    pub fn dock_space_over_viewport(
        &self,
        flags: DockNodeFlags, /*window_class: &WindowClass*/
    ) -> ImGuiID {
        unsafe { ImGui_DockSpaceOverViewport(std::ptr::null(), flags.bits(), std::ptr::null()) }
    }
    pub fn set_next_window_dock_id(&self, dock_id: ImGuiID, cond: Cond) {
        unsafe {
            ImGui_SetNextWindowDockID(dock_id, cond.bits());
        }
    }
    //SetNextWindowClass(const ImGuiWindowClass* window_class)
    pub fn get_window_doc_id(&self) -> ImGuiID {
        unsafe { ImGui_GetWindowDockID() }
    }
    pub fn is_window_docked(&self) -> bool {
        unsafe { ImGui_IsWindowDocked() }
    }
}

/// Identifier of a registered font. Only the values obtained from the latest call to [`UiBuilder::build_custom_atlas`] are actually valid.
///
/// `FontId::default()` wil be the default font.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FontId(usize);

/// Identifier for a registered custom rectangle. Only the values obtained from the latest call to
/// [`UiBuilder::build_custom_atlas`] are actually valid.
///
/// The `CustomRectIndex::default()` is provided as a convenience, but it is always invalid, and
/// will panic if used.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CustomRectIndex(i32);

impl Default for CustomRectIndex {
    fn default() -> Self {
        // Always invalid, do not use!
        CustomRectIndex(-1)
    }
}

#[derive(Debug)]
pub struct FontAtlasPtr<'ui> {
    ptr: &'ui mut ImFontAtlas,
}

type PixelImage<'a> = image::ImageBuffer<image::Rgba<u8>, &'a mut [u8]>;
type SubPixelImage<'a, 'b> = image::SubImage<&'a mut PixelImage<'b>>;

type FuncCustomRect<A> = Box<dyn FnOnce(&mut A, &mut SubPixelImage<'_, '_>)>;

pub struct FontAtlasMut<'ui, A: ?Sized> {
    ptr: FontAtlasPtr<'ui>,
    scale: f32,
    // glyph_ranges pointers have to live until the atlas texture is built
    glyph_ranges: Vec<Vec<[ImWchar; 2]>>,
    custom_rects: Vec<Option<FuncCustomRect<A>>>,
}

/// A reference to the font altas that is to be built.
///
/// You get a value of this type when implementing [`UiBuilder::build_custom_atlas`]. If you want
/// that function to be called again, call [`Context::invalidate_font_atlas`].
impl<'ui, A> FontAtlasMut<'ui, A> {
    /// Adds the given font to the atlas.
    ///
    /// It returns the id to use this font. `FontId` implements `Pushable` so you can use it with
    /// [`Ui::with_push`].
    pub fn add_font(&mut self, font: FontInfo) -> FontId {
        self.add_font_priv(font, false)
    }
    /// Adds several fonts with as a single ImGui font.
    ///
    /// This is useful mainly if different TTF files have different charset coverage but you want
    /// to use them all as a unit.
    pub fn add_font_collection(&mut self, fonts: impl IntoIterator<Item = FontInfo>) -> FontId {
        let mut fonts = fonts.into_iter();
        let first = fonts.next().expect("empty font collection");
        let id = self.add_font_priv(first, false);
        for font in fonts {
            self.add_font_priv(font, true);
        }
        id
    }
    fn add_font_priv(&mut self, mut font: FontInfo, merge: bool) -> FontId {
        unsafe {
            let mut fc = ImFontConfig::new();
            // This is ours, do not free()
            fc.FontDataOwnedByAtlas = false;

            fc.MergeMode = merge;

            // glyph_ranges must be valid for the duration of the atlas, so do not modify the existing self.fonts.
            // You can add new fonts however, but they will not show unless you call update_altas() again
            let glyph_ranges = if font.char_ranges.is_empty() {
                null()
            } else {
                // keep the ptr alive
                let mut char_ranges = std::mem::take(&mut font.char_ranges);
                char_ranges.push([0, 0]); // add the marking NULs
                let ptr = char_ranges[0].as_ptr();
                self.glyph_ranges.push(char_ranges);
                ptr
            };
            let io = ImGui_GetIO();
            match font.ttf {
                TtfData::Bytes(bytes) => {
                    ImFontAtlas_AddFontFromMemoryTTF(
                        (*io).Fonts,
                        bytes.as_ptr() as *mut _,
                        bytes.len() as i32,
                        font.size * self.scale,
                        &fc,
                        glyph_ranges,
                    );
                }
                TtfData::DefaultFont => {
                    fc.SizePixels = font.size * self.scale;
                    ImFontAtlas_AddFontDefault((*io).Fonts, &fc);
                }
            }
            FontId((*(*io).Fonts).Fonts.len() - 1)
        }
    }
    /// Adds an image as a substitution for a character in a font.
    pub fn add_custom_rect_font_glyph(
        &mut self,
        font: FontId,
        id: char,
        size: impl Into<mint::Vector2<u32>>,
        advance_x: f32,
        offset: Vector2,
        draw: impl FnOnce(&mut A, &mut SubPixelImage<'_, '_>) + 'static,
    ) -> CustomRectIndex {
        let size = size.into();
        unsafe {
            let io = ImGui_GetIO();

            let font = font_ptr(font);
            let idx = ImFontAtlas_AddCustomRectFontGlyph(
                (*io).Fonts,
                font,
                id as ImWchar,
                i32::try_from(size.x).unwrap(),
                i32::try_from(size.y).unwrap(),
                advance_x,
                &v2_to_im(offset),
            );
            self.add_custom_rect_at(idx as usize, Box::new(draw));
            CustomRectIndex(idx)
        }
    }
    /// Adds an arbitrary image to the font atlas.
    ///
    /// The returned `CustomRectIndex` can be used later to draw the image.
    pub fn add_custom_rect_regular(
        &mut self,
        size: impl Into<mint::Vector2<u32>>,
        draw: impl FnOnce(&mut A, &mut SubPixelImage<'_, '_>) + 'static,
    ) -> CustomRectIndex {
        let size = size.into();
        unsafe {
            let io = ImGui_GetIO();

            let idx = ImFontAtlas_AddCustomRectRegular(
                (*io).Fonts,
                i32::try_from(size.x).unwrap(),
                i32::try_from(size.y).unwrap(),
            );
            self.add_custom_rect_at(idx as usize, Box::new(draw));
            CustomRectIndex(idx)
        }
    }
    fn add_custom_rect_at(&mut self, idx: usize, f: FuncCustomRect<A>) {
        if idx >= self.custom_rects.len() {
            self.custom_rects.resize_with(idx + 1, || None);
        }
        self.custom_rects[idx] = Some(f);
    }
    unsafe fn build_custom_rects(self, app: &mut A) {
        let mut tex_data = std::ptr::null_mut();
        let mut tex_width = 0;
        let mut tex_height = 0;
        let mut pixel_size = 0;
        let io;
        unsafe {
            io = ImGui_GetIO();
            ImFontAtlas_GetTexDataAsRGBA32(
                (*io).Fonts,
                &mut tex_data,
                &mut tex_width,
                &mut tex_height,
                &mut pixel_size,
            );
        }
        let pixel_size = pixel_size as usize;
        assert!(pixel_size == 4);
        let tex_data = unsafe {
            std::slice::from_raw_parts_mut(
                tex_data,
                tex_width as usize * tex_height as usize * pixel_size,
            )
        };
        let mut pixel_image =
            PixelImage::from_raw(tex_width as u32, tex_height as u32, tex_data).unwrap();

        for (idx, f) in self.custom_rects.into_iter().enumerate() {
            if let Some(f) = f {
                unsafe {
                    let rect = &(*(*io).Fonts).CustomRects[idx];
                    let mut sub_image = pixel_image.sub_image(
                        rect.X as u32,
                        rect.Y as u32,
                        rect.Width as u32,
                        rect.Height as u32,
                    );
                    f(app, &mut sub_image);
                }
            }
        }
    }
}

impl<'ui, A> Deref for FontAtlasMut<'ui, A> {
    type Target = FontAtlasPtr<'ui>;
    fn deref(&self) -> &FontAtlasPtr<'ui> {
        &self.ptr
    }
}

pub struct FontAtlas<'ui> {
    ptr: FontAtlasPtr<'ui>,
}

impl<'ui> Deref for FontAtlas<'ui> {
    type Target = FontAtlasPtr<'ui>;
    fn deref(&self) -> &FontAtlasPtr<'ui> {
        &self.ptr
    }
}

impl FontAtlasPtr<'_> {
    pub fn texture_id(&self) -> TextureId {
        unsafe { TextureId::from_id(self.ptr.TexID) }
    }
    pub fn texture_size(&self) -> [i32; 2] {
        [self.ptr.TexWidth, self.ptr.TexHeight]
    }
    pub fn get_custom_rect(&self, index: CustomRectIndex) -> ImFontAtlasCustomRect {
        self.ptr.CustomRects[index.0 as usize]
    }
}

#[derive(Debug)]
pub struct SizeCallbackData<'a> {
    ptr: &'a mut ImGuiSizeCallbackData,
}

impl SizeCallbackData<'_> {
    pub fn pos(&self) -> Vector2 {
        im_to_v2(self.ptr.Pos)
    }
    pub fn current_size(&self) -> Vector2 {
        im_to_v2(self.ptr.CurrentSize)
    }
    pub fn desired_size(&self) -> Vector2 {
        im_to_v2(self.ptr.DesiredSize)
    }
    pub fn set_desired_size(&mut self, sz: Vector2) {
        self.ptr.DesiredSize = v2_to_im(sz);
    }
}

unsafe extern "C" fn call_size_callback<A>(ptr: *mut ImGuiSizeCallbackData) {
    let ptr = &mut *ptr;
    let id = ptr.UserData as usize;
    let data = SizeCallbackData { ptr };
    Ui::<A>::run_callback(id, data);
}

pub struct WindowDrawList<'ui, A> {
    ui: &'ui Ui<A>,
    ptr: *mut ImDrawList,
}

impl<'ui, A> WindowDrawList<'ui, A> {
    pub fn add_line(&self, p1: Vector2, p2: Vector2, color: Color, thickness: f32) {
        unsafe {
            ImDrawList_AddLine(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                color.as_u32(),
                thickness,
            );
        }
    }
    pub fn add_rect(
        &self,
        p_min: Vector2,
        p_max: Vector2,
        color: Color,
        rounding: f32,
        flags: DrawFlags,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddRect(
                self.ptr,
                &v2_to_im(p_min),
                &v2_to_im(p_max),
                color.as_u32(),
                rounding,
                flags.bits(),
                thickness,
            );
        }
    }
    pub fn add_rect_filled(
        &self,
        p_min: Vector2,
        p_max: Vector2,
        color: Color,
        rounding: f32,
        flags: DrawFlags,
    ) {
        unsafe {
            ImDrawList_AddRectFilled(
                self.ptr,
                &v2_to_im(p_min),
                &v2_to_im(p_max),
                color.as_u32(),
                rounding,
                flags.bits(),
            );
        }
    }
    pub fn add_rect_filled_multicolor(
        &self,
        p_min: Vector2,
        p_max: Vector2,
        col_upr_left: Color,
        col_upr_right: Color,
        col_bot_right: Color,
        col_bot_left: Color,
    ) {
        unsafe {
            ImDrawList_AddRectFilledMultiColor(
                self.ptr,
                &v2_to_im(p_min),
                &v2_to_im(p_max),
                col_upr_left.as_u32(),
                col_upr_right.as_u32(),
                col_bot_right.as_u32(),
                col_bot_left.as_u32(),
            );
        }
    }
    pub fn add_quad(
        &self,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        p4: Vector2,
        color: Color,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddQuad(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                &v2_to_im(p4),
                color.as_u32(),
                thickness,
            );
        }
    }
    pub fn add_quad_filled(
        &self,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        p4: Vector2,
        color: Color,
    ) {
        unsafe {
            ImDrawList_AddQuadFilled(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                &v2_to_im(p4),
                color.as_u32(),
            );
        }
    }
    pub fn add_triangle(
        &self,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        color: Color,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddTriangle(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                color.as_u32(),
                thickness,
            );
        }
    }
    pub fn add_triangle_filled(&self, p1: Vector2, p2: Vector2, p3: Vector2, color: Color) {
        unsafe {
            ImDrawList_AddTriangleFilled(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                color.as_u32(),
            );
        }
    }
    pub fn add_circle(
        &self,
        center: Vector2,
        radius: f32,
        color: Color,
        num_segments: i32,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddCircle(
                self.ptr,
                &v2_to_im(center),
                radius,
                color.as_u32(),
                num_segments,
                thickness,
            );
        }
    }
    pub fn add_circle_filled(&self, center: Vector2, radius: f32, color: Color, num_segments: i32) {
        unsafe {
            ImDrawList_AddCircleFilled(
                self.ptr,
                &v2_to_im(center),
                radius,
                color.as_u32(),
                num_segments,
            );
        }
    }
    pub fn add_ngon(
        &self,
        center: Vector2,
        radius: f32,
        color: Color,
        num_segments: i32,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddNgon(
                self.ptr,
                &v2_to_im(center),
                radius,
                color.as_u32(),
                num_segments,
                thickness,
            );
        }
    }
    pub fn add_ngon_filled(&self, center: Vector2, radius: f32, color: Color, num_segments: i32) {
        unsafe {
            ImDrawList_AddNgonFilled(
                self.ptr,
                &v2_to_im(center),
                radius,
                color.as_u32(),
                num_segments,
            );
        }
    }
    pub fn add_ellipse(
        &self,
        center: Vector2,
        radius: Vector2,
        color: Color,
        rot: f32,
        num_segments: i32,
        thickness: f32,
    ) {
        unsafe {
            ImDrawList_AddEllipse(
                self.ptr,
                &v2_to_im(center),
                &v2_to_im(radius),
                color.as_u32(),
                rot,
                num_segments,
                thickness,
            );
        }
    }
    pub fn add_ellipse_filled(
        &self,
        center: Vector2,
        radius: Vector2,
        color: Color,
        rot: f32,
        num_segments: i32,
    ) {
        unsafe {
            ImDrawList_AddEllipseFilled(
                self.ptr,
                &v2_to_im(center),
                &v2_to_im(radius),
                color.as_u32(),
                rot,
                num_segments,
            );
        }
    }
    pub fn add_text(&self, pos: Vector2, color: Color, text: &str) {
        unsafe {
            let (start, end) = text_ptrs(text);
            ImDrawList_AddText(self.ptr, &v2_to_im(pos), color.as_u32(), start, end);
        }
    }
    pub fn add_text_ex(
        &self,
        font: FontId,
        font_size: f32,
        pos: Vector2,
        color: Color,
        text: &str,
        wrap_width: f32,
        cpu_fine_clip_rect: Option<ImVec4>,
    ) {
        unsafe {
            let (start, end) = text_ptrs(text);
            ImDrawList_AddText1(
                self.ptr,
                font_ptr(font),
                font_size,
                &v2_to_im(pos),
                color.as_u32(),
                start,
                end,
                wrap_width,
                cpu_fine_clip_rect
                    .as_ref()
                    .map(|x| x as *const _)
                    .unwrap_or(null()),
            );
        }
    }
    pub fn add_polyline(&self, points: &[ImVec2], color: Color, flags: DrawFlags, thickness: f32) {
        unsafe {
            ImDrawList_AddPolyline(
                self.ptr,
                points.as_ptr(),
                points.len() as i32,
                color.as_u32(),
                flags.bits(),
                thickness,
            );
        }
    }
    pub fn add_convex_poly_filled(&self, points: &[ImVec2], color: Color) {
        unsafe {
            ImDrawList_AddConvexPolyFilled(
                self.ptr,
                points.as_ptr(),
                points.len() as i32,
                color.as_u32(),
            );
        }
    }
    pub fn add_concave_poly_filled(&self, points: &[ImVec2], color: Color) {
        unsafe {
            ImDrawList_AddConcavePolyFilled(
                self.ptr,
                points.as_ptr(),
                points.len() as i32,
                color.as_u32(),
            );
        }
    }
    pub fn add_bezier_cubic(
        &self,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        p4: Vector2,
        color: Color,
        thickness: f32,
        num_segments: i32,
    ) {
        unsafe {
            ImDrawList_AddBezierCubic(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                &v2_to_im(p4),
                color.as_u32(),
                thickness,
                num_segments,
            );
        }
    }
    pub fn add_bezier_quadratic(
        &self,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        color: Color,
        thickness: f32,
        num_segments: i32,
    ) {
        unsafe {
            ImDrawList_AddBezierQuadratic(
                self.ptr,
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                color.as_u32(),
                thickness,
                num_segments,
            );
        }
    }
    pub fn add_image(
        &self,
        user_texture_id: TextureId,
        p_min: Vector2,
        p_max: Vector2,
        uv_min: Vector2,
        uv_max: Vector2,
        color: Color,
    ) {
        unsafe {
            ImDrawList_AddImage(
                self.ptr,
                user_texture_id.id(),
                &v2_to_im(p_min),
                &v2_to_im(p_max),
                &v2_to_im(uv_min),
                &v2_to_im(uv_max),
                color.as_u32(),
            );
        }
    }
    pub fn add_image_quad(
        &self,
        user_texture_id: TextureId,
        p1: Vector2,
        p2: Vector2,
        p3: Vector2,
        p4: Vector2,
        uv1: Vector2,
        uv2: Vector2,
        uv3: Vector2,
        uv4: Vector2,
        color: Color,
    ) {
        unsafe {
            ImDrawList_AddImageQuad(
                self.ptr,
                user_texture_id.id(),
                &v2_to_im(p1),
                &v2_to_im(p2),
                &v2_to_im(p3),
                &v2_to_im(p4),
                &v2_to_im(uv1),
                &v2_to_im(uv2),
                &v2_to_im(uv3),
                &v2_to_im(uv4),
                color.as_u32(),
            );
        }
    }
    pub fn add_image_rounded(
        &self,
        user_texture_id: TextureId,
        p_min: Vector2,
        p_max: Vector2,
        uv_min: Vector2,
        uv_max: Vector2,
        color: Color,
        rounding: f32,
        flags: DrawFlags,
    ) {
        unsafe {
            ImDrawList_AddImageRounded(
                self.ptr,
                user_texture_id.id(),
                &v2_to_im(p_min),
                &v2_to_im(p_max),
                &v2_to_im(uv_min),
                &v2_to_im(uv_max),
                color.as_u32(),
                rounding,
                flags.bits(),
            );
        }
    }

    pub fn add_callback(&self, cb: impl FnOnce(&mut A) + 'static) {
        // Callbacks are only called once, convert the FnOnce into an FnMut to register
        // They are called after `do_ui` so first argument pointer is valid.
        // The second argument is not used, set to `()``.
        let mut cb = Some(cb);
        unsafe {
            let id = self.ui.push_callback(move |a, _: ()| {
                if let Some(cb) = cb.take() {
                    cb(&mut *a);
                }
            });
            ImDrawList_AddCallback(
                self.ptr,
                Some(call_drawlist_callback::<A>),
                id as *mut c_void,
            );
        }
    }
    pub fn add_draw_cmd(&self) {
        unsafe {
            ImDrawList_AddDrawCmd(self.ptr);
        }
    }
}

unsafe extern "C" fn call_drawlist_callback<A>(
    _parent_lilst: *const ImDrawList,
    cmd: *const ImDrawCmd,
) {
    let id = (*cmd).UserCallbackData as usize;
    Ui::<A>::run_callback(id, ());
}

/// Represents any type that can be converted to a Dear ImGui hash id.
pub trait Hashable {
    // These are unsafe because they should be called only inside a frame (holding a &mut Ui)
    unsafe fn get_id(&self) -> ImGuiID;
    unsafe fn push(&self);
}

impl Hashable for &str {
    unsafe fn get_id(&self) -> ImGuiID {
        let (start, end) = text_ptrs(self);
        ImGui_GetID1(start, end)
    }
    unsafe fn push(&self) {
        let (start, end) = text_ptrs(self);
        ImGui_PushID1(start, end);
    }
}

impl Hashable for usize {
    unsafe fn get_id(&self) -> ImGuiID {
        ImGui_GetID2(*self as *const c_void)
    }
    unsafe fn push(&self) {
        ImGui_PushID2(*self as *const c_void);
    }
}

/// Any value that can be applied with a _push_ function and unapplied with a _pop_ function.
///
/// Apply to the current frame using [`Ui::with_push`]. If you want to apply several values at the
/// same time use a tuple or an array.
/// Only tuples up to 4 values are supported, but you can apply arbitrarily many pushables by
/// creating tuples of tuples: `(A, B, C, (D, E, F, (G, H, I, J)))`.
pub trait Pushable {
    unsafe fn push(&self);
    unsafe fn pop(&self);
}

struct PushableGuard<'a, P: Pushable>(&'a P);

impl<P: Pushable> Drop for PushableGuard<'_, P> {
    fn drop(&mut self) {
        unsafe {
            self.0.pop();
        }
    }
}

#[allow(clippy::needless_lifetimes)]
unsafe fn push_guard<'a, P: Pushable>(p: &'a P) -> PushableGuard<'a, P> {
    p.push();
    PushableGuard(p)
}

/// A [`Pushable`] that does nothing.
impl Pushable for () {
    unsafe fn push(&self) {}
    unsafe fn pop(&self) {}
}

impl<A: Pushable> Pushable for (A,) {
    unsafe fn push(&self) {
        self.0.push();
    }
    unsafe fn pop(&self) {
        self.0.pop();
    }
}

impl<A: Pushable, B: Pushable> Pushable for (A, B) {
    unsafe fn push(&self) {
        self.0.push();
        self.1.push();
    }
    unsafe fn pop(&self) {
        self.1.pop();
        self.0.pop();
    }
}

impl<A: Pushable, B: Pushable, C: Pushable> Pushable for (A, B, C) {
    unsafe fn push(&self) {
        self.0.push();
        self.1.push();
        self.2.push();
    }
    unsafe fn pop(&self) {
        self.2.pop();
        self.1.pop();
        self.0.pop();
    }
}

impl<A: Pushable, B: Pushable, C: Pushable, D: Pushable> Pushable for (A, B, C, D) {
    unsafe fn push(&self) {
        self.0.push();
        self.1.push();
        self.2.push();
        self.3.push();
    }
    unsafe fn pop(&self) {
        self.3.pop();
        self.2.pop();
        self.1.pop();
        self.0.pop();
    }
}

impl Pushable for &[&dyn Pushable] {
    unsafe fn push(&self) {
        for st in *self {
            st.push();
        }
    }
    unsafe fn pop(&self) {
        for st in self.iter().rev() {
            st.pop();
        }
    }
}

/// A [`Pushable`] that is applied optionally.
impl<T: Pushable> Pushable for Option<T> {
    unsafe fn push(&self) {
        if let Some(s) = self {
            s.push();
        }
    }
    unsafe fn pop(&self) {
        if let Some(s) = self {
            s.pop();
        }
    }
}

impl Pushable for FontId {
    unsafe fn push(&self) {
        ImGui_PushFont(font_ptr(*self));
    }
    unsafe fn pop(&self) {
        ImGui_PopFont();
    }
}

pub type StyleColor = (ColorId, Color);

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TextureId(ImTextureID);

impl TextureId {
    pub fn id(&self) -> ImTextureID {
        self.0
    }
    pub unsafe fn from_id(id: ImTextureID) -> Self {
        Self(id)
    }
}

impl Pushable for StyleColor {
    unsafe fn push(&self) {
        ImGui_PushStyleColor1(self.0.bits(), &self.1.into());
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleColor(1);
    }
}

impl Pushable for [StyleColor] {
    unsafe fn push(&self) {
        for sc in self {
            sc.push();
        }
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleColor(self.len() as i32);
    }
}

impl<const N: usize> Pushable for [StyleColor; N] {
    unsafe fn push(&self) {
        self.as_slice().push();
    }
    unsafe fn pop(&self) {
        self.as_slice().pop();
    }
}

pub type StyleColorF = (ColorId, ImVec4);

impl Pushable for StyleColorF {
    unsafe fn push(&self) {
        ImGui_PushStyleColor1(self.0.bits(), &self.1);
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleColor(1);
    }
}

impl Pushable for [StyleColorF] {
    unsafe fn push(&self) {
        for sc in self {
            sc.push();
        }
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleColor(self.len() as i32);
    }
}

impl<const N: usize> Pushable for [StyleColorF; N] {
    unsafe fn push(&self) {
        self.as_slice().push();
    }
    unsafe fn pop(&self) {
        self.as_slice().pop();
    }
}

#[derive(Debug, Copy, Clone)]
pub enum StyleValue {
    F32(f32),
    Vec2(Vector2),
}

pub type Style = (StyleVar, StyleValue);

impl Pushable for Style {
    unsafe fn push(&self) {
        match self.1 {
            StyleValue::F32(f) => ImGui_PushStyleVar(self.0.bits(), f),
            StyleValue::Vec2(v) => ImGui_PushStyleVar1(self.0.bits(), &v2_to_im(v)),
        }
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleVar(1);
    }
}

impl Pushable for [Style] {
    unsafe fn push(&self) {
        for sc in self {
            sc.push();
        }
    }
    unsafe fn pop(&self) {
        ImGui_PopStyleVar(self.len() as i32);
    }
}

impl<const N: usize> Pushable for [Style; N] {
    unsafe fn push(&self) {
        self.as_slice().push();
    }
    unsafe fn pop(&self) {
        self.as_slice().pop();
    }
}

#[derive(Debug, Copy, Clone)]
pub struct ItemWidth(pub f32);

impl Pushable for ItemWidth {
    unsafe fn push(&self) {
        ImGui_PushItemWidth(self.0);
    }
    unsafe fn pop(&self) {
        ImGui_PopItemWidth();
    }
}

#[derive(Debug, Copy, Clone)]
pub struct Indent(pub f32);

impl Pushable for Indent {
    unsafe fn push(&self) {
        ImGui_Indent(self.0);
    }
    unsafe fn pop(&self) {
        ImGui_Unindent(self.0);
    }
}

#[derive(Debug, Copy, Clone)]
pub struct TextWrapPos(pub f32);

impl Pushable for TextWrapPos {
    unsafe fn push(&self) {
        ImGui_PushTextWrapPos(self.0);
    }
    unsafe fn pop(&self) {
        ImGui_PopTextWrapPos();
    }
}

#[derive(Debug, Copy, Clone)]
pub struct TabStop(pub bool);

impl Pushable for TabStop {
    unsafe fn push(&self) {
        ImGui_PushTabStop(self.0);
    }
    unsafe fn pop(&self) {
        ImGui_PopTabStop();
    }
}

#[derive(Debug, Copy, Clone)]
pub struct ButtonRepeat(pub bool);

impl Pushable for ButtonRepeat {
    unsafe fn push(&self) {
        ImGui_PushButtonRepeat(self.0);
    }
    unsafe fn pop(&self) {
        ImGui_PopButtonRepeat();
    }
}

#[derive(Debug, Copy, Clone)]
pub struct ItemId<H: Hashable>(pub H);

impl<H: Hashable> Pushable for ItemId<H> {
    unsafe fn push(&self) {
        self.0.push();
    }
    unsafe fn pop(&self) {
        ImGui_PopID();
    }
}

pub struct Viewport<'s> {
    ptr: &'s ImGuiViewport,
}

impl Viewport<'_> {
    pub fn flags(&self) -> ViewportFlags {
        ViewportFlags::from_bits_truncate(self.ptr.Flags)
    }
    pub fn pos(&self) -> Vector2 {
        im_to_v2(self.ptr.Pos)
    }
    pub fn size(&self) -> Vector2 {
        im_to_v2(self.ptr.Size)
    }
    pub fn work_pos(&self) -> Vector2 {
        im_to_v2(self.ptr.WorkPos)
    }
    pub fn work_size(&self) -> Vector2 {
        im_to_v2(self.ptr.WorkSize)
    }
}

decl_builder_with_opt! { TableConfig, ImGui_BeginTable, ImGui_EndTable () (S: IntoCStr)
    (
        str_id (S::Temp) (str_id.as_ptr()),
        column (i32) (column),
        flags (TableFlags) (flags.bits()),
        outer_size (ImVec2) (&outer_size),
        inner_width (f32) (inner_width),
    )
    {
        decl_builder_setter!{flags: TableFlags}
        decl_builder_setter_vector2!{outer_size: Vector2}
        decl_builder_setter!{inner_width: f32}
    }
    {
        pub fn table_config<S: IntoCStr>(&self, str_id: S, column: i32) -> TableConfig<S> {
            TableConfig {
                str_id: str_id.into(),
                column,
                flags: TableFlags::None,
                outer_size: im_vec2(0.0, 0.0),
                inner_width: 0.0,
                push: (),
            }
        }
        pub fn table_next_row(&self, flags: TableRowFlags, min_row_height: f32) {
            unsafe {
                ImGui_TableNextRow(flags.bits(), min_row_height);
            }
        }
        pub fn table_next_column(&self) -> bool {
            unsafe {
                ImGui_TableNextColumn()
            }
        }
        pub fn table_set_column_index(&self, column_n: i32) -> bool {
            unsafe {
                ImGui_TableSetColumnIndex(column_n)
            }
        }
        pub fn table_setup_column(&self, label: impl IntoCStr, flags: TableColumnFlags, init_width_or_weight: f32, user_id: ImGuiID) {
            unsafe {
                ImGui_TableSetupColumn(label.into().as_ptr(), flags.bits(), init_width_or_weight, user_id);
            }
        }
        pub fn table_setup_scroll_freeze(&self, cols: i32, rows: i32) {
            unsafe {
                ImGui_TableSetupScrollFreeze(cols, rows);
            }
        }
        pub fn table_headers_row(&self) {
            unsafe {
                ImGui_TableHeadersRow();
            }
        }
        pub fn table_angle_headers_row(&self) {
            unsafe {
                ImGui_TableAngledHeadersRow();
            }
        }
        pub fn table_get_columns_count(&self) -> i32 {
            unsafe {
                ImGui_TableGetColumnCount()
            }
        }
        pub fn table_get_column_index(&self) -> i32 {
            unsafe {
                ImGui_TableGetColumnIndex()
            }
        }
        pub fn table_get_row_index(&self) -> i32 {
            unsafe {
                ImGui_TableGetRowIndex()
            }
        }
        pub fn table_get_column_flags(&self, column_n: Option<i32>) -> TableColumnFlags {
            let bits = unsafe {
                ImGui_TableGetColumnFlags(column_n.unwrap_or(-1))
            };
            TableColumnFlags::from_bits_truncate(bits)
        }
        pub fn table_set_column_enabled(&self, column_n: Option<i32>, enabled: bool) {
            unsafe {
                ImGui_TableSetColumnEnabled(column_n.unwrap_or(-1), enabled);
            };
        }
        pub fn table_set_bg_color(&self, target: TableBgTarget, color: Color, column_n: Option<i32>) {
            unsafe {
                ImGui_TableSetBgColor(target.bits(), color.as_u32(), column_n.unwrap_or(-1));
            };
        }
        //TODO: ImGui_TableGetSortSpecs, TableGetColumnName
    }
}

/// Helper token class that allows to set the drag&drop payload, once.
pub struct DragDropPayloadSetter<'a> {
    _dummy: PhantomData<&'a ()>,
}

/// This is a sub-set of [`Cond`], only for drag&drop payloads.
pub enum DragDropPayloadCond {
    Always,
    Once,
}

impl<'a> DragDropPayloadSetter<'a> {
    pub fn set(self, type_: impl IntoCStr, data: &[u8], cond: DragDropPayloadCond) -> bool {
        // For some reason ImGui does not accept a non-null pointer with length 0.
        let ptr = if data.is_empty() {
            null()
        } else {
            data.as_ptr() as *const c_void
        };
        let len = data.len();
        let cond = match cond {
            DragDropPayloadCond::Always => Cond::Always,
            DragDropPayloadCond::Once => Cond::Once,
        };
        unsafe { ImGui_SetDragDropPayload(type_.into().as_ptr(), ptr, len, cond.bits()) }
    }
}

/// Helpar class to get the drag&drop payload.
pub struct DragDropPayloadGetter<'a> {
    _dummy: PhantomData<&'a ()>,
}

/// The payload of a drag&drop operation.
///
/// It contains a "type", and a byte array.
pub struct DragDropPayload<'a> {
    pay: &'a ImGuiPayload,
}

impl<'a> DragDropPayloadGetter<'a> {
    pub fn any(&self, flags: DragDropAcceptFlags) -> Option<DragDropPayload<'a>> {
        unsafe {
            let pay = ImGui_AcceptDragDropPayload(null(), flags.bits());
            if pay.is_null() {
                None
            } else {
                Some(DragDropPayload { pay: &*pay })
            }
        }
    }
    pub fn by_type(
        &self,
        type_: impl IntoCStr,
        flags: DragDropAcceptFlags,
    ) -> Option<DragDropPayload<'a>> {
        unsafe {
            let pay = ImGui_AcceptDragDropPayload(type_.into().as_ptr(), flags.bits());
            if pay.is_null() {
                None
            } else {
                Some(DragDropPayload { pay: &*pay })
            }
        }
    }
    pub fn peek(&self) -> Option<DragDropPayload<'a>> {
        unsafe {
            let pay = ImGui_GetDragDropPayload();
            if pay.is_null() {
                None
            } else {
                Some(DragDropPayload { pay: &*pay })
            }
        }
    }
}

impl<'a> DragDropPayload<'a> {
    //WARNING: inline functions
    pub fn is_data_type(&self, type_: impl IntoCStr) -> bool {
        if self.pay.DataFrameCount == -1 {
            return false;
        }
        let data_type = unsafe { std::mem::transmute::<&[i8], &[u8]>(&self.pay.DataType) };
        let data_type = CStr::from_bytes_until_nul(data_type).unwrap();
        data_type == type_.into().as_ref()
    }
    pub fn type_(&self) -> Cow<'_, str> {
        let data_type = unsafe { std::mem::transmute::<&[i8], &[u8]>(&self.pay.DataType) };
        let data_type = CStr::from_bytes_until_nul(data_type).unwrap();
        data_type.to_string_lossy()
    }
    pub fn is_preview(&self) -> bool {
        self.pay.Preview
    }
    pub fn is_delivery(&self) -> bool {
        self.pay.Delivery
    }
    pub fn data(&self) -> &[u8] {
        if self.pay.Data.is_null() {
            &[]
        } else {
            unsafe {
                std::slice::from_raw_parts(self.pay.Data as *const u8, self.pay.DataSize as usize)
            }
        }
    }
}

pub const PAYLOAD_TYPE_COLOR_3F: &CStr =
    unsafe { CStr::from_bytes_with_nul_unchecked(IMGUI_PAYLOAD_TYPE_COLOR_3F) };
pub const PAYLOAD_TYPE_COLOR_4F: &CStr =
    unsafe { CStr::from_bytes_with_nul_unchecked(IMGUI_PAYLOAD_TYPE_COLOR_4F) };