diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index 3672ad45..0c35d957 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -40,48 +40,59 @@ pub(crate) fn ask_to_save(app: &mut TextureCraftApp, then: crate::state::Unsaved /// (in the browser, Save downloads the project). fn unsaved(app: &mut TextureCraftApp, ctx: &egui::Context) { use crate::state::Unsaved; + if ctx.input(|i| i.viewport().close_requested()) && ask_to_save(app, Unsaved::Quit) { ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); } - let Some(then) = app.ui.windows.unsaved.clone() else { return }; + let Some(pending) = app.ui.windows.unsaved.clone() else { return }; + let mut open = true; - let mut save = None; + let mut save_first = None; let mut cancel = false; - let name: [(&str, &dyn std::fmt::Display); 1] = [("name", &app.session.doc.project.name)]; - let question = - if then == Unsaved::Quit { trf("Save the changes to {name} before closing?", &name) } else { trf("Save the changes to {name} first?", &name) }; + let question_args: [(&str, &dyn std::fmt::Display); 1] = [("name", &app.session.doc.project.name)]; + let question = if pending == Unsaved::Quit { + trf("Save the changes to {name} before closing?", &question_args) + } else { + trf("Save the changes to {name} first?", &question_args) + }; dialog("unsaved", tr("Unsaved changes"), &mut open).show(ctx, |ui| { ui.label(question); ui.add_space(8.0); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - if ui.add(primary_button(ui, tr("Save"))).clicked() { - save = Some(true); - } - if ui.button(tr("Don't save")).clicked() { - save = Some(false); - } - if ui.button(tr("Cancel")).clicked() { - cancel = true; - } + + // One row keeps the window to its contents; a bare right-to-left layout fills the screen height + ui.horizontal(|ui| { + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.add(primary_button(ui, tr("Save"))).clicked() { + save_first = Some(true); + } + if ui.button(tr("Don't save")).clicked() { + save_first = Some(false); + } + if ui.button(tr("Cancel")).clicked() { + cancel = true; + } + }); }); }); if !open || cancel { app.ui.windows.unsaved = None; } - match save { + + match save_first { Some(true) => { file_action(app, ctx, "file.save"); } - // Going ahead anyway: the changes are let go. + // Going ahead anyway: the changes are let go Some(false) => app.session.modified = false, None => return, } - // A cancelled or failed save keeps the project and this dialog open. + + // A cancelled or failed save keeps the project and this dialog open if app.session.modified { return; } app.ui.windows.unsaved = None; - match then { + match pending { Unsaved::Quit => ctx.send_viewport_cmd(egui::ViewportCommand::Close), Unsaved::Command(id) => crate::menus::invoke(app, ctx, &id), #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/tests/close_unsaved.rs b/crates/ui-egui/tests/close_unsaved.rs index 9f518f18..a0c5481d 100644 --- a/crates/ui-egui/tests/close_unsaved.rs +++ b/crates/ui-egui/tests/close_unsaved.rs @@ -90,3 +90,17 @@ fn closing_the_project_with_changes_asks_first() { assert_ne!(name(&h), "Edited", "Don't save closes the project"); assert!(h.state().ui.windows.unsaved.is_none() && !h.state().session.modified); } + +/// The dialog fits one sentence and one button row instead of growing to the screen height +#[test] +fn the_dialog_fits_its_contents() { + // The title bar, one line, one button row and margins take 119 px at 1x and the bug made it 420; the 31 px left over absorbs font and theme tweaks + const MAX_HEIGHT: f32 = 150.0; + + let mut h = harness(true); + request_close(&mut h); + h.run_steps(2); + + let dialog = h.ctx.memory(|m| m.area_rect(egui::Id::unique("unsaved"))).expect("the dialog is shown"); + assert!(dialog.height() < MAX_HEIGHT, "the dialog is {} px tall", dialog.height()); +}