Skip to content

Commit

Permalink
Add example select_file
Browse files Browse the repository at this point in the history
  • Loading branch information
fluxxcode committed Feb 1, 2024
1 parent 34eed29 commit 25b7701
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/select_file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "select_file"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
eframe = { version = "0.25.0", default-features = false, features = ["glow"] }
egui-file-dialog = { version = "0.1.0", path = "../../"}
7 changes: 7 additions & 0 deletions examples/select_file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Example showing how to select a file using the file dialog.

```
cargo run -p select_file
```

![](screenshot.png)
Binary file added examples/select_file/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions examples/select_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::path::PathBuf;

use eframe::egui;
use egui_file_dialog::FileDialog;

struct MyApp {
file_dialog: FileDialog,
selected_file: Option<PathBuf>,
}

impl MyApp {
pub fn new(_cc: &eframe::CreationContext) -> Self {
Self {
file_dialog: FileDialog::new(),
selected_file: None,
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if ui.button("Select file").clicked() {
self.file_dialog.select_file();
}

ui.label(format!("Selected file: {:?}", self.selected_file));

if let Some(path) = self.file_dialog.update(ctx).selected() {
self.selected_file = Some(path.to_path_buf());
}
});
}
}

fn main() -> eframe::Result<()> {
eframe::run_native(
"File dialog example",
eframe::NativeOptions::default(),
Box::new(|ctx| Box::new(MyApp::new(ctx))),
)
}

0 comments on commit 25b7701

Please sign in to comment.