Skip to content

Commit

Permalink
add save example
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Jan 9, 2024
1 parent d87cf1e commit cc44d37
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
44 changes: 21 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ eframe = { version = "0.22.0", default-features = false, features = [

# You only need serde if you want app persistence:
serde = { version = "1", features = ["derive"] }
rfd = "0.11"
rfd = "0.12"
tokio = { version = "1", features = ["sync", "rt", "time"] }
anyhow = "1.0.71"
log = "0.4.19"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Browse
Example app showing how to pick a file on web and desktop.
## Egui file browser

Example app showing how to pick a file on both web and desktop.

The web application can be accessed here:

https://woelper.github.io/egui_pick_file/


For native:
Expand Down
21 changes: 16 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Default for BrowseApp {
fn default() -> Self {
Self {
text_channel: channel(),
sample_text: "yo".into(),
sample_text: "This is some sample text".into(),
}
}
}
Expand All @@ -26,14 +26,14 @@ impl BrowseApp {
impl eframe::App for BrowseApp {
fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
// assign sample text once it comes in
if let Ok(f) = self.text_channel.1.try_recv() {
self.sample_text = f;
if let Ok(text) = self.text_channel.1.try_recv() {
self.sample_text = text;
}

egui::CentralPanel::default().show(ctx, |ui| {
ui.label(&self.sample_text);
ui.text_edit_multiline(&mut self.sample_text);
// a simple button opening the dialog
if ui.button("Open text file").clicked() {
if ui.button("📂 Open text file").clicked() {
let sender = self.text_channel.0.clone();
let task = rfd::AsyncFileDialog::new().pick_file();
// Context is wrapped in an Arc so it's cheap to clone as per:
Expand All @@ -49,6 +49,17 @@ impl eframe::App for BrowseApp {
}
});
}

if ui.button("💾 Save text to file").clicked() {
let task = rfd::AsyncFileDialog::new().save_file();
let contents = self.sample_text.clone();
execute(async move {
let file = task.await;
if let Some(file) = file {
_ = file.write(contents.as_bytes()).await;
}
});
}
});
}
}
Expand Down

0 comments on commit cc44d37

Please sign in to comment.