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
|
//- small helpers (same discipline as mp3_player: zero heap allocs per frame)
internal b32 ui_hovered(PlatformInput *input, Rect rect)
{
return input->mouse_in_window && rect_contains(rect, input->mouse_x, input->mouse_y);
}
internal b32 ui_clicked(PlatformInput *input, Rect rect)
{
return ButtonPressed(input->mouse[0]) && ui_hovered(input, rect);
}
internal f32 ui_baseline(App *app, FontKind kind, f32 size, f32 y0, f32 height)
{
f32 line = font_line_height(&app->fonts, kind, size);
f32 ascent = font_ascent(&app->fonts, kind, size);
return floorf(y0 + (height - line) * 0.5f + ascent + 0.5f);
}
internal String8 ui_format(Arena *arena, const char *format, ...)
{
va_list args;
va_start(args, format);
char buffer[256];
int length = vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
length = Clamp(length, 0, (int)sizeof(buffer) - 1);
u8 *out = PushArray(arena, u8, (u64)Max(length, 1));
MemCpy(out, buffer, (u64)length);
return ToString8Size(out, (u64)length);
}
internal void format_bytes(char *out, u32 size, f64 bytes)
{
const char *units[] = {"B", "KB", "MB", "GB", "TB"};
u32 unit = 0;
while(bytes >= 1024.0 && unit + 1 < ArrayCount(units)) {
bytes /= 1024.0;
++unit;
}
snprintf(out, size, "%.1f %s", bytes, units[unit]);
}
internal void app_message(App *app, const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(app->message, sizeof(app->message), format, args);
va_end(args);
app->message_ns = time_now_ns();
}
internal b32 str8_contains_nocase(String8 haystack, const char *needle, u32 needle_size)
{
if(needle_size == 0) return true;
for(u64 start = 0; start + needle_size <= haystack.size; ++start) {
u32 index = 0;
for(; index < needle_size; ++index) {
u8 a = haystack.data[start + index], b = (u8)needle[index];
if(a >= 'A' && a <= 'Z') a += 32;
if(b >= 'A' && b <= 'Z') b += 32;
if(a != b) break;
}
if(index == needle_size) return true;
}
return false;
}
internal void scroll_update(ScrollList *scroll, f32 content, f32 view, f32 dt, b32 *animating)
{
(void)dt;
(void)animating;
f32 max_offset = Max(content - view, 0.0f);
scroll->target = Clamp(scroll->target, 0.0f, max_offset);
scroll->offset = scroll->target;
}
internal void app_apply_settings(App *app)
{
settings_clamp(&app->settings);
if(app->diagram) {
app->diagram->default_routing = app->settings.default_routing;
app->diagram->focus_hops = app->settings.focus_hops;
}
}
//- layout pool (identical RAD-style binary split tree to mp3_player)
internal s32 layout_alloc_panel(Layout *layout)
{
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
if(layout->panels[index].kind == PANEL_FREE) {
MemSetZero(&layout->panels[index], sizeof(Panel));
layout->panels[index].kind = PANEL_LEAF;
layout->panels[index].parent = -1;
return index;
}
}
return -1;
}
internal s32 layout_alloc_view(Layout *layout, ViewKind kind)
{
for(s32 index = 0; index < UI_MAX_VIEWS; ++index) {
if(layout->views[index].kind == VIEW_NONE) {
MemSetZero(&layout->views[index], sizeof(View));
layout->views[index].kind = (u32)kind;
layout->views[index].cam_zoom = 1.0f;
layout->views[index].cam_target_zoom = 1.0f;
layout->views[index].cam_fit_pending = (kind == VIEW_CANVAS);
return index;
}
}
return -1;
}
internal s32 layout_leaf(Layout *layout, s32 parent, ViewKind *kinds, u32 count)
{
s32 panel = layout_alloc_panel(layout);
Panel *p = &layout->panels[panel];
p->kind = PANEL_LEAF;
p->parent = parent;
for(u32 index = 0; index < count && p->tab_count < UI_MAX_TABS; ++index) {
s32 view = layout_alloc_view(layout, kinds[index]);
if(view >= 0) p->tabs[p->tab_count++] = (u8)view;
}
return panel;
}
internal s32 layout_split(Layout *layout, s32 parent, u32 axis, f32 ratio)
{
s32 panel = layout_alloc_panel(layout);
Panel *p = &layout->panels[panel];
p->kind = PANEL_SPLIT;
p->parent = parent;
p->axis = axis;
p->ratio = ratio;
return panel;
}
internal void layout_add_status_bar(Layout *layout)
{
ViewKind kinds[] = {VIEW_STATUS};
s32 old_root = layout->root;
s32 split = layout_split(layout, -1, 1, -1.0f);
s32 bar = layout_leaf(layout, split, kinds, 1);
layout->panels[split].children[0] = old_root;
layout->panels[split].children[1] = bar;
layout->panels[old_root].parent = split;
layout->root = split;
}
internal void ui_layout_default(Layout *layout)
{
MemSetZero(layout, sizeof(*layout));
layout->version = UI_LAYOUT_VERSION;
// [ Canvas (72%) ] | [ Outline | Palette | Layers ] / [ Inspector | Edges | Source | Views ]
ViewKind canvas_kinds[] = {VIEW_CANVAS};
ViewKind top_kinds[] = {VIEW_OUTLINE, VIEW_PALETTE, VIEW_LAYERS};
ViewKind bot_kinds[] = {VIEW_INSPECTOR, VIEW_EDGES, VIEW_SOURCE, VIEW_VIEWS};
s32 root = layout_split(layout, -1, 0, 0.70f);
s32 left = layout_leaf(layout, root, canvas_kinds, ArrayCount(canvas_kinds));
s32 right = layout_split(layout, root, 1, 0.46f);
s32 top = layout_leaf(layout, right, top_kinds, ArrayCount(top_kinds));
s32 bottom = layout_leaf(layout, right, bot_kinds, ArrayCount(bot_kinds));
layout->panels[root].children[0] = left;
layout->panels[root].children[1] = right;
layout->panels[right].children[0] = top;
layout->panels[right].children[1] = bottom;
layout->root = root;
layout->focused = left;
layout_add_status_bar(layout);
}
internal b32 layout_valid(Layout *layout)
{
if(layout->version != UI_LAYOUT_VERSION || layout->root < 0 || layout->root >= UI_MAX_PANELS) return false;
if(layout->panels[layout->root].kind == PANEL_FREE) return false;
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind == PANEL_SPLIT) {
for(u32 c = 0; c < 2; ++c) {
s32 child = p->children[c];
if(child < 0 || child >= UI_MAX_PANELS || layout->panels[child].kind == PANEL_FREE || layout->panels[child].parent != index) return false;
}
} else if(p->kind == PANEL_LEAF) {
if(p->tab_count > UI_MAX_TABS) return false;
for(u32 t = 0; t < p->tab_count; ++t) {
if(p->tabs[t] >= UI_MAX_VIEWS || layout->views[p->tabs[t]].kind == VIEW_NONE || layout->views[p->tabs[t]].kind >= VIEW_KIND_COUNT) return false;
}
} else if(p->kind != PANEL_FREE) {
return false;
}
}
return layout->focused >= 0 && layout->focused < UI_MAX_PANELS && layout->panels[layout->focused].kind == PANEL_LEAF;
}
internal s32 layout_first_leaf(Layout *layout, s32 panel)
{
while(layout->panels[panel].kind == PANEL_SPLIT) panel = layout->panels[panel].children[0];
return panel;
}
internal s32 panel_active_view(Layout *layout, s32 panel)
{
Panel *p = &layout->panels[panel];
if(p->kind != PANEL_LEAF || p->tab_count == 0) return -1;
return p->tabs[Min(p->active_tab, p->tab_count - 1)];
}
internal void layout_collapse(Layout *layout, s32 leaf)
{
Panel *p = &layout->panels[leaf];
if(p->kind != PANEL_LEAF || p->tab_count > 0 || p->parent < 0) return;
s32 parent = p->parent;
Panel *pp = &layout->panels[parent];
s32 sibling = (pp->children[0] == leaf) ? pp->children[1] : pp->children[0];
s32 grand = pp->parent;
Panel moved = layout->panels[sibling];
moved.parent = grand;
*pp = moved;
if(pp->kind == PANEL_SPLIT) {
layout->panels[pp->children[0]].parent = parent;
layout->panels[pp->children[1]].parent = parent;
}
layout->panels[sibling].kind = PANEL_FREE;
layout->panels[leaf].kind = PANEL_FREE;
if(layout->focused == leaf || layout->focused == sibling || layout->panels[layout->focused].kind != PANEL_LEAF) {
layout->focused = layout_first_leaf(layout, parent);
}
}
internal void layout_remove_tab(Layout *layout, s32 panel, u32 tab)
{
Panel *p = &layout->panels[panel];
for(u32 index = tab; index + 1 < p->tab_count; ++index) p->tabs[index] = p->tabs[index + 1];
--p->tab_count;
if(p->active_tab >= p->tab_count && p->active_tab > 0) --p->active_tab;
}
internal void layout_close_view(Layout *layout, s32 panel, u32 tab)
{
Panel *p = &layout->panels[panel];
if(tab >= p->tab_count) return;
layout->views[p->tabs[tab]].kind = VIEW_NONE;
layout_remove_tab(layout, panel, tab);
layout_collapse(layout, panel);
}
typedef enum DropZone { DROP_NONE, DROP_CENTER, DROP_LEFT, DROP_RIGHT, DROP_TOP, DROP_BOTTOM } DropZone;
internal void layout_move_view(Layout *layout, s32 from_panel, s32 view, s32 to_panel, DropZone zone)
{
Panel *source = &layout->panels[from_panel];
u32 tab = 0;
for(; tab < source->tab_count && source->tabs[tab] != view; ++tab) {}
if(tab == source->tab_count) return;
if(from_panel == to_panel && (zone == DROP_CENTER || source->tab_count == 1)) return;
if(zone == DROP_CENTER) {
Panel *target = &layout->panels[to_panel];
if(target->tab_count >= UI_MAX_TABS) return;
layout_remove_tab(layout, from_panel, tab);
target->tabs[target->tab_count] = (u8)view;
target->active_tab = target->tab_count++;
layout->focused = to_panel;
layout_collapse(layout, from_panel);
return;
}
s32 keep = layout_alloc_panel(layout);
s32 fresh = (keep >= 0) ? layout_alloc_panel(layout) : -1;
if(keep < 0 || fresh < 0) {
if(keep >= 0) layout->panels[keep].kind = PANEL_FREE;
return;
}
layout_remove_tab(layout, from_panel, tab);
Panel *target = &layout->panels[to_panel];
layout->panels[keep] = *target;
layout->panels[keep].parent = to_panel;
Panel *fresh_panel = &layout->panels[fresh];
fresh_panel->kind = PANEL_LEAF;
fresh_panel->parent = to_panel;
fresh_panel->tabs[0] = (u8)view;
fresh_panel->tab_count = 1;
b32 fresh_first = (zone == DROP_LEFT || zone == DROP_TOP);
target->kind = PANEL_SPLIT;
target->axis = (zone == DROP_LEFT || zone == DROP_RIGHT) ? 0 : 1;
target->ratio = 0.5f;
target->children[0] = fresh_first ? fresh : keep;
target->children[1] = fresh_first ? keep : fresh;
target->tab_count = 0;
s32 source_now = (from_panel == to_panel) ? keep : from_panel;
layout->focused = fresh;
layout_collapse(layout, source_now);
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_LEAF) continue;
for(u32 t = 0; t < p->tab_count; ++t) {
if(p->tabs[t] == view) { layout->focused = index; p->active_tab = t; }
}
}
}
internal void layout_compute(Layout *layout, s32 panel, Rect rect)
{
Panel *p = &layout->panels[panel];
p->rect = rect;
if(p->kind != PANEL_SPLIT) return;
f32 half = UI_GAP * 0.5f;
if(p->axis == 0) {
f32 split = rect.x0 + rect_w(rect) * p->ratio;
layout_compute(layout, p->children[0], Rect{rect.x0, rect.y0, split - half, rect.y1});
layout_compute(layout, p->children[1], Rect{split + half, rect.y0, rect.x1, rect.y1});
} else {
f32 split = (p->ratio < 0) ? Max(rect.y1 - half + p->ratio * UI_STATUS_H, rect.y0 + half)
: rect.y0 + rect_h(rect) * p->ratio;
layout_compute(layout, p->children[0], Rect{rect.x0, rect.y0, rect.x1, split - half});
layout_compute(layout, p->children[1], Rect{rect.x0, split + half, rect.x1, rect.y1});
}
}
internal s32 layout_find_kind(Layout *layout, ViewKind kind, s32 *out_panel, u32 group = 0)
{
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_LEAF) continue;
for(u32 t = 0; t < p->tab_count; ++t) {
if(layout->views[p->tabs[t]].kind == (u32)kind && layout->views[p->tabs[t]].group == group) {
if(out_panel) *out_panel = index;
return p->tabs[t];
}
}
}
return -1;
}
internal void layout_open(Layout *layout, ViewKind kind, u32 group = 0)
{
s32 panel = -1;
s32 view = layout_find_kind(layout, kind, &panel, group);
if(view >= 0 && kind != VIEW_CANVAS) {
Panel *p = &layout->panels[panel];
for(u32 t = 0; t < p->tab_count; ++t) if(p->tabs[t] == view) p->active_tab = t;
layout->focused = panel;
return;
}
if(view >= 0 && kind == VIEW_CANVAS) {
Panel *p = &layout->panels[panel];
for(u32 t = 0; t < p->tab_count; ++t) if(p->tabs[t] == view) p->active_tab = t;
layout->focused = panel;
return;
}
Panel *p = &layout->panels[layout->focused];
view = layout_alloc_view(layout, kind);
if(view < 0) return;
layout->views[view].group = group;
if(p->tab_count >= UI_MAX_TABS) { layout->views[view].kind = VIEW_NONE; return; }
p->tabs[p->tab_count] = (u8)view;
p->active_tab = p->tab_count++;
}
internal void layout_split_with(Layout *layout, ViewKind kind, u32 axis, u32 group = 0)
{
s32 copy = layout_alloc_view(layout, kind);
if(copy < 0) return;
layout->views[copy].group = group;
Panel *p = &layout->panels[layout->focused];
if(p->tab_count >= UI_MAX_TABS) { layout->views[copy].kind = VIEW_NONE; return; }
p->tabs[p->tab_count++] = (u8)copy;
layout_move_view(layout, layout->focused, copy, layout->focused, axis == 0 ? DROP_RIGHT : DROP_BOTTOM);
}
internal void layout_split_focused(Layout *layout, u32 axis)
{
s32 view = panel_active_view(layout, layout->focused);
if(view < 0) return;
layout_split_with(layout, (ViewKind)layout->views[view].kind, axis, layout->views[view].group);
}
internal b32 panel_is_bar(Layout *layout, s32 panel)
{
return rect_h(layout->panels[panel].rect) < UI_TAB_H + UI_COLUMN_H + UI_ROW_H;
}
internal void layout_focus_direction(Layout *layout, s32 dx, s32 dy)
{
Rect current = layout->panels[layout->focused].rect;
s32 best = -1;
f32 best_d = 1e9f;
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_LEAF || index == layout->focused || panel_is_bar(layout, index)) continue;
Rect r = p->rect;
f32 d = -1;
if(dx > 0 && r.x0 >= current.x1 - 1 && r.y0 < current.y1 && r.y1 > current.y0) d = r.x0 - current.x1;
if(dx < 0 && r.x1 <= current.x0 + 1 && r.y0 < current.y1 && r.y1 > current.y0) d = current.x0 - r.x1;
if(dy > 0 && r.y0 >= current.y1 - 1 && r.x0 < current.x1 && r.x1 > current.x0) d = r.y0 - current.y1;
if(dy < 0 && r.y1 <= current.y0 + 1 && r.x0 < current.x1 && r.x1 > current.x0) d = current.y0 - r.y1;
if(d >= 0 && d < best_d) { best_d = d; best = index; }
}
if(best >= 0) layout->focused = best;
}
internal void layout_focus_next(Layout *layout)
{
for(s32 step = 1; step <= UI_MAX_PANELS; ++step) {
s32 index = (layout->focused + step) % UI_MAX_PANELS;
if(layout->panels[index].kind == PANEL_LEAF && !panel_is_bar(layout, index)) { layout->focused = index; return; }
}
}
internal void layout_leaves(Layout *layout, s32 panel, s32 *out, u32 *count)
{
Panel *p = &layout->panels[panel];
if(p->kind == PANEL_SPLIT) {
layout_leaves(layout, p->children[0], out, count);
layout_leaves(layout, p->children[1], out, count);
} else if(p->kind == PANEL_LEAF && *count < UI_MAX_PANELS) {
out[(*count)++] = panel;
}
}
internal void layout_cycle_views(Layout *layout, s32 direction)
{
s32 leaves[UI_MAX_PANELS];
u32 leaf_count = 0;
layout_leaves(layout, layout->root, leaves, &leaf_count);
s32 panels[UI_MAX_PANELS * UI_MAX_TABS];
u32 tabs[UI_MAX_PANELS * UI_MAX_TABS];
u32 count = 0;
s32 at = -1;
for(u32 leaf = 0; leaf < leaf_count; ++leaf) {
s32 panel = leaves[leaf];
Panel *p = &layout->panels[panel];
if(panel_is_bar(layout, panel)) continue;
for(u32 t = 0; t < p->tab_count; ++t) {
if(panel == layout->focused && t == p->active_tab) at = (s32)count;
panels[count] = panel;
tabs[count] = t;
++count;
}
}
if(count == 0) return;
s32 next = (at < 0) ? 0 : (at + direction + (s32)count) % (s32)count;
layout->focused = panels[next];
layout->panels[panels[next]].active_tab = tabs[next];
}
internal b32 layout_view_visible(Layout *layout, ViewKind kind)
{
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
s32 view = panel_active_view(layout, index);
if(view >= 0 && layout->views[view].kind == (u32)kind) return true;
}
return false;
}
internal View *find_canvas_view(App *app)
{
s32 focused_v = panel_active_view(&app->layout, app->layout.focused);
if(focused_v >= 0 && app->layout.views[focused_v].kind == VIEW_CANVAS) return &app->layout.views[focused_v];
for(s32 i = 0; i < UI_MAX_PANELS; ++i) {
s32 v = panel_active_view(&app->layout, i);
if(v >= 0 && app->layout.views[v].kind == VIEW_CANVAS) return &app->layout.views[v];
}
return NULL;
}
//- view registry & profiler groups
typedef enum ProfileMetric {
PM_SECTION_FRAME, PM_FPS, PM_FRAME_CPU, PM_FRAME_SPLIT, PM_UI_SPLIT, PM_RENDER_SPLIT, PM_IDLE, PM_DRAW,
PM_SECTION_MEMORY, PM_RSS, PM_RSS_KIND, PM_VIRTUAL, PM_STACK_MAIN,
PM_SECTION_HEAP, PM_HEAP_LIVE, PM_HEAP_ALLOCS, PM_HEAP_FREES, PM_HEAP_BLOCKS, PM_HEAP_TOTAL, PM_HEAP_GLIBC,
PM_SECTION_ARENAS, PM_ARENA,
PM_SECTION_THREADS, PM_THREAD,
PM_SECTION_PROCESS, PM_CPU, PM_FAULTS, PM_SWITCHES,
PM_SECTION_GPU, PM_GPU_MEMORY, PM_GPU_SWAPCHAIN,
PM_SECTION_FONTS, PM_FONT_GLYPHS, PM_FONT_RUNS, PM_FONT_ATLAS,
PM_SECTION_DIAGRAM, PM_DIA_NODES, PM_DIA_EDGES, PM_DIA_MEMBERS, PM_DIA_LAYOUT, PM_DIA_UNDO,
PM_COUNT,
} ProfileMetric;
global_variable const char *profile_labels[PM_COUNT] = {
"frame", "frame rate", "frame cpu", "frame split", "ui split", "render split", "between frames", "draw",
"memory", "resident", "resident by kind", "virtual", "main thread stack",
"heap (malloc, whole process)", "live", "allocations", "frees", "blocks alive", "handed out", "glibc arenas",
"arenas", "",
"threads and stacks", "",
"process", "cpu", "page faults", "context switches",
"gpu", "allocated by us", "swapchain",
"fonts", "glyph cache", "shaped text cache", "atlas",
"diagram", "nodes", "edges", "sections / members", "last auto-layout", "undo history",
};
global_variable const char *profile_group_names[] = {"frame", "memory", "heap", "arenas", "threads", "process", "gpu", "fonts", "diagram"};
internal b32 profile_is_section(u32 m)
{
return m == PM_SECTION_FRAME || m == PM_SECTION_MEMORY || m == PM_SECTION_HEAP || m == PM_SECTION_ARENAS ||
m == PM_SECTION_THREADS || m == PM_SECTION_PROCESS || m == PM_SECTION_GPU || m == PM_SECTION_FONTS ||
m == PM_SECTION_DIAGRAM;
}
typedef struct ViewType ViewType;
struct ViewType {
u32 kind;
u32 group;
const char *name;
};
//- the manual (help.txt, embedded in app.cpp)
// NOTE: line offsets built once, the help view and :help <topic> index into them
#define HELP_MAX_LINES 2048
global_variable u32 help_line_offsets[HELP_MAX_LINES + 1];
global_variable u32 help_line_count;
internal String8 help_text(void)
{
return EmbeddedString8(design_help_text);
}
internal void help_index(void)
{
if(help_line_count) return;
String8 text = help_text();
help_line_offsets[0] = 0;
u32 lines = 0;
for(u64 i = 0; i < text.size && lines < HELP_MAX_LINES; ++i) {
if(text.data[i] == '\n') help_line_offsets[++lines] = (u32)(i + 1);
}
// NOTE: a trailing newline doesn't start another line
if(lines < HELP_MAX_LINES && help_line_offsets[lines] < text.size) help_line_offsets[++lines] = (u32)text.size + 1;
help_line_count = lines;
}
internal String8 help_line(u32 line)
{
help_index();
if(line >= help_line_count) return str8_zero();
String8 text = help_text();
u32 start = help_line_offsets[line];
u32 end = help_line_offsets[line + 1] - 1; // drop the newline
if(end > text.size) end = (u32)text.size;
if(end > start && text.data[end - 1] == '\r') --end;
return ToString8Size(text.data + start, end > start ? end - start : 0);
}
internal b32 help_line_is_heading(u32 line)
{
String8 text = help_line(line);
if(text.size == 0) return false;
b32 dashes = true, upper = false, lower = false;
for(u64 i = 0; i < text.size; ++i) {
u8 c = text.data[i];
if(c != '-' && c != '=') dashes = false;
if(c >= 'A' && c <= 'Z') upper = true;
if(c >= 'a' && c <= 'z') lower = true;
}
return dashes || (upper && !lower && text.data[0] != ' ');
}
// NOTE: numbered section headings look like "10. EDGES: KINDS, CREATING, ROUTING"
internal String8 help_heading_title(u32 line)
{
String8 text = help_line(line);
if(text.size == 0 || text.data[0] < '0' || text.data[0] > '9' || !help_line_is_heading(line)) return str8_zero();
u64 at = 0;
while(at < text.size && text.data[at] >= '0' && text.data[at] <= '9') ++at;
if(at >= text.size || text.data[at] != '.') return str8_zero();
++at;
while(at < text.size && text.data[at] == ' ') ++at;
return ToString8Size(text.data + at, text.size - at);
}
// NOTE: best match first: a section whose title starts with the topic, then a section title
// containing it, then any line of the body. common words map onto the section they mean.
internal s32 help_find_topic(const char *topic)
{
local_persist const char *aliases[][2] = {
{"keys", "keyboard"}, {"key", "keyboard"}, {"shortcuts", "keyboard"}, {"bindings", "keyboard"},
{"dsl", "the .dit.txt"}, {"format", "the .dit.txt"}, {"syntax", "the .dit.txt"}, {"text", "the .dit.txt"},
{"open", "opening"}, {"files", "opening"}, {"drop", "opening"}, {"drag", "opening"},
{"dnd", "opening"}, {"mac", "platform"}, {"macos", "platform"}, {"windows", "platform"},
{"linux", "platform"}, {"x11", "platform"}, {"wayland", "platform"}, {"vulkan", "troubleshooting"},
{"problems", "troubleshooting"}, {"scan", "scanning"}, {"scanner", "scanning"}, {"theme", "settings"},
{"themes", "settings"}, {"colors", "settings"}, {"fonts", "settings"}, {"save", "saving"},
{"export", "saving"}, {"svg", "saving"}, {"cli", "command line"}, {"options", "command line"},
{"panels", "panels"}, {"tabs", "panels"}, {"views", "the workspace"}, {"workspace", "the workspace"},
{"members", "nodes"}, {"sections", "nodes"}, {"node", "nodes"}, {"edge", "edges"},
{"focus", "layers"}, {"tags", "layers"}, {"mouse", "canvas: mouse"}, {"canvas", "canvas: keyboard"},
{"inspector", "tables"}, {"outline", "tables"}, {"start", "getting started"}, {"modes", "modes"},
{"command", "all commands"}, {"commands", "all commands"}, {"align", "layout"}, {"routing", "edges"},
};
for(u32 index = 0; index < ArrayCount(aliases); ++index) {
if(!strcasecmp(topic, aliases[index][0])) { topic = aliases[index][1]; break; }
}
help_index();
u32 size = (u32)strlen(topic);
if(!size) return -1;
// NOTE: the body starts at the first numbered heading, the intro / contents never match
u32 body = 0;
while(body < help_line_count && help_heading_title(body).size == 0) ++body;
for(u32 line = body; line < help_line_count; ++line) {
String8 title = help_heading_title(line);
if(title.size >= size && strncasecmp((char *)title.data, topic, size) == 0) return (s32)line;
}
for(u32 line = body; line < help_line_count; ++line) {
String8 title = help_heading_title(line);
if(title.size && str8_contains_nocase(title, topic, size)) return (s32)line;
}
for(u32 line = body; line < help_line_count; ++line) {
if(str8_contains_nocase(help_line(line), topic, size)) return (s32)line;
}
return -1;
}
internal void app_open_help(App *app, const char *topic)
{
layout_open(&app->layout, VIEW_HELP);
s32 panel = -1;
s32 index = layout_find_kind(&app->layout, VIEW_HELP, &panel);
if(index < 0) return;
View *view = &app->layout.views[index];
if(topic && topic[0]) {
s32 line = help_find_topic(topic);
if(line < 0) {
app_message(app, "help: nothing about \"%s\"", topic);
return;
}
view->cursor = line;
view->scroll.target = (f32)line * UI_ROW_H;
app_message(app, "help: %s", topic);
} else {
app_message(app, "help: j/k scroll, :help <topic> jumps, Ctrl+w c closes the tab");
}
}
#define UI_MAX_VIEW_TYPES 48
typedef struct BaseView BaseView;
struct BaseView {
u32 kind;
const char *name;
const char *title;
};
global_variable const BaseView ui_base_views[] = {
{VIEW_CANVAS, "canvas", "Canvas"},
{VIEW_OUTLINE, "outline", "Outline"},
{VIEW_INSPECTOR, "inspector", "Inspector"},
{VIEW_EDGES, "edges", "Edges"},
{VIEW_LAYERS, "layers", "Layers"},
{VIEW_PALETTE, "palette", "Palette"},
{VIEW_SOURCE, "source", "Source"},
{VIEW_VIEWS, "views", "Views"},
{VIEW_STATUS, "status", "Status"},
{VIEW_HELP, "help", "Help"},
{VIEW_DOCS, "docs", "Docs"},
};
internal u32 view_types(ViewType *out)
{
u32 count = 0;
for(u32 i = 0; i < ArrayCount(ui_base_views); ++i) out[count++] = ViewType{ui_base_views[i].kind, 0, ui_base_views[i].name};
u32 group = 0;
for(u32 i = 0; i < settings_desc_count() && count < UI_MAX_VIEW_TYPES; ++i) {
if(settings_desc(i)->kind == SETTING_KIND_SECTION) out[count++] = ViewType{VIEW_SETTINGS, group++, settings_desc(i)->name};
}
for(u32 i = 0; i < ArrayCount(profile_group_names) && count < UI_MAX_VIEW_TYPES; ++i) out[count++] = ViewType{VIEW_PROFILER, i, profile_group_names[i]};
return count;
}
internal const char *view_type_name(u32 kind, u32 group)
{
ViewType types[UI_MAX_VIEW_TYPES];
u32 count = view_types(types);
for(u32 i = 0; i < count; ++i) if(types[i].kind == kind && types[i].group == group) return types[i].name;
return "?";
}
internal String8 view_title(Arena *arena, View *view)
{
for(u32 i = 0; i < ArrayCount(ui_base_views); ++i) {
if(ui_base_views[i].kind == view->kind) return str8((char *)ui_base_views[i].title);
}
String8 name = ui_format(arena, "%s", view_type_name(view->kind, view->group));
if(name.size && name.data[0] >= 'a' && name.data[0] <= 'z') name.data[0] -= 32;
return name;
}
internal b32 view_resolve(const char *name, ViewType *out)
{
ViewType types[UI_MAX_VIEW_TYPES];
u32 count = view_types(types);
u32 size = (u32)strlen(name), hits = 0;
if(size == 0) return false;
for(u32 i = 0; i < count; ++i) {
if(!strcasecmp(types[i].name, name)) { *out = types[i]; return true; }
if(!strncasecmp(types[i].name, name, size)) { *out = types[i]; ++hits; }
}
return hits == 1;
}
//- docs, links, clipboard and png export, then the canvas renderer
#include "app_features.cpp"
#include "app_canvas.cpp"
//- watch-style table rows & columns
typedef enum RowKind {
ROW_NODE,
ROW_SECTION,
ROW_MEMBER,
ROW_EDGE,
ROW_LAYER,
ROW_PALETTE_KIND,
ROW_PALETTE_TPL,
ROW_SOURCE_LINE,
ROW_HELP_LINE,
ROW_INSP_FIELD,
ROW_SETTING,
ROW_PROFILER,
ROW_VIEW_TYPE,
ROW_STATUS,
ROW_HEADER,
ROW_DOC_FIELD, // docs view: a = DocField
ROW_DOC_LINE, // docs view: a = offset into the doc text, b = length
} RowKind;
typedef enum DocField {
DOC_FIELD_TITLE,
DOC_FIELD_LINK,
DOC_FIELD_URL,
DOC_FIELD_REPO,
DOC_FIELD_HEADING,
DOC_FIELD_EMPTY,
} DocField;
typedef struct Row Row;
struct Row {
u32 a;
u32 b;
u8 kind;
u8 depth;
u8 expandable;
u8 expanded;
};
#define UI_MAX_ROWS 4096
typedef enum InspField {
INSP_TITLE,
INSP_ROUTING,
INSP_FOCUS,
// node fields
INSP_N_NAME,
INSP_N_KIND,
INSP_N_STEREO,
INSP_N_BORDER,
INSP_N_COLOR,
INSP_N_TAG,
INSP_N_NOTE,
INSP_N_POS,
INSP_N_COLLAPSED,
INSP_N_PINNED,
INSP_N_ADD_SECTION,
// edge fields
INSP_E_SRC,
INSP_E_DST,
INSP_E_KIND,
INSP_E_ROUTING,
INSP_E_SRC_PORT,
INSP_E_DST_PORT,
INSP_E_LABEL,
INSP_E_SRC_MULT,
INSP_E_DST_MULT,
INSP_E_TAG,
INSP_E_COLOR,
// docs & links (appended so the old values keep their numbers)
INSP_N_LINK,
INSP_N_DOC,
INSP_E_DOC,
INSP_E_BENDS,
INSP_DOC,
INSP_REPO,
INSP_REF,
INSP_ROOT,
} InspField;
typedef enum StatusItem {
STATUS_MODE,
STATUS_MESSAGE,
STATUS_SELECTION,
STATUS_STATS,
STATUS_PERF,
STATUS_KEYS,
STATUS_COUNT,
} StatusItem;
typedef struct Column Column;
struct Column {
const char *title;
f32 width;
b32 mono;
};
internal u32 view_columns(ViewKind kind, Column *out)
{
switch(kind) {
case VIEW_OUTLINE:
out[0] = {"Structure", 0.56f, false};
out[1] = {"Kind", 0.22f, true};
out[2] = {"Tag", 0.22f, true};
return 3;
case VIEW_INSPECTOR:
out[0] = {"Field", 0.38f, false};
out[1] = {"Value", 0.44f, true};
out[2] = {"Info", 0.18f, true};
return 3;
case VIEW_EDGES:
out[0] = {"Source", 0.26f, false};
out[1] = {"Rel", 0.12f, true};
out[2] = {"Target", 0.26f, false};
out[3] = {"Label", 0.22f, false};
out[4] = {"Route", 0.14f, true};
return 5;
case VIEW_LAYERS:
out[0] = {"Layer / Tag", 0.44f, true};
out[1] = {"Nodes", 0.18f, true};
out[2] = {"Edges", 0.18f, true};
out[3] = {"State", 0.20f, true};
return 4;
case VIEW_PALETTE:
out[0] = {"Item", 0.46f, false};
out[1] = {"Action", 0.54f, true};
return 2;
case VIEW_SOURCE:
out[0] = {"#", 0.11f, true};
out[1] = {"DSL", 0.89f, true};
return 2;
case VIEW_DOCS:
out[0] = {"Documentation (enter: edit gx: open online gf: open the file)", 1.0f, false};
return 1;
case VIEW_HELP:
out[0] = {"#", 0.08f, true};
out[1] = {"Help (F1, :help <topic>)", 0.92f, true};
return 2;
case VIEW_VIEWS:
out[0] = {"View", 0.45f, false};
out[1] = {"Group", 0.55f, true};
return 2;
case VIEW_SETTINGS:
case VIEW_PROFILER:
out[0] = {"Field", 0.34f, false};
out[1] = {"Value", 0.30f, true};
out[2] = {"Note", 0.36f, false};
return 3;
case VIEW_STATUS:
out[0] = {"Item", 0.28f, false};
out[1] = {"Value", 0.72f, true};
return 2;
default: return 0;
}
}
internal b32 view_expanded(View *view, u32 id)
{
return id < VIEW_EXPANDED_WORDS * 64 && (view->expanded[id >> 6] & (1ull << (id & 63))) != 0;
}
internal void view_set_expanded(View *view, u32 id, b32 on)
{
if(id >= VIEW_EXPANDED_WORDS * 64) return;
if(on) view->expanded[id >> 6] |= (1ull << (id & 63));
else view->expanded[id >> 6] &= ~(1ull << (id & 63));
}
internal void outline_push_node(App *app, View *view, s32 idx, u8 depth, Row *out, u32 *count, u32 max)
{
if(*count >= max) return;
Diagram *dia = app->diagram;
DiagramNode *n = &dia->nodes[idx];
b32 has_children = (n->section_count > 0);
if(!has_children && n->kind == NODE_MODULE) {
for(u32 j = 0; j < dia->node_count; ++j) if(dia->nodes[j].alive && dia->nodes[j].parent == idx) { has_children = true; break; }
}
b32 exp = has_children && (n->kind == NODE_MODULE ? !view_expanded(view, (u32)idx) : view_expanded(view, (u32)idx));
out[(*count)++] = Row{(u32)idx, 0, ROW_NODE, depth, (u8)has_children, (u8)exp};
if(!exp) return;
// children inside container
if(n->kind == NODE_MODULE) {
for(u32 j = 0; j < dia->node_count && *count < max; ++j) {
if(dia->nodes[j].alive && dia->nodes[j].parent == idx) {
outline_push_node(app, view, (s32)j, (u8)(depth + 1), out, count, max);
}
}
}
// sections & members
for(s32 s = n->first_section; s >= 0 && *count < max; s = dia->sections[s].next) {
out[(*count)++] = Row{(u32)s, (u32)idx, ROW_SECTION, (u8)(depth + 1), 0, 0};
for(s32 m = dia->sections[s].first_member; m >= 0 && *count < max; m = dia->members[m].next) {
out[(*count)++] = Row{(u32)m, (u32)idx, ROW_MEMBER, (u8)(depth + 2), 0, 0};
}
}
}
internal u32 view_rows(App *app, View *view, Row *out, u32 max)
{
Diagram *dia = app->diagram;
u32 count = 0;
switch(view->kind) {
case VIEW_OUTLINE: {
for(u32 i = 0; i < dia->node_count && count < max; ++i) {
if(dia->nodes[i].alive && dia->nodes[i].parent < 0) {
outline_push_node(app, view, (s32)i, 0, out, &count, max);
}
}
} break;
case VIEW_INSPECTOR: {
if(app->active_node >= 0 && app->active_node < (s32)dia->node_count && dia->nodes[app->active_node].alive) {
DiagramNode *n = &dia->nodes[app->active_node];
out[count++] = Row{INSP_N_NAME, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_KIND, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_STEREO, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_BORDER, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_COLOR, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_TAG, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_NOTE, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_DOC, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_LINK, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_POS, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_COLLAPSED, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_N_PINNED, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
for(s32 s = n->first_section; s >= 0 && count < max; s = dia->sections[s].next) {
out[count++] = Row{(u32)s, (u32)app->active_node, ROW_SECTION, 0, 1, (u8)!dia->sections[s].collapsed};
if(!dia->sections[s].collapsed) {
for(s32 m = dia->sections[s].first_member; m >= 0 && count < max; m = dia->members[m].next) {
out[count++] = Row{(u32)m, (u32)s, ROW_MEMBER, 1, 0, 0};
}
}
}
if(count < max) out[count++] = Row{INSP_N_ADD_SECTION, (u32)app->active_node, ROW_INSP_FIELD, 0, 0, 0};
} else if(app->active_edge >= 0 && app->active_edge < (s32)dia->edge_count && dia->edges[app->active_edge].alive) {
out[count++] = Row{INSP_E_SRC, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_DST, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_KIND, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_ROUTING, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_SRC_PORT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_DST_PORT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_LABEL, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_SRC_MULT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_DST_MULT, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_TAG, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_COLOR, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_E_DOC, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
if(!diagram_edge_is_message(dia, app->active_edge)) out[count++] = Row{INSP_E_BENDS, (u32)app->active_edge, ROW_INSP_FIELD, 0, 0, 0};
} else {
out[count++] = Row{INSP_TITLE, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_ROUTING, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_FOCUS, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_DOC, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_REPO, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_REF, 0, ROW_INSP_FIELD, 0, 0, 0};
out[count++] = Row{INSP_ROOT, 0, ROW_INSP_FIELD, 0, 0, 0};
}
} break;
case VIEW_EDGES: {
for(u32 i = 0; i < dia->edge_count && count < max; ++i) {
if(dia->edges[i].alive) out[count++] = Row{i, 0, ROW_EDGE, 0, 0, 0};
}
} break;
case VIEW_LAYERS: {
for(u32 i = 0; i < dia->tag_count && count < max; ++i) {
out[count++] = Row{i, 0, ROW_LAYER, 0, 0, 0};
}
} break;
case VIEW_PALETTE: {
out[count++] = Row{0, 0, ROW_HEADER, 0, 0, 0};
for(u32 i = 0; i < NODE_KIND_COUNT && count < max; ++i) out[count++] = Row{i, 0, ROW_PALETTE_KIND, 0, 0, 0};
out[count++] = Row{1, 0, ROW_HEADER, 0, 0, 0};
for(u32 i = 0; i < diagram_template_count() && count < max; ++i) out[count++] = Row{i, 0, ROW_PALETTE_TPL, 0, 0, 0};
} break;
case VIEW_SOURCE: {
// count lines in DSL (cached per frame on scratch during draw)
ScratchArena sc = ScratchGet();
String8 dsl = diagram_to_dsl(sc.arena, dia);
u32 lines = 1;
for(u64 i = 0; i < dsl.size; ++i) if(dsl.data[i] == '\n') ++lines;
for(u32 l = 0; l < lines && count < max; ++l) out[count++] = Row{l, 0, ROW_SOURCE_LINE, 0, 0, 0};
scratch_end(&sc);
} break;
case VIEW_DOCS: {
u32 kind; s32 index;
docs_target(app, &kind, &index);
out[count++] = Row{DOC_FIELD_TITLE, 0, ROW_DOC_FIELD, 0, 0, 0};
if(kind == DOC_TARGET_NODE) {
out[count++] = Row{DOC_FIELD_LINK, 0, ROW_DOC_FIELD, 0, 0, 0};
if(dia->nodes[index].link[0]) out[count++] = Row{DOC_FIELD_URL, 0, ROW_DOC_FIELD, 0, 0, 0};
}
if(kind == DOC_TARGET_DIAGRAM) out[count++] = Row{DOC_FIELD_REPO, 0, ROW_DOC_FIELD, 0, 0, 0};
out[count++] = Row{DOC_FIELD_HEADING, 0, ROW_DOC_FIELD, 0, 0, 0};
String8 doc = doc_target_text(app, kind, index);
if(!doc.size) {
out[count++] = Row{DOC_FIELD_EMPTY, 0, ROW_DOC_FIELD, 0, 0, 0};
} else {
// NOTE: wrapped to the panel: the table column is the whole width, mono-ish estimate
f32 width = view_panel_width(app, view) - 24.0f;
u32 cols = (u32)Max(width / Max(UI_FONT * 0.56f, 4.0f), 16.0f);
ScratchArena sc = ScratchGet();
DocLines lines;
doc_wrap(sc.arena, (const char *)doc.data, (u32)doc.size, cols, &lines);
for(u32 l = 0; l < lines.count && count < max; ++l) out[count++] = Row{lines.start[l], lines.length[l], ROW_DOC_LINE, 0, 0, 0};
scratch_end(&sc);
}
} break;
case VIEW_HELP: {
help_index();
for(u32 l = 0; l < help_line_count && count < max; ++l) out[count++] = Row{l, 0, ROW_HELP_LINE, 0, 0, 0};
} break;
case VIEW_VIEWS: {
ViewType types[UI_MAX_VIEW_TYPES];
u32 n = view_types(types);
for(u32 i = 0; i < n && count < max; ++i) out[count++] = Row{types[i].kind, types[i].group, ROW_VIEW_TYPE, 0, 0, 0};
} break;
case VIEW_SETTINGS: {
u32 cur_group = UINT32_MAX;
for(u32 i = 0; i < settings_desc_count() && count < max; ++i) {
if(settings_desc(i)->kind == SETTING_KIND_SECTION) {
cur_group = (cur_group == UINT32_MAX) ? 0 : cur_group + 1;
continue;
}
if(cur_group == view->group) out[count++] = Row{i, 0, ROW_SETTING, 0, 0, 0};
}
} break;
case VIEW_PROFILER: {
u32 cur_group = UINT32_MAX;
Profiler *p = app->profiler;
for(u32 m = 0; m < PM_COUNT && count < max; ++m) {
if(profile_is_section(m)) {
cur_group = (cur_group == UINT32_MAX) ? 0 : cur_group + 1;
continue;
}
if(cur_group != view->group) continue;
if(m == PM_ARENA && p) {
for(u32 a = 0; a < p->arena_count && count < max; ++a) out[count++] = Row{m, a, ROW_PROFILER, 0, 0, 0};
} else if(m == PM_THREAD && p) {
for(u32 t = 0; t < p->thread_count && count < max; ++t) out[count++] = Row{m, t, ROW_PROFILER, 0, 0, 0};
} else {
out[count++] = Row{m, 0, ROW_PROFILER, 0, 0, 0};
}
}
} break;
case VIEW_STATUS: {
for(u32 i = 0; i < STATUS_COUNT && count < max; ++i) out[count++] = Row{i, 0, ROW_STATUS, 0, 0, 0};
} break;
}
return count;
}
typedef struct Cells Cells;
struct Cells {
String8 text[UI_MAX_COLUMNS];
b32 bright;
b32 dim;
b32 right;
b32 has_swatch;
u32 swatch;
};
internal void view_cells(App *app, View *view, Row *row, Cells *out, Arena *arena, String8 *dsl_cache = NULL)
{
MemSetZero(out, sizeof(*out));
Diagram *dia = app->diagram;
switch(row->kind) {
case ROW_HEADER: {
out->text[0] = str8(row->a == 0 ? "NODE ARCHETYPES" : "TEMPLATES");
out->dim = true;
} break;
case ROW_NODE: {
DiagramNode *n = &dia->nodes[row->a];
out->text[0] = str8(n->name);
out->text[1] = str8((char *)node_kind_name(n->kind));
out->text[2] = str8(n->tag);
out->bright = ((s32)row->a == app->active_node || n->selected);
out->dim = diagram_node_hidden(dia, (s32)row->a);
} break;
case ROW_SECTION: {
NodeSection *s = &dia->sections[row->a];
out->text[0] = ui_format(arena, "[%s]", s->title);
out->text[1] = ui_format(arena, "%u items", s->member_count);
out->text[2] = str8(view->kind == VIEW_INSPECTOR ? "a:add dd:del" : "");
out->dim = true;
} break;
case ROW_MEMBER: {
NodeMember *m = &dia->members[row->a];
out->text[0] = m->visibility ? ui_format(arena, "%c %s", m->visibility, m->name) : str8(m->name);
out->text[1] = str8(m->type);
out->text[2] = str8(m->badge);
} break;
case ROW_EDGE: {
DiagramEdge *e = &dia->edges[row->a];
out->text[0] = str8(dia->nodes[e->src].name);
out->text[1] = str8((char *)edge_kind_symbol(e->kind));
out->text[2] = str8(dia->nodes[e->dst].name);
out->text[3] = str8(e->label);
out->text[4] = str8((char *)edge_routing_name(e->routing == ROUTE_AUTO ? dia->default_routing : e->routing));
out->bright = ((s32)row->a == app->active_edge || e->selected);
} break;
case ROW_LAYER: {
TagState *t = &dia->tags[row->a];
out->text[0] = str8(t->name);
out->text[1] = ui_format(arena, "%u", t->node_count);
out->text[2] = ui_format(arena, "%u", t->edge_count);
out->text[3] = str8(t->hidden ? "hidden" : (t->dimmed ? "dimmed" : "visible"));
out->dim = t->hidden || t->dimmed;
out->bright = !t->hidden && !t->dimmed;
} break;
case ROW_PALETTE_KIND: {
out->text[0] = str8((char *)node_kind_name(row->a));
out->text[1] = ui_format(arena, "enter: insert [%s]", node_kind_badge(row->a));
} break;
case ROW_PALETTE_TPL: {
const DiagramTemplate *t = diagram_template_info(row->a);
out->text[0] = str8((char *)t->name);
out->text[1] = str8((char *)t->description);
} break;
case ROW_DOC_FIELD: {
u32 kind; s32 index;
docs_target(app, &kind, &index);
switch(row->a) {
case DOC_FIELD_TITLE: out->text[0] = doc_target_name(app, arena, kind, index); out->bright = true; break;
case DOC_FIELD_LINK: out->text[0] = dia->nodes[index].link[0] ? ui_format(arena, "link %s", dia->nodes[index].link) : str8((char *)"link - (enter: set one, e.g. src/file.c:42)"); out->dim = !dia->nodes[index].link[0]; break;
case DOC_FIELD_URL: {
char url[1024];
if(diagram_link_url(dia, dia->nodes[index].link, url, sizeof(url))) out->text[0] = ui_format(arena, " -> %s", url);
else {
char path[1024]; u32 line = 0;
diagram_link_local(dia, dia->nodes[index].link, path, sizeof(path), &line);
out->text[0] = ui_format(arena, " -> %s%s", path, file_exists(path) ? "" : " (missing)");
}
out->dim = true;
} break;
case DOC_FIELD_REPO: out->text[0] = dia->repo[0] ? ui_format(arena, "repo %s @ %s", dia->repo, dia->repo_ref[0] ? dia->repo_ref : "HEAD") : str8((char *)"repo - (:repo detect, or :repo <url> [ref])"); out->dim = !dia->repo[0]; break;
case DOC_FIELD_HEADING: out->text[0] = str8((char *)"---- documentation ----"); out->dim = true; break;
case DOC_FIELD_EMPTY: out->text[0] = str8((char *)"(nothing written yet: enter opens the editor)"); out->dim = true; break;
}
} break;
case ROW_DOC_LINE: {
u32 kind; s32 index;
docs_target(app, &kind, &index);
String8 doc = doc_target_text(app, kind, index);
if((u64)row->a + row->b <= doc.size) out->text[0] = ToString8Size(doc.data + row->a, row->b);
} break;
case ROW_HELP_LINE: {
out->text[0] = ui_format(arena, "%4u", row->a + 1);
out->text[1] = help_line(row->a);
// NOTE: headings (all caps lines and their ---- underlines) stand out
out->bright = help_line_is_heading(row->a);
} break;
case ROW_SOURCE_LINE: {
out->text[0] = ui_format(arena, "%4u", row->a + 1);
if(dsl_cache && dsl_cache->size > 0) {
u32 cur = 0;
u64 start = 0;
for(u64 i = 0; i <= dsl_cache->size; ++i) {
if(i == dsl_cache->size || dsl_cache->data[i] == '\n') {
if(cur == row->a) {
out->text[1] = ToString8Size(dsl_cache->data + start, i - start);
break;
}
++cur;
start = i + 1;
}
}
}
} break;
case ROW_INSP_FIELD: {
switch(row->a) {
case INSP_TITLE: out->text[0] = str8("diagram title"); out->text[1] = str8(dia->title); out->text[2] = str8("enter: edit"); break;
case INSP_ROUTING: out->text[0] = str8("default routing"); out->text[1] = str8((char *)edge_routing_name(dia->default_routing)); out->text[2] = str8("h/l"); break;
case INSP_FOCUS: out->text[0] = str8("focus mode"); out->text[1] = str8(dia->focus_mode ? "on" : "off"); out->text[2] = str8("F toggles"); break;
case INSP_N_NAME: out->text[0] = str8("name"); out->text[1] = str8(dia->nodes[row->b].name); out->text[2] = str8("enter"); break;
case INSP_N_KIND: out->text[0] = str8("kind"); out->text[1] = str8((char *)node_kind_name(dia->nodes[row->b].kind)); out->text[2] = str8("h/l"); break;
case INSP_N_STEREO: out->text[0] = str8("stereotype"); out->text[1] = str8(dia->nodes[row->b].stereotype[0] ? dia->nodes[row->b].stereotype : "-"); out->text[2] = str8("enter"); break;
case INSP_N_BORDER: out->text[0] = str8("border"); out->text[1] = str8((char *)border_style_name(dia->nodes[row->b].border_style)); out->text[2] = str8("h/l"); break;
case INSP_N_COLOR: {
u32 c = dia->nodes[row->b].color;
out->text[0] = str8("accent color");
out->text[1] = c ? ui_format(arena, "#%08X", c) : str8("theme default");
out->text[2] = str8("enter / h l");
if(c) { out->has_swatch = true; out->swatch = c; }
} break;
case INSP_N_TAG: out->text[0] = str8("tag / layer"); out->text[1] = str8(dia->nodes[row->b].tag[0] ? dia->nodes[row->b].tag : "-"); out->text[2] = str8("enter"); break;
case INSP_N_NOTE: out->text[0] = str8("note"); out->text[1] = str8(dia->nodes[row->b].note[0] ? dia->nodes[row->b].note : "-"); out->text[2] = str8("enter"); break;
case INSP_N_POS: out->text[0] = str8("geometry"); out->text[1] = ui_format(arena, "%.0f, %.0f (%.0fx%.0f)", dia->nodes[row->b].x, dia->nodes[row->b].y, dia->nodes[row->b].w, dia->nodes[row->b].h); break;
case INSP_N_COLLAPSED: out->text[0] = str8("collapsed"); out->text[1] = str8(dia->nodes[row->b].collapsed ? "yes" : "no"); out->text[2] = str8("h/l"); break;
case INSP_N_PINNED: out->text[0] = str8("pinned"); out->text[1] = str8(dia->nodes[row->b].pinned ? "yes" : "no"); out->text[2] = str8("h/l"); break;
case INSP_N_LINK: {
DiagramNode *n = &dia->nodes[row->b];
out->text[0] = str8("source link");
out->text[1] = str8(n->link[0] ? n->link : "-");
out->text[2] = str8(n->link[0] ? "enter gx gf" : "enter");
} break;
case INSP_N_DOC: {
String8 doc = diagram_doc(dia, dia->nodes[row->b].doc_off, dia->nodes[row->b].doc_len);
u64 first = 0;
while(first < doc.size && doc.data[first] != '\n') ++first;
out->text[0] = str8("documentation");
out->text[1] = doc.size ? ToString8Size(doc.data, first) : str8("-");
out->text[2] = doc.size ? ui_format(arena, "%llu chars", (unsigned long long)doc.size) : str8("enter");
} break;
case INSP_N_ADD_SECTION: out->text[0] = str8("+ add section"); out->text[1] = str8(""); out->text[2] = str8("enter / A"); out->bright = true; break;
case INSP_E_SRC: out->text[0] = str8("source"); out->text[1] = str8(dia->nodes[dia->edges[row->b].src].name); break;
case INSP_E_DST: out->text[0] = str8("target"); out->text[1] = str8(dia->nodes[dia->edges[row->b].dst].name); break;
case INSP_E_KIND: out->text[0] = str8("relationship"); out->text[1] = ui_format(arena, "%s (%s)", edge_kind_name(dia->edges[row->b].kind), edge_kind_symbol(dia->edges[row->b].kind)); out->text[2] = str8("h/l"); break;
case INSP_E_ROUTING: out->text[0] = str8("routing"); out->text[1] = str8((char *)edge_routing_name(dia->edges[row->b].routing)); out->text[2] = str8("h/l"); break;
case INSP_E_SRC_PORT: out->text[0] = str8("source port"); out->text[1] = str8((char *)port_side_name(dia->edges[row->b].src_port)); out->text[2] = str8("h/l"); break;
case INSP_E_DST_PORT: out->text[0] = str8("target port"); out->text[1] = str8((char *)port_side_name(dia->edges[row->b].dst_port)); out->text[2] = str8("h/l"); break;
case INSP_E_LABEL: out->text[0] = str8("label"); out->text[1] = str8(dia->edges[row->b].label[0] ? dia->edges[row->b].label : "-"); out->text[2] = str8("enter"); break;
case INSP_E_SRC_MULT: out->text[0] = str8("src mult"); out->text[1] = str8(dia->edges[row->b].src_mult[0] ? dia->edges[row->b].src_mult : "-"); out->text[2] = str8("enter"); break;
case INSP_E_DST_MULT: out->text[0] = str8("dst mult"); out->text[1] = str8(dia->edges[row->b].dst_mult[0] ? dia->edges[row->b].dst_mult : "-"); out->text[2] = str8("enter"); break;
case INSP_E_TAG: out->text[0] = str8("tag / layer"); out->text[1] = str8(dia->edges[row->b].tag[0] ? dia->edges[row->b].tag : "-"); out->text[2] = str8("enter"); break;
case INSP_E_DOC: {
String8 doc = diagram_doc(dia, dia->edges[row->b].doc_off, dia->edges[row->b].doc_len);
u64 first = 0;
while(first < doc.size && doc.data[first] != '\n') ++first;
out->text[0] = str8("documentation");
out->text[1] = doc.size ? ToString8Size(doc.data, first) : str8("-");
out->text[2] = str8("enter");
} break;
case INSP_E_BENDS:
out->text[0] = str8("bends");
out->text[1] = dia->edges[row->b].waypoint_count ? ui_format(arena, "%u", dia->edges[row->b].waypoint_count) : str8("none (drag the dots on the edge)");
out->text[2] = str8(dia->edges[row->b].waypoint_count ? "enter: clear" : "");
break;
case INSP_DOC: {
out->text[0] = str8("documentation");
out->text[1] = dia->doc_len ? ui_format(arena, "%u chars", dia->doc_len) : str8("-");
out->text[2] = str8("enter");
} break;
case INSP_REPO: out->text[0] = str8("repo url"); out->text[1] = str8(dia->repo[0] ? dia->repo : "-"); out->text[2] = str8("enter / :repo detect"); break;
case INSP_REF: out->text[0] = str8("repo ref"); out->text[1] = str8(dia->repo_ref[0] ? dia->repo_ref : "HEAD"); out->text[2] = str8("enter"); break;
case INSP_ROOT: {
char root[1024];
diagram_link_root(dia, root, sizeof(root));
out->text[0] = str8("links root");
out->text[1] = dia->repo_root[0] ? str8(dia->repo_root) : ui_format(arena, "auto: %s", root);
out->text[2] = str8("enter");
} break;
case INSP_E_COLOR: {
u32 c = dia->edges[row->b].color;
out->text[0] = str8("color");
out->text[1] = c ? ui_format(arena, "#%08X", c) : str8("theme default");
out->text[2] = str8("enter");
if(c) { out->has_swatch = true; out->swatch = c; }
} break;
}
} break;
case ROW_SETTING: {
SettingDesc *desc = settings_desc(row->a);
out->text[0] = str8((char *)desc->name);
out->text[2] = desc->note ? str8((char *)desc->note) : str8("");
if(desc->kind == SETTING_KIND_INFO) {
if(desc->action == SETTING_INFO_FRAME) out->text[1] = ui_format(arena, "%.2f ms (%.0f fps, %u quads)", app->frame_ms, app->fps, app->draw.count);
if(desc->action == SETTING_INFO_DIAGRAM) out->text[1] = ui_format(arena, "%u nodes, %u edges, %u members", dia->live_nodes, dia->live_edges, dia->member_count);
if(desc->action == SETTING_INFO_DATA_DIR) out->text[1] = app->data_dir;
if(desc->action == SETTING_INFO_RELOADS) out->text[1] = ui_format(arena, "%u", app->ui_generation);
} else if(desc->kind == SETTING_KIND_ACTION) {
out->text[1] = str8("run");
out->bright = true;
} else {
out->text[1] = settings_format(arena, &app->settings, desc);
if(desc->kind == SETTING_KIND_COLOR) {
out->has_swatch = true;
out->swatch = *SettingField(&app->settings, desc, u32);
}
}
} break;
case ROW_PROFILER: {
Profiler *p = app->profiler;
out->text[0] = str8((char *)profile_labels[row->a]);
if(!p) break;
char a[32], b[32], c[32];
switch(row->a) {
case PM_FPS: out->text[1] = ui_format(arena, "%.1f fps", app->fps); out->text[2] = ui_format(arena, "%u frames", p->frames_total); break;
case PM_FRAME_CPU: out->text[1] = ui_format(arena, "%.2f ms", p->average); out->text[2] = ui_format(arena, "p50 %.2f p99 %.2f max %.2f", p->p50, p->p99, p->worst); break;
case PM_FRAME_SPLIT: out->text[1] = ui_format(arena, "ui %.2f / vk %.2f ms", p->ui_ms, p->render_ms); out->text[2] = ui_format(arena, "p99 %.2f / %.2f", p->ui_p99, p->render_p99); break;
case PM_UI_SPLIT: out->text[1] = ui_format(arena, "%.0f / %.0f / %.0f us", p->ui_phase_us[0], p->ui_phase_us[1], p->ui_phase_us[3]); out->text[2] = str8("keys / panels+canvas / overlay"); break;
case PM_RENDER_SPLIT: out->text[1] = ui_format(arena, "%.2f / %.2f / %.2f ms", app->renderer.wait_ms, app->renderer.record_ms, app->renderer.submit_ms); out->text[2] = str8("fence / record / submit"); break;
case PM_IDLE: out->text[1] = ui_format(arena, "%.1f ms", p->idle_ms); out->text[2] = str8("asleep in poll()"); break;
case PM_DRAW: out->text[1] = ui_format(arena, "%u quads %u batches", app->draw.count, app->renderer.batches); out->text[2] = ui_format(arena, "peak %u / %u", app->draw.peak, (u32)DRAW_MAX_INSTANCES); break;
case PM_RSS: format_bytes(a, sizeof(a), (f64)p->rss); format_bytes(b, sizeof(b), (f64)p->rss_peak); out->text[1] = str8(a); out->text[2] = ui_format(arena, "peak %s", b); break;
case PM_RSS_KIND: format_bytes(a, sizeof(a), (f64)p->rss_anon); format_bytes(b, sizeof(b), (f64)p->rss_file); format_bytes(c, sizeof(c), (f64)p->rss_shmem); out->text[1] = ui_format(arena, "anon %s", a); out->text[2] = ui_format(arena, "file %s shm %s", b, c); break;
case PM_VIRTUAL: format_bytes(a, sizeof(a), (f64)p->vm_size); format_bytes(b, sizeof(b), (f64)p->vm_lib); out->text[1] = str8(a); out->text[2] = ui_format(arena, "libs %s", b); break;
case PM_STACK_MAIN: format_bytes(a, sizeof(a), (f64)p->vm_stack); out->text[1] = str8(a); break;
case PM_HEAP_LIVE: format_bytes(a, sizeof(a), (f64)Max(AtomicLoadRelaxed(&p->heap_live), (s64)0)); format_bytes(b, sizeof(b), (f64)AtomicLoadRelaxed(&p->heap_peak)); out->text[1] = str8(a); out->text[2] = ui_format(arena, "peak %s", b); break;
case PM_HEAP_ALLOCS: out->text[1] = ui_format(arena, "%llu", (unsigned long long)AtomicLoadRelaxed(&p->malloc_count)); out->text[2] = ui_format(arena, "%.0f /s", p->mallocs_per_s); break;
case PM_HEAP_FREES: out->text[1] = ui_format(arena, "%llu", (unsigned long long)AtomicLoadRelaxed(&p->free_count)); out->text[2] = ui_format(arena, "%.0f /s", p->frees_per_s); break;
case PM_HEAP_BLOCKS: out->text[1] = ui_format(arena, "%lld", (long long)((s64)AtomicLoadRelaxed(&p->malloc_count) - (s64)AtomicLoadRelaxed(&p->free_count))); break;
case PM_HEAP_TOTAL: format_bytes(a, sizeof(a), (f64)AtomicLoadRelaxed(&p->heap_total)); out->text[1] = str8(a); break;
case PM_HEAP_GLIBC: format_bytes(a, sizeof(a), (f64)p->heap_in_use); format_bytes(b, sizeof(b), (f64)p->heap_free); out->text[1] = ui_format(arena, "used %s", a); out->text[2] = ui_format(arena, "free %s", b); break;
case PM_ARENA: {
ProfileArena *ar = &p->arenas[row->b];
out->text[0] = str8(ar->name);
format_bytes(a, sizeof(a), (f64)arena_position(ar->arena));
format_bytes(b, sizeof(b), (f64)ar->arena->capacity);
format_bytes(c, sizeof(c), (f64)ar->arena->peak_position);
out->text[1] = ui_format(arena, "%s / %s", a, b);
out->text[2] = ui_format(arena, "peak %s %.0f/s", c, ar->allocs_per_s);
} break;
case PM_THREAD: {
ProfileThread *th = &p->threads[row->b];
out->text[0] = str8(th->name);
out->text[1] = ui_format(arena, "%.1f%% cpu", th->cpu * 100.0f);
format_bytes(a, sizeof(a), (f64)th->stack_touched);
out->text[2] = ui_format(arena, "stack %s %.0f csw/s", a, th->csw_per_s);
} break;
case PM_CPU: out->text[1] = ui_format(arena, "%.1f%%", p->cpu_process * 100.0f); break;
case PM_FAULTS: out->text[1] = ui_format(arena, "%llu min / %llu maj", (unsigned long long)p->minflt, (unsigned long long)p->majflt); out->text[2] = ui_format(arena, "%.0f /s", p->minflt_per_s); break;
case PM_SWITCHES: out->text[1] = ui_format(arena, "%llu vol / %llu inv", (unsigned long long)p->vcsw, (unsigned long long)p->ivcsw); out->text[2] = ui_format(arena, "%.0f /s", p->csw_per_s); break;
case PM_GPU_MEMORY: format_bytes(a, sizeof(a), (f64)app->renderer.gpu_bytes); out->text[1] = str8(a); break;
case PM_GPU_SWAPCHAIN: out->text[1] = ui_format(arena, "%ux%u (%u imgs)", app->renderer.extent.width, app->renderer.extent.height, app->renderer.image_count); out->text[2] = str8(app->renderer.present_mode == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : "fifo"); break;
case PM_FONT_GLYPHS: out->text[1] = ui_format(arena, "%u / %u", app->fonts.glyph_count, (u32)FONT_GLYPH_SLOTS); out->text[2] = ui_format(arena, "%u faces", app->fonts.face_count); break;
case PM_FONT_RUNS: out->text[1] = ui_format(arena, "%u / %u", app->fonts.run_count, (u32)FONT_RUN_SLOTS); break;
case PM_FONT_ATLAS: out->text[1] = ui_format(arena, "%u%% full", (app->fonts.shelf_y * 100) / FONT_ATLAS_SIZE); out->text[2] = ui_format(arena, "%ux%u R8", (u32)FONT_ATLAS_SIZE, (u32)FONT_ATLAS_SIZE); break;
case PM_DIA_NODES: out->text[1] = ui_format(arena, "%u live (%u visible)", dia->live_nodes, dia->visible_nodes); out->text[2] = ui_format(arena, "cap %u", dia->node_cap); break;
case PM_DIA_EDGES: out->text[1] = ui_format(arena, "%u live (%u visible)", dia->live_edges, dia->visible_edges); out->text[2] = ui_format(arena, "cap %u", dia->edge_cap); break;
case PM_DIA_MEMBERS: out->text[1] = ui_format(arena, "%u sec / %u mem", dia->section_count, dia->member_count); break;
case PM_DIA_LAYOUT: out->text[1] = ui_format(arena, "%.2f ms", dia->layout_ms); break;
case PM_DIA_UNDO: out->text[1] = ui_format(arena, "%u undo / %u redo", dia->undo_count, dia->redo_count); break;
}
} break;
case ROW_VIEW_TYPE: {
out->text[0] = str8((char *)view_type_name(row->a, row->b));
out->text[1] = str8(row->a == VIEW_SETTINGS ? "settings" : (row->a == VIEW_PROFILER ? "profiler" : "workspace"));
} break;
case ROW_STATUS: {
const char *mode = "NORMAL";
if(app->overlay == UI_OVERLAY_SEARCH) mode = "SEARCH";
else if(app->overlay == UI_OVERLAY_COMMAND) mode = "CMD";
else if(app->overlay == UI_OVERLAY_EDIT) mode = "EDIT";
else if(app->overlay == UI_OVERLAY_DOC) mode = "DOC";
else if(app->png.state != PNG_IDLE) mode = "EXPORT";
else if(app->canvas_drag == CANVAS_DRAG_CONNECT) mode = "CONNECT";
else if(dia->focus_mode) mode = "FOCUS";
b32 show_msg = app->message[0] && (time_now_ns() - app->message_ns < (u64)(ui_theme.message_seconds * 1e9));
switch(row->a) {
case STATUS_MODE:
out->text[0] = str8("mode");
out->text[1] = str8((char *)mode);
out->bright = true;
break;
case STATUS_MESSAGE:
out->text[0] = str8("message");
out->text[1] = show_msg ? str8(app->message) : str8("a:add e:edge-kind L:layout f:fit F:focus /:search :cmd");
out->dim = !show_msg;
break;
case STATUS_SELECTION:
out->text[0] = str8("selection");
if(app->active_node >= 0 && dia->nodes[app->active_node].alive) {
u32 sc = diagram_selected_node_count(dia);
out->text[1] = sc > 1 ? ui_format(arena, "%s (+%u)", dia->nodes[app->active_node].name, sc - 1)
: ui_format(arena, "%s [%s]", dia->nodes[app->active_node].name, node_kind_name(dia->nodes[app->active_node].kind));
} else if(app->active_edge >= 0 && dia->edges[app->active_edge].alive) {
DiagramEdge *e = &dia->edges[app->active_edge];
out->text[1] = ui_format(arena, "%s %s %s", dia->nodes[e->src].name, edge_kind_symbol(e->kind), dia->nodes[e->dst].name);
}
out->right = true;
break;
case STATUS_STATS:
out->text[0] = str8("diagram");
out->text[1] = ui_format(arena, "edge:%s %un %ue", edge_kind_symbol(app->canvas_connect_kind), dia->live_nodes, dia->live_edges);
out->right = true;
out->dim = true;
break;
case STATUS_PERF:
out->text[0] = str8("perf");
out->text[1] = ui_format(arena, "%.2fms %uq", app->frame_ms, app->draw.count);
out->right = true;
out->dim = true;
break;
case STATUS_KEYS:
out->text[0] = str8("keys");
if(app->count || app->pending) {
if(app->count && app->pending) out->text[1] = ui_format(arena, "%u%c", app->count, (char)app->pending);
else if(app->count) out->text[1] = ui_format(arena, "%u", app->count);
else out->text[1] = ui_format(arena, "%c", (char)app->pending);
}
out->right = true;
break;
}
} break;
}
}
//- table actions (activate, adjust, inline edit)
internal void start_edit(App *app, EditTargetKind kind, s32 index, const char *initial)
{
app->edit_kind = kind;
app->edit_index = index;
app->edit_inline = app->card_focused;
app->overlay = UI_OVERLAY_EDIT;
app->overlay_opened = true;
snprintf(app->input, sizeof(app->input), "%s", initial ? initial : "");
app->input_size = (u32)strlen(app->input);
}
internal void edit_accept(App *app)
{
Diagram *dia = app->diagram;
char text[sizeof(app->input) + 1];
MemCpy(text, app->input, app->input_size);
text[app->input_size] = 0;
switch(app->edit_kind) {
case EDIT_NODE_LINK:
if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->nodes[app->edit_index].link, sizeof(dia->nodes[app->edit_index].link), "%s", dsl_skip_ws(text));
dia->dirty = true;
++dia->version;
}
break;
case EDIT_DIAGRAM_REPO:
snprintf(dia->repo, sizeof(dia->repo), "%s", dsl_skip_ws(text));
dia->dirty = true;
break;
case EDIT_DIAGRAM_REF:
snprintf(dia->repo_ref, sizeof(dia->repo_ref), "%s", dsl_skip_ws(text));
dia->dirty = true;
break;
case EDIT_DIAGRAM_ROOT:
snprintf(dia->repo_root, sizeof(dia->repo_root), "%s", dsl_skip_ws(text));
dia->dirty = true;
break;
case EDIT_SETTING:
if(!settings_parse(&app->settings, settings_desc((u32)app->edit_index), text)) app_message(app, "invalid value: %s", text);
app_apply_settings(app);
break;
case EDIT_DIAGRAM_TITLE:
snprintf(dia->title, sizeof(dia->title), "%s", text);
dia->dirty = true;
break;
case EDIT_NODE_NAME:
if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->nodes[app->edit_index].name, sizeof(dia->nodes[app->edit_index].name), "%s", text);
diagram_measure_node(dia, app->edit_index);
diagram_fit_containers(dia);
++dia->version;
}
break;
case EDIT_NODE_STEREO:
if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->nodes[app->edit_index].stereotype, sizeof(dia->nodes[app->edit_index].stereotype), "%s", text);
diagram_measure_node(dia, app->edit_index);
++dia->version;
}
break;
case EDIT_NODE_TAG:
if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->nodes[app->edit_index].tag, sizeof(dia->nodes[app->edit_index].tag), "%s", text);
diagram_rebuild_tags(dia);
++dia->version;
}
break;
case EDIT_NODE_NOTE:
if(app->edit_index >= 0 && dia->nodes[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->nodes[app->edit_index].note, sizeof(dia->nodes[app->edit_index].note), "%s", text);
diagram_measure_node(dia, app->edit_index);
++dia->version;
}
break;
case EDIT_NODE_COLOR:
case EDIT_EDGE_COLOR: {
u32 val = 0;
if(text[0] && strcmp(text, "default") != 0 && strcmp(text, "0") != 0) {
const char *at = (text[0] == '#') ? text + 1 : text;
val = (u32)strtoul(at, NULL, 16);
if(strlen(at) == 6) val = (val << 8) | 0xFF;
}
diagram_push_undo(dia);
if(app->edit_kind == EDIT_NODE_COLOR && app->edit_index >= 0) dia->nodes[app->edit_index].color = val;
if(app->edit_kind == EDIT_EDGE_COLOR && app->edit_index >= 0) dia->edges[app->edit_index].color = val;
++dia->version;
} break;
case EDIT_EDGE_LABEL:
if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->edges[app->edit_index].label, sizeof(dia->edges[app->edit_index].label), "%s", text);
++dia->version;
}
break;
case EDIT_EDGE_SRC_MULT:
if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->edges[app->edit_index].src_mult, sizeof(dia->edges[app->edit_index].src_mult), "%s", text);
++dia->version;
}
break;
case EDIT_EDGE_DST_MULT:
if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->edges[app->edit_index].dst_mult, sizeof(dia->edges[app->edit_index].dst_mult), "%s", text);
++dia->version;
}
break;
case EDIT_EDGE_TAG:
if(app->edit_index >= 0 && dia->edges[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->edges[app->edit_index].tag, sizeof(dia->edges[app->edit_index].tag), "%s", text);
diagram_rebuild_tags(dia);
++dia->version;
}
break;
case EDIT_SECTION_TITLE:
if(app->edit_index >= 0 && dia->sections[app->edit_index].alive) {
diagram_push_undo(dia);
snprintf(dia->sections[app->edit_index].title, sizeof(dia->sections[app->edit_index].title), "%s", text);
diagram_measure_node(dia, dia->sections[app->edit_index].node);
++dia->version;
}
break;
case EDIT_MEMBER_LINE:
if(app->edit_index >= 0 && dia->members[app->edit_index].alive) {
diagram_push_undo(dia);
NodeMember *m = &dia->members[app->edit_index];
const char *p = dsl_skip_ws(text);
m->visibility = 0;
if((*p == '+' || *p == '-' || *p == '#' || *p == '~' || *p == '@' || *p == '*') && (p[1] == ' ' || p[1] == '\t')) {
m->visibility = *p;
p = dsl_skip_ws(p + 1);
}
char work[sizeof(app->input) + 1];
snprintf(work, sizeof(work), "%s", p);
m->badge[0] = 0;
m->type[0] = 0;
char *lb = strrchr(work, '['), *rb = lb ? strchr(lb, ']') : NULL;
if(lb && rb) { *lb = 0; *rb = 0; snprintf(m->badge, sizeof(m->badge), "%s", lb + 1); }
char *colon = strchr(work, ':');
if(colon) {
*colon = 0;
snprintf(m->type, sizeof(m->type), "%s", dsl_skip_ws(colon + 1));
u32 tl = (u32)strlen(m->type);
while(tl > 0 && m->type[tl - 1] == ' ') m->type[--tl] = 0;
}
snprintf(m->name, sizeof(m->name), "%s", work);
u32 nl = (u32)strlen(m->name);
while(nl > 0 && m->name[nl - 1] == ' ') m->name[--nl] = 0;
diagram_measure_node(dia, dia->sections[m->section].node);
diagram_fit_containers(dia);
++dia->version;
}
break;
}
}
internal void view_adjust(App *app, View *view, Row *row, s32 dir)
{
Diagram *dia = app->diagram;
if(row->kind == ROW_SETTING) {
settings_adjust(&app->settings, settings_desc(row->a), dir);
app_apply_settings(app);
return;
}
if(row->kind == ROW_INSP_FIELD) {
switch(row->a) {
case INSP_ROUTING:
dia->default_routing = (dia->default_routing - 1 + 3 + (u32)dir) % 3 + 1;
app->settings.default_routing = dia->default_routing;
break;
case INSP_FOCUS: dia->focus_mode = !dia->focus_mode; break;
case INSP_N_KIND: {
diagram_push_undo(dia);
DiagramNode *n = &dia->nodes[row->b];
n->kind = (n->kind + NODE_KIND_COUNT + (u32)dir) % NODE_KIND_COUNT;
diagram_measure_node(dia, (s32)row->b);
} break;
case INSP_N_BORDER: {
diagram_push_undo(dia);
DiagramNode *n = &dia->nodes[row->b];
n->border_style = (n->border_style + BORDER_STYLE_COUNT + (u32)dir) % BORDER_STYLE_COUNT;
} break;
case INSP_N_COLLAPSED: diagram_push_undo(dia); dia->nodes[row->b].collapsed = !dia->nodes[row->b].collapsed; diagram_measure_node(dia, (s32)row->b); break;
case INSP_N_PINNED: dia->nodes[row->b].pinned = !dia->nodes[row->b].pinned; break;
case INSP_E_KIND: diagram_push_undo(dia); dia->edges[row->b].kind = (dia->edges[row->b].kind + EDGE_KIND_COUNT + (u32)dir) % EDGE_KIND_COUNT; break;
case INSP_E_ROUTING: diagram_push_undo(dia); dia->edges[row->b].routing = (dia->edges[row->b].routing + ROUTE_COUNT + (u32)dir) % ROUTE_COUNT; break;
case INSP_E_SRC_PORT: diagram_push_undo(dia); dia->edges[row->b].src_port = (dia->edges[row->b].src_port + PORT_COUNT + (u32)dir) % PORT_COUNT; break;
case INSP_E_DST_PORT: diagram_push_undo(dia); dia->edges[row->b].dst_port = (dia->edges[row->b].dst_port + PORT_COUNT + (u32)dir) % PORT_COUNT; break;
}
++dia->version;
return;
}
if(row->kind == ROW_EDGE) {
diagram_push_undo(dia);
dia->edges[row->a].kind = (dia->edges[row->a].kind + EDGE_KIND_COUNT + (u32)dir) % EDGE_KIND_COUNT;
++dia->version;
}
}
internal void view_activate(App *app, View *view, Row *row)
{
Diagram *dia = app->diagram;
switch(row->kind) {
case ROW_NODE: {
diagram_clear_selection(dia);
dia->nodes[row->a].selected = true;
app->active_node = (s32)row->a;
app->active_edge = -1;
app->pan_to_active = true;
} break;
case ROW_EDGE: {
diagram_clear_selection(dia);
dia->edges[row->a].selected = true;
app->active_edge = (s32)row->a;
app->active_node = -1;
} break;
case ROW_LAYER: {
dia->tags[row->a].hidden = !dia->tags[row->a].hidden;
} break;
case ROW_PALETTE_KIND: {
View *cv = find_canvas_view(app);
f32 wx = cv ? cv->cam_x : 200.0f;
f32 wy = cv ? cv->cam_y : 200.0f;
diagram_push_undo(dia);
s32 idx = diagram_add_node(dia, (NodeKind)row->a, node_kind_name(row->a), wx - 110.0f, wy - 40.0f);
if(idx >= 0) {
if(row->a == NODE_STRUCT || row->a == NODE_CLASS) {
s32 s = diagram_add_section(dia, idx, "fields");
diagram_add_member(dia, s, '+', "id", "u64", "");
} else if(row->a == NODE_INTERFACE) {
s32 s = diagram_add_section(dia, idx, "methods");
diagram_add_member(dia, s, '+', "execute()", "void", "pure");
} else if(row->a == NODE_ENUM) {
s32 s = diagram_add_section(dia, idx, "variants");
diagram_add_member(dia, s, 0, "None", "", "");
}
diagram_clear_selection(dia);
dia->nodes[idx].selected = true;
app->active_node = idx;
app->active_edge = -1;
app_message(app, "added %s", node_kind_name(row->a));
}
} break;
case ROW_PALETTE_TPL: {
const DiagramTemplate *t = diagram_template_info(row->a);
diagram_push_undo(dia);
diagram_load_template(dia, t->name);
app->active_node = -1;
app->active_edge = -1;
for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true;
app_message(app, "loaded template: %s (%u nodes, %u edges)", t->name, dia->live_nodes, dia->live_edges);
} break;
case ROW_VIEW_TYPE:
layout_open(&app->layout, (ViewKind)row->a, row->b);
break;
case ROW_DOC_FIELD:
case ROW_DOC_LINE: {
u32 kind; s32 index;
docs_target(app, &kind, &index);
if(row->kind == ROW_DOC_FIELD && row->a == DOC_FIELD_LINK) start_edit(app, EDIT_NODE_LINK, index, dia->nodes[index].link);
else if(row->kind == ROW_DOC_FIELD && row->a == DOC_FIELD_URL) app_open_link(app, index, true);
else if(row->kind == ROW_DOC_FIELD && row->a == DOC_FIELD_REPO) start_edit(app, EDIT_DIAGRAM_REPO, 0, dia->repo);
else doc_edit_open(app, (DocTargetKind)kind, index);
} break;
case ROW_SECTION:
if(view->kind == VIEW_INSPECTOR) {
start_edit(app, EDIT_SECTION_TITLE, (s32)row->a, dia->sections[row->a].title);
}
break;
case ROW_MEMBER: {
NodeMember *m = &dia->members[row->a];
char buf[160];
snprintf(buf, sizeof(buf), "%s%s%s%s%s%s%s",
m->visibility ? (char[3]){m->visibility, ' ', 0} : "",
m->name,
m->type[0] ? " : " : "", m->type,
m->badge[0] ? " [" : "", m->badge, m->badge[0] ? "]" : "");
start_edit(app, EDIT_MEMBER_LINE, (s32)row->a, buf);
} break;
case ROW_INSP_FIELD: {
switch(row->a) {
case INSP_TITLE: start_edit(app, EDIT_DIAGRAM_TITLE, 0, dia->title); break;
case INSP_N_NAME: start_edit(app, EDIT_NODE_NAME, (s32)row->b, dia->nodes[row->b].name); break;
case INSP_N_STEREO: start_edit(app, EDIT_NODE_STEREO, (s32)row->b, dia->nodes[row->b].stereotype); break;
case INSP_N_TAG: start_edit(app, EDIT_NODE_TAG, (s32)row->b, dia->nodes[row->b].tag); break;
case INSP_N_NOTE: start_edit(app, EDIT_NODE_NOTE, (s32)row->b, dia->nodes[row->b].note); break;
case INSP_N_COLOR: {
char hex[16] = {};
if(dia->nodes[row->b].color) snprintf(hex, sizeof(hex), "#%08X", dia->nodes[row->b].color);
start_edit(app, EDIT_NODE_COLOR, (s32)row->b, hex);
} break;
case INSP_N_ADD_SECTION: {
diagram_push_undo(dia);
s32 s = diagram_add_section(dia, (s32)row->b, "fields");
if(s >= 0) start_edit(app, EDIT_SECTION_TITLE, s, "fields");
} break;
case INSP_E_LABEL: start_edit(app, EDIT_EDGE_LABEL, (s32)row->b, dia->edges[row->b].label); break;
case INSP_N_LINK: start_edit(app, EDIT_NODE_LINK, (s32)row->b, dia->nodes[row->b].link); break;
case INSP_N_DOC: doc_edit_open(app, DOC_TARGET_NODE, (s32)row->b); break;
case INSP_E_DOC: doc_edit_open(app, DOC_TARGET_EDGE, (s32)row->b); break;
case INSP_DOC: doc_edit_open(app, DOC_TARGET_DIAGRAM, -1); break;
case INSP_REPO: start_edit(app, EDIT_DIAGRAM_REPO, 0, dia->repo); break;
case INSP_REF: start_edit(app, EDIT_DIAGRAM_REF, 0, dia->repo_ref); break;
case INSP_ROOT: start_edit(app, EDIT_DIAGRAM_ROOT, 0, dia->repo_root); break;
case INSP_E_BENDS:
if(dia->edges[row->b].waypoint_count) {
diagram_push_undo(dia);
dia->edges[row->b].waypoint_count = 0;
++dia->version;
app_message(app, "bends cleared");
}
break;
case INSP_E_SRC_MULT: start_edit(app, EDIT_EDGE_SRC_MULT, (s32)row->b, dia->edges[row->b].src_mult); break;
case INSP_E_DST_MULT: start_edit(app, EDIT_EDGE_DST_MULT, (s32)row->b, dia->edges[row->b].dst_mult); break;
case INSP_E_TAG: start_edit(app, EDIT_EDGE_TAG, (s32)row->b, dia->edges[row->b].tag); break;
case INSP_E_COLOR: {
char hex[16] = {};
if(dia->edges[row->b].color) snprintf(hex, sizeof(hex), "#%08X", dia->edges[row->b].color);
start_edit(app, EDIT_EDGE_COLOR, (s32)row->b, hex);
} break;
default: view_adjust(app, view, row, 1); break;
}
} break;
case ROW_SETTING: {
SettingDesc *desc = settings_desc(row->a);
if(desc->kind == SETTING_KIND_ACTION) {
if(desc->action == SETTING_ACTION_RESET_LAYOUT) ui_layout_default(&app->layout);
if(desc->action == SETTING_ACTION_RESET_SETTINGS) { settings_default(&app->settings); app_apply_settings(app); }
if(desc->action == SETTING_ACTION_RESET_COLORS) settings_apply_scheme(&app->settings, app->settings.scheme < UI_SCHEME_CUSTOM ? app->settings.scheme : UI_SCHEME_MONO_DARK);
} else if(desc->kind == SETTING_KIND_BOOL || desc->kind == SETTING_KIND_ENUM || desc->kind == SETTING_KIND_CHOICE || desc->kind == SETTING_KIND_FONT) {
settings_adjust(&app->settings, desc, 1);
app_apply_settings(app);
} else if(desc->kind == SETTING_KIND_F32 || desc->kind == SETTING_KIND_U32 || desc->kind == SETTING_KIND_COLOR) {
ScratchArena sc = ScratchGet();
String8 val = settings_format(sc.arena, &app->settings, desc);
char buf[64];
snprintf(buf, sizeof(buf), Str8Fmt, Str8Arg(val));
scratch_end(&sc);
start_edit(app, EDIT_SETTING, (s32)row->a, buf);
}
} break;
}
}
//- floating inspector
// NOTE: a small card next to the selection on the canvas, so editing a node doesn't send your eyes
// (and the keyboard focus) to the other side of the window. it is the Inspector itself: same rows,
// cells, activate and adjust, minus the fields that only make sense in the full panel. edits run
// inline in the row. zero allocations: rows live on the scratch arena for the frame.
#define CARD_WIDTH 296.0f
#define CARD_MAX_ROWS 12
#define CARD_GAP 14.0f
internal View *focused_view(App *app);
internal b32 card_keeps_field(u32 field)
{
switch(field) {
case INSP_N_BORDER: case INSP_N_POS: case INSP_N_COLLAPSED: case INSP_N_PINNED:
case INSP_E_SRC: case INSP_E_DST: case INSP_E_SRC_PORT: case INSP_E_DST_PORT:
return false;
}
return true;
}
internal b32 card_field_is_choice(u32 field)
{
return field == INSP_N_KIND || field == INSP_E_KIND || field == INSP_E_ROUTING;
}
internal u32 card_rows(App *app, Row *out, u32 max)
{
u32 count = view_rows(app, &app->card_view, out, max);
u32 kept = 0;
for(u32 index = 0; index < count; ++index) {
if(out[index].kind == ROW_INSP_FIELD && !card_keeps_field(out[index].a)) continue;
out[kept++] = out[index];
}
return kept;
}
internal u32 card_target_of(App *app)
{
Diagram *dia = app->diagram;
if(app->active_node >= 0 && app->active_node < (s32)dia->node_count && dia->nodes[app->active_node].alive) return (u32)app->active_node + 1;
if(app->active_edge >= 0 && app->active_edge < (s32)dia->edge_count && dia->edges[app->active_edge].alive) return ((u32)app->active_edge + 1) | CARD_EDGE_BIT;
return 0;
}
// NOTE: which row the running inline edit belongs to
internal b32 card_row_is_edit(App *app, Row *row)
{
s32 index = app->edit_index;
u32 field = 0;
switch(app->edit_kind) {
case EDIT_MEMBER_LINE: return row->kind == ROW_MEMBER && (s32)row->a == index;
case EDIT_SECTION_TITLE: return row->kind == ROW_SECTION && (s32)row->a == index;
case EDIT_NODE_NAME: field = INSP_N_NAME; break;
case EDIT_NODE_STEREO: field = INSP_N_STEREO; break;
case EDIT_NODE_TAG: field = INSP_N_TAG; break;
case EDIT_NODE_NOTE: field = INSP_N_NOTE; break;
case EDIT_NODE_COLOR: field = INSP_N_COLOR; break;
case EDIT_EDGE_LABEL: field = INSP_E_LABEL; break;
case EDIT_EDGE_SRC_MULT: field = INSP_E_SRC_MULT; break;
case EDIT_EDGE_DST_MULT: field = INSP_E_DST_MULT; break;
case EDIT_EDGE_TAG: field = INSP_E_TAG; break;
case EDIT_EDGE_COLOR: field = INSP_E_COLOR; break;
default: return false;
}
return row->kind == ROW_INSP_FIELD && row->a == field && (s32)row->b == index;
}
// NOTE: where the card goes on this canvas, or false when it doesn't show. draw_canvas calls it twice:
// before its mouse handling, so clicks on the card never reach the diagram underneath, and again after
// it, so the card follows this frame's selection, drag and snap. the app only redraws on input, a
// stale decision would stick until the next mouse move. pure apart from the target reset.
internal b32 card_place(App *app, PlatformInput *input, s32 view_index, View *view, Rect rect)
{
Diagram *dia = app->diagram;
// one card, on the focused canvas, else the first one
View *home = focused_view(app);
if(!home || home->kind != VIEW_CANVAS) home = find_canvas_view(app);
if(home != view) return false;
u32 target = card_target_of(app);
if(target != app->card_target) {
app->card_target = target;
app->card_dismissed = 0;
app->card_focused = false;
app->card_view.kind = VIEW_INSPECTOR;
app->card_view.cursor = 0;
app->card_view.scroll = ScrollList{};
}
// NOTE: pressing a node already starts a node drag; get out of the way only once it really moves.
// panning keeps the card, it follows the anchor
b32 moved = fabsf(input->mouse_x - app->canvas_press_x) > 3.0f || fabsf(input->mouse_y - app->canvas_press_y) > 3.0f;
b32 dragging = app->canvas_drag != CANVAS_DRAG_NONE && app->canvas_drag != CANVAS_DRAG_PAN &&
app->canvas_drag_view == view_index && moved;
b32 show = ui_theme.floating_inspector && target && app->card_dismissed != target && !dragging &&
rect_w(rect) >= 360.0f && rect_h(rect) >= 200.0f;
//- anchor: the node's screen box, or the edge's label point
Rect anchor = {};
if(show && !(target & CARD_EDGE_BIT)) {
s32 node = (s32)target - 1;
DiagramNode *n = &dia->nodes[node];
Vec2f a = canvas_w2s(view, rect, n->x, n->y);
Vec2f b = canvas_w2s(view, rect, n->x + n->w, n->y + n->h);
anchor = Rect{a.x, a.y, b.x, b.y};
if(diagram_node_hidden(dia, node) || diagram_selected_node_count(dia) > 1) show = false;
} else if(show) {
s32 edge = (s32)(target & ~CARD_EDGE_BIT) - 1;
EdgePath path;
if(diagram_edge_hidden(dia, edge) || !diagram_edge_route(dia, edge, &path)) show = false;
else {
Vec2f p = canvas_w2s(view, rect, path.label_pos.x, path.label_pos.y);
anchor = Rect{p.x - 4.0f, p.y - 4.0f, p.x + 4.0f, p.y + 4.0f};
}
}
if(show && (anchor.x1 < rect.x0 || anchor.x0 > rect.x1 || anchor.y1 < rect.y0 || anchor.y0 > rect.y1)) show = false;
if(!show) {
app->card_focused = false;
return false;
}
ScratchArena scratch = ScratchGet();
Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS);
u32 count = card_rows(app, rows, UI_MAX_ROWS);
scratch_end(&scratch);
f32 head = UI_ROW_H + 6.0f;
f32 foot = UI_FONT_SMALL + 12.0f;
f32 w = Min(CARD_WIDTH, rect_w(rect) - 16.0f);
f32 h = Min(head + (f32)Min(count, (u32)CARD_MAX_ROWS) * UI_ROW_H + foot, rect_h(rect) - 44.0f);
// right of the anchor, else left of it, else on top of it, always inside the canvas
f32 x = anchor.x1 + CARD_GAP;
if(x + w > rect.x1 - 8.0f) x = anchor.x0 - CARD_GAP - w;
if(x < rect.x0 + 8.0f) x = Clamp(anchor.x1 + CARD_GAP, rect.x0 + 8.0f, rect.x1 - 8.0f - w);
f32 y = Clamp(anchor.y0, rect.y0 + 36.0f, rect.y1 - 8.0f - h);
app->card_rect = Rect{x, y, x + w, y + h};
return true;
}
internal void card_draw_input(App *app, Rect field)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
String8 text = ToString8Size(app->input, app->input_size);
f32 tw = font_measure(fonts, FONT_MONO, UI_FONT, text);
f32 shift = Max(tw - (rect_w(field) - 14.0f), 0.0f); // keep the caret in view
draw_rect(d, field, COLOR_BG, 0);
draw_border(d, field, COLOR_ACCENT, 0, 1);
draw_clip_push(d, rect_shrink(field, 1));
f32 x = field.x0 + 6.0f - shift;
draw_text(d, fonts, FONT_MONO, UI_FONT, x, ui_baseline(app, FONT_MONO, UI_FONT, field.y0, rect_h(field)), text, COLOR_TEXT);
draw_rect(d, Rect{x + tw + 1.0f, field.y0 + 4.0f, x + tw + 2.5f, field.y1 - 4.0f}, COLOR_ACCENT, 0);
draw_clip_pop(d);
app->edit_drawn = true;
}
internal void card_draw(App *app, PlatformInput *input, s32 panel, Rect canvas, b32 owns_mouse)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
Diagram *dia = app->diagram;
View *cv = &app->card_view;
Rect box = app->card_rect;
b32 editing = (app->overlay == UI_OVERLAY_EDIT && app->edit_inline);
// NOTE: clicks only count when the card was under the mouse before the canvas handled it, else a
// click that just selected a node could land on that node's freshly placed card too
b32 live = (app->overlay == UI_OVERLAY_NONE) && owns_mouse;
ScratchArena scratch = ScratchGet();
Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS);
u32 count = card_rows(app, rows, UI_MAX_ROWS);
cv->row_count = count;
cv->cursor = count ? Clamp(cv->cursor, 0, (s32)count - 1) : 0;
f32 row_h = UI_ROW_H;
f32 pad = 10.0f;
Rect head = {box.x0, box.y0, box.x1, box.y0 + row_h + 6.0f};
Rect foot = {box.x0, box.y1 - (UI_FONT_SMALL + 12.0f), box.x1, box.y1};
Rect body = {box.x0, head.y1, box.x1, foot.y0};
f32 lx = body.x0 + pad; // label column
f32 vx = body.x0 + rect_w(body) * 0.38f; // value column
f32 rx = body.x1 - pad; // right edge of the content
b32 over = owns_mouse && ui_hovered(input, box);
draw_shadow(d, box, 0x00000099u, 0, 18);
draw_rect(d, box, COLOR_PANEL, 0);
//- header: what this is, and a close button
{
draw_rect(d, head, COLOR_HEADER, 0);
String8 title = {};
if(app->card_target & CARD_EDGE_BIT) {
DiagramEdge *e = &dia->edges[(app->card_target & ~CARD_EDGE_BIT) - 1];
title = ui_format(scratch.arena, "%s %s %s", dia->nodes[e->src].name, edge_kind_symbol(e->kind), dia->nodes[e->dst].name);
} else {
DiagramNode *n = &dia->nodes[app->card_target - 1];
title = (n->parent >= 0) ? ui_format(scratch.arena, "%s in %s", node_kind_badge(n->kind), dia->nodes[n->parent].name)
: str8((char *)node_kind_badge(n->kind));
}
Rect close = {head.x1 - rect_h(head), head.y0, head.x1, head.y1};
b32 hot = live && ui_hovered(input, close);
if(hot) {
draw_rect(d, close, COLOR_HOVER, 0);
app->canvas_cursor = PLATFORM_CURSOR_POINTER;
if(ButtonPressed(input->mouse[0])) {
app->card_dismissed = app->card_target;
app->card_focused = false;
}
}
f32 base = ui_baseline(app, FONT_MONO, UI_FONT_SMALL, head.y0, rect_h(head));
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, lx, base, close.x0 - lx - 4.0f, title, COLOR_TEXT_DIM);
f32 cx = (close.x0 + close.x1) * 0.5f, cy = (close.y0 + close.y1) * 0.5f;
draw_line(d, cx - 4.0f, cy - 4.0f, cx + 4.0f, cy + 4.0f, 1.2f, hot ? COLOR_TEXT : COLOR_TEXT_DIM);
draw_line(d, cx - 4.0f, cy + 4.0f, cx + 4.0f, cy - 4.0f, 1.2f, hot ? COLOR_TEXT : COLOR_TEXT_DIM);
draw_rect(d, Rect{head.x0, head.y1 - 1, head.x1, head.y1}, COLOR_PANEL_EDGE, 0);
}
//- rows
f32 view_h = Max(rect_h(body), 1.0f);
cv->visible_rows = (u32)Max(floorf(view_h / row_h), 1.0f);
if(over && input->scroll_y != 0.0f) cv->scroll.target += input->scroll_y * row_h * (f32)ui_theme.scroll_lines;
// follow the row being edited (a member just added with 'a' sits at the end of its section)
if(editing) {
for(u32 index = 0; index < count; ++index) {
if(!card_row_is_edit(app, &rows[index])) continue;
if((s32)index != cv->cursor) {
cv->cursor = (s32)index;
f32 y = (f32)index * row_h;
if(y < cv->scroll.target) cv->scroll.target = y;
if(y + row_h > cv->scroll.target + view_h) cv->scroll.target = y + row_h - view_h;
cv->scroll.offset = cv->scroll.target; // jump, the field has to be on screen this frame
}
break;
}
}
scroll_update(&cv->scroll, (f32)count * row_h, view_h, app->dt, &app->animating);
u32 first = (u32)Max(floorf(cv->scroll.offset / row_h), 0.0f);
u32 last = Min(count, first + cv->visible_rows + 2);
Rect edit_field = {};
Cells cells;
draw_clip_push(d, body);
for(u32 index = first; index < last; ++index) {
Row *row = &rows[index];
f32 y = body.y0 + (f32)index * row_h - cv->scroll.offset;
Rect r = {body.x0, y, body.x1, y + row_h};
b32 hov = live && over && ui_hovered(input, rect_intersect(r, body));
b32 sel = app->card_focused && (s32)index == cv->cursor;
b32 edit = editing && card_row_is_edit(app, row);
f32 base = ui_baseline(app, FONT_REGULAR, UI_FONT, y, row_h);
f32 mono = ui_baseline(app, FONT_MONO, UI_FONT, y, row_h);
Rect hit = {}; // the row's small button: + on a section, x on a member
s32 dir = 0; // choice fields: which arrow the mouse is on
view_cells(app, cv, row, &cells, scratch.arena);
if(sel) draw_rect(d, r, COLOR_SELECTED, 0);
else if(hov) draw_rect(d, r, COLOR_HOVER, 0);
switch(row->kind) {
case ROW_INSP_FIELD: {
b32 action = (row->a == INSP_N_ADD_SECTION);
draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, lx, base, (action ? rx : vx - 8.0f) - lx, cells.text[0], action ? COLOR_TEXT_FAINT : COLOR_TEXT_DIM);
if(action) break;
if(edit) {
edit_field = Rect{vx - 6.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f};
} else if(card_field_is_choice(row->a)) {
// < value >: the arrows step back and forth, the value steps forward
u32 arrow = (hov || sel) ? COLOR_TEXT : COLOR_TEXT_FAINT;
draw_text(d, fonts, FONT_MONO, UI_FONT, vx, mono, str8("<"), arrow);
draw_text(d, fonts, FONT_MONO, UI_FONT, rx - 8.0f, mono, str8(">"), arrow);
draw_text_fit(d, fonts, FONT_MONO, UI_FONT, vx + 16.0f, mono, rx - vx - 28.0f, cells.text[1], COLOR_TEXT);
if(hov) dir = (input->mouse_x >= vx - 4.0f && input->mouse_x < vx + 14.0f) ? -1 : 1;
} else {
f32 x = vx;
if(cells.has_swatch) {
f32 side = Min(row_h - 10.0f, 12.0f);
Rect sw = {x, y + (row_h - side) * 0.5f, x + side, y + (row_h + side) * 0.5f};
draw_rect(d, sw, cells.swatch, 0);
draw_border(d, sw, COLOR_PANEL_EDGE, 0, 1);
x += side + 7.0f;
}
b32 empty = cells.text[1].size == 0;
draw_text_fit(d, fonts, FONT_MONO, UI_FONT, x, mono, rx - x, empty ? str8("-") : cells.text[1], empty ? COLOR_TEXT_FAINT : COLOR_TEXT);
}
} break;
case ROW_SECTION: {
NodeSection *s = &dia->sections[row->a];
draw_text(d, fonts, FONT_MONO, UI_FONT, lx, mono, str8(s->collapsed ? "+" : "-"), COLOR_TEXT_DIM);
if(edit) {
edit_field = Rect{lx + 14.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f};
break;
}
draw_text_fit(d, fonts, FONT_SEMIBOLD, UI_FONT_SMALL, lx + 16.0f, ui_baseline(app, FONT_SEMIBOLD, UI_FONT_SMALL, y, row_h), vx - lx - 20.0f, str8(s->title), COLOR_TEXT_DIM);
if(hov || sel) {
hit = Rect{rx - row_h + 6.0f, y, rx + 6.0f, y + row_h};
b32 on = hov && ui_hovered(input, hit);
if(on) draw_rect(d, hit, COLOR_HOVER, 0);
f32 cx = (hit.x0 + hit.x1) * 0.5f, cy = y + row_h * 0.5f;
draw_line(d, cx - 4.0f, cy, cx + 4.0f, cy, 1.2f, on ? COLOR_ACCENT : COLOR_TEXT_DIM);
draw_line(d, cx, cy - 4.0f, cx, cy + 4.0f, 1.2f, on ? COLOR_ACCENT : COLOR_TEXT_DIM);
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, vx, mono, hit.x0 - vx - 4.0f, str8("a: add"), COLOR_TEXT_FAINT);
} else {
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, vx, mono, rx - vx, cells.text[1], COLOR_TEXT_FAINT);
}
} break;
case ROW_MEMBER: {
f32 nx = lx + 16.0f;
if(edit) {
edit_field = Rect{nx - 6.0f, y + 2.0f, rx + 4.0f, y + row_h - 2.0f};
break;
}
f32 right = rx;
if(hov || sel) {
hit = Rect{rx - row_h + 6.0f, y, rx + 6.0f, y + row_h};
b32 on = hov && ui_hovered(input, hit);
if(on) draw_rect(d, hit, COLOR_HOVER, 0);
f32 cx = (hit.x0 + hit.x1) * 0.5f, cy = y + row_h * 0.5f;
draw_line(d, cx - 3.5f, cy - 3.5f, cx + 3.5f, cy + 3.5f, 1.2f, on ? COLOR_TEXT : COLOR_TEXT_FAINT);
draw_line(d, cx - 3.5f, cy + 3.5f, cx + 3.5f, cy - 3.5f, 1.2f, on ? COLOR_TEXT : COLOR_TEXT_FAINT);
right = hit.x0 - 4.0f;
} else if(cells.text[2].size) {
f32 bw = Min(font_measure(fonts, FONT_MONO, UI_FONT_SMALL, cells.text[2]) + 8.0f, (rx - vx) * 0.5f);
Rect pill = {rx - bw, y + 5.0f, rx, y + row_h - 5.0f};
draw_rect(d, pill, COLOR_TAB, 0);
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, pill.x0 + 4.0f, ui_baseline(app, FONT_MONO, UI_FONT_SMALL, y, row_h), bw - 8.0f, cells.text[2], COLOR_TEXT_DIM);
right = pill.x0 - 6.0f;
}
// NOTE: members flow like on the node (name, then its type), they don't use the columns
f32 name_w = font_measure(fonts, FONT_MONO, UI_FONT, cells.text[0]);
f32 type_w = font_measure(fonts, FONT_MONO, UI_FONT, cells.text[1]);
f32 room = right - nx;
f32 fit_n = (name_w + 10.0f + type_w <= room) ? name_w : Max(room * 0.62f, room - 10.0f - type_w);
f32 tx = nx + draw_text_fit(d, fonts, FONT_MONO, UI_FONT, nx, mono, fit_n, cells.text[0], COLOR_TEXT) + 10.0f;
if(tx < right) draw_text_fit(d, fonts, FONT_MONO, UI_FONT, tx, mono, right - tx, cells.text[1], COLOR_TEXT_DIM);
} break;
}
if(hov) app->canvas_cursor = PLATFORM_CURSOR_POINTER;
//- mouse: one click does the obvious thing, no double clicks needed
if(hov && ButtonPressed(input->mouse[0])) {
app->layout.focused = panel;
app->card_focused = true;
cv->cursor = (s32)index;
b32 on_hit = hit.x1 > hit.x0 && ui_hovered(input, hit);
if(row->kind == ROW_SECTION) {
NodeSection *s = &dia->sections[row->a];
if(on_hit) {
diagram_push_undo(dia);
s32 m = diagram_add_member(dia, (s32)row->a, '+', "new_field", "u32", "");
if(m >= 0) start_edit(app, EDIT_MEMBER_LINE, m, "+ new_field : u32");
} else if(input->mouse_x < lx + 14.0f) {
s->collapsed = !s->collapsed;
diagram_measure_node(dia, s->node);
++dia->version;
} else {
view_activate(app, cv, row);
}
} else if(row->kind == ROW_MEMBER && on_hit) {
diagram_push_undo(dia);
diagram_delete_member(dia, (s32)row->a);
app_message(app, "deleted member (u to undo)");
} else if(dir) {
view_adjust(app, cv, row, dir);
} else {
view_activate(app, cv, row);
}
}
}
//- the inline edit field, drawn last so it sits on top of its row
if(edit_field.x1 > edit_field.x0) {
card_draw_input(app, edit_field);
// a click anywhere else keeps the text, like leaving a field in any property editor
if(ButtonPressed(input->mouse[0]) && !ui_hovered(input, edit_field)) {
app->overlay = UI_OVERLAY_NONE;
edit_accept(app);
}
}
draw_clip_pop(d);
f32 content = (f32)count * row_h;
if(content > view_h + 0.5f) {
f32 thumb_h = Max(view_h * view_h / content, 16.0f);
f32 thumb_y = body.y0 + (view_h - thumb_h) * (cv->scroll.offset / (content - view_h));
draw_rect(d, Rect{body.x1 - 4, thumb_y, body.x1 - 1, thumb_y + thumb_h}, COLOR_TRACK, 0);
}
//- footer: the keys that work right now
{
const char *hint = "enter: edit here I: hide";
if(editing) hint = "enter: save esc: cancel";
else if(app->card_focused) hint = "j/k h/l: change enter: edit a: add esc";
draw_rect(d, Rect{foot.x0, foot.y0, foot.x1, foot.y0 + 1}, COLOR_PANEL_EDGE, 0);
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, lx, ui_baseline(app, FONT_MONO, UI_FONT_SMALL, foot.y0, rect_h(foot)), rx - lx, str8((char *)hint), COLOR_TEXT_FAINT);
}
draw_border(d, box, app->card_focused ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE, 0, 1);
scratch_end(&scratch);
}
//- watch-style table rendering (reorderable / resizable / hideable columns, identical to mp3_player)
typedef struct TableColumns TableColumns;
struct TableColumns {
u32 logical[UI_MAX_COLUMNS];
f32 x[UI_MAX_COLUMNS];
f32 w[UI_MAX_COLUMNS];
u32 count;
};
internal void table_columns_compute(View *view, Column *columns, u32 total, Rect rect, TableColumns *out)
{
if(view->column_count != total) {
view->column_count = total;
view->column_hidden = 0;
for(u32 c = 0; c < total; ++c) {
view->column_order[c] = (u8)c;
view->column_weight[c] = columns[c].width;
}
}
f32 sum = 0;
out->count = 0;
for(u32 pos = 0; pos < total; ++pos) {
u32 c = view->column_order[pos];
if(c >= total || (view->column_hidden & (1u << c))) continue;
out->logical[out->count++] = c;
sum += Max(view->column_weight[c], 0.04f);
}
if(out->count == 0 && total > 0) {
out->logical[0] = 0;
out->count = 1;
sum = Max(view->column_weight[0], 0.04f);
}
f32 x = rect.x0, width = rect_w(rect);
for(u32 i = 0; i < out->count; ++i) {
f32 w = width * Max(view->column_weight[out->logical[i]], 0.04f) / sum;
out->x[i] = x;
out->w[i] = w;
x += w;
}
}
internal void draw_table(App *app, PlatformInput *input, s32 panel, View *view, Rect rect, b32 focused)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
ScratchArena scratch = ScratchGet();
b32 modal = (app->overlay != UI_OVERLAY_NONE);
s32 vi = (s32)(view - app->layout.views);
Column columns[UI_MAX_COLUMNS];
u32 col_total = view_columns((ViewKind)view->kind, columns);
TableColumns layout;
table_columns_compute(view, columns, col_total, rect, &layout);
f32 header_h = UI_COLUMN_H;
Rect header = {rect.x0, rect.y0, rect.x1, rect.y0 + header_h};
Rect body = {rect.x0, header.y1, rect.x1, rect.y1};
//- column headers (drag to reorder, drag edge to resize, middle click to hide)
if(header_h > 0 && layout.count > 0) {
draw_rect(d, header, COLOR_HEADER, 0);
f32 hb = ui_baseline(app, FONT_MONO, UI_FONT_SMALL, header.y0, header_h);
for(u32 i = 0; i < layout.count; ++i) {
u32 c = layout.logical[i];
Rect cell = {layout.x[i], header.y0, layout.x[i] + layout.w[i], header.y1};
if(i > 0) {
draw_rect(d, Rect{cell.x0, header.y0 + 3, cell.x0 + 1, header.y1 - 3}, COLOR_PANEL_EDGE, 0);
Rect edge = {cell.x0 - 4, header.y0, cell.x0 + 4, header.y1};
if(!modal && ui_hovered(input, edge)) {
app->table_cursor = PLATFORM_CURSOR_RESIZE_X;
if(ButtonPressed(input->mouse[0])) { app->column_resize_view = vi; app->column_resize_index = i - 1; }
}
}
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, cell.x0 + 8, hb, layout.w[i] - 12, str8((char *)columns[c].title), COLOR_TEXT_DIM);
if(!modal && ui_hovered(input, cell) && app->column_resize_view < 0) {
if(ButtonPressed(input->mouse[2]) && layout.count > 1) view->column_hidden |= (1u << c);
}
}
draw_rect(d, Rect{header.x0, header.y1 - 1, header.x1, header.y1}, COLOR_PANEL_EDGE, 0);
}
if(app->column_resize_view == vi) {
if(input->mouse[0].ended_down && app->column_resize_index + 1 < layout.count) {
u32 a = layout.logical[app->column_resize_index];
u32 b = layout.logical[app->column_resize_index + 1];
f32 pair_w = layout.w[app->column_resize_index] + layout.w[app->column_resize_index + 1];
f32 sum_w = view->column_weight[a] + view->column_weight[b];
f32 t = Clamp((input->mouse_x - layout.x[app->column_resize_index]) / Max(pair_w, 1.0f), 0.12f, 0.88f);
view->column_weight[a] = sum_w * t;
view->column_weight[b] = sum_w * (1.0f - t);
app->table_cursor = PLATFORM_CURSOR_RESIZE_X;
} else {
app->column_resize_view = -1;
}
}
Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS);
u32 count = view_rows(app, view, rows, UI_MAX_ROWS);
view->row_count = count;
if(count > 0) view->cursor = Clamp(view->cursor, 0, (s32)count - 1);
else view->cursor = 0;
f32 row_h = UI_ROW_H;
f32 view_h = Max(rect_h(body), 1.0f);
view->visible_rows = (u32)Max(floorf(view_h / row_h), 1.0f);
if(!modal && ui_hovered(input, body) && input->scroll_y != 0.0f) {
view->scroll.target += input->scroll_y * row_h * (f32)ui_theme.scroll_lines;
}
scroll_update(&view->scroll, (f32)count * row_h, view_h, app->dt, &app->animating);
u32 first = (u32)Max(floorf(view->scroll.offset / row_h), 0.0f);
u32 last = Min(count, first + view->visible_rows + 2);
view->first_visible = first;
String8 dsl_cache = {};
if(view->kind == VIEW_SOURCE) dsl_cache = diagram_to_dsl(scratch.arena, app->diagram);
draw_clip_push(d, body);
Cells cells;
for(u32 index = first; index < last; ++index) {
Row *row = &rows[index];
f32 y = body.y0 + (f32)index * row_h - view->scroll.offset;
Rect r = {body.x0, y, body.x1, y + row_h};
b32 sel = ((s32)index == view->cursor);
b32 hov = !modal && ui_hovered(input, rect_intersect(r, body));
view_cells(app, view, row, &cells, scratch.arena, &dsl_cache);
if(sel) draw_rect(d, r, focused ? COLOR_SELECTED : color_alpha(COLOR_SELECTED, 0.55f), 0);
else if(hov) draw_rect(d, r, COLOR_HOVER, 0);
else if(ui_theme.zebra && (index & 1)) draw_rect(d, r, COLOR_ZEBRA, 0);
u32 color = cells.bright ? COLOR_ACCENT : (cells.dim ? COLOR_TEXT_FAINT : COLOR_TEXT);
for(u32 pos = 0; pos < layout.count; ++pos) {
u32 c = layout.logical[pos];
f32 cx = layout.x[pos];
f32 w = layout.w[pos];
f32 indent = 0.0f;
if(c == 0) {
indent = (f32)row->depth * ui_theme.tree_indent;
if(row->expandable) {
draw_text(d, fonts, FONT_MONO, UI_FONT, cx + 8 + indent, ui_baseline(app, FONT_MONO, UI_FONT, y, row_h), str8(row->expanded ? "-" : "+"), COLOR_TEXT_DIM);
indent += 13.0f;
}
}
FontKind font = columns[c].mono ? FONT_MONO : ((cells.bright && c == 0) ? FONT_SEMIBOLD : FONT_REGULAR);
if(c == 1 && cells.has_swatch) {
f32 side = Min(row_h - 8, 14.0f);
Rect sw = {cx + 8, y + (row_h - side) * 0.5f, cx + 8 + side, y + (row_h + side) * 0.5f};
draw_rect(d, sw, cells.swatch, 0);
draw_border(d, sw, COLOR_PANEL_EDGE, 0, 1);
indent = side + 8;
}
u32 tint = (c > 0 && !cells.bright) ? (cells.dim ? COLOR_TEXT_FAINT : COLOR_TEXT_DIM) : color;
draw_text_fit(d, fonts, font, UI_FONT, cx + 8 + indent, ui_baseline(app, font, UI_FONT, y, row_h), w - 14 - indent, cells.text[c], tint);
}
if(hov && ButtonPressed(input->mouse[0])) {
app->layout.focused = panel;
view->cursor = (s32)index;
u64 now = time_now_ns();
b32 twice = (now - app->last_click_ns < (u64)ui_theme.double_click_ms * 1000000ull) &&
fabsf(input->mouse_x - app->last_click_x) < 4 && fabsf(input->mouse_y - app->last_click_y) < 4;
b32 on_box = row->expandable && input->mouse_x < body.x0 + 8 + row->depth * ui_theme.tree_indent + 14.0f;
if(on_box && row->kind == ROW_NODE) {
view_set_expanded(view, row->a, !view_expanded(view, row->a));
now = 0;
} else if(on_box && row->kind == ROW_SECTION) {
app->diagram->sections[row->a].collapsed = !app->diagram->sections[row->a].collapsed;
diagram_measure_node(app->diagram, app->diagram->sections[row->a].node);
now = 0;
} else if(twice) {
view_activate(app, view, row);
now = 0;
} else if(row->kind == ROW_NODE) {
diagram_clear_selection(app->diagram);
app->diagram->nodes[row->a].selected = true;
app->active_node = (s32)row->a;
app->active_edge = -1;
} else if(row->kind == ROW_EDGE) {
diagram_clear_selection(app->diagram);
app->diagram->edges[row->a].selected = true;
app->active_edge = (s32)row->a;
app->active_node = -1;
}
app->last_click_ns = now;
app->last_click_x = input->mouse_x;
app->last_click_y = input->mouse_y;
}
}
draw_clip_pop(d);
if(count == 0) {
const char *empty = "empty";
if(view->kind == VIEW_LAYERS) empty = "no tags yet (set a node's tag in Inspector)";
if(view->kind == VIEW_EDGES) empty = "no edges (drag from a node port or right-drag a node)";
draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, body.x0 + 10, body.y0 + 18, rect_w(body) - 20, str8((char *)empty), COLOR_TEXT_FAINT);
}
f32 content = (f32)count * row_h;
if(content > view_h && ui_theme.show_scrollbars) {
f32 thumb_h = Max(view_h * view_h / content, 20.0f);
f32 thumb_y = body.y0 + (view_h - thumb_h) * (view->scroll.offset / (content - view_h));
draw_rect(d, Rect{body.x1 - 4, thumb_y, body.x1 - 1, thumb_y + thumb_h}, COLOR_TRACK, 0);
}
scratch_end(&scratch);
}
//- panels & bar (exact RAD docking UX)
internal DropZone drop_zone(Rect rect, f32 x, f32 y)
{
f32 u = (x - rect.x0) / Max(rect_w(rect), 1.0f);
f32 v = (y - rect.y0) / Max(rect_h(rect), 1.0f);
if(u < 0.25f) return DROP_LEFT;
if(u > 0.75f) return DROP_RIGHT;
if(v < 0.25f) return DROP_TOP;
if(v > 0.75f) return DROP_BOTTOM;
return DROP_CENTER;
}
internal Rect drop_preview(Rect rect, DropZone zone)
{
switch(zone) {
case DROP_LEFT: return Rect{rect.x0, rect.y0, rect.x0 + rect_w(rect) * 0.5f, rect.y1};
case DROP_RIGHT: return Rect{rect.x0 + rect_w(rect) * 0.5f, rect.y0, rect.x1, rect.y1};
case DROP_TOP: return Rect{rect.x0, rect.y0, rect.x1, rect.y0 + rect_h(rect) * 0.5f};
case DROP_BOTTOM: return Rect{rect.x0, rect.y0 + rect_h(rect) * 0.5f, rect.x1, rect.y1};
default: return rect;
}
}
internal b32 draw_bar(App *app, PlatformInput *input, s32 panel, s32 view_index, Rect rect, b32 modal)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
View *view = &app->layout.views[view_index];
f32 size = UI_FONT_SMALL;
ScratchArena scratch = ScratchGet();
draw_rect(d, rect, COLOR_BG, 0);
if(!modal && ui_hovered(input, rect) && app->drag_view < 0) {
if(ButtonPressed(input->mouse[0])) {
app->drag_view = view_index;
app->drag_panel = panel;
app->drag_x = input->mouse_x;
app->drag_y = input->mouse_y;
app->drag_active = false;
}
if(ButtonPressed(input->mouse[2])) {
Panel *p = &app->layout.panels[panel];
scratch_end(&scratch);
layout_close_view(&app->layout, panel, p->active_tab);
return true;
}
}
Row *rows = PushArray(scratch.arena, Row, 64);
u32 count = view_rows(app, view, rows, 64);
u32 shown = (view->kind == VIEW_STATUS || view->kind == VIEW_INSPECTOR || view->kind == VIEW_PROFILER || view->kind == VIEW_SETTINGS) ? 1 : 0;
Cells *all = PushArray(scratch.arena, Cells, count + 1);
f32 base = ui_baseline(app, FONT_MONO, size, rect.y0, Min(rect_h(rect), UI_STATUS_H));
f32 right = rect.x1 - 10;
draw_clip_push(d, rect);
for(u32 pass = 0; pass < 2; ++pass) {
f32 left = rect.x0;
for(u32 index = 0; index < count; ++index) {
Cells *cells = &all[index];
if(pass == 0) view_cells(app, view, &rows[index], cells, scratch.arena);
String8 text = cells->text[shown];
if(text.size == 0 || (b32)cells->right != (pass == 0)) continue;
u32 color = cells->dim ? COLOR_TEXT_DIM : COLOR_TEXT;
if(cells->right) {
right -= font_measure(fonts, FONT_MONO, size, text);
draw_text(d, fonts, FONT_MONO, size, right, base, text, color);
right -= 18;
} else if(cells->bright) {
f32 w = font_measure(fonts, FONT_MONO, size, text) + 16;
draw_rect(d, Rect{left, rect.y0, left + w, rect.y1}, COLOR_TAB_ACTIVE, 0);
draw_text(d, fonts, FONT_MONO, size, left + 8, base, text, COLOR_ACCENT);
left += w + 10;
} else if(right - left > 24) {
left += draw_text_fit(d, fonts, FONT_REGULAR, size, left, base, right - left - 8, text, COLOR_TEXT_DIM) + 18;
}
}
}
draw_clip_pop(d);
scratch_end(&scratch);
return false;
}
internal void draw_panels(App *app, PlatformInput *input)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
Layout *layout = &app->layout;
u32 cursor = PLATFORM_CURSOR_DEFAULT;
b32 modal = (app->overlay != UI_OVERLAY_NONE);
f32 tab_font = UI_FONT_SMALL + 0.5f;
//- split resize handles
for(s32 index = 0; index < UI_MAX_PANELS && !modal; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_SPLIT) continue;
Rect a = layout->panels[p->children[0]].rect;
Rect handle = (p->axis == 0) ? Rect{a.x1 - 1, p->rect.y0, a.x1 + UI_GAP + 1, p->rect.y1}
: Rect{p->rect.x0, a.y1 - 1, p->rect.x1, a.y1 + UI_GAP + 1};
b32 over = ui_hovered(input, handle) && app->drag_view < 0;
if(over && ButtonPressed(input->mouse[0])) app->resize_panel = index;
if(over || app->resize_panel == index) cursor = (p->axis == 0) ? PLATFORM_CURSOR_RESIZE_X : PLATFORM_CURSOR_RESIZE_Y;
}
if(app->resize_panel >= 0) {
Panel *p = &layout->panels[app->resize_panel];
if(p->kind == PANEL_SPLIT) {
f32 t = (p->axis == 0) ? (input->mouse_x - p->rect.x0) / rect_w(p->rect) : (input->mouse_y - p->rect.y0) / rect_h(p->rect);
p->ratio = Clamp(t, 0.08f, 0.92f);
f32 lines = (p->rect.y1 - input->mouse_y) / UI_STATUS_H;
if(p->axis == 1 && lines < 3.5f) p->ratio = -Max(roundf(lines), 1.0f);
}
if(!input->mouse[0].ended_down) app->resize_panel = -1;
}
//- leaves
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_LEAF) continue;
b32 focused = (layout->focused == index) && !modal;
Rect rect = p->rect;
if(panel_is_bar(layout, index)) {
s32 view = panel_active_view(layout, index);
if(view >= 0 && draw_bar(app, input, index, view, rect, modal)) return;
continue;
}
draw_rect(d, rect, COLOR_PANEL, 0);
//- tab bar
Rect bar = {rect.x0, rect.y0, rect.x1, rect.y0 + UI_TAB_H};
draw_rect(d, bar, COLOR_BG, 0);
ScratchArena tab_scratch = ScratchGet();
String8 *titles = PushArray(tab_scratch.arena, String8, UI_MAX_TABS);
f32 *widths = PushArray(tab_scratch.arena, f32, UI_MAX_TABS);
f32 total = 0;
for(u32 t = 0; t < p->tab_count; ++t) {
titles[t] = view_title(tab_scratch.arena, &layout->views[p->tabs[t]]);
widths[t] = font_measure(fonts, FONT_REGULAR, tab_font, titles[t]) + 24;
total += widths[t] + 1;
}
f32 fit = (total > rect_w(bar) && total > 0) ? rect_w(bar) / total : 1.0f;
draw_clip_push(d, bar);
f32 tx = bar.x0;
for(u32 t = 0; t < p->tab_count; ++t) {
String8 title = titles[t];
f32 w = Max(widths[t] * fit, 28.0f);
Rect tab = {tx, bar.y0, tx + w, bar.y1};
b32 active = (t == p->active_tab);
b32 over = ui_hovered(input, rect_intersect(tab, bar)) && !modal;
draw_rect(d, tab, active ? COLOR_TAB_ACTIVE : (over ? COLOR_TAB : COLOR_BG), 0);
if(active && focused) draw_rect(d, Rect{tab.x0, tab.y0, tab.x1, tab.y0 + 2}, COLOR_ACCENT, 0);
draw_text_fit(d, fonts, FONT_REGULAR, tab_font, tab.x0 + 12 * Min(fit, 1.0f), ui_baseline(app, FONT_REGULAR, tab_font, tab.y0, UI_TAB_H), w - 16, title,
active ? (focused ? COLOR_ACCENT : COLOR_TEXT) : COLOR_TEXT_DIM);
if(over && ButtonPressed(input->mouse[0])) {
p->active_tab = t;
layout->focused = index;
app->drag_view = p->tabs[t];
app->drag_panel = index;
app->drag_x = input->mouse_x;
app->drag_y = input->mouse_y;
app->drag_active = false;
}
if(over && ButtonPressed(input->mouse[2])) {
draw_clip_pop(d);
scratch_end(&tab_scratch);
layout_close_view(layout, index, t);
return;
}
tx += w + 1;
}
draw_clip_pop(d);
scratch_end(&tab_scratch);
Rect empty = {tx, bar.y0, bar.x1, bar.y1};
if(!modal && !PlatformWindowIsLayer(app->window) && ui_clicked(input, empty) && app->drag_view < 0) {
u64 now = time_now_ns();
if(now - app->last_click_ns < (u64)ui_theme.double_click_ms * 1000000ull) {
PlatformWindowToggleMaximized(app->window);
app->last_click_ns = 0;
} else {
app->last_click_ns = now;
PlatformWindowBeginMove(app->window);
}
}
//- content: Canvas OR Watch Table
Rect content = {rect.x0, bar.y1, rect.x1, rect.y1};
s32 view = panel_active_view(layout, index);
if(view >= 0) {
draw_clip_push(d, content);
if(layout->views[view].kind == VIEW_CANVAS) {
draw_canvas(app, input, index, &layout->views[view], content, focused);
} else {
draw_table(app, input, index, &layout->views[view], content, focused);
}
draw_clip_pop(d);
} else {
draw_text(d, fonts, FONT_MONO, UI_FONT_SMALL, content.x0 + 10, content.y0 + 20, str8(":view canvas | outline | inspector | edges | layers | palette | source"), COLOR_TEXT_FAINT);
}
draw_border(d, rect, focused ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE, 0, 1);
}
//- tab drag & drop
if(app->drag_view >= 0) {
f32 moved = fabsf(input->mouse_x - app->drag_x) + fabsf(input->mouse_y - app->drag_y);
if(moved > 6) app->drag_active = true;
if(app->drag_active) {
cursor = PLATFORM_CURSOR_GRABBING;
for(s32 index = 0; index < UI_MAX_PANELS; ++index) {
Panel *p = &layout->panels[index];
if(p->kind != PANEL_LEAF || !ui_hovered(input, p->rect)) continue;
DropZone zone = drop_zone(p->rect, input->mouse_x, input->mouse_y);
Rect preview = drop_preview(p->rect, zone);
draw_rect(d, preview, COLOR_DROP, 0);
draw_border(d, preview, COLOR_FOCUS_EDGE, 0, 1);
if(!input->mouse[0].ended_down) {
layout_move_view(layout, app->drag_panel, app->drag_view, index, zone);
break;
}
}
if(app->drag_view >= 0 && layout->views[app->drag_view].kind != VIEW_NONE) {
ScratchArena gs = ScratchGet();
String8 title = view_title(gs.arena, &layout->views[app->drag_view]);
f32 w = font_measure(fonts, FONT_REGULAR, tab_font, title) + 24;
Rect ghost = {input->mouse_x + 10, input->mouse_y + 10, input->mouse_x + 10 + w, input->mouse_y + 10 + UI_TAB_H};
draw_rect(d, ghost, COLOR_TAB_ACTIVE, 0);
draw_border(d, ghost, COLOR_FOCUS_EDGE, 0, 1);
draw_text(d, fonts, FONT_REGULAR, tab_font, ghost.x0 + 12, ui_baseline(app, FONT_REGULAR, tab_font, ghost.y0, UI_TAB_H), title, COLOR_TEXT);
scratch_end(&gs);
}
}
if(!input->mouse[0].ended_down) app->drag_view = -1;
}
if(cursor == PLATFORM_CURSOR_DEFAULT) cursor = (app->canvas_cursor != PLATFORM_CURSOR_DEFAULT) ? app->canvas_cursor : app->table_cursor;
if(app->cursor_shape != cursor) {
app->cursor_shape = cursor;
PlatformWindowSetCursor(app->window, (PlatformCursor)cursor);
}
}
//- search & command bar (with live completion)
internal void search_update(App *app)
{
Diagram *dia = app->diagram;
app->result_count = 0;
app->result_cursor = 0;
if(app->input_size == 0) return;
for(u32 i = 0; i < dia->node_count && app->result_count < UI_MAX_RESULTS; ++i) {
DiagramNode *n = &dia->nodes[i];
if(!n->alive) continue;
if(str8_contains_nocase(str8(n->name), app->input, app->input_size) ||
str8_contains_nocase(str8(n->tag), app->input, app->input_size) ||
str8_contains_nocase(str8(n->stereotype), app->input, app->input_size) ||
str8_contains_nocase(str8(n->link), app->input, app->input_size) ||
str8_contains_nocase(str8(n->note), app->input, app->input_size) ||
str8_contains_nocase(diagram_doc(dia, n->doc_off, n->doc_len), app->input, app->input_size)) {
app->results[app->result_count++] = SearchResult{0, (s32)i, -1};
}
}
for(u32 i = 0; i < dia->member_count && app->result_count < UI_MAX_RESULTS; ++i) {
NodeMember *m = &dia->members[i];
if(!m->alive) continue;
if(str8_contains_nocase(str8(m->name), app->input, app->input_size) ||
str8_contains_nocase(str8(m->type), app->input, app->input_size)) {
app->results[app->result_count++] = SearchResult{2, (s32)i, dia->sections[m->section].node};
}
}
for(u32 i = 0; i < dia->edge_count && app->result_count < UI_MAX_RESULTS; ++i) {
DiagramEdge *e = &dia->edges[i];
if(!e->alive) continue;
if((e->label[0] && str8_contains_nocase(str8(e->label), app->input, app->input_size)) ||
(e->doc_len && str8_contains_nocase(diagram_doc(dia, e->doc_off, e->doc_len), app->input, app->input_size))) {
app->results[app->result_count++] = SearchResult{1, (s32)i, -1};
}
}
}
internal void search_accept(App *app, SearchResult r)
{
Diagram *dia = app->diagram;
diagram_clear_selection(dia);
if(r.kind == 0 || r.kind == 2) {
s32 ni = (r.kind == 0) ? r.index : r.parent_node;
if(ni >= 0 && dia->nodes[ni].alive) {
dia->nodes[ni].selected = true;
app->active_node = ni;
app->active_edge = -1;
app->pan_to_active = true;
}
} else if(r.kind == 1 && r.index >= 0 && dia->edges[r.index].alive) {
dia->edges[r.index].selected = true;
app->active_edge = r.index;
app->active_node = -1;
}
}
typedef struct CommandDesc CommandDesc;
struct CommandDesc {
const char *name;
const char *args;
const char *help;
b32 takes_arg;
};
global_variable const CommandDesc ui_commands[] = {
{"view", "<view>", "open or focus a view", 1},
{"vs", "<view>", "split right with a view", 1},
{"sp", "<view>", "split below with a view", 1},
{"set", "<name>=<value>", "change any setting", 1},
{"add", "<kind> <name>", "add a node (struct/class/iface/module/...)", 1},
{"connect", "<src_key> <dst_key>", "connect two nodes by key", 1},
{"layout", "<layered|lr|force|grid>","auto-layout the diagram", 1},
{"route", "<ortho|straight|bezier>","set default edge routing", 1},
{"align", "<left|right|top|bot|cx|cy|dh|dv>", "align or distribute selected nodes", 1},
{"tpl", "<arch|ecs|microservices|compiler|uml|sequence|stress>", "load showcase template", 1},
{"scan", "<file_or_dir>", "scan C/C++ headers into a live diagram", 1},
{"save", "<path.dit|.dit.txt>", "save diagram (binary or readable DSL)", 1},
{"open", "<file>", "open .dit / .dit.txt / .mmd / .puml (or scan a folder)", 1},
{"import", "<file>", "merge a .dit.txt / .mmd / .puml into this diagram", 1},
{"export", "<file> [opt]", ".svg .png [scale] .mmd [class|flowchart|sequence] .md .puml .dit.txt", 1},
{"doc", "", "edit the documentation of the selection (or the diagram)", 0},
{"link", "<path:line|url>", "set the selected node's source link (empty clears)", 0},
{"browse", "", "open the selected node's link online (gx)", 0},
{"src", "", "open the selected node's file in the editor (gf)", 0},
{"repo", "<url [ref]|detect|clear>", "where links open online (github / gitlab / ...)", 0},
{"root", "<path>", "local checkout the links are relative to", 0},
{"copy", "", "copy the selection (yy, Ctrl+C)", 0},
{"paste", "", "paste at the view centre (p, Ctrl+V)", 0},
{"straighten", "", "remove the bends of the selected edges", 0},
{"seq", "", "re-space the sequence diagram (lifelines and messages)", 0},
{"focus", "", "toggle focus mode (dim outside neighborhood)", 0},
{"fit", "", "fit diagram in canvas", 0},
{"clear", "", "clear the diagram", 0},
{"columns", "", "restore hidden columns of focused table", 0},
{"reset", "", "reset panel layout to default", 0},
{"zoom", "<0.6-3>", "set ui zoom", 1},
{"help", "[topic]", "open the manual (F1), jump to a topic", 0},
{"q", "", "quit", 0},
};
typedef struct Completion Completion;
struct Completion {
String8 text;
String8 detail;
u32 from;
char suffix;
};
#define UI_MAX_COMPLETIONS 64
typedef struct CompletionList CompletionList;
struct CompletionList {
Completion items[UI_MAX_COMPLETIONS];
u32 count;
};
internal u32 completion_match(const char *candidate, const char *token, u32 token_size)
{
String8 c = str8((char *)candidate);
if(token_size == 0) return 2;
if(c.size >= token_size && strncasecmp(candidate, token, token_size) == 0) return 2;
return str8_contains_nocase(c, token, token_size) ? 1 : 0;
}
internal u32 command_completions(App *app, Arena *arena, CompletionList *out)
{
out->count = 0;
const char *in = app->input;
u32 len = app->input_size;
u32 sp = 0;
while(sp < len && in[sp] != ' ') ++sp;
if(sp == len) {
for(u32 tier = 2; tier >= 1; --tier) {
for(u32 i = 0; i < ArrayCount(ui_commands) && out->count < UI_MAX_COMPLETIONS; ++i) {
if(completion_match(ui_commands[i].name, in, len) == tier) {
out->items[out->count++] = Completion{str8((char *)ui_commands[i].name), ui_format(arena, "%s %s", ui_commands[i].args, ui_commands[i].help), 0, (char)(ui_commands[i].takes_arg ? ' ' : 0)};
}
}
}
return out->count;
}
u32 arg_from = sp + 1;
while(arg_from < len && in[arg_from] == ' ') ++arg_from;
const char *arg = in + arg_from;
u32 alen = len - arg_from;
if((sp == 4 && !strncasecmp(in, "view", 4)) || (sp == 2 && (!strncasecmp(in, "vs", 2) || !strncasecmp(in, "sp", 2)))) {
ViewType types[UI_MAX_VIEW_TYPES];
u32 n = view_types(types);
for(u32 i = 0; i < n && out->count < UI_MAX_COMPLETIONS; ++i) {
if(completion_match(types[i].name, arg, alen)) {
out->items[out->count++] = Completion{str8((char *)types[i].name), str8(types[i].kind == VIEW_SETTINGS ? "settings" : (types[i].kind == VIEW_PROFILER ? "profiler" : "view")), arg_from, 0};
}
}
} else if(sp == 3 && !strncasecmp(in, "set", 3)) {
const char *eq = NULL;
for(u32 i = arg_from; i < len; ++i) if(in[i] == '=') { eq = in + i; break; }
if(!eq) {
for(u32 i = 0; i < settings_desc_count() && out->count < UI_MAX_COMPLETIONS; ++i) {
SettingDesc *desc = settings_desc(i);
if(desc->kind == SETTING_KIND_SECTION || desc->kind == SETTING_KIND_ACTION || desc->kind == SETTING_KIND_INFO) continue;
if(completion_match(desc->name, arg, alen)) {
out->items[out->count++] = Completion{str8((char *)desc->name), settings_format(arena, &app->settings, desc), arg_from, '='};
}
}
}
} else if(sp == 3 && !strncasecmp(in, "tpl", 3)) {
for(u32 i = 0; i < diagram_template_count() && out->count < UI_MAX_COMPLETIONS; ++i) {
const DiagramTemplate *t = diagram_template_info(i);
if(completion_match(t->name, arg, alen)) out->items[out->count++] = Completion{str8((char *)t->name), str8((char *)t->description), arg_from, 0};
}
} else if(sp == 6 && !strncasecmp(in, "layout", 6)) {
const char *modes[] = {"layered", "lr", "force", "grid", "sequence"};
const char *desc[] = {"hierarchical top-down (Sugiyama)", "hierarchical left-to-right", "spring force-directed", "compact grid", "lifelines in a row, messages re-spaced"};
for(u32 i = 0; i < 5; ++i) if(completion_match(modes[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)modes[i]), str8((char *)desc[i]), arg_from, 0};
} else if(sp == 5 && !strncasecmp(in, "route", 5)) {
const char *modes[] = {"orthogonal", "straight", "bezier"};
for(u32 i = 0; i < 3; ++i) if(completion_match(modes[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)modes[i]), str8("edge routing"), arg_from, 0};
} else if(sp == 3 && !strncasecmp(in, "add", 3)) {
for(u32 i = 0; i < NODE_KIND_COUNT; ++i) if(completion_match(diagram_node_kind_names[i], arg, alen)) out->items[out->count++] = Completion{str8((char *)diagram_node_kind_names[i]), str8("node kind"), arg_from, ' '};
}
return out->count;
}
internal void command_accept_completion(App *app, Completion *item)
{
u32 pos = Min(item->from, (u32)sizeof(app->input) - 1);
u32 take = Min((u32)item->text.size, (u32)sizeof(app->input) - 1 - pos);
MemCpy(app->input + pos, item->text.data, take);
pos += take;
if(item->suffix && pos + 1 < sizeof(app->input)) app->input[pos++] = item->suffix;
app->input_size = pos;
app->result_cursor = 0;
}
//- opening files (the :open command and drag and drop)
internal b32 app_path_is_directory(const char *path)
{
#if PLATFORM_WINDOWS
WCHAR wide[MAX_PATH * 2];
win32_utf8_to_wide(path, -1, wide, ArrayCount(wide));
DWORD attributes = GetFileAttributesW(wide);
return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY);
#else
struct stat st;
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
#endif
}
internal void app_refit_canvases(App *app)
{
app->active_node = app->active_edge = -1;
for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true;
}
// NOTE: a .dit / .dit.txt replaces the diagram (undoable), a directory runs the C/C++ scanner
internal b32 app_open_file(App *app, const char *path)
{
Diagram *dia = app->diagram;
diagram_push_undo(dia);
if(app_path_is_directory(path)) {
u32 found = diagram_scan_cpp(dia, path);
app_refit_canvases(app);
app_message(app, "scanned %s -> %u nodes, %u edges", path, found, dia->live_edges);
return true;
}
if(diagram_load_file(dia, path)) {
app_refit_canvases(app);
app_message(app, "loaded %s (%u nodes, %u edges)", path, dia->live_nodes, dia->live_edges);
return true;
}
app_message(app, "could not load %s", path);
return false;
}
internal void app_handle_drops(App *app, PlatformInput *input)
{
if(input->drop_count == 0) return;
Log("drop: %u path(s), first: %s", input->drop_count, input->drop_paths[0]);
// NOTE: one diagram per window for now, the first file wins
app_open_file(app, input->drop_paths[0]);
if(input->drop_count > 1) {
app_message(app, "opened %s (%u files dropped, one diagram at a time)", input->drop_paths[0], input->drop_count);
}
}
internal void command_run(App *app)
{
Diagram *dia = app->diagram;
char text[sizeof(app->input) + 1];
MemCpy(text, app->input, app->input_size);
text[app->input_size] = 0;
char *arg = strchr(text, ' ');
if(arg) { *arg++ = 0; while(*arg == ' ') ++arg; }
if(!strcmp(text, "q") || !strcmp(text, "q!") || !strcmp(text, "qa") || !strcmp(text, "wq")) {
app->quit = true;
} else if((!strcmp(text, "view") || !strcmp(text, "vs") || !strcmp(text, "sp")) && arg && arg[0]) {
ViewType type;
if(view_resolve(arg, &type)) {
if(!strcmp(text, "vs")) layout_split_with(&app->layout, (ViewKind)type.kind, 0, type.group);
else if(!strcmp(text, "sp")) layout_split_with(&app->layout, (ViewKind)type.kind, 1, type.group);
else layout_open(&app->layout, (ViewKind)type.kind, type.group);
} else {
app_message(app, "unknown view: %s", arg);
}
} else if(!strcmp(text, "set") && arg) {
if(settings_set(&app->settings, arg)) { app_apply_settings(app); app_message(app, "set %s", arg); }
else app_message(app, "unknown setting or value: %s", arg);
} else if(!strcmp(text, "zoom") && arg) {
app->settings.zoom = (f32)atof(arg);
app_apply_settings(app);
} else if(!strcmp(text, "layout")) {
diagram_push_undo(dia);
if(!arg || !strcasecmp(arg, "layered") || !strcasecmp(arg, "td")) diagram_layout_layered(dia, false);
else if(!strcasecmp(arg, "lr")) diagram_layout_layered(dia, true);
else if(!strcasecmp(arg, "force")) diagram_layout_force(dia, 60);
else if(!strcasecmp(arg, "grid")) diagram_layout_grid(dia);
else if(!strncasecmp(arg, "seq", 3)) diagram_layout_lifelines_beside(dia);
for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true;
app_message(app, "layout complete in %.2f ms", dia->layout_ms);
} else if(!strcmp(text, "route") && arg) {
if(!strncasecmp(arg, "ortho", 5)) dia->default_routing = ROUTE_ORTHOGONAL;
else if(!strncasecmp(arg, "str", 3)) dia->default_routing = ROUTE_STRAIGHT;
else if(!strncasecmp(arg, "bez", 3)) dia->default_routing = ROUTE_BEZIER;
app->settings.default_routing = dia->default_routing;
app_message(app, "routing: %s", edge_routing_name(dia->default_routing));
} else if(!strcmp(text, "align") && arg) {
diagram_push_undo(dia);
if(!strcasecmp(arg, "left")) diagram_align_selected(dia, ALIGN_LEFT);
else if(!strcasecmp(arg, "right")) diagram_align_selected(dia, ALIGN_RIGHT);
else if(!strcasecmp(arg, "top")) diagram_align_selected(dia, ALIGN_TOP);
else if(!strcasecmp(arg, "bot") || !strcasecmp(arg, "bottom")) diagram_align_selected(dia, ALIGN_BOTTOM);
else if(!strcasecmp(arg, "cx")) diagram_align_selected(dia, ALIGN_CENTER_X);
else if(!strcasecmp(arg, "cy")) diagram_align_selected(dia, ALIGN_CENTER_Y);
else if(!strcasecmp(arg, "dh")) diagram_align_selected(dia, DISTRIBUTE_H);
else if(!strcasecmp(arg, "dv")) diagram_align_selected(dia, DISTRIBUTE_V);
} else if(!strcmp(text, "add") && arg) {
char kind_s[32] = {};
const char *rest = dsl_read_quoted(arg, kind_s, sizeof(kind_s));
rest = dsl_skip_ws(rest);
s32 k = dsl_match_node_kind(kind_s);
if(k < 0) { k = NODE_STRUCT; rest = arg; }
View *cv = find_canvas_view(app);
diagram_push_undo(dia);
s32 ni = diagram_add_node(dia, (NodeKind)k, rest[0] ? rest : node_kind_name((u32)k), cv ? cv->cam_x - 100.0f : 100.0f, cv ? cv->cam_y - 40.0f : 100.0f);
if(ni >= 0) {
diagram_clear_selection(dia);
dia->nodes[ni].selected = true;
app->active_node = ni;
app->active_edge = -1;
app_message(app, "added %s \"%s\"", node_kind_name((u32)k), dia->nodes[ni].name);
}
} else if(!strcmp(text, "connect") && arg) {
char k1[48] = {}, k2[48] = {}, k3[24] = {};
const char *p = dsl_read_quoted(arg, k1, sizeof(k1));
p = dsl_read_quoted(p, k2, sizeof(k2));
dsl_read_quoted(p, k3, sizeof(k3));
s32 a = diagram_find_node_by_key(dia, k1), b = diagram_find_node_by_key(dia, k2);
if(a >= 0 && b >= 0) {
s32 ek = k3[0] ? dsl_match_edge_sym(k3) : (s32)app->canvas_connect_kind;
if(ek < 0) ek = EDGE_ASSOCIATION;
diagram_push_undo(dia);
diagram_add_edge(dia, a, b, (EdgeKind)ek, "");
app_message(app, "connected %s %s %s", k1, edge_kind_symbol((u32)ek), k2);
} else {
app_message(app, "node key not found (check Source view for keys)");
}
} else if(!strcmp(text, "tpl") && arg) {
diagram_push_undo(dia);
if(diagram_load_template(dia, arg)) {
app->active_node = app->active_edge = -1;
for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true;
app_message(app, "loaded template: %s (%u nodes, %u edges)", arg, dia->live_nodes, dia->live_edges);
} else {
app_message(app, "unknown template: %s", arg);
}
} else if(!strcmp(text, "scan") && arg) {
diagram_push_undo(dia);
u32 found = diagram_scan_cpp(dia, arg);
app->active_node = app->active_edge = -1;
for(u32 i = 0; i < UI_MAX_VIEWS; ++i) if(app->layout.views[i].kind == VIEW_CANVAS) app->layout.views[i].cam_fit_pending = true;
app_message(app, "scanned %s -> %u nodes, %u edges", arg, found, dia->live_edges);
} else if(!strcmp(text, "save")) {
const char *path = (arg && arg[0]) ? arg : (dia->file_path[0] ? dia->file_path : "diagram.dit.txt");
if(diagram_save_file(dia, path)) app_message(app, "saved %s", path);
else app_message(app, "failed to save %s", path);
} else if(!strcmp(text, "help")) {
app_open_help(app, arg);
} else if(!strcmp(text, "open") && arg) {
app_open_file(app, arg);
} else if(!strcmp(text, "export") && arg) {
// :export <file> [option]: png takes a scale, mermaid a style
char path[512], option[32] = {};
const char *rest = dsl_read_quoted(arg, path, sizeof(path));
dsl_read_quoted(rest, option, sizeof(option));
if(path_has_extension(path, ".png")) {
png_export_begin(app, path, option[0] ? (f32)atof(option) : 2.0f);
} else {
char message[256];
diagram_export_by_extension(dia, path, option, app->settings.colors, message, sizeof(message));
app_message(app, "%s", message);
}
} else if(!strcmp(text, "import") && arg) {
ScratchArena sc = ScratchGet();
String8 file = FileLoad(sc.arena, str8(arg));
if(!file.size) app_message(app, "could not read %s", arg);
else {
// NOTE: through the clipboard path: same format detection, layout and placement
free(app->clip);
app->clip = (char *)malloc(file.size + 1);
app->clip_size = app->clip ? file.size : 0;
if(app->clip) { MemCpy(app->clip, file.data, file.size); app->clip[file.size] = 0; }
u64 keep = app->clip_system_hash;
app->clip_system_hash = 0;
PlatformWindow *window = app->window;
app->window = NULL; // read the register, not the system clipboard
View *cv = find_canvas_view(app);
app_paste(app, true, cv ? cv->cam_x : 0.0f, cv ? cv->cam_y : 0.0f);
app->window = window;
app->clip_system_hash = keep;
}
scratch_end(&sc);
} else if(!strcmp(text, "doc")) {
if(app->active_node >= 0 && dia->nodes[app->active_node].alive) doc_edit_open(app, DOC_TARGET_NODE, app->active_node);
else if(app->active_edge >= 0 && dia->edges[app->active_edge].alive) doc_edit_open(app, DOC_TARGET_EDGE, app->active_edge);
else doc_edit_open(app, DOC_TARGET_DIAGRAM, -1);
} else if(!strcmp(text, "link")) {
if(app->active_node < 0 || !dia->nodes[app->active_node].alive) app_message(app, "select a node first");
else {
diagram_push_undo(dia);
snprintf(dia->nodes[app->active_node].link, sizeof(dia->nodes[app->active_node].link), "%s", arg ? arg : "");
dia->dirty = true;
++dia->version;
app_message(app, arg && arg[0] ? "link: %s" : "link cleared", arg ? arg : "");
}
} else if(!strcmp(text, "browse")) {
app_open_link(app, app->active_node, true);
} else if(!strcmp(text, "src")) {
app_open_link(app, app->active_node, false);
} else if(!strcmp(text, "repo")) {
if(!arg || !arg[0]) {
char root[1024];
diagram_link_root(dia, root, sizeof(root));
app_message(app, "repo: %s @ %s, links from %s", dia->repo[0] ? dia->repo : "(none)", dia->repo_ref[0] ? dia->repo_ref : "HEAD", root);
} else if(!strcmp(arg, "clear")) {
dia->repo[0] = dia->repo_ref[0] = 0;
dia->dirty = true;
app_message(app, "repo cleared");
} else if(!strcmp(arg, "detect")) {
char base[1024], top[1024];
diagram_link_root(dia, base, sizeof(base));
dia->repo[0] = dia->repo_ref[0] = 0;
if(diagram_detect_repo(dia, base, top, sizeof(top))) app_message(app, "repo: %s @ %s (from %s/.git)", dia->repo[0] ? dia->repo : "(no remote)", dia->repo_ref[0] ? dia->repo_ref : "HEAD", top);
else app_message(app, "no git checkout around %s", base);
dia->dirty = true;
} else {
char url[256] = {}, ref[64] = {};
const char *rest = dsl_read_quoted(arg, url, sizeof(url));
dsl_read_quoted(rest, ref, sizeof(ref));
snprintf(dia->repo, sizeof(dia->repo), "%s", url);
if(ref[0]) snprintf(dia->repo_ref, sizeof(dia->repo_ref), "%s", ref);
dia->dirty = true;
app_message(app, "repo: %s @ %s", dia->repo, dia->repo_ref[0] ? dia->repo_ref : "HEAD");
}
} else if(!strcmp(text, "root")) {
snprintf(dia->repo_root, sizeof(dia->repo_root), "%s", arg ? arg : "");
dia->dirty = true;
char root[1024];
diagram_link_root(dia, root, sizeof(root));
app_message(app, "links resolve from %s", root);
} else if(!strcmp(text, "copy")) {
u32 n = app_copy_selection(app, true);
app_message(app, n ? "copied %u node(s)" : "nothing selected", n);
} else if(!strcmp(text, "paste")) {
View *cv = find_canvas_view(app);
app_paste(app, true, cv ? cv->cam_x : 0.0f, cv ? cv->cam_y : 0.0f);
} else if(!strcmp(text, "straighten")) {
diagram_push_undo(dia);
u32 n = 0;
for(u32 i = 0; i < dia->edge_count; ++i) {
DiagramEdge *e = &dia->edges[i];
if(!e->alive || !e->waypoint_count) continue;
if(e->selected || (s32)i == app->active_edge || dia->nodes[e->src].selected || dia->nodes[e->dst].selected) { e->waypoint_count = 0; ++n; }
}
++dia->version;
app_message(app, "straightened %u edge(s)", n);
} else if(!strcmp(text, "seq")) {
diagram_push_undo(dia);
f32 x0 = 1e9f, y0 = 1e9f;
for(u32 i = 0; i < dia->node_count; ++i) {
if(dia->nodes[i].alive && node_kind_is_lifeline(dia->nodes[i].kind)) { x0 = Min(x0, dia->nodes[i].x); y0 = Min(y0, dia->nodes[i].y); }
}
if(x0 > 1e8f) app_message(app, "no lifelines (add one: :add lifeline Client)");
else { diagram_layout_sequence(dia, x0, y0); app_message(app, "sequence re-spaced"); }
} else if(!strcmp(text, "focus")) {
dia->focus_mode = !dia->focus_mode;
app_message(app, "focus mode: %s", dia->focus_mode ? "on" : "off");
} else if(!strcmp(text, "fit")) {
View *cv = find_canvas_view(app);
if(cv) canvas_fit_view(app, cv, false);
} else if(!strcmp(text, "clear")) {
diagram_push_undo(dia);
diagram_clear(dia);
app->active_node = app->active_edge = -1;
app_message(app, "cleared diagram");
} else if(!strcmp(text, "columns")) {
s32 v = panel_active_view(&app->layout, app->layout.focused);
if(v >= 0) app->layout.views[v].column_count = 0;
} else if(!strcmp(text, "reset")) {
ui_layout_default(&app->layout);
} else if(text[0]) {
app_message(app, "not a command: %s", text);
}
}
internal void draw_overlay(App *app, PlatformInput *input, Rect window)
{
if(app->overlay == UI_OVERLAY_NONE) return;
if(app->overlay == UI_OVERLAY_DOC) { doc_edit_draw(app, input, window); return; }
if(app->overlay == UI_OVERLAY_EDIT && app->edit_inline && app->edit_drawn) return;
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
Diagram *dia = app->diagram;
ScratchArena scratch = ScratchGet();
CompletionList *completions = NULL;
u32 shown = (app->overlay == UI_OVERLAY_SEARCH) ? Min(app->result_count, 12u) : 0;
if(app->overlay == UI_OVERLAY_COMMAND) {
completions = PushStruct(scratch.arena, CompletionList);
u32 count = command_completions(app, scratch.arena, completions);
app->result_cursor = (count == 0) ? 0 : Clamp(app->result_cursor, 0, (s32)count - 1);
shown = Min(count, 12u);
}
f32 w = Min(600.0f, rect_w(window) - 40);
f32 h = 40 + (f32)shown * UI_ROW_H + (shown ? 6 : 0);
Rect box = {window.x0 + (rect_w(window) - w) * 0.5f, window.y0 + rect_h(window) * 0.16f, 0, 0};
box.x1 = box.x0 + w;
box.y1 = box.y0 + h;
draw_rect(d, window, 0x00000066u, 0);
draw_shadow(d, box, 0x000000AAu, 0, 18);
draw_rect(d, box, COLOR_TAB_ACTIVE, 0);
draw_border(d, box, COLOR_FOCUS_EDGE, 0, 1);
String8 label = str8("/");
if(app->overlay == UI_OVERLAY_COMMAND) label = str8(":");
else if(app->overlay == UI_OVERLAY_EDIT) label = str8("edit = ");
f32 size = UI_FONT + 1;
f32 base = ui_baseline(app, FONT_MONO, size, box.y0, 40);
f32 lw = draw_text(d, fonts, FONT_MONO, size, box.x0 + 14, base, label, COLOR_TEXT_DIM);
f32 tw = draw_text(d, fonts, FONT_MONO, size, box.x0 + 14 + lw, base, ToString8Size(app->input, app->input_size), COLOR_TEXT);
draw_rect(d, Rect{box.x0 + 15 + lw + tw, box.y0 + 11, box.x0 + 23 + lw + tw, box.y0 + 29}, 0xFFFFFF99u, 0);
f32 y = box.y0 + 40;
if(completions) {
u32 first = (app->result_cursor >= (s32)shown) ? (u32)app->result_cursor - shown + 1 : 0;
for(u32 row = 0; row < shown; ++row) {
u32 index = first + row;
Completion *item = &completions->items[index];
Rect rect = {box.x0 + 4, y, box.x1 - 4, y + UI_ROW_H};
if((s32)index == app->result_cursor) draw_rect(d, rect, COLOR_SELECTED, 0);
if(ui_clicked(input, rect)) command_accept_completion(app, item);
f32 b = ui_baseline(app, FONT_MONO, UI_FONT, y, UI_ROW_H);
f32 name = draw_text(d, fonts, FONT_MONO, UI_FONT, rect.x0 + 10, b, item->text, COLOR_TEXT);
f32 dx = Max(name + 24, rect_w(rect) * 0.28f);
draw_text_fit(d, fonts, FONT_MONO, UI_FONT_SMALL, rect.x0 + dx, b, rect_w(rect) - dx - 10, item->detail, COLOR_TEXT_DIM);
y += UI_ROW_H;
}
shown = 0;
}
for(u32 index = 0; index < shown; ++index) {
SearchResult *r = &app->results[index];
Rect rect = {box.x0 + 4, y, box.x1 - 4, y + UI_ROW_H};
if((s32)index == app->result_cursor) draw_rect(d, rect, COLOR_SELECTED, 0);
if(ui_clicked(input, rect)) { search_accept(app, *r); app->overlay = UI_OVERLAY_NONE; }
String8 kind = str8("node"), name = str8("");
if(r->kind == 0) {
kind = str8((char *)node_kind_name(dia->nodes[r->index].kind));
name = str8(dia->nodes[r->index].name);
} else if(r->kind == 1) {
kind = str8("edge");
name = ui_format(scratch.arena, "%s %s %s : %s", dia->nodes[dia->edges[r->index].src].name, edge_kind_symbol(dia->edges[r->index].kind), dia->nodes[dia->edges[r->index].dst].name, dia->edges[r->index].label);
} else {
kind = str8("member");
name = ui_format(scratch.arena, "%s.%s : %s", dia->nodes[r->parent_node].name, dia->members[r->index].name, dia->members[r->index].type);
}
f32 b = ui_baseline(app, FONT_REGULAR, UI_FONT, y, UI_ROW_H);
draw_text(d, fonts, FONT_MONO, UI_FONT_SMALL, rect.x0 + 10, b, kind, COLOR_TEXT_FAINT);
draw_text_fit(d, fonts, FONT_REGULAR, UI_FONT, rect.x0 + 86, b, rect_w(rect) - 96, name, COLOR_TEXT);
y += UI_ROW_H;
}
scratch_end(&scratch);
}
//- keyboard (vim motions everywhere + canvas hotkeys)
internal View *focused_view(App *app)
{
s32 view = panel_active_view(&app->layout, app->layout.focused);
return view >= 0 ? &app->layout.views[view] : NULL;
}
internal void ui_keyboard_overlay(App *app, PlatformInput *input)
{
if(app->overlay == UI_OVERLAY_DOC) { doc_edit_keys(app, input); return; }
for(u32 index = 0; index < input->key_count; ++index) {
PlatformKeyEvent *event = &input->keys[index];
u32 sym = event->keysym;
b32 ctrl = (event->mods & PLATFORM_MOD_CTRL) != 0;
if(sym == XKB_KEY_Escape || (ctrl && (sym == XKB_KEY_bracketleft || sym == XKB_KEY_c))) {
app->overlay = UI_OVERLAY_NONE;
return;
}
if(sym == XKB_KEY_Return || sym == XKB_KEY_KP_Enter) {
UIOverlay ov = app->overlay;
app->overlay = UI_OVERLAY_NONE;
if(ov == UI_OVERLAY_SEARCH && app->result_count > 0) search_accept(app, app->results[app->result_cursor]);
else if(ov == UI_OVERLAY_COMMAND) command_run(app);
else if(ov == UI_OVERLAY_EDIT) edit_accept(app);
return;
}
if(sym == XKB_KEY_BackSpace) {
if(app->input_size == 0 && app->overlay != UI_OVERLAY_EDIT) { app->overlay = UI_OVERLAY_NONE; return; }
while(app->input_size > 0) {
u8 c = (u8)app->input[--app->input_size];
if((c & 0xC0) != 0x80) break;
}
}
if(ctrl && sym == XKB_KEY_u) app->input_size = 0;
if(ctrl && sym == XKB_KEY_v && app->window) {
// NOTE: one line fields: newlines become spaces
ScratchArena sc = ScratchGet();
String8 paste = PlatformClipboardGet(app->window, sc.arena);
for(u64 i = 0; i < paste.size && app->input_size + 1 < sizeof(app->input); ++i) {
char c = (char)paste.data[i];
if(c == '\r') continue;
app->input[app->input_size++] = (c == '\n' || c == '\t') ? ' ' : c;
}
scratch_end(&sc);
if(app->overlay == UI_OVERLAY_SEARCH) search_update(app);
continue;
}
if(ctrl && sym == XKB_KEY_w) {
while(app->input_size > 0 && app->input[app->input_size - 1] == ' ') --app->input_size;
while(app->input_size > 0 && app->input[app->input_size - 1] != ' ') --app->input_size;
}
if(app->overlay == UI_OVERLAY_COMMAND) {
ScratchArena sc = ScratchGet();
CompletionList *list = PushStruct(sc.arena, CompletionList);
u32 cnt = command_completions(app, sc.arena, list);
if(sym == XKB_KEY_Tab && cnt) command_accept_completion(app, &list->items[Clamp(app->result_cursor, 0, (s32)cnt - 1)]);
s32 step = 0;
if(sym == XKB_KEY_Down || (ctrl && (sym == XKB_KEY_n || sym == XKB_KEY_j))) step = 1;
if(sym == XKB_KEY_Up || sym == XKB_KEY_ISO_Left_Tab || (ctrl && (sym == XKB_KEY_p || sym == XKB_KEY_k))) step = -1;
if(step && cnt) app->result_cursor = (app->result_cursor + step + (s32)cnt) % (s32)cnt;
scratch_end(&sc);
continue;
}
s32 move = 0;
if(sym == XKB_KEY_Down || sym == XKB_KEY_Tab || (ctrl && (sym == XKB_KEY_n || sym == XKB_KEY_j))) move = 1;
if(sym == XKB_KEY_Up || sym == XKB_KEY_ISO_Left_Tab || (ctrl && (sym == XKB_KEY_p || sym == XKB_KEY_k))) move = -1;
if(move && app->result_count) app->result_cursor = (app->result_cursor + move + (s32)app->result_count) % (s32)app->result_count;
}
if(!app->overlay_opened && input->text_size > 0 && !(input->mods & PLATFORM_MOD_CTRL)) {
u32 take = Min((u32)sizeof(app->input) - 1 - app->input_size, input->text_size);
MemCpy(app->input + app->input_size, input->text, take);
app->input_size += take;
if(app->overlay == UI_OVERLAY_COMMAND) app->result_cursor = 0;
}
if(app->overlay == UI_OVERLAY_SEARCH) search_update(app);
}
internal void delete_selected(App *app)
{
Diagram *dia = app->diagram;
u32 del_n = 0, del_e = 0;
diagram_push_undo(dia);
for(u32 i = 0; i < dia->edge_count; ++i) {
if(dia->edges[i].alive && (dia->edges[i].selected || (s32)i == app->active_edge)) {
diagram_delete_edge(dia, (s32)i);
++del_e;
}
}
for(u32 i = 0; i < dia->node_count; ++i) {
if(dia->nodes[i].alive && (dia->nodes[i].selected || (s32)i == app->active_node)) {
diagram_delete_node(dia, (s32)i);
++del_n;
}
}
app->active_node = -1;
app->active_edge = -1;
diagram_rebuild_tags(dia);
if(del_n || del_e) app_message(app, "deleted %u node(s), %u edge(s) (u to undo)", del_n, del_e);
}
// NOTE: the floating inspector takes the keys while it has focus and is on the focused canvas
internal View *keyboard_view(App *app)
{
View *view = focused_view(app);
if(app->card_focused) {
if(app->card_visible && view && view->kind == VIEW_CANVAS) return &app->card_view;
app->card_focused = false;
}
return view;
}
internal u32 keyboard_rows(App *app, View *view, Row *rows)
{
if(!view || view->kind == VIEW_CANVAS) return 0;
return (view == &app->card_view) ? card_rows(app, rows, UI_MAX_ROWS) : view_rows(app, view, rows, UI_MAX_ROWS);
}
internal void ui_keyboard(App *app, PlatformInput *input)
{
if(app->overlay != UI_OVERLAY_NONE) {
ui_keyboard_overlay(app, input);
app->overlay_opened = false;
return;
}
if(input->key_count == 0) return;
Diagram *dia = app->diagram;
View *view = keyboard_view(app);
ScratchArena scratch = ScratchGet();
Row *rows = PushArray(scratch.arena, Row, UI_MAX_ROWS);
u32 count = keyboard_rows(app, view, rows);
for(u32 index = 0; index < input->key_count && app->overlay == UI_OVERLAY_NONE; ++index) {
PlatformKeyEvent *event = &input->keys[index];
u32 sym = event->keysym;
b32 ctrl = (event->mods & PLATFORM_MOD_CTRL) != 0;
b32 shift = (event->mods & PLATFORM_MOD_SHIFT) != 0;
u32 pending = app->pending;
u32 n = app->count ? app->count : 1;
s32 cursor = view ? view->cursor : 0;
s32 page = view ? Max((s32)view->visible_rows, 1) : 10;
Row *row = (view && count) ? &rows[Clamp(cursor, 0, (s32)count - 1)] : NULL;
if(sym == XKB_KEY_Shift_L || sym == XKB_KEY_Shift_R || sym == XKB_KEY_Control_L || sym == XKB_KEY_Control_R ||
sym == XKB_KEY_Alt_L || sym == XKB_KEY_Alt_R || sym == XKB_KEY_Super_L || sym == XKB_KEY_Super_R) continue;
app->pending = 0;
//- two-key commands
if(pending) {
Layout *layout = &app->layout;
if(pending == 'g') {
if(sym == XKB_KEY_g && view && view->kind != VIEW_CANVAS) view->cursor = 0;
if(sym == XKB_KEY_x) app_open_link(app, app->active_node, true);
if(sym == XKB_KEY_f) app_open_link(app, app->active_node, false);
if(sym == XKB_KEY_t || sym == XKB_KEY_T) {
Panel *p = &layout->panels[layout->focused];
if(p->tab_count) p->active_tab = (p->active_tab + (sym == XKB_KEY_t ? 1 : p->tab_count - 1)) % p->tab_count;
}
} else if(pending == 'd' && sym == XKB_KEY_d) {
if(view && view->kind == VIEW_INSPECTOR && row && row->kind == ROW_MEMBER) {
diagram_push_undo(dia);
diagram_delete_member(dia, (s32)row->a);
app_message(app, "deleted member");
} else if(view && view->kind == VIEW_INSPECTOR && row && row->kind == ROW_SECTION) {
diagram_push_undo(dia);
diagram_delete_section(dia, (s32)row->a);
app_message(app, "deleted section");
} else {
app_copy_selection(app, false); // like vim: dd yanks, p puts it back
delete_selected(app);
}
} else if(pending == 'y' && sym == XKB_KEY_y) {
u32 copied = app_copy_selection(app, true);
if(copied) app_message(app, "copied %u node(s) (p pastes at the mouse, P in place)", copied);
else app_message(app, "nothing selected to copy");
} else if(pending == 'w') {
switch(sym) {
case XKB_KEY_h: case XKB_KEY_Left: layout_focus_direction(layout, -1, 0); break;
case XKB_KEY_l: case XKB_KEY_Right: layout_focus_direction(layout, 1, 0); break;
case XKB_KEY_k: case XKB_KEY_Up: layout_focus_direction(layout, 0, -1); break;
case XKB_KEY_j: case XKB_KEY_Down: layout_focus_direction(layout, 0, 1); break;
case XKB_KEY_w: case XKB_KEY_W: layout_focus_next(layout); break;
case XKB_KEY_v: layout_split_focused(layout, 0); break;
case XKB_KEY_s: layout_split_focused(layout, 1); break;
case XKB_KEY_c: case XKB_KEY_q: {
Panel *p = &layout->panels[layout->focused];
if(p->tab_count) layout_close_view(layout, layout->focused, p->active_tab);
} break;
case XKB_KEY_equal: {
for(s32 i = 0; i < UI_MAX_PANELS; ++i) if(layout->panels[i].kind == PANEL_SPLIT && layout->panels[i].ratio > 0) layout->panels[i].ratio = 0.5f;
} break;
}
}
app->count = 0;
view = keyboard_view(app);
count = keyboard_rows(app, view, rows);
continue;
}
//- the manual, from anywhere
if(sym == XKB_KEY_F1) {
app_open_help(app, NULL);
app->count = 0;
view = keyboard_view(app);
count = keyboard_rows(app, view, rows);
continue;
}
//- numeric prefix
if(!ctrl && ((sym >= XKB_KEY_1 && sym <= XKB_KEY_9) || (sym == XKB_KEY_0 && app->count))) {
app->count = Min(app->count * 10 + (sym - XKB_KEY_0), 9999u);
continue;
}
//- global ctrl shortcuts (zoom, panels, undo/redo)
if(ctrl) {
switch(sym) {
case XKB_KEY_equal: case XKB_KEY_plus: app->settings.zoom += 0.1f; app_apply_settings(app); continue;
case XKB_KEY_minus: app->settings.zoom -= 0.1f; app_apply_settings(app); continue;
case XKB_KEY_0: app->settings.zoom = UI_ZOOM_DEFAULT; app_apply_settings(app); continue;
case XKB_KEY_w: app->pending = 'w'; continue;
case XKB_KEY_h: case XKB_KEY_Left: layout_focus_direction(&app->layout, -1, 0); break;
case XKB_KEY_l: case XKB_KEY_Right: layout_focus_direction(&app->layout, 1, 0); break;
case XKB_KEY_k: case XKB_KEY_Up: layout_focus_direction(&app->layout, 0, -1); break;
case XKB_KEY_j: case XKB_KEY_Down: layout_focus_direction(&app->layout, 0, 1); break;
case XKB_KEY_Tab: layout_cycle_views(&app->layout, 1); break;
case XKB_KEY_ISO_Left_Tab: layout_cycle_views(&app->layout, -1); break;
case XKB_KEY_z: if(diagram_undo(dia)) app_message(app, "undo"); break;
case XKB_KEY_c: case XKB_KEY_x: {
if(view && view->kind != VIEW_CANVAS && view->kind != VIEW_OUTLINE) break;
u32 copied = app_copy_selection(app, true);
if(copied && sym == XKB_KEY_x) delete_selected(app);
app_message(app, copied ? (sym == XKB_KEY_x ? "cut %u node(s)" : "copied %u node(s)") : "nothing selected", copied);
} break;
case XKB_KEY_v: {
View *cv = (view && view->kind == VIEW_CANVAS) ? view : find_canvas_view(app);
b32 mouse = view && view->kind == VIEW_CANVAS;
app_paste(app, true, mouse ? app->canvas_last_wx : (cv ? cv->cam_x : 0.0f), mouse ? app->canvas_last_wy : (cv ? cv->cam_y : 0.0f));
} break;
case XKB_KEY_r: if(diagram_redo(dia)) app_message(app, "redo"); break;
case XKB_KEY_d: if(view && view->kind != VIEW_CANVAS) view->cursor = Clamp(cursor + (s32)n * page / 2, 0, Max((s32)count - 1, 0)); break;
case XKB_KEY_u: if(view && view->kind != VIEW_CANVAS) view->cursor = Clamp(cursor - (s32)n * page / 2, 0, Max((s32)count - 1, 0)); break;
}
app->count = 0;
continue;
}
//- floating inspector: esc / tab give the keys back to the canvas
if(view == &app->card_view) {
b32 handled = true;
if(sym == XKB_KEY_Escape || sym == XKB_KEY_Tab || sym == XKB_KEY_ISO_Left_Tab) {
app->card_focused = false;
} else if(sym == XKB_KEY_I) {
app->settings.floating_inspector = false;
app->card_focused = false;
app_message(app, "floating inspector: off (I brings it back)");
} else if((sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) && row && row->kind == ROW_MEMBER) {
diagram_push_undo(dia);
diagram_delete_member(dia, (s32)row->a);
app_message(app, "deleted member (u to undo)");
} else if((sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) && row && row->kind == ROW_SECTION) {
diagram_push_undo(dia);
diagram_delete_section(dia, (s32)row->a);
app_message(app, "deleted section (u to undo)");
} else if(sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) {
// NOTE: never delete the whole node from inside its own inspector
} else if(row && row->kind == ROW_SECTION && (sym == XKB_KEY_h || sym == XKB_KEY_Left || sym == XKB_KEY_l || sym == XKB_KEY_Right)) {
NodeSection *sec = &dia->sections[row->a];
sec->collapsed = (sym == XKB_KEY_h || sym == XKB_KEY_Left);
diagram_measure_node(dia, sec->node);
++dia->version;
} else {
handled = false;
}
if(handled) {
app->count = 0;
view = keyboard_view(app);
count = keyboard_rows(app, view, rows);
continue;
}
}
//- global non-ctrl shortcuts
if(sym == XKB_KEY_colon) {
app->overlay = UI_OVERLAY_COMMAND; app->overlay_opened = true; app->input_size = 0; app->result_cursor = 0;
continue;
}
if(sym == XKB_KEY_slash || sym == XKB_KEY_question) {
app->overlay = UI_OVERLAY_SEARCH; app->overlay_opened = true; app->input_size = 0; app->result_count = 0; app->result_cursor = 0;
continue;
}
if(sym == XKB_KEY_u) { if(diagram_undo(dia)) app_message(app, "undo"); app->count = 0; continue; }
if(sym == XKB_KEY_F) { dia->focus_mode = !dia->focus_mode; app_message(app, "focus mode: %s", dia->focus_mode ? "on" : "off"); app->count = 0; continue; }
if(sym == XKB_KEY_Escape) { diagram_clear_selection(dia); app->active_node = app->active_edge = -1; app->message[0] = 0; app->count = 0; continue; }
if(sym == XKB_KEY_Delete || sym == XKB_KEY_BackSpace) { delete_selected(app); app->count = 0; continue; }
if(sym == XKB_KEY_q) { app->quit = true; return; }
//- Canvas-focused keyboard actions
if(view && view->kind == VIEW_CANVAS) {
f32 pan = 80.0f / Max(view->cam_zoom, 0.1f) * (f32)n;
f32 nud = (f32)Max(ui_theme.grid_size, 8u) * (f32)n;
switch(sym) {
case XKB_KEY_h: case XKB_KEY_Left: view->cam_target_x -= pan; break;
case XKB_KEY_l: case XKB_KEY_Right: view->cam_target_x += pan; break;
case XKB_KEY_k: case XKB_KEY_Up: view->cam_target_y -= pan; break;
case XKB_KEY_j: case XKB_KEY_Down: view->cam_target_y += pan; break;
case XKB_KEY_H: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, -nud, 0); diagram_fit_containers(dia); } break;
case XKB_KEY_L: {
if(shift && app->active_node >= 0 && diagram_selected_node_count(dia) > 0 && app->count) {
diagram_push_undo(dia); diagram_move_node(dia, app->active_node, nud, 0); diagram_fit_containers(dia);
} else {
diagram_push_undo(dia);
diagram_layout_layered(dia, false);
canvas_fit_view(app, view, false);
app_message(app, "layered layout (%.2f ms)", dia->layout_ms);
}
} break;
case XKB_KEY_K: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, 0, -nud); diagram_fit_containers(dia); } break;
case XKB_KEY_J: if(app->active_node >= 0) { diagram_push_undo(dia); diagram_move_node(dia, app->active_node, 0, nud); diagram_fit_containers(dia); } break;
case XKB_KEY_plus: case XKB_KEY_equal: case XKB_KEY_KP_Add: view->cam_target_zoom = Clamp(view->cam_target_zoom * 1.2f, 0.08f, 4.0f); break;
case XKB_KEY_minus: case XKB_KEY_KP_Subtract: view->cam_target_zoom = Clamp(view->cam_target_zoom / 1.2f, 0.08f, 4.0f); break;
case XKB_KEY_0: view->cam_target_zoom = 1.0f; break;
case XKB_KEY_f: canvas_fit_view(app, view, false); break;
case XKB_KEY_Z: canvas_fit_view(app, view, true); break;
case XKB_KEY_g: app->pending = 'g'; continue;
case XKB_KEY_G: app->settings.grid_style = (app->settings.grid_style + 1) % GRID_STYLE_COUNT; app_message(app, "grid: %s", ui_grid_names[app->settings.grid_style]); break;
case XKB_KEY_S: app->settings.snap_to_grid = !app->settings.snap_to_grid; app_message(app, "snap: %s", app->settings.snap_to_grid ? "on" : "off"); break;
case XKB_KEY_m: app->settings.show_minimap = !app->settings.show_minimap; break;
case XKB_KEY_r:
dia->default_routing = (dia->default_routing - 1 + 1) % 3 + 1;
app->settings.default_routing = dia->default_routing;
app_message(app, "routing: %s", edge_routing_name(dia->default_routing));
break;
case XKB_KEY_e:
app->canvas_connect_kind = (app->canvas_connect_kind + 1) % EDGE_KIND_COUNT;
app_message(app, "edge tool: %s (%s) — drag a port or right-drag a node", edge_kind_name(app->canvas_connect_kind), edge_kind_symbol(app->canvas_connect_kind));
break;
case XKB_KEY_a: {
diagram_push_undo(dia);
s32 ni = diagram_add_node(dia, NODE_STRUCT, "NewStruct", app->canvas_last_wx - 100.0f, app->canvas_last_wy - 36.0f);
if(ni >= 0) {
s32 s = diagram_add_section(dia, ni, "fields");
diagram_add_member(dia, s, '+', "field", "u32", "");
diagram_clear_selection(dia);
dia->nodes[ni].selected = true;
app->active_node = ni;
app->active_edge = -1;
start_edit(app, EDIT_NODE_NAME, ni, dia->nodes[ni].name);
}
} break;
case XKB_KEY_c:
if(app->active_node >= 0 && dia->nodes[app->active_node].alive) {
diagram_push_undo(dia);
dia->nodes[app->active_node].collapsed = !dia->nodes[app->active_node].collapsed;
diagram_measure_node(dia, app->active_node);
}
break;
case XKB_KEY_I:
app->settings.floating_inspector = !app->settings.floating_inspector;
app_message(app, "floating inspector: %s", app->settings.floating_inspector ? "on" : "off");
break;
case XKB_KEY_Return: case XKB_KEY_KP_Enter: case XKB_KEY_i:
// enter steps into the floating inspector when it is up, i always renames
if(sym != XKB_KEY_i && app->card_visible) { app->card_focused = true; break; }
if(app->active_node >= 0 && dia->nodes[app->active_node].alive) start_edit(app, EDIT_NODE_NAME, app->active_node, dia->nodes[app->active_node].name);
else if(app->active_edge >= 0 && dia->edges[app->active_edge].alive) start_edit(app, EDIT_EDGE_LABEL, app->active_edge, dia->edges[app->active_edge].label);
break;
case XKB_KEY_d: app->pending = 'd'; continue;
case XKB_KEY_y: app->pending = 'y'; continue;
case XKB_KEY_p: app_paste(app, true, app->canvas_last_wx, app->canvas_last_wy); break;
case XKB_KEY_P: app_paste(app, false, 0.0f, 0.0f); break;
case XKB_KEY_x: {
u32 copied = app_copy_selection(app, false);
if(copied) { delete_selected(app); app_message(app, "cut %u node(s) (p pastes)", copied); }
} break;
case XKB_KEY_D:
if(app->active_node >= 0 && dia->nodes[app->active_node].alive) {
diagram_push_undo(dia);
s32 cp = diagram_duplicate_node(dia, app->active_node, 36.0f, 36.0f);
if(cp >= 0) {
diagram_clear_selection(dia);
dia->nodes[cp].selected = true;
app->active_node = cp;
app_message(app, "duplicated %s", dia->nodes[cp].name);
}
}
break;
case XKB_KEY_Tab: {
if(dia->live_nodes > 0) {
s32 start = (app->active_node >= 0) ? app->active_node : -1;
for(u32 step = 1; step <= dia->node_count; ++step) {
u32 idx = (u32)(start + (s32)step) % dia->node_count;
if(dia->nodes[idx].alive && !diagram_node_hidden(dia, (s32)idx)) {
diagram_clear_selection(dia);
dia->nodes[idx].selected = true;
app->active_node = (s32)idx;
app->active_edge = -1;
canvas_pan_to_node(app, view, (s32)idx);
break;
}
}
}
} break;
}
app->count = 0;
continue;
}
//- Table-focused vim motions
s32 motion = INT32_MIN;
switch(sym) {
case XKB_KEY_j: case XKB_KEY_Down: motion = cursor + (s32)n; break;
case XKB_KEY_k: case XKB_KEY_Up: motion = cursor - (s32)n; break;
case XKB_KEY_G: case XKB_KEY_End: motion = app->count ? (s32)app->count - 1 : (s32)count - 1; break;
case XKB_KEY_Home: motion = 0; break;
case XKB_KEY_Page_Down: motion = cursor + page; break;
case XKB_KEY_Page_Up: motion = cursor - page; break;
case XKB_KEY_g: case XKB_KEY_d: case XKB_KEY_y: app->pending = (u32)('a' + (sym - XKB_KEY_a)); continue;
case XKB_KEY_l: case XKB_KEY_Right:
if(row && row->expandable && row->kind == ROW_NODE) view_set_expanded(view, row->a, true);
else if(view && row) view_adjust(app, view, row, 1);
break;
case XKB_KEY_h: case XKB_KEY_Left:
if(row && row->expandable && row->kind == ROW_NODE) view_set_expanded(view, row->a, false);
else if(view && row) view_adjust(app, view, row, -1);
break;
case XKB_KEY_Return: case XKB_KEY_KP_Enter: case XKB_KEY_o: case XKB_KEY_space:
if(view && row) view_activate(app, view, row);
break;
case XKB_KEY_a:
if(view && view->kind == VIEW_INSPECTOR && app->active_node >= 0 && dia->nodes[app->active_node].alive) {
diagram_push_undo(dia);
s32 sec = -1;
if(row && row->kind == ROW_SECTION) sec = (s32)row->a;
else if(row && row->kind == ROW_MEMBER) sec = (s32)row->b;
else sec = dia->nodes[app->active_node].last_section;
if(sec < 0) sec = diagram_add_section(dia, app->active_node, "fields");
s32 m = diagram_add_member(dia, sec, '+', "new_field", "u32", "");
if(m >= 0) start_edit(app, EDIT_MEMBER_LINE, m, "+ new_field : u32");
}
break;
case XKB_KEY_A:
if(view && view->kind == VIEW_INSPECTOR && app->active_node >= 0 && dia->nodes[app->active_node].alive) {
diagram_push_undo(dia);
s32 sec = diagram_add_section(dia, app->active_node, "methods");
if(sec >= 0) start_edit(app, EDIT_SECTION_TITLE, sec, "methods");
}
break;
case XKB_KEY_Tab: layout_focus_next(&app->layout); break;
}
if(motion != INT32_MIN && view && count) {
view->cursor = Clamp(motion, 0, (s32)count - 1);
f32 y = (f32)view->cursor * UI_ROW_H;
f32 vh = (f32)view->visible_rows * UI_ROW_H;
if(y < view->scroll.target) view->scroll.target = y;
if(y + UI_ROW_H > view->scroll.target + vh) view->scroll.target = y + UI_ROW_H - vh;
if(rows[view->cursor].kind == ROW_NODE) {
diagram_clear_selection(dia);
dia->nodes[rows[view->cursor].a].selected = true;
app->active_node = (s32)rows[view->cursor].a;
app->active_edge = -1;
} else if(rows[view->cursor].kind == ROW_EDGE) {
diagram_clear_selection(dia);
dia->edges[rows[view->cursor].a].selected = true;
app->active_edge = (s32)rows[view->cursor].a;
app->active_node = -1;
}
}
app->count = 0;
view = keyboard_view(app);
count = keyboard_rows(app, view, rows);
}
scratch_end(&scratch);
}
//- frame
internal String8 app_font_file(App *app, u32 choice)
{
if(choice >= UI_FONT_CHOICES) return str8_zero();
if(app->font_files[choice].size == 0 && font_path_readable(font_choice(choice)->path)) {
app->font_files[choice] = font_load_path(app->arena, font_choice(choice)->path);
}
return app->font_files[choice];
}
internal void ui_frame(App *app, PlatformInput *input_raw)
{
app->animating = false;
app->ticking = false;
app->table_cursor = PLATFORM_CURSOR_DEFAULT;
app->canvas_cursor = PLATFORM_CURSOR_DEFAULT;
app->canvas_hover_node = -1;
app->canvas_hover_edge = -1;
ui_settings = &app->settings;
ui_theme = app->settings;
font_set_data(&app->fonts, FONT_REGULAR, app_font_file(app, app->settings.font_ui));
font_set_data(&app->fonts, FONT_SEMIBOLD, app_font_file(app, app->settings.font_bold));
font_set_data(&app->fonts, FONT_MONO, app_font_file(app, app->settings.font_mono));
f32 zoom = (app->settings.zoom > 0) ? app->settings.zoom : 1.0f;
if(app->ui_scale > 0.25f && app->ui_scale < 4.0f) zoom *= app->ui_scale;
font_set_scale(&app->fonts, (f32)app->scale * zoom);
PlatformInput input = *input_raw;
input.mouse_x /= zoom;
input.mouse_y /= zoom;
if(!layout_valid(&app->layout)) ui_layout_default(&app->layout);
// NOTE: a png export takes over the window: one diagram tile per frame, no ui
if(app->png.state == PNG_TILES) {
png_export_frame(app);
return;
}
app_handle_drops(app, &input);
u64 t_keys = time_now_ns();
ui_keyboard(app, &input);
if(app->quit) return;
u64 t_keys_end = time_now_ns();
f32 w = (f32)app->width / zoom;
f32 h = (f32)app->height / zoom;
Rect window = {0, 0, w, h};
draw_begin(&app->draw, app->renderer.extent.width, app->renderer.extent.height, (f32)app->scale * zoom);
app->draw.rounding = 1.0f;
app->draw.text_gamma = app->settings.text_gamma;
if(PlatformWindowIsLayer(app->window)) draw_rect(&app->draw, window, COLOR_BG, 0);
Rect panels = {UI_GAP, UI_GAP, w - UI_GAP, h - UI_GAP};
layout_compute(&app->layout, app->layout.root, panels);
app->card_visible = false;
app->edit_drawn = false;
u64 t_panels = time_now_ns();
draw_panels(app, &input);
u64 t_overlay = time_now_ns();
draw_overlay(app, &input, window);
u64 t_end = time_now_ns();
if(app->profiler) {
u64 spans[PROFILE_UI_PHASES] = {t_keys_end - t_keys, t_overlay - t_panels, 0, t_end - t_overlay};
for(u32 phase = 0; phase < PROFILE_UI_PHASES; ++phase) {
f32 *us = &app->profiler->ui_phase_us[phase];
*us += ((f32)spans[phase] / 1000.0f - *us) * 0.05f;
}
}
app->pan_to_active = false;
if(app->profiler && app->profiler->sampling) app->ticking = true;
}
|