mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Use each window's displayed zoom in script views
This commit is contained in:
8 files changed
+116
-14
No files matched your search
@@ -8,8 +8,10 @@ use super::{Arc, Document, NodeId, NodeKind, Selection, Value, cmd, file_access}
|
||||
pub struct WindowView {
|
||||
/// The document point at the centre of the canvas.
|
||||
pub center: vectorcraft_geom::Point,
|
||||
/// The zoom: 1 is 100%.
|
||||
/// The zoom is 1 at the window's displayed 100%.
|
||||
pub zoom: f64,
|
||||
/// The camera uses this scale at the window's displayed 100%.
|
||||
pub zoom_baseline: f64,
|
||||
/// The view's rotation in degrees, clockwise on screen.
|
||||
pub rotation: f64,
|
||||
/// The canvas's width and height in document points at a zoom of 1.
|
||||
@@ -20,9 +22,15 @@ pub struct WindowView {
|
||||
}
|
||||
|
||||
impl WindowView {
|
||||
/// The smallest and largest zoom the canvas shows.
|
||||
/// The physical camera scale stays within these limits.
|
||||
pub const ZOOM_RANGE: (f64, f64) = (0.0313, 640.0);
|
||||
|
||||
/// `zoom_range` returns this window's allowed displayed zoom ratios.
|
||||
pub fn zoom_range(&self) -> (f64, f64) {
|
||||
let (min, max) = Self::ZOOM_RANGE;
|
||||
(min / self.zoom_baseline, max / self.zoom_baseline)
|
||||
}
|
||||
|
||||
/// The part of the document the canvas shows: the box around its four corners, which a
|
||||
/// rotated view turns.
|
||||
pub fn visible(&self) -> vectorcraft_geom::Rect {
|
||||
|
||||
@@ -46,7 +46,9 @@
|
||||
//! - `Document.views` holds the document's drawn windows ([`crate::Session::window_views`]),
|
||||
//! in view-number order, and none without one. View objects keep their window's identity when
|
||||
//! windows are renumbered. The screen mode is the app's:
|
||||
//! setting it on one view sets it on every view. Its `zoom` is 1 at 100%, from 0.0313 to 640; `screenMode` reads
|
||||
//! setting it on one view sets it on every view. Its `zoom` is 1 at the window's displayed 100%;
|
||||
//! the physical camera scale ranges from 0.0313 to 640, and the allowed zoom ratios account for
|
||||
//! the window's display density with Display Print Size at 100% Zoom enabled. `screenMode` reads
|
||||
//! Presentation Mode as `ScreenMode.FULLSCREEN`. Changing a view runs no command, is not
|
||||
//! undoable and stays when the run fails.
|
||||
//! A center outside the canvas after coordinate conversion throws a `RangeError`.
|
||||
@@ -1457,9 +1459,9 @@ fn view_zoom(h: &Host, a: &[Data]) -> Answer {
|
||||
}
|
||||
|
||||
fn view_set_zoom(h: &Host, a: &[Data]) -> Answer {
|
||||
let (doc, id, _) = this_view(h, a)?;
|
||||
let (doc, id, v) = this_view(h, a)?;
|
||||
let z = number(arg(a, 1), "zoom")?;
|
||||
let (min, max) = WindowView::ZOOM_RANGE;
|
||||
let (min, max) = v.zoom_range();
|
||||
if !(min..=max).contains(&z) {
|
||||
return Err(HostError::Range(format!("zoom must be from {min} to {max}")));
|
||||
}
|
||||
|
||||
@@ -195,7 +195,14 @@ fn ruler_origin_round_trips_without_moving_art() {
|
||||
|
||||
/// A window on the document of [`doc`] that shows 300 × 200 pt around (200, 150) at 200%.
|
||||
fn window() -> WindowView {
|
||||
WindowView { center: vectorcraft_geom::Point::new(200.0, 150.0), zoom: 2.0, rotation: 0.0, size: [600.0, 400.0], screen_mode: 0 }
|
||||
WindowView {
|
||||
center: vectorcraft_geom::Point::new(200.0, 150.0),
|
||||
zoom: 2.0,
|
||||
zoom_baseline: 1.0,
|
||||
rotation: 0.0,
|
||||
size: [600.0, 400.0],
|
||||
screen_mode: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the active document in the window `v`.
|
||||
|
||||
@@ -206,9 +206,86 @@ fn a_script_sees_each_window_at_its_own_size() {
|
||||
assert_eq!(width(uids[1]), None, "a document never drawn has no view");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_view_zoom_uses_each_windows_display_percentage_and_keeps_physical_bounds() {
|
||||
for print_size in [true, false] {
|
||||
let mut app = app_with(1);
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.run("window.newWindow", json!({})).unwrap();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": print_size})).unwrap();
|
||||
for (number, ppi, percent) in [(1, 96.0, 100.0), (2, 144.0, 200.0)] {
|
||||
let v = view_mut(&mut app, (0, number)).unwrap();
|
||||
v.shown_ppi = Some(crate::ScreenPpi::Read(Some(ppi)));
|
||||
v.rotation = 0.0;
|
||||
v.zoom = crate::state::zoom_from_display_percent(percent, crate::state::print_ppi(print_size, Some(ppi)));
|
||||
}
|
||||
let shared = crate::script_runs::share_views(&mut app);
|
||||
for (number, (_, script_view)) in (1..).zip(&shared) {
|
||||
let camera = view(&app, (0, number)).unwrap();
|
||||
let width = f64::from(camera.rect.width()) * f64::from(app.ui_scaling()) / camera.zoom;
|
||||
assert!((script_view.visible().width() - width).abs() < 1e-9);
|
||||
}
|
||||
app.session.script_frames = false;
|
||||
let report = app
|
||||
.session
|
||||
.execute(
|
||||
"script.run",
|
||||
&json!({"source":
|
||||
"var vs = app.activeDocument.views; vc.log(vs[0].zoom); vc.log(vs[1].zoom); vs[0].zoom = 3; vs[1].zoom = 4;"
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(report["log"], json!([1, 2]), "print size: {print_size}");
|
||||
crate::script_runs::take_views(&mut app, &shared);
|
||||
for (number, percent) in [(1, 300.0), (2, 400.0)] {
|
||||
let shown = crate::state::zoom_display_percent(view(&app, (0, number)).unwrap().zoom, app.print_ppi_of(Some((0, number))));
|
||||
assert!((shown - percent).abs() < 1e-9, "window {number}, print size {print_size}: {shown}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_view_center_changes_preserve_zoom_at_camera_limits() {
|
||||
for zoom in [0.0313, 640.0] {
|
||||
let mut app = app_with(1);
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": true})).unwrap();
|
||||
let v = view_mut(&mut app, (0, 1)).unwrap();
|
||||
v.shown_ppi = Some(crate::ScreenPpi::Read(Some(96.0)));
|
||||
v.zoom = zoom;
|
||||
let shared = crate::script_runs::share_views(&mut app);
|
||||
app.session.script_frames = false;
|
||||
app.session.execute("script.run", &json!({"source": "app.activeDocument.views[0].centerPoint = [10, 20];"})).unwrap();
|
||||
crate::script_runs::take_views(&mut app, &shared);
|
||||
assert_eq!(view(&app, (0, 1)).unwrap().zoom, zoom);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_view_zoom_refuses_values_outside_each_windows_camera_range() {
|
||||
for ppi in [96.0, 144.0] {
|
||||
let mut app = app_with(1);
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": true})).unwrap();
|
||||
view_mut(&mut app, (0, 1)).unwrap().shown_ppi = Some(crate::ScreenPpi::Read(Some(ppi)));
|
||||
let shared = crate::script_runs::share_views(&mut app);
|
||||
app.session.script_frames = false;
|
||||
let requested = 640.0 / crate::state::zoom_baseline(Some(ppi)) * 1.1;
|
||||
let source = format!("try {{ app.activeDocument.views[0].zoom = {requested}; vc.log('accepted'); }} catch (e) {{ vc.log(e.name); }}");
|
||||
let report = app.session.execute("script.run", &json!({"source": source})).unwrap();
|
||||
assert_eq!(report["log"], json!(["RangeError"]));
|
||||
crate::script_runs::take_views(&mut app, &shared);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_script_lists_and_changes_each_view_of_one_document() {
|
||||
let mut app = app_with(1);
|
||||
app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": false})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.run("window.newWindow", json!({})).unwrap();
|
||||
@@ -243,6 +320,7 @@ fn a_script_lists_and_changes_each_view_of_one_document() {
|
||||
#[test]
|
||||
fn a_script_retains_view_identity_after_renumbering_and_reusing_a_window_number() {
|
||||
let mut app = app_with(1);
|
||||
app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": false})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
app.run("window.newWindow", json!({})).unwrap();
|
||||
|
||||
@@ -54,15 +54,18 @@ pub(crate) fn share_views(app: &mut VectorcraftApp) -> Vec<((u64, u64), WindowVi
|
||||
if !v.fitted || v.rect.width() <= 0.0 || v.rect.height() <= 0.0 {
|
||||
return None;
|
||||
}
|
||||
let size = [f64::from(v.rect.width()) * scaling, f64::from(v.rect.height()) * scaling];
|
||||
Some(((uid, v.runtime_id), WindowView { center: v.center, zoom: v.zoom, rotation: v.rotation, size, screen_mode: mode }))
|
||||
let print = app.print_ppi_of(Some(window));
|
||||
let baseline = crate::state::zoom_baseline(print);
|
||||
let size = [f64::from(v.rect.width()) * scaling / baseline, f64::from(v.rect.height()) * scaling / baseline];
|
||||
let zoom = crate::state::zoom_display_percent(v.zoom, print) / 100.0;
|
||||
Some(((uid, v.runtime_id), WindowView { center: v.center, zoom, zoom_baseline: baseline, rotation: v.rotation, size, screen_mode: mode }))
|
||||
})
|
||||
.collect();
|
||||
app.session.window_views.clone_from(&shared);
|
||||
shared
|
||||
}
|
||||
|
||||
/// Takes the zoom, centre and screen mode a script gave the views [`share_views`] gave.
|
||||
/// Applies the zoom, center and screen mode a script assigned to the views from [`share_views`].
|
||||
pub(crate) fn take_views(app: &mut VectorcraftApp, shared: &[((u64, u64), WindowView)]) {
|
||||
let (min, max) = WindowView::ZOOM_RANGE;
|
||||
for (key, before) in shared {
|
||||
@@ -71,7 +74,9 @@ pub(crate) fn take_views(app: &mut VectorcraftApp, shared: &[((u64, u64), Window
|
||||
continue;
|
||||
}
|
||||
if let Some(v) = crate::doc_windows::view_by_id_mut(app, *key) {
|
||||
v.zoom = now.zoom.clamp(min, max);
|
||||
if now.zoom != before.zoom {
|
||||
v.zoom = (now.zoom * now.zoom_baseline).clamp(min, max);
|
||||
}
|
||||
v.center = now.center;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,7 +522,7 @@ fn a_script_sees_the_window_and_sets_its_zoom_centre_and_screen_mode() {
|
||||
app.run("script.run", json!({"source": source, "name": "v.js"})).unwrap();
|
||||
finish(&mut app);
|
||||
let report = app.session.script_report().cloned().unwrap();
|
||||
assert_eq!(report["log"][0].as_f64(), Some(600.0), "the view is as wide as the canvas at 100%");
|
||||
assert_eq!(report["log"][0].as_f64(), Some(600.0), "the bounds use the physical camera scale");
|
||||
let now = *app.view().unwrap();
|
||||
assert!(now.zoom > before.zoom && now.center != before.center);
|
||||
assert_eq!(app.ui.screen_mode, 2);
|
||||
|
||||
+4
-2
@@ -2792,8 +2792,10 @@ other files through `$`:
|
||||
uses the size of 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, or a `centerPoint` outside the canvas after
|
||||
rotated canvas), `centerPoint`, `zoom` and `screenMode`, in the run's coordinate system. `zoom` is 1 at the window's
|
||||
displayed 100%. With Display Print Size at 100% Zoom off, the range is 0.0313 to 640 (VectorCraft's own range).
|
||||
With it enabled, the app divides these limits by the window's scale at 100% (the screen points per inch used for
|
||||
that window divided by 72). A zoom outside that range, or a `centerPoint` outside the canvas after
|
||||
coordinate conversion, throws a `RangeError`. A `centerPoint` that is not two numbers throws a `TypeError`. `screenMode` reads Normal as
|
||||
`ScreenMode.MULTIWINDOW`, Full Screen Mode with Menu Bar as `ScreenMode.DESKTOP` and Full Screen Mode and
|
||||
Presentation Mode as `ScreenMode.FULLSCREEN` (VectorCraft's own mapping); the screen mode is the app's, so setting it
|
||||
|
||||
@@ -1056,7 +1056,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- The control server writes each reply through a 64 KiB buffer. On Windows 11 it sends the 17 MB `document.inspect` reply of a document with 50,000 objects on one layer to a client over loopback in 0.09 s instead of 34 s.
|
||||
- Scripts: File › Scripts › Other Script… and `script.run` run a JavaScript file against the open documents as one transaction with one undo step for each changed document. A failed or stopped run puts every document back. `crates/engine/src/tests/script_run.rs`.
|
||||
- Scripts: the global `app` gives the open documents, new documents from a startup preset and a `DocumentPreset`, the active document, each document's selection and its page items with their names, notes, visibility, lock, opacity, position, size, bounds and stacking order, and moves and copies them into or beside other objects, as the help pages https://ai-scripting.docsforadobe.dev/jsobjref/Application/, https://ai-scripting.docsforadobe.dev/jsobjref/Documents/, https://ai-scripting.docsforadobe.dev/jsobjref/DocumentPreset/, https://ai-scripting.docsforadobe.dev/jsobjref/Document/, https://ai-scripting.docsforadobe.dev/jsobjref/PageItems/, https://ai-scripting.docsforadobe.dev/jsobjref/PageItem/ and https://ai-scripting.docsforadobe.dev/jsobjref/GroupItem/ describe them; each change runs a command inside the run's transaction (`docs/mcp.md`). A script can check a member with `hasOwnProperty`, and can read a collection by name and name `activeDocument`, `documents` and `selection` without `app.` (https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/). `app.executeMenuCommand` runs the menu items Select All, Deselect, Group, Ungroup, Make and Release Clipping Mask, Make and Release Compound Path, and Fit to Artwork Bounds by their menu command names (https://community.adobe.com/questions-652/a-list-of-illustrator-menu-commands-we-can-call-from-javascript-769510, https://gist.github.com/shalakolee/df3246659d4289eb99149b9ed00a00ee). The `parent` of a page item at the top of a layer is a `Layer` with `name`, `typename` and `toString`.
|
||||
`Document.views` lists the document's drawn windows with their bounds, center, zoom and screen mode (https://ai-scripting.docsforadobe.dev/jsobjref/View/). View objects keep their targets when windows are renumbered. View changes run no command, add no undo step, and remain when a script fails. A center outside the canvas after coordinate conversion throws a `RangeError`. `Document.layers`, `Document.activeLayer`, paths, type and the other classes are not there yet. `crates/engine/src/tests/script_dom.rs`.
|
||||
`Document.views` lists the document's drawn windows with their bounds, center, zoom and screen mode (https://ai-scripting.docsforadobe.dev/jsobjref/View/). Zoom follows each window's displayed percentage, including display density with Display Print Size at 100% Zoom enabled; bounds retain the physical camera scale. View objects keep their targets when windows are renumbered. View changes run no command, add no undo step, and remain when a script fails. A center outside the canvas after coordinate conversion throws a `RangeError`. `Document.layers`, `Document.activeLayer`, paths, type and the other classes are not there yet. `crates/engine/src/tests/script_dom.rs`.
|
||||
- Scripts: Window › Script Console shows each run's `$.write` and `$.writeln` output, `vc.log` entries, uncaught error with the script's name, line and column, and a report of its time, host calls and slowest commands, keeping at most 2,048 lines and counting the lines it dropped. `script.run`'s report carries the same data, and `script.console`, `script.console.clear` and `ui.copyScriptConsole` read, clear and copy it. `crates/engine/src/tests/script_console.rs`.
|
||||
- Scripts: the status bar translates its own words around a failed run's error and the app's own error messages, and shows text the script threw itself (a value, an error it made, or an error whose name or message it changed) as it is, even when that text reads like one of the app's messages. `script.run`'s report gives `messageOrigin` (`script` or `built-in`), and the advice about objects other applications give their scripts follows only the app's own reference errors. VectorCraft's own rule. `crates/js/src/interp_tests.rs`, `crates/engine/src/tests/script_run.rs`, `crates/ui-egui/src/chrome/tests.rs`.
|
||||
- Scripts: the `$` object has the other members the Dollar object page lists (https://extendscript.docsforadobe.dev/extendscript-tools-features/dollar-object/), from `$.about()` to `$.version`, and scripts have the global `localize(object, ...values)` (https://extendscript.docsforadobe.dev/extendscript-tools-features/localizing-extendscript-strings/), `toSource(value)` and `Object.prototype.toSource()`. Each run starts with the interface's locale, and what a script changes in `$` lasts only for that run. `crates/engine/src/tests/script_dollar.rs`, `crates/js/src/extension_tests.rs`.
|
||||
|
||||
Reference in new issue
Block a user