egui_file_dialog/
file_dialog.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
use crate::config::{
    FileDialogConfig, FileDialogKeyBindings, FileDialogLabels, FileDialogStorage, FileFilter,
    Filter, OpeningMode, QuickAccess,
};
use crate::create_directory_dialog::CreateDirectoryDialog;
use crate::data::{
    DirectoryContent, DirectoryContentState, DirectoryEntry, Disk, Disks, UserDirectories,
};
use crate::modals::{FileDialogModal, ModalAction, ModalState, OverwriteFileModal};
use crate::{FileSystem, NativeFileSystem};
use egui::text::{CCursor, CCursorRange};
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Represents the mode the file dialog is currently in.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DialogMode {
    /// When the dialog is currently used to select a single file.
    PickFile,

    /// When the dialog is currently used to select a single directory.
    PickDirectory,

    /// When the dialog is currently used to select multiple files and directories.
    PickMultiple,

    /// When the dialog is currently used to save a file.
    SaveFile,
}

/// Represents the state the file dialog is currently in.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum DialogState {
    /// The dialog is currently open and the user can perform the desired actions.
    Open,

    /// The dialog is currently closed and not visible.
    Closed,

    /// The user has selected a folder or file or specified a destination path for saving a file.
    Picked(PathBuf),

    /// The user has finished selecting multiple files and folders.
    PickedMultiple(Vec<PathBuf>),

    /// The user cancelled the dialog and didn't select anything.
    Cancelled,
}

/// Represents a file dialog instance.
///
/// The `FileDialog` instance can be used multiple times and for different actions.
///
/// # Examples
///
/// ```
/// use egui_file_dialog::FileDialog;
///
/// struct MyApp {
///     file_dialog: FileDialog,
/// }
///
/// impl MyApp {
///     fn update(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {
///         if ui.button("Pick a file").clicked() {
///             self.file_dialog.pick_file();
///         }
///
///         if let Some(path) = self.file_dialog.update(ctx).picked() {
///             println!("Picked file: {:?}", path);
///         }
///     }
/// }
/// ```
#[derive(Debug)]
pub struct FileDialog {
    /// The configuration of the file dialog
    config: FileDialogConfig,

    /// Stack of modal windows to be displayed.
    /// The top element is what is currently being rendered.
    modals: Vec<Box<dyn FileDialogModal + Send + Sync>>,

    /// The mode the dialog is currently in
    mode: DialogMode,
    /// The state the dialog is currently in
    state: DialogState,
    /// If files are displayed in addition to directories.
    /// This option will be ignored when mode == `DialogMode::SelectFile`.
    show_files: bool,
    /// This is an optional ID that can be set when opening the dialog to determine which
    /// operation the dialog is used for. This is useful if the dialog is used multiple times
    /// for different actions in the same view. The ID then makes it possible to distinguish
    /// for which action the user has selected an item.
    /// This ID is not used internally.
    operation_id: Option<String>,

    /// The currently used window ID.
    window_id: egui::Id,

    /// The user directories like Home or Documents.
    /// These are loaded once when the dialog is created or when the `refresh()` method is called.
    user_directories: Option<UserDirectories>,
    /// The currently mounted system disks.
    /// These are loaded once when the dialog is created or when the `refresh()` method is called.
    system_disks: Disks,

    /// Contains the directories that the user opened. Every newly opened directory
    /// is pushed to the vector.
    /// Used for the navigation buttons to load the previous or next directory.
    directory_stack: Vec<PathBuf>,
    /// An offset from the back of `directory_stack` telling which directory is currently open.
    /// If 0, the user is currently in the latest open directory.
    /// If not 0, the user has used the "Previous directory" button and has
    /// opened previously opened directories.
    directory_offset: usize,
    /// The content of the currently open directory
    directory_content: DirectoryContent,

    /// The dialog that is shown when the user wants to create a new directory.
    create_directory_dialog: CreateDirectoryDialog,

    /// Whether the text edit is open for editing the current path.
    path_edit_visible: bool,
    /// Buffer holding the text when the user edits the current path.
    path_edit_value: String,
    /// If the path edit should be initialized. Unlike `path_edit_request_focus`,
    /// this also sets the cursor to the end of the text input field.
    path_edit_activate: bool,
    /// If the text edit of the path should request focus in the next frame.
    path_edit_request_focus: bool,

    /// The item that the user currently selected.
    /// Can be a directory or a folder.
    selected_item: Option<DirectoryEntry>,
    /// Buffer for the input of the file name when the dialog is in `SaveFile` mode.
    file_name_input: String,
    /// This variables contains the error message if the `file_name_input` is invalid.
    /// This can be the case, for example, if a file or folder with the name already exists.
    file_name_input_error: Option<String>,
    /// If the file name input text field should request focus in the next frame.
    file_name_input_request_focus: bool,
    /// The file filter the user selected
    selected_file_filter: Option<egui::Id>,

    /// If we should scroll to the item selected by the user in the next frame.
    scroll_to_selection: bool,
    /// Buffer containing the value of the search input.
    search_value: String,
    /// If the search should be initialized in the next frame.
    init_search: bool,

    /// If any widget was focused in the last frame.
    /// This is used to prevent the dialog from closing when pressing the escape key
    /// inside a text input.
    any_focused_last_frame: bool,
}

/// This tests if file dialog is send and sync.
#[cfg(test)]
const fn test_prop<T: Send + Sync>() {}

#[test]
const fn test() {
    test_prop::<FileDialog>();
}

impl Default for FileDialog {
    /// Creates a new file dialog instance with default values.
    fn default() -> Self {
        Self::new()
    }
}

impl Debug for dyn FileDialogModal + Send + Sync {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "<FileDialogModal>")
    }
}

/// Callback type to inject a custom egui ui inside the file dialog's ui.
///
/// Also gives access to the file dialog, since it would otherwise be inaccessible
/// inside the closure.
type FileDialogUiCallback<'a> = dyn FnMut(&mut egui::Ui, &mut FileDialog) + 'a;

impl FileDialog {
    // ------------------------------------------------------------------------
    // Creation:

    /// Creates a new file dialog instance with default values.
    #[must_use]
    pub fn new() -> Self {
        let file_system = Arc::new(NativeFileSystem);
        Self {
            modals: Vec::new(),

            mode: DialogMode::PickDirectory,
            state: DialogState::Closed,
            show_files: true,
            operation_id: None,

            window_id: egui::Id::new("file_dialog"),

            user_directories: None,
            system_disks: Disks::new_empty(),

            directory_stack: Vec::new(),
            directory_offset: 0,
            directory_content: DirectoryContent::default(),

            create_directory_dialog: CreateDirectoryDialog::from_filesystem(file_system.clone()),

            path_edit_visible: false,
            path_edit_value: String::new(),
            path_edit_activate: false,
            path_edit_request_focus: false,

            selected_item: None,
            file_name_input: String::new(),
            file_name_input_error: None,
            file_name_input_request_focus: true,
            selected_file_filter: None,

            scroll_to_selection: false,
            search_value: String::new(),
            init_search: false,

            any_focused_last_frame: false,

            config: FileDialogConfig::default_from_filesystem(file_system),
        }
    }

    /// Creates a new file dialog object and initializes it with the specified configuration.
    pub fn with_config(config: FileDialogConfig) -> Self {
        let mut obj = Self::new();
        *obj.config_mut() = config;
        obj
    }

    /// Uses the given file system instead of the native file system.
    #[must_use]
    pub fn with_file_system(file_system: Arc<dyn FileSystem + Send + Sync>) -> Self {
        let mut obj = Self::new();
        obj.config.initial_directory = file_system.current_dir().unwrap_or_default();
        obj.config.file_system = file_system;
        obj
    }

    // -------------------------------------------------
    // Open, Update:

    /// Opens the file dialog in the given mode with the given options.
    /// This function resets the file dialog and takes care for the variables that need to be
    /// set when opening the file dialog.
    ///
    /// Returns the result of the operation to load the initial directory.
    ///
    /// If you don't need to set the individual parameters, you can also use the shortcut
    /// methods `select_directory`, `select_file` and `save_file`.
    ///
    /// # Arguments
    ///
    /// * `mode` - The mode in which the dialog should be opened
    /// * `show_files` - If files should also be displayed to the user in addition to directories.
    ///    This is ignored if the mode is `DialogMode::SelectFile`.
    /// * `operation_id` - Sets an ID for which operation the dialog was opened.
    ///    This is useful when the dialog can be used for various operations in a single view.
    ///    The ID can then be used to check which action the user selected an item for.
    ///
    /// # Examples
    ///
    /// The following example shows how the dialog can be used for multiple
    /// actions using the `operation_id`.
    ///
    /// ```
    /// use std::path::PathBuf;
    ///
    /// use egui_file_dialog::{DialogMode, FileDialog};
    ///
    /// struct MyApp {
    ///     file_dialog: FileDialog,
    ///
    ///     picked_file_a: Option<PathBuf>,
    ///     picked_file_b: Option<PathBuf>,
    /// }
    ///
    /// impl MyApp {
    ///     fn update(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {
    ///         if ui.button("Pick file a").clicked() {
    ///             let _ = self.file_dialog.open(DialogMode::PickFile, true, Some("pick_a"));
    ///         }
    ///
    ///         if ui.button("Pick file b").clicked() {
    ///             let _ = self.file_dialog.open(DialogMode::PickFile, true, Some("pick_b"));
    ///         }
    ///
    ///         self.file_dialog.update(ctx);
    ///
    ///         if let Some(path) = self.file_dialog.picked() {
    ///             if self.file_dialog.operation_id() == Some("pick_a") {
    ///                 self.picked_file_a = Some(path.to_path_buf());
    ///             }
    ///             if self.file_dialog.operation_id() == Some("pick_b") {
    ///                 self.picked_file_b = Some(path.to_path_buf());
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    pub fn open(&mut self, mode: DialogMode, mut show_files: bool, operation_id: Option<&str>) {
        self.reset();
        self.refresh();

        if mode == DialogMode::PickFile {
            show_files = true;
        }

        if mode == DialogMode::SaveFile {
            self.file_name_input
                .clone_from(&self.config.default_file_name);
        }

        // Select the default file filter
        if let Some(name) = &self.config.default_file_filter {
            for filter in &self.config.file_filters {
                if filter.name == name.as_str() {
                    self.selected_file_filter = Some(filter.id);
                }
            }
        }

        self.mode = mode;
        self.state = DialogState::Open;
        self.show_files = show_files;
        self.operation_id = operation_id.map(String::from);

        self.window_id = self
            .config
            .id
            .map_or_else(|| egui::Id::new(self.get_window_title()), |id| id);

        self.load_directory(&self.get_initial_directory());
    }

    /// Shortcut function to open the file dialog to prompt the user to pick a directory.
    /// If used, no files in the directories will be shown to the user.
    /// Use the `open()` method instead, if you still want to display files to the user.
    /// This function resets the file dialog. Configuration variables such as
    /// `initial_directory` are retained.
    ///
    /// The function ignores the result of the initial directory loading operation.
    pub fn pick_directory(&mut self) {
        self.open(DialogMode::PickDirectory, false, None);
    }

    /// Shortcut function to open the file dialog to prompt the user to pick a file.
    /// This function resets the file dialog. Configuration variables such as
    /// `initial_directory` are retained.
    ///
    /// The function ignores the result of the initial directory loading operation.
    pub fn pick_file(&mut self) {
        self.open(DialogMode::PickFile, true, None);
    }

    /// Shortcut function to open the file dialog to prompt the user to pick multiple
    /// files and folders.
    /// This function resets the file dialog. Configuration variables such as `initial_directory`
    /// are retained.
    ///
    /// The function ignores the result of the initial directory loading operation.
    pub fn pick_multiple(&mut self) {
        self.open(DialogMode::PickMultiple, true, None);
    }

