Merge pull request #9480 from dotCooCoo/fix/ui-perspective-grid-document

Keep Define Perspective Grid with its document
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 05:24:23 +07:00
commit 57c482d248
2 files changed
+52 -5

No files matched your search

+19 -5
View File
@@ -134,6 +134,7 @@ fn open_on(app: &mut VectorcraftApp, def: GridDefinition, mode: GridMode) -> Res
let dialog = GridDialog::new(
def,
mode,
app.session.active().map(|st| st.uid),
(general_lengths(app).pixels_per_inch, app.session.prefs.numbers_without_units_are_points),
app.session.perspective_presets(),
app.session.new_perspective_preset_name(),
@@ -171,6 +172,8 @@ pub fn open_edit(app: &mut VectorcraftApp, original: Option<&str>, base: &str) -
pub(super) struct GridDialog {
id: DialogId,
mode: GridMode,
/// The document whose grid Define Grid edits.
source_document: Option<u64>,
draft: GridDraft,
/// The presets the Preset row offers, as the document and the preferences have them.
presets: Vec<GridDefinition>,
@@ -190,10 +193,18 @@ pub(super) struct GridDialog {
}
impl GridDialog {
fn new(def: GridDefinition, mode: GridMode, (ppi, bare_points): (f64, bool), presets: Vec<GridDefinition>, new_name: String) -> Self {
fn new(
def: GridDefinition,
mode: GridMode,
source_document: Option<u64>,
(ppi, bare_points): (f64, bool),
presets: Vec<GridDefinition>,
new_name: String,
) -> Self {
let mut dialog = GridDialog {
id: dialog_id(KIND),
mode,
source_document,
draft: GridDraft { def },
presets: Vec::new(),
rows: Vec::new(),
@@ -516,9 +527,12 @@ enum Next {
Presets(String),
}
fn file(app: &mut VectorcraftApp, mode: &GridMode, def: &GridDefinition) -> Result<Next, String> {
fn file(app: &mut VectorcraftApp, mode: &GridMode, source_document: Option<u64>, def: &GridDefinition) -> Result<Next, String> {
let mut params = serde_json::to_value(def).map_err(|e| e.to_string())?;
if *mode == GridMode::Define {
if app.session.active().map(|st| st.uid) != source_document {
return Err(tl!("Define Grid can only change the document where it was opened.").to_owned());
}
app.run("perspective.grid.define", params)?;
return Ok(Next::Close);
}
@@ -546,10 +560,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
let filed = dialogs.with(|host| {
let dialog = host.get_mut::<GridDialog>()?;
dialog.set_presets(presets, new_name);
dialog.held.take().map(|def| (dialog.mode.clone(), def))
dialog.held.take().map(|def| (dialog.mode.clone(), dialog.source_document, def))
});
let Some((mode, def)) = filed else { return };
let next = file(app, &mode, &def);
let Some((mode, source_document, def)) = filed else { return };
let next = file(app, &mode, source_document, &def);
if let Err(why) = &next {
app.status(why.clone());
}
@@ -104,6 +104,39 @@ fn define_grid_opens_on_the_grid_and_applies_its_fields() {
assert_eq!((grid(&app).cell, grid(&app).viewing_angle()), (n.cell, n.viewing_angle()));
}
#[test]
fn define_grid_applies_only_to_the_document_where_it_opened() {
let mut app = app();
app.run("file.new", json!({"width": 800, "height": 600})).unwrap();
let destination = app.session.active().unwrap();
let destination_uid = destination.uid;
let destination_undo = destination.history.undo.len();
let destination_grid = grid(&app);
app.run("document.activate", json!({"index": 0})).unwrap();
let source_uid = app.session.active().unwrap().uid;
app.run("ui.perspectiveGridDialog", json!({})).unwrap();
set(&mut app, "rightColor", json!("#112233")).unwrap();
let ctx = window(&app);
let id = dialog_id(super::perspective_grid::KIND);
host(&app, |h| {
assert!(!h.confirm_by_id(&ctx, id));
assert_eq!(h.held_state(id), Some(HeldOk::Waiting));
});
app.run("document.activate", json!({"index": 1})).unwrap();
super::perspective_grid::pump(&mut app, &ctx);
assert_eq!(app.session.active().unwrap().uid, destination_uid);
assert_eq!(grid(&app), destination_grid, "the first document's draft leaves the active grid alone");
assert_eq!(app.session.active().unwrap().history.undo.len(), destination_undo);
assert!(matches!(host(&app, |h| h.held_state(id)), Some(HeldOk::Refused(_))));
app.run("document.activate", json!({"index": 0})).unwrap();
assert_eq!(app.session.active().unwrap().uid, source_uid);
ok(&mut app);
assert_eq!(kind(&app), None);
assert_eq!(grid(&app).right_color.hex(), "#112233");
}
#[test]
fn define_grid_loads_and_saves_presets() {
let mut app = app();