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.
This commit is contained in:
dotCooCoo committed 2026-11-25 01:06:23 -08:00
1 parent a303ef2283
commit 98a6ed7fc7
6 files changed
+32 -32

No files matched your search

+2
View File
@@ -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,
));
+3 -1
View File
@@ -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]
+8 -9
View File
@@ -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<u64> = 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::<Vec<u64>>()
};
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<u64> = 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]
+15 -18
View File
@@ -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
}
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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