mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Find and Replace: ui.dialog.confirm presses the button the action field names
Since the dialog moved to the craft UI framework, `ui.dialog.confirm` on
Find and Replace was refused ("the dialog refuses OK"), and `ui.inspect`
listed no `action` field, although the params text and docs/mcp.md
describe both.
- `ui.inspect` lists the open dialog's `action`, `find` until a set
changes it.
- `ui.dialog.set` takes `action` (as `field`/`value` or in `fields`):
find, replace, replaceFind, replaceAll or done. Any other value is an
error, and nothing in the request is set.
- `ui.dialog.confirm`, or a set with `action: "confirm"`, presses that
button as a click does and puts `action` back to find. Replace and
Replace & Find find a match when Find Next has reached none; done
closes the dialog. A set whose fields are refused presses nothing.
This commit is contained in:
1 parent
4002b2b66c
commit
284ce3e3d4
4 files changed
+259
No files matched your search
@@ -153,6 +153,7 @@ pub fn inspect(app: &VectorcraftApp, ctx: &egui::Context) -> Value {
|
||||
}
|
||||
let mut dialogs = app.dialogs().map_or_else(|| json!([]), |dialogs| dialogs.with(|host| craft::automation::dialogs::inspect(host)));
|
||||
crate::dialogs::javascript_files::show_selected(app, &mut dialogs);
|
||||
crate::dialogs::find_replace::show_action(app, &mut dialogs);
|
||||
// The Simplify or Smooth slider over the canvas, listed first: `"top"` names a hosted dialog open with it.
|
||||
if let (Some(open), Some(strip)) = (dialogs.as_array_mut(), crate::dialogs::simplify_slider::inspected(app)) {
|
||||
open.insert(0, strip);
|
||||
@@ -1463,6 +1464,10 @@ fn dispatch(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, p: &Val
|
||||
if let Some(r) = crate::dialogs::script_window::dialog_method(app, method, ¶ms) {
|
||||
return wrap(r);
|
||||
}
|
||||
// Find and Replace presses the button its `action` field names and stays open.
|
||||
if let Some(r) = crate::dialogs::find_replace::dialog_method(app, ctx, method, ¶ms) {
|
||||
return wrap(r);
|
||||
}
|
||||
// `ui.dialog.set {action}` answers the dialog after setting its fields: the app's own key, which the
|
||||
// framework's method does not take.
|
||||
let action = if method == "ui.dialog.set" { params.as_object_mut().and_then(|o| o.remove("action")) } else { None };
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
//! `replace` text and `backwards`, the place it reached in `from` (`{id, start, end}`), the drawing's
|
||||
//! UID and Revert counter in `doc`, and the match in `found` (`{index, count}`). Changing the
|
||||
//! loaded drawing, Find text, or switches restarts the search. [`pump`] runs the requests each frame.
|
||||
//! Automation presses the buttons through the dialog's `action` field and `ui.dialog.confirm`
|
||||
//! ([`dialog_method`]).
|
||||
|
||||
use craft::text::find::FindOptions;
|
||||
use craft::text::ui::find::{FindCheck, FindReplaceDialog, FindRequest, FindTab, ReplaceButtons};
|
||||
@@ -109,6 +111,7 @@ pub fn open(app: &mut VectorcraftApp, params: &Map<String, Value>) -> Result<Val
|
||||
.replace_buttons(ReplaceButtons::Separate);
|
||||
let dialogs = app.dialogs().ok_or("no window")?;
|
||||
forget_found(app);
|
||||
app.ui.find_action = None;
|
||||
dialogs.with(|host| host.open(dialog));
|
||||
Ok(Value::Null)
|
||||
}
|
||||
@@ -340,7 +343,14 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
};
|
||||
Some(ran.unwrap_or_else(|e| said(&e)))
|
||||
});
|
||||
report(app, &form, message);
|
||||
}
|
||||
|
||||
/// Tells the open dialog `message`, or the refreshed match line when there is none, and whether a
|
||||
/// match is current.
|
||||
fn report(app: &mut VectorcraftApp, form: &Form, message: Option<String>) {
|
||||
let (matched, match_message) = refresh_match(app, &form.query);
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
dialogs.with(|host| {
|
||||
if let Some(d) = host.get_mut::<FindReplaceDialog>() {
|
||||
if let Some(message) = message.or(match_message) {
|
||||
@@ -351,5 +361,143 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
});
|
||||
}
|
||||
|
||||
/// The values the `action` field takes: the button `ui.dialog.confirm` presses.
|
||||
const ACTIONS: [&str; 5] = ["find", "replace", "replaceFind", "replaceAll", "done"];
|
||||
|
||||
/// The `action` field a `ui.dialog.set` gives, as `field` and `value` or as an item of `fields`, taken
|
||||
/// out of `params`: `Ok(None)` when it gives none, an error when it is not one of [`ACTIONS`].
|
||||
fn take_action(params: &mut Value) -> Result<Option<String>, String> {
|
||||
let Some(o) = params.as_object_mut() else { return Ok(None) };
|
||||
let mut given = None;
|
||||
if o.get("field").and_then(Value::as_str) == Some("action") {
|
||||
if o.contains_key("fields") {
|
||||
return Err("`fields` takes the place of `field` and `value`".into());
|
||||
}
|
||||
o.remove("field");
|
||||
given = Some(o.remove("value").unwrap_or(Value::Null));
|
||||
}
|
||||
let mut only_action = false;
|
||||
if let Some(list) = o.get_mut("fields").and_then(Value::as_array_mut) {
|
||||
let is_action = |item: &Value| item.get("field").and_then(Value::as_str) == Some("action");
|
||||
if let Some(last) = list.iter().rev().find(|item| is_action(item)) {
|
||||
given = Some(last.get("value").cloned().unwrap_or(Value::Null));
|
||||
}
|
||||
let before = list.len();
|
||||
list.retain(|item| !is_action(item));
|
||||
only_action = list.is_empty() && before > 0;
|
||||
}
|
||||
// A list that held only `action` sets nothing else.
|
||||
if only_action {
|
||||
o.remove("fields");
|
||||
}
|
||||
match given {
|
||||
None => Ok(None),
|
||||
Some(Value::String(a)) if ACTIONS.contains(&a.as_str()) => Ok(Some(a)),
|
||||
Some(_) => Err("`action` must be find, replace, replaceFind, replaceAll or done".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The open dialog as `ui.inspect` lists it, `{id, parent, key, fields}` with its `action`, or null.
|
||||
fn listed(app: &VectorcraftApp) -> Value {
|
||||
let Some(dialogs) = app.dialogs() else { return Value::Null };
|
||||
let mut open = dialogs.with(|host| craft::automation::dialogs::inspect(host));
|
||||
show_action(app, &mut open);
|
||||
let id = dialogs.with(|host| host.get::<FindReplaceDialog>().map(|d| d.id().value().to_string()));
|
||||
open.as_array().and_then(|open| open.iter().find(|d| id.as_deref().is_some_and(|id| d["id"] == id)).cloned()).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
/// Adds the `action` field to the open Find and Replace dialog in `ui.inspect`'s `dialogs`.
|
||||
pub(crate) fn show_action(app: &VectorcraftApp, dialogs: &mut Value) {
|
||||
let Some(id) = app.dialogs().and_then(|d| d.with(|host| host.get::<FindReplaceDialog>().map(|d| d.id().value().to_string()))) else { return };
|
||||
let action = app.ui.find_action.clone().unwrap_or_else(|| "find".into());
|
||||
let shown = dialogs.as_array_mut().and_then(|open| open.iter_mut().find(|d| d["id"] == id.as_str()));
|
||||
if let Some(fields) = shown.and_then(|d| d.get_mut("fields")).and_then(Value::as_object_mut) {
|
||||
fields.insert("action".into(), json!(action));
|
||||
}
|
||||
}
|
||||
|
||||
/// `ui.dialog.set` and `ui.dialog.confirm` (`method`) on the open Find and Replace dialog, when
|
||||
/// `params.dialog` names it: a set takes the `action` field, and `ui.dialog.confirm`, or a set with
|
||||
/// `action: "confirm"`, presses the button it names (`find` when none is set) and leaves `action` at
|
||||
/// `find` again. Every other request is the host's, as is a set that gives no `action` field and
|
||||
/// does not confirm: `None`.
|
||||
pub(crate) fn dialog_method(app: &mut VectorcraftApp, ctx: &egui::Context, method: &str, params: &Value) -> Option<Result<Value, String>> {
|
||||
if !matches!(method, "ui.dialog.set" | "ui.dialog.confirm") {
|
||||
return None;
|
||||
}
|
||||
let dialogs = app.dialogs()?;
|
||||
let ours = dialogs.with(|host| {
|
||||
let id = host.get::<FindReplaceDialog>().map(StackDialog::id);
|
||||
id.is_some() && craft::automation::dialogs::dialog_id(host, params).ok() == id
|
||||
});
|
||||
if !ours {
|
||||
return None;
|
||||
}
|
||||
let keys: &[&str] = if method == "ui.dialog.set" { &["dialog", "field", "value", "fields", "action"] } else { &["dialog"] };
|
||||
let mut params = params.clone();
|
||||
let answer = if method == "ui.dialog.set" { params.as_object_mut().and_then(|o| o.remove("action")) } else { Some(json!("confirm")) };
|
||||
let given = if method == "ui.dialog.set" {
|
||||
match take_action(&mut params) {
|
||||
Ok(given) => given,
|
||||
Err(e) => return Some(Err(e)),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let confirm = match answer.as_ref().map(Value::as_str) {
|
||||
None => false,
|
||||
Some(Some("confirm")) => true,
|
||||
// Cancel closes it as the host closes any dialog.
|
||||
Some(Some("cancel")) if given.is_none() => return None,
|
||||
Some(Some("cancel")) => false,
|
||||
Some(_) => return Some(Err("`action` must be confirm or cancel".into())),
|
||||
};
|
||||
if given.is_none() && !confirm {
|
||||
return None;
|
||||
}
|
||||
if let Err(e) = craft::automation::protocol::Params(¶ms).only(None, keys) {
|
||||
return Some(Err(e));
|
||||
}
|
||||
// The other fields are set first, all or none, as the host sets and checks them.
|
||||
let sets = ["field", "value", "fields"].iter().any(|k| params.get(k).is_some());
|
||||
if sets && let Some(Err(e)) = dialogs.with(|host| craft::automation::dialogs::dialog_method(host, ctx, "ui.dialog.set", ¶ms)) {
|
||||
return Some(Err(e));
|
||||
}
|
||||
if let Some(given) = given {
|
||||
app.ui.find_action = Some(given);
|
||||
}
|
||||
if answer.as_ref().and_then(Value::as_str) == Some("cancel") {
|
||||
dialogs.with(|host| host.cancel::<FindReplaceDialog>(ctx));
|
||||
return Some(Ok(Value::Null));
|
||||
}
|
||||
Some(if confirm { press(app, ctx) } else { Ok(listed(app)) })
|
||||
}
|
||||
|
||||
/// Presses the button the `action` field names, as a click on it does, and leaves `action` at
|
||||
/// `find`. Replace and Replace & Find find a match when Find Next has reached none with the Find
|
||||
/// text and the switches as they are now. Done closes the dialog → the dialog as listed, or null
|
||||
/// once closed; a refusal is the dialog's message and the error.
|
||||
fn press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result<Value, String> {
|
||||
let action = app.ui.find_action.take().unwrap_or_else(|| "find".into());
|
||||
let dialogs = app.dialogs().ok_or("no window")?;
|
||||
let form = dialogs.with(|host| host.get::<FindReplaceDialog>().map(form)).ok_or("no hosted dialog is open")?;
|
||||
if action == "done" {
|
||||
dialogs.with(|host| host.cancel::<FindReplaceDialog>(ctx));
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
let found = app.ui.last_find.as_ref().is_some_and(|last| current_match(app, last, &form.query).is_some());
|
||||
let ran = match action.as_str() {
|
||||
"replace" | "replaceFind" if found => replace(app, &form, action == "replaceFind"),
|
||||
"replaceAll" => replace_all(app, &form),
|
||||
_ => find(app, &form),
|
||||
};
|
||||
let message = match &ran {
|
||||
Ok(m) => Some(m.clone()),
|
||||
Err(e) => Some(said(e)),
|
||||
};
|
||||
report(app, &form, message);
|
||||
ran.map(|_| listed(app))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -97,6 +97,107 @@ fn selected(p: &Panel) -> Vec<u64> {
|
||||
p.app().session.active().unwrap().selection.objects.iter().map(|id| id.0).collect()
|
||||
}
|
||||
|
||||
/// A control request answered at once → its result, or its error.
|
||||
fn control(p: &Panel, method: &str, params: Value) -> Result<Value, String> {
|
||||
let (req, _) = craft::automation::ControlRequest::new(method, params);
|
||||
let craft::automation::Reply::Done(r) = crate::control::handle(&mut p.app(), p.ctx(), &req) else { panic!("{method} answered later") };
|
||||
match r.get("error") {
|
||||
Some(e) if !e.is_null() => Err(e.as_str().map_or_else(|| e.to_string(), str::to_string)),
|
||||
_ => Ok(r["result"].clone()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The open Find and Replace dialog's `action`, as `ui.inspect` lists it.
|
||||
fn listed_action(p: &Panel) -> Value {
|
||||
let r = control(p, "ui.inspect", json!({})).unwrap();
|
||||
let open = r["dialogs"].as_array().cloned().unwrap_or_default();
|
||||
open.last().map(|d| d["fields"]["action"].clone()).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
fn set_action(p: &Panel, action: &str) {
|
||||
control(p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": action})).unwrap();
|
||||
}
|
||||
|
||||
/// `ui.inspect` lists the dialog's `action`, `find` until a set changes it. `ui.dialog.confirm`
|
||||
/// presses the button it names, as a click does, keeps the dialog open and puts `action` back to
|
||||
/// find. Replace and Replace & Find find a match when Find Next reached none; done closes it.
|
||||
#[test]
|
||||
fn confirm_presses_the_button_the_action_names() {
|
||||
let (app, ids) = app_with(&["cat cat cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
assert_eq!(listed_action(&p), json!("find"));
|
||||
let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}]);
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap();
|
||||
let before = undo_steps(&p);
|
||||
control(&p, "ui.dialog.confirm", json!({})).unwrap();
|
||||
assert_eq!(matched(&p), Some((ids[0], 0, 3)), "find runs without an action set");
|
||||
assert!(message(&p).starts_with("1 / 3"), "{}", message(&p));
|
||||
|
||||
set_action(&p, "replace");
|
||||
assert_eq!(listed_action(&p), json!("replace"));
|
||||
let shown = control(&p, "ui.dialog.confirm", json!({})).unwrap();
|
||||
assert_eq!(plain(&p, ids[0]), "dog cat cat", "replace changes the match found");
|
||||
assert_eq!((undo_steps(&p), listed_action(&p)), (before + 1, json!("find")));
|
||||
assert_eq!(shown["fields"]["action"], json!("find"));
|
||||
|
||||
// Replace & Find with no match found finds one and changes nothing.
|
||||
set_action(&p, "replaceFind");
|
||||
control(&p, "ui.dialog.confirm", json!({})).unwrap();
|
||||
assert_eq!((plain(&p, ids[0]), matched(&p)), ("dog cat cat".to_string(), Some((ids[0], 4, 7))));
|
||||
set_action(&p, "replaceFind");
|
||||
control(&p, "ui.dialog.confirm", json!({})).unwrap();
|
||||
assert_eq!((plain(&p, ids[0]), matched(&p)), ("dog dog cat".to_string(), Some((ids[0], 8, 11))));
|
||||
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": "replaceAll", "action": "confirm"})).unwrap();
|
||||
assert_eq!((plain(&p, ids[0]), message(&p)), ("dog dog dog".to_string(), "Matches replaced: 1".to_string()));
|
||||
assert_eq!(undo_steps(&p), before + 3);
|
||||
|
||||
// A set and confirm in one request closes the dialog with done.
|
||||
let done = json!([{"field": "action", "value": "done"}]);
|
||||
let closed = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": done, "action": "confirm"})).unwrap();
|
||||
assert_eq!(closed, Value::Null);
|
||||
assert!(!is_open(&p));
|
||||
}
|
||||
|
||||
/// An `action` that names no button is refused when it is set, as are the fields set with it, and
|
||||
/// `action` stays as it was. A set and confirm whose set is refused presses no button.
|
||||
#[test]
|
||||
fn an_action_that_names_no_button_is_refused() {
|
||||
let (app, ids) = app_with(&["cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
let refusal = "`action` must be find, replace, replaceFind, replaceAll or done";
|
||||
for v in [json!("jump"), json!(3), json!(null), json!("Replace")] {
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "action", "value": v}));
|
||||
assert_eq!(r, Err(refusal.to_string()), "{v}");
|
||||
}
|
||||
let fields = json!([{"field": "replace", "value": "dog"}, {"field": "action", "value": "skip"}]);
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields}));
|
||||
assert_eq!(r, Err(refusal.to_string()));
|
||||
assert_eq!((field(&p, "replace"), listed_action(&p)), (json!(""), json!("find")));
|
||||
// A field the dialog refuses leaves `action` as it was.
|
||||
let fields = json!([{"field": "matchCase", "value": "yes"}, {"field": "action", "value": "replaceAll"}]);
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields}));
|
||||
assert!(r.is_err(), "{r:?}");
|
||||
assert_eq!(listed_action(&p), json!("find"));
|
||||
// A request with a key the method does not take is refused.
|
||||
assert!(control(&p, "ui.dialog.confirm", json!({"dialog": "top", "action": "replace"})).is_err());
|
||||
// A set the host refuses presses nothing, even with `action: "confirm"`.
|
||||
let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}, {"field": "action", "value": "replaceAll"}]);
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap();
|
||||
for bad in [json!({"fields": {}}), json!({"value": 1}), json!({"field": "nope", "value": 1})] {
|
||||
let mut request = bad.clone();
|
||||
request["dialog"] = json!("top");
|
||||
request["action"] = json!("confirm");
|
||||
assert!(control(&p, "ui.dialog.set", request).is_err(), "{bad}");
|
||||
assert_eq!((plain(&p, ids[0]), listed_action(&p)), ("cat".to_string(), json!("replaceAll")), "{bad}");
|
||||
}
|
||||
assert!(is_open(&p));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cached_match_cannot_replace_text_restored_by_revert() {
|
||||
let (mut app, ids) = app_with(&["Needle Needle"]);
|
||||
|
||||
@@ -732,6 +732,10 @@ pub struct UiState {
|
||||
/// Edit › Find Next goes on from while the dialog is closed. Not saved.
|
||||
#[serde(skip)]
|
||||
pub last_find: Option<serde_json::Value>,
|
||||
/// The open Find and Replace dialog's `action` field, the button `ui.dialog.confirm` presses
|
||||
/// ([`crate::dialogs::find_replace::dialog_method`]); `None` is `find`. Not saved.
|
||||
#[serde(skip)]
|
||||
pub find_action: Option<String>,
|
||||
/// The notes of the last open that gave any, or of the last save or export, with the status
|
||||
/// text that names the first of them: hovering that text lists them when there are several.
|
||||
/// Not saved.
|
||||
@@ -1530,6 +1534,7 @@ impl Default for UiState {
|
||||
status_sets: 0,
|
||||
replace_font_warning: None,
|
||||
last_find: None,
|
||||
find_action: None,
|
||||
status_notes: None,
|
||||
startup_notes: Vec::new(),
|
||||
status_kept: None,
|
||||
|
||||
Reference in new issue
Block a user