egui_file_dialog/data/
directory_content.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::config::{FileDialogConfig, FileFilter};
use crate::FileSystem;
use egui::mutex::Mutex;
use std::path::{Path, PathBuf};
use std::sync::{mpsc, Arc};
use std::time::SystemTime;
use std::{io, thread};

/// Contains the metadata of a directory item.
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Metadata {
    pub(crate) size: Option<u64>,
    pub(crate) last_modified: Option<SystemTime>,
    pub(crate) created: Option<SystemTime>,
    pub(crate) file_type: Option<String>,
}

impl Metadata {
    /// Create a new custom metadata
    pub const fn new(
        size: Option<u64>,
        last_modified: Option<SystemTime>,
        created: Option<SystemTime>,
        file_type: Option<String>,
    ) -> Self {
        Self {
            size,
            last_modified,
            created,
            file_type,
        }
    }
}

/// Contains the information of a directory item.
///
/// This struct is mainly there so that the information and 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,
    metadata: Metadata,
    is_directory: bool,
    is_system_file: bool,
    is_hidden: 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, file_system: &dyn FileSystem) -> Self {
        Self {
            path: path.to_path_buf(),
            metadata: file_system.metadata(path).unwrap_or_default(),
            is_directory: file_system.is_dir(path),
            is_system_file: !file_system.is_dir(path) && !file_system.is_file(path),
            icon: gen_path_icon(config, path, file_system),
            is_hidden: file_system.is_path_hidden(path),
            selected: false,
        }
    }

    /// Returns the metadata of the directory entry.
    pub const fn metadata(&self) -> &Metadata {
        &self.metadata
    }

    /// Checks if the path of the current directory entry matches the other directory entry.
    pub fn path_eq(&self, other: &Self) -> 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 const 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 const fn is_file(&self) -> bool {
        !self.is_directory
    }

    /// Returns true if the item is a system file.
    pub const 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 const fn is_hidden(&self) -> bool {
        self.is_hidden
    }
}

/// Contains the state of the directory content.
#[derive(Debug, PartialEq, Eq)]
pub enum DirectoryContentState {
    /// If we are currently waiting for the loading process on another thread.
    /// The value is the timestamp when the loading process started.
    Pending(SystemTime),
    /// If loading the directory content finished since the last update call.
    /// This is only returned once.
    Finished,
    /// If loading the directory content was successful.
    Success,
    /// If there was an error loading the directory content.
    /// The value contains the error message.
    Errored(String),
}

type DirectoryContentReceiver =
    Option<Arc<Mutex<mpsc::Receiver<Result<Vec<DirectoryEntry>, std::io::Error>>>>>;

/// Contains the content of a directory.
pub struct DirectoryContent {
    /// Current state of the directory content.
    state: DirectoryContentState,
    /// The loaded directory contents.
    content: Vec<DirectoryEntry>,
    /// Receiver when the content is loaded on a different thread.
    content_recv: DirectoryContentReceiver,
}

impl Default for DirectoryContent {
    fn default() -> Self {
        Self {
            state: DirectoryContentState::Success,
            content: Vec::new(),
            content_recv: None,
        }
    }
}

impl std::fmt::Debug for DirectoryContent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DirectoryContent")
            .field("state", &self.state)
            .field("content", &self.content)
            .field(
                "content_recv",
                if self.content_recv.is_some() {
                    &"<Receiver>"
                } else {
                    &"None"
                },
            )
            .finish()
    }
}

