Skip to content

Commit

Permalink
[wf-panel/window-list] replaced IconProvider with gtk-utils function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgmdev committed Feb 4, 2021
1 parent f2a9667 commit 7cfb188
Showing 1 changed file with 1 addition and 109 deletions.
110 changes: 1 addition & 109 deletions src/panel/widgets/window-list/toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ namespace
extern zwlr_foreign_toplevel_handle_v1_listener toplevel_handle_v1_impl;
}

namespace IconProvider
{
void set_image_from_icon(Gtk::Image& image,
std::string app_id_list, int size, int scale);
}

class WayfireToplevel::impl
{
zwlr_foreign_toplevel_handle_v1 *handle, *parent;
Expand Down Expand Up @@ -277,8 +271,7 @@ class WayfireToplevel::impl
void set_app_id(std::string app_id)
{
this->app_id = app_id;
IconProvider::set_image_from_icon(image, app_id,
24, button.get_scale_factor());
set_image_from_icon(image, app_id, 24, button.get_scale_factor());
}

void send_rectangle_hint()
Expand Down Expand Up @@ -573,104 +566,3 @@ struct zwlr_foreign_toplevel_handle_v1_listener toplevel_handle_v1_impl = {
.parent = handle_toplevel_parent
};
}

/* Icon loading functions */
namespace IconProvider
{
using Icon = Glib::RefPtr<Gio::Icon>;

namespace
{
std::string tolower(std::string str)
{
for (auto& c : str)
c = std::tolower(c);
return str;
}
}

/* Gio::DesktopAppInfo
*
* Usually knowing the app_id, we can get a desktop app info from Gio
* The filename is either the app_id + ".desktop" or lower_app_id + ".desktop" */
Icon get_from_desktop_app_info(std::string app_id)
{
Glib::RefPtr<Gio::DesktopAppInfo> app_info;

std::vector<std::string> prefixes = {
"",
"/usr/share/applications/",
"/usr/share/applications/kde/",
"/usr/share/applications/org.kde.",
"/usr/local/share/applications/",
"/usr/local/share/applications/org.kde.",
};

std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
};

std::vector<std::string> suffixes = {
"",
".desktop"
};

for (auto& prefix : prefixes)
{
for (auto& id : app_id_variations)
{
for (auto& suffix : suffixes)
{
if (!app_info)
{
app_info = Gio::DesktopAppInfo
::create_from_filename(prefix + id + suffix);
}
}
}
}

if (app_info) // success
return app_info->get_icon();

return Icon{};
}

/* Second method: Just look up the built-in icon theme,
* perhaps some icon can be found there */

void set_image_from_icon(Gtk::Image& image,
std::string app_id_list, int size, int scale)
{
std::string app_id;
std::istringstream stream(app_id_list);

/* Wayfire sends a list of app-id's in space separated format, other compositors
* send a single app-id, but in any case this works fine */
while (stream >> app_id)
{
auto icon = get_from_desktop_app_info(app_id);
std::string icon_name = "unknown";

if (!icon)
{
/* Perhaps no desktop app info, but we might still be able to
* get an icon directly from the icon theme */
if (Gtk::IconTheme::get_default()->lookup_icon(app_id, 24))
icon_name = app_id;
} else
{
icon_name = icon->to_string();
}

WfIconLoadOptions options;
options.user_scale = scale;
set_image_icon(image, icon_name, size, options);

/* finally found some icon */
if (icon_name != "unknown")
break;
}
}
};

0 comments on commit 7cfb188

Please sign in to comment.