Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix `img` aspect ratio overriding existing value (#62367)
Zak Nesler59b2ebf10351
diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rsindex 13c8df1f8c5..2c77e5f32ad 100644--- a/crates/gpui/src/elements/img.rs+++ b/crates/gpui/src/elements/img.rs@@ -346,7 +346,10 @@ impl Element for Img {}let image_size = data.render_size(frame_index);- style.aspect_ratio = Some(image_size.width / image_size.height);++ if style.aspect_ratio.is_none() {+ style.aspect_ratio = Some(image_size.width / image_size.height);+ }if let Length::Auto = style.size.width {style.size.width = match style.size.height {@@ -895,6 +898,44 @@ mod tests {);}+ #[gpui::test]+ fn explicit_aspect_ratio_is_not_overridden_by_intrinsic_ratio(cx: &mut TestAppContext) {+ let window = cx.add_empty_window();++ // A portrait image in a square container+ window.draw(point(px(0.), px(0.)), size(px(100.), px(100.)), |_, _| {+ div()+ .size(px(100.))+ .overflow_hidden()+ .child(+ img(ImageSource::Render(test_image_with_size(100, 200)))+ .size_full()+ .aspect_square()+ .object_fit(ObjectFit::Contain),+ )+ .into_any_element()+ });++ let (rendered_bounds, scale_factor) = window.update(|window, _| {+ let sprite = window+ .rendered_frame+ .scene+ .polychrome_sprites+ .last()+ .expect("contained image should paint a sprite");+ (sprite.bounds, window.scale_factor())+ });++ // The element stays 100x100, so the image is letterboxed horizontally+ assert_eq!(+ rendered_bounds,+ Bounds {+ origin: point(px(25.).scale(scale_factor), px(0.).scale(scale_factor)),+ size: size(px(50.).scale(scale_factor), px(100.).scale(scale_factor)),+ }+ );+ }+#[gpui::test]fn image_object_fit_cover_clamps_corner_radii_to_visible_bounds(cx: &mut TestAppContext) {let window = cx.add_empty_window();