DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #146: File › Close, ⌘W and the tab × ask Save / Don't Save / Cancel for unsaved documents
aiongge80e4a656a14
diff --cc crates/ui-egui/src/lib.rsindex 471a98284,d81b0699c..8ad606305--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -889,45 -872,78 +889,117 @@@ pub fn drop_key_name_text(raw: &mut egumod tests {use super::*;+ /// The Control panel's two-row groups were centred as if they were one row tall, so the second+ /// row hung below the panel and was cut off.+ #[test]+ fn control_bar_shows_its_second_row() {+ use egui_kittest::kittest::Queryable as _;+ let mut app = DesignApp::new(designcraft_engine::Session::new(), Services::default());+ app.run("file.newSample", json!({})).unwrap();+ app.ui.control_bar = true;+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(+ |ui, app: &mut DesignApp| {+ let ctx = ui.ctx().clone();+ app.logic(&ctx);+ app.ui(ui);+ },+ app,+ );+ h.run_steps(4);+ let bar = egui::containers::panel::PanelState::load(&h.ctx, egui::Id::new("control_bar")).unwrap().outer_rect;+ assert!(bar.height() > 40.0, "{bar:?}");+ let rows: Vec<egui::Rect> = h.query_all_by_label("Y:").map(|n| n.rect()).filter(|r| bar.contains(r.left_top())).collect();+ assert_eq!(rows.len(), 1, "the Y: caption of the Control panel");+ assert!(rows[0].bottom() <= bar.bottom(), "Y: caption {:?} hangs below the Control panel {bar:?}", rows[0]);+ }++ #[test]+ fn key_names_sent_as_text_are_dropped() {+ let text = |t: &str| egui::Event::Text(t.to_string());+ let events = || {+ vec![text("AltGraph"), text("é"), text("Dead"), text("A"), text("Unidentified"), text("ß"), text("AudioVolumeUp"), text("ab"), text("Æ")]+ };+ let mut raw = egui::RawInput { events: events(), ..Default::default() };+ drop_key_name_text(&mut raw, false);+ assert_eq!(raw.events, vec![text("é"), text("A"), text("ß"), text("ab"), text("Æ")]);+ // A focused text field gets its text from input events, which can be whole words.+ let mut raw = egui::RawInput { events: events(), ..Default::default() };+ drop_key_name_text(&mut raw, true);+ assert_eq!(raw.events, events());+ }++ /// An app with one document that has an unsaved edit.+ fn app_with_unsaved_document() -> DesignApp {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), Services::default());+ app.run("file.new", json!({})).unwrap();+ assert!(!app.session.documents()[0].is_dirty(), "a new document has nothing to save");+ app.run("frame.create", json!({"rect": [36, 36, 136, 136]})).unwrap();+ assert!(app.session.documents()[0].is_dirty());+ app+ }++ /// File ▸ Close (and its shortcut) closed a document with unsaved changes without asking, and+ /// closing discards the document's recovery data too.+ #[test]+ fn closing_an_unsaved_document_asks_first() {+ let mut app = app_with_unsaved_document();+ menus::activate(&mut app, "file.close", &Value::Null);+ assert_eq!(app.session.documents().len(), 1, "the document stays open until the user answers");+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("closeDocument"));+ // Cancel: nothing happens.+ app.ui.dialog = None;+ assert!(app.session.documents()[0].is_dirty());+ // The tab's × asks too.+ menus::close_document(&mut app, Some(0));+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("closeDocument"));+ assert_eq!(app.session.documents().len(), 1);+ }++ #[test]+ fn closing_without_saving_discards_the_document() {+ let mut app = app_with_unsaved_document();+ menus::close_document(&mut app, None);+ app.ui.dialog.as_mut().unwrap().fields.insert("discard".into(), json!(true));+ dialogs::confirm(&mut app).unwrap();+ assert!(app.session.documents().is_empty());+ assert!(app.ui.dialog.is_none());+ }++ #[test]+ fn closing_with_save_writes_the_file_then_closes() {+ let mut app = app_with_unsaved_document();+ let path = std::env::temp_dir().join(format!("designcraft-close-test-{}.designcraft", std::process::id()));+ app.run("file.saveAs", json!({"path": path})).unwrap();+ let saved = std::fs::metadata(&path).unwrap().len();+ app.run("frame.create", json!({"rect": [200, 200, 300, 300]})).unwrap();+ menus::close_document(&mut app, None);+ dialogs::confirm(&mut app).unwrap();+ assert!(app.session.documents().is_empty());+ let written = std::fs::metadata(&path).unwrap().len();+ std::fs::remove_file(&path).unwrap();+ assert!(written > saved, "the second frame was saved ({saved} → {written} bytes)");+ }++ /// Save on a document that has no file opens the file picker; cancelling it must not close the+ /// document. (No picker service here: the same as cancelling.)+ #[test]+ fn a_cancelled_save_keeps_the_document_open() {+ let mut app = app_with_unsaved_document();+ menus::close_document(&mut app, None);+ dialogs::confirm(&mut app).unwrap();+ assert_eq!(app.session.documents().len(), 1);+ assert!(app.session.documents()[0].is_dirty());+ }++ #[test]+ fn closing_a_saved_document_does_not_ask() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), Services::default());+ app.run("file.new", json!({})).unwrap();+ menus::activate(&mut app, "file.close", &Value::Null);+ assert!(app.session.documents().is_empty());+ assert!(app.ui.dialog.is_none());+ }+#[test]fn snap_view_uses_the_saved_switches() {let mut ui = UiState::default();