mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-03 22:40:27 +00:00
Merge pull request #7454
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="20" height="20">
|
||||
<rect x="7.5" y="2.5" width="5" height="9.5" rx="1" fill="none" stroke="currentColor" stroke-width="1.25"/>
|
||||
<path d="M5 9.5 L5 11 L10 15 L15 11 L15 9.5" fill="none" stroke="currentColor" stroke-width="1.25"/>
|
||||
<path d="M10 15 L10 18" fill="none" stroke="currentColor" stroke-width="1.25"/>
|
||||
<rect x="7.5" y="2.5" width="5" height="9.5" rx="2.5" fill="none" stroke="currentColor" stroke-width="1.25"/>
|
||||
<path d="M5 9.5 L5 11 L10 15 L15 11 L15 9.5" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M10 15 L10 18" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round"/>
|
||||
</svg>
|
||||
|
||||
|
Before Width: | Height: | Size: 386 B After Width: | Height: | Size: 458 B |
@@ -636,4 +636,90 @@ mod tests {
|
||||
});
|
||||
assert!(!shapes.is_empty(), "{KEY} icon must paint");
|
||||
}
|
||||
|
||||
/// #7450: the Dictate microphone icon parses cleanly on a `0 0 20 20` viewBox, keeps a
|
||||
/// rounded capsule (`rx >= width / 2`) clear of the U-stand cradle, and renders at 16–22 pt.
|
||||
#[test]
|
||||
fn dictate_icon_geometry_and_rendering() {
|
||||
use craft::theme::{Contrast, Mode};
|
||||
use craft::ui::testing::{Frames, style};
|
||||
|
||||
let (_, bytes) = WORDCRAFT_ICONS.icons.iter().find(|(name, _)| *name == "dictate").expect("dictate is registered");
|
||||
let svg = std::str::from_utf8(bytes).expect("an SVG document is text");
|
||||
|
||||
let tree = craft::ui::primitives::svg_tree::parse(bytes).expect("dictate.svg must parse as a valid SVG document");
|
||||
let size = tree.size();
|
||||
assert_eq!(size.width(), 20.0, "icon viewBox width must be 20");
|
||||
assert_eq!(size.height(), 20.0, "icon viewBox height must be 20");
|
||||
|
||||
fn attr_str<'a>(tag: &'a str, name: &str) -> &'a str {
|
||||
let elem = tag.split('>').next().unwrap_or(tag);
|
||||
let needle = format!("{name}=\"");
|
||||
let start = elem
|
||||
.match_indices(&needle)
|
||||
.find(|&(idx, _)| idx == 0 || elem.as_bytes()[idx - 1].is_ascii_whitespace())
|
||||
.map(|(idx, _)| idx + needle.len())
|
||||
.unwrap_or_else(|| panic!("missing {name} on {elem}"));
|
||||
&elem[start..elem[start..].find('"').map_or(elem.len(), |i| start + i)]
|
||||
}
|
||||
let attr = |tag: &str, name: &str| -> f32 {
|
||||
let val = attr_str(tag, name);
|
||||
val.parse::<f32>().unwrap_or_else(|_| panic!("non-numeric {name}: {val}"))
|
||||
};
|
||||
|
||||
let root = svg.split("<svg").nth(1).expect("dictate.svg must contain a root <svg> element");
|
||||
assert_eq!(attr_str(root, "viewBox"), "0 0 20 20", "root SVG viewBox must be 0 0 20 20");
|
||||
|
||||
let rect = svg.split("<rect").nth(1).expect("dictate.svg must contain a <rect> capsule");
|
||||
let cap_x = attr(rect, "x");
|
||||
let cap_y = attr(rect, "y");
|
||||
let width = attr(rect, "width");
|
||||
let height = attr(rect, "height");
|
||||
let rx = attr(rect, "rx");
|
||||
let cap_stroke = attr(rect, "stroke-width");
|
||||
let cradle = svg.split("<path").nth(1).expect("dictate.svg must contain a cradle <path>");
|
||||
let cradle_stroke = attr(cradle, "stroke-width");
|
||||
let half_strokes = (cap_stroke + cradle_stroke) / 2.0;
|
||||
|
||||
assert!(rx >= width / 2.0, "capsule corner radius ({rx}) must be at least half the width ({width})");
|
||||
assert!(cap_y + height < 15.0, "capsule bottom ({}) must stay clear of the cradle bottom (15.0)", cap_y + height);
|
||||
assert!(cap_x > 5.0 && cap_x + width < 15.0, "capsule x-span ({}..{}) must stay inside the cradle arms (5..15)", cap_x, cap_x + width);
|
||||
|
||||
fn dist_point_to_seg(p: (f32, f32), a: (f32, f32), b: (f32, f32)) -> f32 {
|
||||
let (vx, vy) = (b.0 - a.0, b.1 - a.1);
|
||||
let len_sq = vx * vx + vy * vy;
|
||||
let t = if len_sq > 0.0 { (((p.0 - a.0) * vx + (p.1 - a.1) * vy) / len_sq).clamp(0.0, 1.0) } else { 0.0 };
|
||||
let (dx, dy) = (p.0 - (a.0 + t * vx), p.1 - (a.1 + t * vy));
|
||||
(dx * dx + dy * dy).sqrt()
|
||||
}
|
||||
let cradle_segs = [((5.0_f32, 9.5_f32), (5.0, 11.0)), ((5.0, 11.0), (10.0, 15.0)), ((10.0, 15.0), (15.0, 11.0)), ((15.0, 11.0), (15.0, 9.5))];
|
||||
let mut cap_points = vec![(cap_x, 9.5_f32), (cap_x + width, 9.5_f32), (cap_x + width / 2.0, cap_y + height)];
|
||||
let (arc_cx, arc_cy, arc_r) = (cap_x + width / 2.0, cap_y + height - rx, rx);
|
||||
for step in 0..=8 {
|
||||
let angle = std::f32::consts::PI * (step as f32) / 8.0;
|
||||
cap_points.push((arc_cx - arc_r * angle.cos(), arc_cy + arc_r * angle.sin()));
|
||||
}
|
||||
let min_clearance =
|
||||
cap_points.iter().flat_map(|&p| cradle_segs.iter().map(move |&(a, b)| dist_point_to_seg(p, a, b))).fold(f32::INFINITY, f32::min);
|
||||
assert!(min_clearance > half_strokes, "capsule outline clearance ({min_clearance:.2}) must exceed combined half-strokes ({half_strokes:.2})");
|
||||
|
||||
register();
|
||||
let tokens = style::tokens(Mode::Light, Contrast::Standard);
|
||||
let mut frames = Frames::new();
|
||||
for pt in [16.0_f32, 19.0, 20.0, 22.0] {
|
||||
let target = Rect::from_min_size(Pos2::ZERO, egui::vec2(pt, pt));
|
||||
let icon = IconSpec::svg(IconId::new("dictate"));
|
||||
let (shapes, _) = style::paint_frame(&mut frames, &tokens, craft::i18n::Direction::Ltr, craft::ui::style::Ink::OnPanel, |draw| {
|
||||
draw.icon(target, &icon)
|
||||
});
|
||||
assert!(!shapes.is_empty(), "dictate icon must paint at {pt} pt");
|
||||
let bounds = shapes.iter().map(|s| s.visual_bounding_rect()).fold(Rect::NOTHING, |acc, r| acc.union(r));
|
||||
assert!(bounds.is_positive(), "dictate icon must have positive rendered bounds at {pt} pt, got {bounds:?}");
|
||||
assert!(target.expand(1.0).contains_rect(bounds), "dictate icon at {pt} pt overflowed target {target:?}: {bounds:?}");
|
||||
assert!(
|
||||
bounds.width() >= pt * 0.5 && bounds.height() >= pt * 0.5 && bounds.width() <= 22.5 && bounds.height() <= 22.5,
|
||||
"dictate icon rendered size at {pt} pt out of 16-22 pt bounds: {bounds:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1150,6 +1150,44 @@ fn unimplemented_stub_ribbon_buttons_are_disabled() {
|
||||
.find(|n| n.accesskit_node().label().is_some_and(|l| l.split_whitespace().collect::<Vec<_>>().join(" ") == expected_label))
|
||||
.unwrap_or_else(|| panic!("{tab}: ribbon button '{expected_label}' should exist"));
|
||||
assert!(button.accesskit_node().is_disabled(), "{tab}: stub button {:?} ({id}) must be disabled", button.accesskit_node().label());
|
||||
if expected_label == "Dictate" {
|
||||
fn push_icon_rects(shape: &egui::Shape, clip: egui::Rect, btn_rect: egui::Rect, out: &mut Vec<egui::Rect>) {
|
||||
match shape {
|
||||
egui::Shape::Vec(items) => {
|
||||
for item in items {
|
||||
push_icon_rects(item, clip, btn_rect, out);
|
||||
}
|
||||
}
|
||||
egui::Shape::Text(_) | egui::Shape::Noop => {}
|
||||
other => {
|
||||
let r = other.visual_bounding_rect().intersect(clip);
|
||||
if r.is_positive()
|
||||
&& btn_rect.expand(1.0).contains_rect(r)
|
||||
&& r.width() < btn_rect.width() * 0.9
|
||||
&& r.height() < btn_rect.height() * 0.9
|
||||
{
|
||||
out.push(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let btn_rect = button.rect();
|
||||
let mut icon_rects = Vec::new();
|
||||
for c in &h.output().shapes {
|
||||
push_icon_rects(&c.shape, c.clip_rect, btn_rect, &mut icon_rects);
|
||||
}
|
||||
let icon_rect = icon_rects
|
||||
.into_iter()
|
||||
.reduce(|a, b| a.union(b))
|
||||
.unwrap_or_else(|| panic!("Dictate button ({btn_rect:?}) must paint a bounded icon shape"));
|
||||
assert!(
|
||||
icon_rect.width() >= 16.0 && icon_rect.height() >= 16.0,
|
||||
"Dictate ribbon icon must render at >= 16 pt, got {}x{} ({icon_rect:?})",
|
||||
icon_rect.width(),
|
||||
icon_rect.height()
|
||||
);
|
||||
assert!((icon_rect.width() - icon_rect.height()).abs() <= 2.0, "Dictate icon should be square: {icon_rect:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user