mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 07:49:26 +00:00
Merge pull request #7543 from dotCooCoo/fix/dialogs-keep-preview-warning
Type: typing kept when a dialog or a panel previews keeps its warning and selection
This commit is contained in:
@@ -47,18 +47,22 @@ fn insert_inline(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let len = edit::runs_len(&t.runs);
|
||||
let from_tool = editing == Some(id);
|
||||
let (a, b) = match p.get("at") {
|
||||
None | Some(Value::Null) if from_tool => {
|
||||
let g = |k: &str| tool.get(k).and_then(Value::as_u64).map_or(len, |v| (v as usize).min(len));
|
||||
let (x, y) = (g("start"), g("end"));
|
||||
(x.min(y), x.max(y))
|
||||
}
|
||||
None | Some(Value::Null) => (len, len),
|
||||
Some(v) => {
|
||||
let at = v.as_u64().ok_or_else(|| bad(C, "`at` must be a byte offset"))?.min(len as u64) as usize;
|
||||
(at, at)
|
||||
}
|
||||
// Where the graphic goes in text `len` bytes long.
|
||||
let range = |len: usize| -> Result<(usize, usize)> {
|
||||
Ok(match p.get("at") {
|
||||
None | Some(Value::Null) if from_tool => {
|
||||
let g = |k: &str| tool.get(k).and_then(Value::as_u64).map_or(len, |v| (v as usize).min(len));
|
||||
let (x, y) = (g("start"), g("end"));
|
||||
(x.min(y), x.max(y))
|
||||
}
|
||||
None | Some(Value::Null) => (len, len),
|
||||
Some(v) => {
|
||||
let at = v.as_u64().ok_or_else(|| bad(C, "`at` must be a byte offset"))?.min(len as u64) as usize;
|
||||
(at, at)
|
||||
}
|
||||
})
|
||||
};
|
||||
let (mut a, mut b) = range(len)?;
|
||||
let symbol = match str_param(p, "symbol") {
|
||||
Some(n) if doc.symbols.iter().any(|s| s.name == n) => n.to_string(),
|
||||
Some(n) => return Err(bad(C, format!("no symbol named `{n}`"))),
|
||||
@@ -76,11 +80,16 @@ fn insert_inline(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
None | Some(Value::Null) => 0.0,
|
||||
Some(v) => v.as_f64().filter(|k| k.is_finite() && k.abs() <= 1e5).ok_or_else(|| bad(C, "`shift` must be a number of points"))?,
|
||||
};
|
||||
// A typing session ends first: the insertion is a step of its own.
|
||||
// A typing session ends first: the insertion is a step of its own. Typing that Real-time
|
||||
// Drawing off left unapplied is applied first, and the caret is read in the typed text.
|
||||
let typing = tool.get("typing").and_then(Value::as_bool) == Some(true);
|
||||
if typing && from_tool {
|
||||
s.set_tool_option("commitTyping", &Value::Bool(true));
|
||||
s.commit_interaction()?;
|
||||
super::settle_interaction(s)?;
|
||||
let NodeKind::Text(t) = &s.doc()?.doc.node(id).ok_or(EngineError::NoNode(id))?.kind else {
|
||||
return Err(bad(C, format!("node {} is not text", id.0)));
|
||||
};
|
||||
(a, b) = range(edit::runs_len(&t.runs))?;
|
||||
}
|
||||
let caret = s.edit("Insert Inline Symbol", |d, _| {
|
||||
let mut art = InlineArt { scale, baseline_shift: shift, ..InlineArt::new(symbol.clone()) };
|
||||
|
||||
@@ -1883,13 +1883,14 @@ enum Step {
|
||||
BaselineShift,
|
||||
}
|
||||
|
||||
/// The Type tool's selected text while it edits (its typing session ends first: one undo step).
|
||||
/// The Type tool's selected text while it edits (its typing session ends first: one undo step,
|
||||
/// with typing that Real-time Drawing off left unapplied applied first).
|
||||
pub(crate) fn editing_range(s: &mut Session) -> Result<Option<(NodeId, usize, usize)>> {
|
||||
let o = s.tool_options();
|
||||
let Some(id) = o.get("editing").and_then(Value::as_u64).map(NodeId) else { return Ok(None) };
|
||||
if super::edit::typing_in_progress(s) {
|
||||
s.set_tool_option("commitTyping", &Value::Bool(true));
|
||||
s.commit_interaction()?;
|
||||
super::edit::settle_interaction(s)?;
|
||||
}
|
||||
let at = |k: &str| byte_param(&o, k).unwrap_or(0);
|
||||
Ok(Some((id, at("start"), at("end"))))
|
||||
|
||||
@@ -255,6 +255,21 @@ impl Session {
|
||||
(r, settled)
|
||||
}
|
||||
|
||||
/// [`Session::begin_interaction`] for a dialog or a panel that starts a preview → the warnings
|
||||
/// of the preview that keeping typing in progress applied, for its status bar.
|
||||
pub fn begin_interaction_warned(&mut self, label: &str) -> Result<Vec<String>> {
|
||||
let (r, settled) = self.collect_settled_warnings(|s| s.begin_interaction(label));
|
||||
r.map(|()| settled)
|
||||
}
|
||||
|
||||
/// Keep typing in progress as one undo step, applying the preview Real-time Drawing off left
|
||||
/// unapplied, and end the Type tool's typing session → the applied preview's warnings. Nothing
|
||||
/// changes when no typing is in progress.
|
||||
pub fn keep_typing(&mut self) -> Result<Vec<String>> {
|
||||
let (r, settled) = self.collect_settled_warnings(|s| if cmd::typing_in_progress(s) { cmd::settle_interaction(s) } else { Ok(()) });
|
||||
r.map(|()| settled)
|
||||
}
|
||||
|
||||
pub fn begin_interaction(&mut self, label: &str) -> Result<()> {
|
||||
if self.script_running() {
|
||||
return Err(EngineError::Busy);
|
||||
|
||||
@@ -146,6 +146,31 @@ fn type_tool_inserts_at_its_caret_and_deletes_the_graphic_whole() {
|
||||
assert_eq!(inline_count(&obj(&s, id)), 1);
|
||||
}
|
||||
|
||||
/// With Performance › Real-time Drawing and Editing off, the typing waits for its session to end:
|
||||
/// a graphic inserted while typing applies it first, as a step of its own, and goes in at the
|
||||
/// caret after it.
|
||||
#[test]
|
||||
fn inserting_while_deferred_typing_keeps_the_typing() {
|
||||
let mut s = session();
|
||||
s.prefs.real_time_drawing = false;
|
||||
// The new type starts empty: the caret after "Pay " is past the end of the text until the
|
||||
// typing is applied.
|
||||
s.prefs.placeholder_text = false;
|
||||
circle_symbol(&mut s, "Mana", 10.0);
|
||||
s.execute("symbol.setCurrent", &json!({"name": "Mana"})).unwrap();
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("type", v).unwrap();
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 50.0, 300.0), v).unwrap();
|
||||
}
|
||||
let id = s.doc().unwrap().selection.objects[0];
|
||||
s.tool_text("Pay ", v).unwrap();
|
||||
s.execute("text.insertInline", &json!({})).unwrap();
|
||||
assert_eq!(obj(&s, id).plain_text(), format!("Pay {INLINE_CHAR}"));
|
||||
let labels: Vec<String> = s.doc().unwrap().history.undo.iter().map(|e| e.label.clone()).collect();
|
||||
assert_eq!(labels[labels.len() - 2..], ["Typing", "Insert Inline Symbol"], "{labels:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_graphics_follow_their_symbol() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -2606,6 +2606,35 @@ fn settling_typing_for_a_tool_shows_its_preview_warning() {
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 50.0, 380.0).with_mods(cmd), ViewInfo::default()).unwrap();
|
||||
}
|
||||
|
||||
/// Typing kept as its own step when a dialog or a panel begins its preview, or when the font menus
|
||||
/// end the typing session, returns its applied preview's warning for the status bar. A preview
|
||||
/// without a warning returns none, and with no typing in progress nothing is kept and no step is
|
||||
/// added.
|
||||
#[test]
|
||||
fn settling_typing_for_a_dialog_returns_its_preview_warning() {
|
||||
let warned = |w: &[String]| w.iter().any(|m| m.contains("type"));
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let mut s = deferred_bloat("Typing", true, "selection");
|
||||
let before = steps(&s);
|
||||
let w = s.begin_interaction_warned("Offset Path").unwrap();
|
||||
assert!(warned(&w), "{w:?}");
|
||||
assert_eq!(steps(&s), before + 1, "the typing is a step of its own");
|
||||
assert_eq!(s.doc().unwrap().interaction.as_ref().map(|it| it.label.as_str()), Some("Offset Path"));
|
||||
|
||||
let mut s = deferred_bloat("Typing", true, "selection");
|
||||
let w = s.keep_typing().unwrap();
|
||||
assert!(warned(&w), "{w:?}");
|
||||
assert!(!s.in_interaction());
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
let w = s.keep_typing().unwrap();
|
||||
assert!(w.is_empty(), "{w:?}");
|
||||
assert_eq!(steps(&s), before + 1, "nothing in progress adds no step");
|
||||
|
||||
let mut s = deferred_bloat("Typing", false, "selection");
|
||||
let w = s.begin_interaction_warned("Offset Path").unwrap();
|
||||
assert!(w.is_empty(), "{w:?}");
|
||||
}
|
||||
|
||||
/// File Handling › Use Low Resolution Proxy for Linked EPS (#394): off by default; toggling it does
|
||||
/// not replace full-resolution pixels already in the document.
|
||||
#[test]
|
||||
|
||||
@@ -3173,6 +3173,41 @@ fn a_previewed_transform_while_typing_keeps_the_typing() {
|
||||
assert!((left(&s, id) - 400.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
/// With Performance › Real-time Drawing and Editing off, the typing is applied when its session
|
||||
/// ends. Ending it for another edit applies it first, as a step of its own: a dialog or a panel
|
||||
/// beginning its preview, the font menus ([`Session::keep_typing`]), a paint for the selected
|
||||
/// characters.
|
||||
#[test]
|
||||
fn ending_deferred_typing_for_another_edit_keeps_the_typing() {
|
||||
let typed = |end: &dyn Fn(&mut Session)| {
|
||||
let mut s = session();
|
||||
s.prefs.real_time_drawing = false;
|
||||
let v = ViewInfo::default();
|
||||
let id = text(&mut s, "Hello");
|
||||
s.select_tool("type", v).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
|
||||
click(&mut s, b.x1 - 1.0, b.center().y);
|
||||
s.tool_text(" world", v).unwrap();
|
||||
assert_eq!(obj(&s, id).plain_text(), "Hello", "the typing waits for its session to end");
|
||||
let steps = undo_labels(&s).len();
|
||||
end(&mut s);
|
||||
assert_eq!(obj(&s, id).plain_text(), "Hello world");
|
||||
assert_eq!(undo_labels(&s)[steps], "Typing", "{:?}", undo_labels(&s));
|
||||
};
|
||||
typed(&|s| {
|
||||
assert!(s.begin_interaction_warned("Fill Color").unwrap().is_empty());
|
||||
s.preview("paint.setFill", &json!({"color": "#00a000"})).unwrap();
|
||||
s.commit_interaction().unwrap();
|
||||
});
|
||||
typed(&|s| {
|
||||
assert!(s.keep_typing().unwrap().is_empty());
|
||||
assert!(!s.in_interaction());
|
||||
});
|
||||
typed(&|s| {
|
||||
s.execute("paint.setFill", &json!({"color": "#00a000"})).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
/// #1950: a colour previewed while typing (the Color Picker, a Color panel slider) colours the
|
||||
/// typed text: its interaction used to join the typing session and preview from the document
|
||||
/// before the typing, so the new type came back as its placeholder text.
|
||||
|
||||
Reference in New Issue
Block a user