mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Scripts: ScriptUI dialogs keep to their limits and steps under reentrant handlers
- A show() that a handler's error ended with steps left in `answers` now ends the run and rolls it back, even when the script catches the error. - A list step's value written as a whole float (1.0) is an item index, in `answers` and in ui.dialog.confirm; a fraction is still refused. - A progress bar's fraction stays finite across the widest range. - add() checks the control and item counts again after the getters and conversions it runs, refuses an initial item array longer than the items left before reading it, and counts a new list's items only once the list is added. - show() keeps the values a getter gives while their properties are read, and reads the dialog again when a getter changes a list after it was read; a list that changes again throws an Error. - A click or close from ui.dialog.confirm gives the text still typed in the dialog's fields first. - Quitting while a handler runs keeps the dialog and its queued events, so Cancel shows it again with them. - Screen readers call an edit field without a name by the visible static text just before it in its container. - The dialog's heading follows a title a handler changed while the dialog is open. - A list box gives screen readers a list box node named by its name or the static text before it, with the selected item as its value, and a drop-down list is named the same way. - The dialog closes when show() returns, also when the script goes on running. - Enter clicks the default button while the keyboard focus is on a control other than an edit field, such as a slider.
This commit is contained in:
1 parent
c3ea4e2f9c
commit
3745b14db4
11 files changed
+564
-76
No files matched your search
@@ -21,7 +21,7 @@ use std::collections::VecDeque;
|
||||
use std::rc::Rc;
|
||||
|
||||
use serde_json::{Map, Value, json};
|
||||
use vectorcraft_js::{Arg, Data, HostError, HostFunction, Realm, RunError};
|
||||
use vectorcraft_js::{Arg, Data, HostError, HostFunction, LimitKind, Realm, RunError, RunErrorKind};
|
||||
|
||||
use super::scriptui::{self, ScriptUiDialog, Showing, UiEvent, UiStep};
|
||||
use super::{Host, console};
|
||||
@@ -258,6 +258,11 @@ impl Dialogs {
|
||||
self.window.borrow().as_ref().map(|s| s.dialog.clone())
|
||||
}
|
||||
|
||||
/// The ScriptUI dialog whose `show()` is running, as it last waited, also while a handler runs.
|
||||
pub(crate) fn showing_window(&self) -> Option<ScriptUiDialog> {
|
||||
self.window.borrow().as_ref().map(|s| s.dialog.clone())
|
||||
}
|
||||
|
||||
/// Gives the waiting ScriptUI dialog `event`, once it is checked against the dialog.
|
||||
pub(crate) fn window_answer(&self, realm: &mut Realm, event: UiEvent) -> Result<(), String> {
|
||||
if !self.window_waiting.get() {
|
||||
@@ -347,10 +352,19 @@ impl Dialogs {
|
||||
self.window_waiting.set(false);
|
||||
let Some(showing) = self.window.borrow_mut().take() else { return Ok(()) };
|
||||
let left = showing.steps.as_ref().map_or(0, std::collections::VecDeque::len);
|
||||
let steps = if left == 1 { "step" } else { "steps" };
|
||||
let unused = format!("show: the dialog's answer in `answers` has {left} {steps} the dialog did not use");
|
||||
if normal && left > 0 {
|
||||
self.push_record(showing.record(json!("missing")));
|
||||
let steps = if left == 1 { "step" } else { "steps" };
|
||||
return Err(HostError::Abort(format!("show: the dialog's answer in `answers` has {left} {steps} the dialog did not use")));
|
||||
return Err(HostError::Abort(unused));
|
||||
}
|
||||
// A show that a handler's error ended still ends the run when steps are left: the script
|
||||
// can catch that error, and the run must not finish as if it took every step.
|
||||
if left > 0 {
|
||||
let mut failed = self.failed.borrow_mut();
|
||||
if failed.is_none() {
|
||||
*failed = Some(RunError { kind: RunErrorKind::Limit(LimitKind::Host), message: unused, line: 0, column: 0, authored: false });
|
||||
}
|
||||
}
|
||||
let answer = match result {
|
||||
_ if !normal => json!("error"),
|
||||
|
||||
@@ -1833,6 +1833,15 @@ impl Session {
|
||||
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.waiting_window())).flatten()
|
||||
}
|
||||
|
||||
/// The ScriptUI dialog whose `show()` the running script is in, whether it waits at it or runs
|
||||
/// one of its handlers. A run asked to stop is in none.
|
||||
pub fn script_window_showing(&self) -> Option<ScriptUiDialog> {
|
||||
if self.script_stop.is_set() {
|
||||
return None;
|
||||
}
|
||||
self.script_key.as_ref().and_then(|k| with_job(k, |j| j.host.dialogs.showing_window())).flatten()
|
||||
}
|
||||
|
||||
/// Gives the ScriptUI dialog the running script waits at `event`; the next
|
||||
/// [`Session::script_pump`] runs the control's handler. → an error, and the dialog still
|
||||
/// waits, when nothing waits or the dialog cannot take the event ([`scriptui::check_event`]).
|
||||
@@ -2158,6 +2167,12 @@ impl Session {
|
||||
}
|
||||
job.host.calls_this_slice.set(0);
|
||||
let outcome = run_slice(self, &mut job);
|
||||
// A failure a dialog recorded in the slice (steps a show left unused) ends the run, even
|
||||
// when the script went on.
|
||||
let outcome = match (outcome, job.host.dialogs.take_failure()) {
|
||||
(Ok(Ok(_)), Some(e)) => Ok(Err(e)),
|
||||
(outcome, _) => outcome,
|
||||
};
|
||||
match outcome {
|
||||
Err(panic) => {
|
||||
let e = RunError { kind: RunErrorKind::Internal, message: panic, line: 0, column: 0, authored: false };
|
||||
|
||||
@@ -148,6 +148,23 @@ pub struct UiNode {
|
||||
bounds: Option<UiRect>,
|
||||
}
|
||||
|
||||
impl UiNode {
|
||||
/// How much of a progress bar is done, from 0 to 1.
|
||||
pub fn fraction(&self) -> f32 {
|
||||
if self.max <= self.min || self.value <= self.min {
|
||||
return 0.0;
|
||||
}
|
||||
if self.value >= self.max {
|
||||
return 1.0;
|
||||
}
|
||||
let span = self.max - self.min;
|
||||
// A range wider than the largest number is measured at half scale.
|
||||
let part =
|
||||
if span.is_finite() { (self.value - self.min) / span } else { (self.value / 2.0 - self.min / 2.0) / (self.max / 2.0 - self.min / 2.0) };
|
||||
part.clamp(0.0, 1.0) as f32
|
||||
}
|
||||
}
|
||||
|
||||
/// A dialog a run shows, checked and laid out.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ScriptUiDialog {
|
||||
@@ -302,8 +319,8 @@ pub(crate) fn step_event(dialog: &ScriptUiDialog, step: &UiStep) -> Result<UiEve
|
||||
let event = match (step.event.as_str(), &step.value) {
|
||||
("click", _) => UiEvent::Click(id),
|
||||
(_, Some(Value::String(t))) => UiEvent::Change(id, UiValue::Text(t.clone())),
|
||||
(_, Some(Value::Number(n))) if matches!(dialog.nodes[id].kind, UiKind::DropDownList | UiKind::ListBox) => match n.as_u64() {
|
||||
Some(k) => UiEvent::Change(id, UiValue::Index(Some(usize::try_from(k).unwrap_or(usize::MAX)))),
|
||||
(_, Some(Value::Number(n))) if matches!(dialog.nodes[id].kind, UiKind::DropDownList | UiKind::ListBox) => match list_index(n) {
|
||||
Some(k) => UiEvent::Change(id, UiValue::Index(Some(k))),
|
||||
None => return Err("a list's `value` must be an item index or null".into()),
|
||||
},
|
||||
(_, Some(Value::Number(n))) => UiEvent::Change(id, UiValue::Number(n.as_f64().unwrap_or(f64::NAN))),
|
||||
@@ -313,6 +330,18 @@ pub(crate) fn step_event(dialog: &ScriptUiDialog, step: &UiStep) -> Result<UiEve
|
||||
check_event(dialog, event)
|
||||
}
|
||||
|
||||
/// A list's item index written as a JSON number: a whole number of 0 or more, as an integer or as
|
||||
/// a float such as `1.0`; an index past the end stays past it. `None` for a fraction or a negative
|
||||
/// number.
|
||||
pub fn list_index(n: &serde_json::Number) -> Option<usize> {
|
||||
if let Some(k) = n.as_u64() {
|
||||
return Some(usize::try_from(k).unwrap_or(usize::MAX));
|
||||
}
|
||||
let f = n.as_f64()?;
|
||||
// `as` saturates a whole number too large for usize at usize::MAX.
|
||||
(f >= 0.0 && f.fract() == 0.0).then_some(f as usize)
|
||||
}
|
||||
|
||||
fn field<'a>(fields: &'a [(String, Data)], name: &str) -> &'a Data {
|
||||
fields.iter().find(|(k, _)| k == name).map_or(&Data::Undefined, |(_, v)| v)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user