    /// Shortcut function to open the file dialog to prompt the user to save a file.
    /// This function resets the file dialog. Configuration variables such as
    /// `initial_directory` are retained.
    ///
    /// The function ignores the result of the initial directory loading operation.
    pub fn save_file(&mut self) {
        self.open(DialogMode::SaveFile, true, None);
    }

    /// The main update method that should be called every frame if the dialog is to be visible.
    ///
    /// This function has no effect if the dialog state is currently not `DialogState::Open`.
    pub fn update(&mut self, ctx: &egui::Context) -> &Self {
        if self.state != DialogState::Open {
            return self;
        }

        self.update_keybindings(ctx);
        self.update_ui(ctx, None);

        self
    }

    /// Sets the width of the right panel.
    pub fn set_right_panel_width(&mut self, width: f32) {
        self.config.right_panel_width = Some(width);
    }

    /// Clears the width of the right panel by setting it to None.
    pub fn clear_right_panel_width(&mut self) {
        self.config.right_panel_width = None;
    }

    /// Do an [update](`Self::update`) with a custom right panel ui.
    ///
    /// Example use cases:
    /// - Show custom information for a file (size, MIME type, etc.)
    /// - Embed a preview, like a thumbnail for an image
    /// - Add controls for custom open options, like open as read-only, etc.
    ///
    /// See [`active_entry`](Self::active_entry) to get the active directory entry
    /// to show the information for.
    ///
    /// This function has no effect if the dialog state is currently not `DialogState::Open`.
    pub fn update_with_right_panel_ui(
        &mut self,
        ctx: &egui::Context,
        f: &mut FileDialogUiCallback,
    ) -> &Self {
        if self.state != DialogState::Open {
            return self;
        }

        self.update_keybindings(ctx);
        self.update_ui(ctx, Some(f));

        self
    }

    // -------------------------------------------------
    // Setter:

    /// Mutably borrow internal `config`.
    pub fn config_mut(&mut self) -> &mut FileDialogConfig {
        &mut self.config
    }

    /// Sets the storage used by the file dialog.
    /// Storage includes all data that is persistently stored between multiple
    /// file dialog instances.
    pub fn storage(mut self, storage: FileDialogStorage) -> Self {
        self.config.storage = storage;
        self
    }

    /// Mutably borrow internal storage.
    pub fn storage_mut(&mut self) -> &mut FileDialogStorage {
        &mut self.config.storage
    }

    /// Sets the keybindings used by the file dialog.
    pub fn keybindings(mut self, keybindings: FileDialogKeyBindings) -> Self {
        self.config.keybindings = keybindings;
        self
    }

    /// Sets the labels the file dialog uses.
    ///
    /// Used to enable multiple language support.
    ///
    /// See `FileDialogLabels` for more information.
    pub fn labels(mut self, labels: FileDialogLabels) -> Self {
        self.config.labels = labels;
        self
    }

    /// Mutably borrow internal `config.labels`.
    pub fn labels_mut(&mut self) -> &mut FileDialogLabels {
        &mut self.config.labels
    }

    /// Sets which directory is loaded when opening the file dialog.
    pub const fn opening_mode(mut self, opening_mode: OpeningMode) -> Self {
        self.config.opening_mode = opening_mode;
        self
    }

    /// If the file dialog window should be displayed as a modal.
    ///
    /// If the window is displayed as modal, the area outside the dialog can no longer be
    /// interacted with and an overlay is displayed.
    pub const fn as_modal(mut self, as_modal: bool) -> Self {
        self.config.as_modal = as_modal;
        self
    }

    /// Sets the color of the overlay when the dialog is displayed as a modal window.
    pub const fn modal_overlay_color(mut self, modal_overlay_color: egui::Color32) -> Self {
        self.config.modal_overlay_color = modal_overlay_color;
        self
    }

    /// Sets the first loaded directory when the dialog opens.
    /// If the path is a file, the file's parent directory is used. If the path then has no
    /// parent directory or cannot be loaded, the user will receive an error.
    /// However, the user directories and system disk allow the user to still select a file in
    /// the event of an error.
    ///
    /// Since `fs::canonicalize` is used, both absolute paths and relative paths are allowed.
    /// See `FileDialog::canonicalize_paths` for more information.
    pub fn initial_directory(mut self, directory: PathBuf) -> Self {
        self.config.initial_directory = directory;
        self
    }

    /// Sets the default file name when opening the dialog in `DialogMode::SaveFile` mode.
    pub fn default_file_name(mut self, name: &str) -> Self {
        self.config.default_file_name = name.to_string();
        self
    }

    /// Sets if the user is allowed to select an already existing file when the dialog is in
    /// `DialogMode::SaveFile` mode.
    ///
    /// If this is enabled, the user will receive a modal asking whether the user really
    /// wants to overwrite an existing file.
    pub const fn allow_file_overwrite(mut self, allow_file_overwrite: bool) -> Self {
        self.config.allow_file_overwrite = allow_file_overwrite;
        self
    }

    /// Sets if the path edit is allowed to select the path as the file to save
    /// if it does not have an extension.
    ///
    /// This can lead to confusion if the user wants to open a directory with the path edit,
    /// types it incorrectly and the dialog tries to select the incorrectly typed folder as
    /// the file to be saved.
    ///
    /// This only affects the `DialogMode::SaveFile` mode.
    pub const fn allow_path_edit_to_save_file_without_extension(mut self, allow: bool) -> Self {
        self.config.allow_path_edit_to_save_file_without_extension = allow;
        self
    }

    /// Sets the separator of the directories when displaying a path.
    /// Currently only used when the current path is displayed in the top panel.
    pub fn directory_separator(mut self, separator: &str) -> Self {
        self.config.directory_separator = separator.to_string();
        self
    }

    /// Sets if the paths in the file dialog should be canonicalized before use.
    ///
    /// By default, all paths are canonicalized. This has the advantage that the paths are
    /// all brought to a standard and are therefore compatible with each other.
    ///
    /// On Windows, however, this results in the namespace prefix `\\?\` being set in
    /// front of the path, which may not be compatible with other applications.
    /// In addition, canonicalizing converts all relative paths to absolute ones.
    ///
    /// See: [Rust docs](https://doc.rust-lang.org/std/fs/fn.canonicalize.html)
    /// for more information.
    ///
    /// In general, it is only recommended to disable canonicalization if
    /// you know what you are doing and have a reason for it.
    /// Disabling canonicalization can lead to unexpected behavior, for example if an
    /// already canonicalized path is then set as the initial directory.
    pub const fn canonicalize_paths(mut self, canonicalize: bool) -> Self {
        self.config.canonicalize_paths = canonicalize;
        self
    }

    /// If the directory content should be loaded via a separate thread.
    /// This prevents the application from blocking when loading large directories
    /// or from slow hard drives.
    pub const fn load_via_thread(mut self, load_via_thread: bool) -> Self {
        self.config.load_via_thread = load_via_thread;
        self
    }

    /// Sets if long filenames should be truncated in the middle.
    /// The extension, if available, will be preserved.
    ///
    /// Warning! If this is disabled, the scroll-to-selection might not work correctly and have
    /// an offset for large directories.
    pub const fn truncate_filenames(mut self, truncate_filenames: bool) -> Self {
        self.config.truncate_filenames = truncate_filenames;
        self
    }

    /// Sets the icon that is used to display errors.
    pub fn err_icon(mut self, icon: &str) -> Self {
        self.config.err_icon = icon.to_string();
        self
    }

    /// Sets the default icon that is used to display files.
    pub fn default_file_icon(mut self, icon: &str) -> Self {
        self.config.default_file_icon = icon.to_string();
        self
    }

    /// Sets the default icon that is used to display folders.
    pub fn default_folder_icon(mut self, icon: &str) -> Self {
        self.config.default_folder_icon = icon.to_string();
        self
    }

    /// Sets the icon that is used to display devices in the left panel.
    pub fn device_icon(mut self, icon: &str) -> Self {
        self.config.device_icon = icon.to_string();
        self
    }

    /// Sets the icon that is used to display removable devices in the left panel.
    pub fn removable_device_icon(mut self, icon: &str) -> Self {
        self.config.removable_device_icon = icon.to_string();
        self
    }

    /// Adds a new file filter the user can select from a dropdown widget.
    ///
    /// NOTE: The name must be unique. If a filter with the same name already exists,
    ///       it will be overwritten.
    ///
    /// # Arguments
    ///
    /// * `name` - Display name of the filter
    /// * `filter` - Sets a filter function that checks whether a given
    ///   Path matches the criteria for this filter.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use egui_file_dialog::FileDialog;
    ///
    /// FileDialog::new()
    ///     .add_file_filter(
    ///         "PNG files",
    ///         Arc::new(|path| path.extension().unwrap_or_default() == "png"))
    ///     .add_file_filter(
    ///         "JPG files",
    ///         Arc::new(|path| path.extension().unwrap_or_default() == "jpg"));
    /// ```
    pub fn add_file_filter(mut self, name: &str, filter: Filter<Path>) -> Self {
        self.config = self.config.add_file_filter(name, filter);
        self
    }

    /// Name of the file filter to be selected by default.
    ///
    /// No file filter is selected if there is no file filter with that name.
    pub fn default_file_filter(mut self, name: &str) -> Self {
        self.config.default_file_filter = Some(name.to_string());
        self
    }

    /// Sets a new icon for specific files or folders.
    ///
    /// # Arguments
    ///
    /// * `icon` - The icon that should be used.
    /// * `filter` - Sets a filter function that checks whether a given
    ///   Path matches the criteria for this icon.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use egui_file_dialog::FileDialog;
    ///
    /// FileDialog::new()
    ///     // .png files should use the "document with picture (U+1F5BB)" icon.
    ///     .set_file_icon("🖻", Arc::new(|path| path.extension().unwrap_or_default() == "png"))
    ///     // .git directories should use the "web-github (U+E624)" icon.
    ///     .set_file_icon("", Arc::new(|path| path.file_name().unwrap_or_default() == ".git"));
    /// ```
    pub fn set_file_icon(mut self, icon: &str, filter: Filter<std::path::Path>) -> Self {
        self.config = self.config.set_file_icon(icon, filter);
        self
    }

    /// Adds a new custom quick access section to the left panel.
    ///
    /// # Examples
    ///
    /// ```
    /// use egui_file_dialog::FileDialog;
    ///
    /// FileDialog::new()
    ///     .add_quick_access("My App", |s| {
    ///         s.add_path("Config", "/app/config");
    ///         s.add_path("Themes", "/app/themes");
    ///         s.add_path("Languages", "/app/languages");
    ///     });
    /// ```
    // pub fn add_quick_access(mut self, heading: &str, builder: &fn(&mut QuickAccess)) -> Self {
    pub fn add_quick_access(
        mut self,
        heading: &str,
        builder: impl FnOnce(&mut QuickAccess),
    ) -> Self {
        self.config = self.config.add_quick_access(heading, builder);
        self
    }

    /// Overwrites the window title.
    ///
    /// By default, the title is set dynamically, based on the `DialogMode`
    /// the dialog is currently in.
    pub fn title(mut self, title: &str) -> Self {
        self.config.title = Some(title.to_string());
        self
    }

    /// Sets the ID of the window.
    pub fn id(mut self, id: impl Into<egui::Id>) -> Self {
        self.config.id = Some(id.into());
        self
    }

    /// Sets the default position of the window.
    pub fn default_pos(mut self, default_pos: impl Into<egui::Pos2>) -> Self {
        self.config.default_pos = Some(default_pos.into());
        self
    }

    /// Sets the window position and prevents it from being dragged around.
    pub fn fixed_pos(mut self, pos: impl Into<egui::Pos2>) -> Self {
        self.config.fixed_pos = Some(pos.into());
        self
    }

