mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge #14424 into the merge queue batch
Check Spelling: start at the active layer, go up the stack and wrap
This commit is contained in:
commit
ddb38c9efc
7 files changed
+456
-18
No files matched your search
@@ -54,14 +54,17 @@ fn char_offsets(text: &str, offsets: &[usize]) -> Vec<usize> {
|
||||
result
|
||||
}
|
||||
|
||||
/// Type layers top to bottom (Layers panel order), skipping fully locked ones.
|
||||
fn type_layers(doc: &Document) -> Vec<LayerId> {
|
||||
doc.walk().into_iter().rev().filter(|(p, _, l)| matches!(l.content, LayerContent::Text(_)) && !doc.locks_at(p).all).map(|(_, _, l)| l.id).collect()
|
||||
}
|
||||
|
||||
fn has_type(s: &Session) -> std::result::Result<(), String> {
|
||||
let d = s.active().ok_or("no document open")?;
|
||||
if type_layers(&d.doc).is_empty() { Err("the document has no type layers".into()) } else { Ok(()) }
|
||||
/// Type layers in checking order, skipping fully locked ones: from the type layer `start` up the
|
||||
/// stack, then from the bottom up to just below it, as the reference does (#14174). Without a
|
||||
/// type layer to start at, top to bottom (Layers panel order).
|
||||
fn type_layers(doc: &Document, start: Option<LayerId>) -> Vec<LayerId> {
|
||||
let mut layers: Vec<(LayerId, bool)> =
|
||||
doc.walk().into_iter().filter(|(_, _, l)| matches!(l.content, LayerContent::Text(_))).map(|(p, _, l)| (l.id, doc.locks_at(&p).all)).collect();
|
||||
match start.and_then(|id| layers.iter().position(|(l, _)| *l == id)) {
|
||||
Some(i) => layers.rotate_left(i),
|
||||
None => layers.reverse(),
|
||||
}
|
||||
layers.into_iter().filter(|(_, locked)| !locked).map(|(id, _)| id).collect()
|
||||
}
|
||||
|
||||
fn text(doc: &Document, id: LayerId) -> Option<&str> {
|
||||
@@ -82,13 +85,18 @@ fn accepted(s: &Session, p: &Value) -> HashSet<String> {
|
||||
|
||||
fn layers_param(s: &Session, p: &Value) -> Result<Vec<LayerId>> {
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let all = type_layers(&st.doc);
|
||||
if p.get("allLayers").and_then(Value::as_bool) == Some(false) || p.get("layer").is_some() {
|
||||
let active = layer_param(s, p)?;
|
||||
Ok(all.into_iter().filter(|id| *id == active).collect())
|
||||
} else {
|
||||
Ok(all)
|
||||
return Ok(type_layers(&st.doc, None).into_iter().filter(|id| *id == active).collect());
|
||||
}
|
||||
// The dialog passes the layer it was opened on (null without one), which stays the start when
|
||||
// the active layer changes.
|
||||
let start = match p.get("from") {
|
||||
None => st.active_layer,
|
||||
Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_u64().map(LayerId).ok_or_else(|| bad("edit.checkSpelling", "`from` must be a layer id"))?),
|
||||
};
|
||||
Ok(type_layers(&st.doc, start))
|
||||
}
|
||||
|
||||
fn list(s: &Session, p: &Value) -> Result<Value> {
|
||||
@@ -283,8 +291,9 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
label: "Check Spelling…",
|
||||
menu: &["Edit"],
|
||||
shortcut: None,
|
||||
params: r#"{"action":"list|change|changeAll|addToDictionary|removeFromDictionary|suggest"="list","allLayers":bool=true,"layer":id|name? (check one layer),"ignore":[word]? (Ignore All for this check),"suggestions":n=5; change: "layer"? (default: the active layer),"start","end" (chars),"word"? (verified),"replace"; changeAll: "word","replace"; addToDictionary/removeFromDictionary/suggest: "word"} → list: {"misspellings":[{"layer","start","end","word","suggestions"}],"count"} (an action records only a call without "action", as {"allLayers"} with its dialog on)"#,
|
||||
enabled: has_type,
|
||||
params: r#"{"action":"list|change|changeAll|addToDictionary|removeFromDictionary|suggest"="list","allLayers":bool=true,"layer":id|name? (check one layer),"from":id|null? (all layers: start at this type layer, go up and wrap; default: the active layer; null, or a `from` that is not a type layer: top to bottom),"ignore":[word]? (Ignore All for this check),"suggestions":n=5; change: "layer"? (default: the active layer),"start","end" (chars),"word"? (verified),"replace"; changeAll: "word","replace"; addToDictionary/removeFromDictionary/suggest: "word"} → list: {"misspellings":[{"layer","start","end","word","suggestions"}],"count"} (an action records only a call without "action", as {"allLayers"} with its dialog on)"#,
|
||||
// Any open document, also one without type layers, where it lists nothing.
|
||||
enabled: crate::commands::has_doc,
|
||||
journal: true,
|
||||
run: check_spelling,
|
||||
},
|
||||
@@ -468,11 +477,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disabled_without_type() {
|
||||
fn enabled_only_with_a_document() {
|
||||
let mut s = Session::new();
|
||||
assert!(!s.is_enabled("edit.checkSpelling"));
|
||||
s.execute("file.new", json!({"width": 10, "height": 10})).unwrap();
|
||||
assert!(!s.is_enabled("edit.checkSpelling"));
|
||||
assert!(s.is_enabled("edit.checkSpelling"));
|
||||
assert!(!s.is_enabled("type.insertText"));
|
||||
}
|
||||
|
||||
@@ -498,3 +507,11 @@ mod tests {
|
||||
assert_eq!(s.execute("type.caret", json!({"layer": id, "index": inserted["caret"]})).unwrap()["index"], inserted["caret"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "type_spell_enabled_tests.rs"]
|
||||
mod enabled_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "type_spell_order_tests.rs"]
|
||||
mod order_tests;
|
||||
@@ -0,0 +1,67 @@
|
||||
//! #14172: Edit > Check Spelling is enabled in any open document, also one without type layers,
|
||||
//! where it finds nothing and adds no history state; with no document open it stays greyed.
|
||||
//! `type.insertText` keeps needing an active type layer.
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
pub(super) const CMD: &str = "edit.checkSpelling";
|
||||
|
||||
/// A white document with only its Background, as File > New makes one.
|
||||
pub(super) fn blank() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 800, "height": 500, "background": "white"})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
pub(super) fn history(s: &Session) -> Vec<String> {
|
||||
s.active().unwrap().history.entries()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn greyed_without_a_document() {
|
||||
let s = Session::new();
|
||||
assert!(!s.is_enabled(CMD));
|
||||
assert_eq!(s.disabled_reason(CMD).as_deref(), Some("no document open"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enabled_in_a_document_without_type_layers() {
|
||||
let mut s = blank();
|
||||
assert!(s.is_enabled(CMD), "{:?}", s.disabled_reason(CMD));
|
||||
s.execute("layer.new.layer", json!({"name": "Empty"})).unwrap();
|
||||
assert!(s.is_enabled(CMD), "an empty pixel layer on top keeps it enabled");
|
||||
assert!(!s.is_enabled("type.insertText"), "Insert Glyph still needs an active type layer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn listing_a_document_without_type_layers_finds_nothing_and_adds_no_history() {
|
||||
let mut s = blank();
|
||||
s.execute("layer.new.layer", json!({"name": "Empty"})).unwrap();
|
||||
let before = history(&s);
|
||||
for params in [json!({"action": "list"}), json!({})] {
|
||||
assert_eq!(s.execute(CMD, params.clone()).unwrap(), json!({"misspellings": [], "count": 0}), "{params}");
|
||||
}
|
||||
assert_eq!(history(&s), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enabled_with_type_layers_as_before() {
|
||||
let mut s = blank();
|
||||
s.execute("type.create", json!({"x": 10, "y": 50, "text": "Teh end", "size": 12})).unwrap();
|
||||
assert!(s.is_enabled(CMD));
|
||||
assert!(s.is_enabled("type.insertText"));
|
||||
assert_eq!(s.execute(CMD, json!({"action": "list"})).unwrap()["count"], 1);
|
||||
}
|
||||
|
||||
/// Lock All on the only type layer: greyed on main, now enabled, and it lists nothing since a
|
||||
/// fully locked layer is not checked (its prompt is #14175).
|
||||
#[test]
|
||||
fn enabled_with_only_a_lock_all_type_layer_and_lists_nothing() {
|
||||
let mut s = blank();
|
||||
let id = s.execute("type.create", json!({"x": 10, "y": 50, "text": "Teh end", "size": 12})).unwrap()["layer"].clone();
|
||||
s.execute("layer.setProps", json!({"layer": id, "locked": true})).unwrap();
|
||||
assert!(s.is_enabled(CMD), "{:?}", s.disabled_reason(CMD));
|
||||
assert_eq!(s.execute(CMD, json!({"action": "list"})).unwrap()["count"], 0);
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
//! #14174: with Check All Layers on, Check Spelling starts at the active layer, goes up the stack
|
||||
//! and wraps to the bottom-most type layer, stopping just below the start; words within a layer
|
||||
//! come left to right. Whether hidden and locked layers are checked is #14175, so the order is
|
||||
//! checked on the layers listed, and the words on the visible unlocked ones.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use photocraft_doc::LayerId;
|
||||
|
||||
use super::enabled_tests::{CMD, blank};
|
||||
use crate::Session;
|
||||
|
||||
/// The issue's document, type layers bottom to top, then an empty pixel layer on top. The hidden
|
||||
/// and locked layers' texts are not given there; they hold typos so that listing them shows.
|
||||
const LAYERS: [(&str, &str); 10] = [
|
||||
("T1 typos", "Teh quick brwn fox"),
|
||||
("T2 repeat", "the the end"),
|
||||
("T3 clean", "hello world"),
|
||||
("T4 hidden", "frist hidden"),
|
||||
("T5 locked", "lockd text"),
|
||||
("T6 para", "paragrph text here"),
|
||||
("T7 german", "Hallo Weltt"),
|
||||
("T8 repeat2", "brwn dog Teh"),
|
||||
("T9 hidden2", "secnd hidden"),
|
||||
("T10 locked2", "lockd again"),
|
||||
];
|
||||
|
||||
const HIDDEN: [&str; 2] = ["T4 hidden", "T9 hidden2"];
|
||||
|
||||
fn document() -> Session {
|
||||
let mut s = blank();
|
||||
for (i, (name, text)) in LAYERS.into_iter().enumerate() {
|
||||
let y = 30.0 + 40.0 * i as f64;
|
||||
let params = if name == "T6 para" {
|
||||
json!({"box": [10, y - 20.0, 400, 36], "text": text, "size": 12, "name": name})
|
||||
} else {
|
||||
json!({"x": 10, "y": y, "text": text, "size": 12, "name": name})
|
||||
};
|
||||
let id = s.execute("type.create", params).unwrap()["layer"].clone();
|
||||
if HIDDEN.contains(&name) {
|
||||
s.execute("layer.setProps", json!({"layer": id, "visible": false})).unwrap();
|
||||
}
|
||||
if name.contains("locked") {
|
||||
s.execute("layer.setProps", json!({"layer": id, "locked": true})).unwrap();
|
||||
}
|
||||
}
|
||||
s.execute("layer.new.layer", json!({"name": "Empty"})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn name_of(s: &Session, id: &Value) -> String {
|
||||
s.active().unwrap().doc.layer(LayerId(id.as_u64().unwrap())).unwrap().name.clone()
|
||||
}
|
||||
|
||||
/// (layer name, word) of each misspelling `{"action":"list"}` returns with `params` added.
|
||||
fn listed(s: &mut Session, params: Value) -> Vec<(String, String)> {
|
||||
let mut p = json!({"action": "list"});
|
||||
p.as_object_mut().unwrap().extend(params.as_object().unwrap().clone());
|
||||
let r = s.execute(CMD, p).unwrap();
|
||||
r["misspellings"].as_array().unwrap().iter().map(|m| (name_of(s, &m["layer"]), m["word"].as_str().unwrap().to_string())).collect()
|
||||
}
|
||||
|
||||
/// The list with `start` active and Check All Layers on (the default).
|
||||
fn check_from(s: &mut Session, start: &str) -> Vec<(String, String)> {
|
||||
s.execute("layer.select", json!({"layer": start})).unwrap();
|
||||
listed(s, json!({}))
|
||||
}
|
||||
|
||||
/// The words of the visible unlocked layers, as "layer: word". T7's "Hallo" is left out: only the
|
||||
/// English dictionary flags it, and checking T7 in German is #14189.
|
||||
fn words(list: &[(String, String)]) -> Vec<String> {
|
||||
list.iter()
|
||||
.filter(|(l, w)| !HIDDEN.contains(&l.as_str()) && !l.contains("locked") && !(l == "T7 german" && w == "Hallo"))
|
||||
.map(|(l, w)| format!("{l}: {w}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Asserts the listed layers come in `order` (a layer not listed is passed over).
|
||||
fn assert_layer_order(list: &[(String, String)], order: &[&str]) {
|
||||
let mut visited: Vec<&str> = list.iter().map(|(l, _)| l.as_str()).collect();
|
||||
visited.dedup();
|
||||
let expected: Vec<&str> = order.iter().copied().filter(|l| visited.contains(l)).collect();
|
||||
assert_eq!(visited, expected, "layers visited out of order: {list:?}");
|
||||
}
|
||||
|
||||
/// T1..T10 starting at `start` and wrapping.
|
||||
fn rotation(start: usize) -> Vec<&'static str> {
|
||||
LAYERS.iter().cycle().skip(start).take(LAYERS.len()).map(|(n, _)| *n).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn started_on_t3_goes_up_then_wraps_to_t1_and_t2() {
|
||||
let mut s = document();
|
||||
let list = check_from(&mut s, "T3 clean");
|
||||
assert_eq!(words(&list), ["T6 para: paragrph", "T7 german: Weltt", "T8 repeat2: brwn", "T8 repeat2: Teh", "T1 typos: Teh", "T1 typos: brwn"]);
|
||||
assert_layer_order(&list, &rotation(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn started_on_t6_checks_t6_t7_t8_then_t1() {
|
||||
let mut s = document();
|
||||
let list = check_from(&mut s, "T6 para");
|
||||
assert_eq!(words(&list), ["T6 para: paragrph", "T7 german: Weltt", "T8 repeat2: brwn", "T8 repeat2: Teh", "T1 typos: Teh", "T1 typos: brwn"]);
|
||||
assert_layer_order(&list, &rotation(5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn started_on_t1_goes_bottom_up_with_words_left_to_right() {
|
||||
let mut s = document();
|
||||
let list = check_from(&mut s, "T1 typos");
|
||||
assert_eq!(list.first().map(|(l, w)| (l.as_str(), w.as_str())), Some(("T1 typos", "Teh")), "main started at \"secnd\" in T9 hidden2");
|
||||
assert_eq!(words(&list), ["T1 typos: Teh", "T1 typos: brwn", "T6 para: paragrph", "T7 german: Weltt", "T8 repeat2: brwn", "T8 repeat2: Teh"]);
|
||||
assert_layer_order(&list, &rotation(0));
|
||||
}
|
||||
|
||||
/// The top type layer is T10 locked2: after it the check starts again at the bottom.
|
||||
#[test]
|
||||
fn started_on_the_top_type_layer_goes_on_from_the_bottom_up() {
|
||||
let mut s = document();
|
||||
let list = check_from(&mut s, "T10 locked2");
|
||||
assert_eq!(list.first().map(|(l, w)| (l.as_str(), w.as_str())), Some(("T1 typos", "Teh")));
|
||||
assert_layer_order(&list, &rotation(9));
|
||||
}
|
||||
|
||||
/// T8 is the top unlocked type layer: it comes first, then T1 upward.
|
||||
#[test]
|
||||
fn started_on_t8_checks_it_first_then_from_the_bottom() {
|
||||
let mut s = document();
|
||||
let list = check_from(&mut s, "T8 repeat2");
|
||||
assert_eq!(words(&list), ["T8 repeat2: brwn", "T8 repeat2: Teh", "T1 typos: Teh", "T1 typos: brwn", "T6 para: paragrph", "T7 german: Weltt"]);
|
||||
assert_layer_order(&list, &rotation(7));
|
||||
}
|
||||
|
||||
/// `from` names the start for a caller whose active layer has moved on, as the dialog does.
|
||||
#[test]
|
||||
fn from_sets_the_start_whatever_layer_is_active() {
|
||||
let mut s = document();
|
||||
s.execute("layer.select", json!({"layer": "T6 para"})).unwrap();
|
||||
let t6 = s.active().unwrap().active_layer.unwrap().0;
|
||||
s.execute("layer.select", json!({"layer": "T1 typos"})).unwrap();
|
||||
let list = listed(&mut s, json!({"from": t6}));
|
||||
assert_eq!(list.first().map(|(l, _)| l.as_str()), Some("T6 para"));
|
||||
assert_layer_order(&list, &rotation(5));
|
||||
assert!(s.execute(CMD, json!({"action": "list", "from": "T6 para"})).is_err(), "`from` takes an id");
|
||||
}
|
||||
|
||||
/// Not measured: an active layer that is not a type layer keeps today's top-to-bottom order.
|
||||
#[test]
|
||||
fn a_pixel_or_background_start_keeps_the_top_to_bottom_order() {
|
||||
let mut s = document();
|
||||
let mut top_down: Vec<&str> = LAYERS.iter().map(|(n, _)| *n).collect();
|
||||
top_down.reverse();
|
||||
for start in ["Empty", "Background"] {
|
||||
let list = check_from(&mut s, start);
|
||||
assert_eq!(words(&list).first().map(String::as_str), Some("T8 repeat2: brwn"), "{start}");
|
||||
assert_layer_order(&list, &top_down);
|
||||
}
|
||||
}
|
||||
|
||||
/// `from` null, an id that is no layer or a pixel layer's id: top to bottom, as without a type layer
|
||||
/// to start at, whatever layer is active.
|
||||
#[test]
|
||||
fn a_null_unknown_or_pixel_from_checks_top_to_bottom() {
|
||||
let mut s = document();
|
||||
let mut top_down: Vec<&str> = LAYERS.iter().map(|(n, _)| *n).collect();
|
||||
top_down.reverse();
|
||||
s.execute("layer.select", json!({"layer": "Empty"})).unwrap();
|
||||
let pixel = s.active().unwrap().active_layer.unwrap().0;
|
||||
s.execute("layer.select", json!({"layer": "T1 typos"})).unwrap();
|
||||
for from in [json!(null), json!(999_999), json!(pixel)] {
|
||||
let list = listed(&mut s, json!({"from": from}));
|
||||
assert_eq!(words(&list).first().map(String::as_str), Some("T8 repeat2: brwn"), "from {from}");
|
||||
assert_layer_order(&list, &top_down);
|
||||
}
|
||||
}
|
||||
|
||||
/// The issue's one measurement with several layers selected: T8 active with T1 also selected
|
||||
/// starts at T8. (Here `add` makes the added layer active, so T1 is selected first.)
|
||||
#[test]
|
||||
fn with_t1_and_t8_selected_and_t8_active_the_check_starts_at_t8() {
|
||||
let mut s = document();
|
||||
s.execute("layer.select", json!({"layer": "T1 typos"})).unwrap();
|
||||
s.execute("layer.select", json!({"layer": "T8 repeat2", "mode": "add"})).unwrap();
|
||||
assert_eq!(s.active().unwrap().selected_layers().len(), 2);
|
||||
let list = listed(&mut s, json!({}));
|
||||
assert_eq!(words(&list).first().map(String::as_str), Some("T8 repeat2: brwn"));
|
||||
assert_layer_order(&list, &rotation(7));
|
||||
}
|
||||
|
||||
/// Check All Layers off still checks the active layer alone.
|
||||
#[test]
|
||||
fn one_layer_check_lists_only_that_layer() {
|
||||
let mut s = document();
|
||||
s.execute("layer.select", json!({"layer": "T8 repeat2"})).unwrap();
|
||||
assert_eq!(words(&listed(&mut s, json!({"allLayers": false}))), ["T8 repeat2: brwn", "T8 repeat2: Teh"]);
|
||||
}
|
||||
@@ -47,6 +47,10 @@ pub struct TypePanelsUi {
|
||||
/// `panel_docking::command`.
|
||||
pub fn menu(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option<Result<Value, String>> {
|
||||
if id == "edit.checkSpelling" && params.get("action").is_none() && params.get("ui").is_none_or(|u| u.as_bool() != Some(false)) {
|
||||
// A document without any type layer opens no dialog and shows nothing, as in the reference (#14172).
|
||||
if app.session.active().is_some_and(|st| !photocraft_engine::type_extra_cmds::has_type_layers(&st.doc)) {
|
||||
return Some(app.session.execute("edit.checkSpelling", json!({"action": "list"})).map_err(|e| e.to_string()));
|
||||
}
|
||||
return Some(open_spelling(app, params.get("allLayers").and_then(Value::as_bool).unwrap_or(true)));
|
||||
}
|
||||
let paragraph = match id {
|
||||
@@ -80,3 +84,7 @@ fn target(app: &PhotocraftApp) -> Value {
|
||||
}
|
||||
json!({})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "type_spell_enabled_ui_tests.rs"]
|
||||
mod spell_enabled_ui_tests;
|
||||
@@ -31,10 +31,13 @@ pub struct SpellDialog {
|
||||
}
|
||||
|
||||
/// The engine's misspellings, as the raw JSON items of `edit.checkSpelling`. A single-layer check without a layer
|
||||
/// lists nothing: an absent opening layer must not adopt the newly active one.
|
||||
/// lists nothing: an absent opening layer must not adopt the newly active one. Check All Layers starts at the opening
|
||||
/// layer, or top to bottom (null) without one (#14174).
|
||||
fn list(app: &mut PhotocraftApp, all_layers: bool, layer: Option<LayerId>, ignore: &[String]) -> Vec<Value> {
|
||||
let mut p = json!({ "action": "list", "allLayers": all_layers, "ignore": ignore, "suggestions": 8 });
|
||||
if !all_layers {
|
||||
if all_layers {
|
||||
p["from"] = json!(layer.map(|l| l.0));
|
||||
} else {
|
||||
let Some(layer) = layer else { return Vec::new() };
|
||||
p["layer"] = json!(layer.0);
|
||||
}
|
||||
@@ -295,3 +298,7 @@ mod tests {
|
||||
assert_eq!(action.dialogs.iter().copied().collect::<Vec<_>>(), [0]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../type_spell_order_ui_tests.rs"]
|
||||
mod spell_order_ui_tests;
|
||||
@@ -0,0 +1,65 @@
|
||||
//! #14172: Edit > Check Spelling... chosen from the menu in a document without type layers is
|
||||
//! enabled and opens nothing: no dialog, no message, no history state. With no document open the
|
||||
//! item is greyed.
|
||||
|
||||
use craft::text::ui::spelling::CheckSpellingDialog;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
|
||||
/// An app with `doc` open (or none): a white 800 x 500 document with an empty layer on top.
|
||||
pub(super) fn app(doc: bool) -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
if doc {
|
||||
app.run("file.new", json!({"width": 800, "height": 500, "background": "white"})).unwrap();
|
||||
app.run("layer.new.layer", json!({"name": "Empty"})).unwrap();
|
||||
}
|
||||
app.sync_views();
|
||||
app
|
||||
}
|
||||
|
||||
/// Whether Edit > Check Spelling... is enabled in the menu.
|
||||
pub(super) fn enabled(app: &PhotocraftApp) -> bool {
|
||||
crate::menus::is_enabled(app, "edit.checkSpelling")
|
||||
}
|
||||
|
||||
/// Chooses Edit > Check Spelling... as the menu does.
|
||||
pub(super) fn choose(app: &mut PhotocraftApp) -> Result<serde_json::Value, String> {
|
||||
let ctx = app.ctx().clone();
|
||||
crate::menus::invoke(app, &ctx, "edit.checkSpelling", json!({}))
|
||||
}
|
||||
|
||||
fn history(app: &PhotocraftApp) -> Vec<String> {
|
||||
app.session.active().unwrap().history.entries()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_spelling_without_type_layers_is_enabled_and_opens_nothing() {
|
||||
let mut app = app(true);
|
||||
let before = history(&app);
|
||||
assert!(enabled(&app), "Check Spelling... is enabled without type layers");
|
||||
choose(&mut app).unwrap();
|
||||
assert!(app.ui.type_panels.spell.is_none(), "no Check Spelling dialog");
|
||||
assert!(app.framework.dialogs.is_empty(), "no dialog at all");
|
||||
assert!(app.ui.status.is_empty() && !app.ui.status_error, "no message: {:?}", app.ui.status);
|
||||
assert_eq!(history(&app), before, "no history state");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_spelling_is_greyed_without_a_document() {
|
||||
assert!(!enabled(&app(false)));
|
||||
}
|
||||
|
||||
/// Lock All on the only type layer is not measured: the menu keeps today's dialog, which opens
|
||||
/// with nothing listed (the hidden and locked layer prompts are #14175).
|
||||
#[test]
|
||||
fn check_spelling_with_only_a_lock_all_type_layer_opens_an_empty_dialog() {
|
||||
let mut app = app(true);
|
||||
let id = app.run("type.create", json!({"x": 10, "y": 60, "text": "Teh end"})).unwrap()["layer"].clone();
|
||||
app.run("layer.setProps", json!({"layer": id, "locked": true})).unwrap();
|
||||
assert!(enabled(&app), "Check Spelling... is enabled");
|
||||
choose(&mut app).unwrap();
|
||||
assert!(app.ui.type_panels.spell.is_some(), "the Check Spelling dialog opens");
|
||||
let dialog = app.framework.dialogs.get::<CheckSpellingDialog>().expect("the hosted dialog");
|
||||
assert!(dialog.items().is_empty(), "with nothing listed: {:?}", dialog.items());
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
//! #14174: the Check Spelling dialog opened from the menu with Check All Layers on starts at the
|
||||
//! active layer, goes up and wraps, and keeps that start when a Change refreshes the list after
|
||||
//! another layer became active.
|
||||
|
||||
use craft::text::ui::spelling::CheckSpellingDialog;
|
||||
use craft::ui::dialog::StackDialog as _;
|
||||
use serde_json::json;
|
||||
|
||||
use super::super::spell_enabled_ui_tests::{app, choose};
|
||||
use super::*;
|
||||
|
||||
fn dialog(app: &PhotocraftApp) -> &CheckSpellingDialog {
|
||||
app.framework.dialogs.get::<CheckSpellingDialog>().expect("the Check Spelling dialog is open")
|
||||
}
|
||||
|
||||
/// The dialog's words, in its order.
|
||||
fn words(app: &PhotocraftApp) -> Vec<String> {
|
||||
dialog(app).items().iter().map(|i| i.word.clone()).collect()
|
||||
}
|
||||
|
||||
/// The layers of the dialog's words, in its order.
|
||||
fn layers(app: &PhotocraftApp) -> Vec<u64> {
|
||||
dialog(app).items().iter().map(|i| i.object).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dialog_starts_at_the_opening_layer_and_keeps_it_after_a_change() {
|
||||
let mut app = app(true);
|
||||
let bottom = app.run("type.create", json!({"x": 10, "y": 60, "text": "Teh quik one"})).unwrap()["layer"].as_u64().unwrap();
|
||||
app.run("type.create", json!({"x": 10, "y": 120, "text": "hello world"})).unwrap();
|
||||
let top = app.run("type.create", json!({"x": 10, "y": 180, "text": "brwn dog"})).unwrap()["layer"].as_u64().unwrap();
|
||||
app.run("layer.select", json!({"layer": bottom})).unwrap();
|
||||
choose(&mut app).unwrap();
|
||||
assert_eq!(words(&app), ["Teh", "quik", "brwn"], "from the bottom layer up, not from the top");
|
||||
|
||||
// The host makes the top layer active while the dialog is open; Change refreshes the list.
|
||||
app.run("layer.select", json!({"layer": top})).unwrap();
|
||||
let item = dialog(&app).items()[0].clone();
|
||||
change(&mut app, &item, "The");
|
||||
let items = relist(&mut app);
|
||||
app.framework.dialogs.get_mut::<CheckSpellingDialog>().unwrap().set_items(items);
|
||||
assert_eq!(words(&app), ["quik", "brwn"], "the start stays the layer the dialog was opened on");
|
||||
assert_eq!(text_of(&app, bottom), "The quik one", "Change replaced \"Teh\"");
|
||||
}
|
||||
|
||||
/// Bottom "Teh one" and top "brwn dog" over the empty layer; returns their ids.
|
||||
fn two_layers() -> (Box<PhotocraftApp>, u64, u64) {
|
||||
let mut app = app(true);
|
||||
let bottom = app.run("type.create", json!({"x": 10, "y": 60, "text": "Teh one"})).unwrap()["layer"].as_u64().unwrap();
|
||||
let top = app.run("type.create", json!({"x": 10, "y": 120, "text": "brwn dog"})).unwrap()["layer"].as_u64().unwrap();
|
||||
(app, bottom, top)
|
||||
}
|
||||
|
||||
/// Opened with no layer active, Check All Layers goes top to bottom, and selecting a layer later
|
||||
/// does not move the start there.
|
||||
#[test]
|
||||
fn dialog_opened_without_an_active_layer_keeps_the_top_to_bottom_order() {
|
||||
let (mut app, bottom, top) = two_layers();
|
||||
app.run("select.deselectLayers", json!({})).unwrap();
|
||||
open_spelling(&mut app, true).unwrap();
|
||||
app.run("layer.select", json!({"layer": bottom})).unwrap();
|
||||
let first = relist(&mut app).first().map(|i| i.object);
|
||||
assert_eq!(first, Some(top), "the top layer comes first");
|
||||
}
|
||||
|
||||
/// Opened with Check All Layers off on the bottom layer, then turned on after the top layer became
|
||||
/// active: the check starts at the opening layer.
|
||||
#[test]
|
||||
fn turning_check_all_layers_on_starts_at_the_opening_layer() {
|
||||
let (mut app, bottom, top) = two_layers();
|
||||
let ctx = app.ctx().clone();
|
||||
app.run("layer.select", json!({"layer": bottom})).unwrap();
|
||||
open_spelling(&mut app, false).unwrap();
|
||||
app.run("layer.select", json!({"layer": top})).unwrap();
|
||||
app.framework.dialogs.get_mut::<CheckSpellingDialog>().unwrap().set_json_field(&ctx, "allLayers", json!(true)).unwrap();
|
||||
take(&mut app, &ctx);
|
||||
assert_eq!(layers(&app), [bottom, top]);
|
||||
}
|
||||
Reference in new issue
Block a user