mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Scripts: palette events and typed texts keep their own file access scope
A waiting palette event merges with a newer one only when both have the same file access scope, and a click or close merges only after its palette's last waiting change. A text typed in a field keeps the scope of each pass that edited it and goes to the script under what both that scope and the committing pass allow; a click or close that commits it takes those limits too. Passes recorded before the app takes them stay separate. Scope coverage checks root containment instead of comparing the same roots object, and the progress line describes when update() runs a waiting event.
This commit is contained in:
1 parent
91f413e9bb
commit
48b59a3ccc
8 files changed
+707
-49
No files matched your search
@@ -77,7 +77,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Script Palette Event",
|
||||
[],
|
||||
None,
|
||||
"{palette: an id script.palettes lists, event: click|change|close, control?: a control (its index, name or text; click and change), value?: the new value (an edit field's text, a slider's number, or a list's item index or null; change)} give a shown palette a click, a change or a close, as the person does: it runs as a run of its own in the palette's engine, with its report like script.run's, once no run is going, oldest first; while it waits, another click on the same control, change of it or close of the same palette takes its place with the newer value, and a list's change ends its run with an error when the list holds other items by then; dialogs throw in the handler in the app, and with nobody to ask the run ends at one; a hidden palette, a control that is disabled, hidden or cannot take the event, and a 65th waiting event are refused, and with nobody to ask so is an event while an undo group is open → in the app {waiting: true, merged: whether it took the place of a waiting event, waitingEvents}, and the report later in ui.inspect's scriptReport; with nobody to ask, the run's report (or its error) once it ends",
|
||||
"{palette: an id script.palettes lists, event: click|change|close, control?: a control (its index, name or text; click and change), value?: the new value (an edit field's text, a slider's number, or a list's item index or null; change)} give a shown palette a click, a change or a close, as the person does: it runs as a run of its own in the palette's engine, with its report like script.run's, once no run is going, oldest first; a newer click on the same control, change of it or close of the same palette replaces the most recent waiting event of that kind for that control or palette only when it has the same file access roots; otherwise the new event waits at the end of the line (merged: false); a click or close takes its place only if no change of its palette waits after it, otherwise it waits as a new event (merged: false); a list's change ends its run with an error when the list holds other items by then; dialogs throw in the handler in the app, and with nobody to ask the run ends at one; a hidden palette, a control that is disabled, hidden or cannot take the event, and a 65th waiting event are refused, and with nobody to ask so is an event while an undo group is open → in the app {waiting: true, merged: whether it took the place of a waiting event, waitingEvents}, and the report later in ui.inspect's scriptReport; with nobody to ask, the run's report (or its error) once it ends",
|
||||
always,
|
||||
palette_event
|
||||
),
|
||||
|
||||
@@ -148,6 +148,13 @@ impl AutomationRoots {
|
||||
Arc::new(Self { read: both(&self.read, &other.read), write: both(&self.write, &other.write) })
|
||||
}
|
||||
|
||||
/// Whether these roots grant everything `other` grants: each root of `other` lies inside one of
|
||||
/// these roots of the same kind.
|
||||
pub(crate) fn contains(&self, other: &Self) -> bool {
|
||||
let within = |mine: &[Root], theirs: &[Root]| theirs.iter().all(|o| mine.iter().any(|r| inside(&o.resolved, &r.resolved)));
|
||||
within(&self.read, &other.read) && within(&self.write, &other.write)
|
||||
}
|
||||
|
||||
/// The roots for people: `read A, B; write C` (`none` for a kind without any).
|
||||
pub fn describe(&self) -> String {
|
||||
let list = |roots: &[Root]| {
|
||||
@@ -262,23 +269,34 @@ impl ScopeHandle {
|
||||
Self(Some(scope), GATE.with(|g| g.borrow().clone()), GATE_READS_EXEMPT.with(std::cell::Cell::get))
|
||||
}
|
||||
|
||||
/// Whether this scope allows everything the calling thread's scope allows: its roots are none or
|
||||
/// the same roots, and its script gate is none or the same gate.
|
||||
/// Whether this scope allows everything the calling thread's scope allows ([`ScopeHandle::covers`]).
|
||||
pub(crate) fn covers_current(&self) -> bool {
|
||||
let here = Self::capture();
|
||||
let gate = match (&self.1, &here.1) {
|
||||
self.covers(&Self::capture())
|
||||
}
|
||||
|
||||
/// Whether this scope allows everything `other` allows: its roots are none, or each root of
|
||||
/// `other` lies inside one of its roots of the same kind, and its script gate is none or the
|
||||
/// same gate.
|
||||
fn covers(&self, other: &Self) -> bool {
|
||||
let gate = match (&self.1, &other.1) {
|
||||
(None, _) => true,
|
||||
(Some(a), Some(b)) => Arc::ptr_eq(a, b),
|
||||
(Some(_), None) => false,
|
||||
};
|
||||
let roots = match (self.roots(), here.roots()) {
|
||||
let roots = match (self.roots(), other.roots()) {
|
||||
(None, _) => true,
|
||||
(Some(a), Some(b)) => Arc::ptr_eq(&a, &b),
|
||||
(Some(a), Some(b)) => Arc::ptr_eq(&a, &b) || a.contains(&b),
|
||||
(Some(_), None) => false,
|
||||
};
|
||||
gate && roots
|
||||
}
|
||||
|
||||
/// Whether this scope and `other` allow the same: each covers the other, and reads are exempt
|
||||
/// from the gate in both or in neither.
|
||||
pub fn same(&self, other: &Self) -> bool {
|
||||
self.2 == other.2 && self.covers(other) && other.covers(self)
|
||||
}
|
||||
|
||||
/// Run `f` in the captured scope.
|
||||
pub fn run<T>(self, f: impl FnOnce() -> T) -> T {
|
||||
let Self(scope, gate, exempt) = self;
|
||||
|
||||
@@ -6,11 +6,12 @@
|
||||
//! without `#targetengine`, or with `main`, `new Window("palette")` throws. `show()` returns at once.
|
||||
//! Each click, change or close the person gives a palette waits here, and then runs as a run of its
|
||||
//! own in the palette's engine, which takes the mutation lease like any other run: one event at a
|
||||
//! time, oldest first, each starting only once no run is going. While an event waits, another click
|
||||
//! on the same control, another change of it or another close of the same palette takes its place
|
||||
//! in line, with the newer value, instead of waiting as well. A palette's `update()` in a run of the
|
||||
//! palette's engine takes the events that wait for that engine's palettes and runs their handlers in
|
||||
//! that run instead ([`Palettes::take_for`]).
|
||||
//! time, oldest first, each starting only once no run is going. A new click, change or close replaces
|
||||
//! the most recent waiting event of the same kind for the same control or palette only when that
|
||||
//! event has the same file access scope. Otherwise the new event waits at the end of the line. A click or
|
||||
//! close replaces an event only after its palette's last waiting change. A palette's `update()` in
|
||||
//! a run of the palette's engine takes the events that wait for that engine's palettes and runs their
|
||||
//! handlers in that run instead ([`Palettes::take_for`]).
|
||||
//!
|
||||
//! A palette closes when its script calls `close()` or the person closes it, unless `onClose`
|
||||
//! returns `false`; when its engine is dropped or ends with an internal error; and with the session.
|
||||
@@ -177,10 +178,18 @@ impl Palettes {
|
||||
};
|
||||
let element = control_of(&mut event).and_then(|c| p.dialog.nodes.get(*c)).map(|n| n.id);
|
||||
let scope = crate::file_access::ScopeHandle::capture();
|
||||
if let Some(slot) = self.waiting.iter_mut().find(|q| q.palette == id && same_place(q, &event, element)) {
|
||||
// Only the most recent event for the same place can merge, in the same file access scope.
|
||||
// A click or close merges only after its palette's last waiting change.
|
||||
let closing = matches!(event, UiEvent::Click(_) | UiEvent::Close);
|
||||
let after = if closing {
|
||||
self.waiting.iter().rposition(|q| q.palette == id && matches!(q.event, UiEvent::Change(..))).map_or(0, |at| at + 1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let at = self.waiting.iter_mut().skip(after).rev().find(|q| q.palette == id && same_place(q, &event, element));
|
||||
if let Some(slot) = at.filter(|q| q.scope.same(&scope)) {
|
||||
slot.event = event;
|
||||
slot.list = list;
|
||||
slot.scope = scope;
|
||||
return Ok(Waiting::Merged);
|
||||
}
|
||||
if self.waiting.len() >= MAX_WAITING {
|
||||
|
||||
@@ -283,6 +283,135 @@ fn update_takes_eligible_events_past_deferred_events_in_scope_order() {
|
||||
assert_eq!(s.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_does_not_take_the_place_of_a_waiting_change_given_in_another_scope() {
|
||||
let mut s = one_doc();
|
||||
run_in_app(
|
||||
&mut s,
|
||||
"#targetengine merge\nvar p = new Window('palette'); var t = p.add('edittext', undefined, ''); \
|
||||
var b = p.add('button', undefined, 'B'); t.onChange = function () { vc.log('change ' + t.text); }; \
|
||||
b.onClick = function () { vc.log('click'); }; p.show();",
|
||||
);
|
||||
let id = only_palette(&s);
|
||||
let roots = crate::file_access::AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
let change = |text: &str| UiEvent::Change(1, crate::script::UiValue::Text(text.into()));
|
||||
assert_eq!(crate::file_access::confine(Some(&roots), || s.script_palette_event(id, change("x"))), Ok(Waiting::Added));
|
||||
crate::file_access::unconfined(|| {
|
||||
assert_eq!(s.script_palette_event(id, UiEvent::Click(2)), Ok(Waiting::Added));
|
||||
assert_eq!(s.script_palette_event(id, change("y")), Ok(Waiting::Added), "the confined change keeps its own place");
|
||||
});
|
||||
let report = crate::file_access::unconfined(|| run_in_app(&mut s, "#targetengine merge\np.update();"));
|
||||
assert_eq!(report["log"], json!(["click", "change y"]), "the click stays before the later change of its scope");
|
||||
assert_eq!(s.script_palette_events_waiting(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_change_does_not_merge_past_a_waiting_change_given_in_another_scope() {
|
||||
let mut s = one_doc();
|
||||
run_in_app(
|
||||
&mut s,
|
||||
"#targetengine merge\nvar p = new Window('palette'); var t = p.add('edittext', undefined, ''); \
|
||||
t.onChange = function () { vc.log('change ' + t.text); }; p.show();",
|
||||
);
|
||||
let id = only_palette(&s);
|
||||
let roots = crate::file_access::AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
let change = |text: &str| UiEvent::Change(1, crate::script::UiValue::Text(text.into()));
|
||||
assert_eq!(crate::file_access::confine(Some(&roots), || s.script_palette_event(id, change("x"))), Ok(Waiting::Added));
|
||||
assert_eq!(crate::file_access::unconfined(|| s.script_palette_event(id, change("y"))), Ok(Waiting::Added));
|
||||
assert_eq!(crate::file_access::confine(Some(&roots), || s.script_palette_event(id, change("z"))), Ok(Waiting::Added));
|
||||
assert_eq!(s.script_palette_events_waiting(), 3);
|
||||
let mut logs = Vec::new();
|
||||
crate::file_access::unconfined(|| {
|
||||
while let Some(started) = s.script_palette_start() {
|
||||
started.unwrap();
|
||||
logs.push(pump(&mut s).unwrap().unwrap()["log"].clone());
|
||||
}
|
||||
});
|
||||
assert_eq!(logs, [json!(["change x"]), json!(["change y"]), json!(["change z"])]);
|
||||
assert_eq!(s.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_does_not_take_a_place_ahead_of_a_change_that_waits_after_it() {
|
||||
let mut s = one_doc();
|
||||
run_in_app(
|
||||
&mut s,
|
||||
"#targetengine ahead\nvar p = new Window('palette'); var t = p.add('edittext', undefined, ''); \
|
||||
var b = p.add('button', undefined, 'B'); t.onChange = function () { vc.log('change ' + t.text); }; \
|
||||
b.onClick = function () { vc.log('click ' + t.text); }; p.show();",
|
||||
);
|
||||
let id = only_palette(&s);
|
||||
assert_eq!(s.script_palette_event(id, UiEvent::Click(2)), Ok(Waiting::Added));
|
||||
assert_eq!(s.script_palette_event(id, UiEvent::Change(1, crate::script::UiValue::Text("y".into()))), Ok(Waiting::Added));
|
||||
assert_eq!(s.script_palette_event(id, UiEvent::Click(2)), Ok(Waiting::Added), "the click waits after the change");
|
||||
let report = run_in_app(&mut s, "#targetengine ahead\np.update();");
|
||||
assert_eq!(report["log"], json!(["click ", "change y", "click y"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_clicks_and_closes_merge_after_the_last_change_of_their_palette() {
|
||||
use crate::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
for closing in [UiEvent::Click(1), UiEvent::Close] {
|
||||
for confined_change in [false, true] {
|
||||
let mut s = one_doc();
|
||||
run_in_app(&mut s, &format!("{TOOLS} p.onClose = function () {{ vc.log('close'); return false; }};"));
|
||||
let id = only_palette(&s);
|
||||
unconfined(|| assert_eq!(s.script_palette_event(id, closing.clone()), Ok(Waiting::Added)));
|
||||
let change = UiEvent::Change(2, crate::script::UiValue::Text("typed".into()));
|
||||
let queued = if confined_change {
|
||||
confine(Some(&roots), || s.script_palette_event(id, change))
|
||||
} else {
|
||||
unconfined(|| s.script_palette_event(id, change))
|
||||
};
|
||||
assert_eq!(queued, Ok(Waiting::Added));
|
||||
unconfined(|| {
|
||||
assert_eq!(s.script_palette_event(id, closing.clone()), Ok(Waiting::Added));
|
||||
assert_eq!(s.script_palette_event(id, closing.clone()), Ok(Waiting::Merged));
|
||||
});
|
||||
assert_eq!(s.script_palette_events_waiting(), 3);
|
||||
let mut logs = Vec::new();
|
||||
unconfined(|| {
|
||||
while let Some(started) = s.script_palette_start() {
|
||||
started.unwrap();
|
||||
logs.push(pump(&mut s).unwrap().unwrap()["log"].clone());
|
||||
}
|
||||
});
|
||||
let expected = if matches!(closing, UiEvent::Close) {
|
||||
[json!(["close"]), json!(["change typed"]), json!(["close"])]
|
||||
} else {
|
||||
[json!(["click 1"]), json!(["change typed"]), json!(["click 2"])]
|
||||
};
|
||||
assert_eq!(logs, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_takes_events_whose_roots_grant_all_the_run_s_roots_grant() {
|
||||
let mut s = one_doc();
|
||||
run_in_app(
|
||||
&mut s,
|
||||
"#targetengine contain\nvar p = new Window('palette'); var b = p.add('button', undefined, 'B'); \
|
||||
b.onClick = function () { vc.log('clicked'); }; p.show();",
|
||||
);
|
||||
let id = only_palette(&s);
|
||||
let base = std::fs::canonicalize(std::env::temp_dir()).unwrap();
|
||||
let inner = base.join(format!("vc-script-palette-contain-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&inner).unwrap();
|
||||
let wide = crate::file_access::AutomationRoots::new(&[&base], &[]).unwrap().unwrap();
|
||||
let same = crate::file_access::AutomationRoots::new(&[&base], &[]).unwrap().unwrap();
|
||||
let narrow = crate::file_access::AutomationRoots::new(&[&inner], &[]).unwrap().unwrap();
|
||||
for (run_roots, what) in [(same, "the same roots made again"), (narrow, "roots inside the event's roots")] {
|
||||
assert_eq!(crate::file_access::confine(Some(&wide), || s.script_palette_event(id, UiEvent::Click(1))), Ok(Waiting::Added));
|
||||
let report = crate::file_access::confine(Some(&run_roots), || run_in_app(&mut s, "#targetengine contain\np.update(); vc.log('after');"));
|
||||
assert_eq!(report["log"], json!(["clicked", "after"]), "{what}: {report}");
|
||||
assert_eq!(s.script_palette_events_waiting(), 0, "{what}");
|
||||
}
|
||||
let _ = std::fs::remove_dir(inner);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_handler_keeps_the_file_access_scope_of_the_request_that_gave_its_event() {
|
||||
let base = std::env::temp_dir().join(format!("vc-script-palette-scope-{}", std::process::id()));
|
||||
|
||||
@@ -39,16 +39,18 @@ pub(crate) struct PaletteWindow {
|
||||
palette: ScriptPalette,
|
||||
/// The text typed in each edit field that the script has not had yet, by the field's id.
|
||||
drafts: BTreeMap<u64, String>,
|
||||
/// Each draft's typing scope, narrowed by every pass that edits the text.
|
||||
draft_scopes: BTreeMap<u64, ScopeHandle>,
|
||||
/// The events the person gave since the app last took them, each with its control by its id,
|
||||
/// and a list's change with the items the person saw, so a run that changes the palette before
|
||||
/// the app takes them leaves each with its own control.
|
||||
given: Vec<(ScopeHandle, Vec<Queued>)>,
|
||||
given: Vec<(ScopeHandle, ScopeHandle, Vec<Queued>)>,
|
||||
open: bool,
|
||||
}
|
||||
|
||||
impl PaletteWindow {
|
||||
fn new(palette: ScriptPalette) -> Self {
|
||||
PaletteWindow { palette, drafts: BTreeMap::new(), given: Vec::new(), open: true }
|
||||
PaletteWindow { palette, drafts: BTreeMap::new(), draft_scopes: BTreeMap::new(), given: Vec::new(), open: true }
|
||||
}
|
||||
|
||||
/// The palette as the session has it now. A typed text whose field the palette no longer has is
|
||||
@@ -68,21 +70,95 @@ impl PaletteWindow {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn type_text(&mut self, i: usize, text: &str) {
|
||||
if let Some(n) = self.palette.dialog.nodes.get(i) {
|
||||
self.drafts.insert(n.id, text.to_string());
|
||||
self.take_pass(vec![(n.id, Some(text.to_string()))], Vec::new());
|
||||
}
|
||||
}
|
||||
|
||||
/// Gives the text typed in edit field `i` as `text`, as the field does at Enter or when it loses
|
||||
/// the focus.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn commit_text(&mut self, i: usize, text: &str) {
|
||||
if let Some(n) = self.palette.dialog.nodes.get(i) {
|
||||
self.take_pass(vec![(n.id, None)], vec![UiEvent::Change(i, UiValue::Text(text.to_string()))]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes one pass's typed texts (by field index) and events, as the palette's body reports them.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn pass(&mut self, drafts: &[(usize, Option<&str>)], events: Vec<UiEvent>) {
|
||||
let drafts = drafts.iter().filter_map(|(i, t)| self.palette.dialog.nodes.get(*i).map(|n| (n.id, t.map(str::to_string)))).collect();
|
||||
self.take_pass(drafts, events);
|
||||
}
|
||||
|
||||
/// Gives `event`, as the person does in the palette.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn give(&mut self, event: UiEvent) {
|
||||
self.record(std::slice::from_ref(&event));
|
||||
self.record(std::slice::from_ref(&event), &BTreeMap::new());
|
||||
}
|
||||
|
||||
fn record(&mut self, events: &[UiEvent]) {
|
||||
let events: Vec<Queued> = events.iter().filter_map(|e| script_window::queued(&self.palette.dialog, e)).collect();
|
||||
if !events.is_empty() {
|
||||
self.given.push((ScopeHandle::capture_now(), events));
|
||||
/// Takes what the person did in one pass: the typed texts (`None` for a field whose text was
|
||||
/// given or dropped) and the events. A typed text keeps the scope of each pass that edited it,
|
||||
/// narrowed to what all of them allow, and a text the field gives goes in line in that scope.
|
||||
fn take_pass(&mut self, drafts: Vec<(u64, Option<String>)>, events: Vec<UiEvent>) {
|
||||
let mut committed = BTreeMap::new();
|
||||
for (id, draft) in drafts {
|
||||
match draft {
|
||||
Some(text) => {
|
||||
// A pass that leaves the text as it was leaves its scope as it was too.
|
||||
let edited = self.drafts.get(&id) != Some(&text);
|
||||
let scope = match self.draft_scopes.remove(&id) {
|
||||
Some(typed) if edited => typed.within_current(),
|
||||
Some(typed) => typed,
|
||||
None => ScopeHandle::capture_now(),
|
||||
};
|
||||
self.draft_scopes.insert(id, scope);
|
||||
self.drafts.insert(id, text);
|
||||
}
|
||||
None => {
|
||||
if let Some(typed) = self.draft_scopes.remove(&id) {
|
||||
committed.insert(id, typed);
|
||||
}
|
||||
self.drafts.remove(&id);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.record(&events, &committed);
|
||||
}
|
||||
|
||||
/// Puts `events` in line in the scope of the calling thread, except that a field's change takes
|
||||
/// the scope its text was typed in (`committed`), narrowed to this one. Every click or close
|
||||
/// in the pass also takes what all of those text scopes allow.
|
||||
fn record(&mut self, events: &[UiEvent], committed: &BTreeMap<u64, ScopeHandle>) {
|
||||
let here = ScopeHandle::capture_now();
|
||||
let given: Vec<ScopeHandle> = events
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
UiEvent::Change(i, UiValue::Text(_)) => self.palette.dialog.nodes.get(*i).and_then(|n| committed.get(&n.id)),
|
||||
_ => None,
|
||||
})
|
||||
.map(|t| t.clone().within_current())
|
||||
.collect();
|
||||
let mut events: Vec<_> = events.iter().collect();
|
||||
events.sort_by_key(|e| matches!(e, UiEvent::Click(_) | UiEvent::Close));
|
||||
let mut batches: Vec<(ScopeHandle, Vec<Queued>)> = Vec::new();
|
||||
for e in events {
|
||||
let Some(q) = script_window::queued(&self.palette.dialog, e) else { continue };
|
||||
let typed = match e {
|
||||
UiEvent::Change(i, UiValue::Text(_)) => self.palette.dialog.nodes.get(*i).and_then(|n| committed.get(&n.id)),
|
||||
_ => None,
|
||||
};
|
||||
let mut scope = typed.map_or_else(|| here.clone(), |t| t.clone().within_current());
|
||||
if matches!(e, UiEvent::Click(_) | UiEvent::Close) {
|
||||
for t in &given {
|
||||
scope = t.clone().run(|| scope.within_current());
|
||||
}
|
||||
}
|
||||
match batches.last_mut() {
|
||||
Some((last, list)) if last.same(&scope) => list.push(q),
|
||||
_ => batches.push((scope, vec![q])),
|
||||
}
|
||||
}
|
||||
self.given.extend(batches.into_iter().map(|(scope, events)| (here.clone(), scope, events)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,12 +193,6 @@ impl StackDialog for PaletteWindow {
|
||||
.opened(Bind::bound(open))
|
||||
.show(ui, |ui, _| script_window::controls(ui, &palette.dialog, drafts_now, true));
|
||||
let pass = shown.inner.unwrap_or_default();
|
||||
for (id, draft) in pass.drafts {
|
||||
match draft {
|
||||
Some(text) => drafts.insert(id, text),
|
||||
None => drafts.remove(&id),
|
||||
};
|
||||
}
|
||||
let mut events = pass.events;
|
||||
// The close box gives the script the close; the palette stays open until the script closes
|
||||
// it.
|
||||
@@ -130,7 +200,7 @@ impl StackDialog for PaletteWindow {
|
||||
self.open = true;
|
||||
events.push(UiEvent::Close);
|
||||
}
|
||||
self.record(&events);
|
||||
self.take_pass(pass.drafts.into_iter().collect(), events);
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -157,7 +227,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
match host.get_mut_by_id::<PaletteWindow>(palette_id(p.id)) {
|
||||
Some(w) => {
|
||||
w.sync(p.clone());
|
||||
taken.push((p.id, std::mem::take(&mut w.drafts), std::mem::take(&mut w.given)));
|
||||
taken.push((p.id, std::mem::take(&mut w.drafts), std::mem::take(&mut w.draft_scopes), std::mem::take(&mut w.given)));
|
||||
}
|
||||
None if !modal => {
|
||||
host.open(PaletteWindow::new(p.clone()));
|
||||
@@ -168,39 +238,80 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
taken
|
||||
})
|
||||
.unwrap_or_default();
|
||||
for (id, mut drafts, given) in taken {
|
||||
for (id, mut drafts, mut draft_scopes, given) in taken {
|
||||
let Some(p) = shown.iter().find(|p| p.id == id) else { continue };
|
||||
// Each event goes to its control as the palette is now; one whose control is gone, or whose
|
||||
// list's items changed since the person chose, is dropped.
|
||||
let mut change_refused = false;
|
||||
for (scope, given) in given {
|
||||
scope.run(|| {
|
||||
let mut events: Vec<UiEvent> = given.into_iter().filter_map(|q| script_window::resolved(&p.dialog, q)).collect();
|
||||
drafted(app, &p.dialog, &mut drafts, &mut events);
|
||||
give(app, ctx, p, &mut drafts, events, &mut change_refused);
|
||||
});
|
||||
for (pass_scope, scope, given) in given {
|
||||
let mut events: Vec<UiEvent> = given.into_iter().filter_map(|q| script_window::resolved(&p.dialog, q)).collect();
|
||||
for (event_scope, event) in drafted(app, &p.dialog, &mut drafts, &mut events, &scope, &pass_scope, &draft_scopes) {
|
||||
event_scope.run(|| give(app, ctx, p, &mut drafts, vec![event], &mut change_refused));
|
||||
}
|
||||
// A text that went back in its field without a scope of its own takes this one.
|
||||
for field in drafts.keys() {
|
||||
draft_scopes.entry(*field).or_insert_with(|| scope.clone());
|
||||
}
|
||||
}
|
||||
draft_scopes.retain(|field, _| drafts.contains_key(field));
|
||||
with_host(app, |host, _| {
|
||||
if let Some(w) = host.get_mut_by_id::<PaletteWindow>(palette_id(id)) {
|
||||
w.drafts = drafts;
|
||||
w.draft_scopes = draft_scopes;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Puts the text still typed in the fields of palette `w` (`drafts`) before a click or a close in
|
||||
/// `events`. A text the script would refuse stays in its field with its error in the status bar,
|
||||
/// and a click or a close waits for the person to fix it.
|
||||
fn drafted(app: &mut VectorcraftApp, w: &ScriptUiDialog, drafts: &mut BTreeMap<u64, String>, events: &mut Vec<UiEvent>) {
|
||||
/// Commits drafts in dialog order, each under its typing and committing scopes. Every draft is
|
||||
/// validated before any is removed. A refused draft keeps the click or close out of line.
|
||||
fn drafted(
|
||||
app: &mut VectorcraftApp,
|
||||
w: &ScriptUiDialog,
|
||||
drafts: &mut BTreeMap<u64, String>,
|
||||
events: &mut Vec<UiEvent>,
|
||||
scope: &ScopeHandle,
|
||||
pass_scope: &ScopeHandle,
|
||||
draft_scopes: &BTreeMap<u64, ScopeHandle>,
|
||||
) -> Vec<(ScopeHandle, UiEvent)> {
|
||||
if let Some(e) = script_window::keep_refused(w, drafts, events) {
|
||||
app.status(e);
|
||||
}
|
||||
if events.iter().any(|e| matches!(e, UiEvent::Click(_) | UiEvent::Close))
|
||||
&& let Err(e) = script_window::commit_texts(w, drafts, events)
|
||||
{
|
||||
events.retain(|e| !matches!(e, UiEvent::Click(_) | UiEvent::Close));
|
||||
app.status(e);
|
||||
let closing = |e: &UiEvent| matches!(e, UiEvent::Click(_) | UiEvent::Close);
|
||||
let mut changes = Vec::new();
|
||||
if events.iter().any(closing) {
|
||||
changes.extend(events.iter().filter(|e| !closing(e)).cloned());
|
||||
let existing = changes.len();
|
||||
match script_window::commit_texts(w, drafts, &mut changes) {
|
||||
Ok(()) => {
|
||||
drop(changes.drain(..existing));
|
||||
}
|
||||
Err(e) => {
|
||||
changes.clear();
|
||||
events.retain(|e| !closing(e));
|
||||
app.status(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut closing_scope = scope.clone();
|
||||
let changes: Vec<_> = changes
|
||||
.into_iter()
|
||||
.map(|event| {
|
||||
let typed = match &event {
|
||||
UiEvent::Change(i, _) => w.nodes.get(*i).and_then(|n| draft_scopes.get(&n.id)),
|
||||
_ => None,
|
||||
};
|
||||
let narrowed = pass_scope.clone().run(|| typed.map_or_else(|| pass_scope.clone(), |t| t.clone().within_current()));
|
||||
closing_scope = narrowed.clone().run(|| closing_scope.clone().within_current());
|
||||
(narrowed, event)
|
||||
})
|
||||
.collect();
|
||||
events.sort_by_key(closing);
|
||||
let at = events.iter().position(closing).unwrap_or(events.len());
|
||||
let mut given: Vec<_> = events.drain(..at).map(|e| (scope.clone(), e)).collect();
|
||||
given.extend(changes);
|
||||
given.extend(events.drain(..).map(|e| (closing_scope.clone(), e)));
|
||||
given
|
||||
}
|
||||
|
||||
/// Puts the events the person gave the palette `p` in line, or shows why one was refused. A
|
||||
|
||||
@@ -194,6 +194,392 @@ fn a_recorded_text_change_is_not_given_again_in_a_later_clicks_scope() {
|
||||
assert_eq!(app.session.script_palettes()[0].dialog.nodes[1].text, "typed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_text_goes_to_the_script_in_the_scope_it_was_typed_in() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
var go = p.add('button', undefined, 'Go'); t.onChange = function () { vc.log('changed'); }; \
|
||||
go.onClick = function () { vc.log('clicked'); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
unconfined(|| {
|
||||
give(&app, id, UiEvent::Click(2));
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "the text and the click that gave it wait in the confined scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
for expected in ["changed", "clicked"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palettes()[0].dialog.nodes[1].text, "typed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_or_close_narrows_the_scope_of_the_draft_it_commits() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
for closing in [UiEvent::Click(2), UiEvent::Close] {
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
var go = p.add('button', undefined, 'Go'); t.onChange = function () { vc.log('changed'); }; \
|
||||
go.onClick = function () { vc.log('clicked ' + t.text); }; \
|
||||
p.onClose = function () { vc.log('closed ' + t.text); return false; }; p.show();",
|
||||
);
|
||||
unconfined(|| with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
confine(Some(&roots), || give(&app, id, closing.clone()));
|
||||
unconfined(|| {
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "the committed draft waits in the confined scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
let last = if matches!(closing, UiEvent::Close) { "closed typed" } else { "clicked typed" };
|
||||
for expected in ["changed", last] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_text_typed_in_two_scopes_goes_to_the_script_in_what_both_allow() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
t.onChange = function () { vc.log('changed'); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
unconfined(|| with_palette(&app, id, |w| w.type_text(1, "ty")).unwrap());
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.commit_text(1, "typed")).unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "the text waits in the confined scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_in_the_pass_that_gives_a_text_runs_after_the_text() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
var go = p.add('button', undefined, 'Go'); t.onChange = function () { vc.log('changed'); }; \
|
||||
go.onClick = function () { vc.log('clicked ' + t.text); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.pass(&[(1, None)], vec![UiEvent::Change(1, UiValue::Text("typed".into())), UiEvent::Click(2)])).unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "the click waits with the text it follows");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_reported_before_a_text_commit_waits_in_the_texts_scope() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var go = p.add('button', undefined, 'Go'); \
|
||||
var t = p.add('edittext', undefined, 'a'); t.onChange = function () { vc.log('changed'); }; \
|
||||
go.onClick = function () { vc.log('clicked ' + t.text); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(2, "typed")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.pass(&[(2, None)], vec![UiEvent::Click(1), UiEvent::Change(2, UiValue::Text("typed".into()))])).unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "both events wait in the text's confined scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
for expected in ["changed", "clicked typed"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pass_orders_changes_before_a_click_before_splitting_scopes() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var a = p.add('edittext', undefined, 'a'); \
|
||||
var b = p.add('edittext', undefined, 'b'); var go = p.add('button', undefined, 'Go'); \
|
||||
a.onChange = function () { vc.log('first ' + a.text); }; b.onChange = function () { vc.log('second ' + b.text); }; \
|
||||
go.onClick = function () { vc.log(a.text + ' ' + b.text); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
unconfined(|| with_palette(&app, id, |w| w.type_text(1, "first")).unwrap());
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(2, "second")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| {
|
||||
w.pass(
|
||||
&[(1, None), (2, None)],
|
||||
vec![UiEvent::Click(3), UiEvent::Change(1, UiValue::Text("first".into())), UiEvent::Change(2, UiValue::Text("second".into()))],
|
||||
);
|
||||
})
|
||||
.unwrap();
|
||||
follow(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 3);
|
||||
for expected in ["first first", "second second", "first second"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_focus_loss_and_click_or_close_keep_another_drafts_scope_independent() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
for closing in [UiEvent::Click(3), UiEvent::Close] {
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var a = p.add('edittext', undefined, 'a'); \
|
||||
var b = p.add('edittext', undefined, 'b'); var go = p.add('button', undefined, 'Go'); \
|
||||
a.onChange = function () { vc.log('first ' + a.text); }; b.onChange = function () { vc.log('second ' + b.text); }; \
|
||||
go.onClick = function () { vc.log(a.text + ' ' + b.text); }; \
|
||||
p.onClose = function () { vc.log(a.text + ' ' + b.text); return false; }; p.show();",
|
||||
);
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(1, "first")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| {
|
||||
w.type_text(2, "second");
|
||||
w.pass(&[(1, None)], vec![UiEvent::Change(1, UiValue::Text("first".into())), closing]);
|
||||
})
|
||||
.unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(a.text + ' ' + b.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["second second", "a second"]));
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
for expected in ["first first", "first second"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffered_passes_keep_a_click_or_close_before_a_later_text_commit() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
for confined in [false, true] {
|
||||
for closing in [UiEvent::Click(2), UiEvent::Close] {
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
var go = p.add('button', undefined, 'Go'); t.onChange = function () { vc.log('changed ' + t.text); }; \
|
||||
go.onClick = function () { vc.log('clicked ' + t.text); }; \
|
||||
p.onClose = function () { vc.log('closed ' + t.text); return false; }; p.show();",
|
||||
);
|
||||
let first = if matches!(closing, UiEvent::Close) { "closed a" } else { "clicked a" };
|
||||
let record = || {
|
||||
with_palette(&app, id, |w| {
|
||||
w.pass(&[], vec![closing]);
|
||||
w.type_text(1, "typed");
|
||||
w.commit_text(1, "typed");
|
||||
})
|
||||
.unwrap();
|
||||
};
|
||||
if confined {
|
||||
confine(Some(&roots), record);
|
||||
} else {
|
||||
unconfined(record);
|
||||
}
|
||||
unconfined(|| follow(&mut app));
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2);
|
||||
for expected in [first, "changed typed"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_or_close_commits_drafts_in_dialog_order_across_scopes() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
for closing in [UiEvent::Click(3), UiEvent::Close] {
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var a = p.add('edittext', undefined, 'a'); \
|
||||
var b = p.add('edittext', undefined, 'b'); var go = p.add('button', undefined, 'Go'); \
|
||||
a.onChange = function () { vc.log('first ' + a.text); }; b.onChange = function () { vc.log('second ' + a.text); }; \
|
||||
go.onClick = function () { vc.log(a.text + ' ' + b.text); }; \
|
||||
p.onClose = function () { vc.log(a.text + ' ' + b.text); return false; }; p.show();",
|
||||
);
|
||||
let long = "x".repeat(16 * 1024 + 1);
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(2, &long)).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.type_text(1, "first")).unwrap();
|
||||
give(&app, id, closing.clone());
|
||||
follow(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0, "every draft is validated before any is queued");
|
||||
assert_eq!(with_palette(&app, id, |w| w.typed(1).map(str::to_string)).unwrap(), Some("first".into()));
|
||||
assert_eq!(with_palette(&app, id, |w| w.typed(2).map(str::to_string)).unwrap(), Some(long));
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(2, "second")).unwrap());
|
||||
unconfined(|| {
|
||||
give(&app, id, closing);
|
||||
follow(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 3);
|
||||
for expected in ["first first", "second first", "first second"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_that_flushes_two_scopes_keeps_each_texts_scope() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var a = p.add('edittext', undefined, 'a'); \
|
||||
var b = p.add('edittext', undefined, 'b'); var go = p.add('button', undefined, 'Go'); \
|
||||
a.onChange = function () { vc.log('wide ' + a.text); }; b.onChange = function () { vc.log('narrow ' + b.text); }; \
|
||||
go.onClick = function () { vc.log(a.text + ' ' + b.text); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
unconfined(|| with_palette(&app, id, |w| w.type_text(1, "first")).unwrap());
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(2, "second")).unwrap());
|
||||
unconfined(|| {
|
||||
give(&app, id, UiEvent::Click(3));
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(a.text + ' ' + b.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["wide first", "first b"]), "the wide change runs in the wide scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 2, "the narrow change and the click still wait");
|
||||
for expected in ["narrow second", "first second"] {
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!([expected]));
|
||||
}
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pass_that_leaves_a_typed_text_as_it_was_keeps_its_scope() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
t.onChange = function () { vc.log('changed'); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
unconfined(|| with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.pass(&[(1, Some("typed"))], Vec::new())).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.commit_text(1, "typed")).unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["changed", "typed"]));
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_text_given_at_enter_goes_to_the_script_in_the_scope_it_was_typed_in() {
|
||||
use vectorcraft_engine::file_access::{AutomationRoots, confine, unconfined};
|
||||
|
||||
let mut app = app();
|
||||
let id = shown(
|
||||
&mut app,
|
||||
"#targetengine drafts\nvar p = new Window('palette'); var t = p.add('edittext', undefined, 'a'); \
|
||||
t.onChange = function () { vc.log('changed'); }; p.show();",
|
||||
);
|
||||
let roots = AutomationRoots::new(&[&std::env::temp_dir()], &[]).unwrap().unwrap();
|
||||
confine(Some(&roots), || with_palette(&app, id, |w| w.type_text(1, "typed")).unwrap());
|
||||
unconfined(|| {
|
||||
with_palette(&app, id, |w| w.commit_text(1, "typed")).unwrap();
|
||||
follow(&mut app);
|
||||
start(&mut app, "#targetengine drafts\np.update(); vc.log(t.text);");
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["a"]), "the text waits in its confined scope");
|
||||
assert_eq!(app.session.script_palette_events_waiting(), 1);
|
||||
unconfined(|| {
|
||||
script_runs::poll(&mut app);
|
||||
run_to_end(&mut app);
|
||||
});
|
||||
assert_eq!(app.session.script_report().unwrap()["log"], json!(["changed"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_waiting_behind_a_run_at_its_dialog_asks_for_no_frames() {
|
||||
let mut app = app();
|
||||
|
||||
+7
-2
@@ -3878,8 +3878,13 @@ the same controls, handlers and limits:
|
||||
- Each click, change or close the person gives a palette runs its handler as a run of its own in the palette's
|
||||
engine, with its own report and, when it changes a document, its own Run Script undo step. While another run
|
||||
goes on, the event waits and runs after it; events run one at a time, oldest first. While an event waits, a
|
||||
newer click on the same control, change of the same control or close of the same palette takes its place,
|
||||
with the newer value. A waiting click or change, and a text typed in a field, stay with their control when a
|
||||
newer click on the same control, change of the same control or close of the same palette replaces the most
|
||||
recent waiting event of that kind for that control or palette only when it has the same file access scope.
|
||||
Otherwise the new event waits at the end of the line (`merged: false`). A click or close takes its place only if no
|
||||
change of its palette waits after it; otherwise it waits as a new event (`merged: false`). A text typed in
|
||||
a field goes to the script under what both its typing scope and the scope that commits it allow. A click
|
||||
or close waits under what its scope and all the texts it commits allow. A text edited in several scopes
|
||||
goes under what all of them allow. A waiting click or change, and a text typed in a field, stay with their control when a
|
||||
run adds controls before it. A waiting event whose control is gone, or that its control can no longer take
|
||||
because a handler disabled or hid it, is dropped. A list's change ends its run
|
||||
with an error when a script added or removed the list's items after the person chose, even if the list again
|
||||
|
||||
@@ -1180,7 +1180,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- Plug-in, recovery and script report replies under `--automation-read-root`: `plugin.list`, `plugin.info` and `plugin.safeMode` name the plug-in folder, failed files, plug-in sources and disabled plug-ins' files outside the read roots by file name or `null`, `file.recovery.*` leave out a document's original file and the recovery folder outside them, and `ui.inspect`'s `scriptReport` names files a run wrote outside them by file name. `crates/engine/src/tests/plugins.rs` (`plugin_list_leaves_out_paths_outside_the_read_root`), `crates/engine/src/tests/recovery.rs` (`recovery_replies_leave_out_paths_outside_the_read_root`), `crates/ui-egui/src/tests/automation.rs` (`ui_inspect_script_report_leaves_out_files_outside_the_read_root`).
|
||||
- Scripts: `alert`, `confirm` and `prompt` open dialogs during a run, each with a Stop button that ends the run and puts every document back. In the app the run waits at the call for the person's answer; a run over MCP or the command line takes its answers in order from `script.run`'s `answers`, and the report lists each dialog with its answer. `crates/engine/src/tests/script_dialogs.rs`, `crates/ui-egui/src/tests/script_dialogs.rs`.
|
||||
- Scripts: ScriptUI's `new Window("dialog")` builds a modal dialog of groups, panels, static text, edit fields, buttons, check boxes, radio buttons, drop-down lists, list boxes, sliders and progress bars (https://extendscript.docsforadobe.dev/user-interface-tools/window-object/). `show()` waits for the person and runs the controls' `onClick`, `onChanging` and `onChange` handlers. The window's own callbacks are `onShow`, which runs when `show()` starts, and `onClose`, which runs before the dialog closes and keeps it open when it returns false. `show()` returns the result of `close(result)`, or 1 for the default button and 2 for the cancel button when the button has no `onClick` or click listener and no handler closed the dialog with another result. Controls also take `notify`, size objects a script can set one side of, lists' `find` and `remove`, and `addEventListener` for clicks and changes, which the dialog also takes for `show`; `Window.alert`, `Window.confirm` and `Window.prompt` open the script dialogs; a run over MCP or the command line takes the clicks and changes from a `window` answer. `new Window` and `add` also build a window or a control and the controls inside it from a resource specification (https://extendscript.docsforadobe.dev/user-interface-tools/resource-specifications/). Other control types throw that they are not available yet. `crates/js/src/scriptui_tests.rs`, `crates/engine/src/tests/script_scriptui.rs`, `crates/ui-egui/src/tests/script_window.rs`.
|
||||
- Scripts: ScriptUI's `new Window("palette")` in a script with `#targetengine` shows a palette that stays open beside the documents after the run (https://extendscript.docsforadobe.dev/user-interface-tools/window-object/). Each click, change or close runs its handler as a run of its own in that engine, after any run that is going; a newer event for the same control takes a waiting one's place. `update()` on a palette, or on a group or panel in one, runs the handlers of the events that wait for the engine's palettes, so a Cancel button can stop a loop, and in the script's own code in the app pauses the run so the app draws the palette, at most once every 16 ms; on a showing dialog it runs without an error. A palette closes with `close()`, its close box, or the end of its engine or the session; at most 16 palettes and 64 waiting events. `script.palettes` and `script.palette.event` list and drive them. `crates/js/src/scriptui_tests.rs`, `crates/engine/src/tests/script_palettes.rs`.
|
||||
- Scripts: ScriptUI's `new Window("palette")` in a script with `#targetengine` shows a palette that stays open beside the documents after the run (https://extendscript.docsforadobe.dev/user-interface-tools/window-object/). Each click, change or close runs its handler as a run of its own in that engine, after any run that is going; a newer event for the same control, given under the same file access scope, takes a waiting one's place. `update()` on a palette, or on a group or panel in one, runs the handlers of the events that wait for the engine's palettes whose file access scopes cover the run's scope, so a Cancel button can stop a loop; an event given under narrower roots or another script gate waits for a run of its own instead. `update()` in the script's own code in the app also pauses the run so the app draws the palette, at most once every 16 ms; on a showing dialog it runs without an error. A palette closes with `close()`, its close box, or the end of its engine or the session; at most 16 palettes and 64 waiting events. `script.palettes` and `script.palette.event` list and drive them. `crates/js/src/scriptui_tests.rs`, `crates/engine/src/tests/script_palettes.rs`.
|
||||
- Parameter keys: the engine refuses a key that a command's params description doesn't name before the command runs, in every build and for every caller, including the app's own calls, the commands a command runs and the steps of `command.batch` and `action.batch`. An action recorded by an older version still plays from the Actions panel and the Batch dialog (`Session::replay`). `crates/engine/tests/it/param_keys.rs`.
|
||||
- Parameter key lookups: the engine reads the params description of each built-in command, with the descriptions its `…` entries name, once per process, and answers the key check and the `ids` and `id` questions of each later command from that reading. A plug-in command's description, and a description whose `…` entries name a plug-in command, are still read each time. The accepted keys, the answers and the error messages are the same. In a release build on a one-object document, `object.move` with `ids` and `dx` takes 48 µs of thread CPU time per command instead of 61 µs, and `select.set` with `ids` 2.3 µs instead of 3.2 µs (medians of 7 alternating runs of 20,000 commands in one process, measured while other builds were running; `tests::params_cache::bench_params_keys_once`). The time is saved per command, in scripts, batches and MCP or plug-in call loops, and does not grow with the size of the document. `crates/engine/src/tests/params_cache.rs` compares every built-in command's answers with those of a fresh parse.
|
||||
- Effect parameter types: `effect.apply`, `appearance.addEffect` and `effect.setParams` refuse an effect parameter of another type than the effect reads, for keys only the params text names too (the earlier 3D `perspective`, `lightAzimuth` and `lightElevation`, Scribble's `variation`, Outline Stroke's `width`, Gaussian Blur's `linear`), a Crop Marks `style` other than `roman` or `japanese`, and an installed plug-in effect's parameter of another type than its manifest gives it, in a `command.batch` step too, with the document and undo history unchanged; a replayed action still plays. `effect.setParams` checks the parameters against a Warp's new style, and refuses a `visible` that is not true or false, in a replayed action too. `crates/engine/src/tests/params.rs` (`an_effect_parameter_without_a_default_is_held_to_its_type`, `effect_commands_refuse_an_undefaulted_parameter_of_the_wrong_type`), `crates/engine/src/tests/plugins.rs` (`live_effect_plugins_reshape_editable_art_and_expand`), `crates/engine/src/cmd/effectcmd/tests.rs` (`set_params_refuses_a_visible_that_is_not_true_or_false`, `set_params_checks_the_effect_it_leaves`).
|
||||
|
||||
Reference in new issue
Block a user