    /// Sets the default size of the window.
    pub fn default_size(mut self, size: impl Into<egui::Vec2>) -> Self {
        self.config.default_size = size.into();
        self
    }

    /// Sets the maximum size of the window.
    pub fn max_size(mut self, max_size: impl Into<egui::Vec2>) -> Self {
        self.config.max_size = Some(max_size.into());
        self
    }

    /// Sets the minimum size of the window.
    ///
    /// Specifying a smaller minimum size than the default can lead to unexpected behavior.
    pub fn min_size(mut self, min_size: impl Into<egui::Vec2>) -> Self {
        self.config.min_size = min_size.into();
        self
    }

    /// Sets the anchor of the window.
    pub fn anchor(mut self, align: egui::Align2, offset: impl Into<egui::Vec2>) -> Self {
        self.config.anchor = Some((align, offset.into()));
        self
    }

    /// Sets if the window is resizable.
    pub const fn resizable(mut self, resizable: bool) -> Self {
        self.config.resizable = resizable;
        self
    }

    /// Sets if the window is movable.
    ///
    /// Has no effect if an anchor is set.
    pub const fn movable(mut self, movable: bool) -> Self {
        self.config.movable = movable;
        self
    }

    /// Sets if the title bar of the window is shown.
    pub const fn title_bar(mut self, title_bar: bool) -> Self {
        self.config.title_bar = title_bar;
        self
    }

    /// Sets if the top panel with the navigation buttons, current path display
    /// and search input should be visible.
    pub const fn show_top_panel(mut self, show_top_panel: bool) -> Self {
        self.config.show_top_panel = show_top_panel;
        self
    }

    /// Sets whether the parent folder button should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_parent_button(mut self, show_parent_button: bool) -> Self {
        self.config.show_parent_button = show_parent_button;
        self
    }

    /// Sets whether the back button should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_back_button(mut self, show_back_button: bool) -> Self {
        self.config.show_back_button = show_back_button;
        self
    }

    /// Sets whether the forward button should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_forward_button(mut self, show_forward_button: bool) -> Self {
        self.config.show_forward_button = show_forward_button;
        self
    }

    /// Sets whether the button to create a new folder should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_new_folder_button(mut self, show_new_folder_button: bool) -> Self {
        self.config.show_new_folder_button = show_new_folder_button;
        self
    }

    /// Sets whether the current path should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_current_path(mut self, show_current_path: bool) -> Self {
        self.config.show_current_path = show_current_path;
        self
    }

    /// Sets whether the button to text edit the current path should be visible in the top panel.
    ///
    /// has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_path_edit_button(mut self, show_path_edit_button: bool) -> Self {
        self.config.show_path_edit_button = show_path_edit_button;
        self
    }

    /// Sets whether the menu with the reload button and other options should be visible
    /// inside the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_menu_button(mut self, show_menu_button: bool) -> Self {
        self.config.show_menu_button = show_menu_button;
        self
    }

    /// Sets whether the reload button inside the top panel menu should be visible.
    ///
    /// Has no effect when `FileDialog::show_top_panel` or
    /// `FileDialog::show_menu_button` is disabled.
    pub const fn show_reload_button(mut self, show_reload_button: bool) -> Self {
        self.config.show_reload_button = show_reload_button;
        self
    }

    /// Sets whether the show hidden files and folders option inside the top panel
    /// menu should be visible.
    ///
    /// Has no effect when `FileDialog::show_top_panel` or
    /// `FileDialog::show_menu_button` is disabled.
    pub const fn show_hidden_option(mut self, show_hidden_option: bool) -> Self {
        self.config.show_hidden_option = show_hidden_option;
        self
    }

    /// Sets whether the show system files option inside the top panel
    /// menu should be visible.
    ///
    /// Has no effect when `FileDialog::show_top_panel` or
    /// `FileDialog::show_menu_button` is disabled.
    pub const fn show_system_files_option(mut self, show_system_files_option: bool) -> Self {
        self.config.show_system_files_option = show_system_files_option;
        self
    }

    /// Sets whether the search input should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub const fn show_search(mut self, show_search: bool) -> Self {
        self.config.show_search = show_search;
        self
    }

    /// Sets if the sidebar with the shortcut directories such as
    /// “Home”, “Documents” etc. should be visible.
    pub const fn show_left_panel(mut self, show_left_panel: bool) -> Self {
        self.config.show_left_panel = show_left_panel;
        self
    }

    /// Sets if pinned folders should be listed in the left sidebar.
    /// Disabling this will also disable the functionality to pin a folder.
    pub const fn show_pinned_folders(mut self, show_pinned_folders: bool) -> Self {
        self.config.show_pinned_folders = show_pinned_folders;
        self
    }

    /// Sets if the "Places" section should be visible in the left sidebar.
    /// The Places section contains the user directories such as Home or Documents.
    ///
    /// Has no effect when `FileDialog::show_left_panel` is disabled.
    pub const fn show_places(mut self, show_places: bool) -> Self {
        self.config.show_places = show_places;
        self
    }

    /// Sets if the "Devices" section should be visible in the left sidebar.
    /// The Devices section contains the non removable system disks.
    ///
    /// Has no effect when `FileDialog::show_left_panel` is disabled.
    pub const fn show_devices(mut self, show_devices: bool) -> Self {
        self.config.show_devices = show_devices;
        self
    }

    /// Sets if the "Removable Devices" section should be visible in the left sidebar.
    /// The Removable Devices section contains the removable disks like USB disks.
    ///
    /// Has no effect when `FileDialog::show_left_panel` is disabled.
    pub const fn show_removable_devices(mut self, show_removable_devices: bool) -> Self {
        self.config.show_removable_devices = show_removable_devices;
        self
    }

    // -------------------------------------------------
    // Getter:

    /// Returns the directory or file that the user picked, or the target file
    /// if the dialog is in `DialogMode::SaveFile` mode.
    ///
    /// None is returned when the user has not yet selected an item.
    pub fn picked(&self) -> Option<&Path> {
        match &self.state {
            DialogState::Picked(path) => Some(path),
            _ => None,
        }
    }

    /// Returns the directory or file that the user picked, or the target file
    /// if the dialog is in `DialogMode::SaveFile` mode.
    /// Unlike `FileDialog::picked`, this method returns the picked path only once and
    /// sets the dialog's state to `DialogState::Closed`.
    ///
    /// None is returned when the user has not yet picked an item.
    pub fn take_picked(&mut self) -> Option<PathBuf> {
        match &mut self.state {
            DialogState::Picked(path) => {
                let path = std::mem::take(path);
                self.state = DialogState::Closed;
                Some(path)
            }
            _ => None,
        }
    }

    /// Returns a list of the files and folders the user picked, when the dialog is in
    /// `DialogMode::PickMultiple` mode.
    ///
    /// None is returned when the user has not yet picked an item.
    pub fn picked_multiple(&self) -> Option<Vec<&Path>> {
        match &self.state {
            DialogState::PickedMultiple(items) => {
                Some(items.iter().map(std::path::PathBuf::as_path).collect())
            }
            _ => None,
        }
    }

    /// Returns a list of the files and folders the user picked, when the dialog is in
    /// `DialogMode::PickMultiple` mode.
    /// Unlike `FileDialog::picked_multiple`, this method returns the picked paths only once
    /// and sets the dialog's state to `DialogState::Closed`.
    ///
    /// None is returned when the user has not yet picked an item.
    pub fn take_picked_multiple(&mut self) -> Option<Vec<PathBuf>> {
        match &mut self.state {
            DialogState::PickedMultiple(items) => {
                let items = std::mem::take(items);
                self.state = DialogState::Closed;
                Some(items)
            }
            _ => None,
        }
    }

    /// Returns the currently active directory entry.
    ///
    /// This is either the currently highlighted entry, or the currently active directory
    /// if nothing is being highlighted.
    ///
    /// For the [`DialogMode::SelectMultiple`] counterpart,
    /// see [`FileDialog::active_selected_entries`].
    pub const fn selected_entry(&self) -> Option<&DirectoryEntry> {
        self.selected_item.as_ref()
    }

    /// Returns an iterator over the currently selected entries in [`SelectMultiple`] mode.
    ///
    /// For the counterpart in single selection modes, see [`FileDialog::active_entry`].
    ///
    /// [`SelectMultiple`]: DialogMode::SelectMultiple
    pub fn selected_entries(&self) -> impl Iterator<Item = &DirectoryEntry> {
        self.get_dir_content_filtered_iter().filter(|p| p.selected)
    }

    /// Returns the ID of the operation for which the dialog is currently being used.
    ///
    /// See `FileDialog::open` for more information.
    pub fn operation_id(&self) -> Option<&str> {
        self.operation_id.as_deref()
    }

    /// Returns the mode the dialog is currently in.
    pub const fn mode(&self) -> DialogMode {
        self.mode
    }

    /// Returns the state the dialog is currently in.
    pub fn state(&self) -> DialogState {
        self.state.clone()
    }

    /// Get the window Id
    pub const fn get_window_id(&self) -> egui::Id {
        self.window_id
    }
}

/// UI methods
impl FileDialog {
    /// Main update method of the UI
    ///
    /// Takes an optional callback to show a custom right panel.
    fn update_ui(
        &mut self,
        ctx: &egui::Context,
        right_panel_fn: Option<&mut FileDialogUiCallback>,
    ) {
        let mut is_open = true;

        if self.config.as_modal {
            let re = self.ui_update_modal_background(ctx);
            ctx.move_to_top(re.response.layer_id);
        }

        let re = self.create_window(&mut is_open).show(ctx, |ui| {
            if !self.modals.is_empty() {
                self.ui_update_modals(ui);
                return;
            }

            if self.config.show_top_panel {
                egui::TopBottomPanel::top(self.window_id.with("top_panel"))
                    .resizable(false)
                    .show_inside(ui, |ui| {
                        self.ui_update_top_panel(ui);
                    });
            }

            if self.config.show_left_panel {
                egui::SidePanel::left(self.window_id.with("left_panel"))
                    .resizable(true)
                    .default_width(150.0)
                    .width_range(90.0..=250.0)
                    .show_inside(ui, |ui| {
                        self.ui_update_left_panel(ui);
                    });
            }

            // Optionally, show a custom right panel (see `update_with_custom_right_panel`)
            if let Some(f) = right_panel_fn {
                let mut right_panel = egui::SidePanel::right(self.window_id.with("right_panel"))
                    // Unlike the left panel, we have no control over the contents, so
                    // we don't restrict the width. It's up to the user to make the UI presentable.
                    .resizable(true);
                if let Some(width) = self.config.right_panel_width {
                    right_panel = right_panel.default_width(width);
                }
                right_panel.show_inside(ui, |ui| {
                    f(ui, self);
                });
            }

            egui::TopBottomPanel::bottom(self.window_id.with("bottom_panel"))
                .resizable(false)
                .show_inside(ui, |ui| {
                    self.ui_update_bottom_panel(ui);
                });

            egui::CentralPanel::default().show_inside(ui, |ui| {
                self.ui_update_central_panel(ui);
            });
        });

        if self.config.as_modal {
            if let Some(inner_response) = re {
                ctx.move_to_top(inner_response.response.layer_id);
            }
        }

        self.any_focused_last_frame = ctx.memory(egui::Memory::focused).is_some();

        // User closed the window without finishing the dialog
        if !is_open {
            self.cancel();
        }

        let mut repaint = false;

        // Collect dropped files:
        ctx.input(|i| {
            // Check if files were dropped
            if let Some(dropped_file) = i.raw.dropped_files.last() {
                if let Some(path) = &dropped_file.path {
                    if self.config.file_system.is_dir(path) {
                        // If we dropped a directory, go there
                        self.load_directory(path.as_path());
                        repaint = true;
                    } else if let Some(parent) = path.parent() {
                        // Else, go to the parent directory
                        self.load_directory(parent);
                        self.select_item(&mut DirectoryEntry::from_path(
                            &self.config,
                            path,
                            &*self.config.file_system,
                        ));
                        self.scroll_to_selection = true;
                        repaint = true;
                    }
                }
            }
        });

        // Update GUI if we dropped a file
        if repaint {
            ctx.request_repaint();
        }
    }

