From 06ad35c42b924952dc349469531e03c151d32a6b Mon Sep 17 00:00:00 2001 From: Till Smejkal Date: Sun, 5 Jul 2020 13:07:12 +0200 Subject: [PATCH] Add support for multiple icon themes in the config If there are multiple icon themes defined in the config option 'icon-theme' the module will try from left to right to find an icon. The system default will always be added to this list. --- include/modules/wlr/taskbar.hpp | 4 +-- man/waybar-wlr-taskbar.5.scd | 2 +- src/modules/wlr/taskbar.cpp | 45 ++++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/include/modules/wlr/taskbar.hpp b/include/modules/wlr/taskbar.hpp index 5bcb7ec4..53a2f8c7 100644 --- a/include/modules/wlr/taskbar.hpp +++ b/include/modules/wlr/taskbar.hpp @@ -131,7 +131,7 @@ class Taskbar : public waybar::AModule Gtk::Box box_; std::vector tasks_; - Glib::RefPtr icon_theme_; + std::vector> icon_themes_; struct zwlr_foreign_toplevel_manager_v1 *manager_; struct wl_seat *seat_; @@ -154,7 +154,7 @@ class Taskbar : public waybar::AModule bool show_output(struct wl_output *) const; bool all_outputs() const; - Glib::RefPtr icon_theme() const; + std::vector> icon_themes() const; }; } /* namespace waybar::modules::wlr */ diff --git a/man/waybar-wlr-taskbar.5.scd b/man/waybar-wlr-taskbar.5.scd index 68cf02a7..24946e1a 100644 --- a/man/waybar-wlr-taskbar.5.scd +++ b/man/waybar-wlr-taskbar.5.scd @@ -25,7 +25,7 @@ Addressed by *wlr/taskbar* *icon-theme*: ++ typeof: string ++ - The name of the icon-theme that should be used. If omitted, the system default will be used. + The names of the icon-themes that should be used to find an icon. The list will be traversed from left to right. If omitted, the system default will be used. *icon-size*: ++ typeof: integer ++ diff --git a/src/modules/wlr/taskbar.cpp b/src/modules/wlr/taskbar.cpp index 520b4b91..f669fa90 100644 --- a/src/modules/wlr/taskbar.cpp +++ b/src/modules/wlr/taskbar.cpp @@ -298,12 +298,23 @@ void Task::handle_title(const char *title) void Task::handle_app_id(const char *app_id) { app_id_ = app_id; - if (!image_load_icon(icon_, tbar_->icon_theme(), app_id_, - config_["icon-size"].isInt() ? config_["icon-size"].asInt() : 16)) - spdlog::warn("Failed to load icon for {}", app_id); - if (with_icon_) + if (!with_icon_) + return; + + int icon_size = config_["icon-size"].isInt() ? config_["icon-size"].asInt() : 16; + bool found = false; + for (auto& icon_theme : tbar_->icon_themes()) { + if (image_load_icon(icon_, icon_theme, app_id_, icon_size)) { + found = true; + break; + } + } + + if (found) icon_.show(); + else + spdlog::debug("Couldn't find icon for {}", app_id_); } void Task::handle_output_enter(struct wl_output *output) @@ -555,13 +566,23 @@ Taskbar::Taskbar(const std::string &id, const waybar::Bar &bar, const Json::Valu /* Get the configured icon theme if specified */ if (config_["icon-theme"].isString()) { - icon_theme_ = Gtk::IconTheme::create(); - icon_theme_->set_custom_theme(config_["icon-theme"].asString()); - spdlog::debug("Use custom icon theme: {}.", config_["icon-theme"].asString()); - } else { - spdlog::debug("Use system default icon theme"); - icon_theme_ = Gtk::IconTheme::get_default(); + auto icon_theme_config = config_["icon-theme"].asString(); + size_t start = 0, end = 0; + + do { + end = icon_theme_config.find(',', start); + auto it_name = trim(icon_theme_config.substr(start, end-start)); + + auto it = Gtk::IconTheme::create(); + it->set_custom_theme(it_name); + spdlog::debug("Use custom icon theme: {}", it_name); + + icon_themes_.push_back(it); + + start = end == std::string::npos ? end : end + 1; + } while(end != std::string::npos); } + icon_themes_.push_back(Gtk::IconTheme::get_default()); } Taskbar::~Taskbar() @@ -677,9 +698,9 @@ bool Taskbar::all_outputs() const return result; } -Glib::RefPtr Taskbar::icon_theme() const +std::vector> Taskbar::icon_themes() const { - return icon_theme_; + return icon_themes_; } } /* namespace waybar::modules::wlr */