mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Follow-up to the review of #9147 (script palettes): - A waiting click or change that can no longer run (its control was disabled, hidden or removed by an earlier handler) is dropped before the open edit is settled, so it no longer commits or cancels a drag in the active document with no handler run. - A palette title or other getter that hides or closes the palette while the palette is being described no longer sends that description to the host: the host keeps the controls the palette had when it was hidden. - A typed text that matches the field's stored text is still given to the script, before a click, a close or on focus loss, when another change of that field waits, so the handler reads the typed text and not the waiting change. - After a palette event's run fails to begin, the app does not retry and ask for a frame on every pass. It tries again after the next input in the window, another palette event, or a change to the documents' edit state, the active document or the recovery settings. - The print test now checks that the click's handler ran after the print jobs were canceled, and the list test only fails when the earlier item's text changed.
2856 lines
127 KiB
Rust
2856 lines
127 KiB
Rust
//! ScriptUI's `Window` with modal dialogs, as the help page
|
|
//! <https://extendscript.docsforadobe.dev/user-interface-tools/window-object/> describes it, whose
|
|
//! drawing a host does ([`crate::Realm::define_script_ui`]).
|
|
//!
|
|
//! `new Window("dialog"[, title, bounds, properties])` makes a dialog, with or without `new`, and
|
|
//! `new Window("palette", ...)` a palette, once the host's `palette` allows it. A window of type
|
|
//! `window` throws an `Error` that says it is not available yet.
|
|
//! `new Window` and `add` also take a resource specification in place of the type, as the help
|
|
//! page <https://extendscript.docsforadobe.dev/user-interface-tools/resource-specifications/>
|
|
//! describes it (read by [`crate::scriptui_resource`]), and then read no other argument: the
|
|
//! window or control is made with the creation properties given in `properties`. Then, in order,
|
|
//! each control listed in its braces is added to it through `add`, with `add`'s limits, and
|
|
//! assigned to the container's property of that name, a list's `items` gives its item texts as
|
|
//! `add`'s third argument does, and each other value is assigned to the property of its name. A
|
|
//! malformed specification throws a `SyntaxError`. `add(type[, bounds, text, properties])` adds a
|
|
//! control to a dialog, a palette, a `group` or a `panel` and returns it: `statictext`, `edittext`, `button`,
|
|
//! `checkbox`, `radiobutton`, `dropdownlist`, `listbox` (whose third argument is an array of item
|
|
//! texts), `slider` (third to fifth: value, minimum, maximum) and `progressbar` (value, maximum).
|
|
//! The other documented control types throw an `Error` that says they are not available yet, and
|
|
//! `show()` throws one for an `edittext` whose `properties` have `noecho`. A
|
|
//! group takes its creation properties as the third argument (or the fourth when the third is not
|
|
//! an object), and a list takes them there in place of its items, with the items in
|
|
//! `properties.items`. A list's `add("item", text)` adds an item, `find(text)` finds one and
|
|
//! `remove(what)` removes one; its `selection` is an item or `null`, and takes an item, an index or
|
|
//! `null`. A container's `remove(what)` removes a control, which then has no `parent` and no longer
|
|
//! belongs to the dialog: its `window`, and that of each control it holds, is `null`, and `add`,
|
|
//! `remove` and `removeAll` on them throw an `Error`. Each element has an id, which the description
|
|
//! gives, that no other element of the run has.
|
|
//! Each element starts with `preferredSize`, `size`, `minimumSize` and `maximumSize` objects whose
|
|
//! `width` and `height` are -1, which leaves the size to the layout. Setting a radio button's
|
|
//! `value` to true sets the other radio buttons next to it in its container to false, as a click
|
|
//! does.
|
|
//!
|
|
//! `addEventListener(type, handler[, capture])` and `removeEventListener` register handlers for
|
|
//! the events `show()` runs: `click`, `change`, `changing`, and `show` on a dialog only; a window
|
|
//! or control takes at most [`MAX_LISTENERS`] of them. A handler gets an event object, in the
|
|
//! capture phase on the containers above the control, then on the control after its `onClick`,
|
|
//! `onChange` or `onChanging`, then in the bubbling phase on the containers again, until one calls
|
|
//! `stopPropagation()`. A handler removed before its turn does not run, and a container that no
|
|
//! longer holds the control when its turn comes does not get the event. Events are not cancelable,
|
|
//! and `preventDefault()` does nothing.
|
|
//! `notify(event)` runs the same handlers, and `notify("onClick")` turns a check box over and turns
|
|
//! a radio button on first; it does not close the dialog. `Window.alert`, `Window.confirm` and
|
|
//! `Window.prompt` are the global functions of those names, when the host defines them.
|
|
//!
|
|
//! `show()` gives the host's `open` the dialog's description and runs `onShow`. Then, until the
|
|
//! dialog closes, it gives the host's `wait` the description again, and `wait` gives back what the
|
|
//! person did, which may pause the run ([`crate::interp::Pause`]): a `click` on a control, a
|
|
//! `change` with its new value, or a `close`. The control takes the change, its `onClick`,
|
|
//! `onChanging` or `onChange` runs, and the next description shows what the handler changed. A
|
|
//! button without `onClick` and without a `click` listener of its own closes the dialog with 1 when
|
|
//! it is the dialog's default element and with 2 when it is its cancel element, unless a capturing
|
|
//! listener on a container stopped the click. `close(result)` closes the dialog after the running
|
|
//! handler returns, and `show()` gives `result`, or 0 without one; `onClose` runs first and keeps
|
|
//! the dialog open when it returns `false`. A dialog that is showing cannot be shown again, and no
|
|
//! other dialog can be shown while one is. When `show()` ends, the host's `end` is given its result
|
|
//! and whether it ended normally.
|
|
//!
|
|
//! A palette's `show()` gives the host's `paletteShow` its description and its id (none the first
|
|
//! time), which gives back the id, then runs `onShow` and returns `undefined` without waiting.
|
|
//! `hide()` gives `paletteHide` the id, and `close()` runs `onClose`, and unless it returns `false`
|
|
//! gives `paletteClose` the id; a `close()` while `onClose` runs does nothing more. The realm keeps
|
|
//! each palette the host shows until it closes. `Window.__vcPaletteEvent()` takes an event from the
|
|
//! host's `paletteEvent` (`{palette, event, id, value, element}`, or `null`, with `element` the id
|
|
//! of the control), and gives it to that palette as a dialog takes one, without giving
|
|
//! `paletteUpdate` any description: the host gets the palettes' controls when the run ends
|
|
//! ([`crate::Realm::refresh_script_ui`]). `add()` on a shown palette gives `paletteUpdate` that
|
|
//! palette's description, so the host's list of its controls stays in step.
|
|
//! `update()` on a shown palette, or on a `group` or a `panel` in one, takes each event that waits
|
|
//! for a palette of the run's engine from the host's `paletteTake` (as `paletteEvent` gives it, or
|
|
//! `null`) and gives it to its palette, while the host's `palettePending` says one waits, giving
|
|
//! `paletteUpdate` each palette's description before each one. When a handler's exception ends
|
|
//! this, `update()` calls `palettePending` once more before the exception leaves it. Then, called from the script's own code (not from a
|
|
//! handler, a callback of a built-in function, a getter or a conversion), it asks the host's
|
|
//! `paletteDraw` whether the palette is drawn now, and when it is, gives `paletteUpdate` the
|
|
//! palette's description. On the showing dialog, or a group or panel in it, `update()` does
|
|
//! nothing more, and on a window that is not showing it throws an `Error`. A dialog takes the
|
|
//! person's events once the running handler returns.
|
|
//!
|
|
//! An element keeps its kind, its container, its controls and a list's items in properties that a
|
|
//! script cannot change. The description lists the dialog first and every control after the
|
|
//! container that holds it; it is read with a loop, and a dialog with more than [`MAX_CONTROLS`]
|
|
//! controls, nested more than [`MAX_DEPTH`] deep, with more than [`MAX_ITEMS`] list items, or with a
|
|
//! text longer than [`MAX_TEXT_BYTES`] throws a `RangeError`. These and [`MAX_LISTENERS`] are
|
|
//! VectorCraft's own limits. A getter that changes a list's items or an item's text after the list
|
|
//! was read, or adds or removes a control after the dialog's controls were read, makes `show()`
|
|
//! read the dialog again, and a dialog that changes again throws an `Error`. A `text` getter
|
|
//! that changes a list or a container while `remove(text)` looks for an item or a control makes
|
|
//! `remove` throw an `Error`.
|
|
|
|
use crate::HostItem;
|
|
use crate::builtins::{arg, constructor, data, method, native, pausable_method};
|
|
use crate::heap::{Class, ENUMERABLE, HIDDEN, NativeKind, OBJECT_BYTES, OPEN, ObjId, Object, PROP_BYTES, Prop, key, string_bytes};
|
|
use crate::host::Data;
|
|
use crate::interp::{Abrupt, ErrorType, Interp, JsResult, Pause};
|
|
use crate::scriptui_resource::{self, Fault, Res, Spec};
|
|
use crate::value::{Value, index_key};
|
|
|
|
/// The global property, not an identifier, that the run of a palette's handler calls as
|
|
/// `this[PALETTE_EVENT_GLOBAL]()` to take its event ([`palette_event`]).
|
|
pub const PALETTE_EVENT_GLOBAL: &str = "%vcPaletteEvent";
|
|
|
|
/// The most controls a dialog holds, its groups and panels included.
|
|
pub const MAX_CONTROLS: usize = 512;
|
|
/// How deeply controls nest in a dialog: a control the dialog holds is at depth 1.
|
|
pub const MAX_DEPTH: usize = 16;
|
|
/// The most items a dialog's lists hold together.
|
|
pub const MAX_ITEMS: usize = 1024;
|
|
/// The most bytes (UTF-8) of one text in a dialog: a control's text or name, or a list item's.
|
|
pub const MAX_TEXT_BYTES: usize = 16 << 10;
|
|
/// The most event listeners one window or control holds.
|
|
pub const MAX_LISTENERS: usize = 256;
|
|
|
|
/// The control types `add` makes.
|
|
pub const BUILT: [&str; 11] =
|
|
["group", "panel", "statictext", "edittext", "button", "checkbox", "radiobutton", "dropdownlist", "listbox", "slider", "progressbar"];
|
|
/// The documented control types that are not available yet.
|
|
const LATER: [&str; 10] =
|
|
["tabbedpanel", "tab", "scrollbar", "image", "iconbutton", "treeview", "flashplayer", "customview", "custombutton", "customboundedvalue"];
|
|
|
|
const KIND: &str = "__vcKind";
|
|
const PARENT: &str = "__vcParent";
|
|
const WINDOW: &str = "__vcWindow";
|
|
const KIDS: &str = "__vcKids";
|
|
const DEPTH: &str = "__vcDepth";
|
|
const COUNT: &str = "__vcCount";
|
|
const ITEM_COUNT: &str = "__vcItemCount";
|
|
const CLOSING: &str = "__vcClosing";
|
|
const ITEMS: &str = "__vcItems";
|
|
/// How many times a list's items have been replaced, which tells a list from one whose items were
|
|
/// removed and added again with the same texts.
|
|
const ITEMS_REV: &str = "__vcItemsRev";
|
|
const SELECTION: &str = "__vcSelection";
|
|
const LIST: &str = "__vcList";
|
|
const PALETTE_ID: &str = "__vcPaletteId";
|
|
const SHOWN: &str = "__vcShown";
|
|
const IN_CLOSE: &str = "__vcInClose";
|
|
/// How many times a palette was hidden: an event it took before a hide is not its event after a
|
|
/// later `show()`.
|
|
const HIDES: &str = "__vcHides";
|
|
/// How many times a list's items were added or removed: a palette's waiting change of the list is
|
|
/// for the items it had then.
|
|
const REVISION: &str = "__vcRevision";
|
|
const VALUE: &str = "__vcValue";
|
|
const LISTENERS: &str = "__vcListeners";
|
|
const STOPPED: &str = "__vcStopped";
|
|
const ID: &str = "__vcId";
|
|
/// The number of listeners an element has had registered, which numbers each registration.
|
|
const REGISTERED: &str = "__vcRegistered";
|
|
|
|
/// The longest array an element keeps in a slot: a list's items, a container's controls, its
|
|
/// listeners (four values each), and the elements of a description.
|
|
const LIST_CAP: u32 = {
|
|
let most = if MAX_ITEMS > MAX_CONTROLS + 1 { MAX_ITEMS } else { MAX_CONTROLS + 1 };
|
|
let most = if most > 4 * MAX_LISTENERS { most } else { 4 * MAX_LISTENERS };
|
|
most as u32
|
|
};
|
|
|
|
/// The size properties every element starts with, as objects with `width` and `height`.
|
|
const SIZES: [&str; 4] = ["preferredSize", "size", "minimumSize", "maximumSize"];
|
|
/// The events `addEventListener` takes, which `show()` and `notify` run.
|
|
const EVENTS: [&str; 4] = ["click", "change", "changing", "show"];
|
|
/// The other documented events, which nothing runs yet.
|
|
const EVENTS_LATER: [&str; 14] = [
|
|
"move",
|
|
"moving",
|
|
"resize",
|
|
"resizing",
|
|
"enterKey",
|
|
"focus",
|
|
"blur",
|
|
"mousedown",
|
|
"mouseup",
|
|
"mousemove",
|
|
"mouseover",
|
|
"mouseout",
|
|
"keyup",
|
|
"keydown",
|
|
];
|
|
|
|
/// The objects of `Window`, kept by the realm, the dialog that is showing, and the palettes the
|
|
/// host shows, shown or hidden.
|
|
#[derive(Clone, Debug)]
|
|
pub(crate) struct Classes {
|
|
window_proto: ObjId,
|
|
control_proto: ObjId,
|
|
item_proto: ObjId,
|
|
event_proto: ObjId,
|
|
layout: ObjId,
|
|
host: ObjId,
|
|
selection_get: ObjId,
|
|
selection_set: ObjId,
|
|
radio_get: ObjId,
|
|
radio_set: ObjId,
|
|
showing: Option<ObjId>,
|
|
palettes: Vec<ObjId>,
|
|
/// Whether an `update()` is taking the waiting events ([`take_waiting`]).
|
|
taking: bool,
|
|
/// The event objects whose listeners are running, innermost last.
|
|
dispatching: Vec<ObjId>,
|
|
/// The containers above the targets of the running events.
|
|
above: Vec<ObjId>,
|
|
/// The id the next element gets: a number no other element of the run has.
|
|
next_id: f64,
|
|
}
|
|
|
|
impl Classes {
|
|
/// The objects a collection keeps.
|
|
pub(crate) fn objects(&self) -> Vec<ObjId> {
|
|
let mut all = vec![
|
|
self.window_proto,
|
|
self.control_proto,
|
|
self.item_proto,
|
|
self.event_proto,
|
|
self.layout,
|
|
self.host,
|
|
self.selection_get,
|
|
self.selection_set,
|
|
self.radio_get,
|
|
self.radio_set,
|
|
];
|
|
all.extend(self.showing);
|
|
all.extend(self.palettes.iter().copied());
|
|
all.extend(self.dispatching.iter().copied());
|
|
all.extend(self.above.iter().copied());
|
|
all
|
|
}
|
|
}
|
|
|
|
/// Is `kind` a window's: a dialog's or a palette's?
|
|
fn is_window(kind: &str) -> bool {
|
|
kind == "dialog" || kind == "palette"
|
|
}
|
|
|
|
/// Does `kind` hold controls?
|
|
fn is_container(kind: &str) -> bool {
|
|
is_window(kind) || kind == "group" || kind == "panel"
|
|
}
|
|
|
|
/// Forgets the dialog that was showing, and the events that were running, when a run ended where
|
|
/// it was.
|
|
pub(crate) fn end_run(i: &mut Interp) {
|
|
if let Some(c) = i.script_ui.as_mut() {
|
|
c.showing = None;
|
|
c.dispatching.clear();
|
|
c.above.clear();
|
|
}
|
|
}
|
|
|
|
fn classes(i: &Interp) -> JsResult<Classes> {
|
|
i.script_ui.clone().ok_or_else(|| Abrupt::Internal("Window is not installed".into()))
|
|
}
|
|
|
|
fn error(i: &mut Interp, message: &str) -> Abrupt {
|
|
i.throw(ErrorType::Error, message)
|
|
}
|
|
|
|
fn range(i: &mut Interp, message: &str) -> Abrupt {
|
|
i.throw(ErrorType::Range, message)
|
|
}
|
|
|
|
/// The value of the own data property `name` of `o`, `undefined` when it has none.
|
|
fn slot(i: &Interp, o: ObjId, name: &str) -> JsResult<Value> {
|
|
Ok(match i.ordinary_own_property(o, &key(name))? {
|
|
Some(Prop::Data { value, .. }) => value,
|
|
_ => Value::Undefined,
|
|
})
|
|
}
|
|
|
|
/// Sets the property `name` of `o` that a script can read but not change.
|
|
fn set_slot(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> {
|
|
i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES))?;
|
|
i.define(o, key(name), Prop::Data { value, flags: 0 })
|
|
}
|
|
|
|
/// Gives the list `o` the array `kept` as its items and counts the replacement ([`ITEMS_REV`]).
|
|
fn set_items(i: &mut Interp, o: ObjId, kept: ObjId) -> JsResult<()> {
|
|
let rev = number_slot(i, o, ITEMS_REV)? + 1.0;
|
|
// The array stays kept from collection while the revision's slot reserves room.
|
|
i.rooted(&[Value::Object(kept)], |i| set_slot(i, o, ITEMS_REV, Value::Number(rev)))?;
|
|
set_slot(i, o, ITEMS, Value::Object(kept))
|
|
}
|
|
|
|
/// The writes that give the list `o` the array `kept` as its items and `copy` as the array a script
|
|
/// can change, counting the replacement ([`ITEMS_REV`]), for [`write_all`].
|
|
fn item_writes(i: &Interp, o: ObjId, kept: Value, copy: Value) -> JsResult<[Write<'static>; 3]> {
|
|
let rev = number_slot(i, o, ITEMS_REV)? + 1.0;
|
|
Ok([(o, ITEMS_REV, Value::Number(rev), 0), (o, ITEMS, kept, 0), (o, "items", copy, OPEN)])
|
|
}
|
|
|
|
/// Sets the property `name` of `o` that a script can change.
|
|
fn set_open(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> {
|
|
i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES))?;
|
|
i.define(o, key(name), Prop::Data { value, flags: OPEN })
|
|
}
|
|
|
|
fn number_slot(i: &Interp, o: ObjId, name: &str) -> JsResult<f64> {
|
|
Ok(match slot(i, o, name)? {
|
|
Value::Number(n) => n,
|
|
_ => 0.0,
|
|
})
|
|
}
|
|
|
|
fn object_slot(i: &Interp, o: ObjId, name: &str) -> JsResult<Option<ObjId>> {
|
|
Ok(slot(i, o, name)?.as_object())
|
|
}
|
|
|
|
/// An element's kind, or `None` for a value that is not a ScriptUI window or control.
|
|
fn kind_of(i: &Interp, v: &Value) -> JsResult<Option<(ObjId, String)>> {
|
|
let Some(o) = v.as_object() else { return Ok(None) };
|
|
Ok(match slot(i, o, KIND)? {
|
|
Value::String(s) => Some((o, s.to_rust_string())),
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
/// `this` as an element, or a `TypeError` naming `what`.
|
|
fn element(i: &mut Interp, this: &Value, what: &str) -> JsResult<(ObjId, String)> {
|
|
match kind_of(i, this)? {
|
|
Some(e) => Ok(e),
|
|
None => Err(i.type_error(&format!("{what}: this is not a ScriptUI window or control"))),
|
|
}
|
|
}
|
|
|
|
/// The dialog that holds the element `o`, or an `Error` naming `what` for a control that was
|
|
/// removed from its dialog.
|
|
fn window_of(i: &mut Interp, o: ObjId, what: &str) -> JsResult<ObjId> {
|
|
match object_slot(i, o, WINDOW)? {
|
|
Some(w) => Ok(w),
|
|
None => Err(error(i, &format!("{what}: the control was removed from its dialog"))),
|
|
}
|
|
}
|
|
|
|
/// [`window_of`] for an element whose controls or items are to be removed, checked to be in that
|
|
/// dialog: each container from it up to the dialog holds the one below. An object a script made to
|
|
/// look like an element is a `TypeError` naming `what`.
|
|
fn window_in(i: &mut Interp, o: ObjId, what: &str) -> JsResult<ObjId> {
|
|
let win = window_of(i, o, what)?;
|
|
let mut at = o;
|
|
for _ in 0..=MAX_DEPTH {
|
|
if at == win {
|
|
break;
|
|
}
|
|
let Some(p) = object_slot(i, at, PARENT)? else { break };
|
|
if !kids_of(i, p)?.iter().any(|k| same(k, &Value::Object(at))) {
|
|
break;
|
|
}
|
|
at = p;
|
|
}
|
|
if at == win && object_slot(i, win, WINDOW)? == Some(win) {
|
|
return Ok(win);
|
|
}
|
|
Err(i.type_error(&format!("{what}: this is not a ScriptUI window or control")))
|
|
}
|
|
|
|
/// Are `a` and `b` the same object?
|
|
fn same(a: &Value, b: &Value) -> bool {
|
|
matches!((a.as_object(), b.as_object()), (Some(x), Some(y)) if x == y)
|
|
}
|
|
|
|
/// The values of an array made by [`list`]. An array longer than any such array ([`LIST_CAP`]),
|
|
/// which only an object a script made to look like an element holds, reads as empty.
|
|
fn list_values(i: &Interp, a: ObjId) -> JsResult<Vec<Value>> {
|
|
let length = match &i.object(a)?.class {
|
|
Class::Array { length, .. } if *length <= LIST_CAP => *length,
|
|
_ => return Ok(Vec::new()),
|
|
};
|
|
(0..length)
|
|
.map(|n| {
|
|
Ok(match i.ordinary_own_property(a, &index_key(n))? {
|
|
Some(Prop::Data { value, .. }) => value,
|
|
_ => Value::Undefined,
|
|
})
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// A new array holding `items`: one a script cannot change unless `open`.
|
|
fn list(i: &mut Interp, items: &[Value], open: bool) -> JsResult<ObjId> {
|
|
i.rooted(items, |i| i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(items.len() + 1))))?;
|
|
let proto = i.intrinsics.array_proto;
|
|
let a = i.heap.alloc_object(Object::new(Class::Array { length: 0, length_writable: open }, Some(proto)));
|
|
let flags = if open { OPEN } else { ENUMERABLE };
|
|
for (n, v) in items.iter().enumerate() {
|
|
let n = u32::try_from(n).map_err(|_| Abrupt::Internal("a list is too long".into()))?;
|
|
i.define(a, index_key(n), Prop::Data { value: v.clone(), flags })?;
|
|
}
|
|
i.object_mut(a)?.extensible = open;
|
|
Ok(a)
|
|
}
|
|
|
|
/// A write of `value` to the property of an object with a name and flags, made by [`write_all`].
|
|
type Write<'a> = (ObjId, &'a str, Value, u8);
|
|
|
|
/// Makes `writes` in order, after checking once that the heap has room for the properties they
|
|
/// add, so a run that ends here (out of memory) makes none of them.
|
|
fn write_all(i: &mut Interp, writes: Vec<Write<'_>>) -> JsResult<()> {
|
|
let values: Vec<Value> = writes.iter().map(|(_, _, v, _)| v.clone()).collect();
|
|
i.rooted(&values, |i| {
|
|
// The heap grows only by the properties a write adds.
|
|
let mut growth = 0;
|
|
for (at, name, _, _) in &writes {
|
|
if !i.object(*at)?.props.contains(&key(name)) {
|
|
growth += PROP_BYTES + string_bytes(name.len());
|
|
}
|
|
}
|
|
i.reserve_heap(growth)?;
|
|
for (at, name, value, flags) in writes {
|
|
i.define(at, key(name), Prop::Data { value, flags })?;
|
|
}
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
/// Adds `v` to the end of the list in `o`'s slot `name`, puts a copy a script can change in its
|
|
/// property `public`, and makes the writes in `more` with them. Both arrays are made before the
|
|
/// first write, so a run that ends here leaves the lists and `more`'s properties as they were.
|
|
fn append<'a>(i: &mut Interp, o: ObjId, name: &'a str, public: &'a str, v: Value, more: Vec<Write<'a>>) -> JsResult<()> {
|
|
let mut items = match object_slot(i, o, name)? {
|
|
Some(a) => list_values(i, a)?,
|
|
None => Vec::new(),
|
|
};
|
|
items.push(v);
|
|
i.rooted(&items.clone(), |i| {
|
|
let kept = Value::Object(list(i, &items, false)?);
|
|
i.rooted(std::slice::from_ref(&kept), |i| {
|
|
let copy = Value::Object(list(i, &items, true)?);
|
|
let mut writes = vec![(o, name, kept.clone(), 0), (o, public, copy, OPEN)];
|
|
writes.extend(more);
|
|
write_all(i, writes)
|
|
})
|
|
})
|
|
}
|
|
|
|
fn text_arg(i: &mut Interp, v: &Value) -> JsResult<Value> {
|
|
Ok(match v {
|
|
Value::Undefined => Value::str(""),
|
|
v => Value::String(i.to_str(v)?),
|
|
})
|
|
}
|
|
|
|
/// A new element of `kind`, with the properties every element has.
|
|
fn new_element(
|
|
i: &mut Interp,
|
|
proto: ObjId,
|
|
kind: &str,
|
|
parent: Option<ObjId>,
|
|
window: Option<ObjId>,
|
|
depth: usize,
|
|
args: [&Value; 3],
|
|
) -> JsResult<ObjId> {
|
|
let [bounds, text, props] = args;
|
|
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(32)))?;
|
|
let o = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
|
|
i.temp_roots.push(Value::Object(o));
|
|
let window = window.unwrap_or(o);
|
|
let parent = parent.map_or(Value::Null, Value::Object);
|
|
let layout = classes(i)?.layout;
|
|
set_slot(i, o, KIND, Value::str(kind))?;
|
|
set_slot(i, o, PARENT, parent.clone())?;
|
|
set_slot(i, o, WINDOW, Value::Object(window))?;
|
|
set_slot(i, o, DEPTH, Value::Number(depth as f64))?;
|
|
let id = match i.script_ui.as_mut() {
|
|
Some(c) => {
|
|
c.next_id += 1.0;
|
|
c.next_id
|
|
}
|
|
None => 0.0,
|
|
};
|
|
set_slot(i, o, ID, Value::Number(id))?;
|
|
let listeners = list(i, &[], false)?;
|
|
set_slot(i, o, LISTENERS, Value::Object(listeners))?;
|
|
set_open(i, o, "type", Value::str(kind))?;
|
|
set_open(i, o, "parent", parent)?;
|
|
set_open(i, o, "window", Value::Object(window))?;
|
|
let text = text_arg(i, text)?;
|
|
set_open(i, o, "text", text)?;
|
|
let props = match props {
|
|
Value::Object(_) => props.clone(),
|
|
_ => Value::Object(i.new_object()),
|
|
};
|
|
set_open(i, o, "properties", props.clone())?;
|
|
let name = i.get_value(&props, &key("name"))?;
|
|
let name = text_arg(i, &name)?;
|
|
set_open(i, o, "name", name)?;
|
|
set_open(i, o, "enabled", Value::Bool(true))?;
|
|
set_open(i, o, "visible", Value::Bool(true))?;
|
|
if !matches!(bounds, Value::Undefined | Value::Null) {
|
|
set_open(i, o, "bounds", bounds.clone())?;
|
|
}
|
|
set_open(i, o, "layout", Value::Object(layout))?;
|
|
for name in SIZES {
|
|
let d = dimension(i, -1.0, -1.0)?;
|
|
set_open(i, o, name, d)?;
|
|
}
|
|
let container = is_container(kind);
|
|
if container {
|
|
let kids = list(i, &[], false)?;
|
|
set_slot(i, o, KIDS, Value::Object(kids))?;
|
|
let children = list(i, &[], true)?;
|
|
set_open(i, o, "children", Value::Object(children))?;
|
|
set_open(i, o, "orientation", Value::str(if kind == "group" { "row" } else { "column" }))?;
|
|
set_open(i, o, "alignChildren", Value::str("center"))?;
|
|
set_open(i, o, "spacing", Value::Number(10.0))?;
|
|
set_open(i, o, "margins", Value::Number(if kind == "group" { 0.0 } else { 15.0 }))?;
|
|
}
|
|
// Layout properties given with the creation properties start the element with those values.
|
|
let layout_names: &[&str] = if container { &["alignment", "orientation", "alignChildren", "spacing", "margins"] } else { &["alignment"] };
|
|
for name in layout_names {
|
|
let v = i.get_value(&props, &key(name))?;
|
|
if !matches!(v, Value::Undefined) {
|
|
set_open(i, o, name, v)?;
|
|
}
|
|
}
|
|
Ok(o)
|
|
}
|
|
|
|
/// A new size object with `width` and `height`.
|
|
fn dimension(i: &mut Interp, width: f64, height: f64) -> JsResult<Value> {
|
|
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(2)))?;
|
|
let o = i.new_object();
|
|
i.rooted(&[Value::Object(o)], |i| {
|
|
set_open(i, o, "width", Value::Number(width))?;
|
|
set_open(i, o, "height", Value::Number(height))?;
|
|
Ok(Value::Object(o))
|
|
})
|
|
}
|
|
|
|
/// `new Window(type[, title, bounds, properties])`.
|
|
fn window_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let kind = i.to_str(&arg(args, 0))?.to_rust_string();
|
|
let spec = if is_resource(&kind) { Some(resource(i, &kind, "Window")?) } else { None };
|
|
let lower = spec.as_ref().map_or(kind.as_str(), |s| s.class.as_str()).to_lowercase();
|
|
if lower == "window" {
|
|
return Err(error(i, "Window: a window of type window is not available yet; use dialog or palette"));
|
|
}
|
|
if lower != "dialog" && lower != "palette" {
|
|
return Err(error(i, "Window: the type must be dialog, palette or window"));
|
|
}
|
|
if lower == "palette" {
|
|
let allowed = host_fn(i, "palette")?;
|
|
if !i.is_callable(&allowed) {
|
|
return Err(error(i, "Window: this host shows no palettes"));
|
|
}
|
|
i.call(&allowed, &Value::Undefined, &[])?;
|
|
}
|
|
let ty = if lower == "palette" { "palette" } else { "dialog" };
|
|
let proto = classes(i)?.window_proto;
|
|
let mark = i.temp_roots.len();
|
|
let r = (|| {
|
|
// A resource specification gives everything; the other arguments are not read.
|
|
let o = match &spec {
|
|
Some(spec) => {
|
|
let creation = creation_properties(i, spec)?;
|
|
new_element(i, proto, ty, None, None, 0, [&Value::Undefined, &Value::Undefined, &creation])?
|
|
}
|
|
None => new_element(i, proto, ty, None, None, 0, [&arg(args, 2), &arg(args, 1), &arg(args, 3)])?,
|
|
};
|
|
set_slot(i, o, COUNT, Value::Number(0.0))?;
|
|
set_slot(i, o, ITEM_COUNT, Value::Number(0.0))?;
|
|
if let Some(spec) = &spec {
|
|
apply_resource(i, o, spec, false)?;
|
|
}
|
|
Ok(Value::Object(o))
|
|
})();
|
|
i.temp_roots.truncate(mark);
|
|
r
|
|
}
|
|
|
|
/// Is the type `text` a resource specification: a type with braces, or a name before a `:`? A type
|
|
/// name alone is not.
|
|
fn is_resource(text: &str) -> bool {
|
|
text.contains('{') || text.contains(':')
|
|
}
|
|
|
|
/// The resource specification `text` given to `what`, or a `SyntaxError` or `RangeError` naming
|
|
/// `what`.
|
|
fn resource(i: &mut Interp, text: &str, what: &str) -> JsResult<Spec> {
|
|
match scriptui_resource::parse(text) {
|
|
Ok(spec) => Ok(spec),
|
|
Err(Fault::Syntax(m)) => Err(i.throw(ErrorType::Syntax, &format!("{what}: {m}"))),
|
|
Err(Fault::Limit(m)) => Err(range(i, &format!("{what}: {m}"))),
|
|
}
|
|
}
|
|
|
|
/// The value of a resource specification's `properties`, its creation properties, kept in the
|
|
/// temporary roots; `undefined` when it has none.
|
|
fn creation_properties(i: &mut Interp, spec: &Spec) -> JsResult<Value> {
|
|
let Some((_, v)) = spec.props.iter().rev().find(|(name, _)| name == "properties") else { return Ok(Value::Undefined) };
|
|
if matches!(v, Res::Control(_)) {
|
|
return Err(i.type_error("properties: give the creation properties as an object"));
|
|
}
|
|
let v = resource_value(i, v)?;
|
|
i.temp_roots.push(v.clone());
|
|
Ok(v)
|
|
}
|
|
|
|
/// A new value for the resource value `v`, built in the temporary roots.
|
|
fn resource_value(i: &mut Interp, v: &Res) -> JsResult<Value> {
|
|
Ok(match v {
|
|
Res::Null => Value::Null,
|
|
Res::Bool(b) => Value::Bool(*b),
|
|
Res::Number(n) => Value::Number(*n),
|
|
Res::String(s) => Value::str(s),
|
|
Res::Array(items) => {
|
|
let mut values = Vec::with_capacity(items.len());
|
|
for item in items {
|
|
let v = resource_value(i, item)?;
|
|
i.temp_roots.push(v.clone());
|
|
values.push(v);
|
|
}
|
|
let a = list(i, &values, true)?;
|
|
i.temp_roots.push(Value::Object(a));
|
|
Value::Object(a)
|
|
}
|
|
Res::Object(props) => {
|
|
i.reserve_heap(OBJECT_BYTES)?;
|
|
let o = i.new_object();
|
|
i.temp_roots.push(Value::Object(o));
|
|
for (name, v) in props {
|
|
let v = resource_value(i, v)?;
|
|
set_open(i, o, name, v)?;
|
|
}
|
|
Value::Object(o)
|
|
}
|
|
Res::Control(_) => return Err(i.type_error("a control is not a value")),
|
|
})
|
|
}
|
|
|
|
/// Adds the control `spec` describes to the container `parent`, as `add` does, with the controls
|
|
/// and properties it lists.
|
|
fn add_resource(i: &mut Interp, parent: ObjId, spec: &Spec) -> JsResult<Value> {
|
|
let creation = creation_properties(i, spec)?;
|
|
// `add` reads a slider's and a progress bar's later arguments as numbers; their creation
|
|
// properties are set after `add` makes them.
|
|
let class = spec.class.to_lowercase();
|
|
let numeric = matches!(class.as_str(), "slider" | "progressbar");
|
|
// A list's `items` gives its item texts, as `add`'s third argument does.
|
|
let list_kind = matches!(class.as_str(), "dropdownlist" | "listbox");
|
|
let items = match spec.props.iter().rev().find(|(name, _)| name == "items") {
|
|
// Only an array is item texts; `add` would read an object third as creation properties.
|
|
Some((_, Res::Null)) if list_kind => Value::Undefined,
|
|
Some((_, v)) if list_kind && !matches!(v, Res::Array(_)) => {
|
|
return Err(i.type_error("items: give a list's items as an array"));
|
|
}
|
|
Some((_, v)) if list_kind => {
|
|
let v = resource_value(i, v)?;
|
|
i.temp_roots.push(v.clone());
|
|
v
|
|
}
|
|
_ => Value::Undefined,
|
|
};
|
|
let args = if numeric { vec![Value::str(&spec.class)] } else { vec![Value::str(&spec.class), Value::Undefined, items, creation.clone()] };
|
|
let c = add(i, Value::Object(parent), &args, false)?;
|
|
i.temp_roots.push(c.clone());
|
|
let Some(o) = c.as_object() else { return Err(Abrupt::Internal("add gave no control".into())) };
|
|
if numeric && creation.as_object().is_some() {
|
|
set_open(i, o, "properties", creation.clone())?;
|
|
let name = i.get_value(&creation, &key("name"))?;
|
|
let name = text_arg(i, &name)?;
|
|
set_open(i, o, "name", name)?;
|
|
let alignment = i.get_value(&creation, &key("alignment"))?;
|
|
if !matches!(alignment, Value::Undefined) {
|
|
set_open(i, o, "alignment", alignment)?;
|
|
}
|
|
}
|
|
apply_resource(i, o, spec, list_kind)?;
|
|
Ok(c)
|
|
}
|
|
|
|
/// Sets the properties `spec` lists on the element `o`, in order, leaving out `properties`, and
|
|
/// `items` for a list (`list`): a control is added to `o` and assigned to `o`'s property of that
|
|
/// name, and any other value is assigned to the property of its name.
|
|
fn apply_resource(i: &mut Interp, o: ObjId, spec: &Spec, list: bool) -> JsResult<()> {
|
|
for (name, v) in &spec.props {
|
|
if name == "properties" || (list && name == "items") {
|
|
continue;
|
|
}
|
|
let value = match v {
|
|
Res::Control(child) => add_resource(i, o, child)?,
|
|
v => resource_value(i, v)?,
|
|
};
|
|
i.temp_roots.push(value.clone());
|
|
i.put(o, key(name), value, true)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn number_arg(i: &mut Interp, v: &Value, what: &str) -> JsResult<f64> {
|
|
let n = i.to_number(v)?;
|
|
if !n.is_finite() {
|
|
return Err(range(i, &format!("{what} is not a finite number")));
|
|
}
|
|
Ok(n)
|
|
}
|
|
|
|
/// `container.add(type[, bounds, text, properties])` and `list.add("item", text)`.
|
|
fn add(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, kind) = element(i, &this, "add")?;
|
|
window_of(i, o, "add")?;
|
|
let ty = i.to_str(&arg(args, 0))?.to_rust_string();
|
|
if kind == "dropdownlist" || kind == "listbox" {
|
|
if ty.to_lowercase() != "item" {
|
|
return Err(error(i, "add: a list holds only items"));
|
|
}
|
|
let item = add_item(i, o, &arg(args, 1))?;
|
|
if let Some(win) = object_slot(i, o, WINDOW)? {
|
|
i.rooted(std::slice::from_ref(&item), |i| publish_palette(i, win))?;
|
|
}
|
|
return Ok(item);
|
|
}
|
|
if !is_container(&kind) {
|
|
return Err(error(i, &format!("add: a {kind} holds no controls")));
|
|
}
|
|
if is_resource(&ty) {
|
|
let spec = resource(i, &ty, "add")?;
|
|
let mark = i.temp_roots.len();
|
|
let r = add_resource(i, o, &spec);
|
|
i.temp_roots.truncate(mark);
|
|
return r;
|
|
}
|
|
let ty = ty.to_lowercase();
|
|
if LATER.contains(&ty.as_str()) {
|
|
return Err(error(i, &format!("add: the control type {ty} is not available yet")));
|
|
}
|
|
let Some(&ty) = BUILT.iter().find(|b| **b == ty) else { return Err(error(i, &format!("add: there is no control type {ty}"))) };
|
|
let depth = number_slot(i, o, DEPTH)? as usize + 1;
|
|
if depth > MAX_DEPTH {
|
|
return Err(range(i, &format!("add: controls can be nested at most {MAX_DEPTH} deep")));
|
|
}
|
|
let win = window_of(i, o, "add")?;
|
|
let count = number_slot(i, win, COUNT)?;
|
|
if count as usize >= MAX_CONTROLS {
|
|
return Err(range(i, &format!("add: a dialog can hold at most {MAX_CONTROLS} controls")));
|
|
}
|
|
let list_kind = ty == "dropdownlist" || ty == "listbox";
|
|
let third = arg(args, 2);
|
|
let is_array = |i: &Interp, v: &Value| -> JsResult<bool> {
|
|
Ok(match v.as_object() {
|
|
Some(a) => matches!(i.object(a)?.class, Class::Array { .. }),
|
|
None => false,
|
|
})
|
|
};
|
|
// A group takes its creation properties third, and so may a list in place of its items; when
|
|
// the third argument is not an object, they are the fourth.
|
|
let props_third = third.as_object().is_some() && (ty == "group" || (list_kind && !is_array(i, &third)?));
|
|
let props = if props_third { third.clone() } else { arg(args, 3) };
|
|
let proto = classes(i)?.control_proto;
|
|
let mark = i.temp_roots.len();
|
|
let r = (|| {
|
|
let mut given_items = if list_kind && !props_third { third.clone() } else { Value::Undefined };
|
|
if list_kind && props.as_object().is_some() {
|
|
let listed = i.get_value(&props, &key("items"))?;
|
|
i.temp_roots.push(listed.clone());
|
|
if !matches!(listed, Value::Undefined | Value::Null) {
|
|
if !matches!(given_items, Value::Undefined | Value::Null) {
|
|
return Err(i.type_error("add: give a list's items as the third argument or in properties.items, not both"));
|
|
}
|
|
given_items = listed;
|
|
}
|
|
}
|
|
if ty == "listbox" && props.as_object().is_some() && i.get_value(&props, &key("multiselect"))?.truthy() {
|
|
return Err(error(i, "add: a list box that selects several items is not available yet"));
|
|
}
|
|
let creation = if matches!(ty, "slider" | "progressbar") { Value::Undefined } else { props.clone() };
|
|
let text = if list_kind || matches!(ty, "group" | "slider" | "progressbar") { Value::Undefined } else { third.clone() };
|
|
let c = new_element(i, proto, ty, Some(o), Some(win), depth, [&arg(args, 1), &text, &creation])?;
|
|
match ty {
|
|
"checkbox" => set_open(i, c, "value", Value::Bool(false))?,
|
|
"radiobutton" => {
|
|
set_slot(i, c, VALUE, Value::Bool(false))?;
|
|
let cl = classes(i)?;
|
|
i.define(c, key("value"), Prop::Accessor { get: Some(cl.radio_get), set: Some(cl.radio_set), flags: ENUMERABLE })?;
|
|
}
|
|
"slider" | "progressbar" => {
|
|
let (value, min, max) =
|
|
if ty == "slider" { (arg(args, 2), arg(args, 3), arg(args, 4)) } else { (arg(args, 2), Value::Undefined, arg(args, 3)) };
|
|
let given = |v: &Value| !matches!(v, Value::Undefined);
|
|
let value = if given(&value) { number_arg(i, &value, "add: the value")? } else { 0.0 };
|
|
let min = if given(&min) { number_arg(i, &min, "add: the minimum value")? } else { 0.0 };
|
|
let max = if given(&max) { number_arg(i, &max, "add: the maximum value")? } else { 100.0 };
|
|
set_open(i, c, "minvalue", Value::Number(min))?;
|
|
set_open(i, c, "maxvalue", Value::Number(max))?;
|
|
set_open(i, c, "value", Value::Number(value))?;
|
|
}
|
|
"dropdownlist" | "listbox" => {
|
|
let kept = list(i, &[], false)?;
|
|
set_items(i, c, kept)?;
|
|
set_slot(i, c, SELECTION, Value::Number(-1.0))?;
|
|
let items = list(i, &[], true)?;
|
|
set_open(i, c, "items", Value::Object(items))?;
|
|
let cl = classes(i)?;
|
|
i.define(c, key("selection"), Prop::Accessor { get: Some(cl.selection_get), set: Some(cl.selection_set), flags: ENUMERABLE })?;
|
|
}
|
|
_ => {}
|
|
}
|
|
// A new list's items take their places in the dialog only once the list is added.
|
|
let mut staged = 0usize;
|
|
if list_kind {
|
|
match given_items.clone() {
|
|
Value::Undefined | Value::Null => {}
|
|
Value::Object(a) if matches!(i.object(a)?.class, Class::Array { .. }) => {
|
|
let length = match &i.object(a)?.class {
|
|
Class::Array { length, .. } => *length as usize,
|
|
_ => 0,
|
|
};
|
|
if length > MAX_ITEMS.saturating_sub(number_slot(i, win, ITEM_COUNT)? as usize) {
|
|
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
|
|
}
|
|
// Each item is read as a script reads it, so an index getter gives its value.
|
|
let mut texts = Vec::with_capacity(length);
|
|
for n in 0..length {
|
|
let n = u32::try_from(n).map_err(|_| Abrupt::Internal("a list is too long".into()))?;
|
|
let item = i.get_value(&Value::Object(a), &index_key(n))?;
|
|
i.temp_roots.push(item.clone());
|
|
let text = text_arg(i, &item)?;
|
|
i.temp_roots.push(text.clone());
|
|
texts.push(text);
|
|
}
|
|
staged = new_items(i, c, &texts)?;
|
|
}
|
|
_ => return Err(i.type_error("add: give a list's items as an array")),
|
|
}
|
|
}
|
|
// A getter or a conversion above may have added controls or items itself, or removed the
|
|
// container from its dialog. An object made to look like a container is not in the dialog,
|
|
// so it charges nothing to the dialog's controls.
|
|
window_in(i, o, "add")?;
|
|
let count = number_slot(i, win, COUNT)?;
|
|
if count as usize >= MAX_CONTROLS {
|
|
return Err(range(i, &format!("add: a dialog can hold at most {MAX_CONTROLS} controls")));
|
|
}
|
|
let items = number_slot(i, win, ITEM_COUNT)?;
|
|
if items as usize + staged > MAX_ITEMS {
|
|
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
|
|
}
|
|
let counts = vec![(win, COUNT, Value::Number(count + 1.0), 0), (win, ITEM_COUNT, Value::Number(items + staged as f64), 0)];
|
|
append(i, o, KIDS, "children", Value::Object(c), counts)?;
|
|
// The host's copy of a shown palette keeps each control's place in the list, which events
|
|
// name it by.
|
|
publish_palette(i, win)?;
|
|
Ok(Value::Object(c))
|
|
})();
|
|
i.temp_roots.truncate(mark);
|
|
r
|
|
}
|
|
|
|
/// Adds an item with `text` to the list `o`, which the dialog holds.
|
|
fn add_item(i: &mut Interp, o: ObjId, text: &Value) -> JsResult<Value> {
|
|
// The conversion runs first: it may add or remove items itself.
|
|
let text = text_arg(i, text)?;
|
|
i.rooted(std::slice::from_ref(&text), |i| {
|
|
let win = window_in(i, o, "add")?;
|
|
let count = number_slot(i, win, ITEM_COUNT)?;
|
|
if count as usize >= MAX_ITEMS {
|
|
return Err(range(i, &format!("add: a dialog's lists hold at most {MAX_ITEMS} items in all")));
|
|
}
|
|
new_item(i, o, text.clone(), vec![(win, ITEM_COUNT, Value::Number(count + 1.0), 0)])
|
|
})
|
|
}
|
|
|
|
/// Adds an item with `text` to the end of the list `o`, making the writes in `more` with it.
|
|
fn new_item(i: &mut Interp, o: ObjId, text: Value, more: Vec<Write<'_>>) -> JsResult<Value> {
|
|
let proto = classes(i)?.item_proto;
|
|
i.rooted(std::slice::from_ref(&text), |i| {
|
|
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(4)))?;
|
|
let item = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
|
|
i.rooted(&[Value::Object(item)], |i| {
|
|
set_slot(i, item, LIST, Value::Object(o))?;
|
|
set_open(i, item, "text", text.clone())?;
|
|
let mut more = more;
|
|
more.push((o, REVISION, Value::Number(number_slot(i, o, REVISION)? + 1.0), 0));
|
|
append(i, o, ITEMS, "items", Value::Object(item), more)?;
|
|
Ok(Value::Object(item))
|
|
})
|
|
})
|
|
}
|
|
|
|
/// Gives the new list `o`, which has no items yet, an item for each of `texts`, building its
|
|
/// arrays of items once, without counting them in the dialog's items. → how many were added.
|
|
fn new_items(i: &mut Interp, o: ObjId, texts: &[Value]) -> JsResult<usize> {
|
|
let proto = classes(i)?.item_proto;
|
|
let mark = i.temp_roots.len();
|
|
let r = (|| {
|
|
let mut items = Vec::with_capacity(texts.len());
|
|
for text in texts {
|
|
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(4)))?;
|
|
let item = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
|
|
i.temp_roots.push(Value::Object(item));
|
|
set_slot(i, item, LIST, Value::Object(o))?;
|
|
set_open(i, item, "text", text.clone())?;
|
|
items.push(Value::Object(item));
|
|
}
|
|
let kept = list(i, &items, false)?;
|
|
i.temp_roots.push(Value::Object(kept));
|
|
set_items(i, o, kept)?;
|
|
// Each item counts as one change of the list's items ([`REVISION`]), as `add` counts it.
|
|
let revision = number_slot(i, o, REVISION)? + items.len() as f64;
|
|
set_slot(i, o, REVISION, Value::Number(revision))?;
|
|
let copy = list(i, &items, true)?;
|
|
set_open(i, o, "items", Value::Object(copy))?;
|
|
Ok(items.len())
|
|
})();
|
|
i.temp_roots.truncate(mark);
|
|
r
|
|
}
|
|
|
|
fn list_of(i: &mut Interp, this: &Value, what: &str) -> JsResult<ObjId> {
|
|
match kind_of(i, this)? {
|
|
Some((o, kind)) if kind == "dropdownlist" || kind == "listbox" => Ok(o),
|
|
_ => Err(i.type_error(&format!("{what}: this is not a list"))),
|
|
}
|
|
}
|
|
|
|
fn items_of(i: &Interp, o: ObjId) -> JsResult<Vec<Value>> {
|
|
match object_slot(i, o, ITEMS)? {
|
|
Some(a) => list_values(i, a),
|
|
None => Ok(Vec::new()),
|
|
}
|
|
}
|
|
|
|
fn selection_index(i: &Interp, o: ObjId) -> JsResult<Option<usize>> {
|
|
let n = number_slot(i, o, SELECTION)?;
|
|
Ok((n >= 0.0).then_some(n as usize))
|
|
}
|
|
|
|
fn selection_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let o = list_of(i, &this, "selection")?;
|
|
let items = items_of(i, o)?;
|
|
Ok(selection_index(i, o)?.and_then(|n| items.get(n).cloned()).unwrap_or(Value::Null))
|
|
}
|
|
|
|
fn selection_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let o = list_of(i, &this, "selection")?;
|
|
let items = items_of(i, o)?;
|
|
let v = arg(args, 0);
|
|
let n = match &v {
|
|
Value::Undefined | Value::Null => -1.0,
|
|
Value::Number(n) if n.fract() == 0.0 && *n >= 0.0 && (*n as usize) < items.len() => *n,
|
|
Value::Number(n) => return Err(range(i, &format!("selection: the list has no item {n}"))),
|
|
v => match items.iter().position(|item| same(item, v)) {
|
|
Some(n) => n as f64,
|
|
None => return Err(i.type_error("selection: give an item of this list, its index or null")),
|
|
},
|
|
};
|
|
set_slot(i, o, SELECTION, Value::Number(n))?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// The list an item belongs to and its index there.
|
|
fn item_place(i: &mut Interp, this: &Value, what: &str) -> JsResult<(ObjId, Option<usize>)> {
|
|
let list = match this.as_object() {
|
|
Some(o) => object_slot(i, o, LIST)?,
|
|
None => None,
|
|
};
|
|
let Some(list) = list else { return Err(i.type_error(&format!("{what}: this is not a list item"))) };
|
|
let index = items_of(i, list)?.iter().position(|item| same(item, this));
|
|
Ok((list, index))
|
|
}
|
|
|
|
fn item_index(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (_, index) = item_place(i, &this, "index")?;
|
|
Ok(Value::Number(index.map_or(-1.0, |n| n as f64)))
|
|
}
|
|
|
|
fn item_selected_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (list, index) = item_place(i, &this, "selected")?;
|
|
Ok(Value::Bool(index.is_some() && selection_index(i, list)? == index))
|
|
}
|
|
|
|
fn item_selected_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (list, index) = item_place(i, &this, "selected")?;
|
|
let Some(index) = index else { return Ok(Value::Undefined) };
|
|
if arg(args, 0).truthy() {
|
|
set_slot(i, list, SELECTION, Value::Number(index as f64))?;
|
|
} else if selection_index(i, list)? == Some(index) {
|
|
set_slot(i, list, SELECTION, Value::Number(-1.0))?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
fn item_to_string(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
item_place(i, &this, "toString")?;
|
|
let text = i.get_value(&this, &key("text"))?;
|
|
Ok(Value::String(i.to_str(&text)?))
|
|
}
|
|
|
|
/// `list.removeAll()`.
|
|
fn remove_all(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let o = list_of(i, &this, "removeAll")?;
|
|
let win = window_in(i, o, "removeAll")?;
|
|
let n = items_of(i, o)?.len() as f64;
|
|
let count = number_slot(i, win, ITEM_COUNT)?;
|
|
// Both empty arrays are made before the first write, so a run that ends here leaves the list
|
|
// and the dialog's count of items as they were.
|
|
let kept = Value::Object(list(i, &[], false)?);
|
|
i.rooted(std::slice::from_ref(&kept), |i| {
|
|
let items = Value::Object(list(i, &[], true)?);
|
|
let mut writes = vec![(win, ITEM_COUNT, Value::Number((count - n).max(0.0)), 0)];
|
|
writes.extend(item_writes(i, o, kept.clone(), items)?);
|
|
writes.push((o, SELECTION, Value::Number(-1.0), 0));
|
|
writes.push((o, REVISION, Value::Number(number_slot(i, o, REVISION)? + 1.0), 0));
|
|
write_all(i, writes)
|
|
})?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
fn kids_of(i: &Interp, o: ObjId) -> JsResult<Vec<Value>> {
|
|
match object_slot(i, o, KIDS)? {
|
|
Some(a) => list_values(i, a),
|
|
None => Ok(Vec::new()),
|
|
}
|
|
}
|
|
|
|
/// `findElement(name)`: the first control in the element, in order, whose `name` is `name`.
|
|
fn find_element(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "findElement")?;
|
|
let want = i.to_str(&arg(args, 0))?;
|
|
let mark = i.temp_roots.len();
|
|
let r = (|| {
|
|
// The controls still to look at stay rooted while a `name` getter runs, and each one is
|
|
// looked at only while the element still holds it.
|
|
let mut stack: Vec<Value> = kids_of(i, o)?.into_iter().rev().collect();
|
|
i.temp_roots.extend(stack.iter().cloned());
|
|
while let Some(v) = stack.pop() {
|
|
i.use_fuel()?;
|
|
let Some(c) = v.as_object() else { continue };
|
|
if !holds(i, o, c)? {
|
|
continue;
|
|
}
|
|
let name = i.get_value(&v, &key("name"))?;
|
|
if !holds(i, o, c)? {
|
|
continue;
|
|
}
|
|
if matches!(&name, Value::String(s) if *s == want) {
|
|
return Ok(v);
|
|
}
|
|
let kids = kids_of(i, c)?;
|
|
i.temp_roots.extend(kids.iter().cloned());
|
|
stack.extend(kids.into_iter().rev());
|
|
}
|
|
Ok(Value::Null)
|
|
})();
|
|
i.temp_roots.truncate(mark);
|
|
r
|
|
}
|
|
|
|
/// Does the element `top` hold the control `c`: does each container from `c`'s up to `top` hold
|
|
/// the one below?
|
|
fn holds(i: &Interp, top: ObjId, c: ObjId) -> JsResult<bool> {
|
|
let mut at = c;
|
|
for _ in 0..=MAX_DEPTH {
|
|
let Some(p) = object_slot(i, at, PARENT)? else { return Ok(false) };
|
|
if !kids_of(i, p)?.iter().any(|k| same(k, &Value::Object(at))) {
|
|
return Ok(false);
|
|
}
|
|
if p == top {
|
|
return Ok(true);
|
|
}
|
|
at = p;
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
/// Calls the element's handler `name` when it has one. → its result, or `None` without one.
|
|
fn handler(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Option<Value>> {
|
|
let f = i.get_value(&Value::Object(o), &key(name))?;
|
|
if !i.is_callable(&f) {
|
|
return Ok(None);
|
|
}
|
|
i.call(&f, &Value::Object(o), &[]).map(Some)
|
|
}
|
|
|
|
/// The radio buttons that form a set with `c`: it and the radio buttons next to it in its
|
|
/// container, up to the first control of another kind on each side.
|
|
fn radio_set_of(i: &Interp, c: ObjId) -> JsResult<Vec<ObjId>> {
|
|
let Some(parent) = object_slot(i, c, PARENT)? else { return Ok(vec![c]) };
|
|
let kids = kids_of(i, parent)?;
|
|
let Some(at) = kids.iter().position(|k| same(k, &Value::Object(c))) else { return Ok(vec![c]) };
|
|
let radio = |v: &Value| -> JsResult<Option<ObjId>> {
|
|
Ok(match kind_of(i, v)? {
|
|
Some((o, k)) if k == "radiobutton" => Some(o),
|
|
_ => None,
|
|
})
|
|
};
|
|
let mut set = vec![c];
|
|
for k in kids[..at].iter().rev() {
|
|
let Some(o) = radio(k)? else { break };
|
|
set.push(o);
|
|
}
|
|
for k in &kids[at + 1..] {
|
|
let Some(o) = radio(k)? else { break };
|
|
set.push(o);
|
|
}
|
|
Ok(set)
|
|
}
|
|
|
|
fn radio_get(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "value")?;
|
|
Ok(Value::Bool(slot(i, o, VALUE)?.truthy()))
|
|
}
|
|
|
|
/// A radio button's `value`: true turns the other radio buttons of its set off.
|
|
fn radio_set(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "value")?;
|
|
let on = arg(args, 0).truthy();
|
|
if on {
|
|
for other in radio_set_of(i, o)? {
|
|
if other != o {
|
|
set_slot(i, other, VALUE, Value::Bool(false))?;
|
|
}
|
|
}
|
|
}
|
|
set_slot(i, o, VALUE, Value::Bool(on))?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// A listener registered on an element: its event type, handler, capture flag and registration
|
|
/// number.
|
|
type Listener = (Value, Value, bool, f64);
|
|
|
|
/// The listeners registered on `o`, in the order they were added.
|
|
fn listeners_of(i: &Interp, o: ObjId) -> JsResult<Vec<Listener>> {
|
|
let flat = match object_slot(i, o, LISTENERS)? {
|
|
Some(a) => list_values(i, a)?,
|
|
None => Vec::new(),
|
|
};
|
|
Ok(flat
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.map(|[t, f, c, n]| (t.clone(), f.clone(), c.truthy(), if let Value::Number(n) = n { *n } else { 0.0 }))
|
|
.collect())
|
|
}
|
|
|
|
fn set_listeners(i: &mut Interp, o: ObjId, all: &[Listener]) -> JsResult<()> {
|
|
let flat: Vec<Value> = all.iter().flat_map(|(t, f, c, n)| [t.clone(), f.clone(), Value::Bool(*c), Value::Number(*n)]).collect();
|
|
i.rooted(&flat.clone(), |i| {
|
|
let kept = list(i, &flat, false)?;
|
|
set_slot(i, o, LISTENERS, Value::Object(kept))
|
|
})
|
|
}
|
|
|
|
/// Is the stored event type `t` the type `ty`?
|
|
fn is_type(t: &Value, ty: &str) -> bool {
|
|
matches!(t, Value::String(s) if s.to_rust_string() == ty)
|
|
}
|
|
|
|
/// The event type `addEventListener` and `removeEventListener` were given, checked.
|
|
fn event_type(i: &mut Interp, v: &Value, what: &str) -> JsResult<&'static str> {
|
|
let text = i.to_str(v)?.to_rust_string();
|
|
if let Some(name) = EVENTS.iter().find(|e| **e == text) {
|
|
return Ok(name);
|
|
}
|
|
if EVENTS_LATER.contains(&text.as_str()) {
|
|
return Err(error(i, &format!("{what}: the event {text} is not available yet")));
|
|
}
|
|
Err(error(i, &format!("{what}: there is no event {}", text.chars().take(64).collect::<String>())))
|
|
}
|
|
|
|
/// `addEventListener(type, handler[, capture])`.
|
|
fn add_event_listener(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, kind) = element(i, &this, "addEventListener")?;
|
|
let ty = event_type(i, &arg(args, 0), "addEventListener")?;
|
|
if ty == "show" && kind != "dialog" {
|
|
return Err(error(i, "addEventListener: the event show runs only on a dialog"));
|
|
}
|
|
let f = arg(args, 1);
|
|
if !i.is_callable(&f) {
|
|
return Err(i.type_error("addEventListener: give the handler as a function"));
|
|
}
|
|
let capture = arg(args, 2).truthy();
|
|
let mut all = listeners_of(i, o)?;
|
|
if !all.iter().any(|(t, g, c, _)| is_type(t, ty) && same(g, &f) && *c == capture) {
|
|
if all.len() >= MAX_LISTENERS {
|
|
return Err(range(i, &format!("addEventListener: a window or control takes at most {MAX_LISTENERS} listeners")));
|
|
}
|
|
// A listener removed and added again is a new registration, which an event already
|
|
// running does not reach.
|
|
let n = number_slot(i, o, REGISTERED)? + 1.0;
|
|
set_slot(i, o, REGISTERED, Value::Number(n))?;
|
|
all.push((Value::str(ty), f, capture, n));
|
|
set_listeners(i, o, &all)?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `removeEventListener(type, handler[, capture])`.
|
|
fn remove_event_listener(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "removeEventListener")?;
|
|
let ty = event_type(i, &arg(args, 0), "removeEventListener")?;
|
|
let f = arg(args, 1);
|
|
let capture = arg(args, 2).truthy();
|
|
let mut all = listeners_of(i, o)?;
|
|
let before = all.len();
|
|
all.retain(|(t, g, c, _)| !(is_type(t, ty) && same(g, &f) && *c == capture));
|
|
if all.len() != before {
|
|
set_listeners(i, o, &all)?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `event.stopPropagation()`: stops an event whose listeners are running; it does nothing to any
|
|
/// other object.
|
|
fn event_stop(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
if let Some(o) = this.as_object()
|
|
&& classes(i)?.dispatching.contains(&o)
|
|
{
|
|
set_slot(i, o, STOPPED, Value::Bool(true))?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// Runs the listeners for `ty` on `at` (only those for `capture` unless it is `None`), with `event`,
|
|
/// in the order they were added. A listener that an earlier one removed does not run, even when
|
|
/// it was added again, and neither does one registered after `cutoff`, the last registration on
|
|
/// `at` when the event started. None runs once the palette of `taker` no longer takes the event.
|
|
/// → whether it had any; `None` when the palette stopped taking the event.
|
|
fn run_listeners(
|
|
i: &mut Interp,
|
|
at: ObjId,
|
|
ty: &str,
|
|
capture: Option<bool>,
|
|
(event, phase, cutoff): (ObjId, f64, f64),
|
|
taker: Taker,
|
|
) -> JsResult<Option<bool>> {
|
|
let wanted = |i: &Interp| -> JsResult<Vec<(Value, f64)>> {
|
|
Ok(listeners_of(i, at)?
|
|
.into_iter()
|
|
.filter(|(t, _, c, n)| is_type(t, ty) && capture.is_none_or(|want| *c == want) && *n <= cutoff)
|
|
.map(|(_, f, _, n)| (f, n))
|
|
.collect())
|
|
};
|
|
let first = wanted(i)?;
|
|
if first.is_empty() {
|
|
return Ok(Some(false));
|
|
}
|
|
let functions: Vec<Value> = first.iter().map(|(f, _)| f.clone()).collect();
|
|
i.rooted(&functions, |i| {
|
|
set_open(i, event, "currentTarget", Value::Object(at))?;
|
|
set_open(i, event, "eventPhase", Value::Number(phase))?;
|
|
for (f, n) in &first {
|
|
if !wanted(i)?.iter().any(|(_, m)| m == n) {
|
|
continue;
|
|
}
|
|
if !takes(i, taker)? {
|
|
return Ok(None);
|
|
}
|
|
i.call(f, &Value::Object(at), &[Value::Object(event)])?;
|
|
}
|
|
Ok(Some(true))
|
|
})
|
|
}
|
|
|
|
/// Runs the event `ty` on the element `target`: the listeners of the containers above it that
|
|
/// capture, its `callback`, its own listeners, then the listeners of the containers that do not
|
|
/// capture, until one stops it. → whether the event was handled: the element had a callback or a
|
|
/// listener for it, or a capturing listener above it stopped it. Listeners of the containers that
|
|
/// do not capture do not count. A container that a listener took the element out of before its
|
|
/// turn does not hear the event.
|
|
fn dispatch(i: &mut Interp, target: ObjId, ty: &str, callback: &str) -> JsResult<bool> {
|
|
Ok(dispatch_for(i, target, ty, callback, None)?.unwrap_or(false))
|
|
}
|
|
|
|
/// The palette, the event it takes and the event's control, whose handlers run only while the
|
|
/// palette takes the event ([`still_takes`]) and holds the control; `None` for a dialog, which
|
|
/// always takes it.
|
|
type Taker = Option<(ObjId, ObjId, ObjId)>;
|
|
|
|
/// Does the palette of `taker` still take its event and hold its control, with the control and
|
|
/// every container above it enabled and visible as they are now, read without running a getter
|
|
/// ([`plain_state`])? Always true for a dialog.
|
|
fn takes(i: &mut Interp, taker: Taker) -> JsResult<bool> {
|
|
match taker {
|
|
Some((win, ev, c)) => Ok(still_takes(i, win, ev, None)? && holds(i, win, c)? && live_up(i, win, c)?),
|
|
None => Ok(true),
|
|
}
|
|
}
|
|
|
|
/// Whether `c` and each container above it, up to and including `top`, are enabled and visible,
|
|
/// read without running a getter ([`plain_state`]).
|
|
fn live_up(i: &Interp, top: ObjId, c: ObjId) -> JsResult<bool> {
|
|
let mut at = c;
|
|
for _ in 0..=MAX_DEPTH + 1 {
|
|
if !plain_state(i, at, "enabled")? || !plain_state(i, at, "visible")? {
|
|
return Ok(false);
|
|
}
|
|
if at == top {
|
|
return Ok(true);
|
|
}
|
|
let Some(p) = object_slot(i, at, PARENT)? else { return Ok(false) };
|
|
at = p;
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
/// [`dispatch`] for the event a palette takes (`taker`): no handler or listener runs once a getter,
|
|
/// a handler or a listener of the event closed the palette or took the event's control out of it.
|
|
/// → `None` when that happened.
|
|
fn dispatch_for(i: &mut Interp, target: ObjId, ty: &str, callback: &str, taker: Taker) -> JsResult<Option<bool>> {
|
|
let above = containers_above(i, target)?;
|
|
let proto = classes(i)?.event_proto;
|
|
i.reserve_heap(OBJECT_BYTES.saturating_add(PROP_BYTES.saturating_mul(10)))?;
|
|
let event = i.heap.alloc_object(Object::new(Class::Ordinary, Some(proto)));
|
|
// The event and the containers stay kept from collection while the listeners run.
|
|
let kept = match i.script_ui.as_mut() {
|
|
Some(c) => {
|
|
c.dispatching.push(event);
|
|
let mark = c.above.len();
|
|
c.above.extend(above.iter().copied());
|
|
mark
|
|
}
|
|
None => 0,
|
|
};
|
|
let r = run_event(i, event, target, &above, ty, callback, taker);
|
|
if let Some(c) = i.script_ui.as_mut() {
|
|
if let Some(n) = c.dispatching.iter().rposition(|e| *e == event) {
|
|
c.dispatching.remove(n);
|
|
}
|
|
c.above.truncate(kept);
|
|
}
|
|
r
|
|
}
|
|
|
|
/// The containers that hold `target`, innermost first. A chain longer than a dialog's nesting
|
|
/// allows comes from objects a script made to look like elements, and is a `TypeError`.
|
|
fn containers_above(i: &mut Interp, target: ObjId) -> JsResult<Vec<ObjId>> {
|
|
let mut above = Vec::new();
|
|
let mut at = object_slot(i, target, PARENT)?;
|
|
while let Some(p) = at {
|
|
i.use_fuel()?;
|
|
if above.len() > MAX_DEPTH {
|
|
return Err(i.type_error("the event's target is not a ScriptUI window or control"));
|
|
}
|
|
above.push(p);
|
|
at = object_slot(i, p, PARENT)?;
|
|
}
|
|
Ok(above)
|
|
}
|
|
|
|
/// The phases of [`dispatch_for`] for the event object `event`.
|
|
fn run_event(i: &mut Interp, event: ObjId, target: ObjId, above: &[ObjId], ty: &str, callback: &str, taker: Taker) -> JsResult<Option<bool>> {
|
|
i.rooted(&[Value::Object(event)], |i| {
|
|
let bubbles = ty != "show";
|
|
set_open(i, event, "type", Value::str(ty))?;
|
|
set_open(i, event, "target", Value::Object(target))?;
|
|
set_open(i, event, "bubbles", Value::Bool(bubbles))?;
|
|
set_open(i, event, "cancelable", Value::Bool(false))?;
|
|
set_open(i, event, "detail", Value::Number(if ty == "click" { 1.0 } else { 0.0 }))?;
|
|
let stopped = |i: &Interp| -> JsResult<bool> { Ok(slot(i, event, STOPPED)?.truthy()) };
|
|
// A listener registered once the event started does not hear it, in any phase.
|
|
let cutoff = |i: &Interp, o: ObjId| number_slot(i, o, REGISTERED);
|
|
let target_cutoff = cutoff(i, target)?;
|
|
let cutoffs = above.iter().map(|p| cutoff(i, *p)).collect::<JsResult<Vec<f64>>>()?;
|
|
for (p, n) in above.iter().zip(&cutoffs).rev() {
|
|
if !containers_above(i, target)?.contains(p) {
|
|
continue;
|
|
}
|
|
if run_listeners(i, *p, ty, Some(true), (event, 1.0, *n), taker)?.is_none() {
|
|
return Ok(None);
|
|
}
|
|
if stopped(i)? {
|
|
return Ok(Some(true));
|
|
}
|
|
}
|
|
if !takes(i, taker)? {
|
|
return Ok(None);
|
|
}
|
|
let f = i.get_value(&Value::Object(target), &key(callback))?;
|
|
// A getter for the callback may have closed the palette.
|
|
if !takes(i, taker)? {
|
|
return Ok(None);
|
|
}
|
|
let had = i.is_callable(&f);
|
|
if had {
|
|
i.call(&f, &Value::Object(target), &[])?;
|
|
}
|
|
if !takes(i, taker)? {
|
|
return Ok(None);
|
|
}
|
|
let Some(heard) = run_listeners(i, target, ty, None, (event, 2.0, target_cutoff), taker)? else { return Ok(None) };
|
|
if bubbles && !stopped(i)? {
|
|
for (p, n) in above.iter().zip(&cutoffs) {
|
|
if !containers_above(i, target)?.contains(p) {
|
|
continue;
|
|
}
|
|
if run_listeners(i, *p, ty, Some(false), (event, 3.0, *n), taker)?.is_none() {
|
|
return Ok(None);
|
|
}
|
|
if stopped(i)? {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
Ok(Some(had || heard))
|
|
})
|
|
}
|
|
|
|
/// A click on the control `c` of `kind`: a check box turns over and a radio button turns on, then
|
|
/// the click runs. → whether the click was handled, as [`dispatch`] says.
|
|
fn click_control(i: &mut Interp, c: ObjId, kind: &str) -> JsResult<bool> {
|
|
Ok(click_control_for(i, c, kind, None)?.unwrap_or(false))
|
|
}
|
|
|
|
/// [`click_control`] for the click a palette takes (`taker`), as [`dispatch_for`] runs it.
|
|
fn click_control_for(i: &mut Interp, c: ObjId, kind: &str, taker: Taker) -> JsResult<Option<bool>> {
|
|
if kind == "checkbox" {
|
|
let on = i.get_value(&Value::Object(c), &key("value"))?.truthy();
|
|
// A `value` getter may have disabled or hidden the box, or closed its palette.
|
|
if taker.is_some() && !takes(i, taker)? {
|
|
return Ok(None);
|
|
}
|
|
i.put(c, key("value"), Value::Bool(!on), true)?;
|
|
} else if kind == "radiobutton" {
|
|
i.put(c, key("value"), Value::Bool(true), true)?;
|
|
}
|
|
dispatch_for(i, c, "click", "onClick", taker)
|
|
}
|
|
|
|
/// `notify([event])`: simulates the control's `onClick`, `onChange` or `onChanging`.
|
|
fn notify(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, kind) = element(i, &this, "notify")?;
|
|
let event = match arg(args, 0) {
|
|
Value::Undefined if matches!(kind.as_str(), "button" | "checkbox" | "radiobutton") => "onClick".to_string(),
|
|
Value::Undefined => "onChange".to_string(),
|
|
v => i.to_str(&v)?.to_rust_string(),
|
|
};
|
|
match event.as_str() {
|
|
"onClick" => click_control(i, o, &kind)?,
|
|
"onChange" => dispatch(i, o, "change", "onChange")?,
|
|
"onChanging" => dispatch(i, o, "changing", "onChanging")?,
|
|
_ => return Err(error(i, "notify: give onClick, onChange or onChanging")),
|
|
};
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// The index in `values` that `what` names: an index, one of the values, or a text that an
|
|
/// element's `text` equals. A text is converted before the values are read.
|
|
fn index_named(i: &mut Interp, what: &Value, values: impl Fn(&Interp) -> JsResult<Vec<Value>>) -> JsResult<(Vec<Value>, Option<usize>)> {
|
|
match what {
|
|
Value::Number(n) => {
|
|
let all = values(i)?;
|
|
let at = (*n >= 0.0 && n.fract() == 0.0 && (*n as usize) < all.len()).then_some(*n as usize);
|
|
Ok((all, at))
|
|
}
|
|
Value::Object(_) => {
|
|
let all = values(i)?;
|
|
let at = all.iter().position(|v| same(v, what));
|
|
Ok((all, at))
|
|
}
|
|
v => {
|
|
let want = i.to_str(v)?;
|
|
let all = values(i)?;
|
|
i.rooted(&all.clone(), |i| {
|
|
for (n, item) in all.iter().enumerate() {
|
|
i.use_fuel()?;
|
|
let t = i.get_value(item, &key("text"))?;
|
|
if matches!(&t, Value::String(s) if *s == want) {
|
|
return Ok((all.clone(), Some(n)));
|
|
}
|
|
}
|
|
Ok((all.clone(), None))
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `list.find(text)`: the first item whose text is `text`, or `null`.
|
|
fn find(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let o = list_of(i, &this, "find")?;
|
|
let text = Value::String(i.to_str(&arg(args, 0))?);
|
|
let (items, at) = index_named(i, &text, |i| items_of(i, o))?;
|
|
Ok(at.and_then(|n| items.get(n).cloned()).unwrap_or(Value::Null))
|
|
}
|
|
|
|
/// The controls `o` holds whose container is `o`.
|
|
fn own_kids(i: &Interp, o: ObjId) -> JsResult<Vec<Value>> {
|
|
let mut own = Vec::new();
|
|
for k in kids_of(i, o)? {
|
|
if let Some(c) = k.as_object()
|
|
&& object_slot(i, c, PARENT)? == Some(o)
|
|
{
|
|
own.push(k);
|
|
}
|
|
}
|
|
Ok(own)
|
|
}
|
|
|
|
/// The controls in `c` and in the containers it holds, `c` included, and the number of their list
|
|
/// items.
|
|
fn subtree(i: &mut Interp, c: &Value) -> JsResult<(Vec<ObjId>, f64)> {
|
|
let (mut controls, mut items) = (Vec::new(), 0.0);
|
|
let mut stack = vec![c.clone()];
|
|
while let Some(v) = stack.pop() {
|
|
i.use_fuel()?;
|
|
let Some(o) = v.as_object() else { continue };
|
|
controls.push(o);
|
|
items += items_of(i, o)?.len() as f64;
|
|
stack.extend(own_kids(i, o)?);
|
|
}
|
|
Ok((controls, items))
|
|
}
|
|
|
|
/// `remove(what)`: removes a list's item, or a container's control, given by its index, the
|
|
/// object itself or its text. Nothing happens when there is no such item or control.
|
|
fn remove(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, kind) = element(i, &this, "remove")?;
|
|
let what = arg(args, 0);
|
|
window_in(i, o, "remove")?;
|
|
if kind == "dropdownlist" || kind == "listbox" {
|
|
let before = slot(i, o, ITEMS)?;
|
|
let (mut items, at) = i.rooted(std::slice::from_ref(&before), |i| index_named(i, &what, |i| items_of(i, o)))?;
|
|
// A `text` getter read during the lookup may have changed the list or removed it.
|
|
if !same(&slot(i, o, ITEMS)?, &before) {
|
|
return Err(error(i, "remove: the list changed while the item was looked up"));
|
|
}
|
|
let win = window_in(i, o, "remove")?;
|
|
let Some(at) = at else { return Ok(Value::Undefined) };
|
|
items.remove(at);
|
|
let count = number_slot(i, win, ITEM_COUNT)?;
|
|
let selection = match selection_index(i, o)? {
|
|
Some(n) if n == at => Some(-1.0),
|
|
Some(n) if n > at => Some((n - 1) as f64),
|
|
_ => None,
|
|
};
|
|
// Both arrays are made before the first write, so a run that ends here leaves the list,
|
|
// its selection and the dialog's count of items as they were.
|
|
i.rooted(&items.clone(), |i| {
|
|
let kept = Value::Object(list(i, &items, false)?);
|
|
i.rooted(std::slice::from_ref(&kept), |i| {
|
|
let copy = Value::Object(list(i, &items, true)?);
|
|
let mut writes = item_writes(i, o, kept.clone(), copy)?.to_vec();
|
|
writes.push((win, ITEM_COUNT, Value::Number((count - 1.0).max(0.0)), 0));
|
|
writes.extend(selection.map(|n| (o, SELECTION, Value::Number(n), 0)));
|
|
writes.push((o, REVISION, Value::Number(number_slot(i, o, REVISION)? + 1.0), 0));
|
|
write_all(i, writes)
|
|
})
|
|
})?;
|
|
publish_palette(i, win)?;
|
|
return Ok(Value::Undefined);
|
|
}
|
|
if !is_container(&kind) {
|
|
return Err(error(i, &format!("remove: a {kind} holds no controls or items")));
|
|
}
|
|
let before = slot(i, o, KIDS)?;
|
|
let (mut kids, at) = i.rooted(std::slice::from_ref(&before), |i| index_named(i, &what, |i| kids_of(i, o)))?;
|
|
// A `text` getter read during the lookup may have changed the container or removed it.
|
|
if !same(&slot(i, o, KIDS)?, &before) {
|
|
return Err(error(i, "remove: the container's controls changed while the control was looked up"));
|
|
}
|
|
let win = window_in(i, o, "remove")?;
|
|
let Some(at) = at else { return Ok(Value::Undefined) };
|
|
if !matches!(kids.get(at).and_then(Value::as_object), Some(c) if object_slot(i, c, PARENT)? == Some(o)) {
|
|
return Err(i.type_error("remove: this is not a ScriptUI window or control"));
|
|
}
|
|
let gone = kids.remove(at);
|
|
let Some(root) = gone.as_object() else { return Ok(Value::Undefined) };
|
|
let mut keep = kids.clone();
|
|
keep.push(gone);
|
|
i.rooted(&keep, |i| {
|
|
// Everything that can fail runs before the first change, so a run that ends here (out of
|
|
// fuel or memory) leaves the container, the dialog's counts and the control as they were.
|
|
let (controls, items) = subtree(i, &keep[keep.len() - 1])?;
|
|
let kept = Value::Object(list(i, &kids, false)?);
|
|
i.rooted(std::slice::from_ref(&kept), |i| {
|
|
let copy = Value::Object(list(i, &kids, true)?);
|
|
i.rooted(std::slice::from_ref(©), |i| {
|
|
let count = number_slot(i, win, COUNT)?;
|
|
let total = number_slot(i, win, ITEM_COUNT)?;
|
|
let mut writes = vec![
|
|
(o, KIDS, kept.clone(), 0),
|
|
(o, "children", copy.clone(), OPEN),
|
|
(win, COUNT, Value::Number((count - controls.len() as f64).max(0.0)), 0),
|
|
(win, ITEM_COUNT, Value::Number((total - items).max(0.0)), 0),
|
|
];
|
|
writes.extend(detached(root, &controls));
|
|
// The heap grows only by the properties a write adds, so the room for them is
|
|
// checked once, before the first write.
|
|
let mut growth = 0;
|
|
for (at, name, _, _) in &writes {
|
|
if !i.object(*at)?.props.contains(&key(name)) {
|
|
growth += PROP_BYTES + string_bytes(name.len());
|
|
}
|
|
}
|
|
i.reserve_heap(growth)?;
|
|
for (at, name, value, flags) in writes {
|
|
i.define(at, key(name), Prop::Data { value, flags })?;
|
|
}
|
|
Ok(())
|
|
})
|
|
})
|
|
})?;
|
|
// The host's copy of a shown palette keeps each control's place in the list, which events name
|
|
// it by.
|
|
publish_palette(i, win)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// The writes that take the control `root`, removed from its container, out of the dialog: it has
|
|
/// no container, and neither it nor `controls`, the controls it holds, belong to the dialog any
|
|
/// longer, so their `window` is `null`.
|
|
fn detached(root: ObjId, controls: &[ObjId]) -> Vec<(ObjId, &'static str, Value, u8)> {
|
|
let mut writes = Vec::with_capacity(2 * controls.len() + 2);
|
|
for &o in controls {
|
|
writes.push((o, WINDOW, Value::Null, 0));
|
|
writes.push((o, "window", Value::Null, OPEN));
|
|
}
|
|
writes.push((root, PARENT, Value::Null, 0));
|
|
writes.push((root, "parent", Value::Null, OPEN));
|
|
writes
|
|
}
|
|
|
|
fn control_show(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "show")?;
|
|
i.put(o, key("visible"), Value::Bool(true), true)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
fn control_hide(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, _) = element(i, &this, "hide")?;
|
|
i.put(o, key("visible"), Value::Bool(false), true)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
fn nothing(_: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `this` as a window, and whether it is a palette.
|
|
fn this_window(i: &mut Interp, this: &Value, what: &str) -> JsResult<(ObjId, bool)> {
|
|
match kind_of(i, this)? {
|
|
Some((o, kind)) if is_window(&kind) => Ok((o, kind == "palette")),
|
|
_ => Err(i.type_error(&format!("{what}: this is not a ScriptUI window"))),
|
|
}
|
|
}
|
|
|
|
/// `close([result])`: closes the dialog that is showing once the running handler returns, or the
|
|
/// palette at once.
|
|
fn close(i: &mut Interp, this: Value, args: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, palette) = this_window(i, &this, "close")?;
|
|
let result = match arg(args, 0) {
|
|
Value::Undefined => 0.0,
|
|
v => number_arg(i, &v, "close: the result")?,
|
|
};
|
|
if palette {
|
|
close_palette(i, o)?;
|
|
} else if classes(i)?.showing == Some(o) {
|
|
set_slot(i, o, CLOSING, Value::Number(result))?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `hide()`: closes the dialog that is showing with the result 0, or hides the palette, which a
|
|
/// later `show()` shows again as it was. A palette's `visible` is false once the host hid it, and
|
|
/// true again once the host shows it.
|
|
fn hide(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let (o, palette) = this_window(i, &this, "hide")?;
|
|
if !palette {
|
|
return close(i, this, &[], false);
|
|
}
|
|
if let Value::Number(id) = slot(i, o, PALETTE_ID)? {
|
|
let f = host_fn(i, "paletteHide")?;
|
|
i.call(&f, &Value::Undefined, &[Value::Number(id)])?;
|
|
set_slot(i, o, SHOWN, Value::Bool(false))?;
|
|
let hides = number_slot(i, o, HIDES)? + 1.0;
|
|
set_slot(i, o, HIDES, Value::Number(hides))?;
|
|
i.put(o, key("visible"), Value::Bool(false), true)?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `update()` on a window: [`update_window`].
|
|
fn update(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let own = own_code(i);
|
|
let (o, palette) = this_window(i, &this, "update")?;
|
|
update_window(i, o, palette, own)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// Whether the native running now was called from the script's own code in a run that can pause,
|
|
/// as the host's functions find whether a run can wait.
|
|
fn own_code(i: &Interp) -> bool {
|
|
i.in_slice && i.reentry == 0
|
|
}
|
|
|
|
/// `update()` on a `group` or a `panel`: [`update_window`] for the window that holds it.
|
|
fn control_update(i: &mut Interp, this: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let own = own_code(i);
|
|
let (o, kind) = element(i, &this, "update")?;
|
|
if !is_container(&kind) {
|
|
return Err(i.type_error("update: this is not a ScriptUI window or container"));
|
|
}
|
|
let win = window_of(i, o, "update")?;
|
|
let palette = matches!(slot(i, win, KIND)?, Value::String(s) if s.to_rust_string() == "palette");
|
|
update_window(i, win, palette, own)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `update()` on the window `win`. On a shown palette: hands each event that waits for a palette of
|
|
/// the run's engine to its palette ([`take_waiting`]), and then, from the script's own code (`own`),
|
|
/// gives the host the palette's controls when the host draws it now (`paletteDraw`). The showing
|
|
/// dialog's controls go to the host when its handler returns, since a handler runs to its end in
|
|
/// one slice. Whether a palette shows is the host's, not its `visible` property. A window that is
|
|
/// not showing throws.
|
|
fn update_window(i: &mut Interp, win: ObjId, palette: bool, own: bool) -> JsResult<()> {
|
|
let showing = if palette { slot(i, win, SHOWN)?.truthy() } else { classes(i)?.showing == Some(win) };
|
|
if !showing {
|
|
return Err(error(i, "update: the window is not showing"));
|
|
}
|
|
if !palette {
|
|
return Ok(());
|
|
}
|
|
take_waiting(i)?;
|
|
if !own || !slot(i, win, SHOWN)?.truthy() {
|
|
return Ok(());
|
|
}
|
|
let f = host_fn(i, "paletteDraw")?;
|
|
if i.call(&f, &Value::Undefined, &[])?.truthy() {
|
|
update_palette(i, win)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Hands each event the host's `paletteTake` gives, oldest first, to its palette, until none waits
|
|
/// (`palettePending`). Before each one the host gets every palette's controls as the script and the
|
|
/// handlers before it left them, as at the end of a run, and drops an event whose control has since
|
|
/// been disabled, hidden or removed. An `update()` that a getter or a handler calls meanwhile takes
|
|
/// no events: the `update()` already taking them goes on with the next one.
|
|
fn take_waiting(i: &mut Interp) -> JsResult<()> {
|
|
if i.script_ui.as_ref().is_some_and(|c| c.taking) {
|
|
return Ok(());
|
|
}
|
|
set_taking(i, true);
|
|
let r = take_each(i);
|
|
set_taking(i, false);
|
|
r
|
|
}
|
|
|
|
fn set_taking(i: &mut Interp, taking: bool) {
|
|
if let Some(c) = i.script_ui.as_mut() {
|
|
c.taking = taking;
|
|
}
|
|
}
|
|
|
|
/// [`take_waiting`]'s loop.
|
|
fn take_each(i: &mut Interp) -> JsResult<()> {
|
|
let pending = host_fn(i, "palettePending")?;
|
|
let f = host_fn(i, "paletteTake")?;
|
|
loop {
|
|
if !i.call(&pending, &Value::Undefined, &[])?.truthy() {
|
|
return Ok(());
|
|
}
|
|
update_palettes(i)?;
|
|
let answer = i.call(&f, &Value::Undefined, &[])?;
|
|
if answer.as_object().is_none() {
|
|
return Ok(());
|
|
}
|
|
if let Err(e) = deliver(i, answer) {
|
|
// The host learns that the event's handlers have ended, as it does before the next
|
|
// event, when a handler's exception ends the loop.
|
|
if let Abrupt::Throw(v) = &e {
|
|
let v = v.clone();
|
|
let _ = i.rooted(std::slice::from_ref(&v), |i| i.call(&pending, &Value::Undefined, &[]));
|
|
}
|
|
return Err(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn set_palettes(i: &mut Interp, f: impl FnOnce(&mut Vec<ObjId>)) {
|
|
if let Some(c) = i.script_ui.as_mut() {
|
|
f(&mut c.palettes);
|
|
}
|
|
}
|
|
|
|
/// Closes the palette `win` once its `onClose` allows it: the host no longer shows it, its
|
|
/// `visible` is false and its handlers no longer run. A `close()` while its `onClose` runs does
|
|
/// nothing more.
|
|
fn close_palette(i: &mut Interp, win: ObjId) -> JsResult<()> {
|
|
if slot(i, win, IN_CLOSE)?.truthy() {
|
|
return Ok(());
|
|
}
|
|
if !may_close(i, win)? {
|
|
return Ok(());
|
|
}
|
|
if let Value::Number(id) = slot(i, win, PALETTE_ID)? {
|
|
set_slot(i, win, PALETTE_ID, Value::Undefined)?;
|
|
set_slot(i, win, SHOWN, Value::Bool(false))?;
|
|
set_palettes(i, |p| p.retain(|o| *o != win));
|
|
let f = host_fn(i, "paletteClose")?;
|
|
i.call(&f, &Value::Undefined, &[Value::Number(id)])?;
|
|
// The host has closed it; a `visible` the script made read-only stays as it is.
|
|
i.put(win, key("visible"), Value::Bool(false), false)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Gives the host the controls of the palette `win` as they are now, when it shows the palette. A
|
|
/// palette that `hide()` hid is not read: `show()` gives its controls when it shows it again.
|
|
fn update_palette(i: &mut Interp, win: ObjId) -> JsResult<()> {
|
|
let Value::Number(id) = slot(i, win, PALETTE_ID)? else { return Ok(()) };
|
|
if !slot(i, win, SHOWN)?.truthy() {
|
|
return Ok(());
|
|
}
|
|
let hides = number_slot(i, win, HIDES)?;
|
|
let (desc, ..) = describe(i, win)?;
|
|
// A getter that hid or closed the palette leaves the host the controls it had then.
|
|
let same = matches!(slot(i, win, PALETTE_ID)?, Value::Number(now) if now == id);
|
|
if !same || !slot(i, win, SHOWN)?.truthy() || number_slot(i, win, HIDES)? != hides {
|
|
return Ok(());
|
|
}
|
|
let f = host_fn(i, "paletteUpdate")?;
|
|
i.rooted(std::slice::from_ref(&desc), |i| i.call(&f, &Value::Undefined, &[desc.clone(), Value::Number(id)]))?;
|
|
Ok(())
|
|
}
|
|
|
|
/// [`update_palette`] after an `add` or a `remove` that has already changed the palette: what a
|
|
/// getter throws while the palette is read does not take the place of the change's result. The
|
|
/// host gets the controls at the next update, or when the run ends ([`refresh`]).
|
|
fn publish_palette(i: &mut Interp, win: ObjId) -> JsResult<()> {
|
|
match update_palette(i, win) {
|
|
// The exception no longer propagates, so a later error is reported at its own place.
|
|
Err(Abrupt::Throw(_)) => {
|
|
i.passing = Value::Undefined;
|
|
i.thrown_mark = None;
|
|
i.throw_site = None;
|
|
Ok(())
|
|
}
|
|
r => r,
|
|
}
|
|
}
|
|
|
|
/// Gives the host the controls of every palette it shows, after a handler may have changed them.
|
|
fn update_palettes(i: &mut Interp) -> JsResult<()> {
|
|
// A getter may close a later palette, which removes it from the rooted list, so the copy is
|
|
// rooted until the loop ends.
|
|
let palettes = classes(i)?.palettes;
|
|
let roots: Vec<Value> = palettes.iter().map(|&win| Value::Object(win)).collect();
|
|
i.rooted(&roots, |i| {
|
|
for win in palettes {
|
|
update_palette(i, win)?;
|
|
}
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
/// Gives the host the controls of every palette the realm keeps, as a run left them
|
|
/// ([`crate::Realm::refresh_script_ui`]). When the getters queued promise jobs, the jobs run and
|
|
/// the host gets the controls once more, and the jobs that second pass queues run too. An
|
|
/// `update()` that a getter or a job calls meanwhile takes no waiting event, which waits for a run
|
|
/// of its own.
|
|
pub(crate) fn refresh(i: &mut Interp) -> JsResult<()> {
|
|
let was = i.script_ui.as_ref().is_some_and(|c| c.taking);
|
|
set_taking(i, true);
|
|
let r = (|| {
|
|
refresh_palettes(i)?;
|
|
if i.jobs_waiting() {
|
|
i.drain_jobs(Value::Undefined)?;
|
|
refresh_palettes(i)?;
|
|
i.drain_jobs(Value::Undefined)?;
|
|
}
|
|
Ok(())
|
|
})();
|
|
set_taking(i, was);
|
|
r
|
|
}
|
|
|
|
/// Gives the host the controls of every palette the realm keeps, or does nothing without palettes.
|
|
fn refresh_palettes(i: &mut Interp) -> JsResult<()> {
|
|
if i.script_ui.as_ref().is_none_or(|c| c.palettes.is_empty()) {
|
|
return Ok(());
|
|
}
|
|
update_palettes(i)
|
|
}
|
|
|
|
/// A palette's `show()`: the host shows its description at once, then `onShow` runs, and `show()`
|
|
/// returns `undefined` without waiting.
|
|
fn show_palette(i: &mut Interp, win: ObjId) -> JsResult<Pause> {
|
|
let (desc, ..) = describe(i, win)?;
|
|
let id = slot(i, win, PALETTE_ID)?;
|
|
let f = host_fn(i, "paletteShow")?;
|
|
Ok(Pause { host: f, args: vec![desc, id], keep: vec![Value::Object(win)], resume: palette_shown, failed: None })
|
|
}
|
|
|
|
fn palette_shown(i: &mut Interp, keep: &[Value], id: Value) -> JsResult<Value> {
|
|
let Some(win) = arg(keep, 0).as_object() else { return Err(Abrupt::Internal("show lost its palette".into())) };
|
|
let Value::Number(_) = id else { return Err(Abrupt::Internal("the host gave the palette no id".into())) };
|
|
set_slot(i, win, PALETTE_ID, id)?;
|
|
if !classes(i)?.palettes.contains(&win) {
|
|
set_palettes(i, |p| p.push(win));
|
|
}
|
|
set_slot(i, win, SHOWN, Value::Bool(true))?;
|
|
i.put(win, key("visible"), Value::Bool(true), true)?;
|
|
handler(i, win, "onShow")?;
|
|
update_palette(i, win)?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// `Window.__vcPaletteEvent()`, which the run of a palette's handler calls: takes the event from the
|
|
/// host's `paletteEvent` and gives it to the palette the way a dialog takes the person's event. The
|
|
/// host gets every palette's controls once the run ends ([`crate::Realm::refresh_script_ui`]). An
|
|
/// event for a palette that has closed does nothing.
|
|
fn palette_event(i: &mut Interp, _: Value, _: &[Value], _: bool) -> JsResult<Value> {
|
|
let f = host_fn(i, "paletteEvent")?;
|
|
let answer = i.call(&f, &Value::Undefined, &[])?;
|
|
// An `update()` that a handler calls while this event runs takes no waiting event, so a later
|
|
// event does not run before this one has finished.
|
|
let was = i.script_ui.as_ref().is_some_and(|c| c.taking);
|
|
set_taking(i, true);
|
|
let r = deliver(i, answer);
|
|
set_taking(i, was);
|
|
r?;
|
|
Ok(Value::Undefined)
|
|
}
|
|
|
|
/// Gives the host's event `answer` (`{palette, event, id, value, element}`, or anything else for
|
|
/// none) to its palette the way a dialog takes the person's event. An event for a palette that has
|
|
/// closed does nothing.
|
|
fn deliver(i: &mut Interp, answer: Value) -> JsResult<Value> {
|
|
let Some(ev) = answer.as_object() else { return Ok(Value::Undefined) };
|
|
let mark = i.temp_roots.len();
|
|
i.temp_roots.push(answer.clone());
|
|
let r = (|| {
|
|
let id = slot(i, ev, "palette")?;
|
|
let palettes = classes(i)?.palettes;
|
|
let mut win = None;
|
|
for p in palettes {
|
|
if matches!((slot(i, p, PALETTE_ID)?, &id), (Value::Number(a), Value::Number(b)) if a == *b) {
|
|
win = Some(p);
|
|
}
|
|
}
|
|
let Some(win) = win else { return Ok(Value::Undefined) };
|
|
// A hide from here on, even one a later show() follows, ends the palette's hold on the event.
|
|
let hides = number_slot(i, win, HIDES)?;
|
|
set_slot(i, ev, HIDES, Value::Number(hides))?;
|
|
let event = match slot(i, ev, "event")? {
|
|
Value::String(s) => s.to_rust_string(),
|
|
_ => String::new(),
|
|
};
|
|
if event == "close" {
|
|
close_palette(i, win)?;
|
|
} else {
|
|
let (desc, nodes, ..) = describe(i, win)?;
|
|
// A getter read in the description may have closed the palette.
|
|
if !still_takes(i, win, ev, None)? {
|
|
return Ok(Value::Undefined);
|
|
}
|
|
i.temp_roots.push(desc.clone());
|
|
i.temp_roots.push(Value::Object(nodes));
|
|
let held = list_values(i, nodes)?;
|
|
// The host gives the control's id, which is found again in the palette as it is now: a
|
|
// getter read in the description may have added or removed a control before it.
|
|
let at = match slot(i, ev, "element")? {
|
|
Value::Number(element) => {
|
|
let mut at = None;
|
|
for (n, v) in held.iter().enumerate().skip(1) {
|
|
if let Some(c) = v.as_object()
|
|
&& number_slot(i, c, ID)? == element
|
|
{
|
|
at = Some(n);
|
|
break;
|
|
}
|
|
}
|
|
at
|
|
}
|
|
_ => match slot(i, ev, "id")? {
|
|
Value::Number(n) if n >= 1.0 && n.fract() == 0.0 => Some(n as usize),
|
|
_ => None,
|
|
},
|
|
};
|
|
// A getter read in the description may also have removed the control after the
|
|
// description listed it: no handler runs for it.
|
|
let gone = match at.filter(|&n| n >= 1 && n < held.len()) {
|
|
Some(n) => match held[n].as_object() {
|
|
Some(c) => !holds(i, win, c)?,
|
|
None => false,
|
|
},
|
|
None => true,
|
|
};
|
|
let Some(at) = at.filter(|_| !gone) else {
|
|
return Err(error(i, "a palette event names a control the palette no longer holds"));
|
|
};
|
|
// A getter read in the description may have disabled or hidden the control, or a
|
|
// container above it: the event is dropped, as the host drops one for such a control.
|
|
if !live_in(i, &desc, at)? {
|
|
return Ok(Value::Undefined);
|
|
}
|
|
// A getter read later in the description may have disabled or hidden the control or a
|
|
// container above it after the description recorded it: read the states again without
|
|
// running a getter, and drop the event unless they are all live.
|
|
if !live_now(i, &desc, &held, at)? || !still_takes(i, win, ev, None)? || !holds_value(i, win, &held, at)? {
|
|
return Ok(Value::Undefined);
|
|
}
|
|
set_slot(i, ev, "id", Value::Number(at as f64))?;
|
|
take_event(i, win, nodes, ev, &event, None)?;
|
|
}
|
|
Ok(Value::Undefined)
|
|
})();
|
|
i.temp_roots.truncate(mark);
|
|
r
|
|
}
|
|
|
|
/// Whether the element at `at` in the description `desc` ([`describe`]) is enabled and visible, and
|
|
/// every container above it too.
|
|
fn live_in(i: &Interp, desc: &Value, at: usize) -> JsResult<bool> {
|
|
let Some(d) = desc.as_object() else { return Ok(false) };
|
|
let Some(nodes) = slot(i, d, "nodes")?.as_object() else { return Ok(false) };
|
|
let nodes = list_values(i, nodes)?;
|
|
let mut n = Some(at);
|
|
for _ in 0..=nodes.len() {
|
|
let Some(k) = n else { return Ok(true) };
|
|
let Some(node) = nodes.get(k).and_then(Value::as_object) else { return Ok(false) };
|
|
if !slot(i, node, "enabled")?.truthy() || !slot(i, node, "visible")?.truthy() {
|
|
return Ok(false);
|
|
}
|
|
n = match slot(i, node, "parent")? {
|
|
Value::Number(p) if p >= 0.0 && p < k as f64 => Some(p as usize),
|
|
_ => None,
|
|
};
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
/// Whether the control at `at` in `held` (the description's controls, in its order) and every
|
|
/// container above it, by the parents in `desc`, are enabled and visible as they are now, read
|
|
/// without running a getter ([`plain_state`]).
|
|
fn live_now(i: &Interp, desc: &Value, held: &[Value], at: usize) -> JsResult<bool> {
|
|
let Some(d) = desc.as_object() else { return Ok(false) };
|
|
let Some(nodes) = slot(i, d, "nodes")?.as_object() else { return Ok(false) };
|
|
let nodes = list_values(i, nodes)?;
|
|
let mut n = Some(at);
|
|
for _ in 0..=nodes.len() {
|
|
let Some(k) = n else { return Ok(true) };
|
|
let (Some(node), Some(o)) = (nodes.get(k).and_then(Value::as_object), held.get(k).and_then(Value::as_object)) else {
|
|
return Ok(false);
|
|
};
|
|
if !plain_state(i, o, "enabled")? || !plain_state(i, o, "visible")? {
|
|
return Ok(false);
|
|
}
|
|
n = match slot(i, node, "parent")? {
|
|
Value::Number(p) if p >= 0.0 && p < k as f64 => Some(p as usize),
|
|
_ => None,
|
|
};
|
|
}
|
|
Ok(false)
|
|
}
|
|
|
|
/// The state `name` of `o`, its own or the one its prototypes give, read without running a getter;
|
|
/// `false` when a getter gives it or nothing does.
|
|
fn plain_state(i: &Interp, o: ObjId, name: &str) -> JsResult<bool> {
|
|
Ok(match i.find_ordinary_property(o, &key(name))? {
|
|
Some(Prop::Data { value, .. }) => value.truthy(),
|
|
_ => false,
|
|
})
|
|
}
|
|
|
|
/// Whether the palette `win` still holds the control at `at` in `held`.
|
|
fn holds_value(i: &mut Interp, win: ObjId, held: &[Value], at: usize) -> JsResult<bool> {
|
|
match held.get(at).and_then(Value::as_object) {
|
|
Some(c) => holds(i, win, c),
|
|
None => Ok(false),
|
|
}
|
|
}
|
|
|
|
/// Whether the window `win` still takes the event `ev`: a dialog (`keep` is `Some`) always does,
|
|
/// and a palette only while the host shows it under the id the event names and it has not been
|
|
/// hidden since it took the event. A getter or a handler run for the event may have closed or
|
|
/// hidden the palette, and shown it again.
|
|
fn still_takes(i: &mut Interp, win: ObjId, ev: ObjId, keep: Option<&[Value]>) -> JsResult<bool> {
|
|
if keep.is_some() {
|
|
return Ok(true);
|
|
}
|
|
if !slot(i, win, SHOWN)?.truthy() || number_slot(i, ev, HIDES)? != number_slot(i, win, HIDES)? {
|
|
return Ok(false);
|
|
}
|
|
let id = slot(i, ev, "palette")?;
|
|
Ok(matches!((slot(i, win, PALETTE_ID)?, id), (Value::Number(a), Value::Number(b)) if a == b))
|
|
}
|
|
|
|
fn host_fn(i: &mut Interp, name: &str) -> JsResult<Value> {
|
|
let host = classes(i)?.host;
|
|
i.get_value(&Value::Object(host), &key(name))
|
|
}
|
|
|
|
fn set_showing(i: &mut Interp, showing: Option<ObjId>) {
|
|
if let Some(c) = i.script_ui.as_mut() {
|
|
c.showing = showing;
|
|
}
|
|
}
|
|
|
|
/// `show()` on a dialog: gives its description to the host's `open`, then runs it ([`opened`]). On a
|
|
/// palette: [`show_palette`].
|
|
fn show(i: &mut Interp, this: Value, _: &[Value]) -> JsResult<Pause> {
|
|
let (win, palette) = this_window(i, &this, "show")?;
|
|
if palette {
|
|
return show_palette(i, win);
|
|
}
|
|
match classes(i)?.showing {
|
|
Some(o) if o == win => return Err(error(i, "show: this dialog is already showing")),
|
|
Some(_) => return Err(error(i, "show: a dialog cannot open while another dialog is showing")),
|
|
None => {}
|
|
}
|
|
set_showing(i, Some(win));
|
|
set_slot(i, win, CLOSING, Value::Undefined)?;
|
|
let described = describe(i, win);
|
|
let (desc, nodes, def, cancel, lists) = match described {
|
|
Ok(d) => d,
|
|
Err(e) => {
|
|
set_showing(i, None);
|
|
return Err(e);
|
|
}
|
|
};
|
|
let open = match host_fn(i, "open") {
|
|
Ok(open) => open,
|
|
Err(e) => {
|
|
set_showing(i, None);
|
|
return Err(e);
|
|
}
|
|
};
|
|
Ok(Pause {
|
|
host: open,
|
|
args: vec![desc],
|
|
keep: vec![Value::Object(win), Value::Object(nodes), Value::Number(def), Value::Number(cancel), Value::Object(lists)],
|
|
resume: opened,
|
|
failed: Some(failed),
|
|
})
|
|
}
|
|
|
|
/// A host call of `show()` failed, or a handler threw: the dialog is no longer showing. → a stop
|
|
/// (an Abort) the host's `end` gave; an error `end` throws gives way to the failure.
|
|
fn failed(i: &mut Interp, keep: &[Value]) -> JsResult<()> {
|
|
let Some(win) = arg(keep, 0).as_object() else { return Ok(()) };
|
|
if classes(i).ok().and_then(|c| c.showing) != Some(win) {
|
|
return Ok(());
|
|
}
|
|
set_showing(i, None);
|
|
if let Ok(end) = host_fn(i, "end") {
|
|
match i.call(&end, &Value::Undefined, &[Value::Number(0.0), Value::Bool(false)]) {
|
|
Ok(_) | Err(Abrupt::Throw(_)) => {}
|
|
Err(stop) => return Err(stop),
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// `show()` ends with `result`.
|
|
fn finish(i: &mut Interp, win: ObjId, result: f64) -> JsResult<Value> {
|
|
set_showing(i, None);
|
|
set_slot(i, win, CLOSING, Value::Undefined)?;
|
|
let end = host_fn(i, "end")?;
|
|
i.call(&end, &Value::Undefined, &[Value::Number(result), Value::Bool(true)])?;
|
|
Ok(Value::Number(result))
|
|
}
|
|
|
|
/// Runs `onClose`, during which a `close()` of the same palette does nothing. → false when it
|
|
/// returned `false`. A `close()` of a dialog that `onClose` calls belongs to the close it decides
|
|
/// on, so after `false` the dialog waits for the next event.
|
|
fn may_close(i: &mut Interp, win: ObjId) -> JsResult<bool> {
|
|
set_slot(i, win, IN_CLOSE, Value::Bool(true))?;
|
|
let ran = handler(i, win, "onClose");
|
|
set_slot(i, win, IN_CLOSE, Value::Undefined)?;
|
|
let closes = !matches!(ran?, Some(Value::Bool(false)));
|
|
if !closes {
|
|
set_slot(i, win, CLOSING, Value::Undefined)?;
|
|
}
|
|
Ok(closes)
|
|
}
|
|
|
|
/// Closes the dialog when a handler asked to, or asks the host's `wait` what happens next.
|
|
fn next(i: &mut Interp, win: ObjId) -> JsResult<Value> {
|
|
loop {
|
|
i.use_fuel()?;
|
|
if let Value::Number(result) = slot(i, win, CLOSING)? {
|
|
set_slot(i, win, CLOSING, Value::Undefined)?;
|
|
if may_close(i, win)? {
|
|
return finish(i, win, result);
|
|
}
|
|
continue;
|
|
}
|
|
let (desc, nodes, def, cancel, lists) = describe(i, win)?;
|
|
// A getter that `describe` ran may have closed the dialog.
|
|
if matches!(slot(i, win, CLOSING)?, Value::Number(_)) {
|
|
continue;
|
|
}
|
|
let wait = host_fn(i, "wait")?;
|
|
i.again = Some(Pause {
|
|
host: wait,
|
|
args: vec![desc],
|
|
keep: vec![Value::Object(win), Value::Object(nodes), Value::Number(def), Value::Number(cancel), Value::Object(lists)],
|
|
resume: answered,
|
|
failed: Some(failed),
|
|
});
|
|
return Ok(Value::Undefined);
|
|
}
|
|
}
|
|
|
|
/// Runs `r`; when it fails, the dialog is no longer showing.
|
|
fn guarded(i: &mut Interp, keep: &[Value], r: impl FnOnce(&mut Interp) -> JsResult<Value>) -> JsResult<Value> {
|
|
let v = r(i);
|
|
if let Err(e) = &v {
|
|
// The thrown value stays kept from collection while `failed` runs.
|
|
let thrown = match e {
|
|
Abrupt::Throw(t) => vec![t.clone()],
|
|
_ => Vec::new(),
|
|
};
|
|
let thrown_error = !thrown.is_empty();
|
|
let mark = i.temp_roots.len();
|
|
i.temp_roots.extend(thrown);
|
|
let stop = failed(i, keep);
|
|
i.temp_roots.truncate(mark);
|
|
// A stop from `end` is not masked by a thrown error the script could catch.
|
|
if thrown_error {
|
|
stop?;
|
|
}
|
|
}
|
|
v
|
|
}
|
|
|
|
fn opened(i: &mut Interp, keep: &[Value], _: Value) -> JsResult<Value> {
|
|
guarded(i, keep, |i| {
|
|
let Some(win) = arg(keep, 0).as_object() else { return Err(Abrupt::Internal("show lost its dialog".into())) };
|
|
dispatch(i, win, "show", "onShow")?;
|
|
next(i, win)
|
|
})
|
|
}
|
|
|
|
/// What the host's `wait` gave: the event, which the dialog takes, and what happens next.
|
|
fn answered(i: &mut Interp, keep: &[Value], answer: Value) -> JsResult<Value> {
|
|
guarded(i, keep, |i| {
|
|
let (Some(win), Some(nodes)) = (arg(keep, 0).as_object(), arg(keep, 1).as_object()) else {
|
|
return Err(Abrupt::Internal("show lost its dialog".into()));
|
|
};
|
|
let Some(ev) = answer.as_object() else { return Err(Abrupt::Internal("the host gave show no event".into())) };
|
|
let event = match slot(i, ev, "event")? {
|
|
Value::String(s) => s.to_rust_string(),
|
|
_ => String::new(),
|
|
};
|
|
if event == "close" {
|
|
if may_close(i, win)? {
|
|
return finish(i, win, 0.0);
|
|
}
|
|
return next(i, win);
|
|
}
|
|
take_event(i, win, nodes, ev, &event, Some(keep))?;
|
|
next(i, win)
|
|
})
|
|
}
|
|
|
|
/// A click or a change the host gave in `ev` for control `id` of `nodes` (a description's list of
|
|
/// elements): the control takes it and its handlers and listeners run ([`dispatch_for`]). A
|
|
/// dialog's button without `onClick` and without a `click` listener of its own asks to close it
|
|
/// with 1 when it is the default element and 2 when it is the cancel element, which `keep`
|
|
/// (`show()`'s kept values) names; a palette has no `keep`. No handler or listener of a palette
|
|
/// runs once a getter, a setter or an earlier handler of the event closed the palette or took the
|
|
/// control out of it.
|
|
fn take_event(i: &mut Interp, win: ObjId, nodes: ObjId, ev: ObjId, event: &str, keep: Option<&[Value]>) -> JsResult<()> {
|
|
let id = match slot(i, ev, "id")? {
|
|
Value::Number(n) if n >= 0.0 && n.fract() == 0.0 => n as usize,
|
|
_ => return Err(Abrupt::Internal("the host gave show an event without a control".into())),
|
|
};
|
|
let nodes = list_values(i, nodes)?;
|
|
let Some((c, kind)) = nodes.get(id).map(|v| kind_of(i, v)).transpose()?.flatten() else {
|
|
return Err(Abrupt::Internal("the host gave show an event for a control the dialog does not hold".into()));
|
|
};
|
|
let value = slot(i, ev, "value")?;
|
|
let taker = if keep.is_none() { Some((win, ev, c)) } else { None };
|
|
match event {
|
|
"click" => {
|
|
let Some(handled) = click_control_for(i, c, &kind, taker)? else {
|
|
return Ok(());
|
|
};
|
|
// A listener above the button that closed the dialog gives the result.
|
|
if !handled
|
|
&& kind == "button"
|
|
&& let Some(keep) = keep
|
|
&& matches!(slot(i, win, CLOSING)?, Value::Undefined)
|
|
{
|
|
let is = |k: usize| matches!(arg(keep, k), Value::Number(n) if n == id as f64);
|
|
if is(2) {
|
|
set_slot(i, win, CLOSING, Value::Number(1.0))?;
|
|
} else if is(3) {
|
|
set_slot(i, win, CLOSING, Value::Number(2.0))?;
|
|
}
|
|
}
|
|
}
|
|
"change" => {
|
|
match kind.as_str() {
|
|
"edittext" => {
|
|
i.put(c, key("text"), value, true)?;
|
|
if dispatch_for(i, c, "changing", "onChanging", taker)?.is_none() {
|
|
return Ok(());
|
|
}
|
|
}
|
|
"slider" => {
|
|
i.put(c, key("value"), value, true)?;
|
|
if dispatch_for(i, c, "changing", "onChanging", taker)?.is_none() {
|
|
return Ok(());
|
|
}
|
|
}
|
|
_ => {
|
|
let n = match value {
|
|
Value::Number(n) => n,
|
|
_ => -1.0,
|
|
};
|
|
match keep {
|
|
// The index is into the items the person saw.
|
|
Some(keep) => {
|
|
let shown = match arg(keep, 4).as_object() {
|
|
Some(lists) => list_values(i, lists)?.get(id).cloned().unwrap_or(Value::Undefined),
|
|
None => Value::Undefined,
|
|
};
|
|
if !same(&slot(i, c, ITEMS)?, &shown) {
|
|
return Err(error(i, "show: a list's items changed before the person's choice was taken"));
|
|
}
|
|
}
|
|
// A palette's list may have changed while the event waited: the host gives
|
|
// the items the person saw and the list's revision then.
|
|
// The revision is read after the texts, whose getters may change the list.
|
|
None => {
|
|
let same = same_items(i, c, &slot(i, ev, "items")?)?;
|
|
let revised = match slot(i, ev, "revision")? {
|
|
Value::Number(r) => r != number_slot(i, c, REVISION)?,
|
|
_ => false,
|
|
};
|
|
// An item's text getter may have closed the palette, and its `onClose`
|
|
// may have changed the list: the event is then dropped, not refused.
|
|
if !still_takes(i, win, ev, keep)? {
|
|
return Ok(());
|
|
}
|
|
// A getter may have removed the list, which changes neither its
|
|
// revision nor the palette.
|
|
if !holds(i, win, c)? {
|
|
return Err(error(i, "a palette event names a control the palette no longer holds"));
|
|
}
|
|
// An item's text getter may have disabled or hidden the list, or a
|
|
// group above it: the event is then dropped, as for a click.
|
|
if !live_up(i, win, c)? {
|
|
return Ok(());
|
|
}
|
|
if revised || !same || (n >= 0.0 && n as usize >= items_of(i, c)?.len()) {
|
|
return Err(error(i, "a palette's list changed before the person's choice was taken"));
|
|
}
|
|
}
|
|
}
|
|
set_slot(i, c, SELECTION, Value::Number(n))?;
|
|
}
|
|
}
|
|
// `onChanging` may have closed the palette.
|
|
dispatch_for(i, c, "change", "onChange", taker)?;
|
|
}
|
|
_ => return Err(Abrupt::Internal(format!("the host gave show the event {event}"))),
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Does the list `c` hold items with the texts in `seen` (an array of strings), in that order? True
|
|
/// when `seen` is not an array. A text a later item's getter changes after it was compared counts
|
|
/// as a change, and so does a text that is not a plain value of the item's own.
|
|
fn same_items(i: &mut Interp, c: ObjId, seen: &Value) -> JsResult<bool> {
|
|
let Some(seen) = seen.as_object() else { return Ok(true) };
|
|
let seen = list_values(i, seen)?;
|
|
let items = items_of(i, c)?;
|
|
if seen.len() != items.len() {
|
|
return Ok(false);
|
|
}
|
|
i.rooted(&items, |i| {
|
|
let mut texts = Vec::with_capacity(items.len());
|
|
for (item, want) in items.iter().zip(&seen) {
|
|
let Some(item) = item.as_object() else { return Ok(false) };
|
|
let text = get(i, item, "text")?;
|
|
let text = i.to_str(&text)?.to_rust_string();
|
|
if text != i.to_str(want)?.to_rust_string() {
|
|
return Ok(false);
|
|
}
|
|
texts.push((item, text));
|
|
}
|
|
// Read again without running getters. Only a text held as a plain value can be checked so:
|
|
// one a getter, an inherited property or an object gives cannot, and counts as a change.
|
|
for (item, text) in texts {
|
|
let now = match i.get_own_property(item, &key("text"))? {
|
|
Some(Prop::Data { value, .. }) if !matches!(value, Value::Object(_)) => value,
|
|
_ => return Ok(false),
|
|
};
|
|
if i.to_str(&now)?.to_rust_string() != text {
|
|
return Ok(false);
|
|
}
|
|
}
|
|
Ok(true)
|
|
})
|
|
}
|
|
|
|
fn text_of(i: &mut Interp, v: &Value, what: &str) -> JsResult<String> {
|
|
let s = i.to_str(v)?;
|
|
let text = if s.len() > MAX_TEXT_BYTES { None } else { Some(s.to_rust_string()) };
|
|
match text {
|
|
Some(t) if t.len() <= MAX_TEXT_BYTES => Ok(t),
|
|
_ => Err(range(i, &format!("show: {what} is longer than 16 KiB"))),
|
|
}
|
|
}
|
|
|
|
fn get(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Value> {
|
|
i.get_value(&Value::Object(o), &key(name))
|
|
}
|
|
|
|
/// [`get`], keeping the value in the roots [`describe`] lets go of when it returns.
|
|
fn get_kept(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Value> {
|
|
let v = get(i, o, name)?;
|
|
i.temp_roots.push(v.clone());
|
|
Ok(v)
|
|
}
|
|
|
|
fn number_of(i: &mut Interp, o: ObjId, name: &str) -> JsResult<Data> {
|
|
let v = get(i, o, name)?;
|
|
Ok(Data::Number(number_arg(i, &v, &format!("show: a control's {name}"))?))
|
|
}
|
|
|
|
/// An alignment: a name, or an array of a horizontal and a vertical name; `null` when not set.
|
|
fn alignment(i: &mut Interp, v: &Value) -> JsResult<Data> {
|
|
match v {
|
|
Value::Undefined | Value::Null => Ok(Data::Null),
|
|
Value::Object(a) if matches!(i.object(*a)?.class, Class::Array { .. }) => {
|
|
let mut out = Vec::new();
|
|
for n in 0..2 {
|
|
let part = i.get_value(v, &index_key(n))?;
|
|
out.push(Data::String(text_of(i, &part, "an alignment")?.to_lowercase()));
|
|
}
|
|
Ok(Data::Array(out))
|
|
}
|
|
v => Ok(Data::String(text_of(i, v, "an alignment")?.to_lowercase())),
|
|
}
|
|
}
|
|
|
|
/// Numbers read from `v`'s indices, or else from its properties `names`.
|
|
fn numbers(i: &mut Interp, v: &Value, names: &[&str], what: &str) -> JsResult<Data> {
|
|
let Some(o) = v.as_object() else {
|
|
if matches!(v, Value::Undefined | Value::Null) {
|
|
return Ok(Data::Null);
|
|
}
|
|
return Err(i.type_error(&format!("show: give {what} as an array or an object")));
|
|
};
|
|
let array = matches!(i.object(o)?.class, Class::Array { .. });
|
|
let mut out = Vec::new();
|
|
for (n, name) in names.iter().enumerate() {
|
|
let part = if array { i.get_value(v, &index_key(n as u32))? } else { i.get_value(v, &key(name))? };
|
|
out.push(Data::Number(number_arg(i, &part, &format!("show: {what}"))?));
|
|
}
|
|
Ok(Data::Array(out))
|
|
}
|
|
|
|
/// Bounds as `[left, top, right, bottom]`: from an array, from `x`, `y`, `width` and `height`, or
|
|
/// from `left`, `top`, `right` and `bottom`.
|
|
fn bounds(i: &mut Interp, v: &Value) -> JsResult<Data> {
|
|
if let Some(o) = v.as_object()
|
|
&& !matches!(i.object(o)?.class, Class::Array { .. })
|
|
&& !matches!(i.get_value(v, &key("width"))?, Value::Undefined)
|
|
{
|
|
let Data::Array(xywh) = numbers(i, v, &["x", "y", "width", "height"], "bounds")? else { return Ok(Data::Null) };
|
|
let n = |k: usize| match xywh.get(k) {
|
|
Some(Data::Number(n)) => *n,
|
|
_ => 0.0,
|
|
};
|
|
// Two finite numbers can add up to infinity; the sum is kept finite.
|
|
let end = |a: f64, b: f64| (a + b).clamp(-f64::MAX, f64::MAX);
|
|
return Ok(Data::Array(vec![Data::Number(n(0)), Data::Number(n(1)), Data::Number(end(n(0), n(2))), Data::Number(end(n(1), n(3)))]));
|
|
}
|
|
numbers(i, v, &["left", "top", "right", "bottom"], "bounds")
|
|
}
|
|
|
|
/// `base`'s width and height, each replaced by `over`'s when that is above 0.
|
|
fn overridden(base: Data, over: Data) -> Data {
|
|
let Data::Array(over) = over else { return base };
|
|
let mut out = match base {
|
|
Data::Array(b) => b,
|
|
_ => vec![Data::Number(-1.0), Data::Number(-1.0)],
|
|
};
|
|
for (o, v) in out.iter_mut().zip(over) {
|
|
if matches!(v, Data::Number(n) if n > 0.0) {
|
|
*o = v;
|
|
}
|
|
}
|
|
Data::Array(out)
|
|
}
|
|
|
|
fn margins(i: &mut Interp, v: &Value) -> JsResult<Data> {
|
|
if v.as_object().is_some() {
|
|
return numbers(i, v, &["left", "top", "right", "bottom"], "margins");
|
|
}
|
|
let m = number_arg(i, v, "show: margins")?;
|
|
Ok(Data::Array(vec![Data::Number(m); 4]))
|
|
}
|
|
|
|
/// The dialog's description: its elements in order, each after the container that holds it, as
|
|
/// [`Data`] for the host's `open` and `wait`, with the elements in that order, and the indices of
|
|
/// its default and cancel elements (-1 for none), and each element's array of list items as it
|
|
/// was described (`undefined` for an element that is not a list). When a getter changes a list or
|
|
/// a list item's text, or adds or removes a control, after it was read, the dialog is read again
|
|
/// once; a dialog that changes again throws an `Error`.
|
|
fn describe(i: &mut Interp, win: ObjId) -> JsResult<Described> {
|
|
for _ in 0..2 {
|
|
let mark = i.temp_roots.len();
|
|
let r = describe_rooted(i, win);
|
|
i.temp_roots.truncate(mark);
|
|
if let Some(d) = r? {
|
|
return Ok(d);
|
|
}
|
|
}
|
|
Err(error(i, "show: a list's items or a container's controls changed while the dialog was read"))
|
|
}
|
|
|
|
/// A description, the elements in its order, the default and cancel elements, and the lists' items.
|
|
type Described = (Value, ObjId, f64, f64, ObjId);
|
|
|
|
/// Has each list in `order` the array of items and the selection it had when it was described?
|
|
fn lists_unchanged(i: &Interp, order: &[(ObjId, String, f64)], snapshot: &[Value], selections: &[f64]) -> JsResult<bool> {
|
|
for (((o, ..), kept), sel) in order.iter().zip(snapshot).zip(selections) {
|
|
if !matches!(kept, Value::Undefined) && (!same(&slot(i, *o, ITEMS)?, kept) || number_slot(i, *o, SELECTION)? != *sel) {
|
|
return Ok(false);
|
|
}
|
|
}
|
|
Ok(true)
|
|
}
|
|
|
|
/// Has each list item in `texts` the text it had when it was read?
|
|
fn texts_unchanged(i: &Interp, texts: &[(ObjId, Value)]) -> JsResult<bool> {
|
|
for (item, read) in texts {
|
|
match i.ordinary_own_property(*item, &key("text"))? {
|
|
Some(Prop::Data { value, .. }) if crate::interp::same_value(&value, read) => {}
|
|
_ => return Ok(false),
|
|
}
|
|
}
|
|
Ok(true)
|
|
}
|
|
|
|
/// Has each element in `order` the array of controls it had when the dialog was read?
|
|
fn tree_unchanged(i: &Interp, order: &[(ObjId, String, f64)], tree: &[Value]) -> JsResult<bool> {
|
|
for ((o, ..), kept) in order.iter().zip(tree) {
|
|
let now = slot(i, *o, KIDS)?;
|
|
if !(same(&now, kept) || matches!((&now, kept), (Value::Undefined, Value::Undefined))) {
|
|
return Ok(false);
|
|
}
|
|
}
|
|
Ok(true)
|
|
}
|
|
|
|
fn describe_rooted(i: &mut Interp, win: ObjId) -> JsResult<Option<Described>> {
|
|
let mut order: Vec<(ObjId, String, f64)> = Vec::new();
|
|
let mut tree = Vec::new();
|
|
let mut seen = std::collections::HashSet::new();
|
|
let mut stack: Vec<(Value, f64, usize)> = vec![(Value::Object(win), -1.0, 0)];
|
|
while let Some((v, parent, depth)) = stack.pop() {
|
|
i.use_fuel()?;
|
|
let Some((o, kind)) = kind_of(i, &v)? else { return Err(i.type_error("show: the dialog holds something that is not a control")) };
|
|
if !seen.insert(o) {
|
|
return Err(i.type_error("show: a control appears twice in the dialog"));
|
|
}
|
|
if order.len() > MAX_CONTROLS {
|
|
return Err(range(i, &format!("show: a dialog can hold at most {MAX_CONTROLS} controls")));
|
|
}
|
|
if depth > MAX_DEPTH {
|
|
return Err(range(i, &format!("show: controls can be nested at most {MAX_DEPTH} deep")));
|
|
}
|
|
let index = order.len() as f64;
|
|
order.push((o, kind, parent));
|
|
// Each container's array of controls as it is read; a change to it means a later getter
|
|
// added or removed a control.
|
|
let held = slot(i, o, KIDS)?;
|
|
i.temp_roots.push(held.clone());
|
|
tree.push(held);
|
|
for kid in kids_of(i, o)?.into_iter().rev() {
|
|
stack.push((kid, index, depth + 1));
|
|
}
|
|
}
|
|
let in_palette = order.first().is_some_and(|(_, kind, _)| kind == "palette");
|
|
let objects: Vec<Value> = order.iter().map(|(o, ..)| Value::Object(*o)).collect();
|
|
i.temp_roots.extend(objects.iter().cloned());
|
|
let mut nodes = Vec::with_capacity(order.len());
|
|
let mut names = Vec::with_capacity(order.len());
|
|
let mut items_total = 0usize;
|
|
// Each list's array of items as it is read; a change to it means a later getter changed the list.
|
|
let mut snapshot = Vec::with_capacity(order.len());
|
|
// Each list's selection as it is read; a later getter may change it.
|
|
let mut selections = Vec::with_capacity(order.len());
|
|
// Whether each element, in `order`, is enabled and visible with the containers above it.
|
|
let mut live: Vec<(bool, bool)> = Vec::with_capacity(order.len());
|
|
// Each list item's own text as it was read.
|
|
let mut item_texts: Vec<(ObjId, Value)> = Vec::new();
|
|
for (o, kind, parent) in &order {
|
|
let o = *o;
|
|
let mut listed = Value::Undefined;
|
|
let mut selected = -1.0;
|
|
let name_v = get(i, o, "name")?;
|
|
let name = text_of(i, &name_v, "a control's name")?;
|
|
let text_v = get(i, o, "text")?;
|
|
let text = text_of(i, &text_v, "a control's text")?;
|
|
// A palette is described as a dialog is; the host knows which it shows.
|
|
let shown_kind = if kind == "palette" { "dialog" } else { kind.as_str() };
|
|
let mut d: Vec<(String, Data)> = vec![
|
|
("kind".into(), Data::String(shown_kind.to_string())),
|
|
("id".into(), Data::Number(number_slot(i, o, ID)?)),
|
|
("parent".into(), Data::Number(*parent)),
|
|
("name".into(), Data::String(name.clone())),
|
|
("text".into(), Data::String(text.clone())),
|
|
];
|
|
// In a palette, the states are read without running a getter, and a state that a getter
|
|
// gives counts as off, as when an event for the control is delivered ([`plain_state`]).
|
|
let (enabled, visible) = if in_palette {
|
|
(plain_state(i, o, "enabled")?, plain_state(i, o, "visible")?)
|
|
} else {
|
|
(get(i, o, "enabled")?.truthy(), get(i, o, "visible")?.truthy())
|
|
};
|
|
// A control is enabled and visible only while every container above it is.
|
|
let (above_enabled, above_visible) = if *parent >= 0.0 { live.get(*parent as usize).copied().unwrap_or((true, true)) } else { (true, true) };
|
|
live.push((enabled && above_enabled, visible && above_visible));
|
|
// Enter and Escape click a button found by its name or text only when it can be clicked.
|
|
let clickable = kind == "button" && enabled && visible && above_enabled && above_visible;
|
|
names.push((clickable, name, text));
|
|
d.push(("enabled".into(), Data::Bool(enabled)));
|
|
d.push(("visible".into(), Data::Bool(visible)));
|
|
// A value a getter gives is kept from collection while its own properties are read.
|
|
let b = get_kept(i, o, "bounds")?;
|
|
d.push(("bounds".into(), bounds(i, &b)?));
|
|
// `size` set to a positive width or height takes the place of `preferredSize`'s.
|
|
let preferred = get_kept(i, o, "preferredSize")?;
|
|
let preferred = numbers(i, &preferred, &["width", "height"], "preferredSize")?;
|
|
let set = get_kept(i, o, "size")?;
|
|
let set = numbers(i, &set, &["width", "height"], "size")?;
|
|
d.push(("size".into(), overridden(preferred, set)));
|
|
for (name, field) in [("minimumSize", "min"), ("maximumSize", "max")] {
|
|
let v = get_kept(i, o, name)?;
|
|
d.push((field.into(), numbers(i, &v, &["width", "height"], name)?));
|
|
}
|
|
let a = get_kept(i, o, "alignment")?;
|
|
d.push(("alignment".into(), alignment(i, &a)?));
|
|
// The characters a text field or static text reserves room for in its width.
|
|
if matches!(kind.as_str(), "edittext" | "statictext") {
|
|
let n = get(i, o, "characters")?;
|
|
if !matches!(n, Value::Undefined | Value::Null) {
|
|
d.push(("characters".into(), Data::Number(number_arg(i, &n, "show: a control's characters")?)));
|
|
}
|
|
}
|
|
if is_container(kind) {
|
|
let orientation = get(i, o, "orientation")?;
|
|
d.push(("orientation".into(), Data::String(text_of(i, &orientation, "an orientation")?.to_lowercase())));
|
|
let ac = get_kept(i, o, "alignChildren")?;
|
|
d.push(("alignChildren".into(), alignment(i, &ac)?));
|
|
d.push(("spacing".into(), number_of(i, o, "spacing")?));
|
|
let m = get_kept(i, o, "margins")?;
|
|
d.push(("margins".into(), margins(i, &m)?));
|
|
}
|
|
match kind.as_str() {
|
|
"checkbox" | "radiobutton" => {
|
|
let on = get(i, o, "value")?.truthy();
|
|
d.push(("value".into(), Data::Bool(on)));
|
|
}
|
|
"slider" | "progressbar" => {
|
|
d.push(("value".into(), number_of(i, o, "value")?));
|
|
d.push(("minvalue".into(), number_of(i, o, "minvalue")?));
|
|
d.push(("maxvalue".into(), number_of(i, o, "maxvalue")?));
|
|
}
|
|
"edittext" => {
|
|
let props = get_kept(i, o, "properties")?;
|
|
let (multiline, readonly) = match props {
|
|
Value::Object(_) => (i.get_value(&props, &key("multiline"))?.truthy(), i.get_value(&props, &key("readonly"))?.truthy()),
|
|
_ => (false, false),
|
|
};
|
|
// The dialog cannot hide what is typed, so a field meant to hide it is refused before it shows.
|
|
if props.as_object().is_some() && i.get_value(&props, &key("noecho"))?.truthy() {
|
|
return Err(error(i, "show: an edit field that hides its text (noecho) is not available yet"));
|
|
}
|
|
d.push(("multiline".into(), Data::Bool(multiline)));
|
|
d.push(("readonly".into(), Data::Bool(readonly)));
|
|
}
|
|
"dropdownlist" | "listbox" => {
|
|
let kept = slot(i, o, ITEMS)?;
|
|
i.temp_roots.push(kept.clone());
|
|
listed = kept;
|
|
let items = items_of(i, o)?;
|
|
items_total = items_total.saturating_add(items.len());
|
|
if items_total > MAX_ITEMS {
|
|
return Err(range(i, &format!("show: a dialog's lists hold at most {MAX_ITEMS} items in all")));
|
|
}
|
|
let mut texts = Vec::with_capacity(items.len());
|
|
for item in &items {
|
|
let t = i.get_value(item, &key("text"))?;
|
|
// The text as the item holds it; a later getter that changes it changes the list.
|
|
if let Some(at) = item.as_object()
|
|
&& let Some(Prop::Data { value, .. }) = i.get_own_property(at, &key("text"))?
|
|
{
|
|
i.temp_roots.push(value.clone());
|
|
item_texts.push((at, value));
|
|
}
|
|
texts.push(Data::String(text_of(i, &t, "a list item's text")?));
|
|
}
|
|
d.push(("items".into(), Data::Array(texts)));
|
|
d.push(("itemsRevision".into(), Data::Number(number_slot(i, o, ITEMS_REV)?)));
|
|
// The item texts' getters ran above, so the selection is read after them.
|
|
selected = number_slot(i, o, SELECTION)?;
|
|
let sel = selection_index(i, o)?.filter(|n| *n < items.len());
|
|
d.push(("selection".into(), sel.map_or(Data::Null, |n| Data::Number(n as f64))));
|
|
d.push(("revision".into(), Data::Number(number_slot(i, o, REVISION)?)));
|
|
}
|
|
_ => {}
|
|
}
|
|
nodes.push(Data::Object(d));
|
|
snapshot.push(listed);
|
|
selections.push(selected);
|
|
}
|
|
// The default and cancel elements are read before the states below, since their getters may
|
|
// also change a control's state.
|
|
let given_def = get(i, win, "defaultElement")?;
|
|
i.temp_roots.push(given_def.clone());
|
|
let given_cancel = get(i, win, "cancelElement")?;
|
|
i.temp_roots.push(given_cancel.clone());
|
|
// A palette's states are read again once every getter above has run, since one may have
|
|
// changed a control's state after it was read.
|
|
if in_palette {
|
|
let mut live: Vec<(bool, bool)> = Vec::with_capacity(order.len());
|
|
for (n, (o, kind, parent)) in order.iter().enumerate() {
|
|
let (enabled, visible) = (plain_state(i, *o, "enabled")?, plain_state(i, *o, "visible")?);
|
|
let (above_enabled, above_visible) =
|
|
if *parent >= 0.0 { live.get(*parent as usize).copied().unwrap_or((true, true)) } else { (true, true) };
|
|
live.push((enabled && above_enabled, visible && above_visible));
|
|
if let Some(entry) = names.get_mut(n) {
|
|
entry.0 = kind == "button" && enabled && visible && above_enabled && above_visible;
|
|
}
|
|
if let Some(Data::Object(d)) = nodes.get_mut(n) {
|
|
for (field, value) in d.iter_mut() {
|
|
match field.as_str() {
|
|
"enabled" => *value = Data::Bool(enabled),
|
|
"visible" => *value = Data::Bool(visible),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let target = |given: &Value, word: &str| -> f64 {
|
|
if let Some(n) = objects.iter().position(|o| same(o, given)) {
|
|
return n as f64;
|
|
}
|
|
if !matches!(given, Value::Undefined | Value::Null) {
|
|
return -1.0;
|
|
}
|
|
let button = |by_name: bool| names.iter().position(|(c, n, t)| *c && (if by_name { n } else { t }).to_lowercase() == word);
|
|
button(true).or_else(|| button(false)).map_or(-1.0, |n| n as f64)
|
|
};
|
|
let def = target(&given_def, "ok");
|
|
let cancel = target(&given_cancel, "cancel");
|
|
if !lists_unchanged(i, &order, &snapshot, &selections)? || !tree_unchanged(i, &order, &tree)? || !texts_unchanged(i, &item_texts)? {
|
|
return Ok(None);
|
|
}
|
|
let desc = Data::Object(vec![
|
|
("nodes".into(), Data::Array(nodes)),
|
|
("defaultElement".into(), Data::Number(def)),
|
|
("cancelElement".into(), Data::Number(cancel)),
|
|
]);
|
|
let desc = i.write_data(desc, 0, true)?;
|
|
i.temp_roots.push(desc.clone());
|
|
let kept = list(i, &objects, false)?;
|
|
i.temp_roots.push(Value::Object(kept));
|
|
let lists = list(i, &snapshot, false)?;
|
|
Ok(Some((desc, kept, def, cancel, lists)))
|
|
}
|
|
|
|
/// Adds `Window`, its prototype's methods, and the host functions in `items`.
|
|
pub(crate) fn install(i: &mut Interp, items: Vec<(String, HostItem)>) -> JsResult<()> {
|
|
let host = i.host_object("Window", items)?;
|
|
let object_proto = i.intrinsics.object_proto;
|
|
i.rooted(&[Value::Object(host)], |i| {
|
|
i.reserve_heap(OBJECT_BYTES.saturating_mul(5))?;
|
|
let window_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
|
|
i.temp_roots.push(Value::Object(window_proto));
|
|
let control_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
|
|
i.temp_roots.push(Value::Object(control_proto));
|
|
let item_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
|
|
i.temp_roots.push(Value::Object(item_proto));
|
|
let layout = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
|
|
i.temp_roots.push(Value::Object(layout));
|
|
let selection_get = native(i, "selection", selection_get, 0, NativeKind::Plain, false)?;
|
|
i.temp_roots.push(Value::Object(selection_get));
|
|
let selection_set = native(i, "selection", selection_set, 1, NativeKind::Plain, false)?;
|
|
i.temp_roots.push(Value::Object(selection_set));
|
|
let event_proto = i.heap.alloc_object(Object::new(Class::Ordinary, Some(object_proto)));
|
|
i.temp_roots.push(Value::Object(event_proto));
|
|
let radio_get = native(i, "value", radio_get, 0, NativeKind::Plain, false)?;
|
|
i.temp_roots.push(Value::Object(radio_get));
|
|
let radio_set = native(i, "value", radio_set, 1, NativeKind::Plain, false)?;
|
|
i.temp_roots.push(Value::Object(radio_set));
|
|
i.script_ui = Some(Classes {
|
|
window_proto,
|
|
control_proto,
|
|
item_proto,
|
|
event_proto,
|
|
layout,
|
|
host,
|
|
selection_get,
|
|
selection_set,
|
|
radio_get,
|
|
radio_set,
|
|
showing: None,
|
|
palettes: Vec::new(),
|
|
taking: false,
|
|
dispatching: Vec::new(),
|
|
above: Vec::new(),
|
|
next_id: 0.0,
|
|
});
|
|
let window = native(i, "Window", window_ctor, 4, NativeKind::Plain, true)?;
|
|
constructor(i, "Window", window, window_proto)?;
|
|
let event = native(i, "__vcPaletteEvent", palette_event, 0, NativeKind::Plain, false)?;
|
|
i.define(window, key("__vcPaletteEvent"), Prop::Data { value: Value::Object(event), flags: 0 })?;
|
|
// The run of a palette's handler calls the global one through `this`, under a name that is
|
|
// not an identifier: a script can neither replace nor delete it nor declare the name, so
|
|
// the palettes still take events after a script assigns to `Window`.
|
|
let global = i.global;
|
|
i.define(global, key(PALETTE_EVENT_GLOBAL), Prop::Data { value: Value::Object(event), flags: 0 })?;
|
|
// `Window.alert`, `Window.confirm` and `Window.prompt` are the host's global functions.
|
|
for name in ["alert", "confirm", "prompt"] {
|
|
if let Some(Prop::Data { value, .. }) = i.get_own_property(global, &key(name))?
|
|
&& i.is_callable(&value)
|
|
{
|
|
data(i, window, name, value, HIDDEN)?;
|
|
}
|
|
}
|
|
for proto in [window_proto, control_proto] {
|
|
method(i, proto, "add", add, 4)?;
|
|
method(i, proto, "findElement", find_element, 1)?;
|
|
method(i, proto, "remove", remove, 1)?;
|
|
method(i, proto, "addEventListener", add_event_listener, 3)?;
|
|
method(i, proto, "removeEventListener", remove_event_listener, 3)?;
|
|
}
|
|
method(i, window_proto, "close", close, 1)?;
|
|
method(i, window_proto, "hide", hide, 0)?;
|
|
method(i, window_proto, "center", nothing, 1)?;
|
|
method(i, window_proto, "update", update, 0)?;
|
|
pausable_method(i, window_proto, "show", show, 0)?;
|
|
method(i, control_proto, "removeAll", remove_all, 0)?;
|
|
method(i, control_proto, "find", find, 1)?;
|
|
method(i, control_proto, "notify", notify, 1)?;
|
|
method(i, control_proto, "show", control_show, 0)?;
|
|
method(i, control_proto, "hide", control_hide, 0)?;
|
|
method(i, control_proto, "update", control_update, 0)?;
|
|
method(i, event_proto, "stopPropagation", event_stop, 0)?;
|
|
method(i, event_proto, "preventDefault", nothing, 0)?;
|
|
method(i, item_proto, "toString", item_to_string, 0)?;
|
|
let index = native(i, "index", item_index, 0, NativeKind::Plain, false)?;
|
|
i.define(item_proto, key("index"), Prop::Accessor { get: Some(index), set: None, flags: 0 })?;
|
|
let get = native(i, "selected", item_selected_get, 0, NativeKind::Plain, false)?;
|
|
let set = native(i, "selected", item_selected_set, 1, NativeKind::Plain, false)?;
|
|
i.define(item_proto, key("selected"), Prop::Accessor { get: Some(get), set: Some(set), flags: 0 })?;
|
|
method(i, layout, "layout", nothing, 1)?;
|
|
method(i, layout, "resize", nothing, 0)?;
|
|
i.object_mut(layout)?.extensible = false;
|
|
Ok(())
|
|
})
|
|
}
|