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
use egui::{
  vec2, Align2, Context, Id, Key, Layout, Pos2, RichText, ScrollArea, TextEdit, Ui, Vec2, Window,
};
use std::{
  env,
  fmt::Debug,
  fs,
  io::Error,
  path::{Path, PathBuf},
};

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
/// Dialog state.
pub enum State {
  /// Is currently visible.
  Open,
  /// Is currently not visible.
  Closed,
  /// Was canceled.
  Cancelled,
  /// File was selected.
  Selected,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Dialog type.
pub enum DialogType {
  SelectFolder,
  OpenFile,
  SaveFile,
}

/// `egui` component that represents `OpenFileDialog` or `SaveFileDialog`.
pub struct FileDialog {
  /// Current opened path.
  path: PathBuf,

  /// Editable field with path.
  path_edit: String,

  /// Selected file path
  selected_file: Option<FileInfo>,

  /// Editable field with filename.
  filename_edit: String,

  /// Dialog title text
  title: String,

  /// Files in directory.
  files: Result<Vec<FileInfo>, Error>,

  /// Current dialog state.
  state: State,

  /// Dialog type.
  dialog_type: DialogType,

  id: Option<Id>,
  current_pos: Option<Pos2>,
  default_size: Vec2,
  anchor: Option<(Align2, Vec2)>,
  filter: Option<Filter>,
  edit_focus: Option<EditFocus>,
  resizable: bool,
  rename: bool,
  new_folder: bool,

  /// Show drive letters on Windows.
  #[cfg(windows)]
  show_drives: bool,

  /// Show hidden files on unix systems.
  #[cfg(unix)]
  show_hidden: bool,
}

impl Debug for FileDialog {
  #[cfg(unix)]
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("FileDialog")
      .field("path", &self.path)
      .field("path_edit", &self.path_edit)
      .field("selected_file", &self.selected_file)
      .field("filename_edit", &self.filename_edit)
      .field("files", &self.files)
      .field("state", &self.state)
      .field("dialog_type", &self.dialog_type)
      .field("current_pos", &self.current_pos)
      .field("default_size", &self.default_size)
      .field("anchor", &self.anchor)
      // Closures don't implement std::fmt::Debug.
      // .field("filter", &self.filter)
      // .field("edit_focus", &self.edit_focus)
      .field("resizable", &self.resizable)
      .field("rename", &self.rename)
      .field("new_folder", &self.new_folder)
      .field("show_hidden", &self.show_hidden)
      .finish()
  }

  #[cfg(windows)]
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("FileDialog")
      .field("path", &self.path)
      .field("path_edit", &self.path_edit)
      .field("selected_file", &self.selected_file)
      .field("filename_edit", &self.filename_edit)
      .field("files", &self.files)
      .field("state", &self.state)
      .field("dialog_type", &self.dialog_type)
      .field("current_pos", &self.current_pos)
      .field("default_size", &self.default_size)
      .field("anchor", &self.anchor)
      // Closures don't implement std::fmt::Debug.
      // .field("filter", &self.filter)
      // .field("edit_focus", &self.edit_focus)
      .field("resizable", &self.resizable)
      .field("rename", &self.rename)
      .field("new_folder", &self.new_folder)
      .field("show_drives", &self.show_drives)
      .finish()
  }

  #[cfg(all(not(unix), not(windows)))]
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("FileDialog")
      .field("path", &self.path)
      .field("path_edit", &self.path_edit)
      .field("selected_file", &self.selected_file)
      .field("filename_edit", &self.filename_edit)
      .field("files", &self.files)
      .field("state", &self.state)
      .field("dialog_type", &self.dialog_type)
      .field("current_pos", &self.current_pos)
      .field("default_size", &self.default_size)
      .field("anchor", &self.anchor)
      // Closures don't implement std::fmt::Debug.
      // .field("filter", &self.filter)
      // .field("edit_focus", &self.edit_focus)
      .field("resizable", &self.resizable)
      .field("rename", &self.rename)
      .field("new_folder", &self.new_folder)
      .finish()
  }
}

/// Function that returns `true` if the path is accepted.
pub type Filter = Box<dyn Fn(&Path) -> bool + Send + Sync + 'static>;

pub type EditFocus = Box<dyn Fn(bool) + Send + Sync + 'static>;

