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

use crate::config::{FileDialogConfig, FileFilter};

/// Contains the metadata of a directory item.
/// This struct is mainly there so that the metadata can be loaded once and not that
/// a request has to be sent to the OS every frame using, for example, `path.is_file()`.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct DirectoryEntry {
    path: PathBuf,
    is_directory: bool,
    is_system_file: bool,
    icon: String,
    /// If the item is marked as selected as part of a multi selection.
    pub selected: bool,
}

impl DirectoryEntry {
    /// Creates a new directory entry from a path
    pub fn from_path(config: &FileDialogConfig, path: &Path) -> Self {
        Self {
            path: path.to_path_buf(),
            is_directory: path.is_dir(),
            is_system_file: !path.is_dir() && !path.is_file(),
            icon: gen_path_icon(config, path),
            selected: false,
        }
    }

    /// Checks if the path of the current directory entry matches the other directory entry.
    pub fn path_eq(&self, other: &DirectoryEntry) -> bool {
        other.as_path() == self.as_path()
    }

    /// Returns true if the item is a directory.
    /// False is returned if the item is a file or the path did not exist when the
    /// DirectoryEntry object was created.
    pub fn is_dir(&self) -> bool {
        self.is_directory
    }

    /// Returns true if the item is a file.
    /// False is returned if the item is a directory or the path did not exist when the
    /// DirectoryEntry object was created.
    pub fn is_file(&self) -> bool {
        !self.is_directory
    }

    /// Returns true if the item is a system file.
    pub fn is_system_file(&self) -> bool {
        self.is_system_file
    }

    /// Returns the icon of the directory item.
    pub fn icon(&self) -> &str {
        &self.icon
    }

    /// Returns the path of the directory item.
    pub fn as_path(&self) -> &Path {
        &self.path
    }

    /// Clones the path of the directory item.
    pub fn to_path_buf(&self) -> PathBuf {
        self.path.clone()
    }

    /// Returns the file name of the directory item.
    pub fn file_name(&self) -> &str {
        self.path
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or_else(|| {
                // Make sure the root directories like ["C:", "\"] and ["\\?\C:", "\"] are
                // displayed correctly
                #[cfg(windows)]
                if self.path.components().count() == 2 {
                    let path = self
                        .path
                        .iter()
                        .nth(0)
                        .and_then(|seg| seg.to_str())
                        .unwrap_or_default();

                    // Skip path namespace prefix if present, for example: "\\?\C:"
                    if path.contains(r"\\?\") {
                        return path.get(4..).unwrap_or(path);
                    }

                    return path;
                }

                // Make sure the root directory "/" is displayed correctly
                #[cfg(not(windows))]
                if self.path.iter().count() == 1 {
                    return self.path.to_str().unwrap_or_default();
                }

                ""
            })
    }

    /// Returns whether the path this DirectoryEntry points to is considered hidden.
    pub fn is_hidden(&self) -> bool {
        is_path_hidden(self)
    }
}

/// Contains the content of a directory.
#[derive(Default)]
pub struct DirectoryContent {
    content: Vec<DirectoryEntry>,
}

impl DirectoryContent {
    /// Create a new object with empty content
    pub fn new() -> Self {
        Self { content: vec![] }
    }

    /// Create a new DirectoryContent object and loads the contents of the given path.
    /// Use include_files to include or exclude files in the content list.
    pub fn from_path(
        config: &FileDialogConfig,
        path: &Path,
        include_files: bool,
    ) -> io::Result<Self> {
        Ok(Self {
            content: load_directory(config, path, include_files)?,
        })
    }

    /// Checks if the given directory entry is visible with the applied filters.
    fn is_entry_visible(
        dir_entry: &DirectoryEntry,
        show_hidden: bool,
        search_value: &str,
        file_filter: Option<&FileFilter>,
    ) -> bool {
        if !search_value.is_empty()
            && !dir_entry
                .file_name()
                .to_lowercase()
                .contains(&search_value.to_lowercase())
        {
            return false;
        }

        if !show_hidden && dir_entry.is_hidden() {
            return false;
        }

        if let Some(file_filter) = file_filter {
            if dir_entry.is_file() && !(file_filter.filter)(dir_entry.as_path()) {
                return false;
            }
        }

        true
    }

    pub fn filtered_iter<'s>(
        &'s self,
        show_hidden: bool,
        search_value: &'s str,
        file_filter: Option<&'s FileFilter>,
    ) -> impl Iterator<Item = &DirectoryEntry> + 's {
        self.content
            .iter()
            .filter(move |p| Self::is_entry_visible(p, show_hidden, search_value, file_filter))
    }

    pub fn filtered_iter_mut<'s>(
        &'s mut self,
        show_hidden: bool,
        search_value: &'s str,
        file_filter: Option<&'s FileFilter>,
    ) -> impl Iterator<Item = &mut DirectoryEntry> + 's {
        self.content
            .iter_mut()
            .filter(move |p| Self::is_entry_visible(p, show_hidden, search_value, file_filter))
    }

    pub fn reset_multi_selection(&mut self) {
        for item in self.content.iter_mut() {
            item.selected = false;
        }
    }

    /// Returns the number of elements inside the directory.
    pub fn len(&self) -> usize {
        self.content.len()
    }

    /// Pushes a new item to the content.
    pub fn push(&mut self, item: DirectoryEntry) {
        self.content.push(item);
    }

    /// Clears the items inside the directory.
    pub fn clear(&mut self) {
        self.content.clear();
    }
}

/// Loads the contents of the given directory.
fn load_directory(
    config: &FileDialogConfig,
    path: &Path,
    include_files: bool,
) -> io::Result<Vec<DirectoryEntry>> {
    let paths = fs::read_dir(path)?;

    let mut result: Vec<DirectoryEntry> = Vec::new();
    for path in paths {
        match path {
            Ok(entry) => {
                let entry = DirectoryEntry::from_path(config, entry.path().as_path());

                if entry.is_system_file() {
                    continue;
                }

                if !include_files && entry.is_file() {
                    continue;
                }

                result.push(entry);
            }
            Err(_) => continue,
        };
    }

    result.sort_by(|a, b| match a.is_dir() == b.is_dir() {
        true => a.file_name().cmp(b.file_name()),
        false => match a.is_dir() {
            true => std::cmp::Ordering::Less,
            false => std::cmp::Ordering::Greater,
        },
    });

    Ok(result)
}

#[cfg(windows)]
fn is_path_hidden(item: &DirectoryEntry) -> bool {
    use std::os::windows::fs::MetadataExt;

    match fs::metadata(item.as_path()) {
        Ok(metadata) => metadata.file_attributes() & 0x2 > 0,
        Err(_) => false,
    }
}

#[cfg(not(windows))]
fn is_path_hidden(item: &DirectoryEntry) -> bool {
    if item.file_name().bytes().next() == Some(b'.') {
        return true;
    }

    false
}

/// Generates the icon for the specific path.
/// The default icon configuration is taken into account, as well as any configured file icon filters.
fn gen_path_icon(config: &FileDialogConfig, path: &Path) -> String {
    for def in &config.file_icon_filters {
        if (def.filter)(path) {
            return def.icon.clone();
        }
    }

    match path.is_dir() {
        true => config.default_folder_icon.clone(),
        false => config.default_file_icon.clone(),
    }
}