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.
pull/766/head
Till Smejkal 2020-07-05 13:07:12 +02:00
parent b7e8275d90
commit 06ad35c42b
3 changed files with 36 additions and 15 deletions

View File

@ -131,7 +131,7 @@ class Taskbar : public waybar::AModule
Gtk::Box box_;
std::vector<TaskPtr> tasks_;
Glib::RefPtr<Gtk::IconTheme> icon_theme_;
std::vector<Glib::RefPtr<Gtk::IconTheme>> 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<Gtk::IconTheme> icon_theme() const;
std::vector<Glib::RefPtr<Gtk::IconTheme>> icon_themes() const;
};
} /* namespace waybar::modules::wlr */

View File

@ -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 ++

View File

@ -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<Gtk::IconTheme> Taskbar::icon_theme() const
std::vector<Glib::RefPtr<Gtk::IconTheme>> Taskbar::icon_themes() const
{
return icon_theme_;
return icon_themes_;
}
} /* namespace waybar::modules::wlr */