Relative line height and letter spacing edits with a unit suffix add to the current value (#1755)

Found while testing crates/ui-egui/src/panels/typography/spacing.rs:166 (parse_line_height, px branch; also :183 and :180 in parse_letter_spacing, px and % branches)

Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-31 10:37:09 -04:00
committed by GitHub
co-authored by Claude Haiku 5.5
parent 3b5de55587
commit fad47ac8bc
@@ -164,7 +164,11 @@ pub(crate) fn parse_line_height(s: &str, cur: Option<LineHeight>) -> Option<Valu
return widgets::eval(p.trim(), cur_pct).filter(|v| v.is_finite()).map(|v| json!(format!("{}%", trim_num(v.max(0.0)))));
}
if let Some(px) = s.strip_suffix("px") {
return widgets::eval(px.trim(), None).filter(|v| v.is_finite()).map(|v| json!(v.max(0.0)));
let cur_px = match cur {
Some(LineHeight::Pixels(p)) => Some(p),
_ => None,
};
return widgets::eval(px.trim(), cur_px).filter(|v| v.is_finite()).map(|v| json!(v.max(0.0)));
}
match cur {
Some(LineHeight::Percent(p)) => widgets::eval(s, Some(p)).filter(|v| v.is_finite()).map(|v| json!(format!("{}%", trim_num(v.max(0.0))))),
@@ -178,10 +182,18 @@ pub(crate) fn parse_line_height(s: &str, cur: Option<LineHeight>) -> Option<Valu
pub(crate) fn parse_letter_spacing(s: &str, cur: Option<LetterSpacing>) -> Option<Value> {
let s = s.trim();
if let Some(p) = s.strip_suffix('%') {
return widgets::eval(p.trim(), None).filter(|v| v.is_finite()).map(|v| json!(format!("{}%", trim_num(v))));
let cur_pct = match cur {
Some(LetterSpacing::Percent(p)) => Some(p),
_ => None,
};
return widgets::eval(p.trim(), cur_pct).filter(|v| v.is_finite()).map(|v| json!(format!("{}%", trim_num(v))));
}
if let Some(p) = s.strip_suffix("px") {
return widgets::eval(p.trim(), None).filter(|v| v.is_finite()).map(|v| json!(v));
let cur_px = match cur {
Some(LetterSpacing::Pixels(p)) => Some(p),
_ => None,
};
return widgets::eval(p.trim(), cur_px).filter(|v| v.is_finite()).map(|v| json!(v));
}
match cur {
Some(LetterSpacing::Pixels(px)) => widgets::eval(s, Some(px)).filter(|v| v.is_finite()).map(|v| json!(v)),
@@ -206,3 +218,21 @@ pub(crate) fn letter_spacing_of(v: &Value) -> Option<LetterSpacing> {
other => other.as_f64().filter(|v| v.is_finite()).map(LetterSpacing::Pixels),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relative_edit_with_unit_suffix_adds_to_current() {
let lh = Some(LineHeight::Pixels(10.0));
assert_eq!(parse_line_height("+2", lh), Some(json!(12.0)));
assert_eq!(parse_line_height("+2px", lh), Some(json!(12.0)));
let ls = Some(LetterSpacing::Pixels(10.0));
assert_eq!(parse_letter_spacing("+1", ls), Some(json!(11.0)));
assert_eq!(parse_letter_spacing("+1px", ls), Some(json!(11.0)));
let pct = Some(LetterSpacing::Percent(10.0));
assert_eq!(parse_letter_spacing("+1%", pct), parse_letter_spacing("+1", pct));
assert_eq!(parse_letter_spacing("+1%", pct), Some(json!("11%")));
}
}