Files
texturecraft/crates/ui-egui/tests/it/close_unsaved.rs
dotCooCoo 7f52b87b85 UI: use the shared craft-libs crates for dialogs, shortcuts and quit
Bumps craft-ui and craft-window to craft-libs 1b34eba4 and replaces the
app's own dialog, shortcut and quit code with the shared crates.

File dialogs go through craft-dialogs and no longer stop the frame. An
action that needs a path asks for it and continues when the dialog
answers. An answer that acts on the project does nothing, with a toast,
when another project was opened meanwhile. The browser build keeps its
pickers and the startup and crash message boxes stay blocking.

Shortcuts run through craft-keymap's dispatcher, in the main window and
in floating panels. Saved shortcuts keep their stored form, and the
Keyboard shortcuts window and the shortcut labels do not change. A
shortcut on a symbol that needs Shift is recorded without Shift, and
saved shortcuts in the old form still run. A shortcut now runs only on
its exact chord: an extra Shift or Alt no longer runs a plain-key
shortcut.

A window close quits through craft-quit, and the close check runs in
logic, so closing a minimized window with unsaved changes restores it and
asks. A close during a stroke finishes the stroke and asks. The macOS app
menu comes from craft-menu. The unsaved-changes prompt goes on once its
Save dialog has saved the project, and stays open after Cancel or a failed
save.

The window frame guard on Windows and the title bar's buttons stay in the
app, since craft-libs has no code for them.
2026-11-08 22:07:12 -08:00

122 lines
5.2 KiB
Rust

//! 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();
session.modified = modified;
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
h.run_steps(4);
h
}
/// The window's close button, for one frame.
fn request_close(h: &mut Harness<'_, TextureCraftApp>) {
h.input_mut().viewports.entry(ViewportId::ROOT).or_default().events.push(ViewportEvent::Close);
h.step();
h.input_mut().viewports.entry(ViewportId::ROOT).or_default().events.clear();
}
/// The viewport commands of the last frame.
fn sent(h: &Harness<'_, TextureCraftApp>, cmd: &ViewportCommand) -> bool {
h.output().viewport_output.get(&ViewportId::ROOT).is_some_and(|v| v.commands.contains(cmd))
}
#[test]
fn closing_with_changes_asks_and_dont_save_closes() {
let mut h = harness(true);
request_close(&mut h);
assert!(sent(&h, &ViewportCommand::CancelClose), "the close waits for an answer");
h.run_steps(2);
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.unsaved.is_none());
}
#[test]
fn cancel_keeps_the_app_open() {
let mut h = harness(true);
request_close(&mut h);
h.run_steps(2);
h.get_by_label("Cancel").click();
h.step();
assert!(!sent(&h, &ViewportCommand::Close));
assert!(h.state().ui.windows.unsaved.is_none(), "Cancel closes the dialog");
assert!(h.state().session.modified, "and keeps the changes");
}
#[test]
fn closing_without_changes_does_not_ask() {
let mut h = harness(false);
request_close(&mut h);
// macOS keeps the window for `-[NSApplication terminate:]` to close; elsewhere it closes now.
assert_eq!(sent(&h, &ViewportCommand::CancelClose), cfg!(target_os = "macos"));
assert!(h.state().ui.windows.unsaved.is_none());
}
/// `ui` does not run while the window is minimized, so the close decision is made in `logic`.
#[test]
fn closing_a_minimized_window_with_changes_brings_it_back_to_ask() {
let mut h = harness(true);
let root = h.input_mut().viewports.entry(ViewportId::ROOT).or_default();
root.minimized = Some(true);
root.events.push(ViewportEvent::Close);
h.step();
assert!(sent(&h, &ViewportCommand::CancelClose), "the close waits for an answer");
assert!(sent(&h, &ViewportCommand::Minimized(false)), "the window comes back");
assert!(sent(&h, &ViewportCommand::Focus));
assert_eq!(h.state().ui.windows.unsaved, Some(Unsaved::Quit));
}
/// A project picked or dropped in the browser arrives as bytes; it waits on the same dialog.
#[test]
fn a_dropped_project_waits_on_the_dialog() {
let mut h = harness(true);
let dropped = texturecraft_engine::Session::new_project("Dropped", texturecraft_engine::geom::primitives::cube(1.0), "builtin:cube", None, 128).unwrap();
texturecraft_ui_egui::web_files::load(h.state_mut(), "dropped.tcraft", dropped.to_project_bytes().unwrap());
h.run_steps(4);
assert!(matches!(h.state().ui.windows.unsaved, Some(Unsaved::Load(ref name, _)) if name == "dropped.tcraft"), "the dialog is open");
assert_ne!(h.state().session.doc.project.name, "Dropped", "the project stays until answered");
h.get_by_label("Don't save").click();
h.run_steps(2);
assert_eq!(h.state().session.doc.project.name, "Dropped", "Don't save opens it");
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);
}
/// 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());
}