impl FileDialog {
  /// Create dialog that prompts the user to select a folder.
  pub fn select_folder(initial_path: Option<PathBuf>) -> Self {
    FileDialog::new(DialogType::SelectFolder, initial_path)
  }

  /// Create dialog that prompts the user to open a file.
  pub fn open_file(initial_path: Option<PathBuf>) -> Self {
    FileDialog::new(DialogType::OpenFile, initial_path)
  }

  /// Create dialog that prompts the user to save a file.
  pub fn save_file(initial_path: Option<PathBuf>) -> Self {
    FileDialog::new(DialogType::SaveFile, initial_path)
  }

  /// Constructs new file dialog. If no `initial_path` is passed,`env::current_dir` is used.
  fn new(dialog_type: DialogType, initial_path: Option<PathBuf>) -> Self {
    let mut path = initial_path.unwrap_or_else(|| env::current_dir().unwrap_or_default());
    let mut filename_edit = String::new();

    if path.is_file() {
      assert!(dialog_type != DialogType::SelectFolder);

      let info = FileInfo {
        path: path.clone(),
        dir: false,
      };

      filename_edit = get_file_name(&info).to_string();
      path.pop();
    }

    let path_edit = path.to_str().unwrap_or_default().to_string();

    Self {
      path,
      path_edit,
      selected_file: None,
      filename_edit,
      title: match dialog_type {
        DialogType::SelectFolder => "📁  Select Folder",
        DialogType::OpenFile => "📂  Open File",
        DialogType::SaveFile => "💾  Save File",
      }
      .to_string(),
      files: Ok(Vec::new()),
      state: State::Closed,
      dialog_type,

      id: None,
      current_pos: None,
      default_size: vec2(512.0, 512.0),
      anchor: None,
      filter: None,
      edit_focus: None,
      resizable: true,
      rename: true,
      new_folder: true,

      #[cfg(windows)]
      show_drives: true,

      #[cfg(unix)]
      show_hidden: false,
    }
  }

  /// Set the default file name.
  pub fn default_filename(mut self, filename: impl Into<String>) -> Self {
    self.filename_edit = filename.into();
    self
  }

  /// Set the window title text.
  pub fn title(mut self, title: &str) -> Self {
    self.title = match self.dialog_type {
      DialogType::SelectFolder => "📁  ",
      DialogType::OpenFile => "📂  ",
      DialogType::SaveFile => "💾  ",
    }
    .to_string()
      + title;
    self
  }

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

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

  /// Set the window position.
  pub fn current_pos(mut self, current_pos: impl Into<Pos2>) -> Self {
    self.current_pos = Some(current_pos.into());
    self
  }

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

  /// Enable/disable resizing the window. Default is `true`.
  pub fn resizable(mut self, resizable: bool) -> Self {
    self.resizable = resizable;
    self
  }

  /// Show the Rename button. Default is `true`.
  pub fn show_rename(mut self, rename: bool) -> Self {
    self.rename = rename;
    self
  }

  /// Show the New Folder button. Default is `true`.
  pub fn show_new_folder(mut self, new_folder: bool) -> Self {
    self.new_folder = new_folder;
    self
  }

  /// Show the mapped drives on Windows. Default is `true`.
  #[cfg(windows)]
  pub fn show_drives(mut self, drives: bool) -> Self {
    self.show_drives = drives;
    self
  }

  /// Set a function to filter shown files.
  pub fn filter(mut self, filter: Filter) -> Self {
    self.filter = Some(filter);
    self
  }

  /// Calls the `edit_focus` function when a text edit gains or looses focus.
  pub fn edit_focus(mut self, edit_focus: EditFocus) -> Self {
    self.edit_focus = Some(edit_focus);
    self
  }

  /// Get the dialog type.
  pub fn dialog_type(&self) -> DialogType {
    self.dialog_type
  }

  /// Get the window's visibility.
  pub fn visible(&self) -> bool {
    self.state == State::Open
  }

  /// Opens the dialog.
  pub fn open(&mut self) {
    self.state = State::Open;
    self.refresh();
  }

  /// Resulting file path.
  pub fn path(&self) -> Option<&Path> {
    self.selected_file.as_ref().map(|info| info.path.as_path())
  }

