mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 15:26:12 +00:00
Merge pull request #8137 from dotCooCoo/perf/symbols-panel-current-symbol
Symbols: the panel reads the current symbol without running symbol.list
This commit is contained in:
@@ -893,6 +893,10 @@ pub use buildcmds::live_paint_gaps;
|
||||
/// Control bar enable it when it changes one).
|
||||
pub use brushsym::reset_transformation_changes;
|
||||
|
||||
/// The Symbols panel's current symbol, as `symbol.list` answers it (the panel reads it on every
|
||||
/// frame without running the command).
|
||||
pub use brushsym::current_symbol;
|
||||
|
||||
/// The byte order of UTF-16 text without a byte-order mark, by Import Data Sets' rule (the Graph
|
||||
/// Data window reads files by it too).
|
||||
pub use variablecmds::utf16_order_without_mark;
|
||||
|
||||
@@ -27,6 +27,9 @@ use crate::EngineError;
|
||||
/// Whether Reset Transformation changes a symbol instance.
|
||||
pub use symbols::reset_transformation_changes;
|
||||
|
||||
/// The Symbols panel's current symbol, as `symbol.list` answers it.
|
||||
pub use symbols::current_symbol;
|
||||
|
||||
mod brushes;
|
||||
mod dynamic;
|
||||
mod symbols;
|
||||
|
||||
@@ -162,7 +162,7 @@ pub(super) fn symbol_sort(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
if d.symbols.is_sorted_by_key(key) {
|
||||
return ok();
|
||||
}
|
||||
let current = current_symbol(d).or_else(|| symbol_names(d).into_iter().next());
|
||||
let current = current_symbol(d);
|
||||
s.edit("Sort by Name", |d, _| {
|
||||
if let Some(name) = current {
|
||||
d.unknown.insert(CURRENT_SYMBOL.into(), json!(name));
|
||||
@@ -173,12 +173,15 @@ pub(super) fn symbol_sort(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
ok()
|
||||
}
|
||||
|
||||
fn symbol_names(d: &Document) -> Vec<String> {
|
||||
d.symbols.iter().map(|s| s.name.clone()).collect()
|
||||
}
|
||||
|
||||
fn current_symbol(d: &Document) -> Option<String> {
|
||||
d.unknown.get(CURRENT_SYMBOL).and_then(Value::as_str).filter(|n| d.symbols.iter().any(|s| s.name == *n)).map(str::to_string)
|
||||
/// The Symbols panel's current symbol, as `symbol.list` answers it: the stored current symbol
|
||||
/// while `d` has a symbol of that name, else the first symbol; `None` when `d` has no symbols.
|
||||
pub fn current_symbol(d: &Document) -> Option<String> {
|
||||
d.unknown
|
||||
.get(CURRENT_SYMBOL)
|
||||
.and_then(Value::as_str)
|
||||
.filter(|n| d.symbols.iter().any(|s| s.name == *n))
|
||||
.or_else(|| d.symbols.first().map(|s| s.name.as_str()))
|
||||
.map(str::to_string)
|
||||
}
|
||||
|
||||
pub(super) fn symbol_list(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
@@ -200,7 +203,7 @@ pub(super) fn symbol_list(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
Ok(json!({ "symbols": list, "current": current_symbol(d).or_else(|| symbol_names(d).into_iter().next()) }))
|
||||
Ok(json!({ "symbols": list, "current": current_symbol(d) }))
|
||||
}
|
||||
|
||||
/// Store `art` ([`normalize`]d, of natural size `size`) as symbol `name`, which becomes the
|
||||
@@ -398,9 +401,7 @@ fn resolve_symbol(d: &Document, p: &Value, cmd: &str) -> Result<String> {
|
||||
match str_param(p, "name") {
|
||||
Some(n) if d.symbols.iter().any(|s| s.name == n) => Ok(n.to_string()),
|
||||
Some(n) => Err(bad(cmd, format!("no symbol named `{n}`"))),
|
||||
None => {
|
||||
current_symbol(d).or_else(|| symbol_names(d).into_iter().next()).ok_or_else(|| EngineError::Other("the document has no symbols".into()))
|
||||
}
|
||||
None => current_symbol(d).ok_or_else(|| EngineError::Other("the document has no symbols".into())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use vectorcraft_doc::text::INLINE_MAX_SCALE;
|
||||
use vectorcraft_doc::{Document, InlineArt, NodeId, NodeKind, TextRun};
|
||||
use vectorcraft_text::edit;
|
||||
|
||||
use super::brushsym::current_symbol;
|
||||
use super::typecmd::refresh_bounds;
|
||||
use super::{CommandSpec, EngineError, Result, Session, bad, cmd, has_doc, id_param, str_param};
|
||||
|
||||
@@ -97,13 +98,3 @@ fn insert_inline(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
Ok(json!({"id": id.0, "caret": caret}))
|
||||
}
|
||||
|
||||
/// The Symbols panel's current symbol, else the first one.
|
||||
fn current_symbol(d: &Document) -> Option<String> {
|
||||
d.unknown
|
||||
.get("currentSymbol")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|n| d.symbols.iter().any(|s| s.name == *n))
|
||||
.map(str::to_string)
|
||||
.or_else(|| d.symbols.first().map(|s| s.name.clone()))
|
||||
}
|
||||
|
||||
@@ -2599,3 +2599,50 @@ fn symbol_sort_by_name_orders_the_symbols_in_one_undo_step() {
|
||||
s.execute("symbol.sortByName", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), before);
|
||||
}
|
||||
|
||||
/// `cmd::current_symbol` answers what `symbol.list` answers as `current`, and what `rule` (the
|
||||
/// stored name while a symbol has it, else the first of all the names) answers:
|
||||
/// with no symbols, with no stored name, with a stored name that is not first, after the current
|
||||
/// symbol is deleted, with a stored name no symbol has and with a stored value that is not text.
|
||||
#[test]
|
||||
fn current_symbol_is_the_current_symbol_list_answers() {
|
||||
fn rule(d: &Document) -> Option<String> {
|
||||
let names: Vec<String> = d.symbols.iter().map(|s| s.name.clone()).collect();
|
||||
let stored = d.unknown.get("currentSymbol").and_then(Value::as_str).filter(|n| d.symbols.iter().any(|s| s.name == *n)).map(str::to_string);
|
||||
stored.or_else(|| names.into_iter().next())
|
||||
}
|
||||
fn check(s: &mut Session, want: Option<&str>, case: &str) {
|
||||
let listed = s.execute("symbol.list", &json!({})).unwrap()["current"].clone();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let helper = crate::cmd::current_symbol(d);
|
||||
assert_eq!(helper.as_deref(), want, "{case}");
|
||||
assert_eq!(json!(helper), listed, "{case}: symbol.list");
|
||||
assert_eq!(helper, rule(d), "{case}: the rule");
|
||||
}
|
||||
fn store(s: &mut Session, v: Value) {
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.insert("currentSymbol".into(), v);
|
||||
}
|
||||
let mut s = session();
|
||||
check(&mut s, None, "no symbols");
|
||||
store(&mut s, json!("Gone"));
|
||||
check(&mut s, None, "no symbols and a stored name");
|
||||
for (i, name) in ["A", "B", "C"].into_iter().enumerate() {
|
||||
rect(&mut s, 20.0 * i as f64, 0.0, 10.0, 10.0);
|
||||
s.execute("symbol.new", &json!({"name": name})).unwrap();
|
||||
}
|
||||
check(&mut s, Some("C"), "the newest symbol is current");
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).unknown.remove("currentSymbol");
|
||||
check(&mut s, Some("A"), "no stored name");
|
||||
s.execute("symbol.setCurrent", &json!({"name": "B"})).unwrap();
|
||||
check(&mut s, Some("B"), "a stored name that is not first");
|
||||
s.execute("symbol.delete", &json!({"name": "B"})).unwrap();
|
||||
check(&mut s, Some("A"), "the current symbol deleted");
|
||||
store(&mut s, json!("Renamed"));
|
||||
check(&mut s, Some("A"), "a stored name no symbol has");
|
||||
store(&mut s, json!(3));
|
||||
check(&mut s, Some("A"), "a stored number");
|
||||
store(&mut s, json!(["C"]));
|
||||
check(&mut s, Some("A"), "a stored list");
|
||||
store(&mut s, json!("C"));
|
||||
check(&mut s, Some("C"), "a stored name that is last");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user