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
use std::path::{Path, PathBuf};
use std::{fs, io};

use egui::text::{CCursor, CCursorRange};

use crate::config::{FileDialogConfig, FileDialogLabels, Filter, QuickAccess};
use crate::create_directory_dialog::CreateDirectoryDialog;
use crate::data::{DirectoryContent, DirectoryEntry, Disk, Disks, UserDirectories};

/// 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 file
    SelectFile,

    /// When the dialog is currently used to select a directory
    SelectDirectory,

    /// 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.
    Selected(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("Select a file").clicked() {
///             self.file_dialog.select_file();
///         }
///
///         if let Some(path) = self.file_dialog.update(ctx).selected() {
///             println!("Selected file: {:?}", path);
///         }
///     }
/// }
/// ```
pub struct FileDialog {
    /// The configuration of the file dialog
    config: FileDialogConfig,

    /// 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 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,
    /// This variable contains the error message if an error occurred while loading the directory.
    directory_error: Option<String>,

    /// 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 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 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,
}

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

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

    /// Creates a new file dialog instance with default values.
    pub fn new() -> Self {
        Self {
            config: FileDialogConfig::default(),

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

            user_directories: UserDirectories::new(true),
            system_disks: Disks::new_with_refreshed_list(true),

            directory_stack: vec![],
            directory_offset: 0,
            directory_content: DirectoryContent::new(),
            directory_error: None,

            create_directory_dialog: CreateDirectoryDialog::new(),

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

            selected_item: None,
            file_name_input: String::new(),
            file_name_input_error: None,

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

    /// Creates a new file dialog object and initializes it with the specified configuration.
    pub fn with_config(config: FileDialogConfig) -> Self {
        Self::new().overwrite_config(config)
    }

    // -------------------------------------------------
    // 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,
    ///
    ///     selected_file_a: Option<PathBuf>,
    ///     selected_file_b: Option<PathBuf>,
    /// }
    ///
    /// impl MyApp {
    ///     fn update(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {
    ///         if ui.button("Select file a").clicked() {
    ///             let _ = self.file_dialog.open(DialogMode::SelectFile, true, Some("select_a"));
    ///         }
    ///
    ///         if ui.button("Select file b").clicked() {
    ///             let _ = self.file_dialog.open(DialogMode::SelectFile, true, Some("select_b"));
    ///         }
    ///
    ///         self.file_dialog.update(ctx);
    ///
    ///         if let Some(path) = self.file_dialog.selected() {
    ///             if self.file_dialog.operation_id() == Some("select_a") {
    ///                 self.selected_file_a = Some(path.to_path_buf());
    ///             }
    ///             if self.file_dialog.operation_id() == Some("select_b") {
    ///                 self.selected_file_b = Some(path.to_path_buf());
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    pub fn open(
        &mut self,
        mode: DialogMode,
        mut show_files: bool,
        operation_id: Option<&str>,
    ) -> io::Result<()> {
        self.reset();

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

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

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

        self.load_directory(&self.gen_initial_directory(&self.config.initial_directory))
    }

    /// Shortcut function to open the file dialog to prompt the user to select 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 select_directory(&mut self) {
        let _ = self.open(DialogMode::SelectDirectory, false, None);
    }

    /// Shortcut function to open the file dialog to prompt the user to select 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 select_file(&mut self) {
        let _ = self.open(DialogMode::SelectFile, false, 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) {
        let _ = 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_ui(ctx);

        self
    }

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

    /// Overwrites the configuration of the file dialog.
    ///
    /// This is useful when you want to configure multiple `FileDialog` objects with the
    /// same configuration. If you only want to configure a single object,
    /// it's probably easier to use the setter methods like `FileDialog::initial_directory`
    /// or `FileDialog::default_pos`.
    ///
    /// If you want to create a new FileDialog object with a config,
    /// you probably want to use `FileDialog::with_config`.
    ///
    /// NOTE: Any configuration that was set before `FileDialog::overwrite_config`
    /// will be overwritten! \
    /// This means, for example, that the following code is invalid:
    /// ```
    /// pub use egui_file_dialog::{FileDialog, FileDialogConfig};
    ///
    /// fn create_file_dialog() -> FileDialog {
    ///     FileDialog::new()
    ///        .title("Hello world")
    ///         // This will overwrite `.title("Hello world")`!
    ///        .overwrite_config(FileDialogConfig::default())
    /// }
    ///
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use egui_file_dialog::{FileDialog, FileDialogConfig};
    ///
    /// struct MyApp {
    ///     file_dialog_a: FileDialog,
    ///     file_dialog_b: FileDialog,
    /// }
    ///
    /// impl MyApp {
    ///     pub fn new() -> Self {
    ///         let config = FileDialogConfig {
    ///             default_size: egui::Vec2::new(500.0, 500.0),
    ///             resizable: false,
    ///             movable: false,
    ///             ..Default::default()
    ///         };
    ///
    ///         Self {
    ///             file_dialog_a: FileDialog::new()
    ///                 .overwrite_config(config.clone())
    ///                 .title("File Dialog A")
    ///                 .id("fd_a"),
    ///
    ///             file_dialog_b: FileDialog::new()
    ///                 .overwrite_config(config)
    ///                 .title("File Dialog B")
    ///                 .id("fd_b"),
    ///         }
    ///     }
    /// }
    /// ```
    pub fn overwrite_config(mut self, config: FileDialogConfig) -> Self {
        self.config = config;
        self
    }

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

    /// 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 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.clone();
        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 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 fn canonicalize_paths(mut self, canonicalize: bool) -> Self {
        self.config.canonicalize_paths = canonicalize;
        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
    }

    /// 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 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 fn movable(mut self, movable: bool) -> Self {
        self.config.movable = movable;
        self
    }

    /// Sets if the title bar of the window is shown.
    pub 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 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 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 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 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 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 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 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 reload button should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub fn show_reload_button(mut self, show_reload_button: bool) -> Self {
        self.config.show_reload_button = show_reload_button;
        self
    }

    /// Sets whether the search input should be visible in the top panel.
    ///
    /// Has no effect when `FileDialog::show_top_panel` is disabled.
    pub 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 fn show_left_panel(mut self, show_left_panel: bool) -> Self {
        self.config.show_left_panel = show_left_panel;
        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 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 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 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 selected, 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 selected(&self) -> Option<&Path> {
        match &self.state {
            DialogState::Selected(path) => Some(path),
            _ => None,
        }
    }

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

    /// 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 fn mode(&self) -> DialogMode {
        self.mode
    }

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

/// UI methods
impl FileDialog {
    /// Main update method of the UI
    fn update_ui(&mut self, ctx: &egui::Context) {
        let mut is_open = true;

        self.create_window(&mut is_open).show(ctx, |ui| {
            if self.config.show_top_panel {
                egui::TopBottomPanel::top("fe_top_panel")
                    .resizable(false)
                    .show_inside(ui, |ui| {
                        self.ui_update_top_panel(ui);
                    });
            }

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

            egui::TopBottomPanel::bottom("fe_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);
            });
        });

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

    /// Creates a new egui window with the configured options.
    fn create_window<'a>(&self, is_open: &'a mut bool) -> egui::Window<'a> {
        let window_title = match &self.config.title {
            Some(title) => title,
            None => match &self.mode {
                DialogMode::SelectDirectory => &self.config.labels.title_select_directory,
                DialogMode::SelectFile => &self.config.labels.title_select_file,
                DialogMode::SaveFile => &self.config.labels.title_save_file,
            },
        };

        let mut window = egui::Window::new(window_title)
            .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(id) = self.config.id {
            window = window.id(id);
        }

        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
    }

    /// 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 reload button and search input
            if self.config.show_reload_button {
                path_display_width -= BUTTON_SIZE.x + ui.style().spacing.item_spacing.x * 2.5;
            }

            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);
            }

            // Reload button
            if self.config.show_reload_button
                && ui.add_sized(BUTTON_SIZE, egui::Button::new("⟲")).clicked()
            {
                self.refresh();
            }

            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) {
                    let _ = 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,
            )
        {
            let _ = self.load_previous_directory();
        }

        if self.config.show_forward_button
            && self.ui_button_sized(ui, self.directory_offset != 0, *button_size, "⏵", None)
        {
            let _ = 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,
            )
        {
            if let Some(x) = self.current_directory() {
                self.create_directory_dialog.open(x.to_path_buf());
            }
        }
    }

    /// 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.0))
            .rounding(egui::Rounding::from(4.0))
            .show(ui, |ui| {
                const EDIT_BUTTON_SIZE: egui::Vec2 = egui::Vec2::new(22.0, 20.0);

                match self.path_edit_visible {
                    true => self.ui_update_path_edit(ui, width, EDIT_BUTTON_SIZE),
                    false => 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 mut max_width: f32 = width;

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

        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;

                    let mut path = PathBuf::new();

                    if let Some(data) = self.current_directory() {
                        #[cfg(windows)]
                        let mut drive_letter = String::from("\\");

                        for (i, segment) in data.iter().enumerate() {
                            path.push(segment);

                            #[cfg(windows)]
                            let mut file_name = segment.to_str().unwrap_or("<ERR>");

                            #[cfg(windows)]
                            {
                                // Skip the path namespace prefix generated by
                                // fs::canonicalize() on Windows
                                if i == 0 {
                                    drive_letter = file_name.replace(r"\\?\", "");
                                    continue;
                                }

                                // Replace the root segment with the disk letter
                                if i == 1 && segment == "\\" {
                                    file_name = drive_letter.as_str();
                                } else if i != 0 {
                                    ui.label(self.config.directory_separator.as_str());
                                }
                            }

                            #[cfg(not(windows))]
                            let file_name = segment.to_str().unwrap_or("<ERR>");

                            #[cfg(not(windows))]
                            if i != 0 {
                                ui.label(self.config.directory_separator.as_str());
                            }

                            if ui.button(file_name).clicked() {
                                let _ = 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 =
            width - edit_button_size.x - ui.style().spacing.item_spacing.x * 2.0;

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

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

        if response.lost_focus() && ui.ctx().input(|input| input.key_pressed(egui::Key::Enter)) {
            self.path_edit_request_focus = true;
            self.load_path_edit_directory(false);
        } else if !response.has_focus() {
            self.path_edit_visible = false;
        }

        if ui
            .add_sized(edit_button_size, egui::Button::new("✔"))
            .clicked()
        {
            self.load_path_edit_directory(true);
        }
    }

    /// 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.0, 4.0))
            .rounding(egui::Rounding::from(4.0))
            .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("🔍");
                    let re = ui.add_sized(
                        egui::Vec2::new(ui.available_width(), 0.0),
                        egui::TextEdit::singleline(&mut self.search_value),
                    );
                    self.edit_filter_on_text_input(ui, re);
                });
            });
    }

    /// Focuses and types into the filter 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_filter_on_text_input(&mut self, ui: &mut egui::Ui, re: egui::Response) {
        let any_focused = ui.memory(|mem| mem.focused().is_some());
        if any_focused {
            return;
        }
        // Whether to activate the text input widget
        let mut activate = false;
        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);
                activate = true;
            }
        });
        if activate {
            // Focus the filter input widget
            re.request_focus();
            // Set the cursor to the end of the filter input string
            if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), re.id) {
                state
                    .cursor
                    .set_char_range(Some(CCursorRange::one(CCursor::new(
                        self.search_value.len(),
                    ))));
                state.store(ui.ctx(), re.id);
            }
        }
    }

    /// 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| {
            egui::containers::ScrollArea::vertical()
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    let mut spacing = ui.ctx().style().spacing.item_spacing.y * 2.0;

                    // 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 * 4.0;
                    }

                    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 * 4.0;
                    }

                    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 * 4.0;
                    }

                    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 * 4.0;
                    }

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

    /// 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 {
            if ui
                .selectable_label(
                    self.current_directory() == Some(&entry.path),
                    &entry.display_name,
                )
                .clicked()
            {
                let _ = self.load_directory(&entry.path);
            }
        }
    }

    /// Updates the list of the 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 {
        if let Some(dirs) = self.user_directories.clone() {
            ui.add_space(spacing);
            ui.label(self.config.labels.heading_places.as_str());

            if let Some(path) = dirs.home_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.home_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }

            if let Some(path) = dirs.desktop_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.desktop_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }
            if let Some(path) = dirs.document_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.documents_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }
            if let Some(path) = dirs.download_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.downloads_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }
            if let Some(path) = dirs.audio_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.audio_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }
            if let Some(path) = dirs.picture_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.pictures_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }
            if let Some(path) = dirs.video_dir() {
                if ui
                    .selectable_label(
                        self.current_directory() == Some(path),
                        self.config.labels.videos_dir.as_str(),
                    )
                    .clicked()
                {
                    let _ = self.load_directory(path);
                }
            }

            return true;
        }

        false
    }

    /// 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 = match device.is_removable() {
            true => format!(
                "{}  {}",
                self.config.removable_device_icon,
                device.display_name()
            ),
            false => format!("{}  {}", self.config.device_icon, device.display_name()),
        };

        if ui.selectable_label(false, label).clicked() {
            let _ = self.load_directory(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) {
        ui.add_space(5.0);

        self.ui_update_selection_preview(ui);

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

        self.ui_update_action_buttons(ui);
    }

    /// Updates the selection preview like "Selected directory: X"
    fn ui_update_selection_preview(&mut self, ui: &mut egui::Ui) {
        ui.horizontal(|ui| {
            match &self.mode {
                DialogMode::SelectDirectory => {
                    ui.label(self.config.labels.selected_directory.as_str())
                }
                DialogMode::SelectFile => ui.label(self.config.labels.selected_file.as_str()),
                DialogMode::SaveFile => ui.label(self.config.labels.file_name.as_str()),
            };

            match &self.mode {
                DialogMode::SelectDirectory | DialogMode::SelectFile => {
                    if self.is_selection_valid() {
                        if let Some(x) = &self.selected_item {
                            use egui::containers::scroll_area::ScrollBarVisibility;

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

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

    /// Updates the action buttons like save, open and cancel
    fn ui_update_action_buttons(&mut self, ui: &mut egui::Ui) {
        const BUTTON_SIZE: egui::Vec2 = egui::Vec2::new(78.0, 20.0);

        ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
            let label = match &self.mode {
                DialogMode::SelectDirectory | DialogMode::SelectFile => {
                    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(),
            ) {
                match &self.mode {
                    DialogMode::SelectDirectory | DialogMode::SelectFile => {
                        // self.selected_item should always contain a value,
                        // since self.is_selection_valid() validates the selection and
                        // returns false if the selection is none.
                        if let Some(selection) = self.selected_item.clone() {
                            self.finish(selection.to_path_buf());
                        }
                    }
                    DialogMode::SaveFile => {
                        // self.current_directory should always contain a value,
                        // since self.is_selection_valid() makes sure there is no
                        // file_name_input_error. The file_name_input_error
                        // gets validated every time something changes
                        // by the validate_file_name_input, which sets an error
                        // if we are currently not in a directory.
                        if let Some(path) = self.current_directory() {
                            let mut full_path = path.to_path_buf();
                            full_path.push(&self.file_name_input);

                            self.finish(full_path);
                        }
                    }
                }
            }

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

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

    /// Updates the central panel, including the list of items in the currently open directory.
    fn ui_update_central_panel(&mut self, ui: &mut egui::Ui) {
        if let Some(err) = &self.directory_error {
            ui.centered_and_justified(|ui| {
                ui.colored_label(
                    ui.style().visuals.error_fg_color,
                    format!("{} {}", self.config.err_icon, err),
                );
            });
            return;
        }

        ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
            egui::containers::ScrollArea::vertical()
                .auto_shrink([false, false])
                .show(ui, |ui| {
                    // Temporarily take ownership of the directory contents to be able to
                    // update it in the for loop using load_directory.
                    // Otherwise we would get an error that `*self` cannot be borrowed as mutable
                    // more than once at a time.
                    // Make sure to return the function after updating the directory_content,
                    // otherwise the change will be overwritten with the last statement
                    // of the function.
                    let data = std::mem::take(&mut self.directory_content);

                    for path in data.iter() {
                        let file_name = path.file_name();

                        if !self.search_value.is_empty()
                            && !file_name
                                .to_lowercase()
                                .contains(&self.search_value.to_lowercase())
                        {
                            continue;
                        }

                        let mut selected = false;
                        if let Some(x) = &self.selected_item {
                            selected = x == path;
                        }

                        let response =
                            ui.selectable_label(selected, format!("{} {}", path.icon(), file_name));

                        if selected && self.scroll_to_selection {
                            response.scroll_to_me(Some(egui::Align::Center));
                        }

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

                        if response.double_clicked() {
                            if path.is_dir() {
                                let _ = self.load_directory(&path.to_path_buf());
                                return;
                            }

                            self.select_item(path);

                            if self.is_selection_valid() {
                                // self.selected_item should always contain a value
                                // since self.is_selection_valid() validates the selection
                                // and returns false if the selection is none.
                                if let Some(selection) = self.selected_item.clone() {
                                    self.finish(selection.to_path_buf());
                                }
                            }
                        }
                    }

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

                    if let Some(path) = self
                        .create_directory_dialog
                        .update(ui, &self.config)
                        .directory()
                    {
                        let entry = DirectoryEntry::from_path(&self.config, &path);

                        self.directory_content.push(entry.clone());
                        self.select_item(&entry);
                    }
                });
        });
    }

    /// 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
    }
}

/// Implementation
impl FileDialog {
    /// 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 {
        match self.config.canonicalize_paths {
            true => fs::canonicalize(path).unwrap_or(path.to_path_buf()),
            false => path.to_path_buf(),
        }
    }

    /// Resets the dialog to use default values.
    /// Configuration variables such as `initial_directory` are retained.
    fn reset(&mut self) {
        self.state = DialogState::Closed;
        self.show_files = true;
        self.operation_id = None;

        self.user_directories = UserDirectories::new(self.config.canonicalize_paths);
        self.system_disks = Disks::new_with_refreshed_list(self.config.canonicalize_paths);

        self.directory_stack = vec![];
        self.directory_offset = 0;
        self.directory_content = DirectoryContent::new();
        self.directory_error = None;

        self.create_directory_dialog = CreateDirectoryDialog::new();

        self.selected_item = None;
        self.file_name_input = String::new();

        self.scroll_to_selection = false;
        self.search_value = String::new();
    }

    /// Refreshes the dialog.
    /// Including the user directories, system disks and currently open directory.
    fn refresh(&mut self) {
        self.user_directories = UserDirectories::new(self.config.canonicalize_paths);
        self.system_disks = Disks::new_with_refreshed_list(self.config.canonicalize_paths);

        let _ = self.reload_directory();
    }

    /// Finishes the dialog.
    /// `selected_item`` is the item that was selected by the user.
    fn finish(&mut self, selected_item: PathBuf) {
        self.state = DialogState::Selected(selected_item);
    }

    /// 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:
    ///   - Canonicalize the path if enabled
    ///   - Attempts to use the parent directory if the path is a file
    fn gen_initial_directory(&self, path: &Path) -> PathBuf {
        let mut path = self.canonicalize_path(path);

        if path.is_file() {
            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 {
        if let Some(selection) = &self.selected_item {
            return match &self.mode {
                DialogMode::SelectDirectory => selection.is_dir(),
                DialogMode::SelectFile => selection.is_file(),
                DialogMode::SaveFile => self.file_name_input_error.is_none(),
            };
        }

        if self.mode == DialogMode::SaveFile && self.file_name_input_error.is_none() {
            return true;
        }

        false
    }

    /// 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 full_path.is_dir() {
                return Some(self.config.labels.err_directory_exists.clone());
            }
            if full_path.is_file() {
                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, dir_entry: &DirectoryEntry) {
        self.selected_item = Some(dir_entry.clone());

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

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

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

    /// Loads the directory from the path text edit.
    fn load_path_edit_directory(&mut self, close_text_edit: bool) {
        if close_text_edit {
            self.path_edit_visible = false;
        }

        let _ = self.load_directory(&self.canonicalize_path(&PathBuf::from(&self.path_edit_value)));
    }

    /// 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) -> io::Result<()> {
        if self.directory_offset == 0 {
            // There is no next directory that can be loaded
            return Ok(());
        }

        self.directory_offset -= 1;

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

        Ok(())
    }

    /// 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) -> io::Result<()> {
        if self.directory_offset + 1 >= self.directory_stack.len() {
            // There is no previous directory that can be loaded
            return Ok(());
        }

        self.directory_offset += 1;

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

        Ok(())
    }

    /// 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) -> io::Result<()> {
        if let Some(x) = self.current_directory() {
            if let Some(x) = x.to_path_buf().parent() {
                return self.load_directory(x);
            }
        }

        Ok(())
    }

    /// 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.
    fn reload_directory(&mut self) -> io::Result<()> {
        if let Some(x) = self.current_directory() {
            return self.load_directory_content(x.to_path_buf().as_path());
        }

        Ok(())
    }

    /// 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) -> io::Result<()> {
        // 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 Ok(());
            }
        }

        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)?;

        let dir_entry = DirectoryEntry::from_path(&self.config, path);
        self.select_item(&dir_entry);

        // 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();

        Ok(())
    }

    /// Loads the directory content of the given path.
    fn load_directory_content(&mut self, path: &Path) -> io::Result<()> {
        self.directory_error = None;

        self.directory_content =
            match DirectoryContent::from_path(&self.config, path, self.show_files) {
                Ok(content) => content,
                Err(err) => {
                    self.directory_error = Some(err.to_string());
                    return Err(err);
                }
            };

        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();
        }

        Ok(())
    }
}