Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dock/dock-app.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "dock.hpp"
#include "giomm/application.h"
#include "gtk-utils.hpp"
#include "toplevel.hpp"
#include "toplevel-icon.hpp"
#include <iostream>
Expand Down Expand Up @@ -49,7 +50,7 @@ void WfDockApp::on_activate()
{
WayfireShellApp::on_activate();
new CssFromConfigInt("dock/icon_height", ".toplevel-icon {-gtk-icon-size:", "px;}");
IconProvider::load_custom_icons();
IconProvider::load_custom_icons("dock");

/* At this point, wayland connection has been initialized,
* and hopefully outputs have been created */
Expand Down
188 changes: 2 additions & 186 deletions src/dock/toplevel-icon.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <giomm/desktopappinfo.h>
#include <gtkmm/button.h>
#include <gtkmm/box.h>
#include <gtkmm/icontheme.h>
Expand All @@ -12,16 +11,9 @@
#include "toplevel.hpp"
#include "toplevel-icon.hpp"
#include "gtk-utils.hpp"
#include <iostream>
#include <sstream>
#include <cassert>
#include "wf-option-wrap.hpp"

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

class WfToplevelIcon::impl
{
Expand Down Expand Up @@ -88,10 +80,8 @@ class WfToplevelIcon::impl
}

this->app_id = app_id;
IconProvider::set_image_from_icon(image,
app_id,
icon_height,
button.get_scale_factor());
IconProvider::image_set_icon(image,
app_id);
}

void send_rectangle_hint()
Expand Down Expand Up @@ -210,177 +200,3 @@ void WfToplevelIcon::close()
{
return pimpl->close();
}

/* 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;
}

std::map<std::string, std::string> custom_icons;
}

void load_custom_icons()
{
static const std::string prefix = "icon_mapping_";
auto section = WayfireShellApp::get().config.get_section("dock");

for (auto option : section->get_registered_options())
{
if (option->get_name().compare(0, prefix.length(), prefix) != 0)
{
continue;
}

auto app_id = option->get_name().substr(prefix.length());
custom_icons[app_id] = option->get_value_str();
}
}

bool set_custom_icon(Gtk::Image& image, std::string app_id, int size, int scale)
{
if (!custom_icons.count(app_id))
{
return false;
}

image_set_icon(&image, custom_icons[app_id]);
return true;
}

/* 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/share/applications/org.gnome.",
"/usr/local/share/applications/",
"/usr/local/share/applications/org.kde.",
"/usr/local/share/applications/org.gnome.",
};

std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
tolower(app_id),
};
// e.g. org.gnome.Evince.desktop
app_id_variations[2][0] = std::toupper(app_id_variations[2][0]);

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)
{
// special treatment for snap apps
std::string prefix = "/var/lib/snapd/desktop/applications/";
auto& id = app_id_variations[1]; // seems to be lower case
for (auto& suffix : suffixes)
{
app_info = Gio::DesktopAppInfo::create_from_filename(
prefix + id + "_" + id + suffix);
if (app_info)
{
break;
}
}
}

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

return Icon{};
}

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);

bool found_icon = false;

/* 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 */
auto display = image.get_display();
while (stream >> app_id)
{
/* Try first method: custom icon file provided by the user */
if (set_custom_icon(image, app_id, size, scale))
{
found_icon = true;
break;
}

/* Then try to load the DesktopAppInfo */
auto icon = get_from_desktop_app_info(app_id);
std::string icon_name = "unknown";

if (!icon)
{
/* Finally try directly looking up the icon, if it exists */
if (Gtk::IconTheme::get_for_display(display)->lookup_icon(app_id, 24))
{
icon_name = app_id;
}
} else
{
icon_name = icon->to_string();
}

WfIconLoadOptions options;
options.user_scale = scale;
image_set_icon(&image, icon_name);

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

if (!found_icon)
{
std::cout << "Failed to load icon for any of " << app_id_list << std::endl;
}
}
}
7 changes: 0 additions & 7 deletions src/dock/toplevel-icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,3 @@ class WfToplevelIcon
private:
std::unique_ptr<impl> pimpl;
};

namespace IconProvider
{
/* Loads custom app_id -> icon file mappings from the section
* They have the format icon_mapping_<app_id> = <icon file> */
void load_custom_icons();
}
2 changes: 1 addition & 1 deletion src/panel/widgets/command-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ WfCommandOutputButtons::CommandOutput::CommandOutput(const std::string & name,
{
this->tooltip_command = tooltip_command;

image_set_icon(&icon, icon_name);
IconProvider::image_set_icon(icon, icon_name);

if (icon_size > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/panel/widgets/launchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool WfLauncherButton::initialize(std::string name, std::string icon, std::strin

void WfLauncherButton::update_icon()
{
image_set_icon(&m_icon, app_info->get_icon()->to_string());
IconProvider::image_set_icon(m_icon, app_info->get_icon()->to_string());
}

void WfLauncherButton::launch()
Expand Down
2 changes: 1 addition & 1 deletion src/panel/widgets/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ bool WayfireMenu::update_icon()
icon = menu_icon;
}

image_set_icon(&main_image, icon);
IconProvider::image_set_icon(main_image, icon);
return true;
}

Expand Down
Loading
Loading