    /// Updates the main modal background of the file dialog window.
    fn ui_update_modal_background(&self, ctx: &egui::Context) -> egui::InnerResponse<()> {
        egui::Area::new(self.window_id.with("modal_overlay"))
            .interactable(true)
            .fixed_pos(egui::Pos2::ZERO)
            .show(ctx, |ui| {
                let screen_rect = ctx.input(|i| i.screen_rect);

                ui.allocate_response(screen_rect.size(), egui::Sense::click());

                ui.painter().rect_filled(
                    screen_rect,
                    egui::CornerRadius::ZERO,
                    self.config.modal_overlay_color,
                );
            })
    }

    fn ui_update_modals(&mut self, ui: &mut egui::Ui) {
        // Currently, a rendering error occurs when only a single central panel is rendered
        // inside a window. Therefore, when rendering a modal, we render an invisible bottom panel,
        // which prevents the error.
        // This is currently a bit hacky and should be adjusted again in the future.
        egui::TopBottomPanel::bottom(self.window_id.with("modal_bottom_panel"))
            .resizable(false)
            .show_separator_line(false)
            .show_inside(ui, |_| {});

        // We need to use a central panel for the modals so that the
        // window doesn't resize to the size of the modal.
        egui::CentralPanel::default().show_inside(ui, |ui| {
            if let Some(modal) = self.modals.last_mut() {
                #[allow(clippy::single_match)]
                match modal.update(&self.config, ui) {
                    ModalState::Close(action) => {
                        self.exec_modal_action(action);
                        self.modals.pop();
                    }
                    ModalState::Pending => {}
                }
            }
        });
    }

