mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01: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
@@ -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