  /// Set the dialog's current opened path
  pub fn set_path(&mut self, path: impl Into<PathBuf>) {
    self.path = path.into();
    self.refresh();
  }

  /// Dialog state.
  pub fn state(&self) -> State {
    self.state
  }

  /// Returns true, if the file selection was confirmed.
  pub fn selected(&self) -> bool {
    self.state == State::Selected
  }

  fn open_selected(&mut self) {
    if let Some(info) = &self.selected_file {
      if info.dir {
        self.set_path(info.path.clone())
      } else if self.dialog_type == DialogType::OpenFile {
        self.confirm();
      }
    }
  }

  fn confirm(&mut self) {
    self.state = State::Selected;
  }

  fn refresh(&mut self) {
    self.files = self.read_folder();
    self.path_edit = String::from(self.path.to_str().unwrap_or_default());
    self.select(None);
  }

  fn select(&mut self, file: Option<FileInfo>) {
    if let Some(info) = &file {
      self.filename_edit = get_file_name(info).to_owned();
    }
    self.selected_file = file;
  }

  fn can_save(&self) -> bool {
    self.selected_file.is_some() || !self.filename_edit.is_empty()
  }

  fn can_open(&self) -> bool {
    self.selected_file.is_some()
  }

  fn can_rename(&self) -> bool {
    if !self.filename_edit.is_empty() {
      if let Some(file) = &self.selected_file {
        return get_file_name(file) != self.filename_edit;
      }
    }
    false
  }

  /// Shows the dialog if it is open. It is also responsible for state management.
  /// Should be called every ui update.
  pub fn show(&mut self, ctx: &Context) -> &Self {
    self.state = match self.state {
      State::Open => {
        if ctx.input(|state| state.key_pressed(Key::Escape)) {
          self.state = State::Cancelled;
        }

        let mut is_open = true;
        self.ui(ctx, &mut is_open);
        match is_open {
          true => self.state,
          false => State::Cancelled,
        }
      }
      _ => State::Closed,
    };

    self
  }

  fn ui(&mut self, ctx: &Context, is_open: &mut bool) {
    let mut window = Window::new(RichText::new(&self.title).strong())
      .open(is_open)
      .default_size(self.default_size)
      .resizable(self.resizable)
      .collapsible(false);

    if let Some(id) = self.id {
      window = window.id(id);
    }

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

    if let Some(current_pos) = self.current_pos {
      window = window.current_pos(current_pos);
    }

    window.show(ctx, |ui| self.ui_in_window(ui));
  }

