mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9517 from dotCooCoo/fix/ui-package-opening-drawing
Keep Package tied to its opening drawing
This commit is contained in:
4 files changed
+54
-2
No files matched your search
@@ -242,6 +242,7 @@ impl VectorcraftApp {
|
||||
three_d_dialog: None,
|
||||
three_d: Default::default(),
|
||||
export_dialogs: Default::default(),
|
||||
package_document: None,
|
||||
web_export: Default::default(),
|
||||
flattener_preview: Default::default(),
|
||||
flatten_dialog: Default::default(),
|
||||
|
||||
@@ -55,6 +55,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
);
|
||||
return Ok(Value::Null);
|
||||
};
|
||||
let document = st.uid;
|
||||
let p = std::path::Path::new(&path);
|
||||
let stem = p.file_stem().map_or_else(|| "Untitled".into(), |s| s.to_string_lossy().into_owned());
|
||||
let folder = p.parent().map(|f| f.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
@@ -73,6 +74,7 @@ pub fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
.footer_note(Label::from(tl!("Fonts whose licence doesn't allow embedding are not copied.")))
|
||||
});
|
||||
crate::dialogs::host::open_held(app, dialog, |app, answer| confirm(app, &answer.fields).map(|_| ())).ok_or("no window")?;
|
||||
app.package_document = Some(document);
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
@@ -96,13 +98,27 @@ fn params(web: bool) -> Vec<Param> {
|
||||
pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
let id = dialog_id(KEY);
|
||||
let Some(request) = dialogs.with(|host| host.get_mut_by_id::<HostedParamDialog>(id).and_then(HostedParamDialog::pick_path)) else { return };
|
||||
file_asks::browse(app, id, request, PickRequest::folder(PURPOSE), file_asks::answer_path);
|
||||
let Some(request) = dialogs.with(|host| host.get_mut_by_id::<HostedParamDialog>(id).map(HostedParamDialog::pick_path)) else {
|
||||
app.package_document = None;
|
||||
return;
|
||||
};
|
||||
if let Some(request) = request {
|
||||
file_asks::browse(app, id, request, PickRequest::folder(PURPOSE), file_asks::answer_path);
|
||||
}
|
||||
}
|
||||
|
||||
fn require_package_document(app: &VectorcraftApp, document: u64) -> Result<(), String> {
|
||||
if app.session.active().is_none_or(|st| st.uid != document) {
|
||||
return Err(tl!("Package can only save and collect the document where it was opened.").to_owned());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// What OK does: save a dirty document, then write the package (on the worker with Background Export on) or
|
||||
/// download it. `Err` refuses the OK and keeps the dialog open; the reply says whether the package is in the background.
|
||||
pub(super) fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result<Value, String> {
|
||||
let document = app.package_document.ok_or("no Package dialog open")?;
|
||||
require_package_document(app, document)?;
|
||||
let text = |key: &str| fields.text(key).unwrap_or_default();
|
||||
let mut params = json!({ "folder": text("folder"), "name": text("name") });
|
||||
for (key, _) in OPTIONS {
|
||||
@@ -144,6 +160,8 @@ pub(super) fn confirm(app: &mut VectorcraftApp, fields: &DialogFields) -> Result
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// A completed background job may have changed the active document while Package waited.
|
||||
require_package_document(app, document)?;
|
||||
let package = app.session.prepare_package(¶ms).map_err(|e| e.to_string()).inspect_err(|e| app.ui.status = e.clone())?;
|
||||
let label = format!("Packaging {}", text("name"));
|
||||
let later = Rc::new(Cell::new(false));
|
||||
|
||||
@@ -224,6 +224,37 @@ fn package_dialog(app: &mut VectorcraftApp, out: &str) {
|
||||
set(app, "folder", json!(out)).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_opening_drawing_is_required_before_saving_or_packaging() {
|
||||
let dir = Folder::new("opening-drawing");
|
||||
let mut app = saved(&dir);
|
||||
let source = app.session.active().unwrap().uid;
|
||||
app.run("file.new", json!({"width": 300, "height": 300})).unwrap();
|
||||
let destination = app.session.active().unwrap().uid;
|
||||
let destination_path = dir.path("destination.vectorcraft");
|
||||
app.run("document.save", json!({"path": destination_path})).unwrap();
|
||||
let saved_destination = std::fs::read(&destination_path).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap();
|
||||
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().uid, source);
|
||||
let out = dir.path("out");
|
||||
package_dialog(&mut app, &out);
|
||||
app.run("document.activate", json!({"index": 1})).unwrap();
|
||||
assert_eq!(app.session.active().unwrap().uid, destination);
|
||||
let accepted = press_ok(&mut app);
|
||||
assert!(app.session.active().unwrap().is_dirty(), "Package leaves the different drawing unsaved");
|
||||
assert_eq!(std::fs::read(&destination_path).unwrap(), saved_destination);
|
||||
let package = std::path::Path::new(&out).join("card Folder");
|
||||
assert!(!package.exists());
|
||||
assert!(!accepted && matches!(held_state(&app), Some(HeldOk::Refused(_))));
|
||||
|
||||
app.run("document.activate", json!({"index": 0})).unwrap();
|
||||
assert!(press_ok(&mut app));
|
||||
assert!(package.join("card.vectorcraft").is_file());
|
||||
assert!(!package.join("destination.vectorcraft").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn package_is_written_in_the_background_as_it_is_at_once() {
|
||||
let dir = Folder::new("background");
|
||||
|
||||
@@ -288,6 +288,8 @@ pub struct VectorcraftApp {
|
||||
pub(crate) three_d: panels::three_d::Model,
|
||||
/// The open export option dialogs and what runs after each.
|
||||
pub(crate) export_dialogs: dialogs::export_host::ExportDialogs,
|
||||
/// The drawing the open Package dialog saves and collects.
|
||||
pub(crate) package_document: Option<u64>,
|
||||
/// Save for Web's renders and colour table.
|
||||
pub(crate) web_export: dialogs::save_for_web::WebExport,
|
||||
/// The Flattener Preview panel's model.
|
||||
|
||||
Reference in new issue
Block a user