From 0d27949f0a476467ebab8d87cc6af1cac25fd06d Mon Sep 17 00:00:00 2001 From: akliuxingyuan Date: Tue, 30 Aug 2022 23:13:38 +0800 Subject: [PATCH] scale icons for HiDPI monitor --- src/modules/sni/item.cpp | 2 +- src/modules/wlr/taskbar.cpp | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/sni/item.cpp b/src/modules/sni/item.cpp index 7cd00452..33c120e5 100644 --- a/src/modules/sni/item.cpp +++ b/src/modules/sni/item.cpp @@ -309,7 +309,7 @@ void Item::updateImage() { pixbuf = pixbuf->scale_simple(width, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR); } - auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, 0, image.get_window()); + auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image.get_scale_factor(), image.get_window()); image.set(surface); } diff --git a/src/modules/wlr/taskbar.cpp b/src/modules/wlr/taskbar.cpp index b74c44b2..b60cf52f 100644 --- a/src/modules/wlr/taskbar.cpp +++ b/src/modules/wlr/taskbar.cpp @@ -15,6 +15,7 @@ #include #include +#include "gdkmm/general.h" #include "glibmm/error.h" #include "glibmm/fileutils.h" #include "glibmm/refptr.h" @@ -173,18 +174,24 @@ bool Task::image_load_icon(Gtk::Image &image, const Glib::RefPtr } Glib::RefPtr pixbuf; + auto scaled_icon_size = size * image.get_scale_factor(); try { - pixbuf = icon_theme->load_icon(ret_icon_name, size, Gtk::ICON_LOOKUP_FORCE_SIZE); + pixbuf = icon_theme->load_icon(ret_icon_name, scaled_icon_size, Gtk::ICON_LOOKUP_FORCE_SIZE); } catch (...) { if (Glib::file_test(ret_icon_name, Glib::FILE_TEST_EXISTS)) - pixbuf = load_icon_from_file(ret_icon_name, size); + pixbuf = load_icon_from_file(ret_icon_name, scaled_icon_size); else pixbuf = {}; } if (pixbuf) { - image.set(pixbuf); + if (pixbuf->get_width() != scaled_icon_size) { + int width = scaled_icon_size * pixbuf->get_width() / pixbuf->get_height(); + pixbuf = pixbuf->scale_simple(width, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR); + } + auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image.get_scale_factor(), image.get_window()); + image.set(surface); return true; }