mergers 0.8.2

A visual diff and merge tool for files and directories
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
#[allow(clippy::wildcard_imports)]
use super::*;

// ─── Directory Logic (Pure Functions) ──────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub struct DirRowInfo {
    pub status: FileStatus,
    pub name: String,
    pub is_dir: bool,
    pub rel_path: String,
    pub left_size: Option<u64>,
    pub left_mtime: Option<SystemTime>,
    pub right_size: Option<u64>,
    pub right_mtime: Option<SystemTime>,
}

impl DirRowInfo {
    pub fn encode(&self) -> String {
        let s = self.status.code();
        let d = if self.is_dir { "1" } else { "0" };
        let ls = self.left_size.map(format_size).unwrap_or_default();
        let lm = self.left_mtime.map(format_mtime).unwrap_or_default();
        let rs = self.right_size.map(format_size).unwrap_or_default();
        let rm = self.right_mtime.map(format_mtime).unwrap_or_default();
        format!(
            "{s}{SEP}{name}{SEP}{d}{SEP}{rel}{SEP}{ls}{SEP}{lm}{SEP}{rs}{SEP}{rm}",
            name = self.name,
            rel = self.rel_path
        )
    }

    pub fn decode(raw: &str) -> Self {
        let mut parts = raw.splitn(8, SEP);
        let status_code = parts.next().unwrap_or("S");
        let name = parts.next().unwrap_or("").to_string();
        let is_dir = parts.next().unwrap_or("0") == "1";
        let rel_path = parts.next().unwrap_or("").to_string();
        // Skip metadata decoding for now as it's string-formatted;
        // in a real app we might want to store raw values or just use strings for display.
        // For this refactor, we primarily need the identity fields.

        let status = match status_code {
            "D" => FileStatus::Different,
            "C" => FileStatus::Conflict,
            "L" => FileStatus::LeftOnly,
            "R" => FileStatus::RightOnly,
            _ => FileStatus::Same,
        };

        DirRowInfo {
            status,
            name,
            is_dir,
            rel_path,
            left_size: None, // Decoding metadata from formatted string is lossy/complex
            left_mtime: None,
            right_size: None,
            right_mtime: None,
        }
    }

    // Helper to get just the field text for display
    pub fn get_field_text(raw: &str, index: usize) -> &str {
        raw.splitn(8, SEP).nth(index).unwrap_or("")
    }
}

// ─── Directory copy helper ──────────────────────────────────────────────────

fn copy_path_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
    if src.is_file() {
        if let Some(parent) = dst.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::copy(src, dst)?;
    } else if src.is_dir() {
        for entry in walkdir::WalkDir::new(src) {
            let entry = entry.map_err(std::io::Error::other)?;
            let Ok(rel) = entry.path().strip_prefix(src) else {
                continue;
            };
            let target = dst.join(rel);
            if entry.file_type().is_dir() {
                fs::create_dir_all(&target)?;
            } else {
                if let Some(parent) = target.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::copy(entry.path(), &target)?;
            }
        }
    }
    Ok(())
}

/// Recursively collect `(rel_path, status)` of all non-conflicting, non-same files.
fn collect_actionable_files(
    store: &ListStore,
    cm: &HashMap<String, ListStore>,
    out: &mut Vec<(String, FileStatus)>,
) {
    for i in 0..store.n_items() {
        if let Some(obj) = store.item(i).and_downcast::<StringObject>() {
            let info = DirRowInfo::decode(&obj.string());
            if info.is_dir {
                if let Some(child_store) = cm.get(&info.rel_path) {
                    collect_actionable_files(child_store, cm, out);
                }
            } else {
                match info.status {
                    FileStatus::Different | FileStatus::RightOnly | FileStatus::LeftOnly => {
                        out.push((info.rel_path, info.status));
                    }
                    FileStatus::Conflict | FileStatus::Same => {}
                }
            }
        }
    }
}

// ─── Directory scanning ────────────────────────────────────────────────────

fn read_dir_entries(
    dir: &Path,
    dir_filters: &[String],
    hide_hidden: bool,
) -> BTreeMap<String, DirMeta> {
    let mut map = BTreeMap::new();
    if let Ok(rd) = fs::read_dir(dir) {
        for entry in rd.filter_map(Result::ok) {
            let Ok(name) = entry.file_name().into_string() else {
                continue;
            };
            if dir_filters.iter().any(|f| f == &name) {
                continue;
            }
            if hide_hidden && name.starts_with('.') {
                continue;
            }
            // Use lstat (file_type/symlink_metadata) consistently to avoid
            // following symlinks to directories (prevents infinite recursion)
            // and to get consistent size/mtime for symlinks-to-dirs.
            let is_dir = entry.file_type().ok().is_some_and(|ft| ft.is_dir());
            let meta = fs::symlink_metadata(entry.path()).ok();
            map.insert(
                name,
                DirMeta {
                    size: meta.as_ref().map(std::fs::Metadata::len),
                    mtime: meta.as_ref().and_then(|m| m.modified().ok()),
                    is_dir,
                },
            );
        }
    }
    map
}

/// A single entry produced by the background-safe `scan_tree`.
/// Contains all data needed to build GTK stores on the main thread.
struct ScanEntry {
    status: FileStatus,
    name: String,
    is_dir: bool,
    rel_path: String,
    left_size: Option<u64>,
    left_mtime: Option<SystemTime>,
    right_size: Option<u64>,
    right_mtime: Option<SystemTime>,
    /// Child entries (only populated for directories).
    children: Vec<ScanEntry>,
}

/// Background-safe directory comparison: walks directories, reads files, and
/// compares contents. Returns `(entries, aggregate_status)`.
/// Does NOT use any GTK types, so it is `Send` and can run on a worker thread.
fn scan_tree(
    left_root: &Path,
    right_root: &Path,
    rel: &str,
    dir_filters: &[String],
    hide_hidden: bool,
    patch_mode: bool,
) -> (Vec<ScanEntry>, FileStatus) {
    // Only read the conflicts marker in patch mode to avoid misclassifying
    // files when a real directory happens to contain `.mergers-conflicts`.
    let conflicts = if patch_mode && rel.is_empty() {
        let marker = right_root.join(".mergers-conflicts");
        if let Ok(content) = fs::read_to_string(&marker) {
            content.lines().map(String::from).collect()
        } else {
            HashSet::new()
        }
    } else {
        HashSet::new()
    };
    scan_tree_inner(
        left_root,
        right_root,
        rel,
        dir_filters,
        hide_hidden,
        &conflicts,
    )
}

fn scan_tree_inner(
    left_root: &Path,
    right_root: &Path,
    rel: &str,
    dir_filters: &[String],
    hide_hidden: bool,
    conflicts: &HashSet<String>,
) -> (Vec<ScanEntry>, FileStatus) {
    let left_dir = if rel.is_empty() {
        left_root.to_path_buf()
    } else {
        left_root.join(rel)
    };
    let right_dir = if rel.is_empty() {
        right_root.to_path_buf()
    } else {
        right_root.join(rel)
    };

    let left_entries = read_dir_entries(&left_dir, dir_filters, hide_hidden);
    let right_entries = read_dir_entries(&right_dir, dir_filters, hide_hidden);
    let all: BTreeSet<&String> = left_entries.keys().chain(right_entries.keys()).collect();

    // Sort: directories first, then files, alphabetically within each group
    let mut names: Vec<(&String, bool)> = all
        .iter()
        .map(|n| {
            let is_dir = left_entries.get(*n).is_some_and(|m| m.is_dir)
                || right_entries.get(*n).is_some_and(|m| m.is_dir);
            (*n, is_dir)
        })
        .collect();
    names.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(b.0)));

    let mut entries = Vec::new();
    let mut all_same = true;

    for (name, is_dir) in &names {
        let in_left = left_entries.contains_key(*name);
        let in_right = right_entries.contains_key(*name);
        let child_rel = if rel.is_empty() {
            (*name).clone()
        } else {
            format!("{rel}/{name}")
        };

        let (status, children);
        if *is_dir {
            let (child_entries, child_agg) = scan_tree_inner(
                left_root,
                right_root,
                &child_rel,
                dir_filters,
                hide_hidden,
                conflicts,
            );
            status = if !in_left {
                FileStatus::RightOnly
            } else if !in_right {
                FileStatus::LeftOnly
            } else {
                child_agg
            };
            children = child_entries;
        } else {
            children = Vec::new();
            status = match (in_left, in_right) {
                (true, false) => {
                    if conflicts.contains(&child_rel) {
                        FileStatus::Conflict
                    } else {
                        FileStatus::LeftOnly
                    }
                }
                (false, true) => {
                    if conflicts.contains(&child_rel) {
                        FileStatus::Conflict
                    } else {
                        FileStatus::RightOnly
                    }
                }
                (true, true) => {
                    // Check conflict marker first — conflicted files should
                    // always show as Conflict even if contents are identical
                    if conflicts.contains(&child_rel) {
                        FileStatus::Conflict
                    } else {
                        let lm = left_entries.get(*name);
                        let rm = right_entries.get(*name);
                        // Fast path: different sizes means different content
                        if lm.and_then(|m| m.size) == rm.and_then(|m| m.size) {
                            let lc = fs::read(left_dir.join(name));
                            let rc = fs::read(right_dir.join(name));
                            match (lc, rc) {
                                (Ok(l), Ok(r)) if l == r => FileStatus::Same,
                                // Read errors (e.g. symlink-to-dir) → treat as different
                                _ => FileStatus::Different,
                            }
                        } else {
                            FileStatus::Different
                        }
                    }
                }
                _ => unreachable!(),
            };
        }

        if status != FileStatus::Same {
            all_same = false;
        }

        let lm = left_entries.get(*name);
        let rm = right_entries.get(*name);
        entries.push(ScanEntry {
            status,
            name: (*name).clone(),
            is_dir: *is_dir,
            rel_path: child_rel,
            left_size: lm.and_then(|m| m.size),
            left_mtime: lm.and_then(|m| m.mtime),
            right_size: rm.and_then(|m| m.size),
            right_mtime: rm.and_then(|m| m.mtime),
            children,
        });
    }

    let agg = if all_same {
        FileStatus::Same
    } else {
        FileStatus::Different
    };
    (entries, agg)
}

