diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 4be1e51b..3401b409 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -372,7 +372,7 @@ pub fn shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context) { if app.ui.windows.project_config.is_some() { // Its keys are Escape (close) and Enter (OK); a stroke in progress isn't touched #[cfg(not(target_arch = "wasm32"))] - crate::project_config::keys_in(app, input, nothing_focused); + crate::project_config::keys_in(app, input, nothing_focused, popup_open); } else if !popup_open && app.properties_popup.is_some() && input.consume_key(Modifiers::NONE, Key::Escape) { app.properties_popup = None; } else if !popup_open && crate::rail_popup::is_visible(app) && input.consume_key(Modifiers::NONE, Key::Escape) { diff --git a/crates/ui-egui/src/project_config.rs b/crates/ui-egui/src/project_config.rs index 16be9590..3e19579f 100644 --- a/crates/ui-egui/src/project_config.rs +++ b/crates/ui-egui/src/project_config.rs @@ -44,8 +44,12 @@ fn elide_start(text: &str, fits: impl Fn(&str) -> bool) -> String { /// The keys of the open window, read in `menus::shortcuts` before any panel draws: Escape closes /// it (and goes no further, so it doesn't cancel a stroke), Enter confirms it unless a text field -/// or button had the keyboard (`nothing_focused`), whose Enter belongs to it. -pub(crate) fn keys_in(app: &mut TextureCraftApp, input: &mut egui::InputState, nothing_focused: bool) { +/// or button had the keyboard (`nothing_focused`), whose Enter belongs to it. An open dropdown +/// (`popup_open`) takes both keys first: Escape closes only it, Enter doesn't press OK behind it. +pub(crate) fn keys_in(app: &mut TextureCraftApp, input: &mut egui::InputState, nothing_focused: bool, popup_open: bool) { + if popup_open { + return; + } let escape = input.consume_key(Modifiers::NONE, Key::Escape); let enter = input.consume_key(Modifiers::NONE, Key::Enter); if escape { @@ -191,9 +195,16 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { let mut mesh_applied = false; let done = ok && { let reimport = config.reimport.then(|| app.session.remembered_mesh_path().map(str::to_string)).flatten(); - mesh_applied = match mesh_command(changed.as_deref(), reimport.as_deref(), config.preserve) { - Some((command, params)) => app.run(command, params).is_some(), - None => true, + mesh_applied = if config.reimport && changed.is_none() && reimport.is_none() { + // The file went away after the box was ticked: say so and stay open (the tick is dropped, so a second OK applies the rest). + app.toast(tr("File not found"), true); + config.reimport = false; + false + } else { + match mesh_command(changed.as_deref(), reimport.as_deref(), config.preserve) { + Some((command, params)) => app.run(command, params).is_some(), + None => true, + } }; // The format is compared with what the project has now: the Texture Set Settings panel may have set it too. let format = config.normal_format.filter(|format| *format != app.session.doc.project.normal_format); diff --git a/crates/ui-egui/tests/it/project_config.rs b/crates/ui-egui/tests/it/project_config.rs index f3b5a584..c7012a81 100644 --- a/crates/ui-egui/tests/it/project_config.rs +++ b/crates/ui-egui/tests/it/project_config.rs @@ -208,6 +208,61 @@ fn escape_closes_the_window_and_leaves_a_stroke_alone() { assert!(h.state().session.stroke_active(), "Escape went to the window, not to the stroke"); } +#[test] +fn escape_with_the_dropdown_open_closes_only_the_dropdown() { + let dir = temp_dir("escape-dropdown"); + let mut h = app_on_a_quad(&dir); + open_window(&mut h); + click_in_window(&mut h, Role::ComboBox, FORMAT_HINT); + assert!(egui::Popup::is_any_open(&h.ctx), "the dropdown is open"); + h.key_press(egui::Key::Escape); + h.run_steps(3); + assert!(!egui::Popup::is_any_open(&h.ctx), "Escape closed the dropdown"); + assert!(window_is_open(&h), "and left the window"); + h.key_press(egui::Key::Escape); + h.run_steps(3); + assert!(!window_is_open(&h), "the next Escape closes the window"); +} + +#[test] +fn enter_with_the_dropdown_open_does_not_press_ok() { + let dir = temp_dir("enter-dropdown"); + let mut h = app_on_a_quad(&dir); + std::fs::write(dir.join("quad.obj"), TRIANGLE).unwrap(); + open_window(&mut h); + h.state_mut().ui.windows.project_config.as_mut().unwrap().reimport = true; + click_in_window(&mut h, Role::ComboBox, FORMAT_HINT); + // Nothing focused, as the shortcut handler needs for Enter to mean OK + h.ctx.memory_mut(|m| { + if let Some(id) = m.focused() { + m.surrender_focus(id); + } + }); + h.run_steps(1); + assert!(egui::Popup::is_any_open(&h.ctx), "the dropdown is still open"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(window_is_open(&h), "Enter did not press OK behind the dropdown"); + assert_eq!(h.state().session.mesh.triangle_count(), 2, "nothing was applied"); +} + +#[test] +fn ok_after_the_ticked_file_went_missing_reports_it_and_stays_open() { + let dir = temp_dir("ticked-then-missing"); + let mut h = app_on_a_quad(&dir); + open_window(&mut h); + click_in_window(&mut h, Role::CheckBox, "Reimport mesh"); + assert!(own_tick(&h)); + std::fs::remove_file(dir.join("quad.obj")).unwrap(); + click(&mut h, Role::Button, "OK"); + assert!(window_is_open(&h), "the window stays open"); + assert!(h.state().toasts.iter().any(|t| t.error && t.text == "File not found"), "the error shows as a toast"); + assert!(!own_tick(&h), "the tick that can't be applied is dropped"); + assert_eq!(h.state().session.mesh.triangle_count(), 2); + click(&mut h, Role::Button, "OK"); + assert!(!window_is_open(&h), "a second OK has nothing left to read"); +} + #[test] fn enter_confirms_but_not_a_text_fields_enter() { let dir = temp_dir("enter");