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.
This commit is contained in:
bflatastic committed 2026-11-02 08:24:29 +07:00
1 parent 108d790bcc
commit f48d5b6ba4
14 files changed
+91 -25

No files matched your search

+1
View File
@@ -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} أولًا؟"),
];
+1
View File
@@ -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?"),
];
+1
View File
@@ -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}?"),
];
+1
View File
@@ -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} ?"),
];
+1
View File
@@ -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}?"),
];
+1
View File
@@ -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} の変更を保存しますか?"),
];
+1
View File
@@ -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}의 변경 사항을 저장할까요?"),
];
+1
View File
@@ -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}?"),
];
+1
View File
@@ -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} 的更改?"),
];
+6
View File
@@ -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;
+12 -2
View File
@@ -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<Unsaved>,
}
/// 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.
+38 -16
View File
@@ -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;
+23 -5
View File
@@ -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);
}
+3 -2
View File
@@ -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