/// Main-thread: converts a `ScanEntry` tree into a `ListStore` + `children_map`.
fn build_stores(entries: &[ScanEntry], children_map: &mut HashMap<String, ListStore>) -> ListStore {
    let store = ListStore::new::<StringObject>();
    for entry in entries {
        // Skip Same entries — they have no visual purpose in the dir view
        if entry.status == FileStatus::Same {
            continue;
        }
        if entry.is_dir {
            let child_store = build_stores(&entry.children, children_map);
            // Skip empty directories (all children are Same)
            if child_store.n_items() == 0 {
                continue;
            }
            children_map.insert(entry.rel_path.clone(), child_store);
        }
        let info = DirRowInfo {
            status: entry.status,
            name: entry.name.clone(),
            is_dir: entry.is_dir,
            rel_path: entry.rel_path.clone(),
            left_size: entry.left_size,
            left_mtime: entry.left_mtime,
            right_size: entry.right_size,
            right_mtime: entry.right_mtime,
        };
        store.append(&StringObject::new(&info.encode()));
    }
    store
}

// ─── CSS helpers ───────────────────────────────────────────────────────────

fn apply_status_class(widget: &impl WidgetExt, status: &str, is_left: bool) {
    for cls in &[
        "diff-changed",
        "diff-conflict",
        "diff-deleted",
        "diff-inserted",
        "diff-missing",
    ] {
        widget.remove_css_class(cls);
    }
    if is_left {
        match status {
            "D" => widget.add_css_class("diff-changed"),
            "C" => widget.add_css_class("diff-conflict"),
            "L" => widget.add_css_class("diff-deleted"),
            "R" => widget.add_css_class("diff-missing"),
            _ => {}
        }
    } else {
        match status {
            "D" => widget.add_css_class("diff-changed"),
            "C" => widget.add_css_class("diff-conflict"),
            "R" => widget.add_css_class("diff-inserted"),
            "L" => widget.add_css_class("diff-missing"),
            _ => {}
        }
    }
}

// ─── Column factories ──────────────────────────────────────────────────────

/// Name column: `TreeExpander` → Box → [Icon, Label]
fn make_name_factory(is_left: bool) -> SignalListItemFactory {
    let factory = SignalListItemFactory::new();

    factory.connect_setup(|_, list_item| {
        let item = list_item.downcast_ref::<ListItem>().unwrap();
        let expander = TreeExpander::new();
        let hbox = GtkBox::new(Orientation::Horizontal, 4);
        let icon = Image::new();
        let label = Label::new(None);
        label.set_halign(gtk4::Align::Start);
        label.set_hexpand(true);
        hbox.append(&icon);
        hbox.append(&label);
        expander.set_child(Some(&hbox));
        item.set_child(Some(&expander));
    });

    factory.connect_bind(move |_, list_item| {
        let item = list_item.downcast_ref::<ListItem>().unwrap();
        let row = item.item().and_downcast::<TreeListRow>().unwrap();
        let obj = row.item().and_downcast::<StringObject>().unwrap();
        let raw = obj.string();

        let expander = item.child().and_downcast::<TreeExpander>().unwrap();
        expander.set_list_row(Some(&row));

        let hbox = expander.child().and_downcast::<GtkBox>().unwrap();
        let icon = hbox.first_child().and_downcast::<Image>().unwrap();
        let label = icon.next_sibling().and_downcast::<Label>().unwrap();

        let info = DirRowInfo::decode(&raw);

        icon.set_icon_name(Some(if info.is_dir {
            "folder-symbolic"
        } else {
            "text-x-generic-symbolic"
        }));
        label.set_label(&info.name);
        apply_status_class(&label, info.status.code(), is_left);
    });

    factory
}

/// Size / Mtime column: plain label at `field_idx` from encoded row.
fn make_field_factory(
    is_left: bool,
    field_idx: usize,
    align: gtk4::Align,
) -> SignalListItemFactory {
    let factory = SignalListItemFactory::new();

    factory.connect_setup(move |_, list_item| {
        let item = list_item.downcast_ref::<ListItem>().unwrap();
        let label = Label::new(None);
        label.set_halign(align);
        item.set_child(Some(&label));
    });

    factory.connect_bind(move |_, list_item| {
        let item = list_item.downcast_ref::<ListItem>().unwrap();
        let row = item.item().and_downcast::<TreeListRow>().unwrap();
        let obj = row.item().and_downcast::<StringObject>().unwrap();
        let raw = obj.string();

        let label = item.child().and_downcast::<Label>().unwrap();
        let info = DirRowInfo::decode(&raw);
        let missing = (is_left && info.status == FileStatus::RightOnly)
            || (!is_left && info.status == FileStatus::LeftOnly);

        label.set_label(if missing {
            ""
        } else {
            DirRowInfo::get_field_text(&raw, field_idx)
        });
        apply_status_class(&label, info.status.code(), is_left);
    });

    factory
}

// ─── Directory comparison tab ──────────────────────────────────────────────