    /// Creates a new egui window with the configured options.
    fn create_window<'a>(&self, is_open: &'a mut bool) -> egui::Window<'a> {
        let mut window = egui::Window::new(self.get_window_title())
            .id(self.window_id)
            .open(is_open)
            .default_size(self.config.default_size)
            .min_size(self.config.min_size)
            .resizable(self.config.resizable)
            .movable(self.config.movable)
            .title_bar(self.config.title_bar)
            .collapsible(false);

        if let Some(pos) = self.config.default_pos {
            window = window.default_pos(pos);
        }

        if let Some(pos) = self.config.fixed_pos {
            window = window.fixed_pos(pos);
        }

        if let Some((anchor, offset)) = self.config.anchor {
            window = window.anchor(anchor, offset);
        }

        if let Some(size) = self.config.max_size {
            window = window.max_size(size);
        }

        window
    }

    /// Gets the window title to use.
    /// This is either one of the default window titles or the configured window title.
    const fn get_window_title(&self) -> &String {
        match &self.config.title {
            Some(title) => title,
            None => match &self.mode {
                DialogMode::PickDirectory => &self.config.labels.title_select_directory,
                DialogMode::PickFile => &self.config.labels.title_select_file,
                DialogMode::PickMultiple => &self.config.labels.title_select_multiple,
                DialogMode::SaveFile => &self.config.labels.title_save_file,
            },
        }
    }

    /// Updates the top panel of the dialog. Including the navigation buttons,
    /// the current path display, the reload button and the search field.
    fn ui_update_top_panel(&mut self, ui: &mut egui::Ui) {
        const BUTTON_SIZE: egui::Vec2 = egui::Vec2::new(25.0, 25.0);

        ui.horizontal(|ui| {
            self.ui_update_nav_buttons(ui, BUTTON_SIZE);

            let mut path_display_width = ui.available_width();

            // Leave some area for the menu button and search input
            if self.config.show_reload_button {
                path_display_width -= ui
                    .style()
                    .spacing
                    .item_spacing
                    .x
                    .mul_add(2.5, BUTTON_SIZE.x);
            }

            if self.config.show_search {
                path_display_width -= 140.0;
            }

            if self.config.show_current_path {
                self.ui_update_current_path(ui, path_display_width);
            }

            // Menu button containing reload button and different options
            if self.config.show_menu_button
                && (self.config.show_reload_button
                    || self.config.show_hidden_option
                    || self.config.show_system_files_option)
            {
                ui.allocate_ui_with_layout(
                    BUTTON_SIZE,
                    egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
                    |ui| {
                        ui.menu_button("☰", |ui| {
                            if self.config.show_reload_button
                                && ui.button(&self.config.labels.reload).clicked()
                            {
                                self.refresh();
                                ui.close_menu();
                            }

                            if self.config.show_hidden_option
                                && ui
                                    .checkbox(
                                        &mut self.config.storage.show_hidden,
                                        &self.config.labels.show_hidden,
                                    )
                                    .clicked()
                            {
                                self.refresh();
                                ui.close_menu();
                            }

                            if self.config.show_system_files_option
                                && ui
                                    .checkbox(
                                        &mut self.config.storage.show_system_files,
                                        &self.config.labels.show_system_files,
                                    )
                                    .clicked()
                            {
                                self.refresh();
                                ui.close_menu();
                            }
                        });
                    },
                );
            }

            if self.config.show_search {
                self.ui_update_search(ui);
            }
        });

        ui.add_space(ui.ctx().style().spacing.item_spacing.y);
    }

    /// Updates the navigation buttons like parent or previous directory
    fn ui_update_nav_buttons(&mut self, ui: &mut egui::Ui, button_size: egui::Vec2) {
        if self.config.show_parent_button {
            if let Some(x) = self.current_directory() {
                if self.ui_button_sized(ui, x.parent().is_some(), button_size, "⏶", None) {
                    self.load_parent_directory();
                }
            } else {
                let _ = self.ui_button_sized(ui, false, button_size, "⏶", None);
            }
        }

        if self.config.show_back_button
            && self.ui_button_sized(
                ui,
                self.directory_offset + 1 < self.directory_stack.len(),
                button_size,
                "⏴",
                None,
            )
        {
            self.load_previous_directory();
        }

        if self.config.show_forward_button
            && self.ui_button_sized(ui, self.directory_offset != 0, button_size, "⏵", None)
        {
            self.load_next_directory();
        }

        if self.config.show_new_folder_button
            && self.ui_button_sized(
                ui,
                !self.create_directory_dialog.is_open(),
                button_size,
                "+",
                None,
            )
        {
            self.open_new_folder_dialog();
        }
    }

    /// Updates the view to display the current path.
    /// This could be the view for displaying the current path and the individual sections,
    /// as well as the view for text editing of the current path.
    fn ui_update_current_path(&mut self, ui: &mut egui::Ui, width: f32) {
        egui::Frame::default()
            .stroke(egui::Stroke::new(
                1.0,
                ui.ctx().style().visuals.window_stroke.color,
            ))
            .inner_margin(egui::Margin::from(4))
            .corner_radius(egui::CornerRadius::from(4))
            .show(ui, |ui| {
                const EDIT_BUTTON_SIZE: egui::Vec2 = egui::Vec2::new(22.0, 20.0);

                if self.path_edit_visible {
                    self.ui_update_path_edit(ui, width, EDIT_BUTTON_SIZE);
                } else {
                    self.ui_update_path_display(ui, width, EDIT_BUTTON_SIZE);
                }
            });
    }

    /// Updates the view when the currently open path with the individual sections is displayed.
    fn ui_update_path_display(
        &mut self,
        ui: &mut egui::Ui,
        width: f32,
        edit_button_size: egui::Vec2,
    ) {
        ui.style_mut().always_scroll_the_only_direction = true;
        ui.style_mut().spacing.scroll.bar_width = 8.0;

        let max_width = if self.config.show_path_edit_button {
            ui.style()
                .spacing
                .item_spacing
                .x
                .mul_add(-2.0, width - edit_button_size.x)
        } else {
            width
        };

        egui::ScrollArea::horizontal()
            .auto_shrink([false, false])
            .stick_to_right(true)
            .max_width(max_width)
            .show(ui, |ui| {
                ui.horizontal(|ui| {
                    ui.style_mut().spacing.item_spacing.x /= 2.5;
                    ui.style_mut().spacing.button_padding = egui::Vec2::new(5.0, 3.0);

                    let mut path = PathBuf::new();

                    if let Some(data) = self.current_directory() {
                        for (i, segment) in data.iter().enumerate() {
                            path.push(segment);

                            let segment_str = segment.to_str().unwrap_or("<ERR>");

                            if i != 0 {
                                ui.label(self.config.directory_separator.as_str());
                            }

                            if ui.button(segment_str).clicked() {
                                self.load_directory(path.as_path());
                                return;
                            }
                        }
                    }
                });
            });

        if !self.config.show_path_edit_button {
            return;
        }

        if ui
            .add_sized(
                edit_button_size,
                egui::Button::new("🖊").fill(egui::Color32::TRANSPARENT),
            )
            .clicked()
        {
            self.open_path_edit();
        }
    }

    /// Updates the view when the user currently wants to text edit the current path.
    fn ui_update_path_edit(&mut self, ui: &mut egui::Ui, width: f32, edit_button_size: egui::Vec2) {
        let desired_width: f32 = ui
            .style()
            .spacing
            .item_spacing
            .x
            .mul_add(-3.0, width - edit_button_size.x);

        let response = egui::TextEdit::singleline(&mut self.path_edit_value)
            .desired_width(desired_width)
            .show(ui)
            .response;

        if self.path_edit_activate {
            response.request_focus();
            Self::set_cursor_to_end(&response, &self.path_edit_value);
            self.path_edit_activate = false;
        }

        if self.path_edit_request_focus {
            response.request_focus();
            self.path_edit_request_focus = false;
        }

        let btn_response = ui.add_sized(edit_button_size, egui::Button::new("✔"));

        if btn_response.clicked() {
            self.submit_path_edit();
        }

        if !response.has_focus() && !btn_response.contains_pointer() {
            self.path_edit_visible = false;
        }
    }

    /// Updates the search input
    fn ui_update_search(&mut self, ui: &mut egui::Ui) {
        egui::Frame::default()
            .stroke(egui::Stroke::new(
                1.0,
                ui.ctx().style().visuals.window_stroke.color,
            ))
            .inner_margin(egui::Margin::symmetric(4, 4))
            .corner_radius(egui::CornerRadius::from(4))
            .show(ui, |ui| {
                ui.with_layout(egui::Layout::left_to_right(egui::Align::Min), |ui| {
                    ui.add_space(ui.ctx().style().spacing.item_spacing.y);

                    ui.label(egui::RichText::from("🔍").size(15.0));

                    let re = ui.add_sized(
                        egui::Vec2::new(ui.available_width(), 0.0),
                        egui::TextEdit::singleline(&mut self.search_value),
                    );

                    self.edit_search_on_text_input(ui);

                    if re.changed() || self.init_search {
                        self.selected_item = None;
                        self.select_first_visible_item();
                    }

                    if self.init_search {
                        re.request_focus();
                        Self::set_cursor_to_end(&re, &self.search_value);
                        self.directory_content.reset_multi_selection();

                        self.init_search = false;
                    }
                });
            });
    }

    /// Focuses and types into the search input, if text input without
    /// shortcut modifiers is detected, and no other inputs are focused.
    ///
    /// # Arguments
    ///
    /// - `re`: The [`egui::Response`] returned by the filter text edit widget
    fn edit_search_on_text_input(&mut self, ui: &egui::Ui) {
        if ui.memory(|mem| mem.focused().is_some()) {
            return;
        }

        ui.input(|inp| {
            // We stop if any modifier is active besides only shift
            if inp.modifiers.any() && !inp.modifiers.shift_only() {
                return;
            }

            // If we find any text input event, we append it to the filter string
            // and allow proceeding to activating the filter input widget.
            for text in inp.events.iter().filter_map(|ev| match ev {
                egui::Event::Text(t) => Some(t),
                _ => None,
            }) {
                self.search_value.push_str(text);
                self.init_search = true;
            }
        });
    }

    /// Updates the left panel of the dialog. Including the list of the user directories (Places)
    /// and system disks (Devices, Removable Devices).
    fn ui_update_left_panel(&mut self, ui: &mut egui::Ui) {
        ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
            // Spacing multiplier used between sections in the left sidebar
            const SPACING_MULTIPLIER: f32 = 4.0;

            egui::containers::ScrollArea::vertical()
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    // Spacing for the first section in the left sidebar
                    let mut spacing = ui.ctx().style().spacing.item_spacing.y * 2.0;

                    // Update paths pinned to the left sidebar by the user
                    if self.config.show_pinned_folders && self.ui_update_pinned_paths(ui, spacing) {
                        spacing = ui.ctx().style().spacing.item_spacing.y * SPACING_MULTIPLIER;
                    }

                    // Update custom quick access sections
                    let quick_accesses = std::mem::take(&mut self.config.quick_accesses);

                    for quick_access in &quick_accesses {
                        ui.add_space(spacing);
                        self.ui_update_quick_access(ui, quick_access);
                        spacing = ui.ctx().style().spacing.item_spacing.y * SPACING_MULTIPLIER;
                    }

                    self.config.quick_accesses = quick_accesses;

                    // Update native quick access sections
                    if self.config.show_places && self.ui_update_user_directories(ui, spacing) {
                        spacing = ui.ctx().style().spacing.item_spacing.y * SPACING_MULTIPLIER;
                    }

                    let disks = std::mem::take(&mut self.system_disks);

                    if self.config.show_devices && self.ui_update_devices(ui, spacing, &disks) {
                        spacing = ui.ctx().style().spacing.item_spacing.y * SPACING_MULTIPLIER;
                    }

                    if self.config.show_removable_devices
                        && self.ui_update_removable_devices(ui, spacing, &disks)
                    {
                        // Add this when we add a new section after removable devices
                        // spacing = ui.ctx().style().spacing.item_spacing.y * SPACING_MULTIPLIER;
                    }

                    self.system_disks = disks;
                });
        });
    }

    /// Updates a path entry in the left panel.
    ///
    /// Returns the response of the selectable label.
    fn ui_update_left_panel_entry(
        &mut self,
        ui: &mut egui::Ui,
        display_name: &str,
        path: &Path,
    ) -> egui::Response {
        let response = ui.selectable_label(self.current_directory() == Some(path), display_name);

        if response.clicked() {
            self.load_directory(path);
        }

        response
    }

    /// Updates a custom quick access section added to the left panel.
    fn ui_update_quick_access(&mut self, ui: &mut egui::Ui, quick_access: &QuickAccess) {
        ui.label(&quick_access.heading);

        for entry in &quick_access.paths {
            self.ui_update_left_panel_entry(ui, &entry.display_name, &entry.path);
        }
    }

    /// Updates the list of pinned folders.
    ///
    /// Returns true if at least one directory item was included in the list and the
    /// heading is visible. If no item was listed, false is returned.
    fn ui_update_pinned_paths(&mut self, ui: &mut egui::Ui, spacing: f32) -> bool {
        let mut visible = false;

        for (i, path) in self
            .config
            .storage
            .pinned_folders
            .clone()
            .iter()
            .enumerate()
        {
            if i == 0 {
                ui.add_space(spacing);
                ui.label(self.config.labels.heading_pinned.as_str());

                visible = true;
            }

            let response = self.ui_update_left_panel_entry(
                ui,
                &format!("{}  {}", self.config.pinned_icon, path.file_name()),
                path.as_path(),
            );

            self.ui_update_path_context_menu(&response, path);
        }

        visible
    }

    /// Updates the list of user directories (Places).
    ///
    /// Returns true if at least one directory was included in the list and the
    /// heading is visible. If no directory was listed, false is returned.
    fn ui_update_user_directories(&mut self, ui: &mut egui::Ui, spacing: f32) -> bool {
        // Take temporary ownership of the user directories and configuration.
        // This is done so that we don't have to clone the user directories and
        // configured display names.
        let user_directories = std::mem::take(&mut self.user_directories);
        let labels = std::mem::take(&mut self.config.labels);

        let mut visible = false;

        if let Some(dirs) = &user_directories {
            ui.add_space(spacing);
            ui.label(labels.heading_places.as_str());

            if let Some(path) = dirs.home_dir() {
                self.ui_update_left_panel_entry(ui, &labels.home_dir, path);
            }

            if let Some(path) = dirs.desktop_dir() {
                self.ui_update_left_panel_entry(ui, &labels.desktop_dir, path);
            }
            if let Some(path) = dirs.document_dir() {
                self.ui_update_left_panel_entry(ui, &labels.documents_dir, path);
            }
            if let Some(path) = dirs.download_dir() {
                self.ui_update_left_panel_entry(ui, &labels.downloads_dir, path);
            }
            if let Some(path) = dirs.audio_dir() {
                self.ui_update_left_panel_entry(ui, &labels.audio_dir, path);
            }
            if let Some(path) = dirs.picture_dir() {
                self.ui_update_left_panel_entry(ui, &labels.pictures_dir, path);
            }
            if let Some(path) = dirs.video_dir() {
                self.ui_update_left_panel_entry(ui, &labels.videos_dir, path);
            }

            visible = true;
        }

        self.user_directories = user_directories;
        self.config.labels = labels;

        visible
    }

    /// Updates the list of devices like system disks.
    ///
    /// Returns true if at least one device was included in the list and the
    /// heading is visible. If no device was listed, false is returned.
    fn ui_update_devices(&mut self, ui: &mut egui::Ui, spacing: f32, disks: &Disks) -> bool {
        let mut visible = false;

        for (i, disk) in disks.iter().filter(|x| !x.is_removable()).enumerate() {
            if i == 0 {
                ui.add_space(spacing);
                ui.label(self.config.labels.heading_devices.as_str());

                visible = true;
            }

            self.ui_update_device_entry(ui, disk);
        }

        visible
    }

    /// Updates the list of removable devices like USB drives.
    ///
    /// Returns true if at least one device was included in the list and the
    /// heading is visible. If no device was listed, false is returned.
    fn ui_update_removable_devices(
        &mut self,
        ui: &mut egui::Ui,
        spacing: f32,
        disks: &Disks,
    ) -> bool {
        let mut visible = false;

        for (i, disk) in disks.iter().filter(|x| x.is_removable()).enumerate() {
            if i == 0 {
                ui.add_space(spacing);
                ui.label(self.config.labels.heading_removable_devices.as_str());

                visible = true;
            }

            self.ui_update_device_entry(ui, disk);
        }

        visible
    }

    /// Updates a device entry of a device list like "Devices" or "Removable Devices".
    fn ui_update_device_entry(&mut self, ui: &mut egui::Ui, device: &Disk) {
        let label = if device.is_removable() {
            format!(
                "{}  {}",
                self.config.removable_device_icon,
                device.display_name()
            )
        } else {
            format!("{}  {}", self.config.device_icon, device.display_name())
        };

        self.ui_update_left_panel_entry(ui, &label, device.mount_point());
    }

    /// Updates the bottom panel showing the selected item and main action buttons.
    fn ui_update_bottom_panel(&mut self, ui: &mut egui::Ui) {
        const BUTTON_HEIGHT: f32 = 20.0;
        ui.add_space(5.0);

        // Calculate the width of the action buttons
        let label_submit_width = match self.mode {
            DialogMode::PickDirectory | DialogMode::PickFile | DialogMode::PickMultiple => {
                Self::calc_text_width(ui, &self.config.labels.open_button)
            }
            DialogMode::SaveFile => Self::calc_text_width(ui, &self.config.labels.save_button),
        };

        let mut btn_width = Self::calc_text_width(ui, &self.config.labels.cancel_button);
        if label_submit_width > btn_width {
            btn_width = label_submit_width;
        }

        btn_width += ui.spacing().button_padding.x * 4.0;

        // The size of the action buttons "cancel" and "open"/"save"
        let button_size: egui::Vec2 = egui::Vec2::new(btn_width, BUTTON_HEIGHT);

        self.ui_update_selection_preview(ui, button_size);

        if self.mode == DialogMode::SaveFile {
            ui.add_space(ui.style().spacing.item_spacing.y * 2.0);
        }

        self.ui_update_action_buttons(ui, button_size);
    }

    /// Updates the selection preview like "Selected directory: X"
    fn ui_update_selection_preview(&mut self, ui: &mut egui::Ui, button_size: egui::Vec2) {
        const SELECTION_PREVIEW_MIN_WIDTH: f32 = 50.0;
        let item_spacing = ui.style().spacing.item_spacing;

        let render_filter_selection = !self.config.file_filters.is_empty()
            && (self.mode == DialogMode::PickFile || self.mode == DialogMode::PickMultiple);

        let filter_selection_width = button_size.x.mul_add(2.0, item_spacing.x);
        let mut filter_selection_separate_line = false;

        ui.horizontal(|ui| {
            match &self.mode {
                DialogMode::PickDirectory => ui.label(&self.config.labels.selected_directory),
                DialogMode::PickFile => ui.label(&self.config.labels.selected_file),
                DialogMode::PickMultiple => ui.label(&self.config.labels.selected_items),
                DialogMode::SaveFile => ui.label(&self.config.labels.file_name),
            };

            // Make sure there is enough width for the selection preview. If the available
            // width is not enough, render the drop-down menu to select a file filter on
            // a separate line and give the selection preview the entire available width.
            let mut scroll_bar_width: f32 =
                ui.available_width() - filter_selection_width - item_spacing.x;

            if scroll_bar_width < SELECTION_PREVIEW_MIN_WIDTH || !render_filter_selection {
                filter_selection_separate_line = true;
                scroll_bar_width = ui.available_width();
            }

            match &self.mode {
                DialogMode::PickDirectory | DialogMode::PickFile | DialogMode::PickMultiple => {
                    use egui::containers::scroll_area::ScrollBarVisibility;

                    let text = self.get_selection_preview_text();

                    egui::containers::ScrollArea::horizontal()
                        .auto_shrink([false, false])
                        .max_width(scroll_bar_width)
                        .stick_to_right(true)
                        .scroll_bar_visibility(ScrollBarVisibility::AlwaysHidden)
                        .show(ui, |ui| {
                            ui.colored_label(ui.style().visuals.selection.bg_fill, text);
                        });
                }
                DialogMode::SaveFile => {
                    let response = ui.add(
                        egui::TextEdit::singleline(&mut self.file_name_input)
                            .desired_width(f32::INFINITY),
                    );

                    if self.file_name_input_request_focus {
                        response.request_focus();
                        self.file_name_input_request_focus = false;
                    }

                    if response.changed() {
                        self.file_name_input_error = self.validate_file_name_input();
                    }

                    if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
                        self.submit();
                    }
                }
            };

            if !filter_selection_separate_line && render_filter_selection {
                self.ui_update_file_filter_selection(ui, filter_selection_width);
            }
        });

        if filter_selection_separate_line && render_filter_selection {
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
                self.ui_update_file_filter_selection(ui, filter_selection_width);
            });
        }
    }

    fn get_selection_preview_text(&self) -> String {
        if self.is_selection_valid() {
            match &self.mode {
                DialogMode::PickDirectory | DialogMode::PickFile => self
                    .selected_item
                    .as_ref()
                    .map_or_else(String::new, |item| item.file_name().to_string()),
                DialogMode::PickMultiple => {
                    let mut result = String::new();

                    for (i, item) in self
                        .get_dir_content_filtered_iter()
                        .filter(|p| p.selected)
                        .enumerate()
                    {
                        if i == 0 {
                            result += item.file_name();
                            continue;
                        }

                        result += format!(", {}", item.file_name()).as_str();
                    }

                    result
                }
                DialogMode::SaveFile => String::new(),
            }
        } else {
            String::new()
        }
    }

    fn ui_update_file_filter_selection(&mut self, ui: &mut egui::Ui, width: f32) {
        let selected_filter = self.get_selected_file_filter();
        let selected_text = match selected_filter {
            Some(f) => &f.name,
            None => &self.config.labels.file_filter_all_files,
        };

        // The item that the user selected inside the drop down.
        // If none, no item was selected by the user.
        let mut select_filter: Option<Option<egui::Id>> = None;

        egui::containers::ComboBox::from_id_salt(self.window_id.with("file_filter_selection"))
            .width(width)
            .selected_text(selected_text)
            .wrap_mode(egui::TextWrapMode::Truncate)
            .show_ui(ui, |ui| {
                for filter in &self.config.file_filters {
                    let selected = selected_filter.is_some_and(|f| f.id == filter.id);

                    if ui.selectable_label(selected, &filter.name).clicked() {
                        select_filter = Some(Some(filter.id));
                    }
                }

                if ui
                    .selectable_label(
                        selected_filter.is_none(),
                        &self.config.labels.file_filter_all_files,
                    )
                    .clicked()
                {
                    select_filter = Some(None);
                }
            });

        if let Some(i) = select_filter {
            self.selected_file_filter = i;
            self.selected_item = None;
            self.refresh();
        }
    }

    /// Updates the action buttons like save, open and cancel
    fn ui_update_action_buttons(&mut self, ui: &mut egui::Ui, button_size: egui::Vec2) {
        ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
            let label = match &self.mode {
                DialogMode::PickDirectory | DialogMode::PickFile | DialogMode::PickMultiple => {
                    self.config.labels.open_button.as_str()
                }
                DialogMode::SaveFile => self.config.labels.save_button.as_str(),
            };

            if self.ui_button_sized(
                ui,
                self.is_selection_valid(),
                button_size,
                label,
                self.file_name_input_error.as_deref(),
            ) {
                self.submit();
            }

            if ui
                .add_sized(
                    button_size,
                    egui::Button::new(self.config.labels.cancel_button.as_str()),
                )
                .clicked()
            {
                self.cancel();
            }
        });
    }

    /// Updates the central panel. This is either the contents of the directory
    /// or the error message when there was an error loading the current directory.
    fn ui_update_central_panel(&mut self, ui: &mut egui::Ui) {
        if self.update_directory_content(ui) {
            return;
        }

        self.ui_update_central_panel_content(ui);
    }

    /// Updates the directory content (Not the UI!).
    /// This is required because the contents of the directory might be loaded on a
    /// separate thread. This function checks the status of the directory content
    /// and updates the UI accordingly.
    fn update_directory_content(&mut self, ui: &mut egui::Ui) -> bool {
        const SHOW_SPINNER_AFTER: f32 = 0.2;

        match self.directory_content.update() {
            DirectoryContentState::Pending(timestamp) => {
                let now = std::time::SystemTime::now();

                if now
                    .duration_since(*timestamp)
                    .unwrap_or_default()
                    .as_secs_f32()
                    > SHOW_SPINNER_AFTER
                {
                    ui.centered_and_justified(egui::Ui::spinner);
                }

                // Prevent egui from not updating the UI when there is no user input
                ui.ctx().request_repaint();

                true
            }
            DirectoryContentState::Errored(err) => {
                ui.centered_and_justified(|ui| ui.colored_label(ui.visuals().error_fg_color, err));
                true
            }
            DirectoryContentState::Finished => {
                if self.mode == DialogMode::PickDirectory {
                    if let Some(dir) = self.current_directory() {
                        let mut dir_entry =
                            DirectoryEntry::from_path(&self.config, dir, &*self.config.file_system);
                        self.select_item(&mut dir_entry);
                    }
                }

                false
            }
            DirectoryContentState::Success => false,
        }
    }

    /// Updates the contents of the currently open directory.
    /// TODO: Refactor
    fn ui_update_central_panel_content(&mut self, ui: &mut egui::Ui) {
        // Temporarily take ownership of the directory content.
        let mut data = std::mem::take(&mut self.directory_content);

        // If the multi selection should be reset, excluding the currently
        // selected primary item.
        let mut reset_multi_selection = false;

        // The item the user wants to make a batch selection from.
        // The primary selected item is used for item a.
        let mut batch_select_item_b: Option<DirectoryEntry> = None;

        // If we should return after updating the directory entries.
        let mut should_return = false;

        ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
            let scroll_area = egui::containers::ScrollArea::vertical().auto_shrink([false, false]);

            if self.search_value.is_empty()
                && !self.create_directory_dialog.is_open()
                && !self.scroll_to_selection
            {
                // Only update visible items when the search value is empty,
                // the create directory dialog is closed and we are currently not scrolling
                // to the current item.
                scroll_area.show_rows(ui, ui.spacing().interact_size.y, data.len(), |ui, range| {
                    for item in data.iter_range_mut(range) {
                        if self.ui_update_central_panel_entry(
                            ui,
                            item,
                            &mut reset_multi_selection,
                            &mut batch_select_item_b,
                        ) {
                            should_return = true;
                        }
                    }
                });
            } else {
                // Update each element if the search value is not empty as we apply the
                // search value in every frame. We can't use `egui::ScrollArea::show_rows`
                // because we don't know how many files the search value applies to.
                // We also have to update every item when the create directory dialog is open as
                // it's displayed as the last element.
                scroll_area.show(ui, |ui| {
                    for item in data.filtered_iter_mut(&self.search_value.clone()) {
                        if self.ui_update_central_panel_entry(
                            ui,
                            item,
                            &mut reset_multi_selection,
                            &mut batch_select_item_b,
                        ) {
                            should_return = true;
                        }
                    }

                    if let Some(entry) = self.ui_update_create_directory_dialog(ui) {
                        data.push(entry);
                    }
                });
            }
        });

        if should_return {
            return;
        }

        // Reset the multi selection except the currently selected primary item
        if reset_multi_selection {
            for item in data.filtered_iter_mut(&self.search_value) {
                if let Some(selected_item) = &self.selected_item {
                    if selected_item.path_eq(item) {
                        continue;
                    }
                }

                item.selected = false;
            }
        }

        // Check if we should perform a batch selection
        if let Some(item_b) = batch_select_item_b {
            if let Some(item_a) = &self.selected_item {
                self.batch_select_between(&mut data, item_a, &item_b);
            }
        }

        self.directory_content = data;
        self.scroll_to_selection = false;
    }

    /// Updates a single directory content entry.
    /// TODO: Refactor
    fn ui_update_central_panel_entry(
        &mut self,
        ui: &mut egui::Ui,
        item: &mut DirectoryEntry,
        reset_multi_selection: &mut bool,
        batch_select_item_b: &mut Option<DirectoryEntry>,
    ) -> bool {
        let file_name = item.file_name();
        let primary_selected = self.is_primary_selected(item);
        let pinned = self.is_pinned(item);

        let icons = if pinned {
            format!("{} {} ", item.icon(), self.config.pinned_icon)
        } else {
            format!("{} ", item.icon())
        };

        let icons_width = Self::calc_text_width(ui, &icons);

        // Calc available width for the file name and include a small margin
        let available_width = ui.available_width() - icons_width - 15.0;

        let truncate = self.config.truncate_filenames
            && available_width < Self::calc_text_width(ui, file_name);

        let text = if truncate {
            Self::truncate_filename(ui, item, available_width)
        } else {
            file_name.to_owned()
        };

        let mut re =
            ui.selectable_label(primary_selected || item.selected, format!("{icons}{text}"));

        if truncate {
            re = re.on_hover_text(file_name);
        }

        if item.is_dir() {
            self.ui_update_path_context_menu(&re, item);

            if re.context_menu_opened() {
                self.select_item(item);
            }
        }

        if primary_selected && self.scroll_to_selection {
            re.scroll_to_me(Some(egui::Align::Center));
            self.scroll_to_selection = false;
        }

        // The user wants to select the item as the primary selected item
        if re.clicked()
            && !ui.input(|i| i.modifiers.command)
            && !ui.input(|i| i.modifiers.shift_only())
        {
            self.select_item(item);

            // Reset the multi selection except the now primary selected item
            if self.mode == DialogMode::PickMultiple {
                *reset_multi_selection = true;
            }
        }

        // The user wants to select or unselect the item as part of a
        // multi selection
        if self.mode == DialogMode::PickMultiple
            && re.clicked()
            && ui.input(|i| i.modifiers.command)
        {
            if primary_selected {
                // If the clicked item is the primary selected item,
                // deselect it and remove it from the multi selection
                item.selected = false;
                self.selected_item = None;
            } else {
                item.selected = !item.selected;

                // If the item was selected, make it the primary selected item
                if item.selected {
                    self.select_item(item);
                }
            }
        }

        // The user wants to select every item between the last selected item
        // and the current item
        if self.mode == DialogMode::PickMultiple
            && re.clicked()
            && ui.input(|i| i.modifiers.shift_only())
        {
            if let Some(selected_item) = self.selected_item.clone() {
                // We perform a batch selection from the item that was
                // primarily selected before the user clicked on this item.
                *batch_select_item_b = Some(selected_item);

                // And now make this item the primary selected item
                item.selected = true;
                self.select_item(item);
            }
        }

        // The user double clicked on the directory entry.
        // Either open the directory or submit the dialog.
        if re.double_clicked() && !ui.input(|i| i.modifiers.command) {
            if item.is_dir() {
                self.load_directory(&item.to_path_buf());
                return true;
            }

            self.select_item(item);

            self.submit();
        }

        false
    }

    fn ui_update_create_directory_dialog(&mut self, ui: &mut egui::Ui) -> Option<DirectoryEntry> {
        self.create_directory_dialog
            .update(ui, &self.config)
            .directory()
            .map(|path| self.process_new_folder(&path))
    }

    /// Selects every item inside the `directory_content` between `item_a` and `item_b`,
    /// excluding both given items.
    fn batch_select_between(
        &self,
        directory_content: &mut DirectoryContent,
        item_a: &DirectoryEntry,
        item_b: &DirectoryEntry,
    ) {
        // Get the position of item a and item b
        let pos_a = directory_content
            .filtered_iter(&self.search_value)
            .position(|p| p.path_eq(item_a));
        let pos_b = directory_content
            .filtered_iter(&self.search_value)
            .position(|p| p.path_eq(item_b));

        // If both items where found inside the directory entry, mark every item between
        // them as selected
        if let Some(pos_a) = pos_a {
            if let Some(pos_b) = pos_b {
                if pos_a == pos_b {
                    return;
                }

                // Get the min and max of both positions.
                // We will iterate from min to max.
                let mut min = pos_a;
                let mut max = pos_b;

                if min > max {
                    min = pos_b;
                    max = pos_a;
                }

                for item in directory_content
                    .filtered_iter_mut(&self.search_value)
                    .enumerate()
                    .filter(|(i, _)| i > &min && i < &max)
                    .map(|(_, p)| p)
                {
                    item.selected = true;
                }
            }
        }
    }

    /// Helper function to add a sized button that can be enabled or disabled
    fn ui_button_sized(
        &self,
        ui: &mut egui::Ui,
        enabled: bool,
        size: egui::Vec2,
        label: &str,
        err_tooltip: Option<&str>,
    ) -> bool {
        let mut clicked = false;

        ui.add_enabled_ui(enabled, |ui| {
            let response = ui.add_sized(size, egui::Button::new(label));
            clicked = response.clicked();

            if let Some(err) = err_tooltip {
                response.on_disabled_hover_ui(|ui| {
                    ui.horizontal_wrapped(|ui| {
                        ui.spacing_mut().item_spacing.x = 0.0;

                        ui.colored_label(
                            ui.ctx().style().visuals.error_fg_color,
                            format!("{} ", self.config.err_icon),
                        );

                        ui.label(err);
                    });
                });
            }
        });

        clicked
    }

    /// Updates the context menu of a path.
    ///
    /// # Arguments
    ///
    /// * `item_response` - The response of the egui item for which the context menu should
    ///                     be opened.
    /// * `path` - The path for which the context menu should be opened.
    fn ui_update_path_context_menu(
        &mut self,
        item_response: &egui::Response,
        path: &DirectoryEntry,
    ) {
        // Path context menus are currently only used for pinned folders.
        if !self.config.show_pinned_folders {
            return;
        }

        item_response.context_menu(|ui| {
            let pinned = self.is_pinned(path);

            if pinned {
                if ui.button(&self.config.labels.unpin_folder).clicked() {
                    self.unpin_path(path);
                    ui.close_menu();
                }
            } else if ui.button(&self.config.labels.pin_folder).clicked() {
                self.pin_path(path.clone());
                ui.close_menu();
            }
        });
    }

    /// Sets the cursor position to the end of a text input field.
    ///
    /// # Arguments
    ///
    /// * `re` - response of the text input widget
    /// * `data` - buffer holding the text of the input widget
    fn set_cursor_to_end(re: &egui::Response, data: &str) {
        // Set the cursor to the end of the filter input string
        if let Some(mut state) = egui::TextEdit::load_state(&re.ctx, re.id) {
            state
                .cursor
                .set_char_range(Some(CCursorRange::one(CCursor::new(data.len()))));
            state.store(&re.ctx, re.id);
        }
    }

    /// Calculates the width of a single char.
    fn calc_char_width(ui: &egui::Ui, char: char) -> f32 {
        ui.fonts(|f| f.glyph_width(&egui::TextStyle::Body.resolve(ui.style()), char))
    }

    /// Calculates the width of the specified text using the current font configuration.
    /// Does not take new lines or text breaks into account!
    fn calc_text_width(ui: &egui::Ui, text: &str) -> f32 {
        let mut width = 0.0;

        for char in text.chars() {
            width += Self::calc_char_width(ui, char);
        }

        width
    }

    fn truncate_filename(ui: &egui::Ui, item: &DirectoryEntry, max_length: f32) -> String {
        const TRUNCATE_STR: &str = "...";

        let path = item.as_path();

        let file_stem = if item.is_file() {
            path.file_stem().and_then(|f| f.to_str()).unwrap_or("")
        } else {
            item.file_name()
        };

        let extension = if item.is_file() {
            path.extension().map_or(String::new(), |ext| {
                format!(".{}", ext.to_str().unwrap_or(""))
            })
        } else {
            String::new()
        };

        let extension_width = Self::calc_text_width(ui, &extension);
        let reserved = extension_width + Self::calc_text_width(ui, TRUNCATE_STR);

        if max_length <= reserved {
            return format!("{TRUNCATE_STR}{extension}");
        }

        let mut width = reserved;
        let mut front = String::new();
        let mut back = String::new();

        for (i, char) in file_stem.chars().enumerate() {
            let w = Self::calc_char_width(ui, char);

            if width + w > max_length {
                break;
            }

            front.push(char);
            width += w;

            let back_index = file_stem.len() - i - 1;

            if back_index <= i {
                break;
            }

            if let Some(char) = file_stem.chars().nth(back_index) {
                let w = Self::calc_char_width(ui, char);

                if width + w > max_length {
                    break;
                }

                back.push(char);
                width += w;
            }
        }

        format!(
            "{front}{TRUNCATE_STR}{}{extension}",
            back.chars().rev().collect::<String>()
        )
    }
}

