pub struct FileDialog { /* private fields */ }
Expand description
A struct representing a file dialog window.
This struct is used to display a file dialog window to the user, allowing them to select a file or folder. It provides an interface to the operating system’s file dialog APIs, which are used to display the dialog and retrieve the selected file or folder location.
§Examples
Creating a new FileDialog
and showing it to the user, returning the path selected:
use nvdialog_rs as nvdialog;
use nvdialog::FileDialog;
use nvdialog::FileDialogType;
let mut file_dialog = FileDialog::new("Title", FileDialogType::OpenFile);
let path = PathBuf::new();
if let Some(filename) = file_dialog.retrieve_filename() {
path = filename;
// do something with the returned Path
} else {
println!("No file selected.");
}
§FFI
Corresponds to NvdFileDialog
.
Implementations§
Source§impl FileDialog
impl FileDialog
Sourcepub fn new<S: AsRef<str>>(
title: S,
type_of_dialog: FileDialogType,
file_extensions: Option<impl IntoIterator<Item = S>>,
) -> Self
pub fn new<S: AsRef<str>>( title: S, type_of_dialog: FileDialogType, file_extensions: Option<impl IntoIterator<Item = S>>, ) -> Self
Creates a new FileDialog
instance with the specified title and
type of dialog.
This function returns a new FileDialog
instance
with a raw pointer to the underlying NvdFileDialog
struct
initialized based on the specified type of dialog. The title
argument specifies the title to be displayed in the file dialog
window. The type_of_dialog
argument determines whether the dialog
is used for opening a file (FileDialogType::OpenFile
) or saving
a file (FileDialogType::SaveFile
).
If FileDialogType::OpenFile
is specified, the raw
pointer is
obtained by calling the nvd_open_file_dialog_new
function from
the underlying C API. If FileDialogType::SaveFile
is specified,
the raw
pointer is obtained by calling the
nvd_save_file_dialog_new
function from the underlying C API. In
the case of FileDialogType::SaveFile
, the dialog defaults to
suggesting a filename of “filename”.
§Examples
Creating a new FileDialog
instance for opening a file:
let file_dialog = FileDialog::new("Open File", FileDialogType::OpenFile);
Creating a new FileDialog
instance for saving a file:
let file_dialog = FileDialog::new("Save File", FileDialogType::SaveFile);
Examples found in repository?
27fn main() {
28 nvdialog_rs::init().expect("failed to initialize nvdialog");
29 let mut folder_dialog = FileDialog::new("Choose a folder", FileDialogType::OpenFolder, None::<Vec<&str>>);
30 let mut where_to_save = FileDialog::new("Save as...", FileDialogType::SaveFile, None::<Vec<&str>>);
31
32 if let Some(p) = folder_dialog.retrieve_filename() {
33 println!("You chose folder {}", p.display());
34 }
35
36 if let Some(s) = where_to_save.retrieve_filename() {
37 println!("Saved as {}", s.display());
38 }
39}
More examples
31fn main() {
32 nvdialog_rs::init().expect("Can't initialize NvDialog");
33 let mut file_dialog = FileDialog::new(
34 String::from("Select an image"),
35 FileDialogType::OpenFile,
36 Some(vec![
37 "png".to_owned(),
38 "jpg".to_owned(),
39 "jpeg".to_owned(),
40 "ico".to_owned(),
41 "webp".to_owned(),
42 ]),
43 );
44
45 if let Some(file) = file_dialog.retrieve_filename() {
46 DialogBox::new("File chosen", &file.to_str().unwrap(), DialogType::Simple)
47 .expect("Can't create dialog")
48 .show()
49 } else {
50 DialogBox::new("Error", "No file chosen", DialogType::Error)
51 .expect("Can't create dialog")
52 .show()
53 }
54}
Sourcepub fn retrieve_filename(&mut self) -> Option<PathBuf>
pub fn retrieve_filename(&mut self) -> Option<PathBuf>
Retrieves the file name selected in the file dialog. This
function returns a PathBuf
instance containing the selected
file name, or None
if no file was selected.
This function calls the nvd_get_file_location
function from the underlying C API. If the returned pointer from NvDialog is NULL
,
this function returns None
. If the pointer is not null, this function constructs a
CStr
instance from the raw buffer, and constructs a PathBuf
instance from the CStr
.
§Safety
This function constructs the returned PathBuf
using C bytes, which may
cause a panic if the C data contains non-valid UTF-8 characters.
§Returns
Some(PathBuf)
if a file was selected and the selected file name could be converted to aPathBuf
instance.None
if no file was selected.
§Panics
This function may panic with the message “Invalid UTF-8 data” if the raw buffer returned from the underlying C API contains invalid UTF-8 data.
§Examples
let mut file_dialog = FileDialog::new("Open File", FileDialogType::OpenFile);
if let Some(path) = file_dialog.retrieve_filename() {
// A file was selected. Do something with the file...
} else {
eprintln!("No file was selected")
}
Examples found in repository?
27fn main() {
28 nvdialog_rs::init().expect("failed to initialize nvdialog");
29 let mut folder_dialog = FileDialog::new("Choose a folder", FileDialogType::OpenFolder, None::<Vec<&str>>);
30 let mut where_to_save = FileDialog::new("Save as...", FileDialogType::SaveFile, None::<Vec<&str>>);
31
32 if let Some(p) = folder_dialog.retrieve_filename() {
33 println!("You chose folder {}", p.display());
34 }
35
36 if let Some(s) = where_to_save.retrieve_filename() {
37 println!("Saved as {}", s.display());
38 }
39}
More examples
31fn main() {
32 nvdialog_rs::init().expect("Can't initialize NvDialog");
33 let mut file_dialog = FileDialog::new(
34 String::from("Select an image"),
35 FileDialogType::OpenFile,
36 Some(vec![
37 "png".to_owned(),
38 "jpg".to_owned(),
39 "jpeg".to_owned(),
40 "ico".to_owned(),
41 "webp".to_owned(),
42 ]),
43 );
44
45 if let Some(file) = file_dialog.retrieve_filename() {
46 DialogBox::new("File chosen", &file.to_str().unwrap(), DialogType::Simple)
47 .expect("Can't create dialog")
48 .show()
49 } else {
50 DialogBox::new("Error", "No file chosen", DialogType::Error)
51 .expect("Can't create dialog")
52 .show()
53 }
54}