UI: fit the unsaved-changes dialog to its contents

The button row was a right-to-left layout placed straight in the dialog
body, which centres its contents in all the remaining height, so the
auto-sized window grew to 420 px. Wrapping it in a horizontal row (as the
New project dialog does) brings it to 119 px. Adds a regression test on
the dialog's window height.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RebornimandClaude Opus 5.5 committed 2026-11-06 00:04:49 +00:00
1 parent 807c2818cb
commit d9bc1ef6d8
2 files changed
+44 -19

No files matched your search

+30 -19
View File
@@ -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"))]
+14
View File
@@ -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());
}