feat: add fill-svg and preserve-aspect-ratio attributes to image widget (#1148)
* feat: add fill-svg attribute to image widget Co-authored-by: Eddy Otsutsuki <23711001+hypernova7@users.noreply.github.com> * feat: add preserve-aspect-ratio attribute to image widget * docs: document new attributes; changelog entry --------- Co-authored-by: Eddy Otsutsuki <23711001+hypernova7@users.noreply.github.com>
This commit is contained in:
parent
f01396f9d5
commit
5212e62fd3
2 changed files with 26 additions and 2 deletions
|
@ -12,6 +12,7 @@ All notable changes to eww will be listed here, starting at changes since versio
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
- Update rust toolchain to 1.80.1 (By: w-lfchen)
|
- Update rust toolchain to 1.80.1 (By: w-lfchen)
|
||||||
|
- Add `:fill-svg` and `:preserve-aspect-ratio` properties to images (By: hypernova7, w-lfchen)
|
||||||
- Add `:truncate` property to labels, disabled by default (except in cases where truncation would be enabled in version `0.5.0` and before) (By: Rayzeq).
|
- Add `:truncate` property to labels, disabled by default (except in cases where truncation would be enabled in version `0.5.0` and before) (By: Rayzeq).
|
||||||
- Add support for `:hover` css selectors for tray items (By: zeapoz)
|
- Add support for `:hover` css selectors for tray items (By: zeapoz)
|
||||||
- Add `min` and `max` function calls to simplexpr (By: ovalkonia)
|
- Add `min` and `max` function calls to simplexpr (By: ovalkonia)
|
||||||
|
|
|
@ -571,12 +571,35 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
|
||||||
// @prop path - path to the image file
|
// @prop path - path to the image file
|
||||||
// @prop image-width - width of the image
|
// @prop image-width - width of the image
|
||||||
// @prop image-height - height of the image
|
// @prop image-height - height of the image
|
||||||
prop(path: as_string, image_width: as_i32 = -1, image_height: as_i32 = -1) {
|
// @prop preserve-aspect-ratio - whether to keep the aspect ratio when resizing an image. Default: true, false doesn't work for all image types
|
||||||
|
// @prop fill-svg - sets the color of svg images
|
||||||
|
prop(path: as_string, image_width: as_i32 = -1, image_height: as_i32 = -1, preserve_aspect_ratio: as_bool = true, fill_svg: as_string = "") {
|
||||||
|
if !path.ends_with(".svg") && !fill_svg.is_empty() {
|
||||||
|
log::warn!("Fill attribute ignored, file is not an svg image");
|
||||||
|
}
|
||||||
|
|
||||||
if path.ends_with(".gif") {
|
if path.ends_with(".gif") {
|
||||||
let pixbuf_animation = gtk::gdk_pixbuf::PixbufAnimation::from_file(std::path::PathBuf::from(path))?;
|
let pixbuf_animation = gtk::gdk_pixbuf::PixbufAnimation::from_file(std::path::PathBuf::from(path))?;
|
||||||
gtk_widget.set_from_animation(&pixbuf_animation);
|
gtk_widget.set_from_animation(&pixbuf_animation);
|
||||||
} else {
|
} else {
|
||||||
let pixbuf = gtk::gdk_pixbuf::Pixbuf::from_file_at_size(std::path::PathBuf::from(path), image_width, image_height)?;
|
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 = gtk::gio::MemoryInputStream::from_bytes(>k::glib::Bytes::from(svg_data.as_bytes()));
|
||||||
|
pixbuf = gtk::gdk_pixbuf::Pixbuf::from_stream_at_scale(&stream, image_width, image_height, preserve_aspect_ratio, None::<>k::gio::Cancellable>)?;
|
||||||
|
stream.close(None::<>k::gio::Cancellable>)?;
|
||||||
|
} else {
|
||||||
|
pixbuf = gtk::gdk_pixbuf::Pixbuf::from_file_at_scale(std::path::PathBuf::from(path), image_width, image_height, preserve_aspect_ratio)?;
|
||||||
|
}
|
||||||
gtk_widget.set_from_pixbuf(Some(&pixbuf));
|
gtk_widget.set_from_pixbuf(Some(&pixbuf));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue