Fix incorrect tray icon scaling
parent
28e7a96e37
commit
4f6a9b1bc2
|
@ -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*/);
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
if (!pixbuf) {
|
||||||
|
pixbuf = getIconByName("image-missing", getScaledIconSize());
|
||||||
|
}
|
||||||
|
|
||||||
image.set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
|
// If the loaded icon is not square, assume that the icon height should match the
|
||||||
image.set_pixel_size(scaled_icon_size);
|
// requested icon size, but the width is allowed to be different. As such, if the
|
||||||
if (!icon_name.empty()) {
|
// height of the image does not match the requested icon size, resize the icon such that
|
||||||
try {
|
// the aspect ratio is maintained, but the height matches the requested icon size.
|
||||||
// Try to find icons specified by path and filename
|
if (pixbuf->get_height() != 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, 0, image.get_window());
|
||||||
|
image.set(surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
Glib::RefPtr<Gdk::Pixbuf> Item::getIconPixbuf() {
|
||||||
|
try {
|
||||||
|
if (!icon_name.empty()) {
|
||||||
std::ifstream temp(icon_name);
|
std::ifstream temp(icon_name);
|
||||||
if (temp.is_open()) {
|
if (temp.is_open()) {
|
||||||
auto pixbuf = Gdk::Pixbuf::create_from_file(icon_name);
|
return Gdk::Pixbuf::create_from_file(icon_name);
|
||||||
|
|
||||||
if (pixbuf->gobj() != nullptr) {
|
|
||||||
// An icon specified by path and filename may be the wrong size for
|
|
||||||
// the tray
|
|
||||||
// Keep the aspect ratio and scale to make the height equal to scaled_icon_size
|
|
||||||
// If people have non square icons, assume they want it to grow in width not height
|
|
||||||
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, 0, image.get_window());
|
|
||||||
image.set(surface);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
auto icon_by_name = getIconByName(icon_name, scaled_icon_size);
|
|
||||||
auto surface = Gdk::Cairo::create_surface_from_pixbuf(icon_by_name, 0, image.get_window());
|
|
||||||
image.set(surface);
|
|
||||||
}
|
}
|
||||||
} catch (Glib::Error& e) {
|
return getIconByName(icon_name, getScaledIconSize());
|
||||||
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
|
} else if (icon_pixmap) {
|
||||||
|
return icon_pixmap;
|
||||||
}
|
}
|
||||||
} else if (icon_pixmap) {
|
} catch (Glib::Error& e) {
|
||||||
// An icon extracted may be the wrong size for the tray
|
spdlog::error("Item '{}': {}", id, static_cast<std::string>(e.what()));
|
||||||
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());
|
return getIconByName("image-missing", getScaledIconSize());
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue