Skip to content

Commit

Permalink
Add option to show system files in the hamburger menu
Browse files Browse the repository at this point in the history
Some advanced use cases of egui-file-dialog might require picking
system files, so this adds an option in the hamburger menu to show
them, in the same vein as the "Show hidden files" option.
  • Loading branch information
crumblingstatue committed Oct 25, 2024
1 parent c11ce40 commit e16194e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/multilingual/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn get_labels_german() -> FileDialogLabels {

reload: "⟲ Neu laden".to_string(),
show_hidden: " Versteckte Dateien anzeigen".to_string(),
show_system_files: " Systemdateien anzeigen".to_string(),

heading_pinned: "Angeheftet".to_string(),
heading_places: "Orte".to_string(),
Expand Down
3 changes: 3 additions & 0 deletions src/config/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct FileDialogLabels {
pub reload: String,
/// Text used for the option to show or hide hidden files and folders.
pub show_hidden: String,
/// Text used for the option to show or hide system files.
pub show_system_files: String,

// ------------------------------------------------------------------------
// Left panel:
Expand Down Expand Up @@ -128,6 +130,7 @@ impl Default for FileDialogLabels {

reload: "⟲ Reload".to_string(),
show_hidden: " Show hidden".to_string(),
show_system_files: " Show system files".to_string(),

heading_pinned: "Pinned".to_string(),
heading_places: "Places".to_string(),
Expand Down
6 changes: 6 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct FileDialogStorage {
pub pinned_folders: Vec<DirectoryEntry>,
/// If hidden files and folders should be listed inside the directory view.
pub show_hidden: bool,
/// If system files should be listed inside the directory view.
pub show_system_files: bool,
}

impl Default for FileDialogStorage {
Expand All @@ -26,6 +28,7 @@ impl Default for FileDialogStorage {
Self {
pinned_folders: Vec::new(),
show_hidden: false,
show_system_files: false,
}
}
}
Expand Down Expand Up @@ -175,6 +178,8 @@ pub struct FileDialogConfig {
pub show_reload_button: bool,
/// If the show hidden files and folders option inside the top panel menu should be visible.
pub show_hidden_option: bool,
/// If the show system files option inside the top panel menu should be visible.
pub show_system_files_option: bool,
/// If the search input in the top panel should be visible.
pub show_search: bool,

Expand Down Expand Up @@ -245,6 +250,7 @@ impl Default for FileDialogConfig {
show_menu_button: true,
show_reload_button: true,
show_hidden_option: true,
show_system_files_option: true,
show_search: true,

show_left_panel: true,
Expand Down
13 changes: 11 additions & 2 deletions src/data/directory_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,18 @@ impl DirectoryContent {
path: &Path,
include_files: bool,
show_hidden: bool,
show_system_files: bool,
file_filter: Option<&FileFilter>,
) -> io::Result<Self> {
Ok(Self {
content: load_directory(config, path, include_files, show_hidden, file_filter)?,
content: load_directory(
config,
path,
include_files,
show_hidden,
show_system_files,
file_filter,
)?,
})
}

Expand Down Expand Up @@ -190,6 +198,7 @@ fn load_directory(
path: &Path,
include_files: bool,
show_hidden: bool,
show_system_files: bool,
file_filter: Option<&FileFilter>,
) -> io::Result<Vec<DirectoryEntry>> {
let paths = fs::read_dir(path)?;
Expand All @@ -200,7 +209,7 @@ fn load_directory(
Ok(entry) => {
let entry = DirectoryEntry::from_path(config, entry.path().as_path());

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

Expand Down
27 changes: 26 additions & 1 deletion src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,16 @@ impl FileDialog {
self
}

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

/// Sets whether the search input should be visible in the top panel.
///
/// Has no effect when `FileDialog::show_top_panel` is disabled.
Expand Down Expand Up @@ -1161,7 +1171,9 @@ impl FileDialog {

// Menu button containing reload button and different options
if self.config.show_menu_button
&& (self.config.show_reload_button || self.config.show_hidden_option)
&& (self.config.show_reload_button
|| self.config.show_hidden_option
|| self.config.show_system_files_option)
{
ui.allocate_ui_with_layout(
BUTTON_SIZE,
Expand All @@ -1186,6 +1198,18 @@ impl FileDialog {
self.refresh();
ui.close_menu();
}

if self.config.show_system_files_option
&& ui
.checkbox(
&mut self.config.storage.show_system_files,
&self.config.labels.show_system_files,
)
.clicked()
{
self.refresh();
ui.close_menu();
}
});
},
);
Expand Down Expand Up @@ -2856,6 +2880,7 @@ impl FileDialog {
path,
self.show_files,
self.config.storage.show_hidden,
self.config.storage.show_system_files,
self.get_selected_file_filter(),
) {
Ok(content) => content,
Expand Down

0 comments on commit e16194e

Please sign in to comment.