impl DirectoryContent {
    /// 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,
        file_filter: Option<&FileFilter>,
        file_system: Arc<dyn FileSystem + Sync + Send + 'static>,
    ) -> Self {
        if config.load_via_thread {
            Self::with_thread(config, path, include_files, file_filter, file_system)
        } else {
            Self::without_thread(config, path, include_files, file_filter, &*file_system)
        }
    }

    fn with_thread(
        config: &FileDialogConfig,
        path: &Path,
        include_files: bool,
        file_filter: Option<&FileFilter>,
        file_system: Arc<dyn FileSystem + Send + Sync + 'static>,
    ) -> Self {
        let (tx, rx) = mpsc::channel();

        let c = config.clone();
        let p = path.to_path_buf();
        let f = file_filter.cloned();
        thread::spawn(move || {
            let _ = tx.send(load_directory(
                &c,
                &p,
                include_files,
                f.as_ref(),
                &*file_system,
            ));
        });

        Self {
            state: DirectoryContentState::Pending(SystemTime::now()),
            content: Vec::new(),
            content_recv: Some(Arc::new(Mutex::new(rx))),
        }
    }

    fn without_thread(
        config: &FileDialogConfig,
        path: &Path,
        include_files: bool,
        file_filter: Option<&FileFilter>,
        file_system: &dyn FileSystem,
    ) -> Self {
        match load_directory(config, path, include_files, file_filter, file_system) {
            Ok(c) => Self {
                state: DirectoryContentState::Success,
                content: c,
                content_recv: None,
            },
            Err(err) => Self {
                state: DirectoryContentState::Errored(err.to_string()),
                content: Vec::new(),
                content_recv: None,
            },
        }
    }

    pub fn update(&mut self) -> &DirectoryContentState {
        if self.state == DirectoryContentState::Finished {
            self.state = DirectoryContentState::Success;
        }

        if !matches!(self.state, DirectoryContentState::Pending(_)) {
            return &self.state;
        }

        self.update_pending_state()
    }

    fn update_pending_state(&mut self) -> &DirectoryContentState {
        let rx = std::mem::take(&mut self.content_recv);
        let mut update_content_recv = true;

        if let Some(recv) = &rx {
            let value = recv.lock().try_recv();
            match value {
                Ok(result) => match result {
                    Ok(content) => {
                        self.state = DirectoryContentState::Finished;
                        self.content = content;
                        update_content_recv = false;
                    }
                    Err(err) => {
                        self.state = DirectoryContentState::Errored(err.to_string());
                        update_content_recv = false;
                    }
                },
                Err(err) => {
                    if mpsc::TryRecvError::Disconnected == err {
                        self.state =
                            DirectoryContentState::Errored("thread ended unexpectedly".to_owned());
                        update_content_recv = false;
                    }
                }
            }
        }

        if update_content_recv {
            self.content_recv = rx;
        }

        &self.state
    }

    /// Returns an iterator in the given range of the directory cotnents.
    /// No filters are applied using this iterator.
    pub fn iter_range_mut(
        &mut self,
        range: std::ops::Range<usize>,
    ) -> impl Iterator<Item = &mut DirectoryEntry> {
        self.content[range].iter_mut()
    }

    pub fn filtered_iter<'s>(
        &'s self,
        search_value: &'s str,
    ) -> impl Iterator<Item = &'s DirectoryEntry> + 's {
        self.content
            .iter()
            .filter(|p| apply_search_value(p, search_value))
    }

    pub fn filtered_iter_mut<'s>(
        &'s mut self,
        search_value: &'s str,
    ) -> impl Iterator<Item = &'s mut DirectoryEntry> + 's {
        self.content
            .iter_mut()
            .filter(|p| apply_search_value(p, search_value))
    }

    /// Marks each element in the content as unselected.
    pub fn reset_multi_selection(&mut self) {
        for item in &mut self.content {
            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);
    }
}

fn apply_search_value(entry: &DirectoryEntry, value: &str) -> bool {
    value.is_empty()
        || entry
            .file_name()
            .to_lowercase()
            .contains(&value.to_lowercase())
}

/// Loads the contents of the given directory.
fn load_directory(
    config: &FileDialogConfig,
    path: &Path,
    include_files: bool,
    file_filter: Option<&FileFilter>,
    file_system: &dyn FileSystem,
) -> io::Result<Vec<DirectoryEntry>> {
    let mut result: Vec<DirectoryEntry> = Vec::new();
    for path in file_system.read_dir(path)? {
        let entry = DirectoryEntry::from_path(config, &path, file_system);

        if !config.storage.show_system_files && entry.is_system_file() {
            continue;
        }

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

        if !config.storage.show_hidden && entry.is_hidden() {
            continue;
        }

        if let Some(file_filter) = file_filter {
            if entry.is_file() && !(file_filter.filter)(entry.as_path()) {
                continue;
            }
        }

        result.push(entry);
    }

    result.sort_by(|a, b| {
        if a.is_dir() == b.is_dir() {
            a.file_name().cmp(b.file_name())
        } else if a.is_dir() {
            std::cmp::Ordering::Less
        } else {
            std::cmp::Ordering::Greater
        }
    });

    Ok(result)
}

/// 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, file_system: &dyn FileSystem) -> String {
    for def in &config.file_icon_filters {
        if (def.filter)(path) {
            return def.icon.clone();
        }
    }

    if file_system.is_dir(path) {
        config.default_folder_icon.clone()
    } else {
        config.default_file_icon.clone()
    }
}