PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): Browse… buttons for dialog folder and plug-in path fields (#7942, #7531)
VishnuByrraju37a0afa16d96
diff --git a/crates/ui-egui/src/browse_field_tests.rs b/crates/ui-egui/src/browse_field_tests.rsnew file mode 100644index 000000000..2aa3ef44f--- /dev/null+++ b/crates/ui-egui/src/browse_field_tests.rs@@ -0,0 +1,65 @@+//! #7942 / #7531: path fields of command dialogs get a Browse… button (Artboards to Files'+//! folder, Install Plug-in…'s .wasm file) that fills the field from a file dialog.+use crate::PhotocraftApp;+use crate::file_dialog::{FileDialogAnswer, FileDialogRequest};+use egui::vec2;+use egui_kittest::{Harness, kittest::Queryable};+use serde_json::{Value, json};++fn harness(answers: Vec<Option<FileDialogAnswer>>) -> (Harness<'static, PhotocraftApp>, std::rc::Rc<std::cell::RefCell<Vec<FileDialogRequest>>>) {+ let (show, asked) = crate::file_dialog::fake(answers);+ let h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());+ PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services { file_dialog: Some(show), ..Default::default() })+ });+ (h, asked)+}++fn field(h: &Harness<'_, PhotocraftApp>, key: &str) -> Option<Value> {+ h.state().ui.dialogs.last().and_then(|d| d.fields.get(key).cloned())+}++#[test]+fn artboards_to_files_folder_is_chosen_with_browse() {+ let (mut h, asked) = harness(vec![Some(FileDialogAnswer::FolderTo("/tmp/exports".into()))]);+ h.state_mut().run("file.new", json!({"width": 64, "height": 64})).unwrap();+ h.state_mut().sync_views();+ let ctx = h.ctx.clone();+ crate::menus::invoke(h.state_mut(), &ctx, "file.export.artboardsToFiles", json!({})).unwrap();+ h.run_steps(4);+ assert!(field(&h, "dir").is_some(), "the dialog has a Dir field");+ h.get_by_label("Browse…").click();+ h.run_steps(4);+ assert!(matches!(asked.borrow().as_slice(), [FileDialogRequest::Folder { .. }]));+ assert_eq!(field(&h, "dir"), Some(json!("/tmp/exports")));+ assert!(field(&h, crate::dialogs::BROWSE_FIELD).is_none(), "the request is consumed");+}++#[test]+fn cancelling_browse_keeps_the_typed_folder() {+ let (mut h, _) = harness(vec![None]);+ h.state_mut().run("file.new", json!({"width": 64, "height": 64})).unwrap();+ h.state_mut().sync_views();+ let ctx = h.ctx.clone();+ crate::menus::invoke(h.state_mut(), &ctx, "file.export.layersToFiles", json!({})).unwrap();+ h.run_steps(4);+ let before = field(&h, "dir");+ h.get_by_label("Browse…").click();+ h.run_steps(4);+ assert_eq!(field(&h, "dir"), before);+}++#[test]+fn install_plugin_path_is_chosen_with_browse() {+ let (mut h, asked) = harness(vec![Some(FileDialogAnswer::Paths(vec!["/tmp/blur.wasm".into()]))]);+ let ctx = h.ctx.clone();+ crate::menus::invoke(h.state_mut(), &ctx, "plugin.install", json!({})).unwrap();+ h.run_steps(4);+ h.get_by_label("Browse…").click();+ h.run_steps(4);+ match asked.borrow().as_slice() {+ [FileDialogRequest::Open { multiple: false, extensions: Some(e), .. }] => assert_eq!(e, &["wasm".to_string()]),+ other => panic!("unexpected requests {other:?}"),+ }+ assert_eq!(field(&h, "path"), Some(json!("/tmp/blur.wasm")));+}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex e27411b7e..839527aa6 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -7,6 +7,37 @@ use crate::PhotocraftApp;use crate::state::{Dialog, DialogKind};use crate::widgets::{ButtonRole, DialogButton, dialog_buttons};+/// A dialog body sets this field to the key of the text field whose Browse… button was clicked;+/// [`show`] takes it out again and opens the matching file or folder dialog.+pub(crate) const BROWSE_FIELD: &str = "__browse";++/// Browse… beside a path field of dialog `id`: a folder picker for `dir` fields, a `.wasm` picker+/// for Install Plug-in…'s `path`. The answer fills the field.+fn browse_field(app: &mut PhotocraftApp, id: u64, fields: &serde_json::Map<String, Value>, key: &str) {+ let key = key.to_string();+ let r = if key == "dir" {+ app.ask_folder(move |app, path| fill_field(app, id, &key, path))+ } else if fields.get("__command").and_then(Value::as_str) == Some("plugin.install") {+ let initial_dir = crate::file_dialog::last_used_dir(&app.ui.recent_files);+ let request = crate::file_dialog::FileDialogRequest::Open { multiple: false, initial_dir, extensions: Some(vec!["wasm".into()]) };+ app.ask_file(request, move |app, answer| match answer {+ crate::file_dialog::FileDialogAnswer::Paths(mut paths) if !paths.is_empty() => fill_field(app, id, &key, paths.remove(0)),+ _ => Err("Install Plug-in reads a .wasm file from disk".into()),+ })+ } else {+ return;+ };+ if let Err(e) = r.map(|_| ()).or_else(|e| if e == crate::file_dialog::CANCELLED { Ok(()) } else { Err(e) }) {+ app.ui.status = e;+ }+}++fn fill_field(app: &mut PhotocraftApp, id: u64, key: &str, path: String) -> Result<Value, String> {+ let d = app.ui.dialog_mut(id).ok_or("the dialog was closed")?;+ d.fields.insert(key.into(), json!(path));+ Ok(Value::Null)+}+/// Where this frame's dialogs are on screen (the canvas reads last frame's: it draws first).const RECTS: &str = "pc-dialog-rects";@@ -382,6 +413,9 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {}}crate::field_tab::end(ui.ctx(), tab);+ if let Some(Value::String(key)) = fields.remove(BROWSE_FIELD) {+ browse_field(app, d.id, &fields, &key);+ }if crate::guide_layout_ui::owns(&fields) || crate::brush_preset_dialog::owns(&fields) {// These dialogs own their right-side actions and validate Enter themselves.} else if crate::color_picker_ui::owns(&fields) {diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rsindex a908df5eb..c6fd5635c 100644--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@ -885,6 +885,10 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {ui.horizontal(|ui| {ui.label(egui::RichText::new(label(&p.key)).color(t.text_dim));ui.add(egui::TextEdit::singleline(&mut v).desired_width(200.0));+ // Install Plug-in…: pick the .wasm file instead of typing its path (#7531).+ if cmd == "plugin.install" && p.key == "path" && crate::widgets::secondary_button(ui, tl!("Browse…"), 0.0).clicked() {+ f.insert(crate::dialogs::BROWSE_FIELD.into(), json!(p.key));+ }});if v.is_empty() {f.remove(&p.key);diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 8bf2be90d..6b9ba13dd 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -3480,5 +3480,7 @@ mod channel_tint_tests {}}+#[cfg(test)]+mod browse_field_tests;#[cfg(test)]mod options_hit_area_tests;diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rsindex 1cb51a6ea..7d56a22f5 100644--- a/crates/ui-egui/src/view_cmds.rs+++ b/crates/ui-egui/src/view_cmds.rs@@ -1081,6 +1081,7 @@ pub fn form_body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {let labels = f.get("__labels").cloned().unwrap_or(Value::Null);let label = |k: &str| labels.get(k).and_then(Value::as_str).map_or_else(|| label_of(k), str::to_string);let keys: Vec<String> = f.keys().filter(|k| !k.starts_with("__")).cloned().collect();+ let mut browse: Option<String> = None;egui::Grid::new("form-dialog").num_columns(2).spacing([12.0, 6.0]).show(ui, |ui| {for k in keys {let v = f.get(&k).cloned().unwrap_or(Value::Null);@@ -1127,7 +1128,13 @@ pub fn form_body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {crate::widgets::dropdown(ui, &format!("form-{k}"), &mut s, &refs, 200.0);}None => {- ui.add(egui::TextEdit::singleline(&mut s).desired_width(260.0));+ ui.horizontal(|ui| {+ ui.add(egui::TextEdit::singleline(&mut s).desired_width(260.0));+ // A folder field gets the system folder picker (#7942).+ if k == "dir" && crate::widgets::secondary_button(ui, tl!("Browse…"), 0.0).clicked() {+ browse = Some(k.clone());+ }+ });}}json!(s)@@ -1148,6 +1155,9 @@ pub fn form_body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {ui.end_row();}});+ if let Some(key) = browse {+ f.insert(crate::dialogs::BROWSE_FIELD.into(), json!(key));+ }camera_data(ui, f);}