tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
Documentation
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
//! Draggable tab bar widget.

mod layout;
mod node;
mod reconcile;

pub use layout::measure_draggable_tab_bar;
pub use node::DraggableTabBarNode;
pub use reconcile::reconcile_draggable_tab_bar;

use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

use crate::callback::{Callback, KeyHandler};
use crate::core::element::{Element, ElementKind};
use crate::core::event::MouseEvent;
use crate::style::{BorderStyle, Color, FileIconPalette, Length, Padding, Span, Style, StyleSlot};
use crate::utils::file_icons::FileIconOverride;
use crate::utils::file_icons::file_icon;
use crate::widgets::file_tree::FileIconStyle;
use crate::widgets::{Spinner, TabsEvent, Text};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

/// Visual variant for [`DraggableTabBar`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum DraggableTabBarVariant {
    /// Classic segmented tabs with optional border.
    #[default]
    Bordered,
    /// One-line frame-like tabs with left accent markers.
    FrameLine,
}

/// Reorder behavior while dragging tabs.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum DragReorderMode {
    /// Emit reorder events as soon as drag crosses a tab boundary.
    #[default]
    Live,
    /// Emit a single reorder event when mouse is released.
    OnDrop,
}

/// Overflow behavior for a [`DraggableTabBar`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum DraggableTabBarOverflow {
    /// Keep natural tab widths and enable horizontal scrolling when tabs overflow.
    #[default]
    Scroll,
    /// Shrink tab labels down to `min_tab_width` cells before enabling scrolling.
    ShrinkThenScroll {
        /// Minimum total tab width in terminal cells.
        min_tab_width: u16,
    },
}

/// Behavior kind for a [`DraggableTab`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum DraggableTabKind {
    /// A regular selectable, draggable tab.
    #[default]
    Tab,
    /// A pinned action item inside the tab strip, such as a `+` new-tab button.
    Action,
}

/// Event emitted when an action tab is clicked.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DraggableTabActionEvent {
    /// Action tab index in the rendered tab list.
    pub index: usize,
}

/// Event emitted when a tab close button is clicked.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DraggableTabCloseEvent {
    /// Closed tab index.
    pub index: usize,
}

/// Event emitted when a tab is reordered.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DraggableTabReorderEvent {
    /// Source tab index in the current order.
    pub from: usize,
    /// Destination tab index in the current order.
    pub to: usize,
}

/// Event emitted when a tab is transferred to another connected bar.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct DraggableTabTransferEvent {
    /// Source bar identifier.
    pub from_bar: Arc<str>,
    /// Destination bar identifier.
    pub to_bar: Arc<str>,
    /// Source index in `from_bar` before transfer.
    pub from: usize,
    /// Destination index in `to_bar` after transfer.
    pub to: usize,
}

/// Which part of a tab was hit.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DraggableTabHitPart {
    /// Main tab body.
    Body,
    /// Close affordance (`x`) area.
    Close,
}

/// Hit-test result for a tab bar column.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DraggableTabHit {
    /// Tab index.
    pub index: usize,
    /// Hit part.
    pub part: DraggableTabHitPart,
}

/// Inline content rendered before the tab label.
#[derive(Clone, Debug)]
pub(crate) enum TabLeadingContent {
    Spinner(TabLeadingSpinner),
    Text(Text),
}

#[derive(Clone, Debug)]
pub(crate) struct TabLeadingSpinner {
    pub spinner: Spinner,
    pub auto_frame: bool,
}

impl TabLeadingContent {
    pub(crate) fn from_element(element: Element) -> Self {
        match element.kind {
            ElementKind::Spinner(spinner) => Self::Spinner(TabLeadingSpinner {
                auto_frame: spinner.frame.is_none(),
                spinner,
            }),
            ElementKind::Text(text) => Self::Text(text),
            _ => panic!("DraggableTab::leading only supports Spinner or Text elements"),
        }
    }

    pub(crate) fn spinner_mut(&mut self) -> Option<&mut TabLeadingSpinner> {
        match self {
            Self::Spinner(spinner) => Some(spinner),
            Self::Text(_) => None,
        }
    }

    pub(crate) fn spinner_frame(&self) -> Option<usize> {
        match self {
            Self::Spinner(spinner) => spinner.spinner.frame,
            Self::Text(_) => None,
        }
    }

    pub(crate) fn has_spinner(&self) -> bool {
        matches!(self, Self::Spinner(_))
    }

    pub(crate) fn to_span(&self) -> Span {
        match self {
            Self::Spinner(spinner) => {
                let frames = spinner.spinner.spinner_style.frames();
                let frame_str = frames[spinner.spinner.frame.unwrap_or(0) % frames.len()];
                let mut span = Span::new(frame_str);
                span.style = spinner.spinner.style;
                span
            }
            Self::Text(text) => {
                let mut span = Span::new(text.plain_content());
                let span_style = text
                    .spans
                    .first()
                    .map(|span| span.style)
                    .unwrap_or_default();
                span.style = text.style.patch(span_style);
                span
            }
        }
    }
}

/// A single draggable tab item.
#[derive(Clone, Debug)]
pub struct DraggableTab {
    pub(crate) label: Arc<str>,
    pub(crate) kind: DraggableTabKind,
    pub(crate) style: Style,
    pub(crate) hover_style: Style,
    pub(crate) active_style: Style,
    pub(crate) accent_style: Style,
    pub(crate) active_accent_style: Style,
    pub(crate) closeable: bool,
    pub(crate) icon: Option<Span>,
    pub(crate) leading: Option<TabLeadingContent>,
    pub(crate) path: Option<Arc<str>>,
    pub(crate) right_badge: Option<Span>,
}

impl DraggableTab {
    /// Create a tab with label.
    pub fn new(label: impl Into<Arc<str>>) -> Self {
        Self {
            label: label.into(),
            kind: DraggableTabKind::Tab,
            style: Style::default(),
            hover_style: Style::default(),
            active_style: Style::default(),
            accent_style: Style::default(),
            active_accent_style: Style::default(),
            closeable: false,
            icon: None,
            leading: None,
            path: None,
            right_badge: None,
        }
    }

    /// Create a pinned action tab, such as a `+` new-tab button.
    ///
    /// Action tabs emit [`DraggableTabBar::on_action`] instead of changing the
    /// active tab, and do not participate in drag reordering.
    pub fn action(label: impl Into<Arc<str>>) -> Self {
        Self::new(label).kind(DraggableTabKind::Action)
    }

    /// Set tab behavior kind.
    pub fn kind(mut self, kind: DraggableTabKind) -> Self {
        self.kind = kind;
        self
    }

    /// Set tab style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set this tab's hover style.
    ///
    /// This patches over [`DraggableTabBar::tab_hover_style`] for inactive tabs.
    /// Active tabs keep active styling and do not receive hover styling.
    pub fn hover_style(mut self, style: Style) -> Self {
        self.hover_style = style;
        self
    }

    /// Set this tab's active style.
    ///
    /// This patches over [`DraggableTabBar::active_style`] when this tab is selected.
    pub fn active_style(mut self, style: Style) -> Self {
        self.active_style = style;
        self
    }

    /// Set this tab's accent style for the `FrameLine` variant.
    ///
    /// This patches over [`DraggableTabBar::accent_style`] for this tab's accent marker.
    pub fn accent_style(mut self, style: Style) -> Self {
        self.accent_style = style;
        self
    }

    /// Set this tab's active accent style for the `FrameLine` variant.
    ///
    /// This patches over [`DraggableTabBar::active_accent_style`] when this tab is selected.
    pub fn active_accent_style(mut self, style: Style) -> Self {
        self.active_accent_style = style;
        self
    }

    /// Enable or disable close affordance for this tab.
    pub fn closeable(mut self, closeable: bool) -> Self {
        self.closeable = closeable;
        self
    }

    /// Set a custom icon rendered before the label.
    pub fn icon(mut self, icon: impl Into<Span>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set inline content rendered before the label (replaces icon when set).
    ///
    /// Supports [`Spinner`] and [`Text`] elements. Spinner label/layout
    /// properties and text layout properties are ignored because the tab owns
    /// its own label and sizing.
    pub fn leading(mut self, leading: Element) -> Self {
        self.leading = Some(TabLeadingContent::from_element(leading));
        self
    }

    /// Set file path used for automatic file-icon resolution.
    pub fn path(mut self, path: impl Into<Arc<str>>) -> Self {
        self.path = Some(path.into());
        self
    }

    /// Set a generic right-side badge rendered after the label.
    pub fn right_badge(mut self, badge: impl Into<Span>) -> Self {
        self.right_badge = Some(badge.into());
        self
    }
}

impl From<&'static str> for DraggableTab {
    fn from(value: &'static str) -> Self {
        Self::new(value)
    }
}

