Skip to content

Commit

Permalink
Add option to show system files in the hamburger menu (#173)
Browse files Browse the repository at this point in the history
* Add option to show system files in the hamburger menu

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.

* Disable the option to show system files on windows by default
  • Loading branch information
crumblingstatue authored Oct 25, 2024
1 parent 539c0a8 commit 2fd9b3d
Show file tree
Hide file tree
Showing 5 changed files with 54 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
13 changes: 13 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: SHOW_SYSTEM_FILES_OPTION_DEFAULT,
show_search: true,

show_left_panel: true,
Expand All @@ -256,6 +262,13 @@ impl Default for FileDialogConfig {
}
}

/// On Windows, displaying system files is troublesome, the option should only
/// be enabled on explicit request.
#[cfg(windows)]
const SHOW_SYSTEM_FILES_OPTION_DEFAULT: bool = false;
#[cfg(not(windows))]
const SHOW_SYSTEM_FILES_OPTION_DEFAULT: bool = true;

impl FileDialogConfig {
/// Sets the storage used by the file dialog.
/// Storage includes all data that is persistently stored between multiple
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 @@ -886,6 +886,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 @@ -1230,7 +1240,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 @@ -1255,6 +1267,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 @@ -2924,6 +2948,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 2fd9b3d

Please sign in to comment.