diff --git a/wezterm-gui/src/termwindow/background.rs b/wezterm-gui/src/termwindow/background.rs index 606f6c5c877..cacc48a2293 100644 --- a/wezterm-gui/src/termwindow/background.rs +++ b/wezterm-gui/src/termwindow/background.rs @@ -488,16 +488,34 @@ impl crate::TermWindow { } else { ((pixel_height * aspect).floor(), pixel_height) }; + + // Compute image height preserving aspect ratio. + let computed_background_height = if let ( + BackgroundSize::Cover, BackgroundSize::Dimension(width) + ) = (layer.def.height, layer.def.width) { + (width.evaluate_as_pixels(h_context) / aspect).floor() + } else { + min_aspect_height + }; + + // Compute width preserving aspect ratio. + let computed_background_width = if let ( + BackgroundSize::Cover, BackgroundSize::Dimension(height) + ) = (layer.def.width, layer.def.height) { + (height.evaluate_as_pixels(h_context) * aspect).floor() + } else { + min_aspect_width + }; let width = match layer.def.width { BackgroundSize::Contain => max_aspect_width as f32, - BackgroundSize::Cover => min_aspect_width as f32, + BackgroundSize::Cover => computed_background_width as f32, BackgroundSize::Dimension(n) => n.evaluate_as_pixels(h_context), }; let height = match layer.def.height { BackgroundSize::Contain => max_aspect_height as f32, - BackgroundSize::Cover => min_aspect_height as f32, + BackgroundSize::Cover => computed_background_height as f32, BackgroundSize::Dimension(n) => n.evaluate_as_pixels(v_context), };