From 98a6ed7fc759688b4e90a8aca59f37bb7d60204b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 01:06:23 -0800 Subject: [PATCH] Scripts: each view is sized as the area its document was last drawn in Since the move to the craft UI framework each document's view keeps the area it last drew the document in, so Document.views now gives a view for every document the app has drawn, in a tab, a tiled pane or a floating window, sized as that area. A document the app has not drawn yet has no view. View and Views also get toString, as the other script objects have. --- crates/engine/src/script/dom.rs | 2 ++ crates/engine/src/tests/script_dom.rs | 4 ++- crates/ui-egui/src/doc_windows/tests.rs | 17 ++++++------- crates/ui-egui/src/script_runs.rs | 33 +++++++++++-------------- crates/ui-egui/src/tests/script_run.rs | 2 +- docs/mcp.md | 6 ++--- 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 9ddde38e80..9659514491 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -2305,6 +2305,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(), ("length", get(l, views_length)), ("parent", get(l, views_parent)), ("index", method(l, views_index)), + ("toString", class_text("Views")), ]), Some(function(l, views_at)), )); @@ -2318,6 +2319,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(), ("centerPoint", property(l, view_center, view_set_center)), ("zoom", property(l, view_zoom, view_set_zoom)), ("screenMode", property(l, view_screen_mode, view_set_screen_mode)), + ("toString", class_text("View")), ]), None, )); diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index f8aa54db37..2721923650 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -222,7 +222,7 @@ fn the_view_shows_the_part_of_the_document_its_window_shows() { let out = log( &mut s, "var d = app.activeDocument; var v = d.views[0]; vc.log(d.views.length); vc.log(v.typename);\ - vc.log(v.parent === d); vc.log(v.bounds); var r = d.pageItems[d.pageItems.length - 1];\ + vc.log(v.parent === d); vc.log(v.bounds); var r = d.pageItems[0];\ vc.log(r.geometricBounds); vc.log(v.centerPoint); vc.log(r.position[0] + r.width / 2);\ vc.log(r.position[1] - r.height / 2);", ); @@ -234,6 +234,8 @@ fn the_view_shows_the_part_of_the_document_its_window_shows() { let turned = log(&mut s, "vc.log(app.activeDocument.views[0].bounds);"); let width = |b: &Value| b[2].as_f64().unwrap() - b[0].as_f64().unwrap(); assert!(width(&turned[0]) > width(&out[3])); + let names = log(&mut s, "var d = app.activeDocument; vc.log(String(d.views) + String(d.views[0]));"); + assert_eq!(names, vec![json!("[Views][View]")]); } #[test] diff --git a/crates/ui-egui/src/doc_windows/tests.rs b/crates/ui-egui/src/doc_windows/tests.rs index a985c261a6..cf21effb94 100644 --- a/crates/ui-egui/src/doc_windows/tests.rs +++ b/crates/ui-egui/src/doc_windows/tests.rs @@ -191,20 +191,19 @@ fn float_in_window_moves_the_current_document_out_of_the_tabs() { } #[test] -fn a_script_sees_a_view_only_for_windows_of_the_canvass_size() { +fn a_script_sees_each_window_at_its_own_size() { let mut app = app_with(3); - let ctx = egui::Context::default(); + let ctx = crate::testing::ui_context(); frame(&mut app, &ctx, vec![]); - let uids: Vec = app.session.documents().iter().map(|d| d.uid).collect(); - let shared = |app: &mut VectorcraftApp| { - app.views.iter_mut().for_each(|v| v.fitted = true); - crate::script_runs::share_views(app).into_iter().map(|(uid, _)| uid).collect::>() - }; - assert_eq!(shared(&mut app), uids, "tabbed documents share the canvas's size"); app.run("window.arrange.floatInWindow", json!({})).unwrap(); frame(&mut app, &ctx, vec![]); frame(&mut app, &ctx, vec![]); - assert_eq!(shared(&mut app), [uids[2]], "only the current document's window holds the canvas"); + let uids: Vec = app.session.documents().iter().map(|d| d.uid).collect(); + let shared = crate::script_runs::share_views(&mut app); + let width = |uid: u64| shared.iter().find(|(u, _)| *u == uid).map(|(_, v)| v.size[0]); + let (floating, tabbed) = (width(uids[2]).unwrap(), width(uids[0]).unwrap()); + assert!(floating < tabbed, "the floating window is narrower than the main window: {floating} and {tabbed}"); + assert_eq!(width(uids[1]), None, "a document never drawn has no view"); } #[test] diff --git a/crates/ui-egui/src/script_runs.rs b/crates/ui-egui/src/script_runs.rs index e1d2e0b0de..14364ad861 100644 --- a/crates/ui-egui/src/script_runs.rs +++ b/crates/ui-egui/src/script_runs.rs @@ -40,26 +40,23 @@ pub fn poll(app: &mut VectorcraftApp) { app.sync_views(); } -/// Gives each document whose window has the canvas's size its view, which a running script reads -/// as `Document.views`: every document's while the documents are under the tabs, and only the -/// current document's, whose window holds the canvas, while they are tiled or a document floats in -/// a window of its own. A document without one gets none. → the views given, for [`take_views`]. +/// Gives each document whose view has been drawn its view, which a running script reads as +/// `Document.views`, with the size of the area that view last drew its art in. A document without +/// one gets none. → the views given, for [`take_views`]. pub(crate) fn share_views(app: &mut VectorcraftApp) -> Vec<(u64, WindowView)> { - let ui_zoom = f64::from(app.canvas_ui_zoom); - let size = app.canvas_rect.map(|r| [f64::from(r.width()) * ui_zoom, f64::from(r.height()) * ui_zoom]); + // The view's zoom counts screen points before UI scaling; its area is in the interface's points. + let scaling = f64::from(app.ui_scaling()); let mode = app.ui.screen_mode; - let tabbed = app.canvas.panes.last.is_empty() && app.ui.floating.is_empty(); - let current = app.session.active().map(|d| d.uid); - let shared: Vec<(u64, WindowView)> = match size { - Some(size) => app - .view_uids - .iter() - .zip(&app.views) - .filter(|(uid, v)| v.fitted && (tabbed || current == Some(**uid))) - .map(|(uid, v)| (*uid, WindowView { center: v.center, zoom: v.zoom, rotation: v.rotation, size, screen_mode: mode })) - .collect(), - None => vec![], - }; + let shared: Vec<(u64, WindowView)> = app + .view_uids + .iter() + .zip(&app.views) + .filter(|(_, v)| v.fitted && v.rect.width() > 0.0 && v.rect.height() > 0.0) + .map(|(uid, v)| { + let size = [f64::from(v.rect.width()) * scaling, f64::from(v.rect.height()) * scaling]; + (*uid, WindowView { center: v.center, zoom: v.zoom, rotation: v.rotation, size, screen_mode: mode }) + }) + .collect(); app.session.window_views.clone_from(&shared); shared } diff --git a/crates/ui-egui/src/tests/script_run.rs b/crates/ui-egui/src/tests/script_run.rs index 1c8406cdb9..28a3c32649 100644 --- a/crates/ui-egui/src/tests/script_run.rs +++ b/crates/ui-egui/src/tests/script_run.rs @@ -469,9 +469,9 @@ fn a_scripts_file_dialog_keeps_at_most_2048_picked_files() { #[test] fn a_script_sees_the_window_and_sets_its_zoom_centre_and_screen_mode() { let mut app = app(); - app.canvas_rect = Some(egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(600.0, 400.0))); let before = { let v = app.view_mut().unwrap(); + v.rect = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(600.0, 400.0)); v.fitted = true; v.zoom = 1.0; *v diff --git a/docs/mcp.md b/docs/mcp.md index a9f1575d6e..a62288107f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2694,9 +2694,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot `showOptionsDialog` true throws `Dialog unavailable` with no document made. A `Document` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) has `name`, `width`, `height`, `rulerUnits`, `documentColorSpace`, `rulerOrigin`, `convertCoordinate(point, source, destination)`, `pageItems`, `selection` and `views`. `views` (https://ai-scripting.docsforadobe.dev/jsobjref/Views/) - holds one view while the document's window is the desktop app's canvas: every document's while the documents are - under the tabs, and only the current document's while they are tiled or one floats in a window of its own. Other - documents, and every document without a window (headless), have none. `views` has `length`, `[i]` and `index(i)`. The view (https://ai-scripting.docsforadobe.dev/jsobjref/View/) has `typename`, + holds one view once the desktop app has drawn the document in a window, a tab or a tiled pane, sized as the area it + last drew the document in. A document the app has not drawn yet, and every document without a window (headless), + has none. `views` has `length`, `[i]` and `index(i)`. The view (https://ai-scripting.docsforadobe.dev/jsobjref/View/) has `typename`, `parent`, `bounds` (`[left, top, right, bottom]` of the part of the document the canvas shows, the box around a rotated canvas), `centerPoint`, `zoom` and `screenMode`, in the run's coordinate system. `zoom` is 1 at 100% and goes from 0.0313 to 640 (VectorCraft's own range); a zoom outside it throws a `RangeError`. `screenMode` reads Normal as