Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Integration: #1011 axes with an inverted range get no slider; a failed edit.preview closes the preview it opened

aiongg1ad5c18ef883
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index a32124ac1..9ee963317 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -397,13 +397,22 @@ fn preview(s: &mut Session, p: &Value) -> Result<Value> {
return Err(super::bad("edit.preview", format!("{command} is not an edit that can be previewed")));
}
let params = p.get("params").cloned().unwrap_or_else(|| json!({}));
- match s.doc()?.interaction.as_ref().map(|i| i.label.as_str()) {
- None => s.begin_interaction(label)?,
- Some(open) if open == label => {}
+ let opened = match s.doc()?.interaction.as_ref().map(|i| i.label.as_str()) {
+ None => {
+ s.begin_interaction(label)?;
+ true
+ }
+ Some(open) if open == label => false,
// A tool's drag (or another preview) is in progress: leave it alone.
Some(open) => return Err(super::bad("edit.preview", format!("{open} is in progress"))),
+ };
+ if let Err(e) = s.preview(command, params) {
+ // A preview this call opened must not stay open: later edits would get no undo step.
+ if opened {
+ s.cancel_interaction();
+ }
+ return Err(e);
}
- s.preview(command, params)?;
ok()
}
@@ -1016,6 +1025,19 @@ mod preview_tests {
assert_eq!(size(&mut s), before);
}
+ /// A preview whose command fails closes again, so the next edit is its own undo step.
+ #[test]
+ fn a_failed_preview_leaves_no_preview_open() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("edit.preview", &json!({"label": "Size", "command": "type.char", "params": {"attrs": {"size": 20}}}));
+ assert!(r.is_err(), "nothing selected: type.char fails");
+ assert!(s.doc().unwrap().interaction.is_none());
+ let steps = s.doc().unwrap().history.undo.len();
+ s.execute("frame.create", &json!({"rect": [72, 72, 144, 144]})).unwrap();
+ assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1);
+ }
+
#[test]
fn a_cancelled_preview_leaves_the_document_as_it_was() {
let mut s = text_session();
diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs
index d20109d6b..0daf680d7 100644
--- a/crates/ui-egui/src/panels/properties.rs
+++ b/crates/ui-egui/src/panels/properties.rs
@@ -2604,7 +2604,7 @@ fn variable_font_axes(app: &mut DesignApp, ui: &mut egui::Ui, family: &str, styl
return;
}
let db = super::fonts(app);
- let axes = db.axes(family, style);
+ let axes = usable_axes(db.axes(family, style));
if axes.is_empty() {
return;
}
@@ -2652,6 +2652,12 @@ fn variable_font_axes(app: &mut DesignApp, ui: &mut egui::Ui, family: &str, styl
/// The slider step and the field's decimals for an axis: whole units, or hundredths for an axis
/// whose whole range is a few units (Skia's weight runs 0.48–3.2).
+/// The axes a slider can show: a malformed font's axis whose minimum lies above its maximum (or
+/// isn't a number) is left out, since no value fits its range.
+fn usable_axes(axes: Vec<(String, String, f32, f32, f32)>) -> Vec<(String, String, f32, f32, f32)> {
+ axes.into_iter().filter(|a| a.2 <= a.4).collect()
+}
+
fn axis_step(min: f32, max: f32) -> (f32, usize) {
if max - min >= 10.0 { (1.0, 0) } else { (0.01, 2) }
}
@@ -2902,6 +2908,14 @@ mod tests {
text_attrs(app).unwrap()["chars"]["fontStyle"].as_str().unwrap().to_string()
}
+ #[test]
+ fn axes_with_an_inverted_or_missing_range_get_no_slider() {
+ let axis = |tag: &str, min: f32, max: f32| (tag.to_string(), tag.to_string(), min, min, max);
+ let kept = usable_axes(vec![axis("wght", 100.0, 900.0), axis("wdth", 125.0, 75.0), axis("opsz", f32::NAN, 72.0), axis("slnt", 0.0, 0.0)]);
+ let tags: Vec<&str> = kept.iter().map(|a| a.0.as_str()).collect();
+ assert_eq!(tags, ["wght", "slnt"]);
+ }
+
#[test]
fn variable_font_style_keeps_two_decimals_at_most() {
let values = [("wght".to_string(), 512.0_f32), ("wdth".to_string(), 1.25), ("opsz".to_string(), 0.4801)];