Crates.io | egui_file |
lib.rs | egui_file |
version | 0.19.0 |
source | src |
created_at | 2022-08-22 14:44:48.181316 |
updated_at | 2024-09-26 23:21:17.529625 |
description | File dialog window for egui |
homepage | |
repository | https://github.com/Barugon/egui_file |
max_upload_size | |
id | 650411 |
size | 31,079 |
Taken from the Dotrix project, made into a stand-alone library and modified for more use cases.
[dependencies]
egui_file = "0.19"
eframe = "0.29"
use eframe::{
egui::{CentralPanel, Context},
App, Frame,
};
use egui_file::FileDialog;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
#[derive(Default)]
pub struct Demo {
opened_file: Option<PathBuf>,
open_file_dialog: Option<FileDialog>,
}
impl App for Demo {
fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
CentralPanel::default().show(ctx, |ui| {
if (ui.button("Open")).clicked() {
// Show only files with the extension "txt".
let filter = Box::new({
let ext = Some(OsStr::new("txt"));
move |path: &Path| -> bool { path.extension() == ext }
});
let mut dialog = FileDialog::open_file(self.opened_file.clone()).show_files_filter(filter);
dialog.open();
self.open_file_dialog = Some(dialog);
}
if let Some(dialog) = &mut self.open_file_dialog {
if dialog.show(ctx).selected() {
if let Some(file) = dialog.path() {
self.opened_file = Some(file.to_path_buf());
}
}
}
});
}
}
fn main() {
let _ = eframe::run_native(
"File Dialog Demo",
eframe::NativeOptions::default(),
Box::new(|_cc| Ok(Box::new(Demo::default()))),
);
}