feat(img wdgt): custom rendering + new feats

This commit is contained in:
Byson94
2025-12-24 11:18:27 +05:30
parent ad79e81c50
commit 216775f55a

View File

@@ -1495,6 +1495,14 @@ pub(super) fn build_image(
let can_shrink = get_bool_prop(&props, "can_shrink", Some(true))?;
let content_fit_str = get_string_prop(&props, "content_fit", Some("contain"))?;
let content_fit = parse_content_fit(&content_fit_str)?;
let image_width = get_i32_prop(&props, "image_width", Some(-1))?;
let image_height = get_i32_prop(&props, "image_height", Some(-1))?;
let preserve_aspect_ratio = get_bool_prop(&props, "preserve_aspect_ratio", Some(true))?;
let fill_svg = get_string_prop(&props, "fill_svg", Some(""))?;
if !path.ends_with(".svg") && !fill_svg.is_empty() {
log::warn!("Fill attribute ignored, file is not an svg image");
}
widget.set_content_fit(content_fit);
widget.set_can_shrink(can_shrink);
@@ -1522,8 +1530,38 @@ pub(super) fn build_image(
});
}
} else {
let file = gtk4::gio::File::for_path(&path);
widget.set_file(Some(&file));
let pixbuf;
// populate the pixel buffer
if path.ends_with(".svg") && !fill_svg.is_empty() {
let svg_data = std::fs::read_to_string(std::path::PathBuf::from(path.clone()))?;
// The fastest way to add/change fill color
let svg_data = if svg_data.contains("fill=") {
let reg = regex::Regex::new(r#"fill="[^"]*""#)?;
reg.replace(&svg_data, &format!("fill=\"{}\"", fill_svg))
} else {
let reg = regex::Regex::new(r"<svg")?;
reg.replace(&svg_data, &format!("<svg fill=\"{}\"", fill_svg))
};
let stream = gtk4::gio::MemoryInputStream::from_bytes(&gtk4::glib::Bytes::from(
svg_data.as_bytes(),
));
pixbuf = gtk4::gdk_pixbuf::Pixbuf::from_stream_at_scale(
&stream,
image_width,
image_height,
preserve_aspect_ratio,
None::<&gtk4::gio::Cancellable>,
)?;
stream.close(None::<&gtk4::gio::Cancellable>)?;
} else {
pixbuf = gtk4::gdk_pixbuf::Pixbuf::from_file_at_scale(
std::path::PathBuf::from(path),
image_width,
image_height,
preserve_aspect_ratio,
)?;
}
widget.set_pixbuf(Some(&pixbuf));
}
Ok(())