Move extract icon into its own method

This commit is contained in:
Rio Sanjaya
2025-12-24 08:43:03 +07:00
parent c0b9635e28
commit b8da9ee94c
2 changed files with 23 additions and 16 deletions
+1
View File
@@ -14,6 +14,7 @@ class AIconLabel : public ALabel {
bool enable_click = false, bool enable_scroll = false); bool enable_click = false, bool enable_scroll = false);
virtual ~AIconLabel() = default; virtual ~AIconLabel() = default;
auto update() -> void override; auto update() -> void override;
static std::tuple<std::string, std::string> extractIcon(const std::string& input);
protected: protected:
Gtk::Image image_; Gtk::Image image_;
+22 -16
View File
@@ -57,39 +57,45 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const
event_box_.add(box_); event_box_.add(box_);
} }
auto AIconLabel::update() -> void { std::tuple<std::string, std::string> AIconLabel::extractIcon(const std::string& input) {
std::string iconName = ""; std::string iconResult = "";
labelContainsIcon = false; std::string labelResult = input;
try { try {
std::string inputLabel = label_.get_label().c_str();
const std::regex iconSearch(R"((?=\\0icon\\1f).+?(?=\\n))"); const std::regex iconSearch(R"((?=\\0icon\\1f).+?(?=\\n))");
std::smatch iconMatch; std::smatch iconMatch;
if (std::regex_search(inputLabel, iconMatch, iconSearch)) { if (std::regex_search(input, iconMatch, iconSearch)) {
iconName = iconMatch[0].str().substr(9); iconResult = iconMatch[0].str().substr(9);
const std::regex cleanLabelPattern(R"(\\0icon\\1f.+?\\n)"); const std::regex cleanLabelPattern(R"(\\0icon\\1f.+?\\n)");
std::string labelResult = std::regex_replace(inputLabel, cleanLabelPattern, ""); labelResult = std::regex_replace(input, cleanLabelPattern, "");
label_.set_markup(labelResult);
labelContainsIcon = true;
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::warn("Error while parsing icon from label. {}", e.what()); spdlog::warn("Error while parsing icon from label. {}", e.what());
} }
return std::make_tuple(iconResult, labelResult);
}
auto AIconLabel::update() -> void {
labelContainsIcon = false;
auto [iconLabel, cleanLabel] = extractIcon(label_.get_label().c_str());
labelContainsIcon = iconLabel.length() > 0;
if (labelContainsIcon) { if (labelContainsIcon) {
if (iconName.front() == '/') { label_.set_markup(cleanLabel);
auto pixbuf = Gdk::Pixbuf::create_from_file(iconName);
int scaled_icon_size = 24 * image_.get_scale_factor(); if (iconLabel.front() == '/') {
pixbuf = Gdk::Pixbuf::create_from_file(iconName, scaled_icon_size, scaled_icon_size); auto pixbuf = Gdk::Pixbuf::create_from_file(iconLabel);
int scaled_icon_size = 6 * image_.get_scale_factor();
pixbuf = Gdk::Pixbuf::create_from_file(iconLabel, scaled_icon_size, scaled_icon_size);
auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image_.get_scale_factor(), auto surface = Gdk::Cairo::create_surface_from_pixbuf(pixbuf, image_.get_scale_factor(),
image_.get_window()); image_.get_window());
image_.set(surface); image_.set(surface);
image_.set_visible(true); image_.set_visible(true);
} else { } else {
image_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID); image_.set_from_icon_name(iconLabel, Gtk::ICON_SIZE_INVALID);
image_.set_visible(true); image_.set_visible(true);
} }
} }