impl From<String> for DraggableTab {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<Arc<str>> for DraggableTab {
    fn from(value: Arc<str>) -> Self {
        Self::new(value)
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct TabMetrics {
    pub width: usize,
    pub close_start: Option<usize>,
    pub close_end: Option<usize>,
    pub label_width: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum OverflowControlSide {
    Left,
    Right,
}

#[derive(Clone, Debug)]
pub(crate) struct OverflowControl {
    pub start: usize,
    pub end: usize,
    pub label: Arc<str>,
}

#[derive(Clone, Debug)]
pub(crate) struct VisibleTab {
    pub index: usize,
    pub start: usize,
    pub end: usize,
    pub metrics: TabMetrics,
    pub clip_left: usize,
}

#[derive(Clone, Debug)]
pub(crate) struct TabViewportLayout {
    pub offset: usize,
    pub visible_tabs: Vec<VisibleTab>,
    pub hidden_left: usize,
    pub hidden_right: usize,
    pub content_start: usize,
    pub content_width: usize,
    pub left_control: Option<OverflowControl>,
    pub right_control: Option<OverflowControl>,
}

pub(crate) const TAB_SCROLL_STEP_CHARS: usize = 12;
pub(crate) const TAB_SCROLL_BUTTON_STEP_CHARS: usize = TAB_SCROLL_STEP_CHARS * 2;

pub(crate) struct TabDisplayOptions<'a> {
    pub variant: DraggableTabBarVariant,
    pub divider: char,
    pub accent_symbol: char,
    pub close_symbol: &'a str,
    pub show_close_buttons: bool,
    pub tab_max_width: Option<u16>,
    pub overflow: DraggableTabBarOverflow,
    pub show_file_icons: bool,
    pub file_icon_style: FileIconStyle,
    pub file_icon_palette: &'a FileIconPalette,
    pub file_icon_overrides: &'a HashMap<Arc<str>, FileIconOverride>,
}

pub(crate) struct TabViewportOptions {
    pub scroll_offset: usize,
    pub viewport_width: usize,
    pub show_overflow_controls: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum DraggableTabHitTarget {
    Tab(DraggableTabHit),
    Overflow(OverflowControlSide),
}

/// A draggable tab bar suitable for editor-like UIs.
#[derive(Clone)]
pub struct DraggableTabBar {
    pub(crate) tabs: Arc<[DraggableTab]>,
    pub(crate) active: usize,
    pub(crate) style: Style,
    pub(crate) focus_style: StyleSlot,
    pub(crate) hover_style: StyleSlot,
    pub(crate) tab_hover_style: StyleSlot,
    pub(crate) active_style: StyleSlot,
    pub(crate) close_style: Style,
    pub(crate) close_hover_style: Style,
    pub(crate) divider: char,
    pub(crate) border: bool,
    pub(crate) border_style: BorderStyle,
    pub(crate) padding: Padding,
    pub(crate) width: Length,
    pub(crate) height: Length,
    pub(crate) variant: DraggableTabBarVariant,
    pub(crate) accent_symbol: char,
    pub(crate) active_accent_symbol: char,
    pub(crate) accent_style: Style,
    pub(crate) active_accent_style: Style,
    pub(crate) close_symbol: Arc<str>,
    pub(crate) show_close_buttons: bool,
    pub(crate) close_on_hover_only: bool,
    pub(crate) tab_max_width: Option<u16>,
    pub(crate) overflow: DraggableTabBarOverflow,
    pub(crate) scroll_wheel: bool,
    pub(crate) show_overflow_controls: bool,
    pub(crate) overflow_style: Style,
    pub(crate) overflow_hover_style: Style,
    pub(crate) scroll_offset: usize,
    pub(crate) show_file_icons: bool,
    pub(crate) file_icon_style: FileIconStyle,
    pub(crate) file_icon_palette: FileIconPalette,
    pub(crate) file_icon_overrides: HashMap<Arc<str>, FileIconOverride>,
    pub(crate) bar_id: Option<Arc<str>>,
    pub(crate) drag_group: Option<Arc<str>>,
    pub(crate) draggable: bool,
    pub(crate) drag_preview: bool,
    pub(crate) reorder_mode: DragReorderMode,
    pub(crate) drag_threshold: u16,
    pub(crate) on_change: Option<Callback<TabsEvent>>,
    pub(crate) on_action: Option<Callback<DraggableTabActionEvent>>,
    pub(crate) on_close: Option<Callback<DraggableTabCloseEvent>>,
    pub(crate) on_reorder: Option<Callback<DraggableTabReorderEvent>>,
    pub(crate) on_transfer: Option<Callback<DraggableTabTransferEvent>>,
    pub(crate) on_click: Option<Callback<MouseEvent>>,
    pub(crate) on_key: Option<KeyHandler>,
    pub(crate) disabled: bool,
    pub(crate) disabled_style: Style,
    pub(crate) focusable: bool,
}

impl Default for DraggableTabBar {
    fn default() -> Self {
        Self {
            tabs: Arc::new([]),
            active: 0,
            style: Style::default(),
            focus_style: StyleSlot::Inherit,
            hover_style: StyleSlot::Inherit,
            tab_hover_style: StyleSlot::Inherit,
            active_style: StyleSlot::Inherit,
            close_style: Style::default(),
            close_hover_style: Style::default(),
            divider: '',
            border: false,
            border_style: BorderStyle::Plain,
            padding: Padding::default(),
            width: Length::Flex(1),
            height: Length::Auto,
            variant: DraggableTabBarVariant::Bordered,
            accent_symbol: '',
            active_accent_symbol: '',
            accent_style: Style::default(),
            active_accent_style: Style::default(),
            close_symbol: Arc::from(""),
            show_close_buttons: true,
            close_on_hover_only: false,
            tab_max_width: None,
            overflow: DraggableTabBarOverflow::Scroll,
            scroll_wheel: true,
            show_overflow_controls: true,
            overflow_style: Style::default(),
            overflow_hover_style: Style::default(),
            scroll_offset: 0,
            show_file_icons: false,
            file_icon_style: FileIconStyle::NerdFont,
            file_icon_palette: FileIconPalette::default(),
            file_icon_overrides: HashMap::new(),
            bar_id: None,
            drag_group: None,
            draggable: true,
            drag_preview: true,
            reorder_mode: DragReorderMode::Live,
            drag_threshold: 1,
            on_change: None,
            on_action: None,
            on_close: None,
            on_reorder: None,
            on_transfer: None,
            on_click: None,
            on_key: None,
            disabled: false,
            disabled_style: Style::default(),
            focusable: true,
        }
    }
}

impl DraggableTabBar {
    /// Create an empty draggable tab bar.
    pub fn new() -> Self {
        Self::default()
    }

    pub(crate) fn display_options(&self) -> TabDisplayOptions<'_> {
        TabDisplayOptions {
            variant: self.variant,
            divider: self.divider,
            accent_symbol: self.accent_symbol,
            close_symbol: &self.close_symbol,
            show_close_buttons: self.show_close_buttons,
            tab_max_width: self.tab_max_width,
            overflow: self.overflow,
            show_file_icons: self.show_file_icons,
            file_icon_style: self.file_icon_style,
            file_icon_palette: &self.file_icon_palette,
            file_icon_overrides: &self.file_icon_overrides,
        }
    }

    /// Replace tabs.
    pub fn tabs<I>(mut self, tabs: I) -> Self
    where
        I: IntoIterator<Item = DraggableTab>,
    {
        self.tabs = tabs.into_iter().collect::<Vec<_>>().into();
        self
    }

    /// Add one tab.
    pub fn tab(mut self, tab: impl Into<DraggableTab>) -> Self {
        let mut tabs = self.tabs.to_vec();
        tabs.push(tab.into());
        self.tabs = tabs.into();
        self
    }

    /// Set active tab index.
    pub fn active(mut self, active: usize) -> Self {
        self.active = active;
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set focus style for the whole widget.
    pub fn focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's focus style with additional fields.
    pub fn extend_focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit focus style from the active theme.
    pub fn inherit_focus_style(mut self) -> Self {
        self.focus_style = StyleSlot::Inherit;
        self
    }

    /// Set hover style for the whole widget.
    pub fn hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's hover style with additional fields.
    pub fn extend_hover_style(mut self, style: Style) -> Self {
        self.hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit hover style from the active theme.
    pub fn inherit_hover_style(mut self) -> Self {
        self.hover_style = StyleSlot::Inherit;
        self
    }

    /// Set style for hovered tab.
    pub fn tab_hover_style(mut self, style: Style) -> Self {
        self.tab_hover_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's tab hover style with additional fields.
    pub fn extend_tab_hover_style(mut self, style: Style) -> Self {
        self.tab_hover_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit tab hover style from the active theme.
    pub fn inherit_tab_hover_style(mut self) -> Self {
        self.tab_hover_style = StyleSlot::Inherit;
        self
    }

    /// Set active tab style.
    pub fn active_style(mut self, style: Style) -> Self {
        self.active_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the active theme's active-tab style with additional fields.
    pub fn extend_active_style(mut self, style: Style) -> Self {
        self.active_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit active-tab style from the active theme.
    pub fn inherit_active_style(mut self) -> Self {
        self.active_style = StyleSlot::Inherit;
        self
    }

    /// Set close symbol style.
    pub fn close_style(mut self, style: Style) -> Self {
        self.close_style = style;
        self
    }

    /// Set close symbol hover style.
    pub fn close_hover_style(mut self, style: Style) -> Self {
        self.close_hover_style = style;
        self
    }

    /// Set divider character for bordered variant.
    pub fn divider(mut self, ch: char) -> Self {
        self.divider = ch;
        self
    }

    /// Draw outer border.
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, border_style: BorderStyle) -> Self {
        self.border_style = border_style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Set requested width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Set requested height.
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Set visual variant.
    pub fn variant(mut self, variant: DraggableTabBarVariant) -> Self {
        self.variant = variant;
        self
    }

    /// Set left accent symbol for frame-line variant.
    pub fn accent_symbol(mut self, symbol: char) -> Self {
        self.accent_symbol = symbol;
        self
    }

    /// Set active tab accent symbol for frame-line variant.
    pub fn active_accent_symbol(mut self, symbol: char) -> Self {
        self.active_accent_symbol = symbol;
        self
    }

    /// Set inactive accent style for frame-line variant.
    pub fn accent_style(mut self, style: Style) -> Self {
        self.accent_style = style;
        self
    }

    /// Set active accent style for frame-line variant.
    pub fn active_accent_style(mut self, style: Style) -> Self {
        self.active_accent_style = style;
        self
    }

    /// Set close symbol.
    pub fn close_symbol(mut self, symbol: impl Into<Arc<str>>) -> Self {
        self.close_symbol = symbol.into();
        self
    }

    /// Toggle rendering of close buttons for closeable tabs.
    pub fn show_close_buttons(mut self, show: bool) -> Self {
        self.show_close_buttons = show;
        self
    }

    /// Show close symbols only while the tab is hovered.
    ///
    /// Layout width remains stable (close slot is reserved) to avoid jitter.
    pub fn close_on_hover_only(mut self, only_on_hover: bool) -> Self {
        self.close_on_hover_only = only_on_hover;
        self
    }

    /// Clamp per-tab label width, truncating with right-side ellipsis.
    pub fn tab_max_width(mut self, width: Option<u16>) -> Self {
        self.tab_max_width = width;
        self
    }

    /// Set tab overflow behavior.
    pub fn overflow(mut self, overflow: DraggableTabBarOverflow) -> Self {
        self.overflow = overflow;
        self
    }

    /// Enable mouse wheel horizontal scrolling.
    pub fn scroll_wheel(mut self, enabled: bool) -> Self {
        self.scroll_wheel = enabled;
        self
    }

    /// Show overflow controls when tabs are clipped horizontally.
    pub fn show_overflow_controls(mut self, show: bool) -> Self {
        self.show_overflow_controls = show;
        self
    }

    /// Set style for overflow controls.
    pub fn overflow_style(mut self, style: Style) -> Self {
        self.overflow_style = style;
        self
    }

    /// Set hover style for overflow controls.
    pub fn overflow_hover_style(mut self, style: Style) -> Self {
        self.overflow_hover_style = style;
        self
    }

    /// Set initial horizontal tab scroll offset (tab index).
    pub fn scroll_offset(mut self, offset: usize) -> Self {
        self.scroll_offset = offset;
        self
    }

    /// Enable automatic file icons before tab titles.
    pub fn show_file_icons(mut self, show: bool) -> Self {
        self.show_file_icons = show;
        self
    }

    /// Set file icon style used by automatic tab icons.
    pub fn file_icon_style(mut self, style: FileIconStyle) -> Self {
        self.file_icon_style = style;
        self
    }

    /// Set file icon palette used by automatic tab icons.
    pub fn file_icon_palette(mut self, palette: FileIconPalette) -> Self {
        self.file_icon_palette = palette;
        self
    }

    /// Add file icon override by filename or extension.
    pub fn file_icon_override(
        mut self,
        pattern: impl Into<Arc<str>>,
        icon: impl Into<Arc<str>>,
        color: Option<Color>,
    ) -> Self {
        self.file_icon_overrides.insert(
            pattern.into(),
            FileIconOverride {
                icon: icon.into(),
                color,
            },
        );
        self
    }

    /// Set a stable identifier for this tab bar.
    ///
    /// Required for cross-bar tab transfers.
    pub fn bar_id(mut self, id: impl Into<Arc<str>>) -> Self {
        self.bar_id = Some(id.into());
        self
    }

    /// Set drag group for cross-bar transfers.
    ///
    /// Tabs can transfer only between bars with the same group.
    pub fn drag_group(mut self, group: impl Into<Arc<str>>) -> Self {
        self.drag_group = Some(group.into());
        self
    }

    /// Toggle drag reordering.
    pub fn draggable(mut self, draggable: bool) -> Self {
        self.draggable = draggable;
        self
    }

    /// Show a floating label near the pointer while dragging a tab (default: `true`).
    pub fn drag_preview(mut self, enabled: bool) -> Self {
        self.drag_preview = enabled;
        self
    }

    /// Set drag reorder mode.
    pub fn reorder_mode(mut self, mode: DragReorderMode) -> Self {
        self.reorder_mode = mode;
        self
    }

    /// Set drag start threshold in columns.
    pub fn drag_threshold(mut self, threshold: u16) -> Self {
        self.drag_threshold = threshold;
        self
    }

    /// Callback fired when active tab changes.
    pub fn on_change(mut self, cb: Callback<TabsEvent>) -> Self {
        self.on_change = Some(cb);
        self
    }

    /// Callback fired when an action tab is clicked.
    pub fn on_action(mut self, cb: Callback<DraggableTabActionEvent>) -> Self {
        self.on_action = Some(cb);
        self
    }

    /// Callback fired when a close button is clicked.
    pub fn on_close(mut self, cb: Callback<DraggableTabCloseEvent>) -> Self {
        self.on_close = Some(cb);
        self
    }

    /// Callback fired when tab order changes.
    pub fn on_reorder(mut self, cb: Callback<DraggableTabReorderEvent>) -> Self {
        self.on_reorder = Some(cb);
        self
    }

    /// Callback fired when a tab is moved between connected bars.
    pub fn on_transfer(mut self, cb: Callback<DraggableTabTransferEvent>) -> Self {
        self.on_transfer = Some(cb);
        self
    }

    /// Set on-click handler.
    pub fn on_click(mut self, cb: Callback<MouseEvent>) -> Self {
        self.on_click = Some(cb);
        self
    }

    /// Set on-key handler.
    pub fn on_key(mut self, handler: KeyHandler) -> Self {
        self.on_key = Some(handler);
        self
    }

    /// Set disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set disabled style.
    pub fn disabled_style(mut self, style: Style) -> Self {
        self.disabled_style = style;
        self
    }

    /// Control whether node is focusable.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    #[cfg(test)]
    pub(crate) fn content_width(
        tabs: &[DraggableTab],
        variant: DraggableTabBarVariant,
        divider: char,
        accent_symbol: char,
        close_symbol: &str,
        show_close_buttons: bool,
    ) -> usize {
        Self::content_width_with_options(
            tabs,
            &TabDisplayOptions {
                variant,
                divider,
                accent_symbol,
                close_symbol,
                show_close_buttons,
                tab_max_width: None,
                overflow: DraggableTabBarOverflow::Scroll,
                show_file_icons: false,
                file_icon_style: FileIconStyle::NerdFont,
                file_icon_palette: &FileIconPalette::default(),
                file_icon_overrides: &HashMap::new(),
            },
        )
    }

    pub(crate) fn content_width_with_options(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
    ) -> usize {
        let mut width = 0usize;
        for (i, tab) in tabs.iter().enumerate() {
            width = width.saturating_add(tab_metrics_with_options(tab, opts).width);
            if i + 1 < tabs.len() {
                width = width.saturating_add(separator_width(opts.variant, opts.divider));
            }
        }
        width
    }

    pub(crate) fn content_width_for_viewport(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        viewport_width: usize,
    ) -> usize {
        let metrics = tab_metrics_for_viewport(tabs, opts, Some(viewport_width));
        total_width_for_metrics(&metrics, opts)
    }

    #[cfg(test)]
    pub(crate) fn hit_at_col(
        tabs: &[DraggableTab],
        variant: DraggableTabBarVariant,
        divider: char,
        accent_symbol: char,
        close_symbol: &str,
        show_close_buttons: bool,
        col: usize,
    ) -> Option<DraggableTabHit> {
        Self::hit_at_col_with_options(
            tabs,
            &TabDisplayOptions {
                variant,
                divider,
                accent_symbol,
                close_symbol,
                show_close_buttons,
                tab_max_width: None,
                overflow: DraggableTabBarOverflow::Scroll,
                show_file_icons: false,
                file_icon_style: FileIconStyle::NerdFont,
                file_icon_palette: &FileIconPalette::default(),
                file_icon_overrides: &HashMap::new(),
            },
            col,
        )
    }

    #[cfg(test)]
    pub(crate) fn hit_at_col_with_options(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        col: usize,
    ) -> Option<DraggableTabHit> {
        let mut x = 0usize;
        for (i, tab) in tabs.iter().enumerate() {
            let metrics = tab_metrics_with_options(tab, opts);
            let end = x.saturating_add(metrics.width);
            if col < end {
                let part = if let (Some(close_start), Some(close_end)) =
                    (metrics.close_start, metrics.close_end)
                {
                    let c_start = x.saturating_add(close_start);
                    let c_end = x.saturating_add(close_end);
                    if col >= c_start && col < c_end {
                        DraggableTabHitPart::Close
                    } else {
                        DraggableTabHitPart::Body
                    }
                } else {
                    DraggableTabHitPart::Body
                };
                return Some(DraggableTabHit { index: i, part });
            }
            x = end;

            if i + 1 < tabs.len() {
                let sep_w = separator_width(opts.variant, opts.divider);
                if col < x.saturating_add(sep_w) {
                    return match opts.variant {
                        DraggableTabBarVariant::Bordered => None,
                        DraggableTabBarVariant::FrameLine => Some(DraggableTabHit {
                            index: i,
                            part: DraggableTabHitPart::Body,
                        }),
                    };
                }
                x = x.saturating_add(sep_w);
            }
        }

        None
    }

    #[cfg(test)]
    pub(crate) fn reorder_index_at_col(
        tabs: &[DraggableTab],
        variant: DraggableTabBarVariant,
        divider: char,
        accent_symbol: char,
        close_symbol: &str,
        show_close_buttons: bool,
        col: usize,
    ) -> Option<usize> {
        Self::reorder_index_at_col_with_options(
            tabs,
            &TabDisplayOptions {
                variant,
                divider,
                accent_symbol,
                close_symbol,
                show_close_buttons,
                tab_max_width: None,
                overflow: DraggableTabBarOverflow::Scroll,
                show_file_icons: false,
                file_icon_style: FileIconStyle::NerdFont,
                file_icon_palette: &FileIconPalette::default(),
                file_icon_overrides: &HashMap::new(),
            },
            col,
        )
    }

    #[cfg(test)]
    pub(crate) fn reorder_index_at_col_with_options(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        col: usize,
    ) -> Option<usize> {
        let metrics = tab_metrics_for_viewport(tabs, opts, None);
        Self::reorder_index_at_col_with_metrics(tabs, opts, &metrics, col)
    }

    fn reorder_index_at_col_with_metrics(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        metrics: &[TabMetrics],
        col: usize,
    ) -> Option<usize> {
        let mut x = 0usize;
        let len = tabs.len();
        for (i, metrics) in metrics.iter().enumerate() {
            let end = x.saturating_add(metrics.width);
            if col < end {
                return Some(i);
            }
            x = end;

            if i + 1 < len {
                let sep_w = separator_width(opts.variant, opts.divider);
                if col < x.saturating_add(sep_w) {
                    return Some((i + 1).min(len.saturating_sub(1)));
                }
                x = x.saturating_add(sep_w);
            }
        }
        None
    }

    #[cfg(test)]
    pub(crate) fn adjacent_reorder_target(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        current_index: usize,
        col: usize,
    ) -> Option<usize> {
        Self::adjacent_reorder_target_with_options(tabs, opts, current_index, col)
    }

    #[cfg(test)]
    pub(crate) fn adjacent_reorder_target_with_options(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        current_index: usize,
        col: usize,
    ) -> Option<usize> {
        let metrics = tab_metrics_for_viewport(tabs, opts, None);
        Self::adjacent_reorder_target_with_metrics(tabs, opts, &metrics, current_index, col)
    }

    fn adjacent_reorder_target_with_metrics(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        metrics: &[TabMetrics],
        current_index: usize,
        col: usize,
    ) -> Option<usize> {
        if tabs.is_empty() || current_index >= tabs.len() {
            return None;
        }

        if !tabs.get(current_index).is_some_and(is_reorderable_tab) {
            return None;
        }

        let mut starts = Vec::with_capacity(tabs.len());
        let mut widths = Vec::with_capacity(tabs.len());
        let mut reorder_indices = Vec::new();
        let mut x = 0usize;
        for (i, (tab, metrics)) in tabs.iter().zip(metrics).enumerate() {
            starts.push(x);
            widths.push(metrics.width);
            if is_reorderable_tab(tab) {
                reorder_indices.push(i);
            }
            x = x.saturating_add(metrics.width);
            if i + 1 < tabs.len() {
                x = x.saturating_add(separator_width(opts.variant, opts.divider));
            }
        }

        let mut position = reorder_indices
            .iter()
            .position(|&index| index == current_index)?;
        let mut run_start = position;
        while run_start > 0 && reorder_indices[run_start - 1] + 1 == reorder_indices[run_start] {
            run_start -= 1;
        }
        let mut run_end = position;
        while run_end + 1 < reorder_indices.len()
            && reorder_indices[run_end] + 1 == reorder_indices[run_end + 1]
        {
            run_end += 1;
        }

        let midpoint = |idx: usize| -> usize { starts[idx].saturating_add(widths[idx] / 2) };

        while position < run_end && col >= midpoint(reorder_indices[position + 1]) {
            position += 1;
        }

        while position > run_start && col < midpoint(reorder_indices[position - 1]) {
            position -= 1;
        }

        let target = reorder_indices[position];
        (target != current_index).then_some(target)
    }

    pub(crate) fn viewport_layout(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
    ) -> TabViewportLayout {
        let len = tabs.len();
        if len == 0 || vp.viewport_width == 0 {
            return TabViewportLayout {
                offset: 0,
                visible_tabs: Vec::new(),
                hidden_left: 0,
                hidden_right: 0,
                content_start: 0,
                content_width: 0,
                left_control: None,
                right_control: None,
            };
        }

        let (runs, total_width) = tab_runs_for_viewport(tabs, opts, Some(vp.viewport_width));

        let requested_offset = vp.scroll_offset;
        let mut offset = vp.scroll_offset;
        let mut align_right = false;
        let mut left_width = 0usize;
        let mut right_width = 0usize;
        let mut visible_tabs = Vec::new();
        let mut hidden_left = 0usize;
        let mut hidden_right = 0usize;

        for _ in 0..8 {
            visible_tabs.clear();
            let mut available = vp.viewport_width.saturating_sub(left_width + right_width);
            if available == 0 {
                if right_width > 0 {
                    right_width = 0;
                    available = vp.viewport_width.saturating_sub(left_width);
                } else if left_width > 0 {
                    left_width = 0;
                    available = vp.viewport_width;
                }
            }

            let max_scroll = total_width.saturating_sub(available);
            if align_right || requested_offset >= max_scroll {
                align_right = true;
                offset = max_scroll;
            } else {
                offset = requested_offset.min(max_scroll);
            }
            let view_start = offset;
            let view_end = view_start.saturating_add(available);

            hidden_left = 0;
            hidden_right = 0;
            for (idx, (start, end, metrics)) in runs.iter().enumerate() {
                if *end <= view_start {
                    hidden_left = idx.saturating_add(1);
                    continue;
                }
                if *start >= view_end {
                    hidden_right = len.saturating_sub(idx);
                    break;
                }

                let visible_start = (*start).max(view_start);
                let visible_end = (*end).min(view_end);
                if visible_start >= visible_end {
                    continue;
                }

                if visible_start > *start {
                    hidden_left = idx.saturating_add(1);
                }

                visible_tabs.push(VisibleTab {
                    index: idx,
                    start: visible_start.saturating_sub(view_start),
                    end: visible_end.saturating_sub(view_start),
                    metrics: *metrics,
                    clip_left: visible_start.saturating_sub(*start),
                });

                if visible_end < *end {
                    hidden_right = len.saturating_sub(idx);
                    break;
                }
            }

            if visible_tabs.is_empty() && offset > 0 {
                offset = offset.saturating_sub(1);
                continue;
            }

            if hidden_right == 0 {
                hidden_right = if let Some(last) = visible_tabs.last() {
                    len.saturating_sub(last.index + 1)
                } else {
                    len.saturating_sub(offset)
                };
            }

            let next_left = if vp.show_overflow_controls && hidden_left > 0 {
                overflow_control_width(OverflowControlSide::Left, hidden_left)
            } else {
                0
            };
            let next_right = if vp.show_overflow_controls && hidden_right > 0 {
                overflow_control_width(OverflowControlSide::Right, hidden_right)
            } else {
                0
            };

            if next_left == left_width && next_right == right_width {
                break;
            }

            left_width = next_left;
            right_width = next_right;
        }

        let content_start = left_width.min(vp.viewport_width);
        let content_width = vp.viewport_width.saturating_sub(left_width + right_width);

        let mut shifted_tabs = Vec::with_capacity(visible_tabs.len());
        for tab in visible_tabs {
            shifted_tabs.push(VisibleTab {
                start: tab.start.saturating_add(content_start),
                end: tab.end.saturating_add(content_start),
                ..tab
            });
        }

        let left_control = if left_width > 0 {
            Some(OverflowControl {
                start: 0,
                end: left_width,
                label: overflow_control_label(OverflowControlSide::Left, hidden_left),
            })
        } else {
            None
        };

        let right_control = if right_width > 0 {
            let start = vp.viewport_width.saturating_sub(right_width);
            Some(OverflowControl {
                start,
                end: vp.viewport_width,
                label: overflow_control_label(OverflowControlSide::Right, hidden_right),
            })
        } else {
            None
        };

        TabViewportLayout {
            offset,
            visible_tabs: shifted_tabs,
            hidden_left,
            hidden_right,
            content_start,
            content_width,
            left_control,
            right_control,
        }
    }

    pub(crate) fn hit_target_at_view_col(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        col: usize,
    ) -> Option<DraggableTabHitTarget> {
        let layout = Self::viewport_layout(tabs, opts, vp);

        if let Some(left) = &layout.left_control
            && col >= left.start
            && col < left.end
        {
            return Some(DraggableTabHitTarget::Overflow(OverflowControlSide::Left));
        }
        if let Some(right) = &layout.right_control
            && col >= right.start
            && col < right.end
        {
            return Some(DraggableTabHitTarget::Overflow(OverflowControlSide::Right));
        }

        for tab in &layout.visible_tabs {
            if col < tab.start || col >= tab.end {
                continue;
            }
            let local = col.saturating_sub(tab.start).saturating_add(tab.clip_left);
            let part = if let (Some(close_start), Some(close_end)) =
                (tab.metrics.close_start, tab.metrics.close_end)
            {
                if local >= close_start && local < close_end {
                    DraggableTabHitPart::Close
                } else {
                    DraggableTabHitPart::Body
                }
            } else {
                DraggableTabHitPart::Body
            };
            return Some(DraggableTabHitTarget::Tab(DraggableTabHit {
                index: tab.index,
                part,
            }));
        }

        None
    }

    pub(crate) fn global_col_from_view_col(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        col: usize,
    ) -> Option<usize> {
        let layout = Self::viewport_layout(tabs, opts, vp);

        if col < layout.content_start
            || col >= layout.content_start.saturating_add(layout.content_width)
        {
            return None;
        }

        let view_col = col.saturating_sub(layout.content_start);
        Some(layout.offset.saturating_add(view_col))
    }

    pub(crate) fn reorder_index_at_view_col(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        col: usize,
    ) -> Option<usize> {
        let global_col = Self::global_col_from_view_col(tabs, opts, vp, col)?;
        let metrics = tab_metrics_for_viewport(tabs, opts, Some(vp.viewport_width));
        Self::reorder_index_at_col_with_metrics(tabs, opts, &metrics, global_col)
    }

    pub(crate) fn adjacent_reorder_target_at_view_col(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        current_index: usize,
        col: usize,
    ) -> Option<usize> {
        let global_col = Self::global_col_from_view_col(tabs, opts, vp, col)?;
        let metrics = tab_metrics_for_viewport(tabs, opts, Some(vp.viewport_width));
        Self::adjacent_reorder_target_with_metrics(tabs, opts, &metrics, current_index, global_col)
    }

    pub(crate) fn scroll_offset_for_step(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        step_right: bool,
        step_chars: usize,
    ) -> usize {
        if tabs.is_empty() || vp.viewport_width == 0 {
            return 0;
        }

        let layout = Self::viewport_layout(tabs, opts, vp);
        let current = layout.offset;

        if (step_right && layout.hidden_right == 0) || (!step_right && layout.hidden_left == 0) {
            return current;
        }

        let step = step_chars.max(1);
        let requested = if step_right {
            current.saturating_add(step)
        } else {
            current.saturating_sub(step)
        };

        let canonical = Self::viewport_layout(
            tabs,
            opts,
            &TabViewportOptions {
                scroll_offset: requested,
                viewport_width: vp.viewport_width,
                show_overflow_controls: vp.show_overflow_controls,
            },
        )
        .offset;

        if canonical != current {
            return canonical;
        }

        if step_right {
            let total_width = Self::content_width_for_viewport(tabs, opts, vp.viewport_width);
            return Self::viewport_layout(
                tabs,
                opts,
                &TabViewportOptions {
                    scroll_offset: total_width.saturating_sub(1),
                    viewport_width: vp.viewport_width,
                    show_overflow_controls: vp.show_overflow_controls,
                },
            )
            .offset;
        }

        0
    }

    pub(crate) fn scroll_offset_to_reveal_tab(
        tabs: &[DraggableTab],
        opts: &TabDisplayOptions<'_>,
        vp: &TabViewportOptions,
        tab_index: usize,
    ) -> usize {
        if tabs.is_empty() || vp.viewport_width == 0 {
            return 0;
        }
        let target = tab_index.min(tabs.len().saturating_sub(1));
        let metrics = tab_metrics_for_viewport(tabs, opts, Some(vp.viewport_width));
        let target_start = tabs_prefix_width_from_metrics(&metrics, opts, target);
        let target_width = metrics[target].width;
        let target_end = target_start.saturating_add(target_width);

        let mut offset = vp.scroll_offset;
        for _ in 0..8 {
            let layout = Self::viewport_layout(
                tabs,
                opts,
                &TabViewportOptions {
                    scroll_offset: offset,
                    viewport_width: vp.viewport_width,
                    show_overflow_controls: vp.show_overflow_controls,
                },
            );
            let view_start = layout.offset;
            let view_end = view_start.saturating_add(layout.content_width);

            let next = if target_width > layout.content_width || target_start < view_start {
                target_start
            } else if target_end > view_end {
                target_end.saturating_sub(layout.content_width)
            } else {
                return view_start;
            };

            if next == offset {
                return layout.offset;
            }
            offset = next;
        }

        offset
    }
}

pub(crate) fn is_reorderable_tab(tab: &DraggableTab) -> bool {
    tab.kind == DraggableTabKind::Tab
}

#[cfg(test)]
pub(crate) fn reorder_target_at_col_with_options(
    tabs: &[DraggableTab],
    opts: &TabDisplayOptions<'_>,
    col: usize,
) -> Option<usize> {
    let candidate = DraggableTabBar::reorder_index_at_col_with_options(tabs, opts, col)?;
    if tabs.get(candidate).is_some_and(is_reorderable_tab) {
        return Some(candidate);
    }

    (0..candidate)
        .rev()
        .find(|&index| tabs.get(index).is_some_and(is_reorderable_tab))
        .or_else(|| {
            ((candidate + 1)..tabs.len())
                .find(|&index| tabs.get(index).is_some_and(is_reorderable_tab))
        })
}

pub(crate) fn reorder_target_at_view_col_with_options(
    tabs: &[DraggableTab],
    opts: &TabDisplayOptions<'_>,
    vp: &TabViewportOptions,
    col: usize,
) -> Option<usize> {
    let candidate = DraggableTabBar::reorder_index_at_view_col(tabs, opts, vp, col)?;
    if tabs.get(candidate).is_some_and(is_reorderable_tab) {
        return Some(candidate);
    }

    (0..candidate)
        .rev()
        .find(|&index| tabs.get(index).is_some_and(is_reorderable_tab))
        .or_else(|| {
            ((candidate + 1)..tabs.len())
                .find(|&index| tabs.get(index).is_some_and(is_reorderable_tab))
        })
}

fn separator_width(variant: DraggableTabBarVariant, divider: char) -> usize {
    match variant {
        DraggableTabBarVariant::Bordered => UnicodeWidthChar::width(divider).unwrap_or(1),
        DraggableTabBarVariant::FrameLine => 0,
    }
}

#[cfg(test)]
pub(crate) fn tab_metrics(
    tab: &DraggableTab,
    variant: DraggableTabBarVariant,
    accent_symbol: char,
    close_symbol: &str,
    show_close_buttons: bool,
) -> TabMetrics {
    tab_metrics_with_options(
        tab,
        &TabDisplayOptions {
            variant,
            divider: '|',
            accent_symbol,
            close_symbol,
            show_close_buttons,
            tab_max_width: None,
            overflow: DraggableTabBarOverflow::Scroll,
            show_file_icons: false,
            file_icon_style: FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &HashMap::new(),
        },
    )
}

pub(crate) fn tab_metrics_with_options(
    tab: &DraggableTab,
    opts: &TabDisplayOptions<'_>,
) -> TabMetrics {
    let label_w = tab_label_width(tab, opts);
    tab_metrics_with_label_width(tab, opts, label_w)
}

fn tab_label_width(tab: &DraggableTab, opts: &TabDisplayOptions<'_>) -> usize {
    let mut label_w = UnicodeWidthStr::width(tab.label.as_ref());
    if let Some(max) = opts.tab_max_width {
        let max = (max as usize).max(1);
        label_w = label_w.min(max);
    }
    label_w
}

fn tab_metrics_with_label_width(
    tab: &DraggableTab,
    opts: &TabDisplayOptions<'_>,
    label_w: usize,
) -> TabMetrics {
    let icon_w = resolve_tab_icon(
        tab,
        opts.show_file_icons,
        opts.file_icon_style,
        opts.file_icon_palette,
        opts.file_icon_overrides,
    )
    .map(|icon| UnicodeWidthStr::width(icon.content.as_ref()).saturating_add(1))
    .unwrap_or(0);

    let badge_w = tab
        .right_badge
        .as_ref()
        .map(|badge| UnicodeWidthStr::width(badge.content.as_ref()))
        .unwrap_or(0);
    let badge_gap_w = if badge_w > 0 { 1 } else { 0 };

    let close_symbol_w = UnicodeWidthStr::width(opts.close_symbol).max(1);
    let has_close = opts.show_close_buttons && tab.closeable && is_reorderable_tab(tab);
    let close_gap_w = if has_close { 1 } else { 0 };
    let close_zone_w = if has_close { close_symbol_w } else { 0 };

    match opts.variant {
        DraggableTabBarVariant::Bordered => {
            let close_start =
                has_close.then_some(1 + icon_w + label_w + badge_gap_w + badge_w + close_gap_w);
            let close_end = has_close.then_some(
                1 + icon_w + label_w + badge_gap_w + badge_w + close_gap_w + close_zone_w,
            );
            TabMetrics {
                width: 1
                    + icon_w
                    + label_w
                    + badge_gap_w
                    + badge_w
                    + close_gap_w
                    + close_zone_w
                    + 1,
                close_start,
                close_end,
                label_width: label_w,
            }
        }
        DraggableTabBarVariant::FrameLine => {
            let accent_w = UnicodeWidthChar::width(opts.accent_symbol).unwrap_or(1);
            let close_start = has_close
                .then_some(accent_w + 1 + icon_w + label_w + badge_gap_w + badge_w + close_gap_w);
            let close_end = has_close.then_some(
                accent_w
                    + 1
                    + icon_w
                    + label_w
                    + badge_gap_w
                    + badge_w
                    + close_gap_w
                    + close_zone_w,
            );
            TabMetrics {
                width: accent_w
                    + 1
                    + icon_w
                    + label_w
                    + badge_gap_w
                    + badge_w
                    + close_gap_w
                    + close_zone_w
                    + 1,
                close_start,
                close_end,
                label_width: label_w,
            }
        }
    }
}

fn natural_tab_metrics(tabs: &[DraggableTab], opts: &TabDisplayOptions<'_>) -> Vec<TabMetrics> {
    tabs.iter()
        .map(|tab| tab_metrics_with_options(tab, opts))
        .collect()
}

fn tab_metrics_for_viewport(
    tabs: &[DraggableTab],
    opts: &TabDisplayOptions<'_>,
    viewport_width: Option<usize>,
) -> Vec<TabMetrics> {
    let natural = natural_tab_metrics(tabs, opts);
    let Some(viewport_width) = viewport_width else {
        return natural;
    };

    let DraggableTabBarOverflow::ShrinkThenScroll { min_tab_width } = opts.overflow else {
        return natural;
    };

    if tabs.is_empty() || viewport_width == 0 {
        return natural;
    }

    let natural_total = total_width_for_metrics(&natural, opts);
    if natural_total <= viewport_width {
        return natural;
    }

    let min_tab_width = (min_tab_width as usize).max(1);
    let min_label_widths = natural
        .iter()
        .map(|metrics| {
            let fixed_width = metrics.width.saturating_sub(metrics.label_width);
            metrics
                .label_width
                .min(min_tab_width.saturating_sub(fixed_width))
        })
        .collect::<Vec<_>>();
    let fixed_total = natural
        .iter()
        .map(|metrics| metrics.width.saturating_sub(metrics.label_width))
        .sum::<usize>()
        .saturating_add(separator_width(opts.variant, opts.divider) * tabs.len().saturating_sub(1));
    let min_total = fixed_total.saturating_add(min_label_widths.iter().sum::<usize>());

    if min_total >= natural_total {
        return natural;
    }

    if min_total > viewport_width {
        return tabs
            .iter()
            .zip(min_label_widths)
            .map(|(tab, label_width)| tab_metrics_with_label_width(tab, opts, label_width))
            .collect();
    }

    let max_label_width = natural
        .iter()
        .map(|metrics| metrics.label_width)
        .max()
        .unwrap_or(0);
    let mut low = 0usize;
    let mut high = max_label_width;
    while low < high {
        let mid = (low + high).div_ceil(2);
        let total = total_width_for_label_cap(&natural, &min_label_widths, fixed_total, mid);
        if total <= viewport_width {
            low = mid;
        } else {
            high = mid.saturating_sub(1);
        }
    }

    let cap = low;
    let mut label_widths = natural
        .iter()
        .zip(&min_label_widths)
        .map(|(metrics, &min_label_width)| metrics.label_width.min(cap.max(min_label_width)))
        .collect::<Vec<_>>();
    let total = fixed_total.saturating_add(label_widths.iter().sum::<usize>());
    let mut spare = viewport_width.saturating_sub(total);
    if spare > 0 {
        for (label_width, natural_metrics) in label_widths.iter_mut().zip(&natural) {
            if spare == 0 {
                break;
            }
            if *label_width < natural_metrics.label_width {
                *label_width += 1;
                spare -= 1;
            }
        }
    }

    tabs.iter()
        .zip(label_widths)
        .map(|(tab, label_width)| tab_metrics_with_label_width(tab, opts, label_width))
        .collect()
}

fn total_width_for_label_cap(
    natural: &[TabMetrics],
    min_label_widths: &[usize],
    fixed_total: usize,
    cap: usize,
) -> usize {
    fixed_total.saturating_add(
        natural
            .iter()
            .zip(min_label_widths)
            .map(|(metrics, &min_label_width)| metrics.label_width.min(cap.max(min_label_width)))
            .sum::<usize>(),
    )
}

fn total_width_for_metrics(metrics: &[TabMetrics], opts: &TabDisplayOptions<'_>) -> usize {
    let tabs_width = metrics.iter().map(|metrics| metrics.width).sum::<usize>();
    tabs_width.saturating_add(
        separator_width(opts.variant, opts.divider) * metrics.len().saturating_sub(1),
    )
}

fn tab_runs_for_metrics(
    metrics: &[TabMetrics],
    opts: &TabDisplayOptions<'_>,
) -> (Vec<(usize, usize, TabMetrics)>, usize) {
    let mut runs = Vec::with_capacity(metrics.len());
    let mut total_width = 0usize;
    for (i, tab_metrics) in metrics.iter().copied().enumerate() {
        let start = total_width;
        let end = start.saturating_add(tab_metrics.width);
        runs.push((start, end, tab_metrics));
        total_width = end;
        if i + 1 < metrics.len() {
            total_width = total_width.saturating_add(separator_width(opts.variant, opts.divider));
        }
    }
    (runs, total_width)
}

fn tab_runs_for_viewport(
    tabs: &[DraggableTab],
    opts: &TabDisplayOptions<'_>,
    viewport_width: Option<usize>,
) -> (Vec<(usize, usize, TabMetrics)>, usize) {
    let metrics = tab_metrics_for_viewport(tabs, opts, viewport_width);
    tab_runs_for_metrics(&metrics, opts)
}

fn tabs_prefix_width_from_metrics(
    metrics: &[TabMetrics],
    opts: &TabDisplayOptions<'_>,
    offset: usize,
) -> usize {
    let upto = offset.min(metrics.len());
    let tabs_width = metrics
        .iter()
        .take(upto)
        .map(|metrics| metrics.width)
        .sum::<usize>();
    tabs_width.saturating_add(separator_width(opts.variant, opts.divider) * upto.saturating_sub(1))
}

#[cfg(test)]
fn tabs_prefix_width(tabs: &[DraggableTab], opts: &TabDisplayOptions<'_>, offset: usize) -> usize {
    let metrics = tab_metrics_for_viewport(tabs, opts, None);
    tabs_prefix_width_from_metrics(&metrics, opts, offset)
}

pub(crate) fn tab_fully_visible_at_offset(
    tabs: &[DraggableTab],
    opts: &TabDisplayOptions<'_>,
    tab_index: usize,
    offset: usize,
    viewport_width: usize,
) -> bool {
    if tabs.is_empty() || tab_index >= tabs.len() || viewport_width == 0 {
        return false;
    }
    let metrics = tab_metrics_for_viewport(tabs, opts, Some(viewport_width));
    let tab_start = tabs_prefix_width_from_metrics(&metrics, opts, tab_index);
    let tab_width = metrics[tab_index].width;
    let tab_end = tab_start.saturating_add(tab_width);
    tab_start >= offset && tab_end <= offset.saturating_add(viewport_width)
}

fn overflow_control_label(side: OverflowControlSide, hidden_count: usize) -> Arc<str> {
    match side {
        OverflowControlSide::Left => Arc::from(format!("{} ", hidden_count)),
        OverflowControlSide::Right => Arc::from(format!("{}", hidden_count)),
    }
}

fn overflow_control_width(side: OverflowControlSide, hidden_count: usize) -> usize {
    UnicodeWidthStr::width(overflow_control_label(side, hidden_count).as_ref())
}

fn lookup_icon_override<'a>(
    key: &str,
    overrides: &'a HashMap<Arc<str>, FileIconOverride>,
) -> Option<&'a FileIconOverride> {
    let path = Path::new(key);
    if let Some(name) = path.file_name().and_then(|n| n.to_str())
        && let Some(override_icon) = overrides.get(name)
    {
        return Some(override_icon);
    }
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        return overrides.get(ext);
    }
    None
}

pub(crate) fn resolve_tab_icon(
    tab: &DraggableTab,
    show_file_icons: bool,
    file_icon_style: FileIconStyle,
    file_icon_palette: &FileIconPalette,
    file_icon_overrides: &HashMap<Arc<str>, FileIconOverride>,
) -> Option<Span> {
    if let Some(leading) = &tab.leading {
        return Some(leading.to_span());
    }

    if let Some(icon) = &tab.icon {
        return Some(icon.clone());
    }

    if !show_file_icons {
        return None;
    }

    let key = tab.path.as_deref().unwrap_or(tab.label.as_ref());
    if let Some(override_icon) = lookup_icon_override(key, file_icon_overrides) {
        let mut span = Span::new(override_icon.icon.clone());
        if let Some(color) = override_icon.color {
            span = span.fg(color);
        }
        return Some(span);
    }

    match file_icon_style {
        FileIconStyle::Text => Some(Span::new("[F]")),
        FileIconStyle::NerdFont | FileIconStyle::NerdFontColored => {
            let (icon, color) = file_icon(key, file_icon_palette);
            let mut span = Span::new(icon);
            if matches!(file_icon_style, FileIconStyle::NerdFontColored)
                && let Some(color) = color
            {
                span = span.fg(color);
            }
            Some(span)
        }
    }
}

impl From<DraggableTabBar> for Element {
    fn from(value: DraggableTabBar) -> Self {
        Element::new(ElementKind::DraggableTabBar(Box::new(value)))
    }
}

impl crate::layout::hash::LayoutHash for DraggableTabBar {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        _recurse: &dyn Fn(&crate::core::element::Element) -> Option<u64>,
    ) -> Option<()> {
        use std::hash::Hash;
        self.width.hash(hasher);
        self.height.hash(hasher);
        self.border.hash(hasher);
        self.border_style.hash(hasher);
        self.padding.hash(hasher);
        self.tabs.len().hash(hasher);
        self.active.hash(hasher);
        self.divider.hash(hasher);
        self.close_symbol.hash(hasher);
        self.accent_symbol.hash(hasher);
        self.active_accent_symbol.hash(hasher);
        self.close_on_hover_only.hash(hasher);
        self.tab_max_width.hash(hasher);
        self.overflow.hash(hasher);
        self.show_overflow_controls.hash(hasher);
        self.scroll_offset.hash(hasher);
        self.show_file_icons.hash(hasher);
        self.file_icon_style.hash(hasher);
        self.variant.hash(hasher);
        self.show_close_buttons.hash(hasher);
        self.draggable.hash(hasher);
        self.drag_preview.hash(hasher);
        self.reorder_mode.hash(hasher);
        self.drag_threshold.hash(hasher);
        Some(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::style::{FileIconPalette, Span};

    use super::{
        DraggableTab, DraggableTabBar, DraggableTabBarOverflow, DraggableTabBarVariant,
        DraggableTabHitPart,
    };
    use crate::widgets::FileIconStyle;

    #[test]
    fn hit_at_col_detects_close_region() {
        let tabs = vec![DraggableTab::new("main.rs").closeable(true)];
        let label_w = "main.rs".chars().count();
        let hit = DraggableTabBar::hit_at_col(
            &tabs,
            DraggableTabBarVariant::Bordered,
            '|',
            '|',
            "x",
            true,
            1 + label_w + 1,
        )
        .expect("expected hit");
        assert_eq!(hit.index, 0);
        assert_eq!(hit.part, DraggableTabHitPart::Close);
    }

    #[test]
    fn hit_at_col_detects_close_region_with_badge() {
        let tab = DraggableTab::new("main.rs")
            .right_badge(Span::new("M"))
            .closeable(true);
        let tabs = vec![tab];
        let metrics =
            super::tab_metrics(&tabs[0], DraggableTabBarVariant::Bordered, '|', "x", true);
        let close_col = metrics.close_start.expect("close start");
        let hit = DraggableTabBar::hit_at_col(
            &tabs,
            DraggableTabBarVariant::Bordered,
            '|',
            '|',
            "x",
            true,
            close_col,
        )
        .expect("expected hit");
        assert_eq!(hit.index, 0);
        assert_eq!(hit.part, DraggableTabHitPart::Close);
    }

    #[test]
    fn action_tab_does_not_reserve_close_region() {
        let tab = DraggableTab::action("+").closeable(true);
        let metrics = super::tab_metrics(&tab, DraggableTabBarVariant::Bordered, '|', "x", true);

        assert_eq!(metrics.close_start, None);
        assert_eq!(metrics.close_end, None);
        assert_eq!(metrics.width, 3);
    }

    #[test]
    fn hit_at_col_returns_none_on_separator() {
        let tabs = vec![DraggableTab::new("a"), DraggableTab::new("b")];
        let separator_col = 3; // first tab " a " is width 3
        let hit = DraggableTabBar::hit_at_col(
            &tabs,
            DraggableTabBarVariant::Bordered,
            '|',
            '|',
            "x",
            true,
            separator_col,
        );
        assert!(hit.is_none());
    }

    #[test]
    fn frame_line_content_width_is_nonzero() {
        let tabs = vec![DraggableTab::new("file.rs").closeable(true)];
        let width = DraggableTabBar::content_width(
            &tabs,
            DraggableTabBarVariant::FrameLine,
            '|',
            '|',
            "x",
            true,
        );
        assert!(width > 0);
    }

    #[test]
    fn reorder_index_maps_separator_to_adjacent_tab() {
        let tabs = vec![DraggableTab::new("a"), DraggableTab::new("b")];
        let separator_col = 3; // first tab " a " is width 3
        let idx = DraggableTabBar::reorder_index_at_col(
            &tabs,
            DraggableTabBarVariant::Bordered,
            '|',
            '|',
            "x",
            true,
            separator_col,
        )
        .expect("expected mapped index");
        assert_eq!(idx, 1);
    }

    #[test]
    fn adjacent_reorder_waits_until_midpoint_for_wider_neighbor() {
        let tabs = vec![DraggableTab::new("a"), DraggableTab::new("very-long-name")];
        let at_divider = 3; // after " a " in bordered variant

        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: super::DraggableTabBarOverflow::Scroll,
            show_file_icons: false,
            file_icon_style: FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &HashMap::new(),
        };
        let target = DraggableTabBar::adjacent_reorder_target(&tabs, &opts, 0, at_divider);
        assert!(target.is_none());

        let second_start =
            super::tab_metrics(&tabs[0], DraggableTabBarVariant::Bordered, '|', "x", false).width
                + super::separator_width(DraggableTabBarVariant::Bordered, '|');
        let second_mid = second_start
            + super::tab_metrics(&tabs[1], DraggableTabBarVariant::Bordered, '|', "x", false).width
                / 2;
        let target = DraggableTabBar::adjacent_reorder_target(&tabs, &opts, 0, second_mid);
        assert_eq!(target, Some(1));
    }

    #[test]
    fn adjacent_reorder_does_not_target_trailing_action_tab() {
        let tabs = vec![
            DraggableTab::new("a"),
            DraggableTab::new("b"),
            DraggableTab::action("+"),
        ];
        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: super::DraggableTabBarOverflow::Scroll,
            show_file_icons: false,
            file_icon_style: FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &HashMap::new(),
        };
        let action_mid = super::tabs_prefix_width(&tabs, &opts, 2)
            + super::tab_metrics(&tabs[2], DraggableTabBarVariant::Bordered, '|', "x", false).width
                / 2;

        let target = DraggableTabBar::adjacent_reorder_target(&tabs, &opts, 1, action_mid);

        assert_eq!(target, None);
    }

    #[test]
    fn on_drop_reorder_maps_action_hit_to_previous_tab() {
        let tabs = vec![
            DraggableTab::new("a"),
            DraggableTab::new("b"),
            DraggableTab::action("+"),
        ];
        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: super::DraggableTabBarOverflow::Scroll,
            show_file_icons: false,
            file_icon_style: FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &HashMap::new(),
        };
        let action_col = super::tabs_prefix_width(&tabs, &opts, 2);

        let raw_target =
            DraggableTabBar::reorder_index_at_col_with_options(&tabs, &opts, action_col);
        let reorder_target = super::reorder_target_at_col_with_options(&tabs, &opts, action_col);

        assert_eq!(raw_target, Some(2));
        assert_eq!(reorder_target, Some(1));
    }

    #[test]
    fn viewport_layout_keeps_partially_visible_tab() {
        let tabs = vec![
            DraggableTab::new("alpha"),
            DraggableTab::new("beta-gamma"),
            DraggableTab::new("delta"),
        ];
        let first =
            super::tab_metrics(&tabs[0], DraggableTabBarVariant::Bordered, '|', "x", false).width;
        let sep = super::separator_width(DraggableTabBarVariant::Bordered, '|');
        let second =
            super::tab_metrics(&tabs[1], DraggableTabBarVariant::Bordered, '|', "x", false).width;
        let viewport_width = first + sep + (second / 2).max(1);

        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let layout = DraggableTabBar::viewport_layout(
            &tabs,
            &super::TabDisplayOptions {
                variant: DraggableTabBarVariant::Bordered,
                divider: '|',
                accent_symbol: '|',
                close_symbol: "x",
                show_close_buttons: false,
                tab_max_width: None,
                overflow: super::DraggableTabBarOverflow::Scroll,
                show_file_icons: false,
                file_icon_style: crate::widgets::FileIconStyle::NerdFont,
                file_icon_palette: &FileIconPalette::default(),
                file_icon_overrides: &overrides,
            },
            &super::TabViewportOptions {
                scroll_offset: 0,
                viewport_width,
                show_overflow_controls: true,
            },
        );

        assert_eq!(layout.visible_tabs.len(), 2);
        assert_eq!(layout.visible_tabs[0].index, 0);
        assert_eq!(layout.visible_tabs[1].index, 1);
        assert!(
            layout.visible_tabs[1]
                .end
                .saturating_sub(layout.visible_tabs[1].start)
                < second
        );
        assert_eq!(layout.hidden_right, 2);
    }

    #[test]
    fn viewport_layout_can_clip_left_tab() {
        let tabs = vec![
            DraggableTab::new("alpha"),
            DraggableTab::new("beta"),
            DraggableTab::new("gamma"),
        ];
        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let layout = DraggableTabBar::viewport_layout(
            &tabs,
            &super::TabDisplayOptions {
                variant: DraggableTabBarVariant::Bordered,
                divider: '|',
                accent_symbol: '|',
                close_symbol: "x",
                show_close_buttons: false,
                tab_max_width: None,
                overflow: super::DraggableTabBarOverflow::Scroll,
                show_file_icons: false,
                file_icon_style: crate::widgets::FileIconStyle::NerdFont,
                file_icon_palette: &FileIconPalette::default(),
                file_icon_overrides: &overrides,
            },
            &super::TabViewportOptions {
                scroll_offset: 1,
                viewport_width: 10,
                show_overflow_controls: true,
            },
        );

        assert!(layout.hidden_left > 0);
        let first = layout.visible_tabs.first().expect("expected visible tabs");
        assert_eq!(first.index, 0);
        assert!(first.clip_left > 0);
    }

    #[test]
    fn shrink_then_scroll_fits_tabs_before_scrolling() {
        let tabs = vec![
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
        ];
        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: DraggableTabBarOverflow::ShrinkThenScroll { min_tab_width: 5 },
            show_file_icons: false,
            file_icon_style: crate::widgets::FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &overrides,
        };

        let layout = DraggableTabBar::viewport_layout(
            &tabs,
            &opts,
            &super::TabViewportOptions {
                scroll_offset: 0,
                viewport_width: 20,
                show_overflow_controls: true,
            },
        );

        assert_eq!(layout.hidden_left, 0);
        assert_eq!(layout.hidden_right, 0);
        assert!(layout.left_control.is_none());
        assert!(layout.right_control.is_none());
        assert_eq!(layout.visible_tabs.len(), 3);
        assert_eq!(layout.visible_tabs[0].metrics.label_width, 4);
        assert_eq!(layout.visible_tabs.last().expect("last tab").end, 20);
    }

    #[test]
    fn shrink_then_scroll_scrolls_after_min_widths() {
        let tabs = vec![
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
        ];
        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: DraggableTabBarOverflow::ShrinkThenScroll { min_tab_width: 5 },
            show_file_icons: false,
            file_icon_style: crate::widgets::FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &overrides,
        };

        let layout = DraggableTabBar::viewport_layout(
            &tabs,
            &opts,
            &super::TabViewportOptions {
                scroll_offset: 0,
                viewport_width: 14,
                show_overflow_controls: true,
            },
        );

        assert_eq!(layout.visible_tabs[0].metrics.width, 5);
        assert_eq!(layout.visible_tabs[0].metrics.label_width, 3);
        assert_eq!(layout.hidden_left, 0);
        assert!(layout.hidden_right > 0);
        assert!(layout.right_control.is_some());
    }

    #[test]
    fn shrink_then_scroll_hit_testing_uses_shrunken_widths() {
        let tabs = vec![
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
            DraggableTab::new("abcdef"),
        ];
        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: DraggableTabBarOverflow::ShrinkThenScroll { min_tab_width: 5 },
            show_file_icons: false,
            file_icon_style: crate::widgets::FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &overrides,
        };

        let hit = DraggableTabBar::hit_target_at_view_col(
            &tabs,
            &opts,
            &super::TabViewportOptions {
                scroll_offset: 0,
                viewport_width: 20,
                show_overflow_controls: true,
            },
            7,
        );

        assert!(matches!(
            hit,
            Some(super::DraggableTabHitTarget::Tab(super::DraggableTabHit {
                index: 1,
                part: DraggableTabHitPart::Body,
            }))
        ));
    }

