mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Merge pull request #10725 from dotCooCoo/fix/find-replace-confirm-action
Find and Replace: automation presses the button the dialog's action field names
This commit is contained in:
7 files changed
+404
-3
No files matched your search
@@ -1018,7 +1018,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.paramDialog",
|
||||
"Command Dialog",
|
||||
"",
|
||||
"{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed",
|
||||
"{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). An action whose button the open tab does not show is refused: find runs on the Find and Replace tabs, replace, replaceFind and replaceAll on the Replace tab, done on every tab. After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed",
|
||||
),
|
||||
(
|
||||
"ui.recolorDialog",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -338,9 +341,18 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
|
||||
FindRequest::ReplaceAll { .. } => replace_all(app, &form),
|
||||
_ => return None,
|
||||
};
|
||||
// A click presses a button as `ui.dialog.confirm` does: `action` goes back to find.
|
||||
app.ui.find_action = None;
|
||||
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 +363,175 @@ 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) };
|
||||
// The host's setter refuses the same mix.
|
||||
if o.contains_key("fields") && (o.contains_key("field") || o.contains_key("value")) {
|
||||
return Err("`fields` takes the place of `field` and `value`".into());
|
||||
}
|
||||
let mut given = None;
|
||||
if o.get("field").and_then(Value::as_str) == Some("action") {
|
||||
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) {
|
||||
// The framework's limit counts `action` with the other items.
|
||||
let most = craft::automation::dialogs::MAX_DIALOG_FIELDS;
|
||||
if list.len() > most {
|
||||
return Err(format!("`fields` holds {} items; at most {most}", list.len()));
|
||||
}
|
||||
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 answers with the dialog as
|
||||
/// `ui.inspect` lists it, `action` included, 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 with `action: "cancel"` that gives
|
||||
/// no `action` field: `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 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. A set that
|
||||
// gives no `action` and does not confirm goes to the host's setter whole, which refuses one
|
||||
// with nothing to set.
|
||||
let sets = ["field", "value", "fields"].iter().any(|k| params.get(k).is_some()) || (given.is_none() && !confirm);
|
||||
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)) })
|
||||
}
|
||||
|
||||
/// Whether the dialog's `tab` (its `tab` field: `find`, `replace` or `goTo`) shows the button
|
||||
/// `action` presses, enabled. Every tab shows Done. The Find tab shows Find Next, and the Replace tab
|
||||
/// shows Find Next, Replace, Replace & Find and Replace All. The Go To tab shows no Find Next: its
|
||||
/// first button is Go To, which stays disabled while the dialog has no go-to kinds.
|
||||
fn shown_on(tab: &str, action: &str) -> bool {
|
||||
match action {
|
||||
"done" => true,
|
||||
"find" => matches!(tab, "find" | "replace"),
|
||||
_ => tab == "replace",
|
||||
}
|
||||
}
|
||||
|
||||
/// 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. A button the open tab does not
|
||||
/// show ([`shown_on`]) and, with an empty Find text, every button but Done run nothing, as a click
|
||||
/// cannot press them: the press is an error that leaves the document, the message and `action` as
|
||||
/// they were.
|
||||
fn press(app: &mut VectorcraftApp, ctx: &egui::Context) -> Result<Value, String> {
|
||||
let action = app.ui.find_action.clone().unwrap_or_else(|| "find".into());
|
||||
let dialogs = app.dialogs().ok_or("no window")?;
|
||||
let (form, tab) = dialogs
|
||||
.with(|host| host.get::<FindReplaceDialog>().map(|d| (form(d), d.json_fields().and_then(|f| f["tab"].as_str().map(str::to_string)))))
|
||||
.ok_or("no hosted dialog is open")?;
|
||||
let tab = tab.unwrap_or_default();
|
||||
if !shown_on(&tab, &action) {
|
||||
return Err(format!("the {tab} tab has no button for `action` {action}"));
|
||||
}
|
||||
if action == "done" {
|
||||
app.ui.find_action = None;
|
||||
dialogs.with(|host| host.cancel::<FindReplaceDialog>(ctx));
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
if form.query["find"].as_str().is_none_or(str::is_empty) {
|
||||
return Err("the Find text is empty".into());
|
||||
}
|
||||
app.ui.find_action = None;
|
||||
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,213 @@ 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));
|
||||
}
|
||||
|
||||
/// With an empty Find text a confirm presses nothing, as the dialog's buttons run nothing: it is
|
||||
/// refused, and the text, the undo steps, the message and `action` stay as they were. Done still
|
||||
/// closes the dialog.
|
||||
#[test]
|
||||
fn a_confirm_with_no_find_text_presses_nothing() {
|
||||
let (app, ids) = app_with(&["cat cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "replace", "value": "dog"})).unwrap();
|
||||
assert_eq!(field(&p, "find"), json!(""));
|
||||
let (before, said) = (undo_steps(&p), message(&p));
|
||||
for action in ["find", "replace", "replaceFind", "replaceAll"] {
|
||||
set_action(&p, action);
|
||||
let r = control(&p, "ui.dialog.confirm", json!({}));
|
||||
assert_eq!(r, Err("the Find text is empty".to_string()), "{action}");
|
||||
assert_eq!((plain(&p, ids[0]), undo_steps(&p), message(&p)), ("cat cat".to_string(), before, said.clone()), "{action}");
|
||||
assert_eq!(listed_action(&p), json!(action));
|
||||
}
|
||||
set_action(&p, "done");
|
||||
assert_eq!(control(&p, "ui.dialog.confirm", json!({})), Ok(Value::Null));
|
||||
assert!(!is_open(&p));
|
||||
}
|
||||
|
||||
/// A confirm presses only a button the open tab shows enabled: on the Find tab the Replace buttons
|
||||
/// and on the Go To tab Find Next are refused, naming the tab, and the text, the undo steps, the
|
||||
/// message and `action` stay as they were. Find Next runs on the Find tab, and Done closes the
|
||||
/// dialog from the Go To tab.
|
||||
#[test]
|
||||
fn a_confirm_presses_only_a_button_the_tab_shows() {
|
||||
let (app, ids) = app_with(&["cat cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
let fields = json!([{"field": "find", "value": "cat"}, {"field": "replace", "value": "dog"}, {"field": "tab", "value": "find"}]);
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields})).unwrap();
|
||||
let (before, said) = (undo_steps(&p), message(&p));
|
||||
for action in ["replace", "replaceFind", "replaceAll"] {
|
||||
set_action(&p, action);
|
||||
let r = control(&p, "ui.dialog.confirm", json!({}));
|
||||
assert_eq!(r, Err(format!("the find tab has no button for `action` {action}")), "{action}");
|
||||
assert_eq!((plain(&p, ids[0]), undo_steps(&p), message(&p)), ("cat cat".to_string(), before, said.clone()), "{action}");
|
||||
assert_eq!(listed_action(&p), json!(action));
|
||||
}
|
||||
set_action(&p, "find");
|
||||
control(&p, "ui.dialog.confirm", json!({})).unwrap();
|
||||
assert_eq!(matched(&p), Some((ids[0], 0, 3)), "Find Next is on the Find tab");
|
||||
|
||||
control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "tab", "value": "goTo"})).unwrap();
|
||||
let (at, said) = (from(&p), message(&p));
|
||||
let r = control(&p, "ui.dialog.confirm", json!({}));
|
||||
assert_eq!(r, Err("the goTo tab has no button for `action` find".to_string()));
|
||||
assert_eq!((from(&p), message(&p), listed_action(&p)), (at, said, json!("find")));
|
||||
set_action(&p, "done");
|
||||
assert_eq!(control(&p, "ui.dialog.confirm", json!({})), Ok(Value::Null));
|
||||
assert!(!is_open(&p));
|
||||
}
|
||||
|
||||
/// `fields` given with `field` or `value` is refused, as the host refuses it, even when the list
|
||||
/// holds only `action`; nothing is set.
|
||||
#[test]
|
||||
fn fields_with_field_or_value_is_refused() {
|
||||
let (app, _) = app_with(&["cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
let refusal = Err("`fields` takes the place of `field` and `value`".to_string());
|
||||
let only_action = json!([{"field": "action", "value": "replace"}]);
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "find", "value": "cat", "fields": only_action}));
|
||||
assert_eq!(r, refusal);
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "value": "cat", "fields": only_action}));
|
||||
assert_eq!(r, refusal);
|
||||
assert_eq!((field(&p, "find"), listed_action(&p)), (json!(""), json!("find")));
|
||||
}
|
||||
|
||||
/// A set of other fields answers with `action` listed. A click on a button, like a confirm, puts
|
||||
/// `action` back to find, and so does opening the dialog again. A `fields` list of more than 256
|
||||
/// items is refused, `action` counted with the rest.
|
||||
#[test]
|
||||
fn action_is_listed_and_goes_back_to_find() {
|
||||
let (app, _) = app_with(&["cat"]);
|
||||
let mut p = Panel::new(app, draw);
|
||||
choose(&p);
|
||||
p.frame();
|
||||
set_action(&p, "replace");
|
||||
let shown = control(&p, "ui.dialog.set", json!({"dialog": "top", "field": "find", "value": "cat"})).unwrap();
|
||||
assert_eq!((shown["fields"]["find"].clone(), shown["fields"]["action"].clone()), (json!("cat"), json!("replace")));
|
||||
assert!(control(&p, "ui.dialog.set", json!({"dialog": "top"})).is_err(), "a set with nothing to set");
|
||||
|
||||
let mut fields = vec![json!({"field": "find", "value": "cat"}); 256];
|
||||
fields.push(json!({"field": "action", "value": "replaceAll"}));
|
||||
let r = control(&p, "ui.dialog.set", json!({"dialog": "top", "fields": fields}));
|
||||
assert_eq!(r, Err("`fields` holds 257 items; at most 256".to_string()));
|
||||
assert_eq!(listed_action(&p), json!("replace"));
|
||||
|
||||
p.click("Find Next");
|
||||
assert_eq!(listed_action(&p), json!("find"), "a click");
|
||||
|
||||
set_action(&p, "replace");
|
||||
control(&p, "ui.dialog.cancel", json!({"dialog": "top"})).unwrap();
|
||||
assert!(!is_open(&p));
|
||||
choose(&p);
|
||||
p.frame();
|
||||
assert_eq!(listed_action(&p), json!("find"), "opening again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cached_match_cannot_replace_text_restored_by_revert() {
|
||||
let (mut app, ids) = app_with(&["Needle Needle"]);
|
||||
|
||||
@@ -1019,7 +1019,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"ui.paramDialog",
|
||||
"Command Dialog",
|
||||
"",
|
||||
"{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed",
|
||||
"{command, label?, params} open a parameter dialog for any command. For commands other than edit.findReplace, params.__doc may be an unsigned document uid: the dialog previews and confirms only while that document is active, and OK after a document switch closes it without running the command. An invalid __doc is an error; omitting it keeps the existing behavior. For edit.findReplace it opens Find and Replace (dialog `findReplace`), which stays open: fields find, replace, matchCase, wholeWord, backwards, hiddenLayers, lockedLayers, and action, which ui.dialog.confirm runs: find (the next match, edit.findNext, shown in the dialog with its place among the matches), replace (the match found, edit.replaceMatch), replaceFind (both), replaceAll (edit.findReplace) or done (close). An action whose button the open tab does not show is refused: find runs on the Find and Replace tabs, replace, replaceFind and replaceAll on the Replace tab, done on every tab. After each one action is find again. Replace and Replace & Find change a match that find reached with the find text and options as they are now, and find one otherwise. The dialog opens on the last search, with the fields params gives in its place (a null field gives none; a field of the wrong type is an error, and ui.dialog.confirm then runs no action), and Edit › Find Next goes on from it in the same document while the dialog is closed",
|
||||
),
|
||||
(
|
||||
"ui.recolorDialog",
|
||||
|
||||
@@ -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,
|
||||
|
||||
+3
-1
@@ -14401,7 +14401,9 @@ In the app, Edit › Find and Replace… opens a dialog that stays open (kind `f
|
||||
fields, Match Case, Find Whole Word, Search Backwards, Check Hidden Layers and Check Locked Layers, and Find, Replace,
|
||||
Replace & Find, Replace All and Done. Its fields are `find`, `replace`, `matchCase`, `wholeWord`, `backwards`,
|
||||
`hiddenLayers`, `lockedLayers` and `action`; `ui.dialog.confirm` runs the `action`: `find` (the default), `replace`,
|
||||
`replaceFind`, `replaceAll` or `done`, which closes the dialog. The dialog shows the match found in its line of text
|
||||
`replaceFind`, `replaceAll` or `done`, which closes the dialog. It refuses an `action` whose button the open tab does
|
||||
not show: `find` runs on the Find and Replace tabs, `replace`, `replaceFind` and `replaceAll` on the Replace tab, and
|
||||
`done` on every tab. The dialog shows the match found in its line of text
|
||||
with its place among the matches. Replace and Replace & Find change a match that Find reached with the Find text and
|
||||
the options as they are now, and find one otherwise. Enter runs Find. Edit › Find Next goes on from the dialog's last
|
||||
search while the dialog is closed, and opens the dialog when there has been none. A place the search reached in one
|
||||
|
||||
Reference in new issue
Block a user