Fix incorrect tray icon scaling

This commit is contained in:
Michael Swiger 2021-07-31 16:14:02 -07:00
parent 28e7a96e37
commit 4f6a9b1bc2
2 changed files with 37 additions and 33 deletions

View File

@ -66,7 +66,9 @@ class Item : public sigc::trackable {
void updateImage(); void updateImage();
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant* variant); Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant* variant);
Glib::RefPtr<Gdk::Pixbuf> getIconPixbuf();
Glib::RefPtr<Gdk::Pixbuf> getIconByName(const std::string& name, int size); Glib::RefPtr<Gdk::Pixbuf> getIconByName(const std::string& name, int size);
double getScaledIconSize();
static void onMenuDestroyed(Item* self, GObject* old_menu_pointer); static void onMenuDestroyed(Item* self, GObject* old_menu_pointer);
void makeMenu(); void makeMenu();
bool handleClick(GdkEventButton* const& /*ev*/); bool handleClick(GdkEventButton* const& /*ev*/);

View File

@ -310,44 +310,41 @@ Glib::RefPtr<Gdk::Pixbuf> Item::extractPixBuf(GVariant* variant) {
} }
void Item::updateImage() { void Item::updateImage() {
auto scale_factor = image.get_scale_factor(); auto pixbuf = getIconPixbuf();
auto scaled_icon_size = icon_size * scale_factor; auto scaled_icon_size = getScaledIconSize();
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU); if (!pixbuf) {
image.set_pixel_size(scaled_icon_size); pixbuf = getIconByName("image-missing", getScaledIconSize());
if (!icon_name.empty()) { }
try {
// Try to find icons specified by path and filename
std::ifstream temp(icon_name);
if (temp.is_open()) {
auto pixbuf = Gdk::Pixbuf::create_from_file(icon_name);
if (pixbuf->gobj() != nullptr) { // If the loaded icon is not square, assume that the icon height should match the
// An icon specified by path and filename may be the wrong size for // requested icon size, but the width is allowed to be different. As such, if the
// the tray // height of the image does not match the requested icon size, resize the icon such that
// Keep the aspect ratio and scale to make the height equal to scaled_icon_size // the aspect ratio is maintained, but the height matches the requested icon size.
// If people have non square icons, assume they want it to grow in width not height if (pixbuf->get_height() != scaled_icon_size) {
int width = scaled_icon_size * pixbuf->get_width() / pixbuf->get_height(); int width = scaled_icon_size * pixbuf->get_width() / pixbuf->get_height();
pixbuf = pixbuf->scale_simple(width, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR); 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, 0, image.get_window());
image.set(surface); image.set(surface);
}
Glib::RefPtr<Gdk::Pixbuf> Item::getIconPixbuf() {
try {
if (!icon_name.empty()) {
std::ifstream temp(icon_name);
if (temp.is_open()) {
return Gdk::Pixbuf::create_from_file(icon_name);
} }
} else { return getIconByName(icon_name, getScaledIconSize());
auto icon_by_name = getIconByName(icon_name, scaled_icon_size); } else if (icon_pixmap) {
auto surface = Gdk::Cairo::create_surface_from_pixbuf(icon_by_name, 0, image.get_window()); return icon_pixmap;
image.set(surface);
} }
} catch (Glib::Error& e) { } catch (Glib::Error& e) {
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what())); spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
} }
} else if (icon_pixmap) { return getIconByName("image-missing", getScaledIconSize());
// An icon extracted may be the wrong size for the tray
icon_pixmap = icon_pixmap->scale_simple(icon_size, scaled_icon_size, Gdk::InterpType::INTERP_BILINEAR);
auto surface = Gdk::Cairo::create_surface_from_pixbuf(icon_pixmap, 0, image.get_window());
image.set(icon_pixmap);
}
} }
Glib::RefPtr<Gdk::Pixbuf> Item::getIconByName(const std::string& name, int request_size) { Glib::RefPtr<Gdk::Pixbuf> Item::getIconByName(const std::string& name, int request_size) {
@ -382,6 +379,11 @@ Glib::RefPtr<Gdk::Pixbuf> Item::getIconByName(const std::string& name, int reque
name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE); name.c_str(), tmp_size, Gtk::IconLookupFlags::ICON_LOOKUP_FORCE_SIZE);
} }
double Item::getScaledIconSize() {
// apply the scale factor from the Gtk window to the requested icon size
return icon_size * image.get_scale_factor();
}
void Item::onMenuDestroyed(Item* self, GObject* old_menu_pointer) { void Item::onMenuDestroyed(Item* self, GObject* old_menu_pointer) {
if (old_menu_pointer == reinterpret_cast<GObject*>(self->dbus_menu)) { if (old_menu_pointer == reinterpret_cast<GObject*>(self->dbus_menu)) {
self->gtk_menu = nullptr; self->gtk_menu = nullptr;