Parse label_ to extract embedded icon markup
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <gdkmm/pixbuf.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
namespace waybar {
|
||||
|
||||
@@ -56,6 +58,42 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const
|
||||
}
|
||||
|
||||
auto AIconLabel::update() -> void {
|
||||
std::string iconName = "";
|
||||
labelContainsIcon = false;
|
||||
try {
|
||||
std::string inputLabel = label_.get_label().c_str();
|
||||
|
||||
const std::regex iconSearch(R"((?=\\0icon\\1f).+?(?=\\n))");
|
||||
std::smatch iconMatch;
|
||||
if (std::regex_search(inputLabel, iconMatch, iconSearch)) {
|
||||
iconName = iconMatch[0].str().substr(9);
|
||||
|
||||
const std::regex cleanLabelPattern(R"(\\0icon\\1f.+?\\n)");
|
||||
std::string labelResult = std::regex_replace(inputLabel, cleanLabelPattern, "");
|
||||
|
||||
label_.set_markup(labelResult);
|
||||
labelContainsIcon = true;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::warn("Error while parsing icon from label. {}", e.what());
|
||||
}
|
||||
|
||||
if (labelContainsIcon) {
|
||||
if (iconName.front() == '/') {
|
||||
auto pixbuf = Gdk::Pixbuf::create_from_file(iconName);
|
||||
int scaled_icon_size = 24 * image_.get_scale_factor();
|
||||
pixbuf = Gdk::Pixbuf::create_from_file(iconName, scaled_icon_size, scaled_icon_size);
|
||||
|
||||
auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image_.get_scale_factor(),
|
||||
image_.get_window());
|
||||
image_.set(surface);
|
||||
image_.set_visible(true);
|
||||
} else {
|
||||
image_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID);
|
||||
image_.set_visible(true);
|
||||
}
|
||||
}
|
||||
|
||||
image_.set_visible(image_.get_visible() && iconEnabled());
|
||||
ALabel::update();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user