Scripts: app.showColorPicker asks for a color

app.showColorPicker(color) opens the Color Picker on an RGBColor,
CMYKColor, GrayColor or LabColor and returns the color picked for OK.
Cancel, and OK with the color unchanged, return a new object of the
color the script gave. Anything else throws a TypeError.

- In the app the run waits at the picker, which previews nothing on
  the selection. Stop and quitting end the run as at the other script
  dialogs and close the picker.
- A run with nobody to ask takes {kind: "colorPicker", value: color |
  null} from answers. The report lists the picker with the color it
  opened on and the answer.
- script.run's params text, docs/mcp.md and the Scripts line in
  docs/target-app-parity.md describe it.
This commit is contained in:
dotCooCoo committed 2026-11-25 02:23:19 -08:00
1 parent aedd5ce1cb
commit 58292d605a
11 files changed
+425 -53

No files matched your search

+93 -19
View File
@@ -5,12 +5,14 @@
//! it is left out or `undefined` ([`DEFAULT_TITLE`]), and `errorIcon` and `noAsDflt` as the
//! boolean `Boolean(value)` gives. `alert` returns `undefined`, `confirm` `true` for Yes and
//! `false` for No, and `prompt` the text in its field for OK and `null` for Cancel.
//! `app.showColorPicker(color)` ([`show_color_picker`]) opens the Color Picker on a color and
//! returns the color picked for OK, or a new object of the color it was given for Cancel.
//!
//! In the app the run waits at the call for the person's answer, and spends no fuel while it
//! waits; every dialog also has Stop, which ends the run and puts everything back. A run with
//! nobody to ask takes its answers, in order, from `script.run`'s `answers`; a call with no
//! answer left, or with the next answer for another kind of dialog, ends the run. The report's
//! `dialogs` lists each dialog and its answer.
//! waits; every dialog but the Color Picker also has Stop, which ends the run and puts
//! everything back. A run with nobody to ask takes its answers, in order, from `script.run`'s
//! `answers`; a call with no answer left, or with the next answer for another kind of dialog,
//! ends the run. The report's `dialogs` lists each dialog and its answer.
//!
//! The values here are VectorCraft's own limits: [`MAX_MESSAGE_BYTES`], [`MAX_TITLE_BYTES`],
//! [`MAX_PROMPT_BYTES`], [`MAX_ANSWERS`], [`MAX_ANSWERS_BYTES`], [`MAX_RECORDS`] and
@@ -21,6 +23,7 @@ use std::collections::VecDeque;
use std::rc::Rc;
use serde_json::{Map, Value, json};
use vectorcraft_color::Color;
use vectorcraft_js::{Arg, Data, HostError, HostFunction, Realm, RunError};
use super::{Host, console};
@@ -48,21 +51,33 @@ pub enum DialogKind {
Alert,
Confirm,
Prompt,
/// `app.showColorPicker`.
ColorPicker,
}
impl DialogKind {
/// The function's name, which is also the `kind` of an answer and of a report's dialog.
/// The `kind` of an answer and of a report's dialog, which is also the function's name for
/// `alert`, `confirm` and `prompt`.
pub fn id(self) -> &'static str {
match self {
Self::Alert => "alert",
Self::Confirm => "confirm",
Self::Prompt => "prompt",
Self::ColorPicker => "colorPicker",
}
}
/// The function's name, as its errors give it.
fn function(self) -> &'static str {
match self {
Self::ColorPicker => "showColorPicker",
k => k.id(),
}
}
}
/// A dialog a script opened, which waits for its answer.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub struct ScriptDialog {
pub kind: DialogKind,
pub message: String,
@@ -74,10 +89,12 @@ pub struct ScriptDialog {
pub no_as_default: bool,
/// `prompt`: the text its field starts with.
pub preset: String,
/// `showColorPicker`: the color the picker starts with. `None` for the other dialogs.
pub color: Option<Color>,
}
/// The answer to a [`ScriptDialog`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub enum DialogAnswer {
/// OK, for `alert`.
Ok,
@@ -85,6 +102,8 @@ pub enum DialogAnswer {
Confirm(bool),
/// OK with the field's text, or Cancel (`None`), for `prompt`.
Prompt(Option<String>),
/// OK with the color picked, or Cancel (`None`), for `showColorPicker`.
ColorPicker(Option<Color>),
}
impl DialogAnswer {
@@ -93,29 +112,51 @@ impl DialogAnswer {
Self::Ok => DialogKind::Alert,
Self::Confirm(_) => DialogKind::Confirm,
Self::Prompt(_) => DialogKind::Prompt,
Self::ColorPicker(_) => DialogKind::ColorPicker,
}
}
/// What the function returns.
fn data(&self) -> Data {
/// What the function returns for dialog `d`: for Cancel in the Color Picker, the color it
/// started with.
fn data(&self, d: &ScriptDialog) -> Data {
match self {
Self::Ok => Data::Undefined,
Self::Confirm(b) => Data::Bool(*b),
Self::Prompt(Some(t)) => Data::String(t.clone()),
Self::Prompt(None) => Data::Null,
Self::ColorPicker(picked) => picked.or(d.color).as_ref().map_or(Data::Null, super::dom::color_data),
}
}
/// The answer as a report gives it: `"ok"`, a boolean, a string or `null`.
/// The answer as a report gives it: `"ok"`, a boolean, a string, a color as the paint commands
/// take it, or `null`.
fn json(&self) -> Value {
match self {
Self::Ok => json!("ok"),
Self::Confirm(b) => json!(b),
Self::Prompt(t) => json!(t),
Self::ColorPicker(c) => c.as_ref().map_or(Value::Null, color_json),
}
}
}
/// `c` as the paint commands take it.
fn color_json(c: &Color) -> Value {
serde_json::to_value(c).unwrap_or(Value::Null)
}
/// Are `c`'s components in their ranges: 0 to 1, and for Lab, L from 0 to 100 and a and b from
/// -128 to 128, as a script's `LabColor` takes them?
fn in_range(c: &Color) -> bool {
let unit = |v: &[f32]| v.iter().all(|x| (0.0..=1.0).contains(x));
match *c {
Color::Rgb { r, g, b } => unit(&[r, g, b]),
Color::Cmyk { c, m, y, k } => unit(&[c, m, y, k]),
Color::Gray { k } => unit(&[k]),
Color::Lab { l, a, b } => (0.0..=100.0).contains(&l) && [a, b].iter().all(|x| (-128.0..=128.0).contains(x)),
}
}
/// `text` as a dialog shows it. With `lines`, a line feed, a carriage return or the two together
/// is a line break; every other control character, and with `lines` false every line break too,
/// shows as its Unicode Control Picture, as the Script Console shows it.
@@ -148,7 +189,8 @@ pub fn cut_to(text: &str, max: usize) -> &str {
}
/// `script.run`'s `answers`: a list of `{kind: "alert", value: "ok"}`, `{kind: "confirm", value:
/// true | false}` and `{kind: "prompt", value: text | null}`. An empty object, which the parameter
/// true | false}`, `{kind: "prompt", value: text | null}` and `{kind: "colorPicker", value: color |
/// null}`, the color in any form the paint commands take. An empty object, which the parameter
/// took before dialogs existed, is an empty list.
pub(crate) fn parse_answers(v: Option<&Value>) -> Result<Vec<DialogAnswer>, String> {
let list = match v {
@@ -180,7 +222,12 @@ fn answer(a: &Value) -> Result<DialogAnswer, &'static str> {
(Some("prompt"), Value::String(s)) if s.len() <= MAX_PROMPT_BYTES => Ok(DialogAnswer::Prompt(Some(s.clone()))),
(Some("prompt"), Value::String(_)) => Err("is longer than 16 KiB"),
(Some("prompt"), _) => Err("is a prompt's answer, whose `value` must be a string or null"),
_ => Err("must have `kind` alert, confirm or prompt"),
(Some("colorPicker"), Value::Null) => Ok(DialogAnswer::ColorPicker(None)),
(Some("colorPicker"), v) => match crate::cmd::color_value(v).filter(in_range) {
Some(c) => Ok(DialogAnswer::ColorPicker(Some(c))),
None => Err("is a color picker's answer, whose `value` must be a color or null"),
},
_ => Err("must have `kind` alert, confirm, prompt or colorPicker"),
}
}
@@ -245,8 +292,9 @@ impl Dialogs {
let mut waiting = self.waiting.borrow_mut();
let Some(d) = waiting.as_ref().filter(|d| d.kind == answer.kind() && fits) else { return false };
self.record(d, answer.json());
let data = answer.data(d);
*waiting = None;
if let Err(e) = realm.answer(answer.data()) {
if let Err(e) = realm.answer(data) {
*self.failed.borrow_mut() = Some(e);
}
true
@@ -263,12 +311,15 @@ impl Dialogs {
self.count.set(self.count.get().saturating_add(1));
let mut r = Map::new();
r.insert("kind".into(), json!(d.kind.id()));
r.insert("message".into(), json!(d.message));
r.insert("title".into(), json!(d.title.as_deref().unwrap_or(DEFAULT_TITLE)));
if d.kind != DialogKind::ColorPicker {
r.insert("message".into(), json!(d.message));
r.insert("title".into(), json!(d.title.as_deref().unwrap_or(DEFAULT_TITLE)));
}
match d.kind {
DialogKind::Alert => r.insert("errorIcon".into(), json!(d.error_icon)),
DialogKind::Confirm => r.insert("noAsDefault".into(), json!(d.no_as_default)),
DialogKind::Prompt => r.insert("preset".into(), json!(d.preset)),
DialogKind::ColorPicker => r.insert("color".into(), d.color.as_ref().map_or(Value::Null, color_json)),
};
r.insert("answer".into(), answer);
let r = Value::Object(r);
@@ -319,7 +370,8 @@ fn open(host: &Host, kind: DialogKind, args: &[Data]) -> Result<Data, HostError>
host.charge_call(args.iter().map(super::data_size).sum())?;
let Some(message) = text(args, 0) else { return Err(HostError::Type(format!("{name}: give a message"))) };
let (title, error_icon, no_as_default, preset) = match kind {
DialogKind::Alert => (text(args, 1), flag(args, 2), false, String::new()),
// `showColorPicker` opens through `show_color_picker`, not here.
DialogKind::Alert | DialogKind::ColorPicker => (text(args, 1), flag(args, 2), false, String::new()),
DialogKind::Confirm => (text(args, 2), false, flag(args, 1), String::new()),
DialogKind::Prompt => {
let Some(preset) = text(args, 1) else { return Err(HostError::Type(format!("{name}: give the text the field starts with"))) };
@@ -335,7 +387,29 @@ fn open(host: &Host, kind: DialogKind, args: &[Data]) -> Result<Data, HostError>
if preset.len() > MAX_PROMPT_BYTES {
return Err(HostError::Range(format!("{name}: the text the field starts with is longer than 16 KiB")));
}
let dialog = ScriptDialog { kind, message, title, error_icon, no_as_default, preset };
wait_or_answer(host, ScriptDialog { kind, message, title, error_icon, no_as_default, preset, color: None })
}
/// `app.showColorPicker(color)`: the Color Picker on `color`. It returns the color picked for OK,
/// and a new object of `color` for Cancel.
pub(super) fn show_color_picker(host: &Host, color: Color) -> Result<Data, HostError> {
let dialog = ScriptDialog {
kind: DialogKind::ColorPicker,
message: String::new(),
title: None,
error_icon: false,
no_as_default: false,
preset: String::new(),
color: Some(color),
};
wait_or_answer(host, dialog)
}
/// Opens `dialog`: in the app the run waits at it, and a run with nobody to ask takes the next of
/// `answers`.
fn wait_or_answer(host: &Host, dialog: ScriptDialog) -> Result<Data, HostError> {
let kind = dialog.kind;
let name = kind.function();
let d = &host.dialogs;
if d.framed {
if !d.can_wait.get() {
@@ -350,11 +424,11 @@ fn open(host: &Host, kind: DialogKind, args: &[Data]) -> Result<Data, HostError>
match next {
Some(a) if a.kind() == kind => {
d.record(&dialog, a.json());
Ok(a.data())
Ok(a.data(&dialog))
}
Some(a) => {
d.record(&dialog, json!("missing"));
Err(HostError::Abort(format!("{name}: the next answer in `answers` is for {}, not {name}", a.kind().id())))
Err(HostError::Abort(format!("{name}: the next answer in `answers` is for {}, not {}", a.kind().id(), kind.id())))
}
None => {
d.record(&dialog, json!("missing"));
+10
View File
@@ -41,6 +41,8 @@ use crate::{DocState, Document, NodeId, NodeKind, Session};
mod colors;
mod paths;
pub(super) use colors::of_color as color_data;
/// The error for a member read or called with no document open.
pub const NO_ACTIVE_DOCUMENT: &str = "No active document";
/// The error for an object that was deleted, or that came from an earlier run.
@@ -691,6 +693,13 @@ fn app_set_coords(h: &Host, a: &[Data]) -> Answer {
Ok(Data::Undefined)
}
/// `app.showColorPicker(color)`: the Color Picker on `color`, which must be an `RGBColor`,
/// `CMYKColor`, `GrayColor` or `LabColor` ([`super::dialogs::show_color_picker`]).
fn app_show_color_picker(h: &Host, a: &[Data]) -> Answer {
let color = colors::solid(arg(a, 1), "showColorPicker's color")?;
super::dialogs::show_color_picker(h, color)
}
/// A path as `getIsFileOpen` compares it: `\` and `/` alike, and on Windows paths (a drive letter
/// or a `\`) letters in one case.
fn path_key(p: &str) -> String {
@@ -1883,6 +1892,7 @@ pub(super) fn install(realm: &mut Realm, link: &Link) -> std::result::Result<(),
("selection", get(l, app_selection)),
("coordinateSystem", property(l, app_coords, app_set_coords)),
("getIsFileOpen", method(l, app_is_open)),
("showColorPicker", method(l, app_show_color_picker)),
]);
application.push(later(l, "Application", "beep"));
let preset = {
+13 -1
View File
@@ -66,7 +66,7 @@ fn shown(v: f32, scale: f64) -> f64 {
}
/// The color object for a solid color.
fn of_color(c: &Color) -> Data {
pub(in crate::script) fn of_color(c: &Color) -> Data {
match *c {
Color::Rgb { r, g, b } => color_object("RGBColor", &[("red", shown(r, 255.0)), ("green", shown(g, 255.0)), ("blue", shown(b, 255.0))]),
Color::Cmyk { c, m, y, k } => color_object(
@@ -131,6 +131,18 @@ pub(super) fn assigned(d: &Data, what: &str) -> std::result::Result<Option<Color
}))
}
/// The solid color a script gave `what`, checked as [`assigned`] checks it: a `NoColor`, a
/// `GradientColor`, a `SpotColor` or a `PatternColor` throws a `TypeError`.
pub(super) fn solid(d: &Data, what: &str) -> std::result::Result<Color, HostError> {
let not_solid = || HostError::Type(format!("{what} must be an RGBColor, CMYKColor, GrayColor or LabColor"));
match assigned(d, what) {
Ok(Some(c)) => Ok(c),
// A NoColor, or one of the LATER classes, which `assigned` refuses with a plain error.
Ok(None) | Err(HostError::Error(_)) => Err(not_solid()),
Err(e) => Err(e),
}
}
/// `color` as `paint.setFill` and `paint.setStroke` take it.
pub(super) fn param(color: &Color) -> Value {
serde_json::to_value(color).unwrap_or(Value::Null)