/// Build the directory comparison widget and all its state (tree model, file
/// watcher, copy/delete handlers, context menus). The widget can be embedded
/// as a tab in an existing notebook or wrapped in its own window.
///
/// Returns `(dir_tab, watcher_alive, left_view, title)`.
pub(super) fn build_dir_tab(
    left_dir: std::path::PathBuf,
    right_dir: std::path::PathBuf,
    labels: &[String],
    tooltip_dirs: &[String],
    settings: Rc<RefCell<Settings>>,
    notebook: &Notebook,
    open_tabs: &Rc<RefCell<Vec<FileTab>>>,
) -> (GtkBox, FileWatcher, ColumnView, String) {
    let left_label_override: Rc<RefCell<Option<String>>> =
        Rc::new(RefCell::new(labels.first().cloned()));
    let right_label_override: Rc<RefCell<Option<String>>> =
        Rc::new(RefCell::new(labels.get(1).cloned()));
    let left_tooltip_override: Rc<RefCell<Option<String>>> =
        Rc::new(RefCell::new(tooltip_dirs.first().cloned()));
    let right_tooltip_override: Rc<RefCell<Option<String>>> =
        Rc::new(RefCell::new(tooltip_dirs.get(1).cloned()));
    // Patch mode: tooltip_dirs is non-empty only for patch comparisons
    let patch_mode = !tooltip_dirs.is_empty();
    let left_dir = Rc::new(RefCell::new(
        std::fs::canonicalize(&left_dir)
            .unwrap_or(left_dir)
            .to_string_lossy()
            .into_owned(),
    ));
    let right_dir = Rc::new(RefCell::new(
        std::fs::canonicalize(&right_dir)
            .unwrap_or(right_dir)
            .to_string_lossy()
            .into_owned(),
    ));

    // Build tree data (Rc<RefCell<…>> so the watcher callback can rebuild)
    let children_map = Rc::new(RefCell::new(HashMap::new()));
    let root_store = ListStore::new::<StringObject>();
    // Initial scan runs in background; tree populates once complete
    {
        let ld = left_dir.borrow().clone();
        let rd = right_dir.borrow().clone();
        let filters = settings.borrow().dir_filters.clone();
        let hide_hidden = settings.borrow().hide_hidden_files;
        let cm = children_map.clone();
        let rs = root_store.clone();
        gtk4::glib::spawn_future_local(async move {
            let (entries, _) = gio::spawn_blocking(move || {
                scan_tree(
                    Path::new(&ld),
                    Path::new(&rd),
                    "",
                    &filters,
                    hide_hidden,
                    patch_mode,
                )
            })
            .await
            .unwrap();
            let store = build_stores(&entries, &mut cm.borrow_mut());
            for i in 0..store.n_items() {
                if let Some(obj) = store.item(i) {
                    rs.append(&obj.downcast::<StringObject>().unwrap());
                }
            }
        });
    }

    // Shared TreeListModel — both panes see the same tree structure
    let cm = children_map.clone();
    let tree_model = TreeListModel::new(root_store.clone(), false, false, move |item| {
        let obj = item.downcast_ref::<StringObject>()?;
        let raw = obj.string();
        let info = DirRowInfo::decode(&raw);
        if info.is_dir {
            cm.borrow()
                .get(&info.rel_path)
                .cloned()
                .map(gio::prelude::Cast::upcast::<gio::ListModel>)
        } else {
            None
        }
    });

    // Both panes share the same SingleSelection so the selected row is
    // highlighted on both sides.  The inactive pane is dimmed via opacity
    // and bordered differently to distinguish from the focused pane.
    let dir_sel = SingleSelection::new(Some(tree_model.clone()));

    // ── Left pane ──────────────────────────────────────────────────
    let left_view = ColumnView::new(Some(dir_sel.clone()));
    left_view.set_show_column_separators(true);
    {
        let col = ColumnViewColumn::new(Some("Name"), Some(make_name_factory(true)));
        col.set_expand(true);
        col.set_resizable(true);
        left_view.append_column(&col);
        let col = ColumnViewColumn::new(
            Some("Size"),
            Some(make_field_factory(true, 4, gtk4::Align::End)),
        );
        col.set_fixed_width(80);
        col.set_resizable(true);
        left_view.append_column(&col);
        let col = ColumnViewColumn::new(
            Some("Modification time"),
            Some(make_field_factory(true, 5, gtk4::Align::Start)),
        );
        col.set_fixed_width(200);
        col.set_resizable(true);
        left_view.append_column(&col);
    }

    // ── Right pane ─────────────────────────────────────────────────
    let right_view = ColumnView::new(Some(dir_sel.clone()));
    right_view.set_opacity(0.55);
    right_view.set_show_column_separators(true);
    {
        let col = ColumnViewColumn::new(Some("Name"), Some(make_name_factory(false)));
        col.set_expand(true);
        col.set_resizable(true);
        right_view.append_column(&col);
        let col = ColumnViewColumn::new(
            Some("Size"),
            Some(make_field_factory(false, 6, gtk4::Align::End)),
        );
        col.set_fixed_width(80);
        col.set_resizable(true);
        right_view.append_column(&col);
        let col = ColumnViewColumn::new(
            Some("Modification time"),
            Some(make_field_factory(false, 7, gtk4::Align::Start)),
        );
        col.set_fixed_width(200);
        col.set_resizable(true);
        right_view.append_column(&col);
    }

    // Track which pane was last focused (true = left, false = right).
    // Both panes share the same SingleSelection so the selected row is
    // highlighted on both sides; CSS borders distinguish the active pane.
    let focused_left = Rc::new(Cell::new(true));
    let sel_syncing = Rc::new(Cell::new(false));
    // CSS classes for focus borders are set on ScrolledWindows below (after
    // they are created), since ColumnView content is clipped by its parent.
    let left_scroll_slot: Rc<RefCell<Option<ScrolledWindow>>> = Rc::new(RefCell::new(None));
    let right_scroll_slot: Rc<RefCell<Option<ScrolledWindow>>> = Rc::new(RefCell::new(None));
    for (own_view, other_view, own_scroll, other_scroll, is_left) in [
        (
            &left_view,
            &right_view,
            &left_scroll_slot,
            &right_scroll_slot,
            true,
        ),
        (
            &right_view,
            &left_view,
            &right_scroll_slot,
            &left_scroll_slot,
            false,
        ),
    ] {
        let fl = focused_left.clone();
        let ov = own_view.clone();
        let otv = other_view.clone();
        let sel = dir_sel.clone();
        let os = own_scroll.clone();
        let ots = other_scroll.clone();
        let fc = EventControllerFocus::new();
        fc.connect_enter(move |_| {
            if fl.get() == is_left {
                return;
            }
            fl.set(is_left);
            ov.set_opacity(1.0);
            otv.set_opacity(0.55);
            if let Some(sw) = os.borrow().as_ref() {
                sw.add_css_class("pane-focused");
                sw.remove_css_class("pane-inactive");
            }
            if let Some(sw) = ots.borrow().as_ref() {
                sw.remove_css_class("pane-focused");
                sw.add_css_class("pane-inactive");
            }
            let pos = sel.selected();
            let vadj = ov
                .ancestor(ScrolledWindow::static_type())
                .and_downcast::<ScrolledWindow>()
                .map(|sw| (sw.vadjustment().value(), sw));
            let v = ov.clone();
            gtk4::glib::idle_add_local_once(move || {
                v.scroll_to(pos, None, gtk4::ListScrollFlags::FOCUS, None);
                if let Some((val, sw)) = vadj {
                    let sw2 = sw.clone();
                    gtk4::glib::idle_add_local_once(move || {
                        sw2.vadjustment().set_value(val);
                    });
                }
            });
        });
        own_view.add_controller(fc);
    }

    // Left/Right arrow keys switch between panes
    {
        let rv = right_view.clone();
        let kc = EventControllerKey::new();
        kc.set_propagation_phase(gtk4::PropagationPhase::Capture);
        kc.connect_key_pressed(move |_, key, _, _| {
            if key == gtk4::gdk::Key::Right {
                rv.grab_focus();
                gtk4::glib::Propagation::Stop
            } else {
                gtk4::glib::Propagation::Proceed
            }
        });
        left_view.add_controller(kc);
        let lv = left_view.clone();
        let kc = EventControllerKey::new();
        kc.set_propagation_phase(gtk4::PropagationPhase::Capture);
        kc.connect_key_pressed(move |_, key, _, _| {
            if key == gtk4::gdk::Key::Left {
                lv.grab_focus();
                gtk4::glib::Propagation::Stop
            } else {
                gtk4::glib::Propagation::Proceed
            }
        });
        right_view.add_controller(kc);
    }

    // ScrolledWindows + Paned
    let left_scroll = ScrolledWindow::builder()
        .hscrollbar_policy(PolicyType::Automatic)
        .min_content_width(360)
        .child(&left_view)
        .build();
    let right_scroll = ScrolledWindow::builder()
        .hscrollbar_policy(PolicyType::Automatic)
        .min_content_width(360)
        .child(&right_view)
        .build();

    // Set initial focus border classes and populate slots for the handlers above.
    left_scroll.add_css_class("pane-focused");
    right_scroll.add_css_class("pane-inactive");
    *left_scroll_slot.borrow_mut() = Some(left_scroll.clone());
    *right_scroll_slot.borrow_mut() = Some(right_scroll.clone());

    // Synchronize vertical scrolling between left and right directory panes
    {
        let syncing = Rc::new(Cell::new(false));
        let rs = right_scroll.clone();
        let s = syncing.clone();
        left_scroll.vadjustment().connect_value_changed(move |adj| {
            if !s.get() {
                s.set(true);
                rs.vadjustment().set_value(adj.value());
                s.set(false);
            }
        });
        let ls = left_scroll.clone();
        let s = syncing;
        right_scroll
            .vadjustment()
            .connect_value_changed(move |adj| {
                if !s.get() {
                    s.set(true);
                    ls.vadjustment().set_value(adj.value());
                    s.set(false);
                }
            });
    }

    // Directory path headers above each pane
    let left_header_text = match *left_label_override.borrow() {
        Some(ref l) => l.clone(),
        None => shortened_path(Path::new(&*left_dir.borrow())),
    };
    let left_header = Label::new(Some(&left_header_text));
    {
        let ld = left_dir.borrow();
        let lto = left_tooltip_override.borrow();
        let left_tip: &str = lto.as_ref().map_or(&*ld, String::as_str);
        left_header.set_tooltip_text(Some(left_tip));
    }
    left_header.set_ellipsize(gtk4::pango::EllipsizeMode::Start);
    left_header.set_hexpand(true);
    let left_copy_btn = Button::from_icon_name("edit-copy-symbolic");
    left_copy_btn.set_has_frame(false);
    left_copy_btn.set_tooltip_text(Some("Copy path"));
    {
        let ld = left_dir.clone();
        left_copy_btn.connect_clicked(move |btn| {
            btn.clipboard().set_text(&ld.borrow());
        });
    }
    let left_header_box = GtkBox::new(Orientation::Horizontal, 0);
    left_header_box.set_margin_start(6);
    left_header_box.set_margin_end(2);
    left_header_box.set_margin_top(4);
    left_header_box.set_margin_bottom(4);
    left_header_box.append(&left_header);
    left_header_box.append(&left_copy_btn);

    let right_header_text = match *right_label_override.borrow() {
        Some(ref l) => l.clone(),
        None => shortened_path(Path::new(&*right_dir.borrow())),
    };
    let right_header = Label::new(Some(&right_header_text));
    {
        let rd = right_dir.borrow();
        let rto = right_tooltip_override.borrow();
        let right_tip: &str = rto.as_ref().map_or(&*rd, String::as_str);
        right_header.set_tooltip_text(Some(right_tip));
    }
    right_header.set_ellipsize(gtk4::pango::EllipsizeMode::Start);
    right_header.set_hexpand(true);
    let right_copy_btn = Button::from_icon_name("edit-copy-symbolic");
    right_copy_btn.set_has_frame(false);
    right_copy_btn.set_tooltip_text(Some("Copy path"));
    {
        let rd = right_dir.clone();
        right_copy_btn.connect_clicked(move |btn| {
            btn.clipboard().set_text(&rd.borrow());
        });
    }
    let right_header_box = GtkBox::new(Orientation::Horizontal, 0);
    right_header_box.set_margin_start(6);
    right_header_box.set_margin_end(2);
    right_header_box.set_margin_top(4);
    right_header_box.set_margin_bottom(4);
    right_header_box.append(&right_header);
    right_header_box.append(&right_copy_btn);

    let left_pane_box = GtkBox::new(Orientation::Vertical, 0);
    left_pane_box.append(&left_header_box);
    left_scroll.set_vexpand(true);
    left_pane_box.append(&left_scroll);
    let right_pane_box = GtkBox::new(Orientation::Vertical, 0);
    right_pane_box.append(&right_header_box);
    right_scroll.set_vexpand(true);
    right_pane_box.append(&right_scroll);

    let dir_paned = Paned::new(Orientation::Horizontal);
    dir_paned.set_start_child(Some(&left_pane_box));
    dir_paned.set_end_child(Some(&right_pane_box));

    // ── Directory toolbar with copy buttons ───────────────────────
    let copy_left_btn = Button::from_icon_name("go-previous-symbolic");
    copy_left_btn.set_tooltip_text(Some("Copy to left (Alt+Left)"));
    let copy_right_btn = Button::from_icon_name("go-next-symbolic");
    copy_right_btn.set_tooltip_text(Some("Copy to right (Alt+Right)"));
    let delete_btn = Button::from_icon_name("user-trash-symbolic");
    delete_btn.set_tooltip_text(Some("Delete selected (Delete)"));

    let dir_copy_box = GtkBox::new(Orientation::Horizontal, 0);
    dir_copy_box.add_css_class("linked");
    dir_copy_box.append(&copy_left_btn);
    dir_copy_box.append(&copy_right_btn);

    let dir_toolbar = GtkBox::new(Orientation::Horizontal, 8);
    dir_toolbar.set_margin_start(6);
    dir_toolbar.set_margin_end(6);
    dir_toolbar.set_margin_top(4);
    dir_toolbar.set_margin_bottom(4);
    let dir_swap_btn = Button::from_icon_name("object-flip-horizontal-symbolic");
    dir_swap_btn.set_tooltip_text(Some("Swap panes"));

    let collapse_btn = Button::from_icon_name("format-indent-less-symbolic");
    collapse_btn.set_tooltip_text(Some("Collapse all"));
    let expand_btn = Button::from_icon_name("format-indent-more-symbolic");
    expand_btn.set_tooltip_text(Some("Expand all"));
    let tree_box = GtkBox::new(Orientation::Horizontal, 0);
    tree_box.add_css_class("linked");
    tree_box.append(&collapse_btn);
    tree_box.append(&expand_btn);

    let dir_prefs_btn = Button::from_icon_name("preferences-system-symbolic");
    dir_prefs_btn.set_tooltip_text(Some(&format!("Preferences ({}+,)", primary_key_name())));
    dir_prefs_btn.set_action_name(Some("win.prefs"));

    let apply_all_btn = Button::from_icon_name("media-skip-backward-symbolic");
    apply_all_btn.set_tooltip_text(Some("Apply all non-conflicting changes right → left"));

    dir_toolbar.append(&dir_copy_box);
    dir_toolbar.append(&delete_btn);
    dir_toolbar.append(&apply_all_btn);
    dir_toolbar.append(&dir_swap_btn);
    dir_toolbar.append(&tree_box);
    dir_toolbar.append(&dir_prefs_btn);

    // Helper: rescan directories and refresh the tree model (async)
    let scan_loading = Rc::new(Cell::new(false));
    let reload_dir = {
        let cm = children_map.clone();
        let rs = root_store.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let st = settings.clone();
        let tm = tree_model.clone();
        let sel = dir_sel.clone();
        let syncing = sel_syncing.clone();
        let lv = left_view.clone();
        let rv = right_view.clone();
        let loading = scan_loading.clone();
        move || {
            if loading.get() {
                return;
            }
            loading.set(true);

            // Suppress selection sync during rebuild
            syncing.set(true);
            let saved_pos = sel.selected();
            let saved_rel = (|| -> Option<String> {
                let row = tm.item(saved_pos)?.downcast::<TreeListRow>().ok()?;
                let obj = row.item().and_downcast::<StringObject>()?;
                Some(DirRowInfo::decode(&obj.string()).rel_path)
            })();

            // Save expanded rel_paths
            let mut expanded: Vec<String> = Vec::new();
            for i in 0..tm.n_items() {
                if let Some(row) = tm.item(i).and_then(|o| o.downcast::<TreeListRow>().ok())
                    && row.is_expanded()
                    && let Some(obj) = row.item().and_downcast::<StringObject>()
                {
                    expanded.push(DirRowInfo::decode(&obj.string()).rel_path);
                }
            }

            // Background scan
            let ld_str = ld.borrow().clone();
            let rd_str = rd.borrow().clone();
            let filters = st.borrow().dir_filters.clone();
            let hide_hidden = st.borrow().hide_hidden_files;
            let cm = cm.clone();
            let rs = rs.clone();
            let tm = tm.clone();
            let sel = sel.clone();
            let syncing = syncing.clone();
            let lv = lv.clone();
            let rv = rv.clone();
            let loading = loading.clone();
            gtk4::glib::spawn_future_local(async move {
                let (entries, _) = gio::spawn_blocking(move || {
                    scan_tree(
                        Path::new(&ld_str),
                        Path::new(&rd_str),
                        "",
                        &filters,
                        hide_hidden,
                        patch_mode,
                    )
                })
                .await
                .unwrap();
                loading.set(false);

                let mut new_map = HashMap::new();
                let new_store = build_stores(&entries, &mut new_map);

                // Skip rebuild if root-level data hasn't changed
                let root_changed = new_store.n_items() != rs.n_items()
                    || (0..new_store.n_items()).any(|i| {
                        new_store
                            .item(i)
                            .and_downcast::<StringObject>()
                            .map(|o| o.string())
                            != rs
                                .item(i)
                                .and_downcast::<StringObject>()
                                .map(|o| o.string())
                    });
                // Always update children_map so re-expanded dirs show fresh data
                *cm.borrow_mut() = new_map;
                if !root_changed {
                    syncing.set(false);
                    return;
                }
                rs.remove_all();
                for i in 0..new_store.n_items() {
                    if let Some(obj) = new_store.item(i) {
                        rs.append(&obj.downcast::<StringObject>().unwrap());
                    }
                }

                // Restore expanded state with a single forward scan.
                // Expanding a row inserts its children right after it, so a
                // forward scan naturally visits those newly-inserted children
                // and keeps overall complexity O(n) in visible rows.
                let expanded_set: HashSet<&str> =
                    expanded.iter().map(std::string::String::as_str).collect();
                let mut i = 0;
                while i < tm.n_items() {
                    if let Some(row) = tm.item(i).and_then(|o| o.downcast::<TreeListRow>().ok())
                        && let Some(obj) = row.item().and_downcast::<StringObject>()
                        && expanded_set
                            .contains(DirRowInfo::decode(&obj.string()).rel_path.as_str())
                    {
                        row.set_expanded(true);
                    }
                    i += 1;
                }

                // Restore selection on both panes
                let n = tm.n_items();
                let mut final_pos = saved_pos.min(if n > 0 { n - 1 } else { 0 });
                if let Some(ref rel) = saved_rel {
                    for i in 0..n {
                        if let Some(row) = tm.item(i).and_then(|o| o.downcast::<TreeListRow>().ok())
                            && let Some(obj) = row.item().and_downcast::<StringObject>()
                            && DirRowInfo::decode(&obj.string()).rel_path == rel.as_str()
                        {
                            final_pos = i;
                            break;
                        }
                    }
                }
                if n > 0 {
                    sel.set_selected(final_pos);
                }
                syncing.set(false);

                // Defer scroll_to until after GTK has laid out the new rows,
                // otherwise the ColumnView hasn't realized the items yet.
                let lv2 = lv.clone();
                let rv2 = rv.clone();
                gtk4::glib::idle_add_local_once(move || {
                    let flags = gtk4::ListScrollFlags::FOCUS;
                    lv2.scroll_to(final_pos, None, flags, None);
                    rv2.scroll_to(final_pos, None, flags, None);
                });
            });
        }
    };

    // Swap panes: swap left/right directories and rescan
    {
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let ll = left_label_override.clone();
        let rl = right_label_override.clone();
        let lto = left_tooltip_override.clone();
        let rto = right_tooltip_override.clone();
        let reload = reload_dir.clone();
        let lh = left_header.clone();
        let rh = right_header.clone();
        let aab = apply_all_btn.clone();
        let swapped = Rc::new(Cell::new(false));
        dir_swap_btn.connect_clicked(move |btn| {
            let tmp = ld.borrow().clone();
            (*ld.borrow_mut()).clone_from(&rd.borrow());
            *rd.borrow_mut() = tmp;
            let tmp_label = ll.borrow().clone();
            (*ll.borrow_mut()).clone_from(&rl.borrow());
            *rl.borrow_mut() = tmp_label;
            let tmp_tip = lto.borrow().clone();
            (*lto.borrow_mut()).clone_from(&rto.borrow());
            *rto.borrow_mut() = tmp_tip;
            let left_text = match *ll.borrow() {
                Some(ref l) => l.clone(),
                None => shortened_path(Path::new(&*ld.borrow())),
            };
            lh.set_text(&left_text);
            {
                let ld_ref = ld.borrow();
                let lto_ref = lto.borrow();
                let left_tip: &str = lto_ref.as_ref().map_or(&*ld_ref, String::as_str);
                lh.set_tooltip_text(Some(left_tip));
            }
            let right_text = match *rl.borrow() {
                Some(ref l) => l.clone(),
                None => shortened_path(Path::new(&*rd.borrow())),
            };
            rh.set_text(&right_text);
            {
                let rd_ref = rd.borrow();
                let rto_ref = rto.borrow();
                let right_tip: &str = rto_ref.as_ref().map_or(&*rd_ref, String::as_str);
                rh.set_tooltip_text(Some(right_tip));
            }
            // In patch mode, disable apply-all when swapped (directions get confused)
            let is_patch = lto.borrow().is_some() || rto.borrow().is_some();
            if is_patch {
                let now_swapped = !swapped.get();
                swapped.set(now_swapped);
                aab.set_sensitive(!now_swapped);
            }
            // Update window title
            if let Some(win) = find_window(btn) {
                let ln = Path::new(ld.borrow().as_str())
                    .file_name()
                    .map_or_else(|| ld.borrow().clone(), |n| n.to_string_lossy().into_owned());
                let rn = Path::new(rd.borrow().as_str())
                    .file_name()
                    .map_or_else(|| rd.borrow().clone(), |n| n.to_string_lossy().into_owned());
                win.set_title(Some(&format!("{ln}{rn}")));
            }
            reload();
        });
    }

    // Helper: get selected row's encoded data
    let get_selected_row = {
        let tm = tree_model.clone();
        let sel = dir_sel.clone();
        move || -> Option<String> {
            let pos = sel.selected();
            let item = tm.item(pos)?;
            let row = item.downcast::<TreeListRow>().ok()?;
            let obj = row.item().and_downcast::<StringObject>()?;
            Some(obj.string().to_string())
        }
    };

    // Directory action group — used by both keyboard shortcuts and toolbar buttons
    let dir_action_group = gio::SimpleActionGroup::new();
    for (name, src_dir, dst_dir, only_status) in [
        (
            "folder-copy-left",
            &right_dir,
            &left_dir,
            FileStatus::RightOnly,
        ),
        (
            "folder-copy-right",
            &left_dir,
            &right_dir,
            FileStatus::LeftOnly,
        ),
    ] {
        let action = gio::SimpleAction::new(name, None);
        let get_row = get_selected_row.clone();
        let sd = src_dir.clone();
        let dd = dst_dir.clone();
        let reload = reload_dir.clone();
        let lv = left_view.clone();
        action.connect_activate(move |_, _| {
            if let Some(raw) = get_row() {
                let info = DirRowInfo::decode(&raw);
                if info.status == only_status
                    || info.status == FileStatus::Different
                    || info.status == FileStatus::Conflict
                {
                    let rel = info.rel_path;
                    let src = Path::new(sd.borrow().as_str()).join(&rel);
                    let dst = Path::new(dd.borrow().as_str()).join(&rel);
                    let reload = reload.clone();
                    let lv2 = lv.clone();
                    let do_copy = move || {
                        if let Err(e) = copy_path_recursive(&src, &dst)
                            && let Some(win) = find_window(&lv2)
                        {
                            show_error_dialog(&win, &format!("Copy failed: {e}"));
                        }
                        reload();
                    };
                    if info.status == FileStatus::Different || info.status == FileStatus::Conflict {
                        if let Some(win) = find_window(&lv) {
                            show_confirm_dialog(
                                &win,
                                &format!("Overwrite {rel}?"),
                                "The destination file will be replaced.",
                                "Overwrite",
                                do_copy,
                            );
                        }
                    } else {
                        do_copy();
                    }
                }
            }
        });
        dir_action_group.add_action(&action);
    }
    // Apply all: copy every non-conflicting changed file from right → left
    {
        let rs = root_store.clone();
        let cm = children_map.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let reload = reload_dir.clone();
        let lv = left_view.clone();
        let base_dir_override: Option<String> = left_tooltip_override.borrow().clone();
        apply_all_btn.connect_clicked(move |_| {
            let ld_str = ld.borrow().clone();
            let rd_str = rd.borrow().clone();
            let mut actionable: Vec<(String, FileStatus)> = Vec::new();
            collect_actionable_files(&rs, &cm.borrow(), &mut actionable);
            // In non-patch mode, LeftOnly delete is not supported
            let is_patch = base_dir_override.is_some();
            if !is_patch {
                actionable.retain(|(_, s)| *s != FileStatus::LeftOnly);
            }
            if actionable.is_empty() {
                return;
            }
            let count = actionable.len();
            let reload = reload.clone();
            let lv2 = lv.clone();
            let base_dir: Option<String> = base_dir_override.clone();
            let do_apply = move || {
                // Suppress file watcher during bulk operations.
                // Mark the root dirs, then re-mark per file so the 600ms
                // window extends across the entire operation.
                mark_saving(Path::new(&ld_str));
                mark_saving(Path::new(&rd_str));
                let mut errors = Vec::new();
                for (rel, status) in &actionable {
                    let left_p = Path::new(&ld_str).join(rel);
                    let right_p = Path::new(&rd_str).join(rel);
                    mark_saving(Path::new(&ld_str));
                    mark_saving(Path::new(&rd_str));
                    let is_patch = base_dir.is_some();
                    match status {
                        FileStatus::Different => {
                            // Copy right → left (in patch mode, symlink follows to original)
                            if let Some(parent) = left_p.parent() {
                                let _ = fs::create_dir_all(parent);
                            }
                            if let Err(e) = copy_path_recursive(&right_p, &left_p) {
                                errors.push(format!("{rel}: {e}"));
                                continue;
                            }
                            // In patch mode, remove temp entries so they vanish from scan
                            if is_patch {
                                let _ = fs::remove_file(&left_p);
                                let _ = fs::remove_file(&right_p);
                            }
                        }
                        FileStatus::RightOnly => {
                            // New file — in patch mode copy to real base dir,
                            // otherwise copy to the left dir.
                            let target = if let Some(ref base) = base_dir {
                                PathBuf::from(base).join(rel)
                            } else {
                                left_p.clone()
                            };
                            if let Some(parent) = target.parent() {
                                let _ = fs::create_dir_all(parent);
                            }
                            if let Err(e) = copy_path_recursive(&right_p, &target) {
                                errors.push(format!("{rel}: {e}"));
                                continue;
                            }
                            // In patch mode, remove temp entries so they vanish from scan
                            if is_patch {
                                let _ = fs::remove_file(&left_p);
                                let _ = fs::remove_file(&right_p);
                            }
                        }
                        FileStatus::LeftOnly => {
                            if is_patch {
                                // In patch mode, left_p is a symlink; resolve to delete original
                                let real_path = fs::canonicalize(&left_p).unwrap_or(left_p.clone());
                                let _ = fs::remove_file(&real_path);
                                let _ = fs::remove_file(&left_p);
                            }
                        }
                        FileStatus::Conflict | FileStatus::Same => {}
                    }
                }
                if !errors.is_empty()
                    && let Some(win) = find_window(&lv2)
                {
                    show_error_dialog(&win, &errors.join("\n"));
                }
                reload();
            };
            if let Some(win) = find_window(&lv) {
                show_confirm_dialog(
                    &win,
                    &format!("Apply {count} file(s)?"),
                    "Non-conflicting changes will be applied (copy, create, or delete).",
                    "Apply",
                    do_apply,
                );
            }
        });
    }
    {
        let action = gio::SimpleAction::new("folder-delete", None);
        let get_row = get_selected_row.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let reload = reload_dir.clone();
        let fl = focused_left.clone();
        let lv = left_view.clone();
        action.connect_activate(move |_, _| {
            if let Some(raw) = get_row() {
                let info = DirRowInfo::decode(&raw);
                let rel = info.rel_path;
                let status = info.status;
                let lp = Path::new(ld.borrow().as_str()).join(&rel);
                let rp = Path::new(rd.borrow().as_str()).join(&rel);
                let path = match status {
                    FileStatus::LeftOnly => Some(lp),
                    FileStatus::RightOnly => Some(rp),
                    FileStatus::Different | FileStatus::Conflict | FileStatus::Same => {
                        Some(if fl.get() { lp } else { rp })
                    }
                };
                if let Some(p) = path
                    && let Some(win) = find_window(&lv)
                {
                    let reload = reload.clone();
                    show_confirm_dialog(
                        &win,
                        &format!("Move {rel} to trash?"),
                        "The file will be moved to the system trash.",
                        "Trash",
                        move || {
                            if let Err(e) = move_to_trash(&p) {
                                eprintln!("Trash failed: {e}");
                            }
                            reload();
                        },
                    );
                }
            }
        });
        dir_action_group.add_action(&action);
    }

    // Open externally: open selected file/dir in system default app
    {
        let action = gio::SimpleAction::new("folder-open-externally", None);
        let get_row = get_selected_row.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let fl = focused_left.clone();
        action.connect_activate(move |_, _| {
            if let Some(raw) = get_row() {
                let info = DirRowInfo::decode(&raw);
                let dir = if fl.get() { &ld } else { &rd };
                let path = Path::new(dir.borrow().as_str()).join(&info.rel_path);
                let abs = path.canonicalize().unwrap_or(path);
                open_externally(&abs);
            }
        });
        dir_action_group.add_action(&action);
    }

    // Collapse all: collapse every expanded row in the tree
    {
        let action = gio::SimpleAction::new("folder-collapse-all", None);
        let tm = tree_model.clone();
        action.connect_activate(move |_, _| {
            // Collapse in reverse so removing children doesn't shift
            // indices we haven't visited yet.
            let mut i = tm.n_items();
            while i > 0 {
                i -= 1;
                if let Some(item) = tm.item(i)
                    && let Ok(row) = item.downcast::<TreeListRow>()
                    && row.is_expanded()
                {
                    row.set_expanded(false);
                }
            }
        });
        dir_action_group.add_action(&action);
    }

    // Expand all: expand every directory row in the tree
    {
        let action = gio::SimpleAction::new("folder-expand-all", None);
        let tm = tree_model.clone();
        action.connect_activate(move |_, _| {
            // Expand iteratively since expanding a row may add new children
            let mut i = 0;
            while i < tm.n_items() {
                if let Some(item) = tm.item(i)
                    && let Ok(row) = item.downcast::<TreeListRow>()
                    && row.is_expandable()
                    && !row.is_expanded()
                {
                    row.set_expanded(true);
                }
                i += 1;
            }
        });
        dir_action_group.add_action(&action);
    }

    // Copy file path: copy the selected file's absolute path to clipboard
    {
        let action = gio::SimpleAction::new("folder-copy-path", None);
        let get_row = get_selected_row.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let fl = focused_left.clone();
        let lv = left_view.clone();
        action.connect_activate(move |_, _| {
            if let Some(raw) = get_row() {
                let info = DirRowInfo::decode(&raw);
                let lp = Path::new(ld.borrow().as_str()).join(&info.rel_path);
                let rp = Path::new(rd.borrow().as_str()).join(&info.rel_path);
                let path =
                    match info.status {
                        FileStatus::LeftOnly => lp,
                        FileStatus::RightOnly => rp,
                        FileStatus::Different | FileStatus::Conflict | FileStatus::Same => {
                            if fl.get() { lp } else { rp }
                        }
                    };
                let abs = path.canonicalize().unwrap_or(path);
                lv.clipboard().set_text(&abs.display().to_string());
            }
        });
        dir_action_group.add_action(&action);
    }

    // ── Dir tab: toolbar + paned ──────────────────────────────────
    let dir_tab = GtkBox::new(Orientation::Vertical, 0);
    dir_tab.append(&dir_toolbar);
    dir_tab.append(&gtk4::Separator::new(Orientation::Horizontal));
    dir_tab.append(&dir_paned);
    dir_tab.set_vexpand(true);
    dir_paned.set_vexpand(true);
    dir_tab.insert_action_group("dir", Some(&dir_action_group));

    // Wire toolbar buttons to activate the corresponding actions
    {
        let dag = dir_action_group.clone();
        copy_left_btn.connect_clicked(move |_| {
            dag.activate_action("folder-copy-left", None);
        });
    }
    {
        let dag = dir_action_group.clone();
        copy_right_btn.connect_clicked(move |_| {
            dag.activate_action("folder-copy-right", None);
        });
    }
    {
        let dag = dir_action_group.clone();
        delete_btn.connect_clicked(move |_| {
            dag.activate_action("folder-delete", None);
        });
    }
    {
        let dag = dir_action_group.clone();
        collapse_btn.connect_clicked(move |_| {
            dag.activate_action("folder-collapse-all", None);
        });
    }
    {
        let dag = dir_action_group.clone();
        expand_btn.connect_clicked(move |_| {
            dag.activate_action("folder-expand-all", None);
        });
    }

    // Capture-phase key handler for dir-only shortcuts (Alt+Left/Right, Delete)
    // so they don't fire when a file-diff tab is focused.
    {
        let key_ctl = EventControllerKey::new();
        key_ctl.set_propagation_phase(gtk4::PropagationPhase::Capture);
        let dag = dir_action_group.clone();
        key_ctl.connect_key_pressed(move |_, key, _, mods| {
            let action_name = if mods.contains(gtk4::gdk::ModifierType::ALT_MASK) {
                match key {
                    k if k == gtk4::gdk::Key::Left => Some("folder-copy-left"),
                    k if k == gtk4::gdk::Key::Right => Some("folder-copy-right"),
                    _ => None,
                }
            } else if mods.is_empty() && key == gtk4::gdk::Key::Delete {
                Some("folder-delete")
            } else {
                None
            };
            if let Some(name) = action_name {
                if let Some(action) = dag.lookup_action(name) {
                    action
                        .downcast_ref::<gio::SimpleAction>()
                        .unwrap()
                        .activate(None);
                }
                return gtk4::glib::Propagation::Stop;
            }
            gtk4::glib::Propagation::Proceed
        });
        dir_tab.add_controller(key_ctl);
    }

    // Add "folder-open-diff" action (uses the passed-in notebook and open_tabs)
    {
        let action = gio::SimpleAction::new("folder-open-diff", None);
        let get_row = get_selected_row.clone();
        let nb = notebook.clone();
        let tabs = open_tabs.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let ll = left_label_override.clone();
        let rl = right_label_override.clone();
        let st = settings.clone();
        action.connect_activate(move |_, _| {
            if let Some(raw) = get_row() {
                let info = DirRowInfo::decode(&raw);
                if !info.is_dir {
                    let labels: Vec<String> = [ll.borrow().clone(), rl.borrow().clone()]
                        .into_iter()
                        .flatten()
                        .collect();
                    open_file_diff(
                        &nb,
                        &info.rel_path,
                        &tabs,
                        &ld.borrow(),
                        &rd.borrow(),
                        &labels,
                        &st,
                    );
                }
            }
        });
        dir_action_group.add_action(&action);
    }

    // ── Dir context menu ────────────────────────────────────────────
    {
        let dir_menu = gio::Menu::new();
        let actions_section = gio::Menu::new();
        actions_section.append(Some("Open Diff"), Some("dir.folder-open-diff"));
        actions_section.append(Some("Copy to Left"), Some("dir.folder-copy-left"));
        actions_section.append(Some("Copy to Right"), Some("dir.folder-copy-right"));
        actions_section.append(Some("Delete"), Some("dir.folder-delete"));
        dir_menu.append_section(None, &actions_section);
        let tree_section = gio::Menu::new();
        tree_section.append(Some("Collapse All"), Some("dir.folder-collapse-all"));
        tree_section.append(Some("Expand All"), Some("dir.folder-expand-all"));
        dir_menu.append_section(None, &tree_section);
        let util_section = gio::Menu::new();
        util_section.append(Some("Open Externally"), Some("dir.folder-open-externally"));
        util_section.append(Some("Copy File Path"), Some("dir.folder-copy-path"));
        dir_menu.append_section(None, &util_section);

        let dir_popover_l = PopoverMenu::from_model(Some(&dir_menu));
        dir_popover_l.set_parent(&left_view);
        dir_popover_l.set_has_arrow(false);
        let dir_popover_r = PopoverMenu::from_model(Some(&dir_menu));
        dir_popover_r.set_parent(&right_view);
        dir_popover_r.set_has_arrow(false);

        let act_open = dir_action_group
            .lookup_action("folder-open-diff")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();
        let act_left = dir_action_group
            .lookup_action("folder-copy-left")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();
        let act_right = dir_action_group
            .lookup_action("folder-copy-right")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();

        let act_del = dir_action_group
            .lookup_action("folder-delete")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();
        let act_ext = dir_action_group
            .lookup_action("folder-open-externally")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();
        let act_path = dir_action_group
            .lookup_action("folder-copy-path")
            .and_downcast::<gio::SimpleAction>()
            .unwrap();

        let setup_dir_ctx = |view: &ColumnView, popover: PopoverMenu, is_left: bool| {
            let gesture = GestureClick::new();
            gesture.set_button(3);
            let get_row = get_selected_row.clone();
            let ao = act_open.clone();
            let al = act_left.clone();
            let ar = act_right.clone();
            let ad = act_del.clone();
            let ae = act_ext.clone();
            let ap = act_path.clone();
            let pop = popover;
            let sel = dir_sel.clone();
            let tm = tree_model.clone();
            let v = view.clone();
            gesture.connect_pressed(move |_, _, x, y| {
                if let Some(pos) = column_view_row_at_y(&v, x, y, tm.n_items()) {
                    sel.set_selected(pos);
                }
                if let Some(raw) = get_row() {
                    let info = DirRowInfo::decode(&raw);
                    let is_dir = info.is_dir;
                    let status = info.status;
                    let exists_on_this_side = match status {
                        FileStatus::LeftOnly => is_left,
                        FileStatus::RightOnly => !is_left,
                        _ => true,
                    };
                    let is_diff_like =
                        status == FileStatus::Different || status == FileStatus::Conflict;
                    ao.set_enabled(!is_dir && (is_diff_like || status == FileStatus::Same));
                    al.set_enabled(
                        exists_on_this_side && (status == FileStatus::RightOnly || is_diff_like),
                    );
                    ar.set_enabled(
                        exists_on_this_side && (status == FileStatus::LeftOnly || is_diff_like),
                    );
                    ad.set_enabled(exists_on_this_side);
                    ae.set_enabled(!is_dir && exists_on_this_side);
                    ap.set_enabled(exists_on_this_side);
                    pop.set_pointing_to(Some(&gtk4::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
                    pop.popup();
                }
            });
            view.add_controller(gesture);
        };
        setup_dir_ctx(&left_view, dir_popover_l, true);
        setup_dir_ctx(&right_view, dir_popover_r, false);
    }

    // Shared activate logic — used by both Enter key and double-click
    let activate_row: Rc<dyn Fn()> = {
        let nb = notebook.clone();
        let tm = tree_model.clone();
        let tabs = open_tabs.clone();
        let ld = left_dir.clone();
        let rd = right_dir.clone();
        let ll = left_label_override.clone();
        let rl = right_label_override.clone();
        let sel = dir_sel.clone();
        let st = settings.clone();
        Rc::new(move || {
            let pos = sel.selected();
            if let Some(item) = tm.item(pos) {
                let row = item.downcast::<TreeListRow>().unwrap();
                let obj = row.item().and_downcast::<StringObject>().unwrap();
                let raw = obj.string();
                let info = DirRowInfo::decode(&raw);
                if info.is_dir {
                    row.set_expanded(!row.is_expanded());
                } else {
                    let labels: Vec<String> = [ll.borrow().clone(), rl.borrow().clone()]
                        .into_iter()
                        .flatten()
                        .collect();
                    open_file_diff(
                        &nb,
                        &info.rel_path,
                        &tabs,
                        &ld.borrow(),
                        &rd.borrow(),
                        &labels,
                        &st,
                    );
                }
            }
        })
    };

    // Double-click activates via connect_activate
    {
        let ar = activate_row.clone();
        left_view.connect_activate(move |_, _| ar());
    }
    {
        let ar = activate_row.clone();
        right_view.connect_activate(move |_, _| ar());
    }

    // Capture Enter on ColumnViews so it works reliably after tab close
    {
        let ar = activate_row.clone();
        let kc = EventControllerKey::new();
        kc.set_propagation_phase(gtk4::PropagationPhase::Capture);
        kc.connect_key_pressed(move |_, key, _, _| {
            if key == gtk4::gdk::Key::Return || key == gtk4::gdk::Key::KP_Enter {
                ar();
                gtk4::glib::Propagation::Stop
            } else {
                gtk4::glib::Propagation::Proceed
            }
        });
        left_view.add_controller(kc);
    }
    {
        let ar = activate_row.clone();
        let kc = EventControllerKey::new();
        kc.set_propagation_phase(gtk4::PropagationPhase::Capture);
        kc.connect_key_pressed(move |_, key, _, _| {
            if key == gtk4::gdk::Key::Return || key == gtk4::gdk::Key::KP_Enter {
                ar();
                gtk4::glib::Propagation::Stop
            } else {
                gtk4::glib::Propagation::Proceed
            }
        });
        right_view.add_controller(kc);
    }

    // ── File watcher ───────────────────────────────────────────────
    let ld_str = left_dir.borrow().clone();
    let rd_str = right_dir.borrow().clone();
    let ld_path = PathBuf::from(&ld_str);
    let rd_path = PathBuf::from(&rd_str);
    let tabs_reload = open_tabs.clone();
    let ld_reload = left_dir.clone();
    let rd_reload = right_dir.clone();
    let reload = reload_dir.clone();
    let st = settings.clone();
    let mut dirty = false;
    let mut retry_count: u32 = 0;
    let mut prev_hide_hidden = st.borrow().hide_hidden_files;
    let mut prev_filters = st.borrow().dir_filters.clone();
    let dir_watcher = start_file_watcher(
        &[ld_path.as_path(), rd_path.as_path()],
        true,
        None,
        move |fs_dirty| {
            if fs_dirty {
                dirty = true;
                retry_count = 0;
            }
            // Also rescan when filter settings change (e.g. via live preferences)
            {
                let s = st.borrow();
                if s.hide_hidden_files != prev_hide_hidden || s.dir_filters != prev_filters {
                    prev_hide_hidden = s.hide_hidden_files;
                    prev_filters = s.dir_filters.clone();
                    dirty = true;
                }
            }
            if dirty
                && !is_saving_under(&[
                    Path::new(&*ld_reload.borrow()),
                    Path::new(&*rd_reload.borrow()),
                ])
            {
                dirty = false;
                reload();
                let mut any_tab_failed = false;
                for tab in tabs_reload.borrow().iter() {
                    if !reload_file_tab(tab) {
                        any_tab_failed = true;
                    }
                }
                if any_tab_failed {
                    retry_count += 1;
                    if retry_count < 5 {
                        dirty = true;
                    } else {
                        eprintln!(
                            "Giving up tab reload after {retry_count} retries \
                             (file unreadable or binary)"
                        );
                    }
                }
            }
        },
    );

    // Build title — prefer label overrides (e.g. patch mode) over raw dir names
    let left_name = left_label_override.borrow().clone().unwrap_or_else(|| {
        Path::new(left_dir.borrow().as_str())
            .file_name()
            .map_or_else(
                || left_dir.borrow().clone(),
                |n| n.to_string_lossy().into_owned(),
            )
    });
    let right_name = right_label_override.borrow().clone().unwrap_or_else(|| {
        Path::new(right_dir.borrow().as_str())
            .file_name()
            .map_or_else(
                || right_dir.borrow().clone(),
                |n| n.to_string_lossy().into_owned(),
            )
    });
    let title = format!("{left_name} \u{2014} {right_name}");

    (dir_tab, dir_watcher, left_view, title)
}

/// Open a directory comparison as a tab in the given notebook.
pub(super) fn open_dir_comparison_tab(
    notebook: &Notebook,
    left_dir: PathBuf,
    right_dir: PathBuf,
    open_tabs: &Rc<RefCell<Vec<FileTab>>>,
    settings: &Rc<RefCell<Settings>>,
) {
    let (dir_widget, dir_watcher, left_view, dir_title) = build_dir_tab(
        left_dir,
        right_dir,
        &[],
        &[],
        Rc::clone(settings),
        notebook,
        open_tabs,
    );

    let (tab_label_box, close_btn) = make_closeable_tab_label(&dir_title);
    let page_num = notebook.append_page(&dir_widget, Some(&tab_label_box));
    notebook.set_current_page(Some(page_num));
    left_view.grab_focus();

    // Stop watcher when tab is removed
    {
        let dw = dir_widget.downgrade();
        let wa = dir_watcher.alive.clone();
        let handler_id: Rc<RefCell<Option<gtk4::glib::SignalHandlerId>>> =
            Rc::new(RefCell::new(None));
        let hid = handler_id.clone();
        let id = notebook.connect_page_removed(move |nb, child, _| {
            if let Some(w) = dw.upgrade()
                && *child == w
            {
                wa.set(false);
                if let Some(id) = hid.borrow_mut().take() {
                    nb.disconnect(id);
                }
            }
        });
        handler_id.borrow_mut().replace(id);
    }
    {
        let nb = notebook.clone();
        let dw = dir_widget;
        close_btn.connect_clicked(move |_| {
            if let Some(n) = nb.page_num(&dw) {
                nb.remove_page(Some(n));
            }
        });
    }
}

// ─── Directory comparison window ───────────────────────────────────────────

pub(super) fn build_dir_window(
    app: &Application,
    left_dir: std::path::PathBuf,
    right_dir: std::path::PathBuf,
    labels: &[String],
    settings: Rc<RefCell<Settings>>,
) {
    build_dir_window_with_tooltips(app, left_dir, right_dir, labels, &[], None, settings);
}

pub(super) fn build_dir_window_with_tooltips(
    app: &Application,
    left_dir: std::path::PathBuf,
    right_dir: std::path::PathBuf,
    labels: &[String],
    tooltip_dirs: &[String],
    on_destroy: Option<Box<dyn Fn() + 'static>>,
    settings: Rc<RefCell<Settings>>,
) {
    let AppWindow {
        window,
        notebook,
        open_tabs,
    } = build_app_window(app, &settings, 900, 600, true);

    let (dir_tab, dir_watcher, left_view, title) = build_dir_tab(
        left_dir,
        right_dir,
        labels,
        tooltip_dirs,
        settings,
        &notebook,
        &open_tabs,
    );
    notebook.append_page(&dir_tab, Some(&Label::new(Some(&title))));

    window.connect_destroy(move |_| {
        dir_watcher.alive.set(false);
    });

    // If a cleanup callback is provided (e.g. patch mode temp dir), run it on destroy
    if let Some(on_destroy) = on_destroy {
        window.connect_destroy(move |_| on_destroy());
    }

    window.present();
    left_view.grab_focus();
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Row encoding/decoding ─────────────────────────────────────

    #[test]
    fn encode_decode_roundtrip() {
        let original = DirRowInfo {
            status: FileStatus::Different,
            name: "test.txt".to_string(),
            is_dir: false,
            rel_path: "subdir/test.txt".to_string(),
            left_size: Some(1024),
            left_mtime: None,
            right_size: Some(2048),
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.status, FileStatus::Different);
        assert_eq!(decoded.name, "test.txt");
        assert!(!decoded.is_dir);
        assert_eq!(decoded.rel_path, "subdir/test.txt");
    }

    #[test]
    fn encode_decode_directory() {
        let original = DirRowInfo {
            status: FileStatus::Same,
            name: "mydir".to_string(),
            is_dir: true,
            rel_path: "parent/mydir".to_string(),
            left_size: None,
            left_mtime: None,
            right_size: None,
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.status, FileStatus::Same);
        assert_eq!(decoded.name, "mydir");
        assert!(decoded.is_dir);
        assert_eq!(decoded.rel_path, "parent/mydir");
    }

    #[test]
    fn encode_decode_left_only() {
        let original = DirRowInfo {
            status: FileStatus::LeftOnly,
            name: "orphan.rs".to_string(),
            is_dir: false,
            rel_path: "orphan.rs".to_string(),
            left_size: Some(512),
            left_mtime: None,
            right_size: None,
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.status, FileStatus::LeftOnly);
        assert_eq!(decoded.name, "orphan.rs");
    }

    #[test]
    fn encode_decode_right_only() {
        let original = DirRowInfo {
            status: FileStatus::RightOnly,
            name: "new_file.rs".to_string(),
            is_dir: false,
            rel_path: "new_file.rs".to_string(),
            left_size: None,
            left_mtime: None,
            right_size: Some(256),
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.status, FileStatus::RightOnly);
        assert_eq!(decoded.name, "new_file.rs");
    }

    #[test]
    fn encode_decode_conflict() {
        let original = DirRowInfo {
            status: FileStatus::Conflict,
            name: "broken.rs".to_string(),
            is_dir: false,
            rel_path: "src/broken.rs".to_string(),
            left_size: Some(100),
            left_mtime: None,
            right_size: Some(200),
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.status, FileStatus::Conflict);
        assert_eq!(decoded.name, "broken.rs");
        assert_eq!(decoded.rel_path, "src/broken.rs");
    }

    #[test]
    fn decode_field_out_of_bounds() {
        // Should return empty string for missing fields via helper
        assert_eq!(DirRowInfo::get_field_text("a\x1fb", 5), "");
        assert_eq!(DirRowInfo::get_field_text("", 0), "");
    }

    #[test]
    fn decode_name_with_special_chars() {
        let original = DirRowInfo {
            status: FileStatus::Same,
            name: "file with spaces.txt".to_string(),
            is_dir: false,
            rel_path: "path/file with spaces.txt".to_string(),
            left_size: None,
            left_mtime: None,
            right_size: None,
            right_mtime: None,
        };
        let encoded = original.encode();
        let decoded = DirRowInfo::decode(&encoded);

        assert_eq!(decoded.name, "file with spaces.txt");
        assert_eq!(decoded.rel_path, "path/file with spaces.txt");
    }

    // ── collect_actionable_files logic ──────────────────────────
    //
    // `collect_actionable_files` walks a GTK ListStore tree, which requires
    // GTK main-thread init and can't be unit-tested directly. These tests
    // verify the filtering predicate it uses (lines 111-116) and the
    // non-patch LeftOnly exclusion (line 1174).

    #[test]
    fn actionable_status_filter() {
        // Mirrors the match arms in collect_actionable_files
        let statuses = [
            (FileStatus::Different, true),
            (FileStatus::RightOnly, true),
            (FileStatus::LeftOnly, true),
            (FileStatus::Conflict, false),
            (FileStatus::Same, false),
        ];
        for (status, expected) in &statuses {
            let is_actionable = matches!(
                status,
                FileStatus::Different | FileStatus::RightOnly | FileStatus::LeftOnly
            );
            assert_eq!(
                is_actionable, *expected,
                "FileStatus::{status:?} should be actionable={expected}"
            );
        }
    }

    #[test]
    fn actionable_mixed_filter() {
        let entries = [
            ("a.rs", FileStatus::Different),
            ("b.rs", FileStatus::Same),
            ("c.rs", FileStatus::Conflict),
            ("d.rs", FileStatus::RightOnly),
            ("e.rs", FileStatus::LeftOnly),
        ];
        let result: Vec<&str> = entries
            .iter()
            .filter(|(_, s)| {
                matches!(
                    s,
                    FileStatus::Different | FileStatus::RightOnly | FileStatus::LeftOnly
                )
            })
            .map(|(name, _)| *name)
            .collect();
        assert_eq!(result, vec!["a.rs", "d.rs", "e.rs"]);
    }

    #[test]
    fn actionable_non_patch_excludes_left_only() {
        // In non-patch mode, LeftOnly is filtered out after collection (line 1174)
        let mut actionable = vec![
            ("a.rs".to_string(), FileStatus::Different),
            ("b.rs".to_string(), FileStatus::LeftOnly),
            ("c.rs".to_string(), FileStatus::RightOnly),
            ("d.rs".to_string(), FileStatus::LeftOnly),
        ];
        // Simulate the retain at line 1174
        actionable.retain(|(_, s)| *s != FileStatus::LeftOnly);
        let paths: Vec<&str> = actionable.iter().map(|(p, _)| p.as_str()).collect();
        assert_eq!(paths, vec!["a.rs", "c.rs"]);
    }

    // ── FileStatus ────────────────────────────────────────────────

    #[test]
    fn file_status_codes() {
        assert_eq!(FileStatus::Same.code(), "S");
        assert_eq!(FileStatus::Different.code(), "D");
        assert_eq!(FileStatus::Conflict.code(), "C");
        assert_eq!(FileStatus::LeftOnly.code(), "L");
        assert_eq!(FileStatus::RightOnly.code(), "R");
    }
}