Skip to content

Commit

Permalink
feat(Tray): icon pixmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexays committed Sep 9, 2018
1 parent 403d9cc commit 8496df5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/modules/sni/sni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Item {
std::string title;
int32_t window_id;
std::string icon_name;
Glib::RefPtr<Gdk::Pixbuf> icon_pixmap;
std::string overlay_icon_name;
std::string attention_icon_name;
std::string attention_movie_name;
Expand All @@ -34,6 +35,7 @@ class Item {
static void getAll(GObject* obj, GAsyncResult* res, gpointer data);

void updateImage();
Glib::RefPtr<Gdk::Pixbuf> extractPixBuf(GVariant* variant);
Glib::RefPtr<Gdk::Pixbuf> getIconByName(std::string name, int size);
Glib::Dispatcher* dp_;
GCancellable* cancellable_ = nullptr;
Expand Down
52 changes: 52 additions & 0 deletions src/modules/sni/sni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void waybar::modules::SNI::Item::getAll(GObject* obj, GAsyncResult* res,
} else if (g_strcmp0(key, "IconName") == 0) {
item->icon_name = g_variant_dup_string(value, nullptr);
} else if (g_strcmp0(key, "IconPixmap") == 0) {
item->icon_pixmap = item->extractPixBuf(value);
// TODO: icon pixmap
} else if (g_strcmp0(key, "OverlayIconName") == 0) {
item->overlay_icon_name = g_variant_dup_string(value, nullptr);
Expand Down Expand Up @@ -114,6 +115,55 @@ void waybar::modules::SNI::Item::getAll(GObject* obj, GAsyncResult* res,
// TODO: handle change
}

Glib::RefPtr<Gdk::Pixbuf> waybar::modules::SNI::Item::extractPixBuf(
GVariant* variant)
{
GVariantIter* it;
g_variant_get(variant, "a(iiay)", &it);
if (it == nullptr) {
return Glib::RefPtr<Gdk::Pixbuf>{};
}
GVariant* val;
gint lwidth = 0;
gint lheight = 0;
gint width;
gint height;
guchar* array = nullptr;
while (g_variant_iter_loop(it, "(ii@ay)", &width, &height, &val)) {
if (width > 0 && height > 0 && val != nullptr
&& width * height > lwidth * lheight) {
auto size = g_variant_get_size(val);
/* Sanity check */
if (size == 4U * width * height) {
/* Find the largest image */
gconstpointer data = g_variant_get_data(val);
if (data != nullptr) {
if (array != nullptr) {
g_free(array);
}
array = static_cast<guchar*>(g_memdup(data, size));
lwidth = width;
lheight = height;
}
}
}
}
g_variant_iter_free(it);
if (array != nullptr) {
/* argb to rgba */
for (uint32_t i = 0; i < 4U * lwidth * lheight; i += 4) {
guchar alpha = array[i];
array[i] = array[i + 1];
array[i + 1] = array[i + 2];
array[i + 2] = array[i + 3];
array[i + 3] = alpha;
}
return Gdk::Pixbuf::create_from_data(array, Gdk::Colorspace::COLORSPACE_RGB,
true, 8, lwidth, lheight, 4 * lwidth);
}
return Glib::RefPtr<Gdk::Pixbuf>{};
}

void waybar::modules::SNI::Item::updateImage()
{
if (!icon_name.empty()) {
Expand All @@ -131,6 +181,8 @@ void waybar::modules::SNI::Item::updateImage()
pixbuf = getIconByName("image-missing", icon_size);
}
image->set(pixbuf);
} else if (icon_pixmap) {
image->set(icon_pixmap);
} else {
image->set_from_icon_name("image-missing", Gtk::ICON_SIZE_MENU);
image->set_pixel_size(icon_size);
Expand Down

0 comments on commit 8496df5

Please sign in to comment.