  fn ui_in_window(&mut self, ui: &mut Ui) {
    enum Command {
      Cancel,
      CreateDirectory,
      Folder,
      Open(FileInfo),
      OpenSelected,
      Refresh,
      Rename(PathBuf, PathBuf),
      Save(FileInfo),
      Select(FileInfo),
      UpDirectory,
    }
    let mut command: Option<Command> = None;

    // Top directory field with buttons.
    egui::TopBottomPanel::top("egui_file_top").show_inside(ui, |ui| {
      ui.horizontal(|ui| {
        ui.add_enabled_ui(self.path.parent().is_some(), |ui| {
          let response = ui.button("⬆").on_hover_text("Parent Folder");
          if response.clicked() {
            command = Some(Command::UpDirectory);
          }
        });
        ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
          let response = ui.button("⟲").on_hover_text("Refresh");
          if response.clicked() {
            command = Some(Command::Refresh);
          }

          let response = ui.add_sized(
            ui.available_size(),
            TextEdit::singleline(&mut self.path_edit),
          );

          if response.lost_focus() {
            if let Some(edit_focus) = &self.edit_focus {
              edit_focus(false);
            }
            let path = PathBuf::from(&self.path_edit);
            command = Some(Command::Open(FileInfo::new(path)));
          } else if response.gained_focus() {
            if let Some(edit_focus) = &self.edit_focus {
              edit_focus(true);
            }
          }
        });
      });
      ui.add_space(ui.spacing().item_spacing.y);
    });

    // Bottom file field.
    egui::TopBottomPanel::bottom("egui_file_bottom").show_inside(ui, |ui| {
      ui.add_space(ui.spacing().item_spacing.y * 2.0);
      ui.horizontal(|ui| {
        ui.label("File:");
        ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
          if self.new_folder && ui.button("New Folder").clicked() {
            command = Some(Command::CreateDirectory);
          }

          if self.rename {
            ui.add_enabled_ui(self.can_rename(), |ui| {
              if ui.button("Rename").clicked() {
                if let Some(from) = self.selected_file.clone() {
                  let to = from.path.with_file_name(&self.filename_edit);
                  command = Some(Command::Rename(from.path, to));
                }
              }
            });
          }

          let response = ui.add_sized(
            ui.available_size(),
            TextEdit::singleline(&mut self.filename_edit),
          );

          if response.lost_focus() {
            if let Some(edit_focus) = &self.edit_focus {
              edit_focus(false);
            }

            let ctx = response.ctx;
            let enter_pressed = ctx.input(|state| state.key_pressed(egui::Key::Enter));
            if enter_pressed && !self.filename_edit.is_empty() {
              let path = self.path.join(&self.filename_edit);
              match self.dialog_type {
                DialogType::SelectFolder => command = Some(Command::Folder),
                DialogType::OpenFile => {
                  if path.exists() {
                    command = Some(Command::Open(FileInfo::new(path)));
                  }
                }
                DialogType::SaveFile => {
                  command = Some(match path.is_dir() {
                    true => Command::Open(FileInfo { path, dir: true }),
                    false => Command::Save(FileInfo { path, dir: false }),
                  });
                }
              }
            }
          } else if response.gained_focus() {
            if let Some(edit_focus) = &self.edit_focus {
              edit_focus(true);
            }
          }
        });
      });

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

      // Confirm, Cancel buttons.
      ui.horizontal(|ui| {
        match self.dialog_type {
          DialogType::SelectFolder => {
            ui.horizontal(|ui| {
              if ui.button("Open").clicked() {
                command = Some(Command::Folder);
              };
            });
          }
          DialogType::OpenFile => {
            ui.horizontal(|ui| {
              ui.set_enabled(self.can_open());
              if ui.button("Open").clicked() {
                command = Some(Command::OpenSelected);
              };
            });
          }
          DialogType::SaveFile => {
            let should_open_directory = match &self.selected_file {
              Some(file) => file.dir,
              None => false,
            };

            if should_open_directory {
              if ui.button("Open").clicked() {
                command = Some(Command::OpenSelected);
              };
            } else {
              ui.horizontal(|ui| {
                ui.set_enabled(self.can_save());
                if ui.button("Save").clicked() {
                  let filename = &self.filename_edit;
                  let path = self.path.join(filename);
                  command = Some(Command::Save(FileInfo::new(path)));
                };
              });
            }
          }
        }

        if ui.button("Cancel").clicked() {
          command = Some(Command::Cancel);
        }

        #[cfg(unix)]
        ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
          if ui.checkbox(&mut self.show_hidden, "Show Hidden").changed() {
            self.refresh();
          }
        });
      });
    });

    // Rows with files.
    egui::CentralPanel::default().show_inside(ui, |ui| {
      ScrollArea::vertical().show_rows(
        ui,
        ui.text_style_height(&egui::TextStyle::Body),
        self.files.as_ref().map_or(0, |files| files.len()),
        |ui, range| match self.files.as_ref() {
          Ok(files) => {
            ui.with_layout(ui.layout().with_cross_justify(true), |ui| {
              let selected = self.selected_file.as_ref().map(|info| &info.path);
              for info in files[range].iter() {
                let label = match info.dir {
                  true => "🗀 ",
                  false => "🗋 ",
                }
                .to_string()
                  + get_file_name(info);

                let is_selected = Some(&info.path) == selected;
                let response = ui.selectable_label(is_selected, label);
                if response.clicked() {
                  command = Some(Command::Select(info.clone()));
                }

                if response.double_clicked() {
                  command = Some(match self.dialog_type == DialogType::SaveFile {
                    true => match info.dir {
                      true => Command::OpenSelected,
                      false => Command::Save(info.clone()),
                    },
                    false => Command::Open(info.clone()),
                  });
                }
              }
            })
            .response
          }
          Err(e) => ui.label(e.to_string()),
        },
      );
    });

    if let Some(command) = command {
      match command {
        Command::Select(info) => self.select(Some(info)),
        Command::Folder => {
          let path = self.get_folder().to_owned();
          self.selected_file = Some(FileInfo { path, dir: true });
          self.confirm();
        }
        Command::Open(path) => {
          self.select(Some(path));
          self.open_selected();
        }
        Command::OpenSelected => self.open_selected(),
        Command::Save(file) => {
          self.selected_file = Some(file);
          self.confirm();
        }
        Command::Cancel => self.state = State::Cancelled,
        Command::Refresh => self.refresh(),
        Command::UpDirectory => {
          if self.path.pop() {
            self.refresh();
          }
        }
        Command::CreateDirectory => {
          let mut path = self.path.clone();
          let name = match self.filename_edit.is_empty() {
            true => "New folder",
            false => &self.filename_edit,
          };
          path.push(name);
          match fs::create_dir(&path) {
            Ok(_) => {
              self.refresh();
              self.select(Some(FileInfo::new(path)));
              // TODO: scroll to selected?
            }
            Err(err) => println!("Error while creating directory: {err}"),
          }
        }
        Command::Rename(from, to) => match fs::rename(from, &to) {
          Ok(_) => {
            self.refresh();
            self.select(Some(FileInfo::new(to)));
          }
          Err(err) => println!("Error while renaming: {err}"),
        },
      };
    }
  }

  fn get_folder(&self) -> &Path {
    if let Some(info) = &self.selected_file {
      if info.dir {
        return info.path.as_path();
      }
    }

    // No selected file or it's not a folder, so use the current path.
    &self.path
  }

  fn read_folder(&self) -> Result<Vec<FileInfo>, Error> {
    fs::read_dir(&self.path).map(|entries| {
      let mut file_infos: Vec<FileInfo> = entries
        .filter_map(|result| result.ok())
        .filter_map(|entry| {
          let info = FileInfo::new(entry.path());
          if !info.dir {
            // Do not show system files.
            if !info.path.is_file() {
              return None;
            }

            // Filter.
            if let Some(filter) = self.filter.as_ref() {
              if !filter(&info.path) {
                return None;
              }
            } else if self.dialog_type == DialogType::SelectFolder {
              return None;
            }
          }

          #[cfg(unix)]
          if !self.show_hidden && get_file_name(&info).starts_with('.') {
            return None;
          }

          Some(info)
        })
        .collect();

      // Sort keeping folders before files.
      file_infos.sort_by(|a, b| match a.dir == b.dir {
        true => a.path.file_name().cmp(&b.path.file_name()),
        false => b.dir.cmp(&a.dir),
      });

      #[cfg(windows)]
      let file_infos = match self.show_drives {
        true => {
          let drives = get_drives();
          let mut infos = Vec::with_capacity(drives.len() + file_infos.len());
          for drive in drives {
            infos.push(FileInfo {
              path: drive,
              dir: true,
            });
          }
          infos.append(&mut file_infos);
          infos
        }
        false => file_infos,
      };

      file_infos
    })
  }
}

