Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Load systray items that are registered without a path (By: Kage-Yami)
- `get_locale` now follows POSIX standard for locale selection (By: mirhahn, w-lfchen)
- Improve multi-monitor handling under wayland (By: bkueng)
- Render image widget at full resolution when display scaling is enabled (By: zane-weissman)

### Features
- Add warning and docs for incompatible `:anchor` and `:exclusive` options
Expand Down
29 changes: 24 additions & 5 deletions crates/eww/src/widgets/widget_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use eww_shared_util::Spanned;
use gdk::{ModifierType, NotifyType};
use glib::translate::FromGlib;
use gtk::{self, glib, prelude::*, DestDefaults, TargetEntry, TargetList};
use gtk::{cairo, gdk::ffi::gdk_cairo_surface_create_from_pixbuf};
use gtk::{gdk, pango};
use itertools::Itertools;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -594,6 +595,7 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
// @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");
}
Expand All @@ -602,9 +604,11 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
let pixbuf_animation = gtk::gdk_pixbuf::PixbufAnimation::from_file(std::path::PathBuf::from(path))?;
gtk_widget.set_from_animation(&pixbuf_animation);
} else {
let pixbuf;
let scale = gtk_widget.scale_factor();
let pixbuf: gtk::gdk_pixbuf::Pixbuf;

// populate the pixel buffer
if path.ends_with(".svg") && !fill_svg.is_empty() {
if path.ends_with(".svg") && !fill_svg.is_empty() { // render with fill
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=") {
Expand All @@ -615,12 +619,27 @@ fn build_gtk_image(bargs: &mut BuilderArgs) -> Result<gtk::Image> {
reg.replace(&svg_data, &format!("<svg fill=\"{}\"", fill_svg))
};
let stream = gtk::gio::MemoryInputStream::from_bytes(&gtk::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::<&gtk::gio::Cancellable>)?;
pixbuf = gtk::gdk_pixbuf::Pixbuf::from_stream_at_scale(&stream, image_width * scale, image_height * scale, preserve_aspect_ratio, None::<&gtk::gio::Cancellable>)?;
stream.close(None::<&gtk::gio::Cancellable>)?;
} else {
pixbuf = gtk::gdk_pixbuf::Pixbuf::from_file_at_scale(std::path::PathBuf::from(path), image_width, image_height, preserve_aspect_ratio)?;
let w = if image_width > 0 {image_width * scale} else {-1};
let h = if image_height > 0 {image_height * scale} else {-1};
pixbuf = gtk::gdk_pixbuf::Pixbuf::from_file_at_scale(std::path::PathBuf::from(path), w, h, preserve_aspect_ratio)?;
//gtk_widget.set_from_pixbuf(Some(&pixbuf));
}
gtk_widget.set_from_pixbuf(Some(&pixbuf));

// render to surface
let surface = unsafe {
// gtk::cairo::Surface will destroy the underlying surface on drop
let ptr = gdk_cairo_surface_create_from_pixbuf(
pixbuf.as_ptr(),
scale,
std::ptr::null_mut(),
);
cairo::Surface::from_raw_full(ptr)?
};

gtk_widget.set_from_surface(Some(surface.as_ref()));
}
},
// @prop icon - name of a theme icon
Expand Down