/// Keybindings
impl FileDialog {
    /// Checks whether certain keybindings have been pressed and executes the corresponding actions.
    fn update_keybindings(&mut self, ctx: &egui::Context) {
        // We don't want to execute keybindings if a modal is currently open.
        // The modals implement the keybindings themselves.
        if let Some(modal) = self.modals.last_mut() {
            modal.update_keybindings(&self.config, ctx);
            return;
        }

        let keybindings = std::mem::take(&mut self.config.keybindings);

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.submit, false) {
            self.exec_keybinding_submit();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.cancel, false) {
            self.exec_keybinding_cancel();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.parent, true) {
            self.load_parent_directory();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.back, true) {
            self.load_previous_directory();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.forward, true) {
            self.load_next_directory();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.reload, true) {
            self.refresh();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.new_folder, true) {
            self.open_new_folder_dialog();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.edit_path, true) {
            self.open_path_edit();
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.home_edit_path, true) {
            if let Some(dirs) = &self.user_directories {
                if let Some(home) = dirs.home_dir() {
                    self.load_directory(home.to_path_buf().as_path());
                    self.open_path_edit();
                }
            }
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.selection_up, false) {
            self.exec_keybinding_selection_up();

            // We want to break out of input fields like search when pressing selection keys
            if let Some(id) = ctx.memory(egui::Memory::focused) {
                ctx.memory_mut(|w| w.surrender_focus(id));
            }
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.selection_down, false) {
            self.exec_keybinding_selection_down();

            // We want to break out of input fields like search when pressing selection keys
            if let Some(id) = ctx.memory(egui::Memory::focused) {
                ctx.memory_mut(|w| w.surrender_focus(id));
            }
        }

        if FileDialogKeyBindings::any_pressed(ctx, &keybindings.select_all, true)
            && self.mode == DialogMode::PickMultiple
        {
            for item in self.directory_content.filtered_iter_mut(&self.search_value) {
                item.selected = true;
            }
        }

        self.config.keybindings = keybindings;
    }

    /// Executes the action when the keybinding `submit` is pressed.
    fn exec_keybinding_submit(&mut self) {
        if self.path_edit_visible {
            self.submit_path_edit();
            return;
        }

        if self.create_directory_dialog.is_open() {
            if let Some(dir) = self.create_directory_dialog.submit().directory() {
                self.process_new_folder(&dir);
            }
            return;
        }

        // Check if there is a directory selected we can open
        if let Some(item) = &self.selected_item {
            // Make sure the selected item is visible inside the directory view.
            let is_visible = self
                .get_dir_content_filtered_iter()
                .any(|p| p.path_eq(item));

            if is_visible && item.is_dir() {
                self.load_directory(&item.to_path_buf());
                return;
            }
        }

        self.submit();
    }

    /// Executes the action when the keybinding `cancel` is pressed.
    fn exec_keybinding_cancel(&mut self) {
        // We have to check if the `create_directory_dialog` and `path_edit_visible` is open,
        // because egui does not consume pressing the escape key inside a text input.
        // So when pressing the escape key inside a text input, the text input is closed
        // but the keybindings still register the press on the escape key.
        // (Although the keybindings are updated before the UI and they check whether another
        //  widget is currently in focus!)
        //
        // This is practical for us because we can close the path edit and
        // the create directory dialog.
        // However, this causes problems when the user presses escape in other text
        // inputs for which we have no status saved. This would then close the entire file dialog.
        // To fix this, we check if any item was focused in the last frame.
        //
        // Note that this only happens with the escape key and not when the enter key is
        // used to close a text input. This is why we don't have to check for the
        // dialogs in `exec_keybinding_submit`.

        if self.create_directory_dialog.is_open() {
            self.create_directory_dialog.close();
        } else if self.path_edit_visible {
            self.close_path_edit();
        } else if !self.any_focused_last_frame {
            self.cancel();
            return;
        }
    }

    /// Executes the action when the keybinding `selection_up` is pressed.
    fn exec_keybinding_selection_up(&mut self) {
        if self.directory_content.len() == 0 {
            return;
        }

        self.directory_content.reset_multi_selection();

        if let Some(item) = &self.selected_item {
            if self.select_next_visible_item_before(&item.clone()) {
                return;
            }
        }

        // No item is selected or no more items left.
        // Select the last item from the directory content.
        self.select_last_visible_item();
    }

    /// Executes the action when the keybinding `selection_down` is pressed.
    fn exec_keybinding_selection_down(&mut self) {
        if self.directory_content.len() == 0 {
            return;
        }

        self.directory_content.reset_multi_selection();

        if let Some(item) = &self.selected_item {
            if self.select_next_visible_item_after(&item.clone()) {
                return;
            }
        }

        // No item is selected or no more items left.
        // Select the last item from the directory content.
        self.select_first_visible_item();
    }
}

/// Implementation
impl FileDialog {
    /// Get the file filter the user currently selected.
    fn get_selected_file_filter(&self) -> Option<&FileFilter> {
        self.selected_file_filter
            .and_then(|id| self.config.file_filters.iter().find(|p| p.id == id))
    }

    /// Gets a filtered iterator of the directory content of this object.
    fn get_dir_content_filtered_iter(&self) -> impl Iterator<Item = &DirectoryEntry> {
        self.directory_content.filtered_iter(&self.search_value)
    }

    /// Opens the dialog to create a new folder.
    fn open_new_folder_dialog(&mut self) {
        if let Some(x) = self.current_directory() {
            self.create_directory_dialog.open(x.to_path_buf());
        }
    }

    /// Function that processes a newly created folder.
    fn process_new_folder(&mut self, created_dir: &Path) -> DirectoryEntry {
        let mut entry =
            DirectoryEntry::from_path(&self.config, created_dir, &*self.config.file_system);

        self.directory_content.push(entry.clone());

        self.select_item(&mut entry);

        entry
    }

    /// Opens a new modal window.
    fn open_modal(&mut self, modal: Box<dyn FileDialogModal + Send + Sync>) {
        self.modals.push(modal);
    }

    /// Executes the given modal action.
    fn exec_modal_action(&mut self, action: ModalAction) {
        match action {
            ModalAction::None => {}
            ModalAction::SaveFile(path) => self.state = DialogState::Picked(path),
        };
    }

    /// Canonicalizes the specified path if canonicalization is enabled.
    /// Returns the input path if an error occurs or canonicalization is disabled.
    fn canonicalize_path(&self, path: &Path) -> PathBuf {
        if self.config.canonicalize_paths {
            dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
        } else {
            path.to_path_buf()
        }
    }

    /// Pins a path to the left sidebar.
    fn pin_path(&mut self, path: DirectoryEntry) {
        self.config.storage.pinned_folders.push(path);
    }

    /// Unpins a path from the left sidebar.
    fn unpin_path(&mut self, path: &DirectoryEntry) {
        self.config
            .storage
            .pinned_folders
            .retain(|p| !p.path_eq(path));
    }

    /// Checks if the path is pinned to the left sidebar.
    fn is_pinned(&self, path: &DirectoryEntry) -> bool {
        self.config
            .storage
            .pinned_folders
            .iter()
            .any(|p| path.path_eq(p))
    }

    fn is_primary_selected(&self, item: &DirectoryEntry) -> bool {
        self.selected_item.as_ref().is_some_and(|x| x.path_eq(item))
    }

    /// Resets the dialog to use default values.
    /// Configuration variables are retained.
    fn reset(&mut self) {
        let config = self.config.clone();
        *self = Self::with_config(config);
    }

    /// Refreshes the dialog.
    /// Including the user directories, system disks and currently open directory.
    fn refresh(&mut self) {
        self.user_directories = self
            .config
            .file_system
            .user_dirs(self.config.canonicalize_paths);
        self.system_disks = self
            .config
            .file_system
            .get_disks(self.config.canonicalize_paths);

        self.reload_directory();
    }

