From f48d5b6ba41dc080ec4de9386712bd0f46503475 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 2 Nov 2026 08:24:29 +0700 Subject: [PATCH] Ask to save before opening, starting or closing another project New project, Open, a recent project, a dropped file, Open sample project and Close replaced a project with unsaved changes without a word. They now wait on the same Save / Don't save / Cancel dialog as quitting, and go ahead once the changes are saved or let go. --- crates/ui-egui/src/i18n/ar.rs | 1 + crates/ui-egui/src/i18n/de.rs | 1 + crates/ui-egui/src/i18n/es.rs | 1 + crates/ui-egui/src/i18n/fr.rs | 1 + crates/ui-egui/src/i18n/it.rs | 1 + crates/ui-egui/src/i18n/ja.rs | 1 + crates/ui-egui/src/i18n/ko.rs | 1 + crates/ui-egui/src/i18n/pt_br.rs | 1 + crates/ui-egui/src/i18n/zh_hans.rs | 1 + crates/ui-egui/src/menus.rs | 6 +++ crates/ui-egui/src/state.rs | 14 ++++++- crates/ui-egui/src/windows.rs | 54 +++++++++++++++++++-------- crates/ui-egui/tests/close_unsaved.rs | 28 +++++++++++--- docs/features.md | 5 ++- 14 files changed, 91 insertions(+), 25 deletions(-) diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rs index 9ce7ad03..9038f41e 100644 --- a/crates/ui-egui/src/i18n/ar.rs +++ b/crates/ui-egui/src/i18n/ar.rs @@ -947,4 +947,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "تغييرات غير محفوظة"), ("Don't save", "عدم الحفظ"), ("Save the changes to {name} before closing?", "هل تريد حفظ التغييرات في {name} قبل الإغلاق؟"), + ("Save the changes to {name} first?", "هل تريد حفظ التغييرات في {name} أولًا؟"), ]; diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rs index eb50e2ff..33a659a9 100644 --- a/crates/ui-egui/src/i18n/de.rs +++ b/crates/ui-egui/src/i18n/de.rs @@ -1002,4 +1002,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "Ungespeicherte Änderungen"), ("Don't save", "Nicht speichern"), ("Save the changes to {name} before closing?", "Änderungen an {name} vor dem Schließen speichern?"), + ("Save the changes to {name} first?", "Änderungen an {name} zuerst speichern?"), ]; diff --git a/crates/ui-egui/src/i18n/es.rs b/crates/ui-egui/src/i18n/es.rs index c415b5f9..2622b67d 100644 --- a/crates/ui-egui/src/i18n/es.rs +++ b/crates/ui-egui/src/i18n/es.rs @@ -987,4 +987,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "Cambios sin guardar"), ("Don't save", "No guardar"), ("Save the changes to {name} before closing?", "¿Guardar los cambios de {name} antes de cerrar?"), + ("Save the changes to {name} first?", "¿Guardar primero los cambios de {name}?"), ]; diff --git a/crates/ui-egui/src/i18n/fr.rs b/crates/ui-egui/src/i18n/fr.rs index 404f7451..5e95e20c 100644 --- a/crates/ui-egui/src/i18n/fr.rs +++ b/crates/ui-egui/src/i18n/fr.rs @@ -1002,4 +1002,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "Modifications non enregistrées"), ("Don't save", "Ne pas enregistrer"), ("Save the changes to {name} before closing?", "Enregistrer les modifications de {name} avant de fermer ?"), + ("Save the changes to {name} first?", "Enregistrer d'abord les modifications de {name} ?"), ]; diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rs index 3734112b..769fe223 100644 --- a/crates/ui-egui/src/i18n/it.rs +++ b/crates/ui-egui/src/i18n/it.rs @@ -996,4 +996,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "Modifiche non salvate"), ("Don't save", "Non salvare"), ("Save the changes to {name} before closing?", "Salvare le modifiche a {name} prima di chiudere?"), + ("Save the changes to {name} first?", "Salvare prima le modifiche a {name}?"), ]; diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 62e3f15b..523048f0 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -969,4 +969,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "未保存の変更"), ("Don't save", "保存しない"), ("Save the changes to {name} before closing?", "閉じる前に {name} の変更を保存しますか?"), + ("Save the changes to {name} first?", "先に {name} の変更を保存しますか?"), ]; diff --git a/crates/ui-egui/src/i18n/ko.rs b/crates/ui-egui/src/i18n/ko.rs index d8b614f5..ca38a29d 100644 --- a/crates/ui-egui/src/i18n/ko.rs +++ b/crates/ui-egui/src/i18n/ko.rs @@ -954,4 +954,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "저장되지 않은 변경 사항"), ("Don't save", "저장 안 함"), ("Save the changes to {name} before closing?", "닫기 전에 {name}의 변경 사항을 저장할까요?"), + ("Save the changes to {name} first?", "먼저 {name}의 변경 사항을 저장할까요?"), ]; diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rs index 9d1d9523..47807f08 100644 --- a/crates/ui-egui/src/i18n/pt_br.rs +++ b/crates/ui-egui/src/i18n/pt_br.rs @@ -984,4 +984,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "Alterações não salvas"), ("Don't save", "Não salvar"), ("Save the changes to {name} before closing?", "Salvar as alterações em {name} antes de fechar?"), + ("Save the changes to {name} first?", "Salvar primeiro as alterações em {name}?"), ]; diff --git a/crates/ui-egui/src/i18n/zh_hans.rs b/crates/ui-egui/src/i18n/zh_hans.rs index c62467da..9b6144f4 100644 --- a/crates/ui-egui/src/i18n/zh_hans.rs +++ b/crates/ui-egui/src/i18n/zh_hans.rs @@ -900,4 +900,5 @@ pub const TABLE: &[(&str, &str)] = &[ ("Unsaved changes", "未保存的更改"), ("Don't save", "不保存"), ("Save the changes to {name} before closing?", "关闭前是否保存对 {name} 的更改?"), + ("Save the changes to {name} first?", "是否先保存对 {name} 的更改?"), ]; diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 6cf976a1..d5e5fe3c 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -117,6 +117,12 @@ pub fn implemented(id: &str) -> bool { /// Invoke a menu item (menus, shortcuts, palette, control channel `ui.menu.invoke`). pub fn invoke(app: &mut TextureCraftApp, ctx: &egui::Context, id: &str) { + #[cfg(not(target_arch = "wasm32"))] + if matches!(id, "ui.newProject" | "file.open" | "file.close" | "project.sample") + && crate::windows::ask_to_save(app, crate::state::Unsaved::Command(id.into())) + { + return; + } if let Some(panel) = crate::docking::view_panel(id) { app.ui.dock.toggle(panel); return; diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 6670c608..ef0ab709 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -76,9 +76,19 @@ pub struct Windows { /// The start window: new, open, recent projects (the app opens it at launch). #[serde(skip)] pub start: bool, - /// Closing with unsaved changes: save, don't save or cancel (desktop). + /// What waits on the unsaved-changes dialog: save, don't save or cancel (desktop). #[serde(skip)] - pub confirm_close: bool, + pub unsaved: Option, +} + +/// An action that replaces or ends the project, held while the project has unsaved changes. +#[derive(Clone, Debug, PartialEq)] +pub enum Unsaved { + Quit, + /// A menu command (New project, Open, Close, Open sample project). + Command(String), + /// Open this project or mesh file. + Open(String), } /// Edit › Settings. diff --git a/crates/ui-egui/src/windows.rs b/crates/ui-egui/src/windows.rs index 8a8ba0b6..68fd1214 100644 --- a/crates/ui-egui/src/windows.rs +++ b/crates/ui-egui/src/windows.rs @@ -30,24 +30,35 @@ pub fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { bake_bar(app, ctx); } #[cfg(not(target_arch = "wasm32"))] - confirm_close(app, ctx); + unsaved(app, ctx); } -/// Closing the app with unsaved changes asks whether to save them first. +/// Hold `then` (quitting, opening or closing a project) behind the unsaved-changes dialog when +/// the project has unsaved changes; false when it can go ahead now. #[cfg(not(target_arch = "wasm32"))] -fn confirm_close(app: &mut TextureCraftApp, ctx: &egui::Context) { - if app.session.modified && ctx.input(|i| i.viewport().close_requested()) { +pub(crate) fn ask_to_save(app: &mut TextureCraftApp, then: crate::state::Unsaved) -> bool { + if app.session.modified { + app.ui.windows.unsaved = Some(then); + } + app.session.modified +} + +/// Quitting, opening or closing a project with unsaved changes asks whether to save them first. +#[cfg(not(target_arch = "wasm32"))] +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); - app.ui.windows.confirm_close = true; - } - if !app.ui.windows.confirm_close { - return; } + let Some(then) = app.ui.windows.unsaved.clone() else { return }; let mut open = true; let mut save = None; let mut cancel = false; - dialog("confirm_close", tr("Unsaved changes"), &mut open).show(ctx, |ui| { - ui.label(trf("Save the changes to {name} before closing?", &[("name", &app.session.doc.project.name)])); + 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) }; + 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() { @@ -61,19 +72,26 @@ fn confirm_close(app: &mut TextureCraftApp, ctx: &egui::Context) { } }); }); - app.ui.windows.confirm_close = open && !cancel; + if !open || cancel { + app.ui.windows.unsaved = None; + } match save { Some(true) => { file_action(app, ctx, "file.save"); } - // Closing 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 app and this dialog open. - if !app.session.modified { - app.ui.windows.confirm_close = false; - ctx.send_viewport_cmd(egui::ViewportCommand::Close); + // A cancelled or failed save keeps the project and this dialog open. + if app.session.modified { + return; + } + app.ui.windows.unsaved = None; + match then { + Unsaved::Quit => ctx.send_viewport_cmd(egui::ViewportCommand::Close), + Unsaved::Command(id) => crate::menus::invoke(app, ctx, &id), + Unsaved::Open(path) => open_path(app, &path), } } @@ -1045,6 +1063,10 @@ pub fn file_action(app: &mut TextureCraftApp, _ctx: &egui::Context, id: &str) -> /// Open a project or mesh by path (menus, Recent projects, drag and drop). #[cfg(not(target_arch = "wasm32"))] pub fn open_path(app: &mut TextureCraftApp, path: &str) { + #[cfg(not(target_arch = "wasm32"))] + if ask_to_save(app, crate::state::Unsaved::Open(path.into())) { + return; + } if app.run("file.open", json!({"path": path})).is_some() { remember(app, path); app.ui.mode = Mode::Painting; diff --git a/crates/ui-egui/tests/close_unsaved.rs b/crates/ui-egui/tests/close_unsaved.rs index 88811700..468634cc 100644 --- a/crates/ui-egui/tests/close_unsaved.rs +++ b/crates/ui-egui/tests/close_unsaved.rs @@ -1,9 +1,11 @@ -//! Closing the app with unsaved changes asks whether to save them first. +//! Closing the app, or opening or closing a project, with unsaved changes asks whether to save +//! them first. use egui::{ViewportCommand, ViewportEvent, ViewportId}; use egui_kittest::Harness; use egui_kittest::kittest::Queryable; use texturecraft_ui_egui::TextureCraftApp; +use texturecraft_ui_egui::state::Unsaved; fn harness(modified: bool) -> Harness<'static, TextureCraftApp> { let mut session = texturecraft_engine::sample_session(256, 64).unwrap(); @@ -31,11 +33,11 @@ fn closing_with_changes_asks_and_dont_save_closes() { request_close(&mut h); assert!(sent(&h, &ViewportCommand::CancelClose), "the close waits for an answer"); h.run_steps(2); - assert!(h.state().ui.windows.confirm_close, "the dialog is open"); + assert_eq!(h.state().ui.windows.unsaved, Some(Unsaved::Quit), "the dialog is open"); h.get_by_label("Don't save").click(); h.step(); assert!(sent(&h, &ViewportCommand::Close), "Don't save closes the app"); - assert!(!h.state().ui.windows.confirm_close); + assert!(h.state().ui.windows.unsaved.is_none()); } #[test] @@ -46,7 +48,7 @@ fn cancel_keeps_the_app_open() { h.get_by_label("Cancel").click(); h.step(); assert!(!sent(&h, &ViewportCommand::Close)); - assert!(!h.state().ui.windows.confirm_close, "Cancel closes the dialog"); + assert!(h.state().ui.windows.unsaved.is_none(), "Cancel closes the dialog"); assert!(h.state().session.modified, "and keeps the changes"); } @@ -55,5 +57,21 @@ fn closing_without_changes_does_not_ask() { let mut h = harness(false); request_close(&mut h); assert!(!sent(&h, &ViewportCommand::CancelClose)); - assert!(!h.state().ui.windows.confirm_close); + assert!(h.state().ui.windows.unsaved.is_none()); +} + +#[test] +fn closing_the_project_with_changes_asks_first() { + let mut h = harness(true); + h.state_mut().session.doc.project.name = "Edited".into(); + let name = |h: &Harness<'_, TextureCraftApp>| h.state().session.doc.project.name.clone(); + let ctx = h.ctx.clone(); + texturecraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "file.close"); + h.run_steps(4); + assert_eq!(h.state().ui.windows.unsaved, Some(Unsaved::Command("file.close".into())), "the dialog is open"); + assert_eq!(name(&h), "Edited", "the project stays until answered"); + h.get_by_label("Don't save").click(); + h.run_steps(2); + assert_ne!(name(&h), "Edited", "Don't save closes the project"); + assert!(h.state().ui.windows.unsaved.is_none() && !h.state().session.modified); } diff --git a/docs/features.md b/docs/features.md index 1408a242..44d5b4de 100644 --- a/docs/features.md +++ b/docs/features.md @@ -67,8 +67,9 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA channel types to add or remove. Show, hide or invert them all at once, or **focus** on the active one. - **Projects** are a single `.tcraft` file: the mesh, every layer (sparse and lossless), baked maps - and imported images. **Project templates** start new work from a saved setup. Closing the app - with unsaved changes asks whether to save them first. + and imported images. **Project templates** start new work from a saved setup. Closing the app, + or opening, starting or closing another project, with unsaved changes asks whether to save them + first. ## Bake and export