#[derive(Clone, Debug, Default)]
struct FileInfo {
  path: PathBuf,
  dir: bool,
}

impl FileInfo {
  fn new(path: PathBuf) -> Self {
    let dir = path.is_dir();
    Self { path, dir }
  }
}

#[cfg(windows)]
fn get_drives() -> Vec<PathBuf> {
  let mut drive_names = Vec::new();
  let mut drives = unsafe { GetLogicalDrives() };
  let mut letter = b'A';
  while drives > 0 {
    if drives & 1 != 0 {
      drive_names.push(format!("{}:\\", letter as char).into());
    }
    drives >>= 1;
    letter += 1;
  }
  drive_names
}

#[cfg(windows)]
fn is_drive_root(path: &Path) -> bool {
  path
    .to_str()
    .filter(|path| &path[1..] == ":\\")
    .and_then(|path| path.chars().next())
    .map_or(false, |ch| ch.is_ascii_uppercase())
}

fn get_file_name(info: &FileInfo) -> &str {
  #[cfg(windows)]
  if info.dir && is_drive_root(&info.path) {
    return info.path.to_str().unwrap_or_default();
  }
  info
    .path
    .file_name()
    .and_then(|name| name.to_str())
    .unwrap_or_default()
}

#[cfg(windows)]
extern "C" {
  pub fn GetLogicalDrives() -> u32;
}