-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add example * Update CHANGELOG.md
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "persistence" | ||
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.27.1", default-features = false, features = ["glow", "persistence"] } | ||
egui-file-dialog = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
This example uses eframe to show how the persistent data of the file dialog can be saved. \ | ||
If the default features of the file dialog are deactivated, the `persistence` feature must be enabled. | ||
|
||
``` | ||
cargo run -p persistence | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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 { | ||
let mut file_dialog = FileDialog::default(); | ||
|
||
// Load the persistent data of the file dialog. | ||
// Alternatively, you can also use the `FileDialog::storage` builder method. | ||
if let Some(storage) = cc.storage { | ||
*file_dialog.storage_mut() = | ||
eframe::get_value(storage, "file_dialog_storage").unwrap_or_default() | ||
} | ||
|
||
Self { | ||
file_dialog, | ||
selected_file: None, | ||
} | ||
} | ||
} | ||
|
||
impl eframe::App for MyApp { | ||
fn save(&mut self, storage: &mut dyn eframe::Storage) { | ||
// Save the persistent data of the file dialog | ||
eframe::set_value( | ||
storage, | ||
"file_dialog_storage", | ||
self.file_dialog.storage_mut(), | ||
); | ||
} | ||
|
||
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)); | ||
|
||
self.file_dialog.update(ctx); | ||
|
||
if let Some(path) = self.file_dialog.take_selected() { | ||
self.selected_file = Some(path); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn main() -> eframe::Result<()> { | ||
let options = eframe::NativeOptions { | ||
viewport: egui::ViewportBuilder::default().with_inner_size([1080.0, 720.0]), | ||
..Default::default() | ||
}; | ||
|
||
eframe::run_native( | ||
"My egui application", | ||
options, | ||
Box::new(|ctx| Box::new(MyApp::new(ctx))), | ||
) | ||
} |