From b8da9ee94c7fffc6c46acd0295bacfc58836e678 Mon Sep 17 00:00:00 2001 From: Rio Sanjaya Date: Wed, 24 Dec 2025 07:45:06 +0700 Subject: [PATCH] Move extract icon into its own method --- include/AIconLabel.hpp | 1 + src/AIconLabel.cpp | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/AIconLabel.hpp b/include/AIconLabel.hpp index 78a4fcf3..2e0956e5 100644 --- a/include/AIconLabel.hpp +++ b/include/AIconLabel.hpp @@ -14,6 +14,7 @@ class AIconLabel : public ALabel { bool enable_click = false, bool enable_scroll = false); virtual ~AIconLabel() = default; auto update() -> void override; + static std::tuple extractIcon(const std::string& input); protected: Gtk::Image image_; diff --git a/src/AIconLabel.cpp b/src/AIconLabel.cpp index 7c5dc2cb..532ed433 100644 --- a/src/AIconLabel.cpp +++ b/src/AIconLabel.cpp @@ -57,39 +57,45 @@ AIconLabel::AIconLabel(const Json::Value &config, const std::string &name, const event_box_.add(box_); } -auto AIconLabel::update() -> void { - std::string iconName = ""; - labelContainsIcon = false; +std::tuple AIconLabel::extractIcon(const std::string& input) { + std::string iconResult = ""; + std::string labelResult = input; 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); + if (std::regex_search(input, iconMatch, iconSearch)) { + iconResult = 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; + labelResult = std::regex_replace(input, cleanLabelPattern, ""); } } catch (const std::exception& e) { 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 (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); + label_.set_markup(cleanLabel); + + if (iconLabel.front() == '/') { + 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(), image_.get_window()); image_.set(surface); image_.set_visible(true); } 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); } }