    #[test]
    fn overflow_right_label_has_left_padding() {
        assert_eq!(
            super::overflow_control_label(super::OverflowControlSide::Right, 1).as_ref(),
            "  1"
        );
    }

    #[test]
    fn stepping_right_reaches_visible_right_edge() {
        let tabs = vec![
            DraggableTab::new("alpha.rs"),
            DraggableTab::new("beta-long-file-name.rs"),
            DraggableTab::new("gamma.rs"),
            DraggableTab::new("delta.rs"),
            DraggableTab::new("epsilon.rs"),
        ];
        let overrides: HashMap<std::sync::Arc<str>, super::FileIconOverride> = HashMap::new();
        let mut offset = 0usize;
        let viewport_width = 24usize;
        let disp_opts = super::TabDisplayOptions {
            variant: DraggableTabBarVariant::Bordered,
            divider: '|',
            accent_symbol: '|',
            close_symbol: "x",
            show_close_buttons: false,
            tab_max_width: None,
            overflow: super::DraggableTabBarOverflow::Scroll,
            show_file_icons: false,
            file_icon_style: crate::widgets::FileIconStyle::NerdFont,
            file_icon_palette: &FileIconPalette::default(),
            file_icon_overrides: &overrides,
        };

        for _ in 0..64 {
            let next = DraggableTabBar::scroll_offset_for_step(
                &tabs,
                &disp_opts,
                &super::TabViewportOptions {
                    scroll_offset: offset,
                    viewport_width,
                    show_overflow_controls: true,
                },
                true,
                super::TAB_SCROLL_STEP_CHARS,
            );
            if next == offset {
                break;
            }
            offset = next;
        }

        let layout = DraggableTabBar::viewport_layout(
            &tabs,
            &disp_opts,
            &super::TabViewportOptions {
                scroll_offset: offset,
                viewport_width,
                show_overflow_controls: true,
            },
        );

        assert_eq!(
            layout.hidden_right, 0,
            "offset={} content_width={} hidden_left={} layout={:?}",
            layout.offset, layout.content_width, layout.hidden_left, layout
        );
    }
}