fix: scale up or down images that do not fit the screen

This commit is contained in:
Alexis Maiquez Murcia
2026-04-06 05:02:07 +02:00
committed by Alexis Maiquez Murcia
parent caa448680e
commit 3a9be7c696
+16
View File
@@ -197,6 +197,21 @@ impl Renderer {
// Draw background // Draw background
if let Some(ref background) = self.background { if let Some(ref background) = self.background {
self.context.save().expect("Failed to save context");
let bg_width = background.width() as f64;
let bg_height = background.height() as f64;
let scale_x = self.width as f64 / bg_width;
let scale_y = self.height as f64 / bg_height;
// Preserve aspect ratio by using the larger scale factor (zoom to fill)
let scale = scale_x.max(scale_y);
// Calculate offsets to center the image
let offset_x = (self.width as f64 - bg_width * scale) / 2.0;
let offset_y = (self.height as f64 - bg_height * scale) / 2.0;
self.context.translate(offset_x, offset_y);
self.context.scale(scale, scale);
self.context.new_path(); self.context.new_path();
self.context self.context
.set_source_surface(background, 0.0, 0.0) .set_source_surface(background, 0.0, 0.0)
@@ -204,6 +219,7 @@ impl Renderer {
self.context self.context
.paint_with_alpha(self.fade_alpha) .paint_with_alpha(self.fade_alpha)
.expect("Failed to paint"); .expect("Failed to paint");
self.context.restore().expect("Failed to restore context");
} }
if self.config.indicator { if self.config.indicator {