egui_file_dialog/
lib.rs

1//! # egui-file-dialog
2//!
3//! An easy-to-use and customizable file dialog (a.k.a. file explorer, file picker) for
4//! [egui](https://github.com/emilk/egui).
5//!
6//! Read more about the project: <https://github.com/jannistpl/egui-file-dialog>
7//!
8//! ### Features
9//!
10//! - Pick a file or a directory
11//! - Save a file (Prompt user for a destination path)
12//!   - Dialog to ask the user if the existing file should be overwritten
13//! - Pick multiple files and folders at once
14//!   (ctrl/shift + click on linux/windows and cmd/shift + click on macOS)
15//! - Open the dialog in a normal or modal window
16//! - Create a new folder
17//! - Keyboard navigation
18//! - Option to show or hide hidden files and folders
19//! - Option to show or hide system files
20//! - Navigation buttons to open the parent or previous directories
21//! - Search for items in a directory
22//! - Add file filters the user can select from a dropdown
23//! - Shortcut for user directories (Home, Documents, ...) and system disks
24//! - Pin folders to the left sidebar
25//! - Manually edit the path via text
26//! - Virtual file system support
27//! - Customization highlights:
28//!   - Customize which areas and functions of the dialog are visible
29//!   - Customize the text labels used by the dialog to enable multilingual support
30//!   - Customize file and folder icons
31//!   - Add custom quick access sections to the left sidebar
32//!   - Customize keybindings used by the file dialog
33//!   - Add a right panel with custom UI
34//!     (An optional information panel to display file previews is included)
35//!
36//! ## Example
37//!
38//! Detailed examples that can be run can be found in the
39//! [examples](https://github.com/jannistpl/egui-file-dialog/tree/develop/examples) folder.
40//!
41//! The following example shows the basic use of the file dialog with
42//! [eframe](https://github.com/emilk/egui/tree/master/crates/eframe) to select a file.
43//!
44//! ```no_run
45//! use std::path::PathBuf;
46//!
47//! use eframe::egui;
48//! use egui_file_dialog::FileDialog;
49//!
50//! struct MyApp {
51//!     file_dialog: FileDialog,
52//!     picked_file: Option<PathBuf>,
53//! }
54//!
55//! impl MyApp {
56//!     pub fn new(_cc: &eframe::CreationContext) -> Self {
57//!         Self {
58//!             // Create a new file dialog object
59//!             file_dialog: FileDialog::new(),
60//!             picked_file: None,
61//!         }
62//!     }
63//! }
64//!
65//! impl eframe::App for MyApp {
66//!     fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
67//!         egui::CentralPanel::default().show(ctx, |ui| {
68//!             if ui.button("Pick file").clicked() {
69//!                 // Open the file dialog to pick a file.
70//!                 self.file_dialog.pick_file();
71//!             }
72//!
73//!             ui.label(format!("Picked file: {:?}", self.picked_file));
74//!
75//!             // Update the dialog
76//!             self.file_dialog.update(ctx);
77//!
78//!             // Check if the user picked a file.
79//!             if let Some(path) = self.file_dialog.take_picked() {
80//!                 self.picked_file = Some(path.to_path_buf());
81//!             }
82//!         });
83//!     }
84//! }
85//!
86//! fn main() -> eframe::Result<()> {
87//!     eframe::run_native(
88//!         "File dialog demo",
89//!         eframe::NativeOptions::default(),
90//!         Box::new(|ctx| Ok(Box::new(MyApp::new(ctx)))),
91//!     )
92//! }
93//! ```
94//!
95//! ### Customization
96//! Many things can be customized so that the dialog can be used in different situations. \
97//! A few highlights of the customization are listed below. For all possible customization
98//! options, see the documentation on
99//! [docs.rs](https://docs.rs/egui-file-dialog/latest/egui_file_dialog/struct.FileDialog.html).
100//!
101//! - Set which areas and functions of the dialog are visible using `FileDialog::show_*` methods
102//! - Update the text labels that the dialog uses. See [Multilingual support](#multilingual-support)
103//! - Customize file and folder icons using `FileDialog::set_file_icon`
104//!   (Currently only unicode is supported)
105//! - Customize keybindings used by the file dialog using `FileDialog::keybindings`.
106//!   See [Keybindings](#keybindings)
107//! - Add a right panel with custom UI using `FileDialog::update_with_right_panel_ui`
108//!
109//! Since the dialog uses the egui style to look like the rest of the application,
110//! the appearance can be customized with `egui::Style` and `egui::Context::set_style`.
111//!
112//! The following example shows how a single file dialog can be customized. \
113//! If you need to configure multiple file dialog objects with the same or almost the same
114//! options, it is a good idea to use `FileDialogConfig` and `FileDialog::with_config`
115//! (See `FileDialogConfig` on [docs.rs](
116//! https://docs.rs/egui-file-dialog/latest/egui_file_dialog/struct.FileDialogConfig.html)).
117//!
118//! ```rust
119//! use std::path::PathBuf;
120//! use std::sync::Arc;
121//!
122//! use egui_file_dialog::FileDialog;
123//!
124//! FileDialog::new()
125//!     .initial_directory(PathBuf::from("/path/to/app"))
126//!     .default_file_name("app.cfg")
127//!     .default_size([600.0, 400.0])
128//!     .resizable(false)
129//!     .show_new_folder_button(false)
130//!     .show_search(false)
131//!     .show_path_edit_button(false)
132//!     // Add a new quick access section to the left sidebar
133//!     .add_quick_access("Project", |s| {
134//!         s.add_path("โ˜†  Examples", "examples");
135//!         s.add_path("๐Ÿ“ท  Media", "media");
136//!         s.add_path("๐Ÿ“‚  Source", "src");
137//!     })
138//!     // Markdown files should use the "document with text (U+1F5B9)" icon
139//!     .set_file_icon(
140//!         "๐Ÿ–น",
141//!         Arc::new(|path| path.extension().unwrap_or_default() == "md"),
142//!     )
143//!     // .gitignore files should use the "web-github (U+E624)" icon
144//!     .set_file_icon(
145//!         "๎˜ค",
146//!         Arc::new(|path| path.file_name().unwrap_or_default() == ".gitignore"),
147//!     )
148//!     // Add file filters the user can select in the bottom right
149//!     .add_file_filter(
150//!         "PNG files",
151//!         Arc::new(|p| p.extension().unwrap_or_default() == "png"),
152//!     )
153//!     .add_file_filter(
154//!         "Rust source files",
155//!         Arc::new(|p| p.extension().unwrap_or_default() == "rs"),
156//!     );
157//! ```
158//!
159//! ### Multilingual support
160//! For desktop applications it is often necessary to offer different languages.
161//! While the dialog currently only offers English labels by default, the labels are
162//! fully customizable. This makes it possible to adapt the labels to different languages.
163//!
164//! The following example shows how the labels can be changed to display the file dialog in
165//! English or German. Checkout `examples/multilingual` for the full example.
166//!
167//! ```
168//! use egui_file_dialog::{FileDialog, FileDialogLabels};
169//!
170//! enum Language {
171//!     English,
172//!     German,
173//! }
174//!
175//! fn get_labels_german() -> FileDialogLabels {
176//!     FileDialogLabels {
177//!         title_select_directory: "๐Ÿ“ Ordner ร–ffnen".to_string(),
178//!         title_select_file: "๐Ÿ“‚ Datei ร–ffnen".to_string(),
179//!         title_save_file: "๐Ÿ“ฅ Datei Speichern".to_string(),
180//!
181//!         // ... See examples/multilingual for the other labels
182//!
183//!         ..Default::default()
184//!     }
185//! }
186//!
187//! /// Updates the labels of the file dialog.
188//! /// Should be called every time the user selects a different language.
189//! fn update_labels(language: &Language, file_dialog: &mut FileDialog) {
190//!     *file_dialog.labels_mut() = match language {
191//!         // English labels are used by default
192//!         Language::English => FileDialogLabels::default(),
193//!         // Use custom labels for German
194//!         Language::German => get_labels_german(),
195//!     };
196//! }
197//! ```
198//!
199//! ### Persistent data
200//! The file dialog currently requires the following persistent data to be stored across
201//! multiple file dialog objects:
202//!
203//! - Folders the user pinned to the left sidebar (`FileDialog::show_pinned_folders`)
204//! - If hidden files and folders should be visible (`FileDialog::show_hidden_option`)
205//! - If system files should be visible (`FileDialog::show_system_files_option`)
206//!
207//! If one of the above feature is activated, the data should be saved by the application.
208//! Otherwise, frustrating situations could arise for the user and the features would not
209//! offer much added value.
210//!
211//! All data that needs to be stored permanently is contained in the `FileDialogStorage` struct.
212//! This struct can be accessed using `FileDialog::storage` or `FileDialog::storage_mut` to save or
213//! load the persistent data. \
214//! By default the feature `serde` is enabled, which implements `serde::Serialize` and
215//! `serde::Deserialize` for the objects to be saved. However, the objects can also be
216//! accessed without the feature enabled.
217//!
218//! Checkout `examples/persistence` for an example.
219
220// Let's keep the public API well documented!
221#![warn(missing_docs)]
222
223mod config;
224mod create_directory_dialog;
225mod data;
226mod file_dialog;
227mod file_system;
228/// Information panel showing the preview and metadata of the selected item
229pub mod information_panel;
230mod modals;
231
232pub use config::{
233    FileDialogConfig, FileDialogKeyBindings, FileDialogLabels, IconFilter, KeyBinding, OpeningMode,
234    PinnedFolder, QuickAccess, QuickAccessPath,
235};
236pub use data::{DirectoryEntry, Disk, Disks, Metadata, UserDirectories};
237pub use file_dialog::{DialogMode, DialogState, FileDialog, FileDialogStorage};
238
239pub use file_system::{FileSystem, NativeFileSystem};