    /// Submits the current selection and tries to finish the dialog, if the selection is valid.
    fn submit(&mut self) {
        // Make sure the selected item or entered file name is valid.
        if !self.is_selection_valid() {
            return;
        }

        self.config.storage.last_picked_dir = self.current_directory().map(PathBuf::from);

        match &self.mode {
            DialogMode::PickDirectory | DialogMode::PickFile => {
                // Should always contain a value since `is_selection_valid` is used to
                // validate the selection.
                if let Some(item) = self.selected_item.clone() {
                    self.state = DialogState::Picked(item.to_path_buf());
                }
            }
            DialogMode::PickMultiple => {
                let result: Vec<PathBuf> = self
                    .selected_entries()
                    .map(crate::DirectoryEntry::to_path_buf)
                    .collect();

                self.state = DialogState::PickedMultiple(result);
            }
            DialogMode::SaveFile => {
                // Should always contain a value since `is_selection_valid` is used to
                // validate the selection.
                if let Some(path) = self.current_directory() {
                    let full_path = path.join(&self.file_name_input);
                    self.submit_save_file(full_path);
                }
            }
        }
    }

    /// Submits the file dialog with the specified path and opens the `OverwriteFileModal`
    /// if the path already exists.
    fn submit_save_file(&mut self, path: PathBuf) {
        if path.exists() {
            self.open_modal(Box::new(OverwriteFileModal::new(path)));

            return;
        }

        self.state = DialogState::Picked(path);
    }

    /// Cancels the dialog.
    fn cancel(&mut self) {
        self.state = DialogState::Cancelled;
    }

    /// This function generates the initial directory based on the configuration.
    /// The function does the following things:
    ///   - Get the path to open based on the opening mode
    ///   - Canonicalize the path if enabled
    ///   - Attempts to use the parent directory if the path is a file
    fn get_initial_directory(&self) -> PathBuf {
        let path = match self.config.opening_mode {
            OpeningMode::AlwaysInitialDir => &self.config.initial_directory,
            OpeningMode::LastVisitedDir => self
                .config
                .storage
                .last_visited_dir
                .as_deref()
                .unwrap_or(&self.config.initial_directory),
            OpeningMode::LastPickedDir => self
                .config
                .storage
                .last_picked_dir
                .as_deref()
                .unwrap_or(&self.config.initial_directory),
        };

        let mut path = self.canonicalize_path(path);

        if self.config.file_system.is_file(&path) {
            if let Some(parent) = path.parent() {
                path = parent.to_path_buf();
            }
        }

        path
    }

    /// Gets the currently open directory.
    fn current_directory(&self) -> Option<&Path> {
        if let Some(x) = self.directory_stack.iter().nth_back(self.directory_offset) {
            return Some(x.as_path());
        }

        None
    }

    /// Checks whether the selection or the file name entered is valid.
    /// What is checked depends on the mode the dialog is currently in.
    fn is_selection_valid(&self) -> bool {
        match &self.mode {
            DialogMode::PickDirectory => self
                .selected_item
                .as_ref()
                .is_some_and(crate::DirectoryEntry::is_dir),
            DialogMode::PickFile => self
                .selected_item
                .as_ref()
                .is_some_and(DirectoryEntry::is_file),
            DialogMode::PickMultiple => self.get_dir_content_filtered_iter().any(|p| p.selected),
            DialogMode::SaveFile => self.file_name_input_error.is_none(),
        }
    }

    /// Validates the file name entered by the user.
    ///
    /// Returns None if the file name is valid. Otherwise returns an error message.
    fn validate_file_name_input(&self) -> Option<String> {
        if self.file_name_input.is_empty() {
            return Some(self.config.labels.err_empty_file_name.clone());
        }

        if let Some(x) = self.current_directory() {
            let mut full_path = x.to_path_buf();
            full_path.push(self.file_name_input.as_str());

            if self.config.file_system.is_dir(&full_path) {
                return Some(self.config.labels.err_directory_exists.clone());
            }

            if !self.config.allow_file_overwrite && self.config.file_system.is_file(&full_path) {
                return Some(self.config.labels.err_file_exists.clone());
            }
        } else {
            // There is most likely a bug in the code if we get this error message!
            return Some("Currently not in a directory".to_string());
        }

        None
    }

    /// Marks the given item as the selected directory item.
    /// Also updates the `file_name_input` to the name of the selected item.
    fn select_item(&mut self, item: &mut DirectoryEntry) {
        if self.mode == DialogMode::PickMultiple {
            item.selected = true;
        }
        self.selected_item = Some(item.clone());

        if self.mode == DialogMode::SaveFile && item.is_file() {
            self.file_name_input = item.file_name().to_string();
            self.file_name_input_error = self.validate_file_name_input();
        }
    }

    /// Attempts to select the last visible item in `directory_content` before the specified item.
    ///
    /// Returns true if an item is found and selected.
    /// Returns false if no visible item is found before the specified item.
    fn select_next_visible_item_before(&mut self, item: &DirectoryEntry) -> bool {
        let mut return_val = false;

        self.directory_content.reset_multi_selection();

        let mut directory_content = std::mem::take(&mut self.directory_content);
        let search_value = std::mem::take(&mut self.search_value);

        let index = directory_content
            .filtered_iter(&search_value)
            .position(|p| p.path_eq(item));

        if let Some(index) = index {
            if index != 0 {
                if let Some(item) = directory_content
                    .filtered_iter_mut(&search_value)
                    .nth(index.saturating_sub(1))
                {
                    self.select_item(item);
                    self.scroll_to_selection = true;
                    return_val = true;
                }
            }
        }

        self.directory_content = directory_content;
        self.search_value = search_value;

        return_val
    }

    /// Attempts to select the last visible item in `directory_content` after the specified item.
    ///
    /// Returns true if an item is found and selected.
    /// Returns false if no visible item is found after the specified item.
    fn select_next_visible_item_after(&mut self, item: &DirectoryEntry) -> bool {
        let mut return_val = false;

        self.directory_content.reset_multi_selection();

        let mut directory_content = std::mem::take(&mut self.directory_content);
        let search_value = std::mem::take(&mut self.search_value);

        let index = directory_content
            .filtered_iter(&search_value)
            .position(|p| p.path_eq(item));

        if let Some(index) = index {
            if let Some(item) = directory_content
                .filtered_iter_mut(&search_value)
                .nth(index.saturating_add(1))
            {
                self.select_item(item);
                self.scroll_to_selection = true;
                return_val = true;
            }
        }

        self.directory_content = directory_content;
        self.search_value = search_value;

        return_val
    }

    /// Tries to select the first visible item inside `directory_content`.
    fn select_first_visible_item(&mut self) {
        self.directory_content.reset_multi_selection();

        let mut directory_content = std::mem::take(&mut self.directory_content);

        if let Some(item) = directory_content
            .filtered_iter_mut(&self.search_value.clone())
            .next()
        {
            self.select_item(item);
            self.scroll_to_selection = true;
        }

        self.directory_content = directory_content;
    }

    /// Tries to select the last visible item inside `directory_content`.
    fn select_last_visible_item(&mut self) {
        self.directory_content.reset_multi_selection();

        let mut directory_content = std::mem::take(&mut self.directory_content);

        if let Some(item) = directory_content
            .filtered_iter_mut(&self.search_value.clone())
            .last()
        {
            self.select_item(item);
            self.scroll_to_selection = true;
        }

        self.directory_content = directory_content;
    }

    /// Opens the text field in the top panel to text edit the current path.
    fn open_path_edit(&mut self) {
        let path = self.current_directory().map_or_else(String::new, |path| {
            path.to_str().unwrap_or_default().to_string()
        });

        self.path_edit_value = path;
        self.path_edit_activate = true;
        self.path_edit_visible = true;
    }

    /// Loads the directory from the path text edit.
    fn submit_path_edit(&mut self) {
        self.close_path_edit();

        let path = self.canonicalize_path(&PathBuf::from(&self.path_edit_value));

        if self.mode == DialogMode::PickFile && self.config.file_system.is_file(&path) {
            self.state = DialogState::Picked(path);
            return;
        }

        // Assume the user wants to save the given path when
        //   - an extension to the file name is given or the path
        //     edit is allowed to save a file without extension,
        //   - the path is not an existing directory,
        //   - and the parent directory exists
        // Otherwise we will assume the user wants to open the path as a directory.
        if self.mode == DialogMode::SaveFile
            && (path.extension().is_some()
                || self.config.allow_path_edit_to_save_file_without_extension)
            && !self.config.file_system.is_dir(&path)
            && path.parent().is_some_and(std::path::Path::exists)
        {
            self.submit_save_file(path);
            return;
        }

        self.load_directory(&path);
    }

    /// Closes the text field at the top to edit the current path without loading
    /// the entered directory.
    fn close_path_edit(&mut self) {
        self.path_edit_visible = false;
    }

    /// Loads the next directory in the `directory_stack`.
    /// If `directory_offset` is 0 and there is no other directory to load, `Ok()` is returned and
    /// nothing changes.
    /// Otherwise, the result of the directory loading operation is returned.
    fn load_next_directory(&mut self) {
        if self.directory_offset == 0 {
            // There is no next directory that can be loaded
            return;
        }

        self.directory_offset -= 1;

        // Copy path and load directory
        if let Some(path) = self.current_directory() {
            self.load_directory_content(path.to_path_buf().as_path());
        }
    }

    /// Loads the previous directory the user opened.
    /// If there is no previous directory left, `Ok()` is returned and nothing changes.
    /// Otherwise, the result of the directory loading operation is returned.
    fn load_previous_directory(&mut self) {
        if self.directory_offset + 1 >= self.directory_stack.len() {
            // There is no previous directory that can be loaded
            return;
        }

        self.directory_offset += 1;

        // Copy path and load directory
        if let Some(path) = self.current_directory() {
            self.load_directory_content(path.to_path_buf().as_path());
        }
    }

    /// Loads the parent directory of the currently open directory.
    /// If the directory doesn't have a parent, `Ok()` is returned and nothing changes.
    /// Otherwise, the result of the directory loading operation is returned.
    fn load_parent_directory(&mut self) {
        if let Some(x) = self.current_directory() {
            if let Some(x) = x.to_path_buf().parent() {
                self.load_directory(x);
            }
        }
    }

    /// Reloads the currently open directory.
    /// If no directory is currently open, `Ok()` will be returned.
    /// Otherwise, the result of the directory loading operation is returned.
    ///
    /// In most cases, this function should not be called directly.
    /// Instead, `refresh` should be used to reload all other data like system disks too.
    fn reload_directory(&mut self) {
        if let Some(x) = self.current_directory() {
            self.load_directory_content(x.to_path_buf().as_path());
        }
    }

    /// Loads the given directory and updates the `directory_stack`.
    /// The function deletes all directories from the `directory_stack` that are currently
    /// stored in the vector before the `directory_offset`.
    ///
    /// The function also sets the loaded directory as the selected item.
    fn load_directory(&mut self, path: &Path) {
        // Do not load the same directory again.
        // Use reload_directory if the content of the directory should be updated.
        if let Some(x) = self.current_directory() {
            if x == path {
                return;
            }
        }

        if self.directory_offset != 0 && self.directory_stack.len() > self.directory_offset {
            self.directory_stack
                .drain(self.directory_stack.len() - self.directory_offset..);
        }

        self.directory_stack.push(path.to_path_buf());
        self.directory_offset = 0;

        self.load_directory_content(path);

        // Clear the entry filter buffer.
        // It's unlikely the user wants to keep the current filter when entering a new directory.
        self.search_value.clear();
    }

    /// Loads the directory content of the given path.
    fn load_directory_content(&mut self, path: &Path) {
        self.config.storage.last_visited_dir = Some(path.to_path_buf());

        self.directory_content = DirectoryContent::from_path(
            &self.config,
            path,
            self.show_files,
            self.get_selected_file_filter(),
            self.config.file_system.clone(),
        );

        self.create_directory_dialog.close();
        self.scroll_to_selection = true;

        if self.mode == DialogMode::SaveFile {
            self.file_name_input_error = self.validate_